blob: c77a7537cbaa5dd7530d5564d5d7236a40487575 [file] [log] [blame]
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework --------------------===//
Jim Laskeye5032892005-12-21 19:48:16 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskeye5032892005-12-21 19:48:16 +00007//
8//===----------------------------------------------------------------------===//
9//
Jim Laskey072200c2007-01-29 18:51:14 +000010// This file contains support for writing dwarf info into asm files.
Jim Laskeye5032892005-12-21 19:48:16 +000011//
12//===----------------------------------------------------------------------===//
Jim Laskey3ea0e0e2006-01-27 18:32:41 +000013
Jim Laskeyb2efb852006-01-04 22:28:25 +000014#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskey52060a02006-01-24 00:49:18 +000015#include "llvm/Module.h"
Devang Pateld1ca9252009-01-05 23:03:32 +000016#include "llvm/DerivedTypes.h"
Devang Patelcf3a4482009-01-15 23:41:32 +000017#include "llvm/Constants.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000018#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000021#include "llvm/CodeGen/MachineLocation.h"
Devang Patel08f053f2009-01-05 17:57:47 +000022#include "llvm/Analysis/DebugInfo.h"
Jim Laskey3f09fc22007-02-28 18:38:31 +000023#include "llvm/Support/Debug.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000024#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000025#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000026#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000027#include "llvm/Support/Mangler.h"
Bill Wendling91b8b802009-03-10 20:41:52 +000028#include "llvm/Support/Timer.h"
Owen Andersoncb371882008-08-21 00:14:44 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohman85496362007-09-24 21:32:18 +000030#include "llvm/System/Path.h"
Jim Laskey563321a2006-09-06 18:34:40 +000031#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000033#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000034#include "llvm/Target/TargetFrameInfo.h"
Duncan Sands53c3a332007-05-16 12:12:23 +000035#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000036#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000037#include "llvm/Target/TargetOptions.h"
Evan Chenge3d42322009-02-25 07:04:34 +000038#include "llvm/ADT/DenseMap.h"
39#include "llvm/ADT/FoldingSet.h"
40#include "llvm/ADT/StringExtras.h"
41#include "llvm/ADT/StringMap.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000042#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000043#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000044using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000045using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000046
Devang Pateleb3fc282009-01-08 23:40:34 +000047static RegisterPass<DwarfWriter>
48X("dwarfwriter", "DWARF Information Writer");
49char DwarfWriter::ID = 0;
50
Bill Wendling68edf5f2009-03-10 22:58:53 +000051static TimerGroup &getDwarfTimerGroup() {
52 static TimerGroup DwarfTimerGroup("Dwarf Exception and Debugging");
53 return DwarfTimerGroup;
Bill Wendling91b8b802009-03-10 20:41:52 +000054}
55
Jim Laskey0d086af2006-02-27 12:43:29 +000056namespace llvm {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000057
Jim Laskey65195462006-10-30 13:35:07 +000058//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000059
60/// Configuration values for initial hash set sizes (log2).
61///
Bill Wendlingb9dcef22009-02-03 21:17:20 +000062static const unsigned InitDiesSetSize = 9; // log2(512)
63static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
64static const unsigned InitValuesSetSize = 9; // log2(512)
Jim Laskeyef42a012006-11-02 20:12:39 +000065
66//===----------------------------------------------------------------------===//
67/// Forward declarations.
68///
69class DIE;
70class DIEValue;
71
72//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000073/// DWLabel - Labels are used to track locations in the assembler file.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000074/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
75/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer181b6c92007-08-05 20:06:04 +000076/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +000077class DWLabel {
78public:
Jim Laskeyef42a012006-11-02 20:12:39 +000079 /// Tag - Label category tag. Should always be a staticly declared C string.
80 ///
81 const char *Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000082
Jim Laskeyef42a012006-11-02 20:12:39 +000083 /// Number - Value to make label unique.
84 ///
85 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +000086
87 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000088
Jim Laskeyef42a012006-11-02 20:12:39 +000089 void Profile(FoldingSetNodeID &ID) const {
Evan Chenge3d42322009-02-25 07:04:34 +000090 ID.AddString(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +000091 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +000092 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000093
Jim Laskeyef42a012006-11-02 20:12:39 +000094#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +000095 void print(std::ostream *O) const {
96 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +000097 }
Jim Laskeyef42a012006-11-02 20:12:39 +000098 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +000099 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +0000100 if (Number) O << Number;
101 }
102#endif
Jim Laskeybd761842006-02-27 17:27:12 +0000103};
104
105//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000106/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
107/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000108class DIEAbbrevData {
Jim Laskeyef42a012006-11-02 20:12:39 +0000109 /// Attribute - Dwarf attribute code.
110 ///
111 unsigned Attribute;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000112
Jim Laskeyef42a012006-11-02 20:12:39 +0000113 /// Form - Dwarf form code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000114 ///
115 unsigned Form;
Jim Laskey0d086af2006-02-27 12:43:29 +0000116public:
Bill Wendlingdd446322009-03-10 23:57:09 +0000117 DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000118
Jim Laskeybd761842006-02-27 17:27:12 +0000119 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000120 unsigned getAttribute() const { return Attribute; }
121 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000122
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000123 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000124 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000125 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000126 ID.AddInteger(Attribute);
127 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000128 }
129};
Jim Laskey063e7652006-01-17 17:31:53 +0000130
Jim Laskey0d086af2006-02-27 12:43:29 +0000131//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000132/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
133/// information object.
134class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000135private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000136 /// Tag - Dwarf tag code.
137 ///
138 unsigned Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000139
Jim Laskeyef42a012006-11-02 20:12:39 +0000140 /// Unique number for node.
141 ///
142 unsigned Number;
143
144 /// ChildrenFlag - Dwarf children flag.
145 ///
146 unsigned ChildrenFlag;
147
148 /// Data - Raw data bytes for abbreviation.
149 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000150 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey0d086af2006-02-27 12:43:29 +0000151public:
Bill Wendlingdd446322009-03-10 23:57:09 +0000152 DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
153 virtual ~DIEAbbrev() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000154
Jim Laskeybd761842006-02-27 17:27:12 +0000155 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000156 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000157 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000158 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000159 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000160 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000161 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000162 void setNumber(unsigned N) { Number = N; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000163
Jim Laskey0d086af2006-02-27 12:43:29 +0000164 /// AddAttribute - Adds another set of attribute information to the
165 /// abbreviation.
166 void AddAttribute(unsigned Attribute, unsigned Form) {
167 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000168 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000169
Jim Laskeyb8509c52006-03-23 18:07:55 +0000170 /// AddFirstAttribute - Adds a set of attribute information to the front
171 /// of the abbreviation.
172 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
173 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
174 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000175
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000176 /// Profile - Used to gather unique data for the abbreviation folding set.
177 ///
178 void Profile(FoldingSetNodeID &ID) {
179 ID.AddInteger(Tag);
180 ID.AddInteger(ChildrenFlag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000181
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000182 // For each attribute description.
183 for (unsigned i = 0, N = Data.size(); i < N; ++i)
184 Data[i].Profile(ID);
185 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000186
Jim Laskey0d086af2006-02-27 12:43:29 +0000187 /// Emit - Print the abbreviation using the specified Dwarf writer.
188 ///
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000189 void Emit(const DwarfDebug &DD) const;
190
Jim Laskey0d086af2006-02-27 12:43:29 +0000191#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000192 void print(std::ostream *O) {
193 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000194 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000195 void print(std::ostream &O);
196 void dump();
197#endif
198};
Jim Laskey063e7652006-01-17 17:31:53 +0000199
Jim Laskey0d086af2006-02-27 12:43:29 +0000200//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000201/// DIE - A structured debug information entry. Has an abbreviation which
202/// describes it's organization.
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000203class CompileUnit;
Jim Laskeyef42a012006-11-02 20:12:39 +0000204class DIE : public FoldingSetNode {
205protected:
206 /// Abbrev - Buffer for constructing abbreviation.
207 ///
208 DIEAbbrev Abbrev;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000209
Jim Laskeyef42a012006-11-02 20:12:39 +0000210 /// Offset - Offset in debug info section.
211 ///
212 unsigned Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000213
Jim Laskeyef42a012006-11-02 20:12:39 +0000214 /// Size - Size of instance + children.
215 ///
216 unsigned Size;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000217
Jim Laskeyef42a012006-11-02 20:12:39 +0000218 /// Children DIEs.
219 ///
220 std::vector<DIE *> Children;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000221
Jim Laskeyef42a012006-11-02 20:12:39 +0000222 /// Attributes values.
223 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000224 SmallVector<DIEValue*, 32> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000225
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000226 /// Abstract compile unit.
227 CompileUnit *AbstractCU;
Jim Laskeyef42a012006-11-02 20:12:39 +0000228public:
Dan Gohman81975f62007-08-27 14:50:10 +0000229 explicit DIE(unsigned Tag)
Bill Wendlinga9519572009-05-08 20:38:02 +0000230 : Abbrev(Tag, DW_CHILDREN_no), Offset(0), Size(0) {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000231 virtual ~DIE();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000232
Jim Laskeyef42a012006-11-02 20:12:39 +0000233 // Accessors.
234 DIEAbbrev &getAbbrev() { return Abbrev; }
235 unsigned getAbbrevNumber() const {
236 return Abbrev.getNumber();
237 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000238 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000239 unsigned getOffset() const { return Offset; }
240 unsigned getSize() const { return Size; }
241 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000242 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000243 CompileUnit *getAbstractCompileUnit() const { return AbstractCU; }
244
Jim Laskeyef42a012006-11-02 20:12:39 +0000245 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
246 void setOffset(unsigned O) { Offset = O; }
247 void setSize(unsigned S) { Size = S; }
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000248 void setAbstractCompileUnit(CompileUnit *CU) { AbstractCU = CU; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000249
Jim Laskeyef42a012006-11-02 20:12:39 +0000250 /// AddValue - Add a value and attributes to a DIE.
251 ///
252 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
253 Abbrev.AddAttribute(Attribute, Form);
254 Values.push_back(Value);
255 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000256
Jim Laskeyef42a012006-11-02 20:12:39 +0000257 /// SiblingOffset - Return the offset of the debug information entry's
258 /// sibling.
259 unsigned SiblingOffset() const { return Offset + Size; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000260
Jim Laskeyef42a012006-11-02 20:12:39 +0000261 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
262 ///
263 void AddSiblingOffset();
264
265 /// AddChild - Add a child to the DIE.
266 ///
267 void AddChild(DIE *Child) {
268 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
269 Children.push_back(Child);
270 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000271
Jim Laskeyef42a012006-11-02 20:12:39 +0000272 /// Detach - Detaches objects connected to it after copying.
273 ///
274 void Detach() {
275 Children.clear();
276 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000277
Jim Laskeyef42a012006-11-02 20:12:39 +0000278 /// Profile - Used to gather unique data for the value folding set.
279 ///
280 void Profile(FoldingSetNodeID &ID) ;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000281
Jim Laskeyef42a012006-11-02 20:12:39 +0000282#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000283 void print(std::ostream *O, unsigned IncIndent = 0) {
284 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000285 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000286 void print(std::ostream &O, unsigned IncIndent = 0);
287 void dump();
288#endif
289};
290
291//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000292/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000293///
294class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000295public:
296 enum {
297 isInteger,
298 isString,
299 isLabel,
300 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000301 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000302 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000303 isEntry,
304 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000305 };
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000306
Jim Laskeyef42a012006-11-02 20:12:39 +0000307 /// Type - Type of data stored in the value.
308 ///
309 unsigned Type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000310
Bill Wendlingdd446322009-03-10 23:57:09 +0000311 explicit DIEValue(unsigned T) : Type(T) {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000312 virtual ~DIEValue() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000313
Jim Laskeyf6733882006-11-02 21:48:18 +0000314 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000315 unsigned getType() const { return Type; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000316
Jim Laskey0d086af2006-02-27 12:43:29 +0000317 // Implement isa/cast/dyncast.
318 static bool classof(const DIEValue *) { return true; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000319
Jim Laskey0d086af2006-02-27 12:43:29 +0000320 /// EmitValue - Emit value via the Dwarf writer.
321 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000322 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000323
Jim Laskey0d086af2006-02-27 12:43:29 +0000324 /// SizeOf - Return the size of a value in bytes.
325 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000326 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000327
Jim Laskeyef42a012006-11-02 20:12:39 +0000328 /// Profile - Used to gather unique data for the value folding set.
329 ///
330 virtual void Profile(FoldingSetNodeID &ID) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000331
Jim Laskeyef42a012006-11-02 20:12:39 +0000332#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000333 void print(std::ostream *O) {
334 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000335 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000336 virtual void print(std::ostream &O) = 0;
337 void dump();
338#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000339};
Jim Laskey063e7652006-01-17 17:31:53 +0000340
Jim Laskey0d086af2006-02-27 12:43:29 +0000341//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000342/// DWInteger - An integer value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000343///
Jim Laskey0d086af2006-02-27 12:43:29 +0000344class DIEInteger : public DIEValue {
345private:
346 uint64_t Integer;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000347
Jim Laskey0d086af2006-02-27 12:43:29 +0000348public:
Dan Gohman81975f62007-08-27 14:50:10 +0000349 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000350
Jim Laskey0d086af2006-02-27 12:43:29 +0000351 // Implement isa/cast/dyncast.
352 static bool classof(const DIEInteger *) { return true; }
353 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000354
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000355 /// BestForm - Choose the best form for integer.
356 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000357 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
358 if (IsSigned) {
359 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
360 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
361 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
362 } else {
363 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
364 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
365 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
366 }
367 return DW_FORM_data8;
368 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000369
Jim Laskey0d086af2006-02-27 12:43:29 +0000370 /// EmitValue - Emit integer of appropriate size.
371 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000372 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000373
Jim Laskey0d086af2006-02-27 12:43:29 +0000374 /// SizeOf - Determine size of integer value in bytes.
375 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000376 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000377
Jim Laskeyef42a012006-11-02 20:12:39 +0000378 /// Profile - Used to gather unique data for the value folding set.
379 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000380 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000381 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000382 ID.AddInteger(Integer);
383 }
Jim Laskey5496f012006-11-09 14:52:14 +0000384 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000385
Jim Laskeyef42a012006-11-02 20:12:39 +0000386#ifndef NDEBUG
387 virtual void print(std::ostream &O) {
388 O << "Int: " << (int64_t)Integer
389 << " 0x" << std::hex << Integer << std::dec;
390 }
391#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000392};
Jim Laskey063e7652006-01-17 17:31:53 +0000393
Jim Laskey0d086af2006-02-27 12:43:29 +0000394//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000395/// DIEString - A string value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000396///
Jim Laskeyef42a012006-11-02 20:12:39 +0000397class DIEString : public DIEValue {
Bill Wendlingdd446322009-03-10 23:57:09 +0000398 const std::string Str;
Jim Laskeyef42a012006-11-02 20:12:39 +0000399public:
Bill Wendlingdd446322009-03-10 23:57:09 +0000400 explicit DIEString(const std::string &S) : DIEValue(isString), Str(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000401
Jim Laskey0d086af2006-02-27 12:43:29 +0000402 // Implement isa/cast/dyncast.
403 static bool classof(const DIEString *) { return true; }
404 static bool classof(const DIEValue *S) { return S->Type == isString; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000405
Jim Laskey0d086af2006-02-27 12:43:29 +0000406 /// EmitValue - Emit string value.
407 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000408 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000409
Jim Laskey0d086af2006-02-27 12:43:29 +0000410 /// SizeOf - Determine size of string value in bytes.
411 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000412 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Bill Wendlingdd446322009-03-10 23:57:09 +0000413 return Str.size() + sizeof(char); // sizeof('\0');
Jim Laskeyef42a012006-11-02 20:12:39 +0000414 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000415
Jim Laskeyef42a012006-11-02 20:12:39 +0000416 /// Profile - Used to gather unique data for the value folding set.
417 ///
Bill Wendlingdd446322009-03-10 23:57:09 +0000418 static void Profile(FoldingSetNodeID &ID, const std::string &Str) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000419 ID.AddInteger(isString);
Bill Wendlingdd446322009-03-10 23:57:09 +0000420 ID.AddString(Str);
Jim Laskeyef42a012006-11-02 20:12:39 +0000421 }
Bill Wendlingdd446322009-03-10 23:57:09 +0000422 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Str); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000423
Jim Laskeyef42a012006-11-02 20:12:39 +0000424#ifndef NDEBUG
425 virtual void print(std::ostream &O) {
Bill Wendlingdd446322009-03-10 23:57:09 +0000426 O << "Str: \"" << Str << "\"";
Jim Laskeyef42a012006-11-02 20:12:39 +0000427 }
428#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000429};
Jim Laskey063e7652006-01-17 17:31:53 +0000430
Jim Laskey0d086af2006-02-27 12:43:29 +0000431//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000432/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000433//
Jim Laskeyef42a012006-11-02 20:12:39 +0000434class DIEDwarfLabel : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000435 const DWLabel Label;
Bill Wendlingdd446322009-03-10 23:57:09 +0000436public:
Dan Gohman81975f62007-08-27 14:50:10 +0000437 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000438
Jim Laskey0d086af2006-02-27 12:43:29 +0000439 // Implement isa/cast/dyncast.
440 static bool classof(const DIEDwarfLabel *) { return true; }
441 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000442
Jim Laskey0d086af2006-02-27 12:43:29 +0000443 /// EmitValue - Emit label value.
444 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000445 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000446
Jim Laskey0d086af2006-02-27 12:43:29 +0000447 /// SizeOf - Determine size of label value in bytes.
448 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000449 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000450
Jim Laskeyef42a012006-11-02 20:12:39 +0000451 /// Profile - Used to gather unique data for the value folding set.
452 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000453 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000454 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000455 Label.Profile(ID);
456 }
Jim Laskey5496f012006-11-09 14:52:14 +0000457 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000458
Jim Laskeyef42a012006-11-02 20:12:39 +0000459#ifndef NDEBUG
460 virtual void print(std::ostream &O) {
461 O << "Lbl: ";
462 Label.print(O);
463 }
464#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000465};
Jim Laskey063e7652006-01-17 17:31:53 +0000466
Jim Laskey0d086af2006-02-27 12:43:29 +0000467//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000468/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000469//
Jim Laskeyef42a012006-11-02 20:12:39 +0000470class DIEObjectLabel : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000471 const std::string Label;
Bill Wendlingdd446322009-03-10 23:57:09 +0000472public:
Dan Gohman81975f62007-08-27 14:50:10 +0000473 explicit DIEObjectLabel(const std::string &L)
474 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000475
Jim Laskey0d086af2006-02-27 12:43:29 +0000476 // Implement isa/cast/dyncast.
477 static bool classof(const DIEObjectLabel *) { return true; }
478 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000479
Jim Laskey0d086af2006-02-27 12:43:29 +0000480 /// EmitValue - Emit label value.
481 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000482 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000483
Jim Laskey0d086af2006-02-27 12:43:29 +0000484 /// SizeOf - Determine size of label value in bytes.
485 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000486 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000487
Jim Laskeyef42a012006-11-02 20:12:39 +0000488 /// Profile - Used to gather unique data for the value folding set.
489 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000490 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000491 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000492 ID.AddString(Label);
493 }
Evan Chenge3d42322009-02-25 07:04:34 +0000494 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label.c_str()); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000495
496#ifndef NDEBUG
497 virtual void print(std::ostream &O) {
498 O << "Obj: " << Label;
499 }
500#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000501};
Jim Laskey063e7652006-01-17 17:31:53 +0000502
Jim Laskey0d086af2006-02-27 12:43:29 +0000503//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000504/// DIESectionOffset - A section offset DIE.
505//
506class DIESectionOffset : public DIEValue {
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000507 const DWLabel Label;
508 const DWLabel Section;
509 bool IsEH : 1;
510 bool UseSet : 1;
Bill Wendlingdd446322009-03-10 23:57:09 +0000511public:
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000512 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
513 bool isEH = false, bool useSet = true)
Bill Wendlingdd446322009-03-10 23:57:09 +0000514 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
515 IsEH(isEH), UseSet(useSet) {}
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000516
517 // Implement isa/cast/dyncast.
518 static bool classof(const DIESectionOffset *) { return true; }
519 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000520
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000521 /// EmitValue - Emit section offset.
522 ///
523 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000524
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000525 /// SizeOf - Determine size of section offset value in bytes.
526 ///
527 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000528
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000529 /// Profile - Used to gather unique data for the value folding set.
530 ///
531 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
532 const DWLabel &Section) {
533 ID.AddInteger(isSectionOffset);
534 Label.Profile(ID);
535 Section.Profile(ID);
536 // IsEH and UseSet are specific to the Label/Section that we will emit
537 // the offset for; so Label/Section are enough for uniqueness.
538 }
539 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
540
541#ifndef NDEBUG
542 virtual void print(std::ostream &O) {
543 O << "Off: ";
544 Label.print(O);
545 O << "-";
546 Section.print(O);
547 O << "-" << IsEH << "-" << UseSet;
548 }
549#endif
550};
551
552//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000553/// DIEDelta - A simple label difference DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000554///
Jim Laskeyef42a012006-11-02 20:12:39 +0000555class DIEDelta : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000556 const DWLabel LabelHi;
557 const DWLabel LabelLo;
Bill Wendlingdd446322009-03-10 23:57:09 +0000558public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000559 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
Bill Wendlingdd446322009-03-10 23:57:09 +0000560 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000561
Jim Laskey0d086af2006-02-27 12:43:29 +0000562 // Implement isa/cast/dyncast.
563 static bool classof(const DIEDelta *) { return true; }
564 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000565
Jim Laskey0d086af2006-02-27 12:43:29 +0000566 /// EmitValue - Emit delta value.
567 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000568 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000569
Jim Laskey0d086af2006-02-27 12:43:29 +0000570 /// SizeOf - Determine size of delta value in bytes.
571 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000572 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000573
Jim Laskeyef42a012006-11-02 20:12:39 +0000574 /// Profile - Used to gather unique data for the value folding set.
575 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000576 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
577 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000578 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000579 LabelHi.Profile(ID);
580 LabelLo.Profile(ID);
581 }
Jim Laskey5496f012006-11-09 14:52:14 +0000582 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000583
584#ifndef NDEBUG
585 virtual void print(std::ostream &O) {
586 O << "Del: ";
587 LabelHi.print(O);
588 O << "-";
589 LabelLo.print(O);
590 }
591#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000592};
Jim Laskey063e7652006-01-17 17:31:53 +0000593
Jim Laskey0d086af2006-02-27 12:43:29 +0000594//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000595/// DIEntry - A pointer to another debug information entry. An instance of this
596/// class can also be used as a proxy for a debug information entry not yet
597/// defined (ie. types.)
598class DIEntry : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000599 DIE *Entry;
Bill Wendlingdd446322009-03-10 23:57:09 +0000600public:
Dan Gohman81975f62007-08-27 14:50:10 +0000601 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000602
Bill Wendlinga9519572009-05-08 20:38:02 +0000603 DIE *getEntry() const { return Entry; }
Bill Wendlingdd446322009-03-10 23:57:09 +0000604 void setEntry(DIE *E) { Entry = E; }
605
Jim Laskey0d086af2006-02-27 12:43:29 +0000606 // Implement isa/cast/dyncast.
607 static bool classof(const DIEntry *) { return true; }
608 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000609
Jim Laskeyb8509c52006-03-23 18:07:55 +0000610 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000611 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000612 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000613
Jim Laskeyb8509c52006-03-23 18:07:55 +0000614 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000615 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000616 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000617 return sizeof(int32_t);
618 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000619
Jim Laskeyef42a012006-11-02 20:12:39 +0000620 /// Profile - Used to gather unique data for the value folding set.
621 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000622 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
623 ID.AddInteger(isEntry);
624 ID.AddPointer(Entry);
625 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000626 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000627 ID.AddInteger(isEntry);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000628
Jim Laskeyef42a012006-11-02 20:12:39 +0000629 if (Entry) {
630 ID.AddPointer(Entry);
631 } else {
632 ID.AddPointer(this);
633 }
634 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000635
Jim Laskeyef42a012006-11-02 20:12:39 +0000636#ifndef NDEBUG
637 virtual void print(std::ostream &O) {
638 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
639 }
640#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000641};
642
643//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000644/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000645//
Jim Laskeyef42a012006-11-02 20:12:39 +0000646class DIEBlock : public DIEValue, public DIE {
Bill Wendlingdd446322009-03-10 23:57:09 +0000647 unsigned Size; // Size in bytes excluding size header.
Jim Laskeyef42a012006-11-02 20:12:39 +0000648public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000649 DIEBlock()
Bill Wendlingdd446322009-03-10 23:57:09 +0000650 : DIEValue(isBlock), DIE(0), Size(0) {}
651 virtual ~DIEBlock() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000652
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000653 // Implement isa/cast/dyncast.
654 static bool classof(const DIEBlock *) { return true; }
655 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000656
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000657 /// ComputeSize - calculate the size of the block.
658 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000659 unsigned ComputeSize(DwarfDebug &DD);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000660
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000661 /// BestForm - Choose the best form for data.
662 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000663 unsigned BestForm() const {
664 if ((unsigned char)Size == Size) return DW_FORM_block1;
665 if ((unsigned short)Size == Size) return DW_FORM_block2;
666 if ((unsigned int)Size == Size) return DW_FORM_block4;
667 return DW_FORM_block;
668 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000669
670 /// EmitValue - Emit block data.
671 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000672 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000673
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000674 /// SizeOf - Determine size of block data in bytes.
675 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000676 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000677
Jim Laskeyef42a012006-11-02 20:12:39 +0000678 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000679 ///
Reid Spencer97821312006-11-02 23:56:21 +0000680 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000681 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000682 DIE::Profile(ID);
683 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000684
Jim Laskeyef42a012006-11-02 20:12:39 +0000685#ifndef NDEBUG
686 virtual void print(std::ostream &O) {
687 O << "Blk: ";
688 DIE::print(O, 5);
689 }
690#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000691};
692
693//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000694/// CompileUnit - This dwarf writer support class manages information associate
695/// with a source file.
696class CompileUnit {
Jim Laskeyef42a012006-11-02 20:12:39 +0000697 /// ID - File identifier for source.
698 ///
699 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000700
Jim Laskeyef42a012006-11-02 20:12:39 +0000701 /// Die - Compile unit debug information entry.
702 ///
703 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000704
Devang Patelbbdc8202009-01-13 23:54:55 +0000705 /// GVToDieMap - Tracks the mapping of unit level debug informaton
706 /// variables to debug information entries.
Devang Patelc2997f42009-01-20 00:58:55 +0000707 std::map<GlobalVariable *, DIE *> GVToDieMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000708
Devang Patelbbdc8202009-01-13 23:54:55 +0000709 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Jim Laskeyef42a012006-11-02 20:12:39 +0000710 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patelc2997f42009-01-20 00:58:55 +0000711 std::map<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000712
713 /// Globals - A map of globally visible named entities for this unit.
714 ///
Bill Wendling972bbac2009-04-09 21:49:15 +0000715 StringMap<DIE*> Globals;
Jim Laskeyef42a012006-11-02 20:12:39 +0000716
717 /// DiesSet - Used to uniquely define dies within the compile unit.
718 ///
719 FoldingSet<DIE> DiesSet;
Jim Laskey0d086af2006-02-27 12:43:29 +0000720public:
Devang Pateld1ca9252009-01-05 23:03:32 +0000721 CompileUnit(unsigned I, DIE *D)
Devang Patelbbdc8202009-01-13 23:54:55 +0000722 : ID(I), Die(D), GVToDieMap(),
Devang Patel7e55b3a2009-01-17 06:51:37 +0000723 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Pateld1ca9252009-01-05 23:03:32 +0000724 {}
725
Jim Laskeyef42a012006-11-02 20:12:39 +0000726 ~CompileUnit() {
727 delete Die;
Jim Laskeyef42a012006-11-02 20:12:39 +0000728 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000729
Jim Laskeybd761842006-02-27 17:27:12 +0000730 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000731 unsigned getID() const { return ID; }
732 DIE* getDie() const { return Die; }
Bill Wendling972bbac2009-04-09 21:49:15 +0000733 StringMap<DIE*> &getGlobals() { return Globals; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000734
735 /// hasContent - Return true if this compile unit has something to write out.
736 ///
737 bool hasContent() const {
738 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000739 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000740
741 /// AddGlobal - Add a new global entity to the compile unit.
742 ///
743 void AddGlobal(const std::string &Name, DIE *Die) {
744 Globals[Name] = Die;
745 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000746
Jim Laskeyef42a012006-11-02 20:12:39 +0000747 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000748 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000749 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
750 return GVToDieMap[GV];
751 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000752
Jim Laskeyef42a012006-11-02 20:12:39 +0000753 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000754 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000755 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
756 return GVToDIEntryMap[GV];
757 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000758
Jim Laskeyef42a012006-11-02 20:12:39 +0000759 /// AddDie - Adds or interns the DIE to the compile unit.
760 ///
761 DIE *AddDie(DIE &Buffer) {
762 FoldingSetNodeID ID;
763 Buffer.Profile(ID);
764 void *Where;
765 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000766
Jim Laskeyef42a012006-11-02 20:12:39 +0000767 if (!Die) {
768 Die = new DIE(Buffer);
769 DiesSet.InsertNode(Die, Where);
770 this->Die->AddChild(Die);
771 Buffer.Detach();
772 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000773
Jim Laskeyef42a012006-11-02 20:12:39 +0000774 return Die;
775 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000776};
777
Jim Laskey65195462006-10-30 13:35:07 +0000778//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000779/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000780///
Jim Laskey65195462006-10-30 13:35:07 +0000781class Dwarf {
Jim Laskey072200c2007-01-29 18:51:14 +0000782protected:
Jim Laskey65195462006-10-30 13:35:07 +0000783 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000784 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000785 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000786
Jim Laskey65195462006-10-30 13:35:07 +0000787 //
788 /// O - Stream to .s file.
789 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000790 raw_ostream &O;
Jim Laskey65195462006-10-30 13:35:07 +0000791
792 /// Asm - Target of Dwarf emission.
793 ///
794 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000795
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000796 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000797 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000798
Jim Laskey65195462006-10-30 13:35:07 +0000799 /// TD - Target data.
800 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000801
Jim Laskey65195462006-10-30 13:35:07 +0000802 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000803 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000804
Jim Laskey65195462006-10-30 13:35:07 +0000805 /// M - Current module.
806 ///
807 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000808
Jim Laskey65195462006-10-30 13:35:07 +0000809 /// MF - Current machine function.
810 ///
811 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000812
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000813 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000814 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000815 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000816
Jim Laskey65195462006-10-30 13:35:07 +0000817 /// SubprogramCount - The running count of functions being compiled.
818 ///
819 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000820
Chris Lattner9251f132007-09-24 03:35:37 +0000821 /// Flavor - A unique string indicating what dwarf producer this is, used to
822 /// unique labels.
823 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000824
825 unsigned SetCounter;
Owen Andersoncb371882008-08-21 00:14:44 +0000826 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattner9251f132007-09-24 03:35:37 +0000827 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000828 : O(OS)
829 , Asm(A)
830 , TAI(T)
831 , TD(Asm->TM.getTargetData())
832 , RI(Asm->TM.getRegisterInfo())
833 , M(NULL)
834 , MF(NULL)
835 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000836 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000837 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000838 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000839 {
840 }
841
842public:
Jim Laskey072200c2007-01-29 18:51:14 +0000843 //===--------------------------------------------------------------------===//
844 // Accessors.
845 //
Bill Wendlinga6f02fb2009-04-10 00:00:25 +0000846 const AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000847 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000848 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000849 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000850
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000851 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
852 const {
853 if (isInSection && TAI->getDwarfSectionOffsetDirective())
854 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000855 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000856 O << TAI->getData32bitsDirective();
857 else
858 O << TAI->getData64bitsDirective();
859 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000860
Jim Laskeyb82313f2007-02-01 16:31:34 +0000861 /// PrintLabelName - Print label name in form used by Dwarf writer.
862 ///
863 void PrintLabelName(DWLabel Label) const {
864 PrintLabelName(Label.Tag, Label.Number);
865 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000866 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000867 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000868 if (Number) O << Number;
869 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000870
Chris Lattner9251f132007-09-24 03:35:37 +0000871 void PrintLabelName(const char *Tag, unsigned Number,
872 const char *Suffix) const {
873 O << TAI->getPrivateGlobalPrefix() << Tag;
874 if (Number) O << Number;
875 O << Suffix;
876 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000877
Jim Laskeyb82313f2007-02-01 16:31:34 +0000878 /// EmitLabel - Emit location label for internal use by Dwarf.
879 ///
880 void EmitLabel(DWLabel Label) const {
881 EmitLabel(Label.Tag, Label.Number);
882 }
883 void EmitLabel(const char *Tag, unsigned Number) const {
884 PrintLabelName(Tag, Number);
885 O << ":\n";
886 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000887
Jim Laskeyb82313f2007-02-01 16:31:34 +0000888 /// EmitReference - Emit a reference to a label.
889 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000890 void EmitReference(DWLabel Label, bool IsPCRelative = false,
891 bool Force32Bit = false) const {
892 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000893 }
894 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000895 bool IsPCRelative = false, bool Force32Bit = false) const {
896 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000897 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000898
Jim Laskeyb82313f2007-02-01 16:31:34 +0000899 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
900 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000901 void EmitReference(const std::string &Name, bool IsPCRelative = false,
902 bool Force32Bit = false) const {
903 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000904
Jim Laskeyb82313f2007-02-01 16:31:34 +0000905 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000906
Jim Laskeyb82313f2007-02-01 16:31:34 +0000907 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
908 }
909
910 /// EmitDifference - Emit the difference between two labels. Some
911 /// assemblers do not behave with absolute expressions with data directives,
912 /// so there is an option (needsSet) to use an intermediary set expression.
913 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000914 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000915 EmitDifference(LabelHi.Tag, LabelHi.Number,
916 LabelLo.Tag, LabelLo.Number,
917 IsSmall);
918 }
919 void EmitDifference(const char *TagHi, unsigned NumberHi,
920 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000921 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000922 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000923 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000924 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000925 O << ",";
926 PrintLabelName(TagHi, NumberHi);
927 O << "-";
928 PrintLabelName(TagLo, NumberLo);
929 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000930
931 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +0000932 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000933 ++SetCounter;
934 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000935 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000936
Jim Laskeyb82313f2007-02-01 16:31:34 +0000937 PrintLabelName(TagHi, NumberHi);
938 O << "-";
939 PrintLabelName(TagLo, NumberLo);
940 }
941 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000942
943 void EmitSectionOffset(const char* Label, const char* Section,
944 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000945 bool IsSmall = false, bool isEH = false,
946 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000947 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000948 if (isEH)
949 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
950 else
951 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
952
953 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000954 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000955 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000956 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000957 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000958
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000959 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000960 O << "-";
961 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000962 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000963 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000964
965 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000966
Chris Lattner9251f132007-09-24 03:35:37 +0000967 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000968 ++SetCounter;
969 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000970 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000971
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000972 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000973
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000974 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000975 O << "-";
976 PrintLabelName(Section, SectionNumber);
977 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000978 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000979 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000980
Jim Laskeyb82313f2007-02-01 16:31:34 +0000981 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
982 /// frame.
983 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +0000984 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000985 int stackGrowth =
986 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
987 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +0000988 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +0000989 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000990
991 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +0000992 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +0000993 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000994
Jim Laskeyb82313f2007-02-01 16:31:34 +0000995 if (LabelID) {
996 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000997
Jim Laskeyb82313f2007-02-01 16:31:34 +0000998 // Throw out move if the label is invalid.
999 if (!LabelID) continue;
1000 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001001
Jim Laskeyb82313f2007-02-01 16:31:34 +00001002 const MachineLocation &Dst = Move.getDestination();
1003 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001004
Jim Laskeyb82313f2007-02-01 16:31:34 +00001005 // Advance row if new location.
1006 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1007 Asm->EmitInt8(DW_CFA_advance_loc4);
1008 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001009 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1010 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001011
Jim Laskeyb82313f2007-02-01 16:31:34 +00001012 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001013 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001014 IsLocal = true;
1015 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001016
Jim Laskeyb82313f2007-02-01 16:31:34 +00001017 // If advancing cfa.
Dan Gohmand735b802008-10-03 15:45:36 +00001018 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1019 if (!Src.isReg()) {
1020 if (Src.getReg() == MachineLocation::VirtualFP) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001021 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1022 Asm->EOL("DW_CFA_def_cfa_offset");
1023 } else {
1024 Asm->EmitInt8(DW_CFA_def_cfa);
1025 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmand735b802008-10-03 15:45:36 +00001026 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001027 Asm->EOL("Register");
1028 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001029
Jim Laskeyb82313f2007-02-01 16:31:34 +00001030 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001031
Jim Laskeyb82313f2007-02-01 16:31:34 +00001032 Asm->EmitULEB128Bytes(Offset);
1033 Asm->EOL("Offset");
1034 } else {
1035 assert(0 && "Machine move no supported yet.");
1036 }
Dan Gohmand735b802008-10-03 15:45:36 +00001037 } else if (Src.isReg() &&
1038 Src.getReg() == MachineLocation::VirtualFP) {
1039 if (Dst.isReg()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001040 Asm->EmitInt8(DW_CFA_def_cfa_register);
1041 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmand735b802008-10-03 15:45:36 +00001042 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001043 Asm->EOL("Register");
1044 } else {
1045 assert(0 && "Machine move no supported yet.");
1046 }
1047 } else {
Dan Gohmand735b802008-10-03 15:45:36 +00001048 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001049 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001050
Jim Laskeyb82313f2007-02-01 16:31:34 +00001051 if (Offset < 0) {
1052 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1053 Asm->EOL("DW_CFA_offset_extended_sf");
1054 Asm->EmitULEB128Bytes(Reg);
1055 Asm->EOL("Reg");
1056 Asm->EmitSLEB128Bytes(Offset);
1057 Asm->EOL("Offset");
1058 } else if (Reg < 64) {
1059 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng42bf74b2009-03-25 01:47:28 +00001060 if (Asm->isVerbose())
Evan Chengd4114192008-07-09 21:53:02 +00001061 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1062 else
1063 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001064 Asm->EmitULEB128Bytes(Offset);
1065 Asm->EOL("Offset");
1066 } else {
1067 Asm->EmitInt8(DW_CFA_offset_extended);
1068 Asm->EOL("DW_CFA_offset_extended");
1069 Asm->EmitULEB128Bytes(Reg);
1070 Asm->EOL("Reg");
1071 Asm->EmitULEB128Bytes(Offset);
1072 Asm->EOL("Offset");
1073 }
1074 }
1075 }
1076 }
1077
Jim Laskey072200c2007-01-29 18:51:14 +00001078};
1079
1080//===----------------------------------------------------------------------===//
Devang Patelf3ee5142009-01-12 22:54:42 +00001081/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001082///
1083class SrcLineInfo {
1084 unsigned Line; // Source line number.
1085 unsigned Column; // Source column.
1086 unsigned SourceID; // Source ID number.
1087 unsigned LabelID; // Label in code ID number.
1088public:
1089 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001090 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00001091
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001092 // Accessors
1093 unsigned getLine() const { return Line; }
1094 unsigned getColumn() const { return Column; }
1095 unsigned getSourceID() const { return SourceID; }
1096 unsigned getLabelID() const { return LabelID; }
1097};
1098
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001099//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +00001100/// DbgVariable - This class is used to track local variable information.
1101///
1102class DbgVariable {
Devang Patel99ec3532009-01-16 19:28:14 +00001103 DIVariable Var; // Variable Descriptor.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00001104 unsigned FrameIndex; // Variable frame index.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001105public:
Devang Patel99ec3532009-01-16 19:28:14 +00001106 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel7a6e5a32009-01-08 02:33:41 +00001107
1108 // Accessors.
Devang Patel99ec3532009-01-16 19:28:14 +00001109 DIVariable getVariable() const { return Var; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001110 unsigned getFrameIndex() const { return FrameIndex; }
1111};
1112
1113//===----------------------------------------------------------------------===//
1114/// DbgScope - This class is used to track scope information.
1115///
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001116class DbgConcreteScope;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001117class DbgScope {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001118 DbgScope *Parent; // Parent to this scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001119 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001120 // Either subprogram or block.
1121 unsigned StartLabelID; // Label ID of the beginning of scope.
1122 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001123 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel9795da52009-01-10 02:42:49 +00001124 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001125 SmallVector<DbgConcreteScope *, 8> ConcreteInsts;// Concrete insts of funcs.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001126public:
Devang Patel0e5200f2009-01-15 18:25:17 +00001127 DbgScope(DbgScope *P, DIDescriptor D)
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001128 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0) {}
1129 virtual ~DbgScope();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001130
1131 // Accessors.
Devang Patel7103c6a2009-01-16 18:01:58 +00001132 DbgScope *getParent() const { return Parent; }
1133 DIDescriptor getDesc() const { return Desc; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001134 unsigned getStartLabelID() const { return StartLabelID; }
1135 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel9795da52009-01-10 02:42:49 +00001136 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1137 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001138 SmallVector<DbgConcreteScope*,8> &getConcreteInsts() { return ConcreteInsts; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001139 void setStartLabelID(unsigned S) { StartLabelID = S; }
1140 void setEndLabelID(unsigned E) { EndLabelID = E; }
1141
1142 /// AddScope - Add a scope to the scope.
1143 ///
1144 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1145
1146 /// AddVariable - Add a variable to the scope.
1147 ///
1148 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
Devang Patel1be3ecc2009-04-15 00:10:26 +00001149
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001150 /// AddConcreteInst - Add a concrete instance to the scope.
1151 ///
1152 void AddConcreteInst(DbgConcreteScope *C) { ConcreteInsts.push_back(C); }
Bill Wendling3f500d92009-05-06 21:21:34 +00001153
1154#ifndef NDEBUG
1155 void dump() const;
1156#endif
Devang Patel1be3ecc2009-04-15 00:10:26 +00001157};
1158
Bill Wendling3f500d92009-05-06 21:21:34 +00001159#ifndef NDEBUG
1160void DbgScope::dump() const {
1161 static unsigned IndentLevel = 0;
1162 std::string Indent(IndentLevel, ' ');
1163
1164 cerr << Indent; Desc.dump();
1165 cerr << " [" << StartLabelID << ", " << EndLabelID << "]\n";
1166
1167 IndentLevel += 2;
1168
1169 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
1170 if (Scopes[i] != this)
1171 Scopes[i]->dump();
1172
1173 IndentLevel -= 2;
1174}
1175#endif
Devang Patel1be3ecc2009-04-15 00:10:26 +00001176
1177//===----------------------------------------------------------------------===//
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001178/// DbgConcreteScope - This class is used to track a scope that holds concrete
1179/// instance information.
1180///
1181class DbgConcreteScope : public DbgScope {
1182 CompileUnit *Unit;
1183 DIE *Die; // Debug info for this concrete scope.
Devang Patel1be3ecc2009-04-15 00:10:26 +00001184public:
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001185 DbgConcreteScope(DIDescriptor D) : DbgScope(NULL, D) {}
Devang Patel1be3ecc2009-04-15 00:10:26 +00001186
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001187 // Accessors.
1188 DIE *getDie() const { return Die; }
1189 void setDie(DIE *D) { Die = D; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001190};
1191
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001192DbgScope::~DbgScope() {
1193 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
1194 delete Scopes[i];
1195 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
1196 delete Variables[j];
1197 for (unsigned k = 0, O = ConcreteInsts.size(); k < O; ++k)
1198 delete ConcreteInsts[k];
1199}
1200
Devang Patel7a6e5a32009-01-08 02:33:41 +00001201//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001202/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001203///
1204class DwarfDebug : public Dwarf {
Jim Laskey65195462006-10-30 13:35:07 +00001205 //===--------------------------------------------------------------------===//
1206 // Attributes used to construct specific Dwarf sections.
1207 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001208
Evan Chenge3d42322009-02-25 07:04:34 +00001209 /// CompileUnitMap - A map of global variables representing compile units to
1210 /// compile units.
1211 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1212
1213 /// CompileUnits - All the compile units in this module.
1214 ///
1215 SmallVector<CompileUnit *, 8> CompileUnits;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001216
Devang Pateldd9db662009-01-30 18:20:31 +00001217 /// MainCU - Some platform prefers one compile unit per .o file. In such
1218 /// cases, all dies are inserted in MainCU.
1219 CompileUnit *MainCU;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001220
Jim Laskeyef42a012006-11-02 20:12:39 +00001221 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001222 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001223 FoldingSet<DIEAbbrev> AbbreviationsSet;
1224
1225 /// Abbreviations - A list of all the unique abbreviations in use.
1226 ///
1227 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001228
Evan Chenge3d42322009-02-25 07:04:34 +00001229 /// DirectoryIdMap - Directory name to directory id map.
1230 ///
1231 StringMap<unsigned> DirectoryIdMap;
Devang Patel8526cc02009-01-05 22:35:52 +00001232
Evan Chenge3d42322009-02-25 07:04:34 +00001233 /// DirectoryNames - A list of directory names.
1234 SmallVector<std::string, 8> DirectoryNames;
1235
1236 /// SourceFileIdMap - Source file name to source file id map.
1237 ///
1238 StringMap<unsigned> SourceFileIdMap;
1239
1240 /// SourceFileNames - A list of source file names.
1241 SmallVector<std::string, 8> SourceFileNames;
1242
1243 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1244 /// id mapped to a unique id.
1245 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1246
1247 /// SourceIds - Reverse map from source id to directory id + file id pair.
1248 ///
1249 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel8526cc02009-01-05 22:35:52 +00001250
Devang Patel07354e52009-01-16 21:07:53 +00001251 /// Lines - List of of source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001252 std::vector<SrcLineInfo> Lines;
1253
Devang Patel07354e52009-01-16 21:07:53 +00001254 /// ValuesSet - Used to uniquely define values.
1255 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001256 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001257
Jim Laskeyef42a012006-11-02 20:12:39 +00001258 /// Values - A list of all the unique values in use.
1259 ///
1260 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001261
Jim Laskey65195462006-10-30 13:35:07 +00001262 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001263 ///
Jim Laskey65195462006-10-30 13:35:07 +00001264 UniqueVector<std::string> StringPool;
1265
Jim Laskey65195462006-10-30 13:35:07 +00001266 /// SectionMap - Provides a unique id per text section.
1267 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001268 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001269
Jim Laskey65195462006-10-30 13:35:07 +00001270 /// SectionSourceLines - Tracks line numbers per text section.
1271 ///
Devang Patelf3ee5142009-01-12 22:54:42 +00001272 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Jim Laskey65195462006-10-30 13:35:07 +00001273
Jim Laskeybacd3042007-02-21 22:48:45 +00001274 /// didInitial - Flag to indicate if initial emission has been done.
1275 ///
1276 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001277
Jim Laskeybacd3042007-02-21 22:48:45 +00001278 /// shouldEmit - Flag to indicate if debug information should be emitted.
1279 ///
1280 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001281
Devang Patel7d2f9722009-04-15 20:41:31 +00001282 // FunctionDbgScope - Top level scope for the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001283 //
Devang Patel7d2f9722009-04-15 20:41:31 +00001284 DbgScope *FunctionDbgScope;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001285
Bill Wendling163ba3f2009-03-10 21:23:25 +00001286 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001287 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendling163ba3f2009-03-10 21:23:25 +00001288
Bill Wendlingf59d10f2009-05-13 23:55:49 +00001289 /// DbgAbstractScopeMap - Tracks abstract instance scopes in the current
1290 /// function.
1291 DenseMap<GlobalVariable *, DbgScope *> DbgAbstractScopeMap;
1292
1293 /// DbgConcreteScopeMap - Tracks concrete instance scopes in the current
1294 /// function.
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001295 DenseMap<GlobalVariable *,
Bill Wendlingf59d10f2009-05-13 23:55:49 +00001296 SmallVector<DbgScope *, 8> > DbgConcreteScopeMap;
Devang Patel1be3ecc2009-04-15 00:10:26 +00001297
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001298 /// InlineInfo - Keep track of inlined functions and their location. This
1299 /// information is used to populate debug_inlined section.
Devang Patel0f7fef32009-04-13 17:02:03 +00001300 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
1301
Devang Patel1be3ecc2009-04-15 00:10:26 +00001302 /// InlinedVariableScopes - Scopes information for the inlined subroutine
1303 /// variables.
1304 DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
1305
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001306 /// AbstractInstanceRootMap - Map of abstract instance roots of inlined
1307 /// functions. These are subroutine entries that contain a DW_AT_inline
1308 /// attribute.
1309 DenseMap<const GlobalVariable *, DbgScope *> AbstractInstanceRootMap;
1310
1311 /// AbstractInstanceRootList - List of abstract instance roots of inlined
1312 /// functions. These are subroutine entries that contain a DW_AT_inline
1313 /// attribute.
1314 SmallVector<DbgScope *, 32> AbstractInstanceRootList;
1315
1316 /// LexicalScopeStack - A stack of lexical scopes. The top one is the current
1317 /// scope.
1318 SmallVector<DbgScope *, 16> LexicalScopeStack;
1319
Bill Wendlinge688faf2009-05-08 21:03:15 +00001320 /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
1321 /// used when calculating the "origin" of a concrete instance of an inlined
1322 /// function.
1323 DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
1324
Bill Wendling163ba3f2009-03-10 21:23:25 +00001325 /// DebugTimer - Timer for the Dwarf debug writer.
1326 Timer *DebugTimer;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001327
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001328 struct FunctionDebugFrameInfo {
1329 unsigned Number;
1330 std::vector<MachineMove> Moves;
1331
1332 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001333 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001334 };
1335
1336 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001337
Bill Wendling163ba3f2009-03-10 21:23:25 +00001338private:
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001339 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendlingc8615e42009-03-10 21:47:45 +00001340 /// maps to the source id. Source id starts at 1.
1341 std::pair<unsigned, unsigned>
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001342 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendlingc8615e42009-03-10 21:47:45 +00001343 return SourceIds[SId-1];
1344 }
1345
1346 /// getNumSourceDirectories - Return the number of source directories in the
1347 /// debug info.
1348 unsigned getNumSourceDirectories() const {
1349 return DirectoryNames.size();
1350 }
1351
1352 /// getSourceDirectoryName - Return the name of the directory corresponding
1353 /// to the id.
1354 const std::string &getSourceDirectoryName(unsigned Id) const {
1355 return DirectoryNames[Id - 1];
1356 }
1357
1358 /// getSourceFileName - Return the name of the source file corresponding
1359 /// to the id.
1360 const std::string &getSourceFileName(unsigned Id) const {
1361 return SourceFileNames[Id - 1];
1362 }
1363
1364 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendlingc8615e42009-03-10 21:47:45 +00001365 unsigned getNumSourceIds() const {
1366 return SourceIds.size();
1367 }
1368
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001369 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001370 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001371 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1372 // Profile the node so that we can make it unique.
1373 FoldingSetNodeID ID;
1374 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001375
Jim Laskeyef42a012006-11-02 20:12:39 +00001376 // Check the set for priors.
1377 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001378
Jim Laskeyef42a012006-11-02 20:12:39 +00001379 // If it's newly added.
1380 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001381 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001382 Abbreviations.push_back(&Abbrev);
1383 // Assign the vector position + 1 as its number.
1384 Abbrev.setNumber(Abbreviations.size());
1385 } else {
1386 // Assign existing abbreviation number.
1387 Abbrev.setNumber(InSet->getNumber());
1388 }
1389 }
1390
Jim Laskey65195462006-10-30 13:35:07 +00001391 /// NewString - Add a string to the constant pool and returns a label.
1392 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001393 DWLabel NewString(const std::string &String) {
1394 unsigned StringID = StringPool.insert(String);
1395 return DWLabel("string", StringID);
1396 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001397
Jim Laskeyef42a012006-11-02 20:12:39 +00001398 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1399 /// entry.
1400 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1401 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001402
Jim Laskeyef42a012006-11-02 20:12:39 +00001403 if (Entry) {
1404 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001405 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001406 void *Where;
1407 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001408
Jim Laskeyf6733882006-11-02 21:48:18 +00001409 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001410
Jim Laskeyef42a012006-11-02 20:12:39 +00001411 Value = new DIEntry(Entry);
1412 ValuesSet.InsertNode(Value, Where);
1413 } else {
1414 Value = new DIEntry(Entry);
1415 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001416
Jim Laskeyef42a012006-11-02 20:12:39 +00001417 Values.push_back(Value);
1418 return Value;
1419 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001420
Jim Laskeyef42a012006-11-02 20:12:39 +00001421 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1422 ///
1423 void SetDIEntry(DIEntry *Value, DIE *Entry) {
Bill Wendlingdd446322009-03-10 23:57:09 +00001424 Value->setEntry(Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001425 // Add to values set if not already there. If it is, we merely have a
1426 // duplicate in the values list (no harm.)
1427 ValuesSet.GetOrInsertNode(Value);
1428 }
1429
1430 /// AddUInt - Add an unsigned integer attribute data and value.
1431 ///
1432 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1433 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1434
1435 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001436 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001437 void *Where;
1438 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1439 if (!Value) {
1440 Value = new DIEInteger(Integer);
1441 ValuesSet.InsertNode(Value, Where);
1442 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001443 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001444
Jim Laskeyef42a012006-11-02 20:12:39 +00001445 Die->AddValue(Attribute, Form, Value);
1446 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001447
Jim Laskeyef42a012006-11-02 20:12:39 +00001448 /// AddSInt - Add an signed integer attribute data and value.
1449 ///
1450 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1451 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1452
1453 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001454 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001455 void *Where;
1456 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1457 if (!Value) {
1458 Value = new DIEInteger(Integer);
1459 ValuesSet.InsertNode(Value, Where);
1460 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001461 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001462
Jim Laskeyef42a012006-11-02 20:12:39 +00001463 Die->AddValue(Attribute, Form, Value);
1464 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001465
Evan Chenge3d42322009-02-25 07:04:34 +00001466 /// AddString - Add a string attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +00001467 ///
1468 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1469 const std::string &String) {
1470 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001471 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001472 void *Where;
1473 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1474 if (!Value) {
1475 Value = new DIEString(String);
1476 ValuesSet.InsertNode(Value, Where);
1477 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001478 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001479
Jim Laskeyef42a012006-11-02 20:12:39 +00001480 Die->AddValue(Attribute, Form, Value);
1481 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001482
Jim Laskeyef42a012006-11-02 20:12:39 +00001483 /// AddLabel - Add a Dwarf label attribute data and value.
1484 ///
1485 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1486 const DWLabel &Label) {
1487 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001488 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001489 void *Where;
1490 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1491 if (!Value) {
1492 Value = new DIEDwarfLabel(Label);
1493 ValuesSet.InsertNode(Value, Where);
1494 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001495 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001496
Jim Laskeyef42a012006-11-02 20:12:39 +00001497 Die->AddValue(Attribute, Form, Value);
1498 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001499
Jim Laskeyef42a012006-11-02 20:12:39 +00001500 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1501 ///
1502 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1503 const std::string &Label) {
1504 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001505 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001506 void *Where;
1507 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1508 if (!Value) {
1509 Value = new DIEObjectLabel(Label);
1510 ValuesSet.InsertNode(Value, Where);
1511 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001512 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001513
Jim Laskeyef42a012006-11-02 20:12:39 +00001514 Die->AddValue(Attribute, Form, Value);
1515 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001516
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001517 /// AddSectionOffset - Add a section offset label attribute data and value.
1518 ///
1519 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1520 const DWLabel &Label, const DWLabel &Section,
1521 bool isEH = false, bool useSet = true) {
1522 FoldingSetNodeID ID;
1523 DIESectionOffset::Profile(ID, Label, Section);
1524 void *Where;
1525 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1526 if (!Value) {
1527 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1528 ValuesSet.InsertNode(Value, Where);
1529 Values.push_back(Value);
1530 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001531
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001532 Die->AddValue(Attribute, Form, Value);
1533 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001534
Jim Laskeyef42a012006-11-02 20:12:39 +00001535 /// AddDelta - Add a label delta attribute data and value.
1536 ///
1537 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patel1be3ecc2009-04-15 00:10:26 +00001538 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001539 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001540 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001541 void *Where;
1542 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1543 if (!Value) {
1544 Value = new DIEDelta(Hi, Lo);
1545 ValuesSet.InsertNode(Value, Where);
1546 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001547 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001548
Jim Laskeyef42a012006-11-02 20:12:39 +00001549 Die->AddValue(Attribute, Form, Value);
1550 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001551
Jim Laskeyef42a012006-11-02 20:12:39 +00001552 /// AddDIEntry - Add a DIE attribute data and value.
1553 ///
1554 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1555 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1556 }
1557
1558 /// AddBlock - Add block data.
1559 ///
1560 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1561 Block->ComputeSize(*this);
1562 FoldingSetNodeID ID;
1563 Block->Profile(ID);
1564 void *Where;
1565 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1566 if (!Value) {
1567 Value = Block;
1568 ValuesSet.InsertNode(Value, Where);
1569 Values.push_back(Value);
1570 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001571 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001572 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001573 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001574 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001575
Jim Laskeyef42a012006-11-02 20:12:39 +00001576 Die->AddValue(Attribute, Block->BestForm(), Value);
1577 }
1578
Jim Laskey65195462006-10-30 13:35:07 +00001579 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001580 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001581 void AddSourceLine(DIE *Die, const DIVariable *V) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001582 // If there is no compile unit specified, don't add a line #.
1583 if (V->getCompileUnit().isNull())
1584 return;
1585
Devang Patel7a6e5a32009-01-08 02:33:41 +00001586 unsigned Line = V->getLineNumber();
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001587 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
1588 assert(FileID && "Invalid file id");
Devang Patel7a6e5a32009-01-08 02:33:41 +00001589 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1590 AddUInt(Die, DW_AT_decl_line, 0, Line);
1591 }
1592
1593 /// AddSourceLine - Add location information to specified debug information
1594 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001595 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001596 // If there is no compile unit specified, don't add a line #.
1597 if (G->getCompileUnit().isNull())
1598 return;
Devang Patel8526cc02009-01-05 22:35:52 +00001599 unsigned Line = G->getLineNumber();
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001600 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
1601 assert(FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001602 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1603 AddUInt(Die, DW_AT_decl_line, 0, Line);
1604 }
1605
Devang Patel99ec3532009-01-16 19:28:14 +00001606 void AddSourceLine(DIE *Die, const DIType *Ty) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001607 // If there is no compile unit specified, don't add a line #.
Devang Pateldd9db662009-01-30 18:20:31 +00001608 DICompileUnit CU = Ty->getCompileUnit();
1609 if (CU.isNull())
1610 return;
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001611
1612 unsigned Line = Ty->getLineNumber();
1613 unsigned FileID = FindCompileUnit(CU).getID();
1614 assert(FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001615 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1616 AddUInt(Die, DW_AT_decl_line, 0, Line);
1617 }
1618
Jim Laskey65195462006-10-30 13:35:07 +00001619 /// AddAddress - Add an address attribute to a die based on the location
1620 /// provided.
1621 void AddAddress(DIE *Die, unsigned Attribute,
Devang Patel1be3ecc2009-04-15 00:10:26 +00001622 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001623 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001624 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001625
Dan Gohmand735b802008-10-03 15:45:36 +00001626 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001627 if (Reg < 32) {
1628 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1629 } else {
1630 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1631 AddUInt(Block, 0, DW_FORM_udata, Reg);
1632 }
1633 } else {
1634 if (Reg < 32) {
1635 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1636 } else {
1637 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1638 AddUInt(Block, 0, DW_FORM_udata, Reg);
1639 }
1640 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1641 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001642
Jim Laskeyef42a012006-11-02 20:12:39 +00001643 AddBlock(Die, Attribute, 0, Block);
1644 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001645
Jim Laskeyef42a012006-11-02 20:12:39 +00001646 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +00001647 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel80303aa2009-01-23 19:13:31 +00001648 if (Ty.isNull())
Devang Patel48d190f2009-01-05 21:47:57 +00001649 return;
Devang Patel48d190f2009-01-05 21:47:57 +00001650
1651 // Check for pre-existence.
1652 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1653 // If it exists then use the existing value.
1654 if (Slot) {
1655 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1656 return;
1657 }
1658
1659 // Set up proxy.
1660 Slot = NewDIEntry();
1661
1662 // Construct type.
1663 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001664 if (Ty.isBasicType(Ty.getTag()))
1665 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1666 else if (Ty.isDerivedType(Ty.getTag()))
1667 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1668 else {
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001669 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelf193ff02009-01-15 19:26:23 +00001670 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1671 }
1672
Devang Patel7009d242009-01-27 23:22:55 +00001673 // Add debug information entry to entity and appropriate context.
1674 DIE *Die = NULL;
1675 DIDescriptor Context = Ty.getContext();
1676 if (!Context.isNull())
1677 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1678
1679 if (Die) {
1680 DIE *Child = new DIE(Buffer);
1681 Die->AddChild(Child);
1682 Buffer.Detach();
1683 SetDIEntry(Slot, Child);
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001684 } else {
Devang Patel7009d242009-01-27 23:22:55 +00001685 Die = DW_Unit->AddDie(Buffer);
1686 SetDIEntry(Slot, Die);
1687 }
1688
Devang Patel48d190f2009-01-05 21:47:57 +00001689 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1690 }
1691
Devang Patele5202732009-01-05 19:07:53 +00001692 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1693 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001694 DIBasicType BTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001695
Devang Patel08f053f2009-01-05 17:57:47 +00001696 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001697 std::string Name;
1698 BTy.getName(Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001699 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001700 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +00001701 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001702 if (!Name.empty())
Devang Patel08f053f2009-01-05 17:57:47 +00001703 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001704 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +00001705 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1706 }
1707
Devang Patele5202732009-01-05 19:07:53 +00001708 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1709 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001710 DIDerivedType DTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001711
Devang Patel08f053f2009-01-05 17:57:47 +00001712 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001713 std::string Name;
1714 DTy.getName(Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001715 uint64_t Size = DTy.getSizeInBits() >> 3;
1716 unsigned Tag = DTy.getTag();
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001717
Devang Patel08f053f2009-01-05 17:57:47 +00001718 // FIXME - Workaround for templates.
1719 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1720
1721 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001722
Devang Patel08f053f2009-01-05 17:57:47 +00001723 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +00001724 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001725 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001726
1727 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001728 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001729 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001730
1731 // Add size if non-zero (derived types might be zero-sized.)
1732 if (Size)
1733 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1734
1735 // Add source line info if available and TyDesc is not a forward
1736 // declaration.
Devang Patelad165be2009-01-27 00:45:04 +00001737 if (!DTy.isForwardDecl())
1738 AddSourceLine(&Buffer, &DTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001739 }
1740
Devang Patelf4215332009-01-05 19:55:51 +00001741 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1742 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001743 DICompositeType CTy) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001744 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001745 std::string Name;
1746 CTy.getName(Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001747
Devang Patelf193ff02009-01-15 19:26:23 +00001748 uint64_t Size = CTy.getSizeInBits() >> 3;
1749 unsigned Tag = CTy.getTag();
Devang Patel49f38cb2009-01-23 01:19:09 +00001750 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001751
Devang Patelf4215332009-01-05 19:55:51 +00001752 switch (Tag) {
1753 case DW_TAG_vector_type:
1754 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001755 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001756 break;
Devang Pateleab4a2e2009-01-20 18:35:14 +00001757 case DW_TAG_enumeration_type:
1758 {
1759 DIArray Elements = CTy.getTypeArray();
1760 // Add enumerators to enumeration type.
1761 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1762 DIE *ElemDie = NULL;
1763 DIEnumerator Enum(Elements.getElement(i).getGV());
1764 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1765 Buffer.AddChild(ElemDie);
1766 }
1767 }
1768 break;
Devang Patelf4215332009-01-05 19:55:51 +00001769 case DW_TAG_subroutine_type:
1770 {
Devang Patelf4215332009-01-05 19:55:51 +00001771 // Add return type.
Bill Wendling3f500d92009-05-06 21:21:34 +00001772 DIArray Elements = CTy.getTypeArray();
Devang Patel48d190f2009-01-05 21:47:57 +00001773 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +00001774 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +00001775
Bill Wendling3f500d92009-05-06 21:21:34 +00001776 // Add prototype flag.
1777 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1778
Devang Patelf4215332009-01-05 19:55:51 +00001779 // Add arguments.
1780 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1781 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001782 DIDescriptor Ty = Elements.getElement(i);
Devang Patel7ab24502009-01-17 06:57:25 +00001783 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +00001784 Buffer.AddChild(Arg);
1785 }
1786 }
1787 break;
1788 case DW_TAG_structure_type:
1789 case DW_TAG_union_type:
Devang Patel86bda412009-03-25 00:28:40 +00001790 case DW_TAG_class_type:
Devang Patelf4215332009-01-05 19:55:51 +00001791 {
1792 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +00001793 DIArray Elements = CTy.getTypeArray();
Devang Patel153745c2009-01-16 00:50:53 +00001794
1795 // A forward struct declared type may not have elements available.
1796 if (Elements.isNull())
1797 break;
1798
Devang Patelf4215332009-01-05 19:55:51 +00001799 // Add elements to structure type.
1800 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1801 DIDescriptor Element = Elements.getElement(i);
Devang Patel5aac3d32009-01-17 08:01:33 +00001802 DIE *ElemDie = NULL;
Devang Patelf193ff02009-01-15 19:26:23 +00001803 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel2d1768c2009-01-17 08:05:14 +00001804 ElemDie = CreateSubprogramDIE(DW_Unit,
1805 DISubprogram(Element.getGV()));
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001806 else if (Element.getTag() == dwarf::DW_TAG_variable) // ??
Devang Patel5aac3d32009-01-17 08:01:33 +00001807 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1808 DIGlobalVariable(Element.getGV()));
Devang Patel2be58932009-01-20 21:02:02 +00001809 else
1810 ElemDie = CreateMemberDIE(DW_Unit,
1811 DIDerivedType(Element.getGV()));
Devang Patel2d1768c2009-01-17 08:05:14 +00001812 Buffer.AddChild(ElemDie);
Devang Patelf4215332009-01-05 19:55:51 +00001813 }
Mike Stumpa6815152009-05-14 18:45:49 +00001814
1815 // FIXME: We'd like an API to register additional attributes for the
1816 // frontend to use while synthesizing, and then we'd use that api in
1817 // clang instead of this.
1818 if (Name == "__block_literal_generic")
1819 AddUInt(&Buffer, DW_AT_APPLE_block, DW_FORM_flag, 1);
1820
Devang Patel13319ce2009-02-17 22:43:44 +00001821 unsigned RLang = CTy.getRunTimeLang();
1822 if (RLang)
1823 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patelf4215332009-01-05 19:55:51 +00001824 }
1825 break;
1826 default:
1827 break;
1828 }
1829
1830 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001831 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001832 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf4215332009-01-05 19:55:51 +00001833
Devang Patelad165be2009-01-27 00:45:04 +00001834 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1835 || Tag == DW_TAG_union_type) {
1836 // Add size if non-zero (derived types might be zero-sized.)
1837 if (Size)
1838 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1839 else {
1840 // Add zero size if it is not a forward declaration.
1841 if (CTy.isForwardDecl())
1842 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1843 else
1844 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1845 }
1846
1847 // Add source line info if available.
1848 if (!CTy.isForwardDecl())
1849 AddSourceLine(&Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001850 }
Devang Patelf4215332009-01-05 19:55:51 +00001851 }
1852
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001853 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1854 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelf193ff02009-01-15 19:26:23 +00001855 int64_t L = SR.getLo();
1856 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +00001857 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1858 if (L != H) {
1859 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1860 if (L)
Devang Patel2d1768c2009-01-17 08:05:14 +00001861 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1862 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel68afdc32009-01-05 18:33:01 +00001863 }
1864 Buffer.AddChild(DW_Subrange);
1865 }
1866
1867 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1868 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1869 DICompositeType *CTy) {
1870 Buffer.setTag(DW_TAG_array_type);
1871 if (CTy->getTag() == DW_TAG_vector_type)
1872 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1873
Devang Patelf9235742009-01-28 21:08:20 +00001874 // Emit derived type.
1875 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001876 DIArray Elements = CTy->getTypeArray();
Devang Patel68afdc32009-01-05 18:33:01 +00001877
1878 // Construct an anonymous type for index type.
1879 DIE IdxBuffer(DW_TAG_base_type);
1880 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1881 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1882 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1883
1884 // Add subranges to array type.
1885 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001886 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001887 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1888 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001889 }
1890 }
1891
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001892 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Pateleab4a2e2009-01-20 18:35:14 +00001893 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001894
1895 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendling0582ae92009-03-13 04:39:26 +00001896 std::string Name;
1897 ETy->getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001898 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patelc69bf2c2009-01-05 18:38:38 +00001899 int64_t Value = ETy->getEnumValue();
1900 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Pateleab4a2e2009-01-20 18:35:14 +00001901 return Enumerator;
Devang Patelc69bf2c2009-01-05 18:38:38 +00001902 }
Devang Patel68afdc32009-01-05 18:33:01 +00001903
Devang Patel5aac3d32009-01-17 08:01:33 +00001904 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001905 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patel5aac3d32009-01-17 08:01:33 +00001906 {
1907 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendling0582ae92009-03-13 04:39:26 +00001908 std::string Name;
1909 GV.getDisplayName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001910 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling0582ae92009-03-13 04:39:26 +00001911 std::string LinkageName;
1912 GV.getLinkageName(LinkageName);
1913 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001914 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1915 AddType(DW_Unit, GVDie, GV.getType());
1916 if (!GV.isLocalToUnit())
1917 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1918 AddSourceLine(GVDie, &GV);
1919 return GVDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001920 }
1921
Devang Patel2be58932009-01-20 21:02:02 +00001922 /// CreateMemberDIE - Create new member DIE.
1923 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1924 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendling0582ae92009-03-13 04:39:26 +00001925 std::string Name;
1926 DT.getName(Name);
1927 if (!Name.empty())
Devang Patel2be58932009-01-20 21:02:02 +00001928 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1929
1930 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1931
1932 AddSourceLine(MemberDie, &DT);
1933
Devang Patel36375ee2009-02-17 21:23:59 +00001934 uint64_t Size = DT.getSizeInBits();
1935 uint64_t FieldSize = DT.getOriginalTypeSize();
1936
1937 if (Size != FieldSize) {
1938 // Handle bitfield.
1939 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1940 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1941
1942 uint64_t Offset = DT.getOffsetInBits();
1943 uint64_t FieldOffset = Offset;
1944 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1945 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1946 FieldOffset = (HiMark - FieldSize);
1947 Offset -= FieldOffset;
1948 // Maybe we need to work from the other end.
1949 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1950 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1951 }
Devang Patel2be58932009-01-20 21:02:02 +00001952 DIEBlock *Block = new DIEBlock();
1953 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1954 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1955 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1956
Devang Patel47661592009-01-21 00:08:04 +00001957 if (DT.isProtected())
1958 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1959 else if (DT.isPrivate())
1960 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1961
Devang Patel2be58932009-01-20 21:02:02 +00001962 return MemberDie;
1963 }
1964
Devang Patel5aac3d32009-01-17 08:01:33 +00001965 /// CreateSubprogramDIE - Create new DIE using SP.
1966 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Bill Wendling3f500d92009-05-06 21:21:34 +00001967 const DISubprogram &SP,
Devang Patel2d1768c2009-01-17 08:05:14 +00001968 bool IsConstructor = false) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001969 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling3f500d92009-05-06 21:21:34 +00001970
Bill Wendling0582ae92009-03-13 04:39:26 +00001971 std::string Name;
1972 SP.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001973 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling3f500d92009-05-06 21:21:34 +00001974
Bill Wendling0582ae92009-03-13 04:39:26 +00001975 std::string LinkageName;
1976 SP.getLinkageName(LinkageName);
Bill Wendling3f500d92009-05-06 21:21:34 +00001977
Bill Wendling0582ae92009-03-13 04:39:26 +00001978 if (!LinkageName.empty())
Bill Wendling3f500d92009-05-06 21:21:34 +00001979 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1980
Devang Patel5aac3d32009-01-17 08:01:33 +00001981 AddSourceLine(SPDie, &SP);
Devang Patel86ae1422009-01-05 18:59:44 +00001982
Devang Patel5aac3d32009-01-17 08:01:33 +00001983 DICompositeType SPTy = SP.getType();
1984 DIArray Args = SPTy.getTypeArray();
Bill Wendling3f500d92009-05-06 21:21:34 +00001985
1986 // Add prototyped tag, if C or ObjC.
1987 unsigned Lang = SP.getCompileUnit().getLanguage();
1988 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89 || Lang == DW_LANG_ObjC)
1989 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patel5aac3d32009-01-17 08:01:33 +00001990
Devang Patel86ae1422009-01-05 18:59:44 +00001991 // Add Return Type.
Devang Patel75b27382009-04-08 22:18:45 +00001992 unsigned SPTag = SPTy.getTag();
Devang Patel9ac08d62009-02-27 18:05:21 +00001993 if (!IsConstructor) {
Devang Patel75b27382009-04-08 22:18:45 +00001994 if (Args.isNull() || SPTag != DW_TAG_subroutine_type)
Devang Patel9ac08d62009-02-27 18:05:21 +00001995 AddType(DW_Unit, SPDie, SPTy);
1996 else
1997 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1998 }
Devang Pateld234e592009-01-30 01:21:46 +00001999
Devang Pateld5863dd2009-02-02 17:51:41 +00002000 if (!SP.isDefinition()) {
2001 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
Bill Wendling3f500d92009-05-06 21:21:34 +00002002 // Add arguments. Do not add arguments for subprogram definition. They
2003 // will be handled through RecordVariable.
Devang Patel75b27382009-04-08 22:18:45 +00002004 if (SPTag == DW_TAG_subroutine_type)
Devang Pateld5863dd2009-02-02 17:51:41 +00002005 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
2006 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2007 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
Bill Wendling3f500d92009-05-06 21:21:34 +00002008 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ??
Devang Pateld5863dd2009-02-02 17:51:41 +00002009 SPDie->AddChild(Arg);
2010 }
2011 }
Devang Pateld234e592009-01-30 01:21:46 +00002012
Devang Patelf193ff02009-01-15 19:26:23 +00002013 if (!SP.isLocalToUnit())
Devang Pateld234e592009-01-30 01:21:46 +00002014 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Bill Wendling3f500d92009-05-06 21:21:34 +00002015
Devang Patel1be3ecc2009-04-15 00:10:26 +00002016 // DW_TAG_inlined_subroutine may refer to this DIE.
2017 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getGV());
2018 Slot = SPDie;
Devang Patel5aac3d32009-01-17 08:01:33 +00002019 return SPDie;
Devang Patel86ae1422009-01-05 18:59:44 +00002020 }
2021
Devang Patel5aac3d32009-01-17 08:01:33 +00002022 /// FindCompileUnit - Get the compile unit for the given descriptor.
2023 ///
Chris Lattnere3f6cea2009-05-05 04:55:56 +00002024 CompileUnit &FindCompileUnit(DICompileUnit Unit) const {
2025 DenseMap<Value *, CompileUnit *>::const_iterator I =
2026 CompileUnitMap.find(Unit.getGV());
2027 assert(I != CompileUnitMap.end() && "Missing compile unit.");
2028 return *I->second;
Devang Patel8526cc02009-01-05 22:35:52 +00002029 }
2030
Devang Patelbbdc8202009-01-13 23:54:55 +00002031 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002032 ///
2033 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
2034 // Get the descriptor.
Devang Patel99ec3532009-01-16 19:28:14 +00002035 const DIVariable &VD = DV->getVariable();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002036
2037 // Translate tag to proper Dwarf tag. The result variable is dropped for
2038 // now.
2039 unsigned Tag;
Devang Patel99ec3532009-01-16 19:28:14 +00002040 switch (VD.getTag()) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00002041 case DW_TAG_return_variable: return NULL;
2042 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2043 case DW_TAG_auto_variable: // fall thru
2044 default: Tag = DW_TAG_variable; break;
2045 }
2046
2047 // Define variable debug information entry.
2048 DIE *VariableDie = new DIE(Tag);
Bill Wendling0582ae92009-03-13 04:39:26 +00002049 std::string Name;
2050 VD.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00002051 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002052
2053 // Add source line info if available.
Devang Patel99ec3532009-01-16 19:28:14 +00002054 AddSourceLine(VariableDie, &VD);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002055
2056 // Add variable type.
Devang Patel99ec3532009-01-16 19:28:14 +00002057 AddType(Unit, VariableDie, VD.getType());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002058
2059 // Add variable address.
2060 MachineLocation Location;
2061 Location.set(RI->getFrameRegister(*MF),
2062 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2063 AddAddress(VariableDie, DW_AT_location, Location);
2064
2065 return VariableDie;
2066 }
2067
Devang Patel7a6e5a32009-01-08 02:33:41 +00002068 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2069 ///
2070 DbgScope *getOrCreateScope(GlobalVariable *V) {
2071 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002072 if (Slot) return Slot;
2073
Devang Patel1be3ecc2009-04-15 00:10:26 +00002074 DbgScope *Parent = NULL;
2075 DIBlock Block(V);
Bill Wendling3f500d92009-05-06 21:21:34 +00002076
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002077 // Don't create a new scope if we already created one for an inlined
2078 // function.
2079 DenseMap<const GlobalVariable *, DbgScope *>::iterator
2080 II = AbstractInstanceRootMap.find(V);
2081 if (II != AbstractInstanceRootMap.end())
2082 return LexicalScopeStack.back();
2083
Devang Patel1be3ecc2009-04-15 00:10:26 +00002084 if (!Block.isNull()) {
2085 DIDescriptor ParentDesc = Block.getContext();
2086 Parent =
2087 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002088 }
Bill Wendling3f500d92009-05-06 21:21:34 +00002089
Devang Patel1be3ecc2009-04-15 00:10:26 +00002090 Slot = new DbgScope(Parent, DIDescriptor(V));
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002091
Devang Patel1be3ecc2009-04-15 00:10:26 +00002092 if (Parent)
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002093 Parent->AddScope(Slot);
Devang Patel1be3ecc2009-04-15 00:10:26 +00002094 else
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002095 // First function is top level function.
Devang Patel7d2f9722009-04-15 20:41:31 +00002096 FunctionDbgScope = Slot;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002097
Devang Patel7a6e5a32009-01-08 02:33:41 +00002098 return Slot;
2099 }
2100
2101 /// ConstructDbgScope - Construct the components of a scope.
2102 ///
2103 void ConstructDbgScope(DbgScope *ParentScope,
2104 unsigned ParentStartID, unsigned ParentEndID,
2105 DIE *ParentDie, CompileUnit *Unit) {
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002106 // Add variables to scope.
2107 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
2108 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2109 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2110 if (VariableDie) ParentDie->AddChild(VariableDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002111 }
2112
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002113 // Add concrete instances to scope.
2114 SmallVector<DbgConcreteScope *, 8> &ConcreteInsts = ParentScope->getConcreteInsts();
2115 for (unsigned i = 0, N = ConcreteInsts.size(); i < N; ++i) {
2116 DbgConcreteScope *ConcreteInst = ConcreteInsts[i];
2117 DIE *Die = ConcreteInst->getDie();
2118
2119 unsigned StartID = ConcreteInst->getStartLabelID();
2120 unsigned EndID = ConcreteInst->getEndLabelID();
2121
2122 // Add the scope bounds.
2123 if (StartID)
2124 AddLabel(Die, DW_AT_low_pc, DW_FORM_addr,
2125 DWLabel("label", StartID));
2126 else
2127 AddLabel(Die, DW_AT_low_pc, DW_FORM_addr,
2128 DWLabel("func_begin", SubprogramCount));
2129
2130 if (EndID)
2131 AddLabel(Die, DW_AT_high_pc, DW_FORM_addr,
2132 DWLabel("label", EndID));
2133 else
2134 AddLabel(Die, DW_AT_high_pc, DW_FORM_addr,
2135 DWLabel("func_end", SubprogramCount));
2136
2137 ParentDie->AddChild(Die);
2138 }
2139
Devang Patel7a6e5a32009-01-08 02:33:41 +00002140 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002141 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002142 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2143 // Define the Scope debug information entry.
2144 DbgScope *Scope = Scopes[j];
Devang Patel7a6e5a32009-01-08 02:33:41 +00002145
Devang Patele9bfb0f2009-01-12 18:41:00 +00002146 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2147 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002148
Devang Patel1be3ecc2009-04-15 00:10:26 +00002149 // Ignore empty scopes.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002150 if (StartID == EndID && StartID != 0) continue;
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002151
2152 // Do not ignore inlined scopes even if they don't have any variables or
2153 // scopes.
2154 if (Scope->getScopes().empty() && Scope->getVariables().empty() &&
2155 Scope->getConcreteInsts().empty())
Devang Patel0f7fef32009-04-13 17:02:03 +00002156 continue;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002157
2158 if (StartID == ParentStartID && EndID == ParentEndID) {
2159 // Just add stuff to the parent scope.
2160 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2161 } else {
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002162 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
Bill Wendling7fa81622009-05-01 08:25:13 +00002163
2164 // Add the scope bounds.
2165 if (StartID)
2166 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2167 DWLabel("label", StartID));
2168 else
2169 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2170 DWLabel("func_begin", SubprogramCount));
2171
2172 if (EndID)
2173 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2174 DWLabel("label", EndID));
2175 else
2176 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2177 DWLabel("func_end", SubprogramCount));
2178
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002179 // Add the scope's contents.
Bill Wendling7fa81622009-05-01 08:25:13 +00002180 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2181 ParentDie->AddChild(ScopeDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002182 }
2183 }
2184 }
2185
Devang Patel7d2f9722009-04-15 20:41:31 +00002186 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002187 ///
Devang Patel7d2f9722009-04-15 20:41:31 +00002188 void ConstructFunctionDbgScope(DbgScope *RootScope) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00002189 // Exit if there is no root scope.
2190 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00002191 DIDescriptor Desc = RootScope->getDesc();
2192 if (Desc.isNull())
2193 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002194
2195 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00002196 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002197
2198 // Get the compile unit context.
Devang Pateldd9db662009-01-30 18:20:31 +00002199 CompileUnit *Unit = MainCU;
2200 if (!Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00002201 Unit = &FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002202
2203 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002204 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002205 assert(SPDie && "Missing subprogram descriptor");
2206
2207 // Add the function bounds.
2208 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2209 DWLabel("func_begin", SubprogramCount));
2210 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2211 DWLabel("func_end", SubprogramCount));
2212 MachineLocation Location(RI->getFrameRegister(*MF));
2213 AddAddress(SPDie, DW_AT_frame_base, Location);
2214
2215 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2216 }
2217
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002218 /// ConstructFunctionDbgScope - Construct the scope for the abstract debug
2219 /// scope.
2220 ///
2221 void ConstructAbstractDbgScope(DbgScope *AbsScope) {
2222 // Exit if there is no root scope.
2223 if (!AbsScope) return;
2224
2225 DIDescriptor Desc = AbsScope->getDesc();
2226 if (Desc.isNull())
2227 return;
2228
2229 // Get the subprogram debug information entry.
2230 DISubprogram SPD(Desc.getGV());
2231
2232 // Get the compile unit context.
2233 CompileUnit *Unit = MainCU;
2234 if (!Unit)
2235 Unit = &FindCompileUnit(SPD.getCompileUnit());
2236
2237 // Get the subprogram die.
2238 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
2239 assert(SPDie && "Missing subprogram descriptor");
2240
2241 ConstructDbgScope(AbsScope, 0, 0, SPDie, Unit);
2242 }
2243
Devang Patel7a6e5a32009-01-08 02:33:41 +00002244 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2245 ///
2246 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Chenge3d42322009-02-25 07:04:34 +00002247 const char *FnName = MF->getFunction()->getNameStart();
2248 if (MainCU) {
Bill Wendling972bbac2009-04-09 21:49:15 +00002249 StringMap<DIE*> &Globals = MainCU->getGlobals();
2250 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002251 if (GI != Globals.end()) {
2252 DIE *SPDie = GI->second;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002253
2254 // Add the function bounds.
2255 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2256 DWLabel("func_begin", SubprogramCount));
2257 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2258 DWLabel("func_end", SubprogramCount));
2259
2260 MachineLocation Location(RI->getFrameRegister(*MF));
2261 AddAddress(SPDie, DW_AT_frame_base, Location);
2262 return;
2263 }
Evan Chenge3d42322009-02-25 07:04:34 +00002264 } else {
2265 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2266 CompileUnit *Unit = CompileUnits[i];
Bill Wendling972bbac2009-04-09 21:49:15 +00002267 StringMap<DIE*> &Globals = Unit->getGlobals();
2268 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002269 if (GI != Globals.end()) {
2270 DIE *SPDie = GI->second;
2271
2272 // Add the function bounds.
2273 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2274 DWLabel("func_begin", SubprogramCount));
2275 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2276 DWLabel("func_end", SubprogramCount));
2277
2278 MachineLocation Location(RI->getFrameRegister(*MF));
2279 AddAddress(SPDie, DW_AT_frame_base, Location);
2280 return;
2281 }
2282 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002283 }
Evan Chenge3d42322009-02-25 07:04:34 +00002284
Devang Patel7a6e5a32009-01-08 02:33:41 +00002285#if 0
2286 // FIXME: This is causing an abort because C++ mangled names are compared
2287 // with their unmangled counterparts. See PR2885. Don't do this assert.
2288 assert(0 && "Couldn't find DIE for machine function!");
2289#endif
2290 }
2291
Jim Laskeyef42a012006-11-02 20:12:39 +00002292 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2293 /// tools to recognize the object file contains Dwarf information.
2294 void EmitInitial() {
2295 // Check to see if we already emitted intial headers.
2296 if (didInitial) return;
2297 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002298
Jim Laskeyef42a012006-11-02 20:12:39 +00002299 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002300 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002301 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002302 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002303 }
2304 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2305 EmitLabel("section_info", 0);
2306 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2307 EmitLabel("section_abbrev", 0);
2308 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2309 EmitLabel("section_aranges", 0);
Scott Michel210de722009-01-26 22:32:51 +00002310 if (TAI->doesSupportMacInfoSection()) {
2311 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2312 EmitLabel("section_macinfo", 0);
2313 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002314 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2315 EmitLabel("section_line", 0);
2316 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2317 EmitLabel("section_loc", 0);
2318 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2319 EmitLabel("section_pubnames", 0);
2320 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2321 EmitLabel("section_str", 0);
2322 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2323 EmitLabel("section_ranges", 0);
2324
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002325 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002326 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002327 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002328 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002329 }
2330
Jim Laskey65195462006-10-30 13:35:07 +00002331 /// EmitDIE - Recusively Emits a debug information entry.
2332 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002333 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002334 // Get the abbreviation for this DIE.
2335 unsigned AbbrevNumber = Die->getAbbrevNumber();
2336 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002337
Jim Laskeybacd3042007-02-21 22:48:45 +00002338 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002339
2340 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002341 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002342
Evan Cheng42bf74b2009-03-25 01:47:28 +00002343 if (Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002344 Asm->EOL(std::string("Abbrev [" +
2345 utostr(AbbrevNumber) +
2346 "] 0x" + utohexstr(Die->getOffset()) +
2347 ":0x" + utohexstr(Die->getSize()) + " " +
2348 TagString(Abbrev->getTag())));
2349 else
2350 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002351
Owen Anderson873e1b52008-06-24 21:44:59 +00002352 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2353 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002354
Jim Laskeyef42a012006-11-02 20:12:39 +00002355 // Emit the DIE attribute values.
2356 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2357 unsigned Attr = AbbrevData[i].getAttribute();
2358 unsigned Form = AbbrevData[i].getForm();
2359 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002360
Jim Laskeyef42a012006-11-02 20:12:39 +00002361 switch (Attr) {
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002362 case DW_AT_sibling:
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002363 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002364 break;
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002365 case DW_AT_abstract_origin: {
2366 DIEntry *E = cast<DIEntry>(Values[i]);
2367 DIE *Origin = E->getEntry();
2368 unsigned Addr =
2369 CompileUnitOffsets[Die->getAbstractCompileUnit()] +
2370 Origin->getOffset();
2371
2372 Asm->EmitInt32(Addr);
2373 break;
Jim Laskeyef42a012006-11-02 20:12:39 +00002374 }
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002375 default:
Jim Laskeyef42a012006-11-02 20:12:39 +00002376 // Emit an attribute using the defined form.
2377 Values[i]->EmitValue(*this, Form);
2378 break;
2379 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002380
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002381 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002382 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002383
Jim Laskeyef42a012006-11-02 20:12:39 +00002384 // Emit the DIE children if any.
2385 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2386 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002387
Bill Wendlinga9519572009-05-08 20:38:02 +00002388 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Jim Laskeyef42a012006-11-02 20:12:39 +00002389 EmitDIE(Children[j]);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002390
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002391 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002392 }
2393 }
2394
Jim Laskey65195462006-10-30 13:35:07 +00002395 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2396 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002397 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2398 // Get the children.
2399 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002400
Jim Laskeyef42a012006-11-02 20:12:39 +00002401 // If not last sibling and has children then add sibling offset attribute.
2402 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2403
2404 // Record the abbreviation.
2405 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002406
Jim Laskeyef42a012006-11-02 20:12:39 +00002407 // Get the abbreviation for this DIE.
2408 unsigned AbbrevNumber = Die->getAbbrevNumber();
2409 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2410
2411 // Set DIE offset
2412 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002413
Jim Laskeyef42a012006-11-02 20:12:39 +00002414 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002415 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2416
Owen Anderson873e1b52008-06-24 21:44:59 +00002417 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2418 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002419
2420 // Size the DIE attribute values.
2421 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2422 // Size attribute value.
2423 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2424 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002425
Jim Laskeyef42a012006-11-02 20:12:39 +00002426 // Size the DIE children if any.
2427 if (!Children.empty()) {
2428 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2429 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002430
Jim Laskeyef42a012006-11-02 20:12:39 +00002431 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2432 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2433 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002434
Jim Laskeyef42a012006-11-02 20:12:39 +00002435 // End of children marker.
2436 Offset += sizeof(int8_t);
2437 }
2438
2439 Die->setSize(Offset - Die->getOffset());
2440 return Offset;
2441 }
Jim Laskey65195462006-10-30 13:35:07 +00002442
2443 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2444 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002445 void SizeAndOffsets() {
Bill Wendlinge688faf2009-05-08 21:03:15 +00002446 // Compute size of compile unit header.
2447 static unsigned Offset =
2448 sizeof(int32_t) + // Length of Compilation Unit Info
2449 sizeof(int16_t) + // DWARF version number
2450 sizeof(int32_t) + // Offset Into Abbrev. Section
2451 sizeof(int8_t); // Pointer Size (in bytes)
2452
Jim Laskey5496f012006-11-09 14:52:14 +00002453 // Process base compile unit.
Devang Pateldd9db662009-01-30 18:20:31 +00002454 if (MainCU) {
Devang Pateldd9db662009-01-30 18:20:31 +00002455 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
Bill Wendlinge688faf2009-05-08 21:03:15 +00002456 CompileUnitOffsets[MainCU] = 0;
Devang Pateldd9db662009-01-30 18:20:31 +00002457 return;
2458 }
Bill Wendlinge688faf2009-05-08 21:03:15 +00002459
2460 // Process all compile units.
2461 unsigned PrevOffset = 0;
2462
Evan Chenge3d42322009-02-25 07:04:34 +00002463 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2464 CompileUnit *Unit = CompileUnits[i];
Bill Wendlinge688faf2009-05-08 21:03:15 +00002465 CompileUnitOffsets[Unit] = PrevOffset;
2466 PrevOffset += SizeAndOffsetDie(Unit->getDie(), Offset, true)
2467 + sizeof(int32_t); // FIXME - extra pad for gdb bug.
Devang Patel72b66352009-01-12 23:05:55 +00002468 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002469 }
2470
Evan Chenge3d42322009-02-25 07:04:34 +00002471 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Jim Laskey65195462006-10-30 13:35:07 +00002472 ///
Evan Chenge3d42322009-02-25 07:04:34 +00002473 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2474 DIE *Die = Unit->getDie();
2475 // Emit the compile units header.
2476 EmitLabel("info_begin", Unit->getID());
2477 // Emit size of content not including length itself
2478 unsigned ContentSize = Die->getSize() +
2479 sizeof(int16_t) + // DWARF version number
2480 sizeof(int32_t) + // Offset Into Abbrev. Section
2481 sizeof(int8_t) + // Pointer Size (in bytes)
2482 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2483
2484 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2485 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2486 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2487 Asm->EOL("Offset Into Abbrev. Section");
2488 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2489
2490 EmitDIE(Die);
2491 // FIXME - extra padding for gdb bug.
2492 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2493 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2494 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2495 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2496 EmitLabel("info_end", Unit->getID());
2497
2498 Asm->EOL();
2499 }
2500
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002501 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002502 // Start debug info section.
2503 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002504
Evan Chenge3d42322009-02-25 07:04:34 +00002505 if (MainCU) {
2506 EmitDebugInfoPerCU(MainCU);
2507 return;
Devang Patel72b66352009-01-12 23:05:55 +00002508 }
Evan Chenge3d42322009-02-25 07:04:34 +00002509
2510 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2511 EmitDebugInfoPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002512 }
2513
Jim Laskey65195462006-10-30 13:35:07 +00002514 /// EmitAbbreviations - Emit the abbreviation section.
2515 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002516 void EmitAbbreviations() const {
2517 // Check to see if it is worth the effort.
2518 if (!Abbreviations.empty()) {
2519 // Start the debug abbrev section.
2520 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002521
Jim Laskeyef42a012006-11-02 20:12:39 +00002522 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002523
Jim Laskeyef42a012006-11-02 20:12:39 +00002524 // For each abbrevation.
2525 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2526 // Get abbreviation data
2527 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002528
Jim Laskeyef42a012006-11-02 20:12:39 +00002529 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002530 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2531 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002532
Jim Laskeyef42a012006-11-02 20:12:39 +00002533 // Emit the abbreviations data.
2534 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002535
Jim Laskeybacd3042007-02-21 22:48:45 +00002536 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002537 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002538
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002539 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002540 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002541
Jim Laskeyef42a012006-11-02 20:12:39 +00002542 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002543
Jim Laskeybacd3042007-02-21 22:48:45 +00002544 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002545 }
2546 }
2547
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002548 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2549 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002550 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002551 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2552 // Define last address of section.
2553 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2554 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2555 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2556 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2557
2558 // Mark end of matrix.
2559 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2560 Asm->EmitULEB128Bytes(1); Asm->EOL();
2561 Asm->EmitInt8(1); Asm->EOL();
2562 }
2563
Jim Laskey65195462006-10-30 13:35:07 +00002564 /// EmitDebugLines - Emit source line information.
2565 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002566 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002567 // If the target is using .loc/.file, the assembler will be emitting the
2568 // .debug_line table automatically.
2569 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002570 return;
2571
Jim Laskeyef42a012006-11-02 20:12:39 +00002572 // Minimum line delta, thus ranging from -10..(255-10).
2573 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2574 // Maximum line delta, thus ranging from -10..(255-10).
2575 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002576
Jim Laskeyef42a012006-11-02 20:12:39 +00002577 // Start the dwarf line section.
2578 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002579
Jim Laskeyef42a012006-11-02 20:12:39 +00002580 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002581
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002582 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002583 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002584 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002585
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002586 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002587
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002588 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002589 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002590 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002591
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002592 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002593
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002594 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002595
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002596 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002597
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002598 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002599
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002600 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002601
Jim Laskeyef42a012006-11-02 20:12:39 +00002602 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002603 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2604 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2605 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2606 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2607 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2608 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2609 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2610 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2611 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002612
Jim Laskeyef42a012006-11-02 20:12:39 +00002613 // Emit directories.
Evan Chenge3d42322009-02-25 07:04:34 +00002614 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2615 Asm->EmitString(getSourceDirectoryName(DI));
2616 Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002617 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002618 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002619
Jim Laskeyef42a012006-11-02 20:12:39 +00002620 // Emit files.
Evan Chenge3d42322009-02-25 07:04:34 +00002621 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2622 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002623 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Chenge3d42322009-02-25 07:04:34 +00002624 Asm->EmitString(getSourceFileName(Id.second));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002625 Asm->EOL("Source");
Evan Chenge3d42322009-02-25 07:04:34 +00002626 Asm->EmitULEB128Bytes(Id.first);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002627 Asm->EOL("Directory #");
2628 Asm->EmitULEB128Bytes(0);
2629 Asm->EOL("Mod date");
2630 Asm->EmitULEB128Bytes(0);
2631 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002632 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002633 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002634
Jim Laskeyef42a012006-11-02 20:12:39 +00002635 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002636
Jim Laskeyef42a012006-11-02 20:12:39 +00002637 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002638 unsigned SecSrcLinesSize = SectionSourceLines.size();
2639
2640 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002641 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002642 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002643
Evan Cheng42bf74b2009-03-25 01:47:28 +00002644 if (Asm->isVerbose()) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002645 const Section* S = SectionMap[j + 1];
Evan Chenge3d42322009-02-25 07:04:34 +00002646 O << '\t' << TAI->getCommentString() << " Section"
2647 << S->getName() << '\n';
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002648 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002649 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002650
2651 // Dwarf assumes we start with first line of first source file.
2652 unsigned Source = 1;
2653 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002654
Jim Laskeyef42a012006-11-02 20:12:39 +00002655 // Construct rows of the address, source, line, column matrix.
2656 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00002657 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002658 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002659 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002660
Evan Cheng42bf74b2009-03-25 01:47:28 +00002661 if (!Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002662 Asm->EOL();
Evan Chenge3d42322009-02-25 07:04:34 +00002663 else {
2664 std::pair<unsigned, unsigned> SourceID =
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002665 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Chenge3d42322009-02-25 07:04:34 +00002666 O << '\t' << TAI->getCommentString() << ' '
2667 << getSourceDirectoryName(SourceID.first) << ' '
2668 << getSourceFileName(SourceID.second)
2669 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2670 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002671
2672 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002673 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002674 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002675 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002676 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002677
Jim Laskeyef42a012006-11-02 20:12:39 +00002678 // If change of source, then switch to the new source.
2679 if (Source != LineInfo.getSourceID()) {
2680 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002681 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2682 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002683 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002684
Jim Laskeyef42a012006-11-02 20:12:39 +00002685 // If change of line.
2686 if (Line != LineInfo.getLine()) {
2687 // Determine offset.
2688 int Offset = LineInfo.getLine() - Line;
2689 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002690
Jim Laskeyef42a012006-11-02 20:12:39 +00002691 // Update line.
2692 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002693
Jim Laskeyef42a012006-11-02 20:12:39 +00002694 // If delta is small enough and in range...
2695 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2696 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002697 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002698 } else {
2699 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002700 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2701 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2702 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002703 }
2704 } else {
2705 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002706 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002707 }
2708 }
2709
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002710 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002711 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002712
2713 if (SecSrcLinesSize == 0)
2714 // Because we're emitting a debug_line section, we still need a line
2715 // table. The linker and friends expect it to exist. If there's nothing to
2716 // put into it, emit an empty table.
2717 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002718
Jim Laskeyef42a012006-11-02 20:12:39 +00002719 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002720
Jim Laskeybacd3042007-02-21 22:48:45 +00002721 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002722 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002723
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002724 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002725 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002726 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002727 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002728 return;
2729
2730 int stackGrowth =
2731 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2732 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002733 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002734
2735 // Start the dwarf frame section.
2736 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2737
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002738 EmitLabel("debug_frame_common", 0);
2739 EmitDifference("debug_frame_common_end", 0,
2740 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002741 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002742
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002743 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002744 Asm->EmitInt32((int)DW_CIE_ID);
2745 Asm->EOL("CIE Identifier Tag");
2746 Asm->EmitInt8(DW_CIE_VERSION);
2747 Asm->EOL("CIE Version");
2748 Asm->EmitString("");
2749 Asm->EOL("CIE Augmentation");
2750 Asm->EmitULEB128Bytes(1);
2751 Asm->EOL("CIE Code Alignment Factor");
2752 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002753 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002754 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002755 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002756
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002757 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002758 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002759
Dale Johannesenb97aec62007-11-13 19:13:01 +00002760 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002761
Evan Cheng05548eb2008-02-29 19:36:59 +00002762 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002763 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002764
Jim Laskeybacd3042007-02-21 22:48:45 +00002765 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002766 }
2767
Jim Laskey65195462006-10-30 13:35:07 +00002768 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2769 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002770 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002771 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002772 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002773
Jim Laskeyef42a012006-11-02 20:12:39 +00002774 // Start the dwarf frame section.
2775 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002776
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002777 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2778 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002779 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002780
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002781 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002782
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002783 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2784 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002785 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002786
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002787 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002788 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002789 EmitDifference("func_end", DebugFrameInfo.Number,
2790 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002791 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002792
Devang Patel5aac3d32009-01-17 08:01:33 +00002793 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00002794 false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002795
Evan Cheng05548eb2008-02-29 19:36:59 +00002796 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002797 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002798
Jim Laskeybacd3042007-02-21 22:48:45 +00002799 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002800 }
2801
Evan Chenge3d42322009-02-25 07:04:34 +00002802 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2803 EmitDifference("pubnames_end", Unit->getID(),
2804 "pubnames_begin", Unit->getID(), true);
2805 Asm->EOL("Length of Public Names Info");
2806
2807 EmitLabel("pubnames_begin", Unit->getID());
2808
2809 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2810
2811 EmitSectionOffset("info_begin", "section_info",
2812 Unit->getID(), 0, true, false);
2813 Asm->EOL("Offset of Compilation Unit Info");
2814
2815 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2816 true);
2817 Asm->EOL("Compilation Unit Length");
2818
Bill Wendling972bbac2009-04-09 21:49:15 +00002819 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendlingf34be822009-04-09 23:51:31 +00002820 for (StringMap<DIE*>::const_iterator
Bill Wendling972bbac2009-04-09 21:49:15 +00002821 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendlingf34be822009-04-09 23:51:31 +00002822 const char *Name = GI->getKeyData();
Evan Chenge3d42322009-02-25 07:04:34 +00002823 DIE * Entity = GI->second;
2824
2825 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendlingf34be822009-04-09 23:51:31 +00002826 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Chenge3d42322009-02-25 07:04:34 +00002827 }
2828
2829 Asm->EmitInt32(0); Asm->EOL("End Mark");
2830 EmitLabel("pubnames_end", Unit->getID());
2831
2832 Asm->EOL();
2833 }
2834
Jim Laskeyef42a012006-11-02 20:12:39 +00002835 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002836 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002837 void EmitDebugPubNames() {
2838 // Start the dwarf pubnames section.
2839 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002840
Evan Chenge3d42322009-02-25 07:04:34 +00002841 if (MainCU) {
2842 EmitDebugPubNamesPerCU(MainCU);
2843 return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002844 }
Evan Chenge3d42322009-02-25 07:04:34 +00002845
2846 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2847 EmitDebugPubNamesPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002848 }
2849
2850 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002851 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002852 void EmitDebugStr() {
2853 // Check to see if it is worth the effort.
2854 if (!StringPool.empty()) {
2855 // Start the dwarf str section.
2856 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002857
Jim Laskeyef42a012006-11-02 20:12:39 +00002858 // For each of strings in the string pool.
2859 for (unsigned StringID = 1, N = StringPool.size();
2860 StringID <= N; ++StringID) {
2861 // Emit a label for reference from debug information entries.
2862 EmitLabel("string", StringID);
2863 // Emit the string itself.
2864 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002865 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002866 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002867
Jim Laskeybacd3042007-02-21 22:48:45 +00002868 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002869 }
2870 }
2871
2872 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002873 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002874 void EmitDebugLoc() {
2875 // Start the dwarf loc section.
2876 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002877
Jim Laskeybacd3042007-02-21 22:48:45 +00002878 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002879 }
2880
2881 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002882 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002883 void EmitDebugARanges() {
2884 // Start the dwarf aranges section.
2885 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002886
Jim Laskeyef42a012006-11-02 20:12:39 +00002887 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002888#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002889 CompileUnit *Unit = GetBaseCompileUnit();
2890
Jim Laskey5496f012006-11-09 14:52:14 +00002891 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002892 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002893
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002894 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002895
Jim Laskey5496f012006-11-09 14:52:14 +00002896 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002897 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002898
Dan Gohman82482942007-09-27 23:12:31 +00002899 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002900
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002901 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002902
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002903 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2904 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002905
Jim Laskey5496f012006-11-09 14:52:14 +00002906 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002907 EmitReference("text_begin", 0); Asm->EOL("Address");
2908 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002909
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002910 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2911 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002912#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002913
Jim Laskeybacd3042007-02-21 22:48:45 +00002914 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002915 }
2916
2917 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002918 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002919 void EmitDebugRanges() {
2920 // Start the dwarf ranges section.
2921 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002922
Jim Laskeybacd3042007-02-21 22:48:45 +00002923 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002924 }
2925
2926 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002927 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002928 void EmitDebugMacInfo() {
Scott Michel210de722009-01-26 22:32:51 +00002929 if (TAI->doesSupportMacInfoSection()) {
2930 // Start the dwarf macinfo section.
2931 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002932
Scott Michel210de722009-01-26 22:32:51 +00002933 Asm->EOL();
2934 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002935 }
2936
Devang Patel0f7fef32009-04-13 17:02:03 +00002937 /// EmitDebugInlineInfo - Emit inline info using following format.
2938 /// Section Header:
2939 /// 1. length of section
2940 /// 2. Dwarf version number
2941 /// 3. address size.
2942 ///
2943 /// Entries (one "entry" for each function that was inlined):
2944 ///
2945 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2946 /// otherwise offset into __debug_str for regular function name.
2947 /// 2. offset into __debug_str section for regular function name.
2948 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2949 /// instances for the function.
2950 ///
2951 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2952 /// inlined instance; the die_offset points to the inlined_subroutine die in
2953 /// the __debug_info section, and the low_pc is the starting address for the
2954 /// inlining instance.
2955 void EmitDebugInlineInfo() {
2956 if (!TAI->doesDwarfUsesInlineInfoSection())
2957 return;
2958
2959 if (!MainCU)
2960 return;
2961
2962 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection());
2963 Asm->EOL();
2964 EmitDifference("debug_inlined_end", 1,
2965 "debug_inlined_begin", 1, true);
2966 Asm->EOL("Length of Debug Inlined Information Entry");
2967
2968 EmitLabel("debug_inlined_begin", 1);
2969
2970 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2971 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2972
2973 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2974 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2975 GlobalVariable *GV = I->first;
2976 SmallVector<unsigned, 4> &Labels = I->second;
2977 DISubprogram SP(GV);
2978 std::string Name;
2979 std::string LName;
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002980
Devang Patel0f7fef32009-04-13 17:02:03 +00002981 SP.getLinkageName(LName);
2982 SP.getName(Name);
2983
2984 Asm->EmitString(LName.empty() ? Name : LName);
2985 Asm->EOL("MIPS linkage name");
2986
2987 Asm->EmitString(Name); Asm->EOL("Function name");
2988
2989 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2990
2991 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2992 LE = Labels.end(); LI != LE; ++LI) {
2993 DIE *SP = MainCU->getDieMapSlotFor(GV);
2994 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2995
2996 if (TD->getPointerSize() == sizeof(int32_t))
2997 O << TAI->getData32bitsDirective();
2998 else
2999 O << TAI->getData64bitsDirective();
3000 PrintLabelName("label", *LI); Asm->EOL("low_pc");
3001 }
3002 }
3003
3004 EmitLabel("debug_inlined_end", 1);
3005 Asm->EOL();
3006 }
3007
Bill Wendlingc8615e42009-03-10 21:47:45 +00003008 /// GetOrCreateSourceID - Look up the source id with the given directory and
3009 /// source file names. If none currently exists, create a new id and insert it
3010 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
3011 /// as well.
3012 unsigned GetOrCreateSourceID(const std::string &DirName,
3013 const std::string &FileName) {
3014 unsigned DId;
3015 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
3016 if (DI != DirectoryIdMap.end()) {
3017 DId = DI->getValue();
3018 } else {
3019 DId = DirectoryNames.size() + 1;
3020 DirectoryIdMap[DirName] = DId;
3021 DirectoryNames.push_back(DirName);
3022 }
3023
3024 unsigned FId;
3025 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
3026 if (FI != SourceFileIdMap.end()) {
3027 FId = FI->getValue();
3028 } else {
3029 FId = SourceFileNames.size() + 1;
3030 SourceFileIdMap[FileName] = FId;
3031 SourceFileNames.push_back(FileName);
3032 }
3033
3034 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
3035 SourceIdMap.find(std::make_pair(DId, FId));
3036 if (SI != SourceIdMap.end())
3037 return SI->second;
3038
3039 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
3040 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
3041 SourceIds.push_back(std::make_pair(DId, FId));
3042
3043 return SrcId;
3044 }
3045
Evan Chenge3d42322009-02-25 07:04:34 +00003046 void ConstructCompileUnit(GlobalVariable *GV) {
3047 DICompileUnit DIUnit(GV);
Bill Wendling0582ae92009-03-13 04:39:26 +00003048 std::string Dir, FN, Prod;
3049 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
3050 DIUnit.getFilename(FN));
Evan Chenge3d42322009-02-25 07:04:34 +00003051
3052 DIE *Die = new DIE(DW_TAG_compile_unit);
3053 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
3054 DWLabel("section_line", 0), DWLabel("section_line", 0),
3055 false);
Bill Wendling0582ae92009-03-13 04:39:26 +00003056 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Chenge3d42322009-02-25 07:04:34 +00003057 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00003058 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendling0582ae92009-03-13 04:39:26 +00003059 if (!Dir.empty())
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00003060 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Chenge3d42322009-02-25 07:04:34 +00003061 if (DIUnit.isOptimized())
3062 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendling0582ae92009-03-13 04:39:26 +00003063 std::string Flags;
3064 DIUnit.getFlags(Flags);
3065 if (!Flags.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00003066 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
3067 unsigned RVer = DIUnit.getRunTimeVersion();
3068 if (RVer)
3069 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
3070
3071 CompileUnit *Unit = new CompileUnit(ID, Die);
3072 if (DIUnit.isMain()) {
3073 assert(!MainCU && "Multiple main compile units are found!");
3074 MainCU = Unit;
3075 }
3076 CompileUnitMap[DIUnit.getGV()] = Unit;
3077 CompileUnits.push_back(Unit);
3078 }
3079
Devang Patelc4523242009-01-05 23:11:11 +00003080 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00003081 void ConstructCompileUnits() {
Evan Chenge3d42322009-02-25 07:04:34 +00003082 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
3083 if (!Root)
3084 return;
3085 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3086 "Malformed compile unit descriptor anchor type");
3087 Constant *RootC = cast<Constant>(*Root->use_begin());
3088 assert(RootC->hasNUsesOrMore(1) &&
3089 "Malformed compile unit descriptor anchor type");
3090 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3091 UI != UE; ++UI)
3092 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3093 UUI != UUE; ++UUI) {
3094 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3095 ConstructCompileUnit(GV);
Devang Pateldd9db662009-01-30 18:20:31 +00003096 }
Evan Chenge3d42322009-02-25 07:04:34 +00003097 }
3098
3099 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
3100 DIGlobalVariable DI_GV(GV);
3101 CompileUnit *DW_Unit = MainCU;
3102 if (!DW_Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00003103 DW_Unit = &FindCompileUnit(DI_GV.getCompileUnit());
Evan Chenge3d42322009-02-25 07:04:34 +00003104
3105 // Check for pre-existence.
3106 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
3107 if (Slot)
3108 return false;
3109
3110 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
3111
3112 // Add address.
3113 DIEBlock *Block = new DIEBlock();
3114 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Bill Wendling7d16e852009-04-10 00:12:49 +00003115 std::string GLN;
Evan Chenge3d42322009-02-25 07:04:34 +00003116 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling7d16e852009-04-10 00:12:49 +00003117 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Chenge3d42322009-02-25 07:04:34 +00003118 AddBlock(VariableDie, DW_AT_location, 0, Block);
3119
3120 // Add to map.
3121 Slot = VariableDie;
Bill Wendling3f500d92009-05-06 21:21:34 +00003122
Evan Chenge3d42322009-02-25 07:04:34 +00003123 // Add to context owner.
3124 DW_Unit->getDie()->AddChild(VariableDie);
Bill Wendling3f500d92009-05-06 21:21:34 +00003125
Evan Chenge3d42322009-02-25 07:04:34 +00003126 // Expose as global. FIXME - need to check external flag.
Bill Wendling0582ae92009-03-13 04:39:26 +00003127 std::string Name;
3128 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003129 return true;
Devang Pateld1ca9252009-01-05 23:03:32 +00003130 }
3131
Devang Patelc4523242009-01-05 23:11:11 +00003132 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patel53cac182009-02-24 00:02:15 +00003133 /// visible global variables. Return true if at least one global DIE is
3134 /// created.
3135 bool ConstructGlobalVariableDIEs() {
Evan Chenge3d42322009-02-25 07:04:34 +00003136 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
3137 if (!Root)
3138 return false;
Devang Patelc4523242009-01-05 23:11:11 +00003139
Evan Chenge3d42322009-02-25 07:04:34 +00003140 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3141 "Malformed global variable descriptor anchor type");
3142 Constant *RootC = cast<Constant>(*Root->use_begin());
3143 assert(RootC->hasNUsesOrMore(1) &&
3144 "Malformed global variable descriptor anchor type");
Devang Patelc4523242009-01-05 23:11:11 +00003145
Evan Chenge3d42322009-02-25 07:04:34 +00003146 bool Result = false;
3147 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3148 UI != UE; ++UI)
3149 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling3f500d92009-05-06 21:21:34 +00003150 UUI != UUE; ++UUI)
3151 Result |= ConstructGlobalVariableDIE(cast<GlobalVariable>(*UUI));
3152
Evan Chenge3d42322009-02-25 07:04:34 +00003153 return Result;
3154 }
Devang Patelc4523242009-01-05 23:11:11 +00003155
Evan Chenge3d42322009-02-25 07:04:34 +00003156 bool ConstructSubprogram(GlobalVariable *GV) {
3157 DISubprogram SP(GV);
3158 CompileUnit *Unit = MainCU;
3159 if (!Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00003160 Unit = &FindCompileUnit(SP.getCompileUnit());
Devang Patelc4523242009-01-05 23:11:11 +00003161
Evan Chenge3d42322009-02-25 07:04:34 +00003162 // Check for pre-existence.
3163 DIE *&Slot = Unit->getDieMapSlotFor(GV);
3164 if (Slot)
3165 return false;
3166
3167 if (!SP.isDefinition())
3168 // This is a method declaration which will be handled while
3169 // constructing class type.
3170 return false;
3171
3172 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
3173
3174 // Add to map.
3175 Slot = SubprogramDie;
3176 // Add to context owner.
3177 Unit->getDie()->AddChild(SubprogramDie);
3178 // Expose as global.
Bill Wendling0582ae92009-03-13 04:39:26 +00003179 std::string Name;
3180 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003181 return true;
Devang Patelc4523242009-01-05 23:11:11 +00003182 }
3183
Devang Patel78eb6ad2009-01-05 23:21:35 +00003184 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patel53cac182009-02-24 00:02:15 +00003185 /// subprograms. Return true if at least one subprogram DIE is created.
3186 bool ConstructSubprograms() {
Evan Chenge3d42322009-02-25 07:04:34 +00003187 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
3188 if (!Root)
3189 return false;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003190
Evan Chenge3d42322009-02-25 07:04:34 +00003191 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3192 "Malformed subprogram descriptor anchor type");
3193 Constant *RootC = cast<Constant>(*Root->use_begin());
3194 assert(RootC->hasNUsesOrMore(1) &&
3195 "Malformed subprogram descriptor anchor type");
Devang Patel78eb6ad2009-01-05 23:21:35 +00003196
Evan Chenge3d42322009-02-25 07:04:34 +00003197 bool Result = false;
3198 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3199 UI != UE; ++UI)
3200 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling3f500d92009-05-06 21:21:34 +00003201 UUI != UUE; ++UUI)
3202 Result |= ConstructSubprogram(cast<GlobalVariable>(*UUI));
3203
Evan Chenge3d42322009-02-25 07:04:34 +00003204 return Result;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003205 }
3206
Jim Laskey65195462006-10-30 13:35:07 +00003207public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003208 //===--------------------------------------------------------------------===//
3209 // Main entry points.
3210 //
Owen Andersoncb371882008-08-21 00:14:44 +00003211 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00003212 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3213 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3214 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3215 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel7d2f9722009-04-15 20:41:31 +00003216 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003217 if (TimePassesIsEnabled)
3218 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00003219 getDwarfTimerGroup());
Jim Laskeyef42a012006-11-02 20:12:39 +00003220 }
Jim Laskey072200c2007-01-29 18:51:14 +00003221 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003222 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3223 delete Values[j];
Bill Wendling163ba3f2009-03-10 21:23:25 +00003224
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003225 for (DenseMap<const GlobalVariable *, DbgScope *>::iterator
3226 I = AbstractInstanceRootMap.begin(),
3227 E = AbstractInstanceRootMap.end(); I != E;++I)
3228 delete I->second;
3229
Bill Wendling163ba3f2009-03-10 21:23:25 +00003230 delete DebugTimer;
Jim Laskeyef42a012006-11-02 20:12:39 +00003231 }
3232
Bill Wendlingc8615e42009-03-10 21:47:45 +00003233 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3234 /// be emitted.
3235 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3236
Devang Patel5d315982009-01-06 21:07:30 +00003237 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3238 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00003239 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003240 if (TimePassesIsEnabled)
3241 DebugTimer->startTimer();
3242
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003243 // Create all the compile unit DIEs.
3244 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00003245
Bill Wendling163ba3f2009-03-10 21:23:25 +00003246 if (CompileUnits.empty()) {
3247 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003248 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003249
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003250 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003251 }
Devang Patel3f7833a2009-01-12 23:09:42 +00003252
Devang Patel53cac182009-02-24 00:02:15 +00003253 // Create DIEs for each of the externally visible global variables.
3254 bool globalDIEs = ConstructGlobalVariableDIEs();
3255
3256 // Create DIEs for each of the externally visible subprograms.
3257 bool subprogramDIEs = ConstructSubprograms();
3258
3259 // If there is not any debug info available for any global variables
3260 // and any subprograms then there is not any debug info to emit.
Bill Wendling163ba3f2009-03-10 21:23:25 +00003261 if (!globalDIEs && !subprogramDIEs) {
3262 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003263 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003264
Devang Patel53cac182009-02-24 00:02:15 +00003265 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003266 }
Devang Patel53cac182009-02-24 00:02:15 +00003267
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003268 MMI = mmi;
3269 shouldEmit = true;
3270 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00003271
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003272 // Prime section data.
3273 SectionMap.insert(TAI->getTextSection());
Devang Patel5d315982009-01-06 21:07:30 +00003274
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003275 // Print out .file directives to specify files for .loc directives. These
3276 // are printed out early so that they precede any .loc directives.
3277 if (TAI->hasDotLocAndDotFile()) {
Evan Chenge3d42322009-02-25 07:04:34 +00003278 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3279 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00003280 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Chenge3d42322009-02-25 07:04:34 +00003281 sys::Path FullPath(getSourceDirectoryName(Id.first));
3282 bool AppendOk =
3283 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003284 assert(AppendOk && "Could not append filename to directory!");
3285 AppendOk = false;
3286 Asm->EmitFile(i, FullPath.toString());
3287 Asm->EOL();
Devang Patel5d315982009-01-06 21:07:30 +00003288 }
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003289 }
Devang Patel5d315982009-01-06 21:07:30 +00003290
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003291 // Emit initial sections
3292 EmitInitial();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003293
3294 if (TimePassesIsEnabled)
3295 DebugTimer->stopTimer();
Devang Patel5d315982009-01-06 21:07:30 +00003296 }
3297
Jim Laskey65195462006-10-30 13:35:07 +00003298 /// BeginModule - Emit all Dwarf sections that should come prior to the
3299 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003300 void BeginModule(Module *M) {
3301 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003302 }
3303
Jim Laskey65195462006-10-30 13:35:07 +00003304 /// EndModule - Emit all Dwarf sections that should come after the content.
3305 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003306 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003307 if (!ShouldEmitDwarfDebug())
3308 return;
3309
3310 if (TimePassesIsEnabled)
3311 DebugTimer->startTimer();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003312
Jim Laskeyef42a012006-11-02 20:12:39 +00003313 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003314 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003315 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003316 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003317 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003318
Jim Laskeyef42a012006-11-02 20:12:39 +00003319 // End text sections.
3320 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003321 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003322 EmitLabel("section_end", i);
3323 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003324
3325 // Emit common frame information.
3326 EmitCommonDebugFrame();
3327
3328 // Emit function debug frame information
3329 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3330 E = DebugFrames.end(); I != E; ++I)
3331 EmitFunctionDebugFrame(*I);
3332
Jim Laskeyef42a012006-11-02 20:12:39 +00003333 // Compute DIE offsets and sizes.
3334 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003335
Jim Laskeyef42a012006-11-02 20:12:39 +00003336 // Emit all the DIEs into a debug info section
3337 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003338
Jim Laskeyef42a012006-11-02 20:12:39 +00003339 // Corresponding abbreviations into a abbrev section.
3340 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003341
Jim Laskeyef42a012006-11-02 20:12:39 +00003342 // Emit source line correspondence into a debug line section.
3343 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003344
Jim Laskeyef42a012006-11-02 20:12:39 +00003345 // Emit info into a debug pubnames section.
3346 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003347
Jim Laskeyef42a012006-11-02 20:12:39 +00003348 // Emit info into a debug str section.
3349 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003350
Jim Laskeyef42a012006-11-02 20:12:39 +00003351 // Emit info into a debug loc section.
3352 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003353
Jim Laskeyef42a012006-11-02 20:12:39 +00003354 // Emit info into a debug aranges section.
3355 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003356
Jim Laskeyef42a012006-11-02 20:12:39 +00003357 // Emit info into a debug ranges section.
3358 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003359
Jim Laskeyef42a012006-11-02 20:12:39 +00003360 // Emit info into a debug macinfo section.
3361 EmitDebugMacInfo();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003362
Devang Patel0f7fef32009-04-13 17:02:03 +00003363 // Emit inline info.
3364 EmitDebugInlineInfo();
3365
Bill Wendling163ba3f2009-03-10 21:23:25 +00003366 if (TimePassesIsEnabled)
3367 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003368 }
3369
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003370 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003371 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003372 void BeginFunction(MachineFunction *MF) {
Bill Wendling1362f972009-03-11 00:03:50 +00003373 this->MF = MF;
3374
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003375 if (!ShouldEmitDwarfDebug()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003376
Bill Wendling163ba3f2009-03-10 21:23:25 +00003377 if (TimePassesIsEnabled)
3378 DebugTimer->startTimer();
3379
Jim Laskeyef42a012006-11-02 20:12:39 +00003380 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003381 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003382
Jim Laskeyef42a012006-11-02 20:12:39 +00003383 // Assumes in correct section after the entry point.
3384 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003385
Argyrios Kyrtzidis28f14122009-05-04 19:23:45 +00003386 // Emit label for the implicitly defined dbg.stoppoint at the start of
3387 // the function.
Mike Stumpfe095f32009-05-04 18:40:41 +00003388 DebugLoc FDL = MF->getDefaultDebugLoc();
3389 if (!FDL.isUnknown()) {
3390 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
3391 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col,
3392 DICompileUnit(DLT.CompileUnit));
3393 Asm->printLabel(LabelID);
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003394 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003395
3396 if (TimePassesIsEnabled)
3397 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003398 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003399
Jim Laskey65195462006-10-30 13:35:07 +00003400 /// EndFunction - Gather and emit post-function debug information.
3401 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003402 void EndFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003403 if (!ShouldEmitDwarfDebug()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003404
Bill Wendling163ba3f2009-03-10 21:23:25 +00003405 if (TimePassesIsEnabled)
3406 DebugTimer->startTimer();
3407
Jim Laskeyb82313f2007-02-01 16:31:34 +00003408 // Define end label for subprogram.
3409 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003410
Jim Laskeyef42a012006-11-02 20:12:39 +00003411 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00003412 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003413 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003414 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003415 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00003416 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00003417 // Append the function info to section info.
3418 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00003419 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00003420 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003421
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003422 // Construct the DbgScope for abstract instances.
3423 for (SmallVector<DbgScope *, 32>::iterator
3424 I = AbstractInstanceRootList.begin(),
3425 E = AbstractInstanceRootList.end(); I != E; ++I)
3426 ConstructAbstractDbgScope(*I);
3427
Jim Laskeyef42a012006-11-02 20:12:39 +00003428 // Construct scopes for subprogram.
Devang Patel7d2f9722009-04-15 20:41:31 +00003429 if (FunctionDbgScope)
3430 ConstructFunctionDbgScope(FunctionDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00003431 else
3432 // FIXME: This is wrong. We are essentially getting past a problem with
3433 // debug information not being able to handle unreachable blocks that have
3434 // debug information in them. In particular, those unreachable blocks that
3435 // have "region end" info in them. That situation results in the "root
3436 // scope" not being created. If that's the case, then emit a "default"
3437 // scope, i.e., one that encompasses the whole function. This isn't
3438 // desirable. And a better way of handling this (and all of the debugging
3439 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003440 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003441
3442 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3443 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003444
3445 // Clear debug info
Devang Patel7d2f9722009-04-15 20:41:31 +00003446 if (FunctionDbgScope) {
3447 delete FunctionDbgScope;
Devang Patel481ff5b2009-01-12 18:48:36 +00003448 DbgScopeMap.clear();
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003449 DbgAbstractScopeMap.clear();
3450 DbgConcreteScopeMap.clear();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003451 InlinedVariableScopes.clear();
Devang Patel7d2f9722009-04-15 20:41:31 +00003452 FunctionDbgScope = NULL;
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003453 LexicalScopeStack.clear();
3454 AbstractInstanceRootList.clear();
Devang Patel481ff5b2009-01-12 18:48:36 +00003455 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003456
Bill Wendling163ba3f2009-03-10 21:23:25 +00003457 Lines.clear();
3458
3459 if (TimePassesIsEnabled)
3460 DebugTimer->stopTimer();
3461 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003462
3463 /// RecordSourceLine - Records location information and associates it with a
3464 /// label. Returns a unique label ID used to generate a label and provide
3465 /// correspondence to the source line list.
3466 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003467 if (TimePassesIsEnabled)
3468 DebugTimer->startTimer();
3469
Evan Chenge3d42322009-02-25 07:04:34 +00003470 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003471 assert(Unit && "Unable to find CompileUnit");
Devang Patelccca7fe2009-01-12 19:17:34 +00003472 unsigned ID = MMI->NextLabelID();
3473 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003474
3475 if (TimePassesIsEnabled)
3476 DebugTimer->stopTimer();
3477
Devang Patelccca7fe2009-01-12 19:17:34 +00003478 return ID;
3479 }
3480
3481 /// RecordSourceLine - Records location information and associates it with a
3482 /// label. Returns a unique label ID used to generate a label and provide
3483 /// correspondence to the source line list.
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00003484 unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003485 if (TimePassesIsEnabled)
3486 DebugTimer->startTimer();
3487
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00003488 std::string Dir, Fn;
3489 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
3490 CU.getFilename(Fn));
Devang Patelccca7fe2009-01-12 19:17:34 +00003491 unsigned ID = MMI->NextLabelID();
3492 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003493
3494 if (TimePassesIsEnabled)
3495 DebugTimer->stopTimer();
3496
Devang Patelccca7fe2009-01-12 19:17:34 +00003497 return ID;
3498 }
3499
Bill Wendlingc8615e42009-03-10 21:47:45 +00003500 /// getRecordSourceLineCount - Return the number of source lines in the debug
3501 /// info.
3502 unsigned getRecordSourceLineCount() const {
Devang Patelccca7fe2009-01-12 19:17:34 +00003503 return Lines.size();
3504 }
3505
Bill Wendlingc8615e42009-03-10 21:47:45 +00003506 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3507 /// timed. Look up the source id with the given directory and source file
3508 /// names. If none currently exists, create a new id and insert it in the
3509 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3510 /// well.
Evan Chenge3d42322009-02-25 07:04:34 +00003511 unsigned getOrCreateSourceID(const std::string &DirName,
3512 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003513 if (TimePassesIsEnabled)
3514 DebugTimer->startTimer();
3515
Bill Wendlingc8615e42009-03-10 21:47:45 +00003516 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003517
3518 if (TimePassesIsEnabled)
3519 DebugTimer->stopTimer();
3520
Evan Chenge3d42322009-02-25 07:04:34 +00003521 return SrcId;
Devang Patelccca7fe2009-01-12 19:17:34 +00003522 }
3523
3524 /// RecordRegionStart - Indicate the start of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003525 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003526 if (TimePassesIsEnabled)
3527 DebugTimer->startTimer();
3528
Devang Patelccca7fe2009-01-12 19:17:34 +00003529 DbgScope *Scope = getOrCreateScope(V);
3530 unsigned ID = MMI->NextLabelID();
3531 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003532 LexicalScopeStack.push_back(Scope);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003533
3534 if (TimePassesIsEnabled)
3535 DebugTimer->stopTimer();
3536
Devang Patelccca7fe2009-01-12 19:17:34 +00003537 return ID;
3538 }
3539
3540 /// RecordRegionEnd - Indicate the end of a region.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003541 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003542 if (TimePassesIsEnabled)
3543 DebugTimer->startTimer();
3544
Bill Wendling5b8479c2009-05-07 17:26:14 +00003545 DbgScope *Scope = getOrCreateScope(V);
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003546 unsigned ID = MMI->NextLabelID();
Devang Patelccca7fe2009-01-12 19:17:34 +00003547 Scope->setEndLabelID(ID);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003548 if (LexicalScopeStack.size() != 0)
3549 LexicalScopeStack.pop_back();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003550
3551 if (TimePassesIsEnabled)
3552 DebugTimer->stopTimer();
3553
Devang Patelccca7fe2009-01-12 19:17:34 +00003554 return ID;
3555 }
3556
3557 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patel1be3ecc2009-04-15 00:10:26 +00003558 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
3559 const MachineInstr *MI) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003560 if (TimePassesIsEnabled)
3561 DebugTimer->startTimer();
3562
Devang Patel0e5200f2009-01-15 18:25:17 +00003563 DIDescriptor Desc(GV);
3564 DbgScope *Scope = NULL;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003565
Devang Patel0e5200f2009-01-15 18:25:17 +00003566 if (Desc.getTag() == DW_TAG_variable) {
3567 // GV is a global variable.
3568 DIGlobalVariable DG(GV);
3569 Scope = getOrCreateScope(DG.getContext().getGV());
3570 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003571 DenseMap<const MachineInstr *, DbgScope *>::iterator
3572 SI = InlinedVariableScopes.find(MI);
Bill Wendling3f500d92009-05-06 21:21:34 +00003573
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003574 if (SI != InlinedVariableScopes.end()) {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003575 // or GV is an inlined local variable.
3576 Scope = SI->second;
3577 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003578 DIVariable DV(GV);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003579 GlobalVariable *V = DV.getContext().getGV();
3580
3581 // FIXME: The code that checks for the inlined local variable is a hack!
3582 DenseMap<const GlobalVariable *, DbgScope *>::iterator
3583 AI = AbstractInstanceRootMap.find(V);
3584
3585 if (AI != AbstractInstanceRootMap.end())
3586 // or GV is an inlined local variable.
3587 Scope = AI->second;
3588 else
3589 // or GV is a local variable.
3590 Scope = getOrCreateScope(V);
Devang Patel1be3ecc2009-04-15 00:10:26 +00003591 }
Devang Patel0e5200f2009-01-15 18:25:17 +00003592 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003593
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003594 assert(Scope && "Unable to find the variable's scope");
Devang Patel99ec3532009-01-16 19:28:14 +00003595 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelccca7fe2009-01-12 19:17:34 +00003596 Scope->AddVariable(DV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003597
3598 if (TimePassesIsEnabled)
3599 DebugTimer->stopTimer();
Devang Patelccca7fe2009-01-12 19:17:34 +00003600 }
Devang Patel0f7fef32009-04-13 17:02:03 +00003601
Devang Patel1be3ecc2009-04-15 00:10:26 +00003602 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003603 unsigned RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
3604 unsigned Line, unsigned Col) {
3605 unsigned LabelID = MMI->NextLabelID();
3606
Devang Patel1be3ecc2009-04-15 00:10:26 +00003607 if (!TAI->doesDwarfUsesInlineInfoSection())
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003608 return LabelID;
Devang Patel1be3ecc2009-04-15 00:10:26 +00003609
Bill Wendlingd5a63812009-05-01 08:40:06 +00003610 if (TimePassesIsEnabled)
3611 DebugTimer->startTimer();
3612
Devang Patel1be3ecc2009-04-15 00:10:26 +00003613 GlobalVariable *GV = SP.getGV();
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003614 DenseMap<const GlobalVariable *, DbgScope *>::iterator
3615 II = AbstractInstanceRootMap.find(GV);
Devang Patel1be3ecc2009-04-15 00:10:26 +00003616
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003617 if (II == AbstractInstanceRootMap.end()) {
3618 // Create an abstract instance entry for this inlined function if it
3619 // doesn't already exist.
3620 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(GV));
Bill Wendlingef956fc2009-05-01 08:32:14 +00003621
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003622 // Get the compile unit context.
3623 CompileUnit *Unit = &FindCompileUnit(SP.getCompileUnit());
3624 DIE *SPDie = Unit->getDieMapSlotFor(GV);
3625 if (!SPDie)
3626 SPDie = CreateSubprogramDIE(Unit, SP);
3627
3628 // Mark as being inlined. This makes this subprogram entry an abstract
3629 // instance root.
3630 // FIXME: Our debugger doesn't care about the value of DW_AT_inline, only
3631 // that it's defined. It probably won't change in the future, but this
3632 // could be more elegant.
3633 AddUInt(SPDie, DW_AT_inline, 0, DW_INL_declared_not_inlined);
3634
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003635 // Keep track of the abstract scope for this function.
3636 DbgAbstractScopeMap[GV] = Scope;
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00003637
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003638 AbstractInstanceRootMap[GV] = Scope;
3639 AbstractInstanceRootList.push_back(Scope);
3640 }
3641
3642 // Create a concrete inlined instance for this inlined function.
3643 DbgConcreteScope *ConcreteScope = new DbgConcreteScope(DIDescriptor(GV));
3644 DIE *ScopeDie = new DIE(DW_TAG_inlined_subroutine);
3645 CompileUnit *Unit = &FindCompileUnit(SP.getCompileUnit());
3646 ScopeDie->setAbstractCompileUnit(Unit);
3647
3648 DIE *Origin = Unit->getDieMapSlotFor(GV);
3649 AddDIEntry(ScopeDie, DW_AT_abstract_origin, DW_FORM_ref4, Origin);
3650 AddUInt(ScopeDie, DW_AT_call_file, 0, Unit->getID());
3651 AddUInt(ScopeDie, DW_AT_call_line, 0, Line);
3652 AddUInt(ScopeDie, DW_AT_call_column, 0, Col);
3653
3654 ConcreteScope->setDie(ScopeDie);
3655 ConcreteScope->setStartLabelID(LabelID);
Bill Wendlingbc12c2b2009-05-12 00:06:59 +00003656 MMI->RecordUsedDbgLabel(LabelID);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003657
3658 LexicalScopeStack.back()->AddConcreteInst(ConcreteScope);
3659
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003660 // Keep track of the concrete scope that's inlined into this function.
3661 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 8> >::iterator
3662 SI = DbgConcreteScopeMap.find(GV);
3663
3664 if (SI == DbgConcreteScopeMap.end())
3665 DbgConcreteScopeMap[GV].push_back(ConcreteScope);
3666 else
3667 SI->second.push_back(ConcreteScope);
3668
Bill Wendlingbc12c2b2009-05-12 00:06:59 +00003669 // Track the start label for this inlined function.
3670 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
3671 I = InlineInfo.find(GV);
3672
3673 if (I == InlineInfo.end())
3674 InlineInfo[GV].push_back(LabelID);
3675 else
3676 I->second.push_back(LabelID);
3677
Bill Wendlingd5a63812009-05-01 08:40:06 +00003678 if (TimePassesIsEnabled)
3679 DebugTimer->stopTimer();
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003680
3681 return LabelID;
Devang Patel0f7fef32009-04-13 17:02:03 +00003682 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00003683
3684 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
3685 unsigned RecordInlinedFnEnd(DISubprogram &SP) {
3686 if (!TAI->doesDwarfUsesInlineInfoSection())
3687 return 0;
3688
Bill Wendlingd5a63812009-05-01 08:40:06 +00003689 if (TimePassesIsEnabled)
3690 DebugTimer->startTimer();
3691
Devang Patel1be3ecc2009-04-15 00:10:26 +00003692 GlobalVariable *GV = SP.getGV();
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00003693 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 8> >::iterator
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003694 I = DbgConcreteScopeMap.find(GV);
Bill Wendling3f500d92009-05-06 21:21:34 +00003695
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003696 if (I == DbgConcreteScopeMap.end()) {
Bill Wendlingd5a63812009-05-01 08:40:06 +00003697 if (TimePassesIsEnabled)
3698 DebugTimer->stopTimer();
3699
Devang Patel1be3ecc2009-04-15 00:10:26 +00003700 return 0;
Bill Wendlingd5a63812009-05-01 08:40:06 +00003701 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00003702
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00003703 SmallVector<DbgScope *, 8> &Scopes = I->second;
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00003704 assert(!Scopes.empty() && "We should have at least one debug scope!");
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00003705 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003706 unsigned ID = MMI->NextLabelID();
3707 MMI->RecordUsedDbgLabel(ID);
3708 Scope->setEndLabelID(ID);
Bill Wendlingd5a63812009-05-01 08:40:06 +00003709
3710 if (TimePassesIsEnabled)
3711 DebugTimer->stopTimer();
3712
Devang Patel1be3ecc2009-04-15 00:10:26 +00003713 return ID;
3714 }
3715
3716 /// RecordVariableScope - Record scope for the variable declared by
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003717 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE. Record scopes
3718 /// for only inlined subroutine variables. Other variables's scopes are
3719 /// determined during RecordVariable().
Devang Patel1be3ecc2009-04-15 00:10:26 +00003720 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI) {
Bill Wendlingd5a63812009-05-01 08:40:06 +00003721 if (TimePassesIsEnabled)
3722 DebugTimer->startTimer();
3723
Devang Patel1be3ecc2009-04-15 00:10:26 +00003724 DISubprogram SP(DV.getContext().getGV());
Bill Wendlingd5a63812009-05-01 08:40:06 +00003725
3726 if (SP.isNull()) {
3727 if (TimePassesIsEnabled)
3728 DebugTimer->stopTimer();
3729
Devang Patel1be3ecc2009-04-15 00:10:26 +00003730 return;
Bill Wendlingd5a63812009-05-01 08:40:06 +00003731 }
3732
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003733 DenseMap<GlobalVariable *, DbgScope *>::iterator
3734 I = DbgAbstractScopeMap.find(SP.getGV());
3735 if (I != DbgAbstractScopeMap.end())
3736 InlinedVariableScopes[DeclareMI] = I->second;
Devang Patel1be3ecc2009-04-15 00:10:26 +00003737
Bill Wendlingd5a63812009-05-01 08:40:06 +00003738 if (TimePassesIsEnabled)
3739 DebugTimer->stopTimer();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003740 }
Jim Laskey65195462006-10-30 13:35:07 +00003741};
3742
Jim Laskey072200c2007-01-29 18:51:14 +00003743//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003744/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003745///
3746class DwarfException : public Dwarf {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003747 struct FunctionEHFrameInfo {
3748 std::string FnName;
3749 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003750 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003751 bool hasCalls;
3752 bool hasLandingPads;
3753 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003754 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003755
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003756 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3757 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003758 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003759 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003760 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003761 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003762 };
3763
3764 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003765
3766 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3767 /// be emitted.
3768 bool shouldEmitTable;
3769
3770 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3771 /// should be emitted.
3772 bool shouldEmitMoves;
3773
3774 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3775 /// should be emitted.
3776 bool shouldEmitTableModule;
3777
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003778 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003779 /// should be emitted.
3780 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003781
Bill Wendling163ba3f2009-03-10 21:23:25 +00003782 /// ExceptionTimer - Timer for the Dwarf exception writer.
3783 Timer *ExceptionTimer;
3784
Jim Laskey3f09fc22007-02-28 18:38:31 +00003785 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3786 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003787 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003788 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003789 int stackGrowth =
3790 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3791 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003792 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003793
Jim Laskeybacd3042007-02-21 22:48:45 +00003794 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003795 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003796
3797 if (!TAI->doesRequireNonLocalEHFrameLabel())
3798 O << TAI->getEHGlobalPrefix();
3799 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003800 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003801
Jim Laskeybacd3042007-02-21 22:48:45 +00003802 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003803 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003804
Jim Laskeybacd3042007-02-21 22:48:45 +00003805 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003806 EmitDifference("eh_frame_common_end", Index,
3807 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003808 Asm->EOL("Length of Common Information Entry");
3809
Jim Laskeybacd3042007-02-21 22:48:45 +00003810 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003811 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003812 Asm->EmitInt32((int)0);
3813 Asm->EOL("CIE Identifier Tag");
3814 Asm->EmitInt8(DW_CIE_VERSION);
3815 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003816
Jim Laskeybacd3042007-02-21 22:48:45 +00003817 // The personality presence indicates that language specific information
3818 // will show up in the eh frame.
3819 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003820 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003821
Jim Laskeybacd3042007-02-21 22:48:45 +00003822 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003823 Asm->EmitULEB128Bytes(1);
3824 Asm->EOL("CIE Code Alignment Factor");
3825 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003826 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003827 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003828 Asm->EOL("CIE Return Address Column");
3829
Jim Laskeybacd3042007-02-21 22:48:45 +00003830 // If there is a personality, we need to indicate the functions location.
3831 if (Personality) {
3832 Asm->EmitULEB128Bytes(7);
3833 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003834
Duncan Sands671fa972008-05-07 19:11:09 +00003835 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003836 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003837 Asm->EOL("Personality (pcrel sdata4 indirect)");
3838 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003839 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003840 Asm->EOL("Personality (pcrel sdata4)");
3841 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003842
Duncan Sands671fa972008-05-07 19:11:09 +00003843 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003844 O << TAI->getPersonalityPrefix();
3845 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3846 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003847 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3848 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003849 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003850
Duncan Sands671fa972008-05-07 19:11:09 +00003851 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3852 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003853
Bill Wendlingd60de512009-01-05 22:53:45 +00003854 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3855 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003856 } else {
3857 Asm->EmitULEB128Bytes(1);
3858 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003859
Bill Wendlingd60de512009-01-05 22:53:45 +00003860 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3861 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003862 }
3863
3864 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003865 std::vector<MachineMove> Moves;
3866 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003867 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003868
Dale Johannesen21d972a2008-04-30 00:43:29 +00003869 // On Darwin the linker honors the alignment of eh_frame, which means it
3870 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3871 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003872 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003873 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003874 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003875
Jim Laskeybacd3042007-02-21 22:48:45 +00003876 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003877 }
Duncan Sands671fa972008-05-07 19:11:09 +00003878
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003879 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003880 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003881 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003882 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
Chris Lattner266c7bb2009-04-13 05:44:34 +00003883
3884 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
3885 "Should not emit 'available externally' functions at all");
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003886
Jim Laskeybacd3042007-02-21 22:48:45 +00003887 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3888
3889 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003890 // If the corresponding function is static, this should not be
3891 // externally visible.
Rafael Espindolabb46f522009-01-15 20:18:42 +00003892 if (linkage != Function::InternalLinkage &&
Devang Patel2d1768c2009-01-17 08:05:14 +00003893 linkage != Function::PrivateLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003894 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3895 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3896 }
3897
Dale Johannesen038129d2008-01-10 02:03:30 +00003898 // If corresponding function is weak definition, this should be too.
Duncan Sands667d4b82009-03-07 15:45:40 +00003899 if ((linkage == Function::WeakAnyLinkage ||
3900 linkage == Function::WeakODRLinkage ||
3901 linkage == Function::LinkOnceAnyLinkage ||
3902 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003903 TAI->getWeakDefDirective())
3904 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3905
3906 // If there are no calls then you can't unwind. This may mean we can
3907 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003908 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003909 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003910 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003911 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003912 !UnwindTablesMandatory &&
Duncan Sands667d4b82009-03-07 15:45:40 +00003913 ((linkage != Function::WeakAnyLinkage &&
3914 linkage != Function::WeakODRLinkage &&
3915 linkage != Function::LinkOnceAnyLinkage &&
3916 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003917 !TAI->getWeakDefDirective() ||
3918 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003919 {
Bill Wendling6e198962007-09-18 01:47:22 +00003920 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003921 // This name has no connection to the function, so it might get
3922 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003923 // dead-stripping unconditionally.
3924 if (const char *UsedDirective = TAI->getUsedDirective())
3925 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003926 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003927 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003928
Jim Laskeybacd3042007-02-21 22:48:45 +00003929 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003930 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3931 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003932 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003933
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003934 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003935
Bill Wendling722f5f12008-12-24 08:05:17 +00003936 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3937 PrintRelDirective(true, true);
3938 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3939
3940 if (!TAI->isAbsoluteEHSectionOffsets())
3941 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3942 } else {
3943 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3944 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3945 true, true, false);
3946 }
3947
Jim Laskeybacd3042007-02-21 22:48:45 +00003948 Asm->EOL("FDE CIE offset");
3949
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003950 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003951 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003952 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003953 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003954 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003955
Jim Laskey3f09fc22007-02-28 18:38:31 +00003956 // If there is a personality and landing pads then point to the language
3957 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003958 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003959 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003960 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003961
3962 if (EHFrameInfo.hasLandingPads)
3963 EmitReference("exception", EHFrameInfo.Number, true, true);
3964 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003965 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003966 Asm->EOL("Language Specific Data Area");
3967 } else {
3968 Asm->EmitULEB128Bytes(0);
3969 Asm->EOL("Augmentation size");
3970 }
Duncan Sands671fa972008-05-07 19:11:09 +00003971
Jim Laskeybacd3042007-02-21 22:48:45 +00003972 // Indicate locations of function specific callee saved registers in
3973 // frame.
Devang Patel5aac3d32009-01-17 08:01:33 +00003974 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00003975 true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003976
Dale Johannesen21d972a2008-04-30 00:43:29 +00003977 // On Darwin the linker honors the alignment of eh_frame, which means it
3978 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3979 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003980 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003981 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003982 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003983
3984 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003985 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003986 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003987 // that depends on unused functions (calling undefined externals) being
3988 // dead-stripped to link correctly. Yes, there really is.
3989 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3990 if (const char *UsedDirective = TAI->getUsedDirective())
3991 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3992 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003993 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003994
Duncan Sands57810cd2007-09-05 11:27:52 +00003995 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003996 ///
3997 /// The general organization of the table is complex, but the basic concepts
3998 /// are easy. First there is a header which describes the location and
3999 /// organization of the three components that follow.
4000 /// 1. The landing pad site information describes the range of code covered
4001 /// by the try. In our case it's an accumulation of the ranges covered
4002 /// by the invokes in the try. There is also a reference to the landing
4003 /// pad that handles the exception once processed. Finally an index into
4004 /// the actions table.
4005 /// 2. The action table, in our case, is composed of pairs of type ids
4006 /// and next action offset. Starting with the action index from the
4007 /// landing pad site, each type Id is checked for a match to the current
4008 /// exception. If it matches then the exception and type id are passed
4009 /// on to the landing pad. Otherwise the next action is looked up. This
4010 /// chain is terminated with a next action of zero. If no type id is
4011 /// found the the frame is unwound and handling continues.
4012 /// 3. Type id table contains references to all the C++ typeinfo for all
4013 /// catches in the function. This tables is reversed indexed base 1.
4014
Duncan Sandsb32edb42007-06-06 15:37:31 +00004015 /// SharedTypeIds - How many leading type ids two landing pads have in common.
4016 static unsigned SharedTypeIds(const LandingPadInfo *L,
4017 const LandingPadInfo *R) {
4018 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004019 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004020 unsigned MinSize = LSize < RSize ? LSize : RSize;
4021 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004022
Duncan Sandsb32edb42007-06-06 15:37:31 +00004023 for (; Count != MinSize; ++Count)
4024 if (LIds[Count] != RIds[Count])
4025 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004026
Duncan Sandsb32edb42007-06-06 15:37:31 +00004027 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004028 }
4029
Duncan Sandsb32edb42007-06-06 15:37:31 +00004030 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00004031 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004032 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004033 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004034 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004035
Duncan Sandsb32edb42007-06-06 15:37:31 +00004036 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00004037 if (LIds[i] != RIds[i])
4038 return LIds[i] < RIds[i];
4039
Duncan Sandsb32edb42007-06-06 15:37:31 +00004040 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004041 }
4042
Duncan Sands53c3a332007-05-16 12:12:23 +00004043 struct KeyInfo {
4044 static inline unsigned getEmptyKey() { return -1U; }
4045 static inline unsigned getTombstoneKey() { return -2U; }
4046 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00004047 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00004048 static bool isPod() { return true; }
4049 };
4050
Duncan Sands57810cd2007-09-05 11:27:52 +00004051 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004052 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004053 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004054 int NextAction;
4055 struct ActionEntry *Previous;
4056 };
4057
Duncan Sands57810cd2007-09-05 11:27:52 +00004058 /// PadRange - Structure holding a try-range and the associated landing pad.
4059 struct PadRange {
4060 // The index of the landing pad.
4061 unsigned PadIndex;
4062 // The index of the begin and end labels in the landing pad's label lists.
4063 unsigned RangeIndex;
4064 };
4065
4066 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
4067
4068 /// CallSiteEntry - Structure describing an entry in the call-site table.
4069 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00004070 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004071 unsigned BeginLabel; // zero indicates the start of the function.
4072 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00004073 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004074 unsigned PadLabel; // zero indicates that there is no landing pad.
4075 unsigned Action;
4076 };
4077
Jim Laskeybacd3042007-02-21 22:48:45 +00004078 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00004079 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00004080 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00004081 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
4082 if (PadInfos.empty()) return;
4083
4084 // Sort the landing pads in order of their type ids. This is used to fold
4085 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00004086 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00004087 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00004088 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00004089 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004090 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
4091
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004092 // Negative type ids index into FilterIds, positive type ids index into
4093 // TypeInfos. The value written for a positive type id is just the type
4094 // id itself. For a negative type id, however, the value written is the
4095 // (negative) byte offset of the corresponding FilterIds entry. The byte
4096 // offset is usually equal to the type id, because the FilterIds entries
4097 // are written using a variable width encoding which outputs one byte per
4098 // entry as long as the value written is not too large, but can differ.
4099 // This kind of complication does not occur for positive type ids because
4100 // type infos are output using a fixed width encoding.
4101 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
4102 SmallVector<int, 16> FilterOffsets;
4103 FilterOffsets.reserve(FilterIds.size());
4104 int Offset = -1;
4105 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
4106 E = FilterIds.end(); I != E; ++I) {
4107 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004108 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004109 }
4110
Duncan Sands57810cd2007-09-05 11:27:52 +00004111 // Compute the actions table and gather the first action index for each
4112 // landing pad site.
4113 SmallVector<ActionEntry, 32> Actions;
4114 SmallVector<unsigned, 64> FirstActions;
4115 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00004116
Duncan Sandsb32edb42007-06-06 15:37:31 +00004117 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00004118 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004119 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004120 const LandingPadInfo *LP = LandingPads[i];
4121 const std::vector<int> &TypeIds = LP->TypeIds;
4122 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004123 unsigned SizeSiteActions = 0;
4124
Duncan Sandsb32edb42007-06-06 15:37:31 +00004125 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00004126 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00004127 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004128
Duncan Sandsb32edb42007-06-06 15:37:31 +00004129 if (NumShared) {
4130 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
4131 assert(Actions.size());
4132 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004133 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
4134 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004135 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004136 SizeAction -=
4137 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004138 SizeAction += -PrevAction->NextAction;
4139 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004140 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00004141 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00004142
Duncan Sandsb32edb42007-06-06 15:37:31 +00004143 // Compute the actions.
4144 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
4145 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004146 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4147 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004148 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004149
4150 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004151 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004152 SizeSiteActions += SizeAction;
4153
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004154 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00004155 Actions.push_back(Action);
4156
4157 PrevAction = &Actions.back();
4158 }
4159
4160 // Record the first action of the landing pad site.
4161 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4162 } // else identical - re-use previous FirstAction
4163
4164 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004165
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00004166 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00004167 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00004168 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004169
Duncan Sands481dc722007-12-19 07:36:31 +00004170 // Compute the call-site table. The entry for an invoke has a try-range
4171 // containing the call, a non-zero landing pad and an appropriate action.
4172 // The entry for an ordinary call has a try-range containing the call and
4173 // zero for the landing pad and the action. Calls marked 'nounwind' have
4174 // no entry and must not be contained in the try-range of any entry - they
4175 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004176 SmallVector<CallSiteEntry, 64> CallSites;
4177
4178 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00004179 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4180 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4181 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00004182 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4183 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00004184 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004185 unsigned BeginLabel = LandingPad->BeginLabels[j];
4186 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4187 PadRange P = { i, j };
4188 PadMap[BeginLabel] = P;
4189 }
4190 }
4191
Duncan Sands481dc722007-12-19 07:36:31 +00004192 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00004193 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00004194
4195 // Whether there is a potentially throwing instruction (currently this means
4196 // an ordinary call) between the end of the previous try-range and now.
4197 bool SawPotentiallyThrowing = false;
4198
4199 // Whether the last callsite entry was for an invoke.
4200 bool PreviousIsInvoke = false;
4201
Duncan Sands481dc722007-12-19 07:36:31 +00004202 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004203 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4204 I != E; ++I) {
4205 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4206 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00004207 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00004208 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00004209 continue;
4210 }
4211
Chris Lattner9e330492007-12-30 20:50:28 +00004212 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00004213 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00004214
Duncan Sands481dc722007-12-19 07:36:31 +00004215 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00004216 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00004217 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004218
Duncan Sands481dc722007-12-19 07:36:31 +00004219 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00004220 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00004221 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00004222 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00004223 continue;
4224
4225 PadRange P = L->second;
4226 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4227
4228 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4229 "Inconsistent landing pad map!");
4230
4231 // If some instruction between the previous try-range and this one may
4232 // throw, create a call-site entry with no landing pad for the region
4233 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00004234 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004235 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4236 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00004237 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004238 }
4239
4240 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00004241 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00004242
Duncan Sands481dc722007-12-19 07:36:31 +00004243 if (LandingPad->LandingPadLabel) {
4244 // This try-range is for an invoke.
4245 CallSiteEntry Site = {BeginLabel, LastLabel,
4246 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00004247
Duncan Sands481dc722007-12-19 07:36:31 +00004248 // Try to merge with the previous call-site.
4249 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00004250 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00004251 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4252 // Extend the range of the previous entry.
4253 Prev.EndLabel = Site.EndLabel;
4254 continue;
4255 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004256 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004257
Duncan Sands481dc722007-12-19 07:36:31 +00004258 // Otherwise, create a new call-site.
4259 CallSites.push_back(Site);
4260 PreviousIsInvoke = true;
4261 } else {
4262 // Create a gap.
4263 PreviousIsInvoke = false;
4264 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004265 }
4266 }
4267 // If some instruction between the previous try-range and the end of the
4268 // function may throw, create a call-site entry with no landing pad for the
4269 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00004270 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004271 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4272 CallSites.push_back(Site);
4273 }
4274
Jim Laskeybacd3042007-02-21 22:48:45 +00004275 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00004276
4277 // Call sites.
4278 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4279 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4280 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4281 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4282 SiteLengthSize +
4283 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00004284 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004285 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00004286
Duncan Sands671fa972008-05-07 19:11:09 +00004287 // Type infos.
4288 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4289 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004290
4291 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004292 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004293 SizeSites + SizeActions + SizeTypes;
4294
4295 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4296 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004297 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004298 TypeOffset;
4299
4300 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004301
Jim Laskeybacd3042007-02-21 22:48:45 +00004302 // Begin the exception table.
4303 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00004304 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00004305 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004306 for (unsigned i = 0; i != SizeAlign; ++i) {
4307 Asm->EmitInt8(0);
4308 Asm->EOL("Padding");
4309 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004310 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004311
Jim Laskeybacd3042007-02-21 22:48:45 +00004312 // Emit the header.
4313 Asm->EmitInt8(DW_EH_PE_omit);
4314 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4315 Asm->EmitInt8(DW_EH_PE_absptr);
4316 Asm->EOL("TType format (DW_EH_PE_absptr)");
4317 Asm->EmitULEB128Bytes(TypeOffset);
4318 Asm->EOL("TType base offset");
4319 Asm->EmitInt8(DW_EH_PE_udata4);
4320 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4321 Asm->EmitULEB128Bytes(SizeSites);
4322 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004323
Duncan Sands57810cd2007-09-05 11:27:52 +00004324 // Emit the landing pad site information.
4325 for (unsigned i = 0; i < CallSites.size(); ++i) {
4326 CallSiteEntry &S = CallSites[i];
4327 const char *BeginTag;
4328 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00004329
Duncan Sands57810cd2007-09-05 11:27:52 +00004330 if (!S.BeginLabel) {
4331 BeginTag = "eh_func_begin";
4332 BeginNumber = SubprogramCount;
4333 } else {
4334 BeginTag = "label";
4335 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004336 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004337
Duncan Sands57810cd2007-09-05 11:27:52 +00004338 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004339 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004340 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004341
Duncan Sands57810cd2007-09-05 11:27:52 +00004342 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004343 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004344 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004345 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004346 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004347 }
4348 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004349
Duncan Sands671fa972008-05-07 19:11:09 +00004350 if (!S.PadLabel)
4351 Asm->EmitInt32(0);
4352 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004353 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004354 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004355 Asm->EOL("Landing pad");
4356
4357 Asm->EmitULEB128Bytes(S.Action);
4358 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004359 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004360
Jim Laskeybacd3042007-02-21 22:48:45 +00004361 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004362 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4363 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004364
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004365 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004366 Asm->EOL("TypeInfo index");
4367 Asm->EmitSLEB128Bytes(Action.NextAction);
4368 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004369 }
4370
4371 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004372 for (unsigned M = TypeInfos.size(); M; --M) {
4373 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004374
4375 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004376
Bill Wendling7d16e852009-04-10 00:12:49 +00004377 if (GV) {
4378 std::string GLN;
4379 O << Asm->getGlobalLinkName(GV, GLN);
4380 } else {
Jim Laskeybacd3042007-02-21 22:48:45 +00004381 O << "0";
Bill Wendling7d16e852009-04-10 00:12:49 +00004382 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004383
Jim Laskeybacd3042007-02-21 22:48:45 +00004384 Asm->EOL("TypeInfo");
4385 }
4386
Duncan Sands73ef58a2007-06-02 16:53:42 +00004387 // Emit the filter typeids.
4388 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4389 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004390 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004391 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004392 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004393
Evan Cheng05548eb2008-02-29 19:36:59 +00004394 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004395 }
4396
Jim Laskey072200c2007-01-29 18:51:14 +00004397public:
4398 //===--------------------------------------------------------------------===//
4399 // Main entry points.
4400 //
Owen Andersoncb371882008-08-21 00:14:44 +00004401 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00004402 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4403 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4404 ExceptionTimer(0) {
4405 if (TimePassesIsEnabled)
4406 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00004407 getDwarfTimerGroup());
Bill Wendling163ba3f2009-03-10 21:23:25 +00004408 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004409
Bill Wendling163ba3f2009-03-10 21:23:25 +00004410 virtual ~DwarfException() {
4411 delete ExceptionTimer;
4412 }
Jim Laskey072200c2007-01-29 18:51:14 +00004413
4414 /// SetModuleInfo - Set machine module information when it's known that pass
4415 /// manager has created it. Set by the target AsmPrinter.
4416 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004417 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004418 }
4419
4420 /// BeginModule - Emit all exception information that should come prior to the
4421 /// content.
4422 void BeginModule(Module *M) {
4423 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004424 }
4425
4426 /// EndModule - Emit all exception information that should come after the
4427 /// content.
4428 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004429 if (TimePassesIsEnabled)
4430 ExceptionTimer->startTimer();
4431
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004432 if (shouldEmitMovesModule || shouldEmitTableModule) {
4433 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Chenge3d42322009-02-25 07:04:34 +00004434 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004435 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004436
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004437 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4438 E = EHFrames.end(); I != E; ++I)
4439 EmitEHFrame(*I);
4440 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004441
4442 if (TimePassesIsEnabled)
4443 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004444 }
4445
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004446 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004447 /// emitted immediately after the function entry point.
4448 void BeginFunction(MachineFunction *MF) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004449 if (TimePassesIsEnabled)
4450 ExceptionTimer->startTimer();
4451
Jim Laskey072200c2007-01-29 18:51:14 +00004452 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004453 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004454
Bill Wendling163ba3f2009-03-10 21:23:25 +00004455 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004456 // Map all labels and get rid of any dead landing pads.
4457 MMI->TidyLandingPads();
Bill Wendling163ba3f2009-03-10 21:23:25 +00004458
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004459 // If any landing pads survive, we need an EH table.
4460 if (MMI->getLandingPads().size())
4461 shouldEmitTable = true;
4462
4463 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004464 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004465 shouldEmitMoves = true;
4466
4467 if (shouldEmitMoves || shouldEmitTable)
4468 // Assumes in correct section after the entry point.
4469 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004470 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004471
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004472 shouldEmitTableModule |= shouldEmitTable;
4473 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendling163ba3f2009-03-10 21:23:25 +00004474
4475 if (TimePassesIsEnabled)
4476 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004477 }
4478
4479 /// EndFunction - Gather and emit post-function exception information.
4480 ///
4481 void EndFunction() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004482 if (TimePassesIsEnabled)
4483 ExceptionTimer->startTimer();
4484
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004485 if (shouldEmitMoves || shouldEmitTable) {
4486 EmitLabel("eh_func_end", SubprogramCount);
4487 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004488
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004489 // Save EH frame information
Bill Wendling7d16e852009-04-10 00:12:49 +00004490 std::string Name;
4491 EHFrames.push_back(
4492 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF, Name),
4493 SubprogramCount,
4494 MMI->getPersonalityIndex(),
4495 MF->getFrameInfo()->hasCalls(),
4496 !MMI->getLandingPads().empty(),
4497 MMI->getFrameMoves(),
4498 MF->getFunction()));
Bill Wendling163ba3f2009-03-10 21:23:25 +00004499 }
4500
4501 if (TimePassesIsEnabled)
4502 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004503 }
4504};
4505
Jim Laskey0d086af2006-02-27 12:43:29 +00004506} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004507
4508//===----------------------------------------------------------------------===//
4509
Jim Laskeyd18e2892006-01-20 20:34:06 +00004510/// Emit - Print the abbreviation using the specified Dwarf writer.
4511///
Jim Laskey072200c2007-01-29 18:51:14 +00004512void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004513 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004514 DD.getAsm()->EmitULEB128Bytes(Tag);
4515 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004516
Jim Laskeyd18e2892006-01-20 20:34:06 +00004517 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004518 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4519 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004520
Jim Laskeyd18e2892006-01-20 20:34:06 +00004521 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004522 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004523 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004524
Jim Laskeyd18e2892006-01-20 20:34:06 +00004525 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004526 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4527 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004528
Jim Laskeyd18e2892006-01-20 20:34:06 +00004529 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004530 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4531 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004532 }
4533
4534 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004535 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4536 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004537}
4538
4539#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004540void DIEAbbrev::print(std::ostream &O) {
4541 O << "Abbreviation @"
4542 << std::hex << (intptr_t)this << std::dec
4543 << " "
4544 << TagString(Tag)
4545 << " "
4546 << ChildrenString(ChildrenFlag)
4547 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004548
Jim Laskeya0f3d172006-09-07 22:06:40 +00004549 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4550 O << " "
4551 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004552 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004553 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004554 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004555 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004556}
Bill Wendlinge8156192006-12-07 01:30:32 +00004557void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004558#endif
4559
4560//===----------------------------------------------------------------------===//
4561
Jim Laskeyef42a012006-11-02 20:12:39 +00004562#ifndef NDEBUG
4563void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004564 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004565}
Jim Laskeyef42a012006-11-02 20:12:39 +00004566#endif
4567
4568//===----------------------------------------------------------------------===//
4569
Jim Laskey063e7652006-01-17 17:31:53 +00004570/// EmitValue - Emit integer of appropriate size.
4571///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004572void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004573 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004574 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004575 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004576 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004577 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004578 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004579 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004580 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004581 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004582 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4583 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4584 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004585 default: assert(0 && "DIE Value form not supported yet"); break;
4586 }
4587}
4588
4589/// SizeOf - Determine size of integer value in bytes.
4590///
Jim Laskey072200c2007-01-29 18:51:14 +00004591unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004592 switch (Form) {
4593 case DW_FORM_flag: // Fall thru
4594 case DW_FORM_ref1: // Fall thru
4595 case DW_FORM_data1: return sizeof(int8_t);
4596 case DW_FORM_ref2: // Fall thru
4597 case DW_FORM_data2: return sizeof(int16_t);
4598 case DW_FORM_ref4: // Fall thru
4599 case DW_FORM_data4: return sizeof(int32_t);
4600 case DW_FORM_ref8: // Fall thru
4601 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004602 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4603 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004604 default: assert(0 && "DIE Value form not supported yet"); break;
4605 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004606 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004607}
4608
Jim Laskey063e7652006-01-17 17:31:53 +00004609//===----------------------------------------------------------------------===//
4610
4611/// EmitValue - Emit string value.
4612///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004613void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Bill Wendlingdd446322009-03-10 23:57:09 +00004614 DD.getAsm()->EmitString(Str);
Jim Laskey063e7652006-01-17 17:31:53 +00004615}
4616
Jim Laskey063e7652006-01-17 17:31:53 +00004617//===----------------------------------------------------------------------===//
4618
4619/// EmitValue - Emit label value.
4620///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004621void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004622 bool IsSmall = Form == DW_FORM_data4;
4623 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004624}
4625
4626/// SizeOf - Determine size of label value in bytes.
4627///
Jim Laskey072200c2007-01-29 18:51:14 +00004628unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004629 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004630 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004631}
Jim Laskeyef42a012006-11-02 20:12:39 +00004632
Jim Laskey063e7652006-01-17 17:31:53 +00004633//===----------------------------------------------------------------------===//
4634
Jim Laskeyd18e2892006-01-20 20:34:06 +00004635/// EmitValue - Emit label value.
4636///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004637void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004638 bool IsSmall = Form == DW_FORM_data4;
4639 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004640}
4641
4642/// SizeOf - Determine size of label value in bytes.
4643///
Jim Laskey072200c2007-01-29 18:51:14 +00004644unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004645 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004646 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004647}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004648
Jim Laskeyd18e2892006-01-20 20:34:06 +00004649//===----------------------------------------------------------------------===//
4650
Jim Laskey063e7652006-01-17 17:31:53 +00004651/// EmitValue - Emit delta value.
4652///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004653void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4654 bool IsSmall = Form == DW_FORM_data4;
4655 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4656 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4657}
4658
4659/// SizeOf - Determine size of delta value in bytes.
4660///
4661unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4662 if (Form == DW_FORM_data4) return 4;
4663 return DD.getTargetData()->getPointerSize();
4664}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004665
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004666//===----------------------------------------------------------------------===//
4667
4668/// EmitValue - Emit delta value.
4669///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004670void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004671 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004672 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004673}
4674
4675/// SizeOf - Determine size of delta value in bytes.
4676///
Jim Laskey072200c2007-01-29 18:51:14 +00004677unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004678 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004679 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004680}
4681
4682//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004683
Jim Laskeyb8509c52006-03-23 18:07:55 +00004684/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004685///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004686void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004687 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004688}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004689
Jim Laskeyd18e2892006-01-20 20:34:06 +00004690//===----------------------------------------------------------------------===//
4691
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004692/// ComputeSize - calculate the size of the block.
4693///
Jim Laskey072200c2007-01-29 18:51:14 +00004694unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004695 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004696 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004697
Jim Laskeyef42a012006-11-02 20:12:39 +00004698 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004699 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004700 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004701 }
4702 return Size;
4703}
4704
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004705/// EmitValue - Emit block data.
4706///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004707void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004708 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004709 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4710 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4711 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4712 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004713 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004714 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004715
Owen Anderson873e1b52008-06-24 21:44:59 +00004716 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004717
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004718 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004719 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004720 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004721 }
4722}
4723
4724/// SizeOf - Determine size of block data in bytes.
4725///
Jim Laskey072200c2007-01-29 18:51:14 +00004726unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004727 switch (Form) {
4728 case DW_FORM_block1: return Size + sizeof(int8_t);
4729 case DW_FORM_block2: return Size + sizeof(int16_t);
4730 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004731 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004732 default: assert(0 && "Improper form for block"); break;
4733 }
4734 return 0;
4735}
4736
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004737//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004738/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004739
4740DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004741 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004742 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004743}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004744
Jim Laskeyb8509c52006-03-23 18:07:55 +00004745/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4746///
4747void DIE::AddSiblingOffset() {
4748 DIEInteger *DI = new DIEInteger(0);
4749 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004750 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004751}
4752
Jim Laskeyef42a012006-11-02 20:12:39 +00004753/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004754///
Jim Laskeyef42a012006-11-02 20:12:39 +00004755void DIE::Profile(FoldingSetNodeID &ID) {
4756 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004757
Jim Laskeyef42a012006-11-02 20:12:39 +00004758 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4759 ID.AddPointer(Children[i]);
4760
4761 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4762 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004763}
Jim Laskeyef42a012006-11-02 20:12:39 +00004764
4765#ifndef NDEBUG
4766void DIE::print(std::ostream &O, unsigned IncIndent) {
4767 static unsigned IndentCount = 0;
4768 IndentCount += IncIndent;
4769 const std::string Indent(IndentCount, ' ');
4770 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004771
Jim Laskeyef42a012006-11-02 20:12:39 +00004772 if (!isBlock) {
4773 O << Indent
4774 << "Die: "
4775 << "0x" << std::hex << (intptr_t)this << std::dec
4776 << ", Offset: " << Offset
4777 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004778 << "\n";
4779
Jim Laskeyef42a012006-11-02 20:12:39 +00004780 O << Indent
4781 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004782 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004783 << ChildrenString(Abbrev.getChildrenFlag());
4784 } else {
4785 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004786 }
4787 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004788
Owen Anderson873e1b52008-06-24 21:44:59 +00004789 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004790
Jim Laskeyef42a012006-11-02 20:12:39 +00004791 IndentCount += 2;
4792 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4793 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004794
4795 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004796 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004797 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004798 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004799
Jim Laskeyef42a012006-11-02 20:12:39 +00004800 O << " "
4801 << FormEncodingString(Data[i].getForm())
4802 << " ";
4803 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004804 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004805 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004806 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004807
Jim Laskeyef42a012006-11-02 20:12:39 +00004808 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4809 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004810 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004811
Jim Laskeyef42a012006-11-02 20:12:39 +00004812 if (!isBlock) O << "\n";
4813 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004814}
4815
Jim Laskeyef42a012006-11-02 20:12:39 +00004816void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004817 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004818}
Jim Laskeybd761842006-02-27 17:27:12 +00004819#endif
Jim Laskey65195462006-10-30 13:35:07 +00004820
4821//===----------------------------------------------------------------------===//
4822/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004823///
Jim Laskey65195462006-10-30 13:35:07 +00004824
Bill Wendling91b8b802009-03-10 20:41:52 +00004825DwarfWriter::DwarfWriter()
Bill Wendling163ba3f2009-03-10 21:23:25 +00004826 : ImmutablePass(&ID), DD(0), DE(0) {}
Jim Laskey65195462006-10-30 13:35:07 +00004827
4828DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004829 delete DE;
4830 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004831}
4832
Jim Laskey65195462006-10-30 13:35:07 +00004833/// BeginModule - Emit all Dwarf sections that should come prior to the
4834/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004835void DwarfWriter::BeginModule(Module *M,
4836 MachineModuleInfo *MMI,
4837 raw_ostream &OS, AsmPrinter *A,
4838 const TargetAsmInfo *T) {
4839 DE = new DwarfException(OS, A, T);
4840 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004841 DE->BeginModule(M);
4842 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00004843 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00004844 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004845}
4846
4847/// EndModule - Emit all Dwarf sections that should come after the content.
4848///
4849void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004850 DE->EndModule();
4851 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004852}
4853
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004854/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004855/// emitted immediately after the function entry point.
4856void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004857 DE->BeginFunction(MF);
4858 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004859}
4860
4861/// EndFunction - Gather and emit post-function debug information.
4862///
Bill Wendlingd751c642008-09-26 00:28:12 +00004863void DwarfWriter::EndFunction(MachineFunction *MF) {
4864 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004865 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004866
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004867 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004868 // Clear function debug information.
4869 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004870}
Devang Patelccca7fe2009-01-12 19:17:34 +00004871
4872/// RecordSourceLine - Records location information and associates it with a
4873/// label. Returns a unique label ID used to generate a label and provide
4874/// correspondence to the source line list.
4875unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00004876 DICompileUnit CU) {
4877 return DD->RecordSourceLine(Line, Col, CU);
Devang Patelccca7fe2009-01-12 19:17:34 +00004878}
4879
4880/// RecordRegionStart - Indicate the start of a region.
4881unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004882 return DD->RecordRegionStart(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004883}
4884
4885/// RecordRegionEnd - Indicate the end of a region.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00004886unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
4887 return DD->RecordRegionEnd(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004888}
4889
4890/// getRecordSourceLineCount - Count source lines.
4891unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004892 return DD->getRecordSourceLineCount();
Devang Patelccca7fe2009-01-12 19:17:34 +00004893}
Devang Patelbb8c5952009-01-13 21:25:00 +00004894
Devang Patelc48c5502009-01-13 21:44:10 +00004895/// RecordVariable - Indicate the declaration of a local variable.
4896///
Devang Patel1be3ecc2009-04-15 00:10:26 +00004897void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
4898 const MachineInstr *MI) {
4899 DD->RecordVariable(GV, FrameIndex, MI);
Devang Patelc48c5502009-01-13 21:44:10 +00004900}
Devang Patelbbdc8202009-01-13 23:54:55 +00004901
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004902/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4903/// be emitted.
4904bool DwarfWriter::ShouldEmitDwarfDebug() const {
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +00004905 return DD && DD->ShouldEmitDwarfDebug();
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004906}
Devang Patel0f7fef32009-04-13 17:02:03 +00004907
Devang Patel1be3ecc2009-04-15 00:10:26 +00004908//// RecordInlinedFnStart - Global variable GV is inlined at the location marked
Devang Patel0f7fef32009-04-13 17:02:03 +00004909//// by LabelID label.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00004910unsigned DwarfWriter::RecordInlinedFnStart(DISubprogram SP, DICompileUnit CU,
4911 unsigned Line, unsigned Col) {
4912 return DD->RecordInlinedFnStart(SP, CU, Line, Col);
Devang Patel0f7fef32009-04-13 17:02:03 +00004913}
4914
Devang Patel1be3ecc2009-04-15 00:10:26 +00004915/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00004916unsigned DwarfWriter::RecordInlinedFnEnd(DISubprogram SP) {
Devang Patel1be3ecc2009-04-15 00:10:26 +00004917 return DD->RecordInlinedFnEnd(SP);
4918}
4919
4920/// RecordVariableScope - Record scope for the variable declared by
4921/// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
4922void DwarfWriter::RecordVariableScope(DIVariable &DV,
4923 const MachineInstr *DeclareMI) {
4924 DD->RecordVariableScope(DV, DeclareMI);
4925}