blob: 83275fb92eb01c91ea4a487d7e5c5a8a3b71851a [file] [log] [blame]
Bill Wendlinga89e67d2009-05-10 23:14:38 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework --------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf info into asm files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/DwarfWriter.h"
Bill Wendlingbbee8c92009-05-15 00:11:17 +000015#include "DIE.h"
16#include "DwarfPrinter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/Module.h"
Devang Patelb3907da2009-01-05 23:03:32 +000018#include "llvm/DerivedTypes.h"
Devang Patel2da0cc42009-01-15 23:41:32 +000019#include "llvm/Constants.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/CodeGen/AsmPrinter.h"
21#include "llvm/CodeGen/MachineModuleInfo.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineLocation.h"
Devang Patelfc187162009-01-05 17:57:47 +000024#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Support/Debug.h"
26#include "llvm/Support/Dwarf.h"
27#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/DataTypes.h"
29#include "llvm/Support/Mangler.h"
Bill Wendlingcb3661f2009-03-10 20:41:52 +000030#include "llvm/Support/Timer.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000031#include "llvm/Support/raw_ostream.h"
Dan Gohman80bbde72007-09-24 21:32:18 +000032#include "llvm/System/Path.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000034#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Target/TargetData.h"
36#include "llvm/Target/TargetFrameInfo.h"
37#include "llvm/Target/TargetInstrInfo.h"
38#include "llvm/Target/TargetMachine.h"
39#include "llvm/Target/TargetOptions.h"
Evan Cheng3e288912009-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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044#include <ostream>
45#include <string>
46using namespace llvm;
47using namespace llvm::dwarf;
48
Devang Patelaa1e8432009-01-08 23:40:34 +000049static RegisterPass<DwarfWriter>
50X("dwarfwriter", "DWARF Information Writer");
51char DwarfWriter::ID = 0;
52
Bill Wendling148ecc42009-03-10 22:58:53 +000053static TimerGroup &getDwarfTimerGroup() {
54 static TimerGroup DwarfTimerGroup("Dwarf Exception and Debugging");
55 return DwarfTimerGroup;
Bill Wendlingcb3661f2009-03-10 20:41:52 +000056}
57
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058namespace llvm {
aslc200b112008-08-16 12:57:46 +000059
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060//===----------------------------------------------------------------------===//
61
62/// Configuration values for initial hash set sizes (log2).
63///
Bill Wendling824a8bf2009-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)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067
68//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069/// CompileUnit - This dwarf writer support class manages information associate
70/// with a source file.
71class CompileUnit {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 /// ID - File identifier for source.
73 ///
74 unsigned ID;
aslc200b112008-08-16 12:57:46 +000075
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 /// Die - Compile unit debug information entry.
77 ///
78 DIE *Die;
aslc200b112008-08-16 12:57:46 +000079
Devang Patel42f6bed2009-01-13 23:54:55 +000080 /// GVToDieMap - Tracks the mapping of unit level debug informaton
81 /// variables to debug information entries.
Devang Patel56b1d132009-01-20 00:58:55 +000082 std::map<GlobalVariable *, DIE *> GVToDieMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083
Bill Wendlingbbee8c92009-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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
88 /// Globals - A map of globally visible named entities for this unit.
89 ///
Bill Wendlinge06da442009-04-09 21:49:15 +000090 StringMap<DIE*> Globals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091
92 /// DiesSet - Used to uniquely define dies within the compile unit.
93 ///
94 FoldingSet<DIE> DiesSet;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095public:
Devang Patelb3907da2009-01-05 23:03:32 +000096 CompileUnit(unsigned I, DIE *D)
Devang Patel42f6bed2009-01-13 23:54:55 +000097 : ID(I), Die(D), GVToDieMap(),
Bill Wendlingbbee8c92009-05-15 00:11:17 +000098 GVToDIEEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Patelb3907da2009-01-05 23:03:32 +000099 {}
100
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 ~CompileUnit() {
102 delete Die;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 }
aslc200b112008-08-16 12:57:46 +0000104
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 // Accessors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 unsigned getID() const { return ID; }
107 DIE* getDie() const { return Die; }
Bill Wendlinge06da442009-04-09 21:49:15 +0000108 StringMap<DIE*> &getGlobals() { return Globals; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109
110 /// hasContent - Return true if this compile unit has something to write out.
111 ///
112 bool hasContent() const {
113 return !Die->getChildren().empty();
114 }
115
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 }
aslc200b112008-08-16 12:57:46 +0000121
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000123 /// specified debug variable.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000124 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
125 return GVToDieMap[GV];
126 }
aslc200b112008-08-16 12:57:46 +0000127
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000128 /// getDIEEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000129 /// specified debug variable.
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000130 DIEEntry *&getDIEEntrySlotFor(GlobalVariable *GV) {
131 return GVToDIEEntryMap[GV];
Devang Patel4a4cbe72009-01-05 21:47:57 +0000132 }
aslc200b112008-08-16 12:57:46 +0000133
Dan Gohmanf17a25c2007-07-18 16:29:46 +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);
aslc200b112008-08-16 12:57:46 +0000141
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 if (!Die) {
143 Die = new DIE(Buffer);
144 DiesSet.InsertNode(Die, Where);
145 this->Die->AddChild(Die);
146 Buffer.Detach();
147 }
aslc200b112008-08-16 12:57:46 +0000148
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 return Die;
150 }
151};
152
153//===----------------------------------------------------------------------===//
Devang Patel35a078f2009-01-12 22:54:42 +0000154/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +0000155///
156class SrcLineInfo {
Bill Wendlingbbee8c92009-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 Patel7dd15a92009-01-08 17:19:22 +0000161public:
162 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendling824a8bf2009-02-03 21:17:20 +0000163 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +0000164
Devang Patel7dd15a92009-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 Patel7dd15a92009-01-08 17:19:22 +0000172//===----------------------------------------------------------------------===//
Devang Patel4d1709e2009-01-08 02:33:41 +0000173/// DbgVariable - This class is used to track local variable information.
174///
175class DbgVariable {
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000176 DIVariable Var; // Variable Descriptor.
Dan Gohmand46dc022009-05-07 19:46:24 +0000177 unsigned FrameIndex; // Variable frame index.
Devang Patel4d1709e2009-01-08 02:33:41 +0000178public:
Devang Patel7c8a2772009-01-16 19:28:14 +0000179 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel4d1709e2009-01-08 02:33:41 +0000180
181 // Accessors.
Devang Patel7c8a2772009-01-16 19:28:14 +0000182 DIVariable getVariable() const { return Var; }
Devang Patel4d1709e2009-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 Wendlinga89e67d2009-05-10 23:14:38 +0000189class DbgConcreteScope;
Devang Patel4d1709e2009-01-08 02:33:41 +0000190class DbgScope {
Devang Patel4d1709e2009-01-08 02:33:41 +0000191 DbgScope *Parent; // Parent to this scope.
Devang Patel49a3bd92009-01-16 18:01:58 +0000192 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel4d1709e2009-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 Patel49a3bd92009-01-16 18:01:58 +0000196 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel63c22f42009-01-10 02:42:49 +0000197 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Bill Wendlinga89e67d2009-05-10 23:14:38 +0000198 SmallVector<DbgConcreteScope *, 8> ConcreteInsts;// Concrete insts of funcs.
Devang Patel4d1709e2009-01-08 02:33:41 +0000199public:
Devang Patel2560d922009-01-15 18:25:17 +0000200 DbgScope(DbgScope *P, DIDescriptor D)
Bill Wendlinga89e67d2009-05-10 23:14:38 +0000201 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0) {}
202 virtual ~DbgScope();
Devang Patel4d1709e2009-01-08 02:33:41 +0000203
204 // Accessors.
Devang Patel49a3bd92009-01-16 18:01:58 +0000205 DbgScope *getParent() const { return Parent; }
206 DIDescriptor getDesc() const { return Desc; }
Devang Patel4d1709e2009-01-08 02:33:41 +0000207 unsigned getStartLabelID() const { return StartLabelID; }
208 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel63c22f42009-01-10 02:42:49 +0000209 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
210 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendlinga89e67d2009-05-10 23:14:38 +0000211 SmallVector<DbgConcreteScope*,8> &getConcreteInsts() { return ConcreteInsts; }
Devang Patel4d1709e2009-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 Patel8a9a7dc2009-04-15 00:10:26 +0000222
Bill Wendlinga89e67d2009-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 Wendling74940d12009-05-06 21:21:34 +0000226
227#ifndef NDEBUG
228 void dump() const;
229#endif
Devang Patel8a9a7dc2009-04-15 00:10:26 +0000230};
231
Bill Wendling74940d12009-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 Patel8a9a7dc2009-04-15 00:10:26 +0000249
250//===----------------------------------------------------------------------===//
Bill Wendlinga89e67d2009-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 Patel8a9a7dc2009-04-15 00:10:26 +0000257public:
Bill Wendlinga89e67d2009-05-10 23:14:38 +0000258 DbgConcreteScope(DIDescriptor D) : DbgScope(NULL, D) {}
Devang Patel8a9a7dc2009-04-15 00:10:26 +0000259
Bill Wendlinga89e67d2009-05-10 23:14:38 +0000260 // Accessors.
261 DIE *getDie() const { return Die; }
262 void setDie(DIE *D) { Die = D; }
Devang Patel4d1709e2009-01-08 02:33:41 +0000263};
264
Bill Wendlinga89e67d2009-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 Patel4d1709e2009-01-08 02:33:41 +0000274//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +0000275/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276///
277class DwarfDebug : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 //===--------------------------------------------------------------------===//
279 // Attributes used to construct specific Dwarf sections.
280 //
aslc200b112008-08-16 12:57:46 +0000281
Evan Cheng3e288912009-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;
aslc200b112008-08-16 12:57:46 +0000289
Devang Patel2ae1db52009-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 Wendlinge0f3a262009-02-20 20:40:28 +0000293
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 /// AbbreviationsSet - Used to uniquely define abbreviations.
295 ///
296 FoldingSet<DIEAbbrev> AbbreviationsSet;
297
298 /// Abbreviations - A list of all the unique abbreviations in use.
299 ///
300 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +0000301
Evan Cheng3e288912009-02-25 07:04:34 +0000302 /// DirectoryIdMap - Directory name to directory id map.
303 ///
304 StringMap<unsigned> DirectoryIdMap;
Devang Patel5f244e32009-01-05 22:35:52 +0000305
Evan Cheng3e288912009-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 Patel5f244e32009-01-05 22:35:52 +0000323
Devang Patel9b829452009-01-16 21:07:53 +0000324 /// Lines - List of of source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +0000325 std::vector<SrcLineInfo> Lines;
326
Devang Patel9b829452009-01-16 21:07:53 +0000327 /// ValuesSet - Used to uniquely define values.
328 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +0000330
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 /// Values - A list of all the unique values in use.
332 ///
333 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +0000334
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 /// StringPool - A UniqueVector of strings used by indirect references.
336 ///
337 UniqueVector<std::string> StringPool;
338
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 /// SectionMap - Provides a unique id per text section.
340 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +0000341 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +0000342
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 /// SectionSourceLines - Tracks line numbers per text section.
344 ///
Devang Patel35a078f2009-01-12 22:54:42 +0000345 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346
347 /// didInitial - Flag to indicate if initial emission has been done.
348 ///
349 bool didInitial;
aslc200b112008-08-16 12:57:46 +0000350
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 /// shouldEmit - Flag to indicate if debug information should be emitted.
352 ///
353 bool shouldEmit;
354
Devang Patel7b60d552009-04-15 20:41:31 +0000355 // FunctionDbgScope - Top level scope for the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +0000356 //
Devang Patel7b60d552009-04-15 20:41:31 +0000357 DbgScope *FunctionDbgScope;
Devang Patel4d1709e2009-01-08 02:33:41 +0000358
Bill Wendlingd9308a62009-03-10 21:23:25 +0000359 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +0000360 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendlingd9308a62009-03-10 21:23:25 +0000361
Bill Wendling66e789e2009-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 Wendlinga89e67d2009-05-10 23:14:38 +0000368 DenseMap<GlobalVariable *,
Bill Wendling66e789e2009-05-13 23:55:49 +0000369 SmallVector<DbgScope *, 8> > DbgConcreteScopeMap;
Devang Patel8a9a7dc2009-04-15 00:10:26 +0000370
Bill Wendlinga89e67d2009-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 Patel88bf96e2009-04-13 17:02:03 +0000373 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
374
Devang Patel8a9a7dc2009-04-15 00:10:26 +0000375 /// InlinedVariableScopes - Scopes information for the inlined subroutine
376 /// variables.
377 DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
378
Bill Wendlinga89e67d2009-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 Wendlingf7e62552009-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 Wendlingd9308a62009-03-10 21:23:25 +0000398 /// DebugTimer - Timer for the Dwarf debug writer.
399 Timer *DebugTimer;
Devang Patel4d1709e2009-01-08 02:33:41 +0000400
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 struct FunctionDebugFrameInfo {
402 unsigned Number;
403 std::vector<MachineMove> Moves;
404
405 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000406 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 };
408
409 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +0000410
Bill Wendlingd9308a62009-03-10 21:23:25 +0000411private:
Bill Wendlingdf25fd62009-03-10 21:59:25 +0000412 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendling278a3922009-03-10 21:47:45 +0000413 /// maps to the source id. Source id starts at 1.
414 std::pair<unsigned, unsigned>
Bill Wendlingdf25fd62009-03-10 21:59:25 +0000415 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendling278a3922009-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 Wendling278a3922009-03-10 21:47:45 +0000438 unsigned getNumSourceIds() const {
439 return SourceIds.size();
440 }
441
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +0000443 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
445 // Profile the node so that we can make it unique.
446 FoldingSetNodeID ID;
447 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +0000448
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 // Check the set for priors.
450 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +0000451
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 // If it's newly added.
453 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +0000454 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
464 /// NewString - Add a string to the constant pool and returns a label.
465 ///
466 DWLabel NewString(const std::string &String) {
467 unsigned StringID = StringPool.insert(String);
468 return DWLabel("string", StringID);
469 }
aslc200b112008-08-16 12:57:46 +0000470
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000471 /// NewDIEEntry - Creates a new DIEEntry to be a proxy for a debug information
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472 /// entry.
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000473 DIEEntry *NewDIEEntry(DIE *Entry = NULL) {
474 DIEEntry *Value;
aslc200b112008-08-16 12:57:46 +0000475
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476 if (Entry) {
477 FoldingSetNodeID ID;
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000478 DIEEntry::Profile(ID, Entry);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 void *Where;
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000480 Value = static_cast<DIEEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +0000481
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +0000483
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000484 Value = new DIEEntry(Entry);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 ValuesSet.InsertNode(Value, Where);
486 } else {
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000487 Value = new DIEEntry(Entry);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 }
aslc200b112008-08-16 12:57:46 +0000489
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 Values.push_back(Value);
491 return Value;
492 }
aslc200b112008-08-16 12:57:46 +0000493
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000494 /// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000495 ///
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000496 void SetDIEEntry(DIEEntry *Value, DIE *Entry) {
Bill Wendling15afa002009-03-10 23:57:09 +0000497 Value->setEntry(Entry);
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
509 DIEInteger::Profile(ID, Integer);
510 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);
516 }
aslc200b112008-08-16 12:57:46 +0000517
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 Die->AddValue(Attribute, Form, Value);
519 }
aslc200b112008-08-16 12:57:46 +0000520
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
527 DIEInteger::Profile(ID, (uint64_t)Integer);
528 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);
534 }
aslc200b112008-08-16 12:57:46 +0000535
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 Die->AddValue(Attribute, Form, Value);
537 }
aslc200b112008-08-16 12:57:46 +0000538
Evan Cheng3e288912009-02-25 07:04:34 +0000539 /// AddString - Add a string attribute data and value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000540 ///
541 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
542 const std::string &String) {
543 FoldingSetNodeID ID;
544 DIEString::Profile(ID, String);
545 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);
551 }
aslc200b112008-08-16 12:57:46 +0000552
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 Die->AddValue(Attribute, Form, Value);
554 }
aslc200b112008-08-16 12:57:46 +0000555
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 /// AddLabel - Add a Dwarf label attribute data and value.
557 ///
558 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000559 const DWLabel &Label) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 FoldingSetNodeID ID;
561 DIEDwarfLabel::Profile(ID, Label);
562 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);
568 }
aslc200b112008-08-16 12:57:46 +0000569
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 Die->AddValue(Attribute, Form, Value);
571 }
aslc200b112008-08-16 12:57:46 +0000572
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
578 DIEObjectLabel::Profile(ID, Label);
579 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);
585 }
aslc200b112008-08-16 12:57:46 +0000586
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 Die->AddValue(Attribute, Form, Value);
588 }
aslc200b112008-08-16 12:57:46 +0000589
Argiris Kirtzidis03449652008-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 }
aslc200b112008-08-16 12:57:46 +0000604
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000605 Die->AddValue(Attribute, Form, Value);
606 }
aslc200b112008-08-16 12:57:46 +0000607
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608 /// AddDelta - Add a label delta attribute data and value.
609 ///
610 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patel8a9a7dc2009-04-15 00:10:26 +0000611 const DWLabel &Hi, const DWLabel &Lo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 FoldingSetNodeID ID;
613 DIEDelta::Profile(ID, Hi, Lo);
614 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);
620 }
aslc200b112008-08-16 12:57:46 +0000621
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622 Die->AddValue(Attribute, Form, Value);
623 }
aslc200b112008-08-16 12:57:46 +0000624
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000625 /// AddDIEEntry - Add a DIE attribute data and value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 ///
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000627 void AddDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
628 Die->AddValue(Attribute, Form, NewDIEEntry(Entry));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 }
630
631 /// AddBlock - Add block data.
632 ///
633 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000634 Block->ComputeSize(TD);
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Lattner3de66892007-09-21 18:25:53 +0000644 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +0000646 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000647 }
aslc200b112008-08-16 12:57:46 +0000648
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 Die->AddValue(Attribute, Block->BestForm(), Value);
650 }
651
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652 /// AddSourceLine - Add location information to specified debug information
653 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +0000654 void AddSourceLine(DIE *Die, const DIVariable *V) {
Chris Lattner88ab9742009-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 Patel4d1709e2009-01-08 02:33:41 +0000659 unsigned Line = V->getLineNumber();
Chris Lattner88ab9742009-05-05 04:55:56 +0000660 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
661 assert(FileID && "Invalid file id");
Devang Patel4d1709e2009-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 Patel7c8a2772009-01-16 19:28:14 +0000668 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Chris Lattner88ab9742009-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 Patel5f244e32009-01-05 22:35:52 +0000672 unsigned Line = G->getLineNumber();
Chris Lattner88ab9742009-05-05 04:55:56 +0000673 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
674 assert(FileID && "Invalid file id");
Devang Patel5f244e32009-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 Patel7c8a2772009-01-16 19:28:14 +0000679 void AddSourceLine(DIE *Die, const DIType *Ty) {
Chris Lattner88ab9742009-05-05 04:55:56 +0000680 // If there is no compile unit specified, don't add a line #.
Devang Patel2ae1db52009-01-30 18:20:31 +0000681 DICompileUnit CU = Ty->getCompileUnit();
682 if (CU.isNull())
683 return;
Chris Lattner88ab9742009-05-05 04:55:56 +0000684
685 unsigned Line = Ty->getLineNumber();
686 unsigned FileID = FindCompileUnit(CU).getID();
687 assert(FileID && "Invalid file id");
Devang Patel5f244e32009-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 /// AddAddress - Add an address attribute to a die based on the location
693 /// provided.
694 void AddAddress(DIE *Die, unsigned Attribute,
Devang Patel8a9a7dc2009-04-15 00:10:26 +0000695 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000696 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +0000698
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000699 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 }
aslc200b112008-08-16 12:57:46 +0000715
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716 AddBlock(Die, Attribute, 0, Block);
717 }
aslc200b112008-08-16 12:57:46 +0000718
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719 /// AddType - Add a new type attribute to the specified entity.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000720 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel165ed512009-01-23 19:13:31 +0000721 if (Ty.isNull())
Devang Patel4a4cbe72009-01-05 21:47:57 +0000722 return;
Devang Patel4a4cbe72009-01-05 21:47:57 +0000723
724 // Check for pre-existence.
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000725 DIEEntry *&Slot = DW_Unit->getDIEEntrySlotFor(Ty.getGV());
Devang Patel4a4cbe72009-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 Wendlingbbee8c92009-05-15 00:11:17 +0000733 Slot = NewDIEEntry();
Devang Patel4a4cbe72009-01-05 21:47:57 +0000734
735 // Construct type.
736 DIE Buffer(DW_TAG_base_type);
Devang Patelef4bf3b2009-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 Wendling824a8bf2009-02-03 21:17:20 +0000742 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelef4bf3b2009-01-15 19:26:23 +0000743 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
744 }
745
Devang Patelb0cb07c2009-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 Wendlingbbee8c92009-05-15 00:11:17 +0000756 SetDIEEntry(Slot, Child);
Bill Wendling824a8bf2009-02-03 21:17:20 +0000757 } else {
Devang Patelb0cb07c2009-01-27 23:22:55 +0000758 Die = DW_Unit->AddDie(Buffer);
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000759 SetDIEEntry(Slot, Die);
Devang Patelb0cb07c2009-01-27 23:22:55 +0000760 }
761
Devang Patel4a4cbe72009-01-05 21:47:57 +0000762 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
763 }
764
Devang Patel46d13752009-01-05 19:07:53 +0000765 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
766 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +0000767 DIBasicType BTy) {
Bill Wendlingf3f16e82009-03-13 04:39:26 +0000768
Devang Patelfc187162009-01-05 17:57:47 +0000769 // Get core information.
Bill Wendlingf3f16e82009-03-13 04:39:26 +0000770 std::string Name;
771 BTy.getName(Name);
Devang Patelfc187162009-01-05 17:57:47 +0000772 Buffer.setTag(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +0000773 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patelfc187162009-01-05 17:57:47 +0000774 // Add name if not anonymous or intermediate type.
Bill Wendlingf3f16e82009-03-13 04:39:26 +0000775 if (!Name.empty())
Devang Patelfc187162009-01-05 17:57:47 +0000776 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +0000777 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelfc187162009-01-05 17:57:47 +0000778 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
779 }
780
Devang Patel46d13752009-01-05 19:07:53 +0000781 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
782 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +0000783 DIDerivedType DTy) {
Bill Wendlingf3f16e82009-03-13 04:39:26 +0000784
Devang Patelfc187162009-01-05 17:57:47 +0000785 // Get core information.
Bill Wendlingf3f16e82009-03-13 04:39:26 +0000786 std::string Name;
787 DTy.getName(Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +0000788 uint64_t Size = DTy.getSizeInBits() >> 3;
789 unsigned Tag = DTy.getTag();
Bill Wendling1c5842b2009-03-09 05:04:40 +0000790
Devang Patelfc187162009-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 Wendling1c5842b2009-03-09 05:04:40 +0000795
Devang Patelfc187162009-01-05 17:57:47 +0000796 // Map to main type, void will not have a type.
Devang Patelef4bf3b2009-01-15 19:26:23 +0000797 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +0000798 AddType(DW_Unit, &Buffer, FromTy);
Devang Patelfc187162009-01-05 17:57:47 +0000799
800 // Add name if not anonymous or intermediate type.
Bill Wendlingf3f16e82009-03-13 04:39:26 +0000801 if (!Name.empty())
Evan Cheng3e288912009-02-25 07:04:34 +0000802 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelfc187162009-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 Patele34e0882009-01-27 00:45:04 +0000810 if (!DTy.isForwardDecl())
811 AddSourceLine(&Buffer, &DTy);
Devang Patelfc187162009-01-05 17:57:47 +0000812 }
813
Devang Patel30c01372009-01-05 19:55:51 +0000814 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
815 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +0000816 DICompositeType CTy) {
Devang Patelb28de842009-01-17 08:01:33 +0000817 // Get core information.
Bill Wendlingf3f16e82009-03-13 04:39:26 +0000818 std::string Name;
819 CTy.getName(Name);
Bill Wendling1c5842b2009-03-09 05:04:40 +0000820
Devang Patelef4bf3b2009-01-15 19:26:23 +0000821 uint64_t Size = CTy.getSizeInBits() >> 3;
822 unsigned Tag = CTy.getTag();
Devang Patel8050bd72009-01-23 01:19:09 +0000823 Buffer.setTag(Tag);
Bill Wendling1c5842b2009-03-09 05:04:40 +0000824
Devang Patel30c01372009-01-05 19:55:51 +0000825 switch (Tag) {
826 case DW_TAG_vector_type:
827 case DW_TAG_array_type:
Devang Patelef4bf3b2009-01-15 19:26:23 +0000828 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +0000829 break;
Devang Patel3798f492009-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 Patel30c01372009-01-05 19:55:51 +0000842 case DW_TAG_subroutine_type:
843 {
Devang Patel30c01372009-01-05 19:55:51 +0000844 // Add return type.
Bill Wendling74940d12009-05-06 21:21:34 +0000845 DIArray Elements = CTy.getTypeArray();
Devang Patel4a4cbe72009-01-05 21:47:57 +0000846 DIDescriptor RTy = Elements.getElement(0);
Devang Patelef4bf3b2009-01-15 19:26:23 +0000847 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel4a4cbe72009-01-05 21:47:57 +0000848
Bill Wendling74940d12009-05-06 21:21:34 +0000849 // Add prototype flag.
850 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
851
Devang Patel30c01372009-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 Patel4a4cbe72009-01-05 21:47:57 +0000855 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld40a7e52009-01-17 06:57:25 +0000856 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patel30c01372009-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 Patel09353602009-03-25 00:28:40 +0000863 case DW_TAG_class_type:
Devang Patel30c01372009-01-05 19:55:51 +0000864 {
865 // Add elements to structure type.
Devang Patelef4bf3b2009-01-15 19:26:23 +0000866 DIArray Elements = CTy.getTypeArray();
Devang Patelcf7acb12009-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 Patel30c01372009-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 Patelb28de842009-01-17 08:01:33 +0000875 DIE *ElemDie = NULL;
Devang Patelef4bf3b2009-01-15 19:26:23 +0000876 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel245446c2009-01-17 08:05:14 +0000877 ElemDie = CreateSubprogramDIE(DW_Unit,
878 DISubprogram(Element.getGV()));
Bill Wendlinga89e67d2009-05-10 23:14:38 +0000879 else if (Element.getTag() == dwarf::DW_TAG_variable) // ??
Devang Patelb28de842009-01-17 08:01:33 +0000880 ElemDie = CreateGlobalVariableDIE(DW_Unit,
881 DIGlobalVariable(Element.getGV()));
Devang Patel5c643892009-01-20 21:02:02 +0000882 else
883 ElemDie = CreateMemberDIE(DW_Unit,
884 DIDerivedType(Element.getGV()));
Devang Patel245446c2009-01-17 08:05:14 +0000885 Buffer.AddChild(ElemDie);
Devang Patel30c01372009-01-05 19:55:51 +0000886 }
Mike Stumpae6cc682009-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 Patel74193d72009-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 Patel30c01372009-01-05 19:55:51 +0000897 }
898 break;
899 default:
900 break;
901 }
902
903 // Add name if not anonymous or intermediate type.
Bill Wendlingf3f16e82009-03-13 04:39:26 +0000904 if (!Name.empty())
Evan Cheng3e288912009-02-25 07:04:34 +0000905 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel30c01372009-01-05 19:55:51 +0000906
Devang Patele34e0882009-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 Patel30c01372009-01-05 19:55:51 +0000923 }
Devang Patel30c01372009-01-05 19:55:51 +0000924 }
925
Bill Wendling824a8bf2009-02-03 21:17:20 +0000926 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
927 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelef4bf3b2009-01-15 19:26:23 +0000928 int64_t L = SR.getLo();
929 int64_t H = SR.getHi();
Devang Patel6fb54132009-01-05 18:33:01 +0000930 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
931 if (L != H) {
Bill Wendlingbbee8c92009-05-15 00:11:17 +0000932 AddDIEEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +0000933 if (L)
Devang Patel245446c2009-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 Patel6fb54132009-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 Patel6ab30e52009-01-28 21:08:20 +0000947 // Emit derived type.
948 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel6fb54132009-01-05 18:33:01 +0000949 DIArray Elements = CTy->getTypeArray();
Devang Patel6fb54132009-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 Patel30c01372009-01-05 19:55:51 +0000959 DIDescriptor Element = Elements.getElement(i);
Devang Patelef4bf3b2009-01-15 19:26:23 +0000960 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
961 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +0000962 }
963 }
964
Bill Wendling824a8bf2009-02-03 21:17:20 +0000965 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3798f492009-01-20 18:35:14 +0000966 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +0000967
968 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendlingf3f16e82009-03-13 04:39:26 +0000969 std::string Name;
970 ETy->getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +0000971 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patela566e812009-01-05 18:38:38 +0000972 int64_t Value = ETy->getEnumValue();
973 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Patel3798f492009-01-20 18:35:14 +0000974 return Enumerator;
Devang Patela566e812009-01-05 18:38:38 +0000975 }
Devang Patel6fb54132009-01-05 18:33:01 +0000976
Devang Patelb28de842009-01-17 08:01:33 +0000977 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendling824a8bf2009-02-03 21:17:20 +0000978 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patelb28de842009-01-17 08:01:33 +0000979 {
980 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendlingf3f16e82009-03-13 04:39:26 +0000981 std::string Name;
982 GV.getDisplayName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +0000983 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendlingf3f16e82009-03-13 04:39:26 +0000984 std::string LinkageName;
985 GV.getLinkageName(LinkageName);
986 if (!LinkageName.empty())
Devang Patelb28de842009-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 Patel526b01d2009-01-05 18:59:44 +0000993 }
994
Devang Patel5c643892009-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 Wendlingf3f16e82009-03-13 04:39:26 +0000998 std::string Name;
999 DT.getName(Name);
1000 if (!Name.empty())
Devang Patel5c643892009-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 Patelf1f30d42009-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 Patel5c643892009-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 Patel2e7ee192009-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 Patel5c643892009-01-20 21:02:02 +00001035 return MemberDie;
1036 }
1037
Devang Patelb28de842009-01-17 08:01:33 +00001038 /// CreateSubprogramDIE - Create new DIE using SP.
1039 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Bill Wendling74940d12009-05-06 21:21:34 +00001040 const DISubprogram &SP,
Devang Patel245446c2009-01-17 08:05:14 +00001041 bool IsConstructor = false) {
Devang Patelb28de842009-01-17 08:01:33 +00001042 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling74940d12009-05-06 21:21:34 +00001043
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001044 std::string Name;
1045 SP.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001046 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling74940d12009-05-06 21:21:34 +00001047
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001048 std::string LinkageName;
1049 SP.getLinkageName(LinkageName);
Bill Wendling74940d12009-05-06 21:21:34 +00001050
Bill Wendlingf3f16e82009-03-13 04:39:26 +00001051 if (!LinkageName.empty())
Bill Wendling74940d12009-05-06 21:21:34 +00001052 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1053
Devang Patelb28de842009-01-17 08:01:33 +00001054 AddSourceLine(SPDie, &SP);
Devang Patel526b01d2009-01-05 18:59:44 +00001055
Devang Patelb28de842009-01-17 08:01:33 +00001056 DICompositeType SPTy = SP.getType();
1057 DIArray Args = SPTy.getTypeArray();
Bill Wendling74940d12009-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 Patelb28de842009-01-17 08:01:33 +00001063
Devang Patel526b01d2009-01-05 18:59:44 +00001064 // Add Return Type.
Devang Patel6e199962009-04-08 22:18:45 +00001065 unsigned SPTag = SPTy.getTag();
Devang Patel688a19f2009-02-27 18:05:21 +00001066 if (!IsConstructor) {
Devang Patel6e199962009-04-08 22:18:45 +00001067 if (Args.isNull() || SPTag != DW_TAG_subroutine_type)
Devang Patel688a19f2009-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 Patel922d1592009-01-30 01:21:46 +00001072
Devang Patelace2cf62009-02-02 17:51:41 +00001073 if (!SP.isDefinition()) {
1074 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
Bill Wendling74940d12009-05-06 21:21:34 +00001075 // Add arguments. Do not add arguments for subprogram definition. They
1076 // will be handled through RecordVariable.
Devang Patel6e199962009-04-08 22:18:45 +00001077 if (SPTag == DW_TAG_subroutine_type)
Devang Patelace2cf62009-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 Wendling74940d12009-05-06 21:21:34 +00001081 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ??
Devang Patelace2cf62009-02-02 17:51:41 +00001082 SPDie->AddChild(Arg);
1083 }
1084 }
Devang Patel922d1592009-01-30 01:21:46 +00001085
Devang Patelef4bf3b2009-01-15 19:26:23 +00001086 if (!SP.isLocalToUnit())
Devang Patel922d1592009-01-30 01:21:46 +00001087 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Bill Wendling74940d12009-05-06 21:21:34 +00001088
Devang Patel8a9a7dc2009-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 Patelb28de842009-01-17 08:01:33 +00001092 return SPDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001093 }
1094
Devang Patelb28de842009-01-17 08:01:33 +00001095 /// FindCompileUnit - Get the compile unit for the given descriptor.
1096 ///
Chris Lattner88ab9742009-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 Patel5f244e32009-01-05 22:35:52 +00001102 }
1103
Devang Patel42f6bed2009-01-13 23:54:55 +00001104 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel4d1709e2009-01-08 02:33:41 +00001105 ///
1106 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1107 // Get the descriptor.
Devang Patel7c8a2772009-01-16 19:28:14 +00001108 const DIVariable &VD = DV->getVariable();
Devang Patel4d1709e2009-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 Patel7c8a2772009-01-16 19:28:14 +00001113 switch (VD.getTag()) {
Devang Patel4d1709e2009-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 Wendlingf3f16e82009-03-13 04:39:26 +00001122 std::string Name;
1123 VD.getName(Name);
Evan Cheng3e288912009-02-25 07:04:34 +00001124 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel4d1709e2009-01-08 02:33:41 +00001125
1126 // Add source line info if available.
Devang Patel7c8a2772009-01-16 19:28:14 +00001127 AddSourceLine(VariableDie, &VD);
Devang Patel4d1709e2009-01-08 02:33:41 +00001128
1129 // Add variable type.
Devang Patel7c8a2772009-01-16 19:28:14 +00001130 AddType(Unit, VariableDie, VD.getType());
Devang Patel4d1709e2009-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 Patel4d1709e2009-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 Wendlinge0f3a262009-02-20 20:40:28 +00001145 if (Slot) return Slot;
1146
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001147 DbgScope *Parent = NULL;
1148 DIBlock Block(V);
Bill Wendling74940d12009-05-06 21:21:34 +00001149
Bill Wendlinga89e67d2009-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 Patel8a9a7dc2009-04-15 00:10:26 +00001157 if (!Block.isNull()) {
1158 DIDescriptor ParentDesc = Block.getContext();
1159 Parent =
1160 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00001161 }
Bill Wendling74940d12009-05-06 21:21:34 +00001162
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001163 Slot = new DbgScope(Parent, DIDescriptor(V));
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001164
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001165 if (Parent)
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001166 Parent->AddScope(Slot);
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001167 else
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001168 // First function is top level function.
Devang Patel7b60d552009-04-15 20:41:31 +00001169 FunctionDbgScope = Slot;
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001170
Devang Patel4d1709e2009-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 Gohmand46dc022009-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 Patel4d1709e2009-01-08 02:33:41 +00001184 }
1185
Bill Wendlinga89e67d2009-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 Patel4d1709e2009-01-08 02:33:41 +00001213 // Add nested scopes.
Devang Patel63c22f42009-01-10 02:42:49 +00001214 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel4d1709e2009-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 Patel4d1709e2009-01-08 02:33:41 +00001218
Devang Patelb9224922009-01-12 18:41:00 +00001219 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1220 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel4d1709e2009-01-08 02:33:41 +00001221
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001222 // Ignore empty scopes.
Devang Patel4d1709e2009-01-08 02:33:41 +00001223 if (StartID == EndID && StartID != 0) continue;
Bill Wendlinga89e67d2009-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 Patel88bf96e2009-04-13 17:02:03 +00001229 continue;
Devang Patel4d1709e2009-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 Wendlinga89e67d2009-05-10 23:14:38 +00001235 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
Bill Wendling821048d2009-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 Wendlinga89e67d2009-05-10 23:14:38 +00001252 // Add the scope's contents.
Bill Wendling821048d2009-05-01 08:25:13 +00001253 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
1254 ParentDie->AddChild(ScopeDie);
Devang Patel4d1709e2009-01-08 02:33:41 +00001255 }
1256 }
1257 }
1258
Devang Patel7b60d552009-04-15 20:41:31 +00001259 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
Devang Patel4d1709e2009-01-08 02:33:41 +00001260 ///
Devang Patel7b60d552009-04-15 20:41:31 +00001261 void ConstructFunctionDbgScope(DbgScope *RootScope) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001262 // Exit if there is no root scope.
1263 if (!RootScope) return;
Devang Patel2560d922009-01-15 18:25:17 +00001264 DIDescriptor Desc = RootScope->getDesc();
1265 if (Desc.isNull())
1266 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00001267
1268 // Get the subprogram debug information entry.
Devang Patel2560d922009-01-15 18:25:17 +00001269 DISubprogram SPD(Desc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00001270
1271 // Get the compile unit context.
Devang Patel2ae1db52009-01-30 18:20:31 +00001272 CompileUnit *Unit = MainCU;
1273 if (!Unit)
Chris Lattner88ab9742009-05-05 04:55:56 +00001274 Unit = &FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00001275
1276 // Get the subprogram die.
Devang Patel57ec9ac2009-01-12 22:58:14 +00001277 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel4d1709e2009-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 Wendlinga89e67d2009-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 Patel4d1709e2009-01-08 02:33:41 +00001317 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
1318 ///
1319 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Cheng3e288912009-02-25 07:04:34 +00001320 const char *FnName = MF->getFunction()->getNameStart();
1321 if (MainCU) {
Bill Wendlinge06da442009-04-09 21:49:15 +00001322 StringMap<DIE*> &Globals = MainCU->getGlobals();
1323 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Cheng3e288912009-02-25 07:04:34 +00001324 if (GI != Globals.end()) {
1325 DIE *SPDie = GI->second;
Devang Patel4d1709e2009-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 Cheng3e288912009-02-25 07:04:34 +00001337 } else {
1338 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
1339 CompileUnit *Unit = CompileUnits[i];
Bill Wendlinge06da442009-04-09 21:49:15 +00001340 StringMap<DIE*> &Globals = Unit->getGlobals();
1341 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Cheng3e288912009-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 Patel4d1709e2009-01-08 02:33:41 +00001356 }
Evan Cheng3e288912009-02-25 07:04:34 +00001357
Devang Patel4d1709e2009-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
aslc200b112008-08-16 12:57:46 +00001371
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001372 // Dwarf sections base addresses.
1373 if (TAI->doesDwarfRequireFrameSection()) {
1374 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1375 EmitLabel("section_debug_frame", 0);
1376 }
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 Michel79f01f52009-01-26 22:32:51 +00001383 if (TAI->doesSupportMacInfoSection()) {
1384 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
1385 EmitLabel("section_macinfo", 0);
1386 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Korobeynikov55b94962008-09-24 22:15:21 +00001398 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001399 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00001400 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401 EmitLabel("data_begin", 0);
1402 }
1403
1404 /// EmitDIE - Recusively Emits a debug information entry.
1405 ///
1406 void EmitDIE(DIE *Die) {
1407 // Get the abbreviation for this DIE.
1408 unsigned AbbrevNumber = Die->getAbbrevNumber();
1409 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00001410
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001411 Asm->EOL();
1412
1413 // Emit the code (index) for the abbreviation.
1414 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00001415
Evan Cheng42ceb472009-03-25 01:47:28 +00001416 if (Asm->isVerbose())
Evan Cheng0eeed442008-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();
aslc200b112008-08-16 12:57:46 +00001424
Owen Anderson88dd6232008-06-24 21:44:59 +00001425 SmallVector<DIEValue*, 32> &Values = Die->getValues();
1426 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00001427
Dan Gohmanf17a25c2007-07-18 16:29:46 +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)");
aslc200b112008-08-16 12:57:46 +00001433
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001434 switch (Attr) {
Bill Wendlinga89e67d2009-05-10 23:14:38 +00001435 case DW_AT_sibling:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001436 Asm->EmitInt32(Die->SiblingOffset());
1437 break;
Bill Wendlinga89e67d2009-05-10 23:14:38 +00001438 case DW_AT_abstract_origin: {
Bill Wendlingbbee8c92009-05-15 00:11:17 +00001439 DIEEntry *E = cast<DIEEntry>(Values[i]);
Bill Wendlinga89e67d2009-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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447 }
Bill Wendlinga89e67d2009-05-10 23:14:38 +00001448 default:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001449 // Emit an attribute using the defined form.
Bill Wendlingbbee8c92009-05-15 00:11:17 +00001450 Values[i]->EmitValue(this, Form);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001451 break;
1452 }
aslc200b112008-08-16 12:57:46 +00001453
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001454 Asm->EOL(AttributeString(Attr));
1455 }
aslc200b112008-08-16 12:57:46 +00001456
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001457 // Emit the DIE children if any.
1458 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
1459 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00001460
Bill Wendlingc2f701a2009-05-08 20:38:02 +00001461 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001462 EmitDIE(Children[j]);
aslc200b112008-08-16 12:57:46 +00001463
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001464 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
1465 }
1466 }
1467
1468 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
1469 ///
1470 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1471 // Get the children.
1472 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00001473
Dan Gohmanf17a25c2007-07-18 16:29:46 +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());
aslc200b112008-08-16 12:57:46 +00001479
Dan Gohmanf17a25c2007-07-18 16:29:46 +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);
aslc200b112008-08-16 12:57:46 +00001486
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001487 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00001488 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
1489
Owen Anderson88dd6232008-06-24 21:44:59 +00001490 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
1491 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001492
1493 // Size the DIE attribute values.
1494 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1495 // Size attribute value.
Bill Wendlingbbee8c92009-05-15 00:11:17 +00001496 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001497 }
aslc200b112008-08-16 12:57:46 +00001498
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001499 // Size the DIE children if any.
1500 if (!Children.empty()) {
1501 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
1502 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00001503
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1505 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
1506 }
aslc200b112008-08-16 12:57:46 +00001507
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001508 // End of children marker.
1509 Offset += sizeof(int8_t);
1510 }
1511
1512 Die->setSize(Offset - Die->getOffset());
1513 return Offset;
1514 }
1515
1516 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
1517 ///
1518 void SizeAndOffsets() {
Bill Wendlingf7e62552009-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001526 // Process base compile unit.
Devang Patel2ae1db52009-01-30 18:20:31 +00001527 if (MainCU) {
Devang Patel2ae1db52009-01-30 18:20:31 +00001528 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
Bill Wendlingf7e62552009-05-08 21:03:15 +00001529 CompileUnitOffsets[MainCU] = 0;
Devang Patel2ae1db52009-01-30 18:20:31 +00001530 return;
1531 }
Bill Wendlingf7e62552009-05-08 21:03:15 +00001532
1533 // Process all compile units.
1534 unsigned PrevOffset = 0;
1535
Evan Cheng3e288912009-02-25 07:04:34 +00001536 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
1537 CompileUnit *Unit = CompileUnits[i];
Bill Wendlingf7e62552009-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 Patel6eae2832009-01-12 23:05:55 +00001541 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001542 }
1543
Evan Cheng3e288912009-02-25 07:04:34 +00001544 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545 ///
Evan Cheng3e288912009-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001574 void EmitDebugInfo() {
1575 // Start debug info section.
1576 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00001577
Evan Cheng3e288912009-02-25 07:04:34 +00001578 if (MainCU) {
1579 EmitDebugInfoPerCU(MainCU);
1580 return;
Devang Patel6eae2832009-01-12 23:05:55 +00001581 }
Evan Cheng3e288912009-02-25 07:04:34 +00001582
1583 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
1584 EmitDebugInfoPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001585 }
1586
1587 /// EmitAbbreviations - Emit the abbreviation section.
1588 ///
1589 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());
aslc200b112008-08-16 12:57:46 +00001594
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001595 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00001596
Dan Gohmanf17a25c2007-07-18 16:29:46 +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];
aslc200b112008-08-16 12:57:46 +00001601
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001602 // Emit the abbrevations code (base 1 index.)
1603 Asm->EmitULEB128Bytes(Abbrev->getNumber());
1604 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00001605
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001606 // Emit the abbreviations data.
Bill Wendlingbbee8c92009-05-15 00:11:17 +00001607 Abbrev->Emit(Asm);
aslc200b112008-08-16 12:57:46 +00001608
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001609 Asm->EOL();
1610 }
aslc200b112008-08-16 12:57:46 +00001611
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001612 // Mark end of abbreviations.
1613 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
1614
1615 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00001616
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001617 Asm->EOL();
1618 }
1619 }
1620
Bill Wendling1983a2a2008-07-20 00:11:19 +00001621 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
1622 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00001623 ///
Bill Wendling1983a2a2008-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001637 /// EmitDebugLines - Emit source line information.
1638 ///
1639 void EmitDebugLines() {
Bill Wendling1983a2a2008-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 Gohmanc55b34a2007-09-24 21:43:52 +00001643 return;
1644
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
1649
1650 // Start the dwarf line section.
1651 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00001652
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001653 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00001654
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001655 EmitDifference("line_end", 0, "line_begin", 0, true);
1656 Asm->EOL("Length of Source Line Info");
1657 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00001658
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001659 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00001660
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001661 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
1662 Asm->EOL("Prolog Length");
1663 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00001664
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001665 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
1666
1667 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
1668
1669 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00001670
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001671 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
1672
1673 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00001674
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001675 // Line number standard opcode encodings argument count
1676 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");
1685
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001686 // Emit directories.
Evan Cheng3e288912009-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");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001690 }
1691 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00001692
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001693 // Emit files.
Evan Cheng3e288912009-02-25 07:04:34 +00001694 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
1695 // Remember source id starts at 1.
Bill Wendlingdf25fd62009-03-10 21:59:25 +00001696 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Cheng3e288912009-02-25 07:04:34 +00001697 Asm->EmitString(getSourceFileName(Id.second));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001698 Asm->EOL("Source");
Evan Cheng3e288912009-02-25 07:04:34 +00001699 Asm->EmitULEB128Bytes(Id.first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001700 Asm->EOL("Directory #");
1701 Asm->EmitULEB128Bytes(0);
1702 Asm->EOL("Mod date");
1703 Asm->EmitULEB128Bytes(0);
1704 Asm->EOL("File size");
1705 }
1706 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00001707
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001708 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00001709
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001710 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00001711 unsigned SecSrcLinesSize = SectionSourceLines.size();
1712
1713 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001714 // Isolate current sections line info.
Devang Patel35a078f2009-01-12 22:54:42 +00001715 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00001716
Evan Cheng42ceb472009-03-25 01:47:28 +00001717 if (Asm->isVerbose()) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001718 const Section* S = SectionMap[j + 1];
Evan Cheng3e288912009-02-25 07:04:34 +00001719 O << '\t' << TAI->getCommentString() << " Section"
1720 << S->getName() << '\n';
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001721 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00001722 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001723
1724 // Dwarf assumes we start with first line of first source file.
1725 unsigned Source = 1;
1726 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00001727
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001728 // Construct rows of the address, source, line, column matrix.
1729 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patel35a078f2009-01-12 22:54:42 +00001730 const SrcLineInfo &LineInfo = LineInfos[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001731 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
1732 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00001733
Evan Cheng42ceb472009-03-25 01:47:28 +00001734 if (!Asm->isVerbose())
Evan Cheng0eeed442008-07-01 23:18:29 +00001735 Asm->EOL();
Evan Cheng3e288912009-02-25 07:04:34 +00001736 else {
1737 std::pair<unsigned, unsigned> SourceID =
Bill Wendlingdf25fd62009-03-10 21:59:25 +00001738 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Cheng3e288912009-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 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001744
1745 // Define the line address.
1746 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00001747 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001748 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
1749 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00001750
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001751 // If change of source, then switch to the new source.
1752 if (Source != LineInfo.getSourceID()) {
1753 Source = LineInfo.getSourceID();
1754 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
1755 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
1756 }
aslc200b112008-08-16 12:57:46 +00001757
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001758 // If change of line.
1759 if (Line != LineInfo.getLine()) {
1760 // Determine offset.
1761 int Offset = LineInfo.getLine() - Line;
1762 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00001763
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001764 // Update line.
1765 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00001766
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001767 // If delta is small enough and in range...
1768 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
1769 // ... then use fast opcode.
1770 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
1771 } else {
1772 // ... otherwise use long hand.
1773 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");
1776 }
1777 } else {
1778 // Copy the previous row (different address or source)
1779 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
1780 }
1781 }
1782
Bill Wendling1983a2a2008-07-20 00:11:19 +00001783 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001784 }
Bill Wendling1983a2a2008-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);
aslc200b112008-08-16 12:57:46 +00001791
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001792 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00001793
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001794 Asm->EOL();
1795 }
aslc200b112008-08-16 12:57:46 +00001796
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001797 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
1798 ///
1799 void EmitCommonDebugFrame() {
1800 if (!TAI->doesDwarfRequireFrameSection())
1801 return;
1802
1803 int stackGrowth =
1804 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1805 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00001806 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001807
1808 // Start the dwarf frame section.
1809 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1810
1811 EmitLabel("debug_frame_common", 0);
1812 EmitDifference("debug_frame_common_end", 0,
1813 "debug_frame_common_begin", 0, true);
1814 Asm->EOL("Length of Common Information Entry");
1815
1816 EmitLabel("debug_frame_common_begin", 0);
1817 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);
aslc200b112008-08-16 12:57:46 +00001826 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00001827 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001828 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00001829
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001830 std::vector<MachineMove> Moves;
1831 RI->getInitialFrameState(Moves);
1832
Dale Johannesenf5a11532007-11-13 19:13:01 +00001833 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001834
Evan Cheng7e7d1942008-02-29 19:36:59 +00001835 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001836 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00001837
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001838 Asm->EOL();
1839 }
1840
1841 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
1842 /// section.
1843 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
1844 if (!TAI->doesDwarfRequireFrameSection())
1845 return;
aslc200b112008-08-16 12:57:46 +00001846
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001847 // Start the dwarf frame section.
1848 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00001849
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001850 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
1851 "debug_frame_begin", DebugFrameInfo.Number, true);
1852 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00001853
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001854 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
1855
1856 EmitSectionOffset("debug_frame_common", "section_debug_frame",
1857 0, 0, true, false);
1858 Asm->EOL("FDE CIE offset");
1859
1860 EmitReference("func_begin", DebugFrameInfo.Number);
1861 Asm->EOL("FDE initial location");
1862 EmitDifference("func_end", DebugFrameInfo.Number,
1863 "func_begin", DebugFrameInfo.Number);
1864 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00001865
Devang Patelb28de842009-01-17 08:01:33 +00001866 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00001867 false);
aslc200b112008-08-16 12:57:46 +00001868
Evan Cheng7e7d1942008-02-29 19:36:59 +00001869 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001870 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
1871
1872 Asm->EOL();
1873 }
1874
Evan Cheng3e288912009-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 Wendlinge06da442009-04-09 21:49:15 +00001892 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendling3f94b412009-04-09 23:51:31 +00001893 for (StringMap<DIE*>::const_iterator
Bill Wendlinge06da442009-04-09 21:49:15 +00001894 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendling3f94b412009-04-09 23:51:31 +00001895 const char *Name = GI->getKeyData();
Evan Cheng3e288912009-02-25 07:04:34 +00001896 DIE * Entity = GI->second;
1897
1898 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendling3f94b412009-04-09 23:51:31 +00001899 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Cheng3e288912009-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001908 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
1909 ///
1910 void EmitDebugPubNames() {
1911 // Start the dwarf pubnames section.
1912 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00001913
Evan Cheng3e288912009-02-25 07:04:34 +00001914 if (MainCU) {
1915 EmitDebugPubNamesPerCU(MainCU);
1916 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001917 }
Evan Cheng3e288912009-02-25 07:04:34 +00001918
1919 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
1920 EmitDebugPubNamesPerCU(CompileUnits[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001921 }
1922
1923 /// EmitDebugStr - Emit visible names into a debug str section.
1924 ///
1925 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());
aslc200b112008-08-16 12:57:46 +00001930
Dan Gohmanf17a25c2007-07-18 16:29:46 +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];
1938 Asm->EmitString(String); Asm->EOL();
1939 }
aslc200b112008-08-16 12:57:46 +00001940
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001941 Asm->EOL();
1942 }
1943 }
1944
1945 /// EmitDebugLoc - Emit visible names into a debug loc section.
1946 ///
1947 void EmitDebugLoc() {
1948 // Start the dwarf loc section.
1949 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00001950
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001951 Asm->EOL();
1952 }
1953
1954 /// EmitDebugARanges - Emit visible names into a debug aranges section.
1955 ///
1956 void EmitDebugARanges() {
1957 // Start the dwarf aranges section.
1958 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00001959
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001960 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00001961#if 0
aslc200b112008-08-16 12:57:46 +00001962 CompileUnit *Unit = GetBaseCompileUnit();
1963
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001964 // Don't include size of length
1965 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00001966
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001967 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00001968
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001969 EmitReference("info_begin", Unit->getID());
1970 Asm->EOL("Offset of Compilation Unit Info");
1971
Dan Gohmancfb72b22007-09-27 23:12:31 +00001972 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001973
1974 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
1975
1976 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
1977 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
1978
1979 // Range 1
1980 EmitReference("text_begin", 0); Asm->EOL("Address");
1981 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
1982
1983 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
1984 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00001985#endif
aslc200b112008-08-16 12:57:46 +00001986
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001987 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001988 }
1989
1990 /// EmitDebugRanges - Emit visible names into a debug ranges section.
1991 ///
1992 void EmitDebugRanges() {
1993 // Start the dwarf ranges section.
1994 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00001995
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001996 Asm->EOL();
1997 }
1998
1999 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2000 ///
2001 void EmitDebugMacInfo() {
Scott Michel79f01f52009-01-26 22:32:51 +00002002 if (TAI->doesSupportMacInfoSection()) {
2003 // Start the dwarf macinfo section.
2004 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00002005
Scott Michel79f01f52009-01-26 22:32:51 +00002006 Asm->EOL();
2007 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002008 }
2009
Devang Patel88bf96e2009-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 Gohmand46dc022009-05-07 19:46:24 +00002053
Devang Patel88bf96e2009-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 Wendling278a3922009-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 Cheng3e288912009-02-25 07:04:34 +00002119 void ConstructCompileUnit(GlobalVariable *GV) {
2120 DICompileUnit DIUnit(GV);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002121 std::string Dir, FN, Prod;
2122 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
2123 DIUnit.getFilename(FN));
Evan Cheng3e288912009-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 Wendlingf3f16e82009-03-13 04:39:26 +00002129 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Cheng3e288912009-02-25 07:04:34 +00002130 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendling1c5842b2009-03-09 05:04:40 +00002131 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002132 if (!Dir.empty())
Bill Wendling1c5842b2009-03-09 05:04:40 +00002133 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Cheng3e288912009-02-25 07:04:34 +00002134 if (DIUnit.isOptimized())
2135 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002136 std::string Flags;
2137 DIUnit.getFlags(Flags);
2138 if (!Flags.empty())
Evan Cheng3e288912009-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 Patel289f2362009-01-05 23:11:11 +00002153 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00002154 void ConstructCompileUnits() {
Evan Cheng3e288912009-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 Patel2ae1db52009-01-30 18:20:31 +00002169 }
Evan Cheng3e288912009-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 Lattner88ab9742009-05-05 04:55:56 +00002176 DW_Unit = &FindCompileUnit(DI_GV.getCompileUnit());
Evan Cheng3e288912009-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 Wendling26a8ab92009-04-10 00:12:49 +00002188 std::string GLN;
Evan Cheng3e288912009-02-25 07:04:34 +00002189 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling26a8ab92009-04-10 00:12:49 +00002190 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Cheng3e288912009-02-25 07:04:34 +00002191 AddBlock(VariableDie, DW_AT_location, 0, Block);
2192
2193 // Add to map.
2194 Slot = VariableDie;
Bill Wendling74940d12009-05-06 21:21:34 +00002195
Evan Cheng3e288912009-02-25 07:04:34 +00002196 // Add to context owner.
2197 DW_Unit->getDie()->AddChild(VariableDie);
Bill Wendling74940d12009-05-06 21:21:34 +00002198
Evan Cheng3e288912009-02-25 07:04:34 +00002199 // Expose as global. FIXME - need to check external flag.
Bill Wendlingf3f16e82009-03-13 04:39:26 +00002200 std::string Name;
2201 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Cheng3e288912009-02-25 07:04:34 +00002202 return true;
Devang Patelb3907da2009-01-05 23:03:32 +00002203 }
2204
Devang Patel289f2362009-01-05 23:11:11 +00002205 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patela9169c32009-02-24 00:02:15 +00002206 /// visible global variables. Return true if at least one global DIE is
2207 /// created.
2208 bool ConstructGlobalVariableDIEs() {
Evan Cheng3e288912009-02-25 07:04:34 +00002209 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
2210 if (!Root)
2211 return false;
Devang Patel289f2362009-01-05 23:11:11 +00002212
Evan Cheng3e288912009-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 Patel289f2362009-01-05 23:11:11 +00002218
Evan Cheng3e288912009-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 Wendling74940d12009-05-06 21:21:34 +00002223 UUI != UUE; ++UUI)
2224 Result |= ConstructGlobalVariableDIE(cast<GlobalVariable>(*UUI));
2225
Evan Cheng3e288912009-02-25 07:04:34 +00002226 return Result;
2227 }
Devang Patel289f2362009-01-05 23:11:11 +00002228
Evan Cheng3e288912009-02-25 07:04:34 +00002229 bool ConstructSubprogram(GlobalVariable *GV) {
2230 DISubprogram SP(GV);
2231 CompileUnit *Unit = MainCU;
2232 if (!Unit)
Chris Lattner88ab9742009-05-05 04:55:56 +00002233 Unit = &FindCompileUnit(SP.getCompileUnit());
Devang Patel289f2362009-01-05 23:11:11 +00002234
Evan Cheng3e288912009-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 Wendlingf3f16e82009-03-13 04:39:26 +00002252 std::string Name;
2253 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Cheng3e288912009-02-25 07:04:34 +00002254 return true;
Devang Patel289f2362009-01-05 23:11:11 +00002255 }
2256
Devang Patele6caf012009-01-05 23:21:35 +00002257 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patela9169c32009-02-24 00:02:15 +00002258 /// subprograms. Return true if at least one subprogram DIE is created.
2259 bool ConstructSubprograms() {
Evan Cheng3e288912009-02-25 07:04:34 +00002260 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
2261 if (!Root)
2262 return false;
Devang Patele6caf012009-01-05 23:21:35 +00002263
Evan Cheng3e288912009-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 Patele6caf012009-01-05 23:21:35 +00002269
Evan Cheng3e288912009-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 Wendling74940d12009-05-06 21:21:34 +00002274 UUI != UUE; ++UUI)
2275 Result |= ConstructSubprogram(cast<GlobalVariable>(*UUI));
2276
Evan Cheng3e288912009-02-25 07:04:34 +00002277 return Result;
Devang Patele6caf012009-01-05 23:21:35 +00002278 }
2279
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002280public:
2281 //===--------------------------------------------------------------------===//
2282 // Main entry points.
2283 //
Owen Anderson847b99b2008-08-21 00:14:44 +00002284 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendlingd9308a62009-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 Patel7b60d552009-04-15 20:41:31 +00002289 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00002290 if (TimePassesIsEnabled)
2291 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling148ecc42009-03-10 22:58:53 +00002292 getDwarfTimerGroup());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002293 }
2294 virtual ~DwarfDebug() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002295 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2296 delete Values[j];
Bill Wendlingd9308a62009-03-10 21:23:25 +00002297
Bill Wendlinga89e67d2009-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 Wendlingd9308a62009-03-10 21:23:25 +00002303 delete DebugTimer;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002304 }
2305
Bill Wendling278a3922009-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 Patel9304b382009-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 Patel91d27b02009-01-12 23:09:42 +00002312 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00002313 if (TimePassesIsEnabled)
2314 DebugTimer->startTimer();
2315
Bill Wendling6baa18d2009-02-03 21:38:21 +00002316 // Create all the compile unit DIEs.
2317 ConstructCompileUnits();
Devang Patel91d27b02009-01-12 23:09:42 +00002318
Bill Wendlingd9308a62009-03-10 21:23:25 +00002319 if (CompileUnits.empty()) {
2320 if (TimePassesIsEnabled)
Bill Wendling0be24752009-03-10 22:02:13 +00002321 DebugTimer->stopTimer();
Bill Wendlingd9308a62009-03-10 21:23:25 +00002322
Bill Wendling6baa18d2009-02-03 21:38:21 +00002323 return;
Bill Wendlingd9308a62009-03-10 21:23:25 +00002324 }
Devang Patel91d27b02009-01-12 23:09:42 +00002325
Devang Patela9169c32009-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 Wendlingd9308a62009-03-10 21:23:25 +00002334 if (!globalDIEs && !subprogramDIEs) {
2335 if (TimePassesIsEnabled)
Bill Wendling0be24752009-03-10 22:02:13 +00002336 DebugTimer->stopTimer();
Bill Wendlingd9308a62009-03-10 21:23:25 +00002337
Devang Patela9169c32009-02-24 00:02:15 +00002338 return;
Bill Wendlingd9308a62009-03-10 21:23:25 +00002339 }
Devang Patela9169c32009-02-24 00:02:15 +00002340
Bill Wendling6baa18d2009-02-03 21:38:21 +00002341 MMI = mmi;
2342 shouldEmit = true;
2343 MMI->setDebugInfoAvailability(true);
Devang Patel9304b382009-01-06 21:07:30 +00002344
Bill Wendling6baa18d2009-02-03 21:38:21 +00002345 // Prime section data.
2346 SectionMap.insert(TAI->getTextSection());
Devang Patel9304b382009-01-06 21:07:30 +00002347
Bill Wendling6baa18d2009-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 Cheng3e288912009-02-25 07:04:34 +00002351 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
2352 // Remember source id starts at 1.
Bill Wendlingdf25fd62009-03-10 21:59:25 +00002353 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Cheng3e288912009-02-25 07:04:34 +00002354 sys::Path FullPath(getSourceDirectoryName(Id.first));
2355 bool AppendOk =
2356 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendling6baa18d2009-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 Patel9304b382009-01-06 21:07:30 +00002361 }
Bill Wendling6baa18d2009-02-03 21:38:21 +00002362 }
Devang Patel9304b382009-01-06 21:07:30 +00002363
Bill Wendling6baa18d2009-02-03 21:38:21 +00002364 // Emit initial sections
2365 EmitInitial();
Bill Wendlingd9308a62009-03-10 21:23:25 +00002366
2367 if (TimePassesIsEnabled)
2368 DebugTimer->stopTimer();
Devang Patel9304b382009-01-06 21:07:30 +00002369 }
2370
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002371 /// BeginModule - Emit all Dwarf sections that should come prior to the
2372 /// content.
2373 void BeginModule(Module *M) {
2374 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002375 }
2376
2377 /// EndModule - Emit all Dwarf sections that should come after the content.
2378 ///
2379 void EndModule() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00002380 if (!ShouldEmitDwarfDebug())
2381 return;
2382
2383 if (TimePassesIsEnabled)
2384 DebugTimer->startTimer();
aslc200b112008-08-16 12:57:46 +00002385
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002386 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002387 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002388 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002389 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002390 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00002391
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002392 // End text sections.
2393 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002394 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002395 EmitLabel("section_end", i);
2396 }
2397
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
2406 // Compute DIE offsets and sizes.
2407 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00002408
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002409 // Emit all the DIEs into a debug info section
2410 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00002411
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002412 // Corresponding abbreviations into a abbrev section.
2413 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00002414
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002415 // Emit source line correspondence into a debug line section.
2416 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00002417
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002418 // Emit info into a debug pubnames section.
2419 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00002420
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002421 // Emit info into a debug str section.
2422 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00002423
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002424 // Emit info into a debug loc section.
2425 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00002426
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002427 // Emit info into a debug aranges section.
2428 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00002429
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002430 // Emit info into a debug ranges section.
2431 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00002432
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002433 // Emit info into a debug macinfo section.
2434 EmitDebugMacInfo();
Bill Wendlingd9308a62009-03-10 21:23:25 +00002435
Devang Patel88bf96e2009-04-13 17:02:03 +00002436 // Emit inline info.
2437 EmitDebugInlineInfo();
2438
Bill Wendlingd9308a62009-03-10 21:23:25 +00002439 if (TimePassesIsEnabled)
2440 DebugTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002441 }
2442
aslc200b112008-08-16 12:57:46 +00002443 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002444 /// emitted immediately after the function entry point.
2445 void BeginFunction(MachineFunction *MF) {
Bill Wendlingc1d211d2009-03-11 00:03:50 +00002446 this->MF = MF;
2447
Bill Wendling50db0792009-02-20 00:44:43 +00002448 if (!ShouldEmitDwarfDebug()) return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002449
Bill Wendlingd9308a62009-03-10 21:23:25 +00002450 if (TimePassesIsEnabled)
2451 DebugTimer->startTimer();
2452
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002453 // Begin accumulating function debug information.
2454 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00002455
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002456 // Assumes in correct section after the entry point.
2457 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00002458
Argiris Kirtzidis8cac4932009-05-04 19:23:45 +00002459 // Emit label for the implicitly defined dbg.stoppoint at the start of
2460 // the function.
asl7a969d82009-05-04 19:10:38 +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);
Argiris Kirtzidis5e3ef112009-05-03 23:27:19 +00002467 }
2468
Bill Wendlingd9308a62009-03-10 21:23:25 +00002469 if (TimePassesIsEnabled)
2470 DebugTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002471 }
aslc200b112008-08-16 12:57:46 +00002472
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002473 /// EndFunction - Gather and emit post-function debug information.
2474 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002475 void EndFunction(MachineFunction *MF) {
Bill Wendling50db0792009-02-20 00:44:43 +00002476 if (!ShouldEmitDwarfDebug()) return;
aslc200b112008-08-16 12:57:46 +00002477
Bill Wendlingd9308a62009-03-10 21:23:25 +00002478 if (TimePassesIsEnabled)
2479 DebugTimer->startTimer();
2480
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002481 // Define end label for subprogram.
2482 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00002483
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002484 // Get function line info.
Devang Patel35a078f2009-01-12 22:54:42 +00002485 if (!Lines.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002486 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002487 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002488 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patel35a078f2009-01-12 22:54:42 +00002489 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002490 // Append the function info to section info.
2491 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patel35a078f2009-01-12 22:54:42 +00002492 Lines.begin(), Lines.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002493 }
aslc200b112008-08-16 12:57:46 +00002494
Bill Wendlinga89e67d2009-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002501 // Construct scopes for subprogram.
Devang Patel7b60d552009-04-15 20:41:31 +00002502 if (FunctionDbgScope)
2503 ConstructFunctionDbgScope(FunctionDbgScope);
Bill Wendlingb22ae7d2008-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 Patel6ccd57e2009-01-13 00:20:51 +00002513 ConstructDefaultDbgScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002514
2515 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2516 MMI->getFrameMoves()));
Devang Patela4162952009-01-12 18:48:36 +00002517
2518 // Clear debug info
Devang Patel7b60d552009-04-15 20:41:31 +00002519 if (FunctionDbgScope) {
2520 delete FunctionDbgScope;
Devang Patela4162952009-01-12 18:48:36 +00002521 DbgScopeMap.clear();
Bill Wendling66e789e2009-05-13 23:55:49 +00002522 DbgAbstractScopeMap.clear();
2523 DbgConcreteScopeMap.clear();
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002524 InlinedVariableScopes.clear();
Devang Patel7b60d552009-04-15 20:41:31 +00002525 FunctionDbgScope = NULL;
Bill Wendlinga89e67d2009-05-10 23:14:38 +00002526 LexicalScopeStack.clear();
2527 AbstractInstanceRootList.clear();
Devang Patela4162952009-01-12 18:48:36 +00002528 }
Devang Patelcb59fd42009-01-12 19:17:34 +00002529
Bill Wendlingd9308a62009-03-10 21:23:25 +00002530 Lines.clear();
2531
2532 if (TimePassesIsEnabled)
2533 DebugTimer->stopTimer();
2534 }
Devang Patelcb59fd42009-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 Wendlingd9308a62009-03-10 21:23:25 +00002540 if (TimePassesIsEnabled)
2541 DebugTimer->startTimer();
2542
Evan Cheng3e288912009-02-25 07:04:34 +00002543 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendling6baa18d2009-02-03 21:38:21 +00002544 assert(Unit && "Unable to find CompileUnit");
Devang Patelcb59fd42009-01-12 19:17:34 +00002545 unsigned ID = MMI->NextLabelID();
2546 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendlingd9308a62009-03-10 21:23:25 +00002547
2548 if (TimePassesIsEnabled)
2549 DebugTimer->stopTimer();
2550
Devang Patelcb59fd42009-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.
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00002557 unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00002558 if (TimePassesIsEnabled)
2559 DebugTimer->startTimer();
2560
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00002561 std::string Dir, Fn;
2562 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
2563 CU.getFilename(Fn));
Devang Patelcb59fd42009-01-12 19:17:34 +00002564 unsigned ID = MMI->NextLabelID();
2565 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendlingd9308a62009-03-10 21:23:25 +00002566
2567 if (TimePassesIsEnabled)
2568 DebugTimer->stopTimer();
2569
Devang Patelcb59fd42009-01-12 19:17:34 +00002570 return ID;
2571 }
2572
Bill Wendling278a3922009-03-10 21:47:45 +00002573 /// getRecordSourceLineCount - Return the number of source lines in the debug
2574 /// info.
2575 unsigned getRecordSourceLineCount() const {
Devang Patelcb59fd42009-01-12 19:17:34 +00002576 return Lines.size();
2577 }
2578
Bill Wendling278a3922009-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 Cheng3e288912009-02-25 07:04:34 +00002584 unsigned getOrCreateSourceID(const std::string &DirName,
2585 const std::string &FileName) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00002586 if (TimePassesIsEnabled)
2587 DebugTimer->startTimer();
2588
Bill Wendling278a3922009-03-10 21:47:45 +00002589 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendlingd9308a62009-03-10 21:23:25 +00002590
2591 if (TimePassesIsEnabled)
2592 DebugTimer->stopTimer();
2593
Evan Cheng3e288912009-02-25 07:04:34 +00002594 return SrcId;
Devang Patelcb59fd42009-01-12 19:17:34 +00002595 }
2596
2597 /// RecordRegionStart - Indicate the start of a region.
Devang Patelcb59fd42009-01-12 19:17:34 +00002598 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00002599 if (TimePassesIsEnabled)
2600 DebugTimer->startTimer();
2601
Devang Patelcb59fd42009-01-12 19:17:34 +00002602 DbgScope *Scope = getOrCreateScope(V);
2603 unsigned ID = MMI->NextLabelID();
2604 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendlinga89e67d2009-05-10 23:14:38 +00002605 LexicalScopeStack.push_back(Scope);
Bill Wendlingd9308a62009-03-10 21:23:25 +00002606
2607 if (TimePassesIsEnabled)
2608 DebugTimer->stopTimer();
2609
Devang Patelcb59fd42009-01-12 19:17:34 +00002610 return ID;
2611 }
2612
2613 /// RecordRegionEnd - Indicate the end of a region.
Dan Gohmand46dc022009-05-07 19:46:24 +00002614 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00002615 if (TimePassesIsEnabled)
2616 DebugTimer->startTimer();
2617
Bill Wendlingd32c9722009-05-07 17:26:14 +00002618 DbgScope *Scope = getOrCreateScope(V);
Dan Gohmand46dc022009-05-07 19:46:24 +00002619 unsigned ID = MMI->NextLabelID();
Devang Patelcb59fd42009-01-12 19:17:34 +00002620 Scope->setEndLabelID(ID);
Bill Wendlinga89e67d2009-05-10 23:14:38 +00002621 if (LexicalScopeStack.size() != 0)
2622 LexicalScopeStack.pop_back();
Bill Wendlingd9308a62009-03-10 21:23:25 +00002623
2624 if (TimePassesIsEnabled)
2625 DebugTimer->stopTimer();
2626
Devang Patelcb59fd42009-01-12 19:17:34 +00002627 return ID;
2628 }
2629
2630 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002631 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
2632 const MachineInstr *MI) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00002633 if (TimePassesIsEnabled)
2634 DebugTimer->startTimer();
2635
Devang Patel2560d922009-01-15 18:25:17 +00002636 DIDescriptor Desc(GV);
2637 DbgScope *Scope = NULL;
Bill Wendlingd9308a62009-03-10 21:23:25 +00002638
Devang Patel2560d922009-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 Patel8a9a7dc2009-04-15 00:10:26 +00002644 DenseMap<const MachineInstr *, DbgScope *>::iterator
2645 SI = InlinedVariableScopes.find(MI);
Bill Wendling74940d12009-05-06 21:21:34 +00002646
Bill Wendlinga89e67d2009-05-10 23:14:38 +00002647 if (SI != InlinedVariableScopes.end()) {
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002648 // or GV is an inlined local variable.
2649 Scope = SI->second;
2650 } else {
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002651 DIVariable DV(GV);
Bill Wendlinga89e67d2009-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 Patel8a9a7dc2009-04-15 00:10:26 +00002664 }
Devang Patel2560d922009-01-15 18:25:17 +00002665 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00002666
Bill Wendling66e789e2009-05-13 23:55:49 +00002667 assert(Scope && "Unable to find the variable's scope");
Devang Patel7c8a2772009-01-16 19:28:14 +00002668 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelcb59fd42009-01-12 19:17:34 +00002669 Scope->AddVariable(DV);
Bill Wendlingd9308a62009-03-10 21:23:25 +00002670
2671 if (TimePassesIsEnabled)
2672 DebugTimer->stopTimer();
Devang Patelcb59fd42009-01-12 19:17:34 +00002673 }
Devang Patel88bf96e2009-04-13 17:02:03 +00002674
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002675 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
Argiris Kirtzidisf4510c02009-05-07 00:16:31 +00002676 unsigned RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
2677 unsigned Line, unsigned Col) {
2678 unsigned LabelID = MMI->NextLabelID();
2679
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002680 if (!TAI->doesDwarfUsesInlineInfoSection())
Argiris Kirtzidisf4510c02009-05-07 00:16:31 +00002681 return LabelID;
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002682
Bill Wendlingb183d0c2009-05-01 08:40:06 +00002683 if (TimePassesIsEnabled)
2684 DebugTimer->startTimer();
2685
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002686 GlobalVariable *GV = SP.getGV();
Bill Wendlinga89e67d2009-05-10 23:14:38 +00002687 DenseMap<const GlobalVariable *, DbgScope *>::iterator
2688 II = AbstractInstanceRootMap.find(GV);
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002689
Bill Wendlinga89e67d2009-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 Wendling0cfbd9b2009-05-01 08:32:14 +00002694
Bill Wendlinga89e67d2009-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 Wendling66e789e2009-05-13 23:55:49 +00002708 // Keep track of the abstract scope for this function.
2709 DbgAbstractScopeMap[GV] = Scope;
Bill Wendling496eea92009-05-13 20:33:33 +00002710
Bill Wendlinga89e67d2009-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 Wendlingbbee8c92009-05-15 00:11:17 +00002722 AddDIEEntry(ScopeDie, DW_AT_abstract_origin, DW_FORM_ref4, Origin);
Bill Wendlinga89e67d2009-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 Wendlingdea0ec22009-05-12 00:06:59 +00002729 MMI->RecordUsedDbgLabel(LabelID);
Bill Wendlinga89e67d2009-05-10 23:14:38 +00002730
2731 LexicalScopeStack.back()->AddConcreteInst(ConcreteScope);
2732
Bill Wendling66e789e2009-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 Wendlingdea0ec22009-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 Wendlingb183d0c2009-05-01 08:40:06 +00002751 if (TimePassesIsEnabled)
2752 DebugTimer->stopTimer();
Argiris Kirtzidisf4510c02009-05-07 00:16:31 +00002753
2754 return LabelID;
Devang Patel88bf96e2009-04-13 17:02:03 +00002755 }
Devang Patel8a9a7dc2009-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 Wendlingb183d0c2009-05-01 08:40:06 +00002762 if (TimePassesIsEnabled)
2763 DebugTimer->startTimer();
2764
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002765 GlobalVariable *GV = SP.getGV();
Bill Wendling496eea92009-05-13 20:33:33 +00002766 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 8> >::iterator
Bill Wendling66e789e2009-05-13 23:55:49 +00002767 I = DbgConcreteScopeMap.find(GV);
Bill Wendling74940d12009-05-06 21:21:34 +00002768
Bill Wendling66e789e2009-05-13 23:55:49 +00002769 if (I == DbgConcreteScopeMap.end()) {
Bill Wendlingb183d0c2009-05-01 08:40:06 +00002770 if (TimePassesIsEnabled)
2771 DebugTimer->stopTimer();
2772
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002773 return 0;
Bill Wendlingb183d0c2009-05-01 08:40:06 +00002774 }
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002775
Bill Wendling496eea92009-05-13 20:33:33 +00002776 SmallVector<DbgScope *, 8> &Scopes = I->second;
Bill Wendling58ed5d22009-04-29 00:15:41 +00002777 assert(!Scopes.empty() && "We should have at least one debug scope!");
Bill Wendling496eea92009-05-13 20:33:33 +00002778 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002779 unsigned ID = MMI->NextLabelID();
2780 MMI->RecordUsedDbgLabel(ID);
2781 Scope->setEndLabelID(ID);
Bill Wendlingb183d0c2009-05-01 08:40:06 +00002782
2783 if (TimePassesIsEnabled)
2784 DebugTimer->stopTimer();
2785
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002786 return ID;
2787 }
2788
2789 /// RecordVariableScope - Record scope for the variable declared by
Bill Wendling66e789e2009-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 Patel8a9a7dc2009-04-15 00:10:26 +00002793 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI) {
Bill Wendlingb183d0c2009-05-01 08:40:06 +00002794 if (TimePassesIsEnabled)
2795 DebugTimer->startTimer();
2796
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002797 DISubprogram SP(DV.getContext().getGV());
Bill Wendlingb183d0c2009-05-01 08:40:06 +00002798
2799 if (SP.isNull()) {
2800 if (TimePassesIsEnabled)
2801 DebugTimer->stopTimer();
2802
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002803 return;
Bill Wendlingb183d0c2009-05-01 08:40:06 +00002804 }
2805
Bill Wendling66e789e2009-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 Patel8a9a7dc2009-04-15 00:10:26 +00002810
Bill Wendlingb183d0c2009-05-01 08:40:06 +00002811 if (TimePassesIsEnabled)
2812 DebugTimer->stopTimer();
Devang Patel8a9a7dc2009-04-15 00:10:26 +00002813 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002814};
2815
2816//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00002817/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002818///
2819class DwarfException : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002820 struct FunctionEHFrameInfo {
2821 std::string FnName;
2822 unsigned Number;
2823 unsigned PersonalityIndex;
2824 bool hasCalls;
2825 bool hasLandingPads;
2826 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00002827 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002828
2829 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
2830 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00002831 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00002832 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002833 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00002834 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835 };
2836
2837 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-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
aslc200b112008-08-16 12:57:46 +00002851 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00002852 /// should be emitted.
2853 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00002854
Bill Wendlingd9308a62009-03-10 21:23:25 +00002855 /// ExceptionTimer - Timer for the Dwarf exception writer.
2856 Timer *ExceptionTimer;
2857
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002858 /// EmitCommonEHFrame - Emit the common eh unwind frame.
2859 ///
2860 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
2861 // Size and sign of stack growth.
2862 int stackGrowth =
2863 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2864 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002865 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002866
2867 // Begin eh frame section.
2868 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00002869
2870 if (!TAI->doesRequireNonLocalEHFrameLabel())
2871 O << TAI->getEHGlobalPrefix();
2872 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002873 EmitLabel("section_eh_frame", Index);
2874
2875 // Define base labels.
2876 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00002877
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002878 // Define the eh frame length.
2879 EmitDifference("eh_frame_common_end", Index,
2880 "eh_frame_common_begin", Index, true);
2881 Asm->EOL("Length of Common Information Entry");
2882
2883 // EH frame header.
2884 EmitLabel("eh_frame_common_begin", Index);
2885 Asm->EmitInt32((int)0);
2886 Asm->EOL("CIE Identifier Tag");
2887 Asm->EmitInt8(DW_CIE_VERSION);
2888 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00002889
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002890 // The personality presence indicates that language specific information
2891 // will show up in the eh frame.
2892 Asm->EmitString(Personality ? "zPLR" : "zR");
2893 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00002894
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002895 // Round out reader.
2896 Asm->EmitULEB128Bytes(1);
2897 Asm->EOL("CIE Code Alignment Factor");
2898 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00002899 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002900 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00002901 Asm->EOL("CIE Return Address Column");
2902
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Wendling2d369922007-09-11 17:20:55 +00002907
Duncan Sands96144f92008-05-07 19:11:09 +00002908 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00002909 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00002910 Asm->EOL("Personality (pcrel sdata4 indirect)");
2911 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00002912 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00002913 Asm->EOL("Personality (pcrel sdata4)");
2914 }
Bill Wendling2d369922007-09-11 17:20:55 +00002915
Duncan Sands96144f92008-05-07 19:11:09 +00002916 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00002917 O << TAI->getPersonalityPrefix();
2918 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
2919 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00002920 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
2921 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00002922 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00002923
Duncan Sands96144f92008-05-07 19:11:09 +00002924 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2925 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00002926
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00002927 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2928 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002929 } else {
2930 Asm->EmitULEB128Bytes(1);
2931 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00002932
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00002933 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2934 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002935 }
2936
2937 // Indicate locations of general callee saved registers in frame.
2938 std::vector<MachineMove> Moves;
2939 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00002940 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002941
Dale Johannesen388f20f2008-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.
aslc200b112008-08-16 12:57:46 +00002945 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00002946 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002947 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00002948
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002949 Asm->EOL();
2950 }
Duncan Sands96144f92008-05-07 19:11:09 +00002951
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002952 /// EmitEHFrame - Emit function exception frame information.
2953 ///
2954 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00002955 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
Chris Lattner68433442009-04-13 05:44:34 +00002956
2957 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
2958 "Should not emit 'available externally' functions at all");
Dale Johannesen3dadeb32008-01-16 19:59:28 +00002959
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002960 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
2961
2962 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00002963 // If the corresponding function is static, this should not be
2964 // externally visible.
Rafael Espindolaa168fc92009-01-15 20:18:42 +00002965 if (linkage != Function::InternalLinkage &&
Devang Patel245446c2009-01-17 08:05:14 +00002966 linkage != Function::PrivateLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00002967 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
2968 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
2969 }
2970
Dale Johannesenf09b5992008-01-10 02:03:30 +00002971 // If corresponding function is weak definition, this should be too.
Duncan Sands19d161f2009-03-07 15:45:40 +00002972 if ((linkage == Function::WeakAnyLinkage ||
2973 linkage == Function::WeakODRLinkage ||
2974 linkage == Function::LinkOnceAnyLinkage ||
2975 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesenf09b5992008-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
aslc200b112008-08-16 12:57:46 +00002981 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00002982 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00002983 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00002984 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00002985 !UnwindTablesMandatory &&
Duncan Sands19d161f2009-03-07 15:45:40 +00002986 ((linkage != Function::WeakAnyLinkage &&
2987 linkage != Function::WeakODRLinkage &&
2988 linkage != Function::LinkOnceAnyLinkage &&
2989 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00002990 !TAI->getWeakDefDirective() ||
2991 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00002992 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00002993 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-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 Johannesen3dadeb32008-01-16 19:59:28 +00002996 // dead-stripping unconditionally.
2997 if (const char *UsedDirective = TAI->getUsedDirective())
2998 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002999 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003000 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003001
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003002 // EH frame header.
3003 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3004 "eh_frame_begin", EHFrameInfo.Number, true);
3005 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003006
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003007 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3008
Bill Wendling189bde72008-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003021 Asm->EOL("FDE CIE offset");
3022
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003023 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003024 Asm->EOL("FDE initial location");
3025 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003026 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003027 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003028
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003029 // If there is a personality and landing pads then point to the language
3030 // specific data area in the exception table.
3031 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003032 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003033 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003034
3035 if (EHFrameInfo.hasLandingPads)
3036 EmitReference("exception", EHFrameInfo.Number, true, true);
3037 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003038 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003039 Asm->EOL("Language Specific Data Area");
3040 } else {
3041 Asm->EmitULEB128Bytes(0);
3042 Asm->EOL("Augmentation size");
3043 }
Duncan Sands96144f92008-05-07 19:11:09 +00003044
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003045 // Indicate locations of function specific callee saved registers in
3046 // frame.
Devang Patelb28de842009-01-17 08:01:33 +00003047 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00003048 true);
aslc200b112008-08-16 12:57:46 +00003049
Dale Johannesen388f20f2008-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.
aslc200b112008-08-16 12:57:46 +00003053 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003054 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003055 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003056
3057 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003058 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003059 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-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 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003066 }
3067
Duncan Sands241a0c92007-09-05 11:27:52 +00003068 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
3088 /// 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;
3092 unsigned LSize = LIds.size(), RSize = RIds.size();
3093 unsigned MinSize = LSize < RSize ? LSize : RSize;
3094 unsigned Count = 0;
3095
3096 for (; Count != MinSize; ++Count)
3097 if (LIds[Count] != RIds[Count])
3098 return Count;
3099
3100 return Count;
3101 }
3102
3103 /// PadLT - Order landing pads lexicographically by type id.
3104 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3105 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3106 unsigned LSize = LIds.size(), RSize = RIds.size();
3107 unsigned MinSize = LSize < RSize ? LSize : RSize;
3108
3109 for (unsigned i = 0; i != MinSize; ++i)
3110 if (LIds[i] != RIds[i])
3111 return LIds[i] < RIds[i];
3112
3113 return LSize < RSize;
3114 }
3115
3116 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 Lattner92eea072007-09-17 18:34:04 +00003120 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003121 static bool isPod() { return true; }
3122 };
3123
Duncan Sands241a0c92007-09-05 11:27:52 +00003124 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003125 struct ActionEntry {
3126 int ValueForTypeID; // The value to write - may not be equal to the type id.
3127 int NextAction;
3128 struct ActionEntry *Previous;
3129 };
3130
Duncan Sands241a0c92007-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 Sands4ff179f2007-12-19 07:36:31 +00003143 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-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 Sands4ff179f2007-12-19 07:36:31 +00003146 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003147 unsigned PadLabel; // zero indicates that there is no landing pad.
3148 unsigned Action;
3149 };
3150
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003151 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003152 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3153 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3154 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.
3159 SmallVector<const LandingPadInfo *, 64> LandingPads;
3160 LandingPads.reserve(PadInfos.size());
3161 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3162 LandingPads.push_back(&PadInfos[i]);
3163 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3164
Dan Gohmanf17a25c2007-07-18 16:29:46 +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);
aslc200b112008-08-16 12:57:46 +00003181 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003182 }
3183
Duncan Sands241a0c92007-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());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003189
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003190 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00003191 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003192 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3193 const LandingPadInfo *LP = LandingPads[i];
3194 const std::vector<int> &TypeIds = LP->TypeIds;
3195 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3196 unsigned SizeSiteActions = 0;
3197
3198 if (NumShared < TypeIds.size()) {
3199 unsigned SizeAction = 0;
3200 ActionEntry *PrevAction = 0;
3201
3202 if (NumShared) {
3203 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3204 assert(Actions.size());
3205 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00003206 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3207 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003208 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00003209 SizeAction -=
3210 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003211 SizeAction += -PrevAction->NextAction;
3212 PrevAction = PrevAction->Previous;
3213 }
3214 }
3215
3216 // Compute the actions.
3217 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3218 int TypeID = TypeIds[I];
3219 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3220 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00003221 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003222
3223 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00003224 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003225 SizeSiteActions += SizeAction;
3226
3227 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
3228 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);
3238
3239 // Compute this sites contribution to size.
3240 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003241 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003242
Duncan Sands4ff179f2007-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 Sands241a0c92007-09-05 11:27:52 +00003249 SmallVector<CallSiteEntry, 64> CallSites;
3250
3251 RangeMapType PadMap;
Duncan Sands4ff179f2007-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 Sands241a0c92007-09-05 11:27:52 +00003255 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3256 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003257 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-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 Sands4ff179f2007-12-19 07:36:31 +00003265 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00003266 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-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 Sands4ff179f2007-12-19 07:36:31 +00003275 // Visit all instructions in order of address.
Duncan Sands241a0c92007-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 Gohmanfa607c92008-07-01 00:05:16 +00003280 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00003281 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00003282 continue;
3283 }
3284
Chris Lattnerda4cff12007-12-30 20:50:28 +00003285 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00003286 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00003287
Duncan Sands4ff179f2007-12-19 07:36:31 +00003288 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00003289 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00003290 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003291
Duncan Sands4ff179f2007-12-19 07:36:31 +00003292 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00003293 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00003294 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00003295 // Nope, it was just some random label.
Duncan Sands241a0c92007-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 Sands4ff179f2007-12-19 07:36:31 +00003307 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003308 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3309 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00003310 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003311 }
3312
3313 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003314 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00003315
Duncan Sands4ff179f2007-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 Sands241a0c92007-09-05 11:27:52 +00003320
Duncan Sands4ff179f2007-12-19 07:36:31 +00003321 // Try to merge with the previous call-site.
3322 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00003323 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-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 Sands241a0c92007-09-05 11:27:52 +00003329 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003330
Duncan Sands4ff179f2007-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 Sands241a0c92007-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 Sands4ff179f2007-12-19 07:36:31 +00003343 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003344 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3345 CallSites.push_back(Site);
3346 }
3347
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003348 // Final tallies.
Duncan Sands96144f92008-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 Sands241a0c92007-09-05 11:27:52 +00003357 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00003358 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00003359
Duncan Sands96144f92008-05-07 19:11:09 +00003360 // Type infos.
3361 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3362 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003363
3364 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00003365 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003366 SizeSites + SizeActions + SizeTypes;
3367
3368 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3369 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00003370 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003371 TypeOffset;
3372
3373 unsigned SizeAlign = (4 - TotalSize) & 3;
3374
3375 // Begin the exception table.
3376 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00003377 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00003378 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003379 for (unsigned i = 0; i != SizeAlign; ++i) {
3380 Asm->EmitInt8(0);
3381 Asm->EOL("Padding");
3382 }
3383 EmitLabel("exception", SubprogramCount);
3384
3385 // 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");
3396
Duncan Sands241a0c92007-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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003402
Duncan Sands241a0c92007-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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003409 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003410
Duncan Sands241a0c92007-09-05 11:27:52 +00003411 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003412 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003413 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003414
Duncan Sands241a0c92007-09-05 11:27:52 +00003415 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00003416 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00003417 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003418 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00003419 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003420 }
3421 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003422
Duncan Sands96144f92008-05-07 19:11:09 +00003423 if (!S.PadLabel)
3424 Asm->EmitInt32(0);
3425 else
Duncan Sands241a0c92007-09-05 11:27:52 +00003426 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003427 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003428 Asm->EOL("Landing pad");
3429
3430 Asm->EmitULEB128Bytes(S.Action);
3431 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003432 }
3433
3434 // Emit the actions.
3435 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3436 ActionEntry &Action = Actions[I];
3437
3438 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
3439 Asm->EOL("TypeInfo index");
3440 Asm->EmitSLEB128Bytes(Action.NextAction);
3441 Asm->EOL("Next action");
3442 }
3443
3444 // Emit the type ids.
3445 for (unsigned M = TypeInfos.size(); M; --M) {
3446 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00003447
3448 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003449
Bill Wendling26a8ab92009-04-10 00:12:49 +00003450 if (GV) {
3451 std::string GLN;
3452 O << Asm->getGlobalLinkName(GV, GLN);
3453 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003454 O << "0";
Bill Wendling26a8ab92009-04-10 00:12:49 +00003455 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003456
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003457 Asm->EOL("TypeInfo");
3458 }
3459
3460 // Emit the filter typeids.
3461 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3462 unsigned TypeID = FilterIds[j];
3463 Asm->EmitULEB128Bytes(TypeID);
3464 Asm->EOL("Filter TypeInfo index");
3465 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003466
Evan Cheng7e7d1942008-02-29 19:36:59 +00003467 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003468 }
3469
3470public:
3471 //===--------------------------------------------------------------------===//
3472 // Main entry points.
3473 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003474 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendlingd9308a62009-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 Wendling148ecc42009-03-10 22:58:53 +00003480 getDwarfTimerGroup());
Bill Wendlingd9308a62009-03-10 21:23:25 +00003481 }
aslc200b112008-08-16 12:57:46 +00003482
Bill Wendlingd9308a62009-03-10 21:23:25 +00003483 virtual ~DwarfException() {
3484 delete ExceptionTimer;
3485 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +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) {
3490 MMI = mmi;
3491 }
3492
3493 /// BeginModule - Emit all exception information that should come prior to the
3494 /// content.
3495 void BeginModule(Module *M) {
3496 this->M = M;
3497 }
3498
3499 /// EndModule - Emit all exception information that should come after the
3500 /// content.
3501 void EndModule() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003502 if (TimePassesIsEnabled)
3503 ExceptionTimer->startTimer();
3504
Dale Johannesen85535762008-04-02 00:25:04 +00003505 if (shouldEmitMovesModule || shouldEmitTableModule) {
3506 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Cheng3e288912009-02-25 07:04:34 +00003507 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen85535762008-04-02 00:25:04 +00003508 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003509
Dale Johannesen85535762008-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 Wendlingd9308a62009-03-10 21:23:25 +00003514
3515 if (TimePassesIsEnabled)
3516 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003517 }
3518
aslc200b112008-08-16 12:57:46 +00003519 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003520 /// emitted immediately after the function entry point.
3521 void BeginFunction(MachineFunction *MF) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003522 if (TimePassesIsEnabled)
3523 ExceptionTimer->startTimer();
3524
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003525 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00003526 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen85535762008-04-02 00:25:04 +00003527
Bill Wendlingd9308a62009-03-10 21:23:25 +00003528 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00003529 // Map all labels and get rid of any dead landing pads.
3530 MMI->TidyLandingPads();
Bill Wendlingd9308a62009-03-10 21:23:25 +00003531
Dale Johannesen85535762008-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 Sandscbc28b12008-07-04 09:55:48 +00003537 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003543 }
Bill Wendlingd9308a62009-03-10 21:23:25 +00003544
Dale Johannesen85535762008-04-02 00:25:04 +00003545 shouldEmitTableModule |= shouldEmitTable;
3546 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendlingd9308a62009-03-10 21:23:25 +00003547
3548 if (TimePassesIsEnabled)
3549 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003550 }
3551
3552 /// EndFunction - Gather and emit post-function exception information.
3553 ///
3554 void EndFunction() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003555 if (TimePassesIsEnabled)
3556 ExceptionTimer->startTimer();
3557
Dale Johannesen85535762008-04-02 00:25:04 +00003558 if (shouldEmitMoves || shouldEmitTable) {
3559 EmitLabel("eh_func_end", SubprogramCount);
3560 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003561
Dale Johannesen85535762008-04-02 00:25:04 +00003562 // Save EH frame information
Bill Wendling26a8ab92009-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 Wendlingd9308a62009-03-10 21:23:25 +00003572 }
3573
3574 if (TimePassesIsEnabled)
3575 ExceptionTimer->stopTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003576 }
3577};
3578
3579} // End of namespace llvm
3580
3581//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003582/// DwarfWriter Implementation
3583///
3584
Bill Wendlingcb3661f2009-03-10 20:41:52 +00003585DwarfWriter::DwarfWriter()
Bill Wendlingd9308a62009-03-10 21:23:25 +00003586 : ImmutablePass(&ID), DD(0), DE(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003587
3588DwarfWriter::~DwarfWriter() {
3589 delete DE;
3590 delete DD;
3591}
3592
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003593/// BeginModule - Emit all Dwarf sections that should come prior to the
3594/// content.
Devang Patelaa1e8432009-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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003601 DE->BeginModule(M);
3602 DD->BeginModule(M);
Devang Patel6ccd57e2009-01-13 00:20:51 +00003603 DD->SetDebugInfo(MMI);
Devang Patelaa1e8432009-01-08 23:40:34 +00003604 DE->SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003605}
3606
3607/// EndModule - Emit all Dwarf sections that should come after the content.
3608///
3609void DwarfWriter::EndModule() {
3610 DE->EndModule();
3611 DD->EndModule();
3612}
3613
aslc200b112008-08-16 12:57:46 +00003614/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003615/// emitted immediately after the function entry point.
3616void DwarfWriter::BeginFunction(MachineFunction *MF) {
3617 DE->BeginFunction(MF);
3618 DD->BeginFunction(MF);
3619}
3620
3621/// EndFunction - Gather and emit post-function debug information.
3622///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003623void DwarfWriter::EndFunction(MachineFunction *MF) {
3624 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003625 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00003626
Bill Wendling5b4796a2008-07-22 00:53:37 +00003627 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003628 // Clear function debug information.
3629 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003630}
Devang Patelcb59fd42009-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,
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00003636 DICompileUnit CU) {
3637 return DD->RecordSourceLine(Line, Col, CU);
Devang Patelcb59fd42009-01-12 19:17:34 +00003638}
3639
3640/// RecordRegionStart - Indicate the start of a region.
3641unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003642 return DD->RecordRegionStart(V);
Devang Patelcb59fd42009-01-12 19:17:34 +00003643}
3644
3645/// RecordRegionEnd - Indicate the end of a region.
Dan Gohmand46dc022009-05-07 19:46:24 +00003646unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
3647 return DD->RecordRegionEnd(V);
Devang Patelcb59fd42009-01-12 19:17:34 +00003648}
3649
3650/// getRecordSourceLineCount - Count source lines.
3651unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendlingd9308a62009-03-10 21:23:25 +00003652 return DD->getRecordSourceLineCount();
Devang Patelcb59fd42009-01-12 19:17:34 +00003653}
Devang Patel70190872009-01-13 21:25:00 +00003654
Devang Patelfe359e72009-01-13 21:44:10 +00003655/// RecordVariable - Indicate the declaration of a local variable.
3656///
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003657void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
3658 const MachineInstr *MI) {
3659 DD->RecordVariable(GV, FrameIndex, MI);
Devang Patelfe359e72009-01-13 21:44:10 +00003660}
Devang Patel42f6bed2009-01-13 23:54:55 +00003661
Bill Wendling50db0792009-02-20 00:44:43 +00003662/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3663/// be emitted.
3664bool DwarfWriter::ShouldEmitDwarfDebug() const {
Argiris Kirtzidis25657342009-05-03 08:50:41 +00003665 return DD && DD->ShouldEmitDwarfDebug();
Bill Wendling50db0792009-02-20 00:44:43 +00003666}
Devang Patel88bf96e2009-04-13 17:02:03 +00003667
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003668//// RecordInlinedFnStart - Global variable GV is inlined at the location marked
Devang Patel88bf96e2009-04-13 17:02:03 +00003669//// by LabelID label.
Argiris Kirtzidisf4510c02009-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 Patel88bf96e2009-04-13 17:02:03 +00003673}
3674
Devang Patel8a9a7dc2009-04-15 00:10:26 +00003675/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
Argiris Kirtzidisf4510c02009-05-07 00:16:31 +00003676unsigned DwarfWriter::RecordInlinedFnEnd(DISubprogram SP) {
Devang Patel8a9a7dc2009-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}