blob: b64f14e11be49f9244a85754baf26c58ddab63db [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 Wendlinga5c8a4e2009-05-10 23:14:38 +00001289 /// DbgConcreteScopeMap - Tracks inlined scopes in the current function.
1290 DenseMap<GlobalVariable *,
1291 SmallVector<DbgConcreteScope *, 8> > DbgConcreteScopeMap;
Devang Patel1be3ecc2009-04-15 00:10:26 +00001292
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001293 /// InlineInfo - Keep track of inlined functions and their location. This
1294 /// information is used to populate debug_inlined section.
Devang Patel0f7fef32009-04-13 17:02:03 +00001295 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
1296
Devang Patel1be3ecc2009-04-15 00:10:26 +00001297 /// InlinedVariableScopes - Scopes information for the inlined subroutine
1298 /// variables.
1299 DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
1300
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001301 /// AbstractInstanceRootMap - Map of abstract instance roots of inlined
1302 /// functions. These are subroutine entries that contain a DW_AT_inline
1303 /// attribute.
1304 DenseMap<const GlobalVariable *, DbgScope *> AbstractInstanceRootMap;
1305
1306 /// AbstractInstanceRootList - List of abstract instance roots of inlined
1307 /// functions. These are subroutine entries that contain a DW_AT_inline
1308 /// attribute.
1309 SmallVector<DbgScope *, 32> AbstractInstanceRootList;
1310
1311 /// LexicalScopeStack - A stack of lexical scopes. The top one is the current
1312 /// scope.
1313 SmallVector<DbgScope *, 16> LexicalScopeStack;
1314
Bill Wendlinge688faf2009-05-08 21:03:15 +00001315 /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
1316 /// used when calculating the "origin" of a concrete instance of an inlined
1317 /// function.
1318 DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
1319
Bill Wendling163ba3f2009-03-10 21:23:25 +00001320 /// DebugTimer - Timer for the Dwarf debug writer.
1321 Timer *DebugTimer;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001322
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001323 struct FunctionDebugFrameInfo {
1324 unsigned Number;
1325 std::vector<MachineMove> Moves;
1326
1327 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001328 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001329 };
1330
1331 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001332
Bill Wendling163ba3f2009-03-10 21:23:25 +00001333private:
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001334 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendlingc8615e42009-03-10 21:47:45 +00001335 /// maps to the source id. Source id starts at 1.
1336 std::pair<unsigned, unsigned>
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001337 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendlingc8615e42009-03-10 21:47:45 +00001338 return SourceIds[SId-1];
1339 }
1340
1341 /// getNumSourceDirectories - Return the number of source directories in the
1342 /// debug info.
1343 unsigned getNumSourceDirectories() const {
1344 return DirectoryNames.size();
1345 }
1346
1347 /// getSourceDirectoryName - Return the name of the directory corresponding
1348 /// to the id.
1349 const std::string &getSourceDirectoryName(unsigned Id) const {
1350 return DirectoryNames[Id - 1];
1351 }
1352
1353 /// getSourceFileName - Return the name of the source file corresponding
1354 /// to the id.
1355 const std::string &getSourceFileName(unsigned Id) const {
1356 return SourceFileNames[Id - 1];
1357 }
1358
1359 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendlingc8615e42009-03-10 21:47:45 +00001360 unsigned getNumSourceIds() const {
1361 return SourceIds.size();
1362 }
1363
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001364 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001365 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001366 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1367 // Profile the node so that we can make it unique.
1368 FoldingSetNodeID ID;
1369 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001370
Jim Laskeyef42a012006-11-02 20:12:39 +00001371 // Check the set for priors.
1372 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001373
Jim Laskeyef42a012006-11-02 20:12:39 +00001374 // If it's newly added.
1375 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001376 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001377 Abbreviations.push_back(&Abbrev);
1378 // Assign the vector position + 1 as its number.
1379 Abbrev.setNumber(Abbreviations.size());
1380 } else {
1381 // Assign existing abbreviation number.
1382 Abbrev.setNumber(InSet->getNumber());
1383 }
1384 }
1385
Jim Laskey65195462006-10-30 13:35:07 +00001386 /// NewString - Add a string to the constant pool and returns a label.
1387 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001388 DWLabel NewString(const std::string &String) {
1389 unsigned StringID = StringPool.insert(String);
1390 return DWLabel("string", StringID);
1391 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001392
Jim Laskeyef42a012006-11-02 20:12:39 +00001393 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1394 /// entry.
1395 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1396 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001397
Jim Laskeyef42a012006-11-02 20:12:39 +00001398 if (Entry) {
1399 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001400 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001401 void *Where;
1402 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001403
Jim Laskeyf6733882006-11-02 21:48:18 +00001404 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001405
Jim Laskeyef42a012006-11-02 20:12:39 +00001406 Value = new DIEntry(Entry);
1407 ValuesSet.InsertNode(Value, Where);
1408 } else {
1409 Value = new DIEntry(Entry);
1410 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001411
Jim Laskeyef42a012006-11-02 20:12:39 +00001412 Values.push_back(Value);
1413 return Value;
1414 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001415
Jim Laskeyef42a012006-11-02 20:12:39 +00001416 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1417 ///
1418 void SetDIEntry(DIEntry *Value, DIE *Entry) {
Bill Wendlingdd446322009-03-10 23:57:09 +00001419 Value->setEntry(Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001420 // Add to values set if not already there. If it is, we merely have a
1421 // duplicate in the values list (no harm.)
1422 ValuesSet.GetOrInsertNode(Value);
1423 }
1424
1425 /// AddUInt - Add an unsigned integer attribute data and value.
1426 ///
1427 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1428 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1429
1430 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001431 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001432 void *Where;
1433 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1434 if (!Value) {
1435 Value = new DIEInteger(Integer);
1436 ValuesSet.InsertNode(Value, Where);
1437 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001438 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001439
Jim Laskeyef42a012006-11-02 20:12:39 +00001440 Die->AddValue(Attribute, Form, Value);
1441 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001442
Jim Laskeyef42a012006-11-02 20:12:39 +00001443 /// AddSInt - Add an signed integer attribute data and value.
1444 ///
1445 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1446 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1447
1448 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001449 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001450 void *Where;
1451 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1452 if (!Value) {
1453 Value = new DIEInteger(Integer);
1454 ValuesSet.InsertNode(Value, Where);
1455 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001456 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001457
Jim Laskeyef42a012006-11-02 20:12:39 +00001458 Die->AddValue(Attribute, Form, Value);
1459 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001460
Evan Chenge3d42322009-02-25 07:04:34 +00001461 /// AddString - Add a string attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +00001462 ///
1463 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1464 const std::string &String) {
1465 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001466 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001467 void *Where;
1468 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1469 if (!Value) {
1470 Value = new DIEString(String);
1471 ValuesSet.InsertNode(Value, Where);
1472 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001473 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001474
Jim Laskeyef42a012006-11-02 20:12:39 +00001475 Die->AddValue(Attribute, Form, Value);
1476 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001477
Jim Laskeyef42a012006-11-02 20:12:39 +00001478 /// AddLabel - Add a Dwarf label attribute data and value.
1479 ///
1480 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1481 const DWLabel &Label) {
1482 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001483 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001484 void *Where;
1485 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1486 if (!Value) {
1487 Value = new DIEDwarfLabel(Label);
1488 ValuesSet.InsertNode(Value, Where);
1489 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001490 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001491
Jim Laskeyef42a012006-11-02 20:12:39 +00001492 Die->AddValue(Attribute, Form, Value);
1493 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001494
Jim Laskeyef42a012006-11-02 20:12:39 +00001495 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1496 ///
1497 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1498 const std::string &Label) {
1499 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001500 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001501 void *Where;
1502 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1503 if (!Value) {
1504 Value = new DIEObjectLabel(Label);
1505 ValuesSet.InsertNode(Value, Where);
1506 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001507 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001508
Jim Laskeyef42a012006-11-02 20:12:39 +00001509 Die->AddValue(Attribute, Form, Value);
1510 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001511
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001512 /// AddSectionOffset - Add a section offset label attribute data and value.
1513 ///
1514 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1515 const DWLabel &Label, const DWLabel &Section,
1516 bool isEH = false, bool useSet = true) {
1517 FoldingSetNodeID ID;
1518 DIESectionOffset::Profile(ID, Label, Section);
1519 void *Where;
1520 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1521 if (!Value) {
1522 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1523 ValuesSet.InsertNode(Value, Where);
1524 Values.push_back(Value);
1525 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001526
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001527 Die->AddValue(Attribute, Form, Value);
1528 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001529
Jim Laskeyef42a012006-11-02 20:12:39 +00001530 /// AddDelta - Add a label delta attribute data and value.
1531 ///
1532 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patel1be3ecc2009-04-15 00:10:26 +00001533 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001534 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001535 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001536 void *Where;
1537 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1538 if (!Value) {
1539 Value = new DIEDelta(Hi, Lo);
1540 ValuesSet.InsertNode(Value, Where);
1541 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001542 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001543
Jim Laskeyef42a012006-11-02 20:12:39 +00001544 Die->AddValue(Attribute, Form, Value);
1545 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001546
Jim Laskeyef42a012006-11-02 20:12:39 +00001547 /// AddDIEntry - Add a DIE attribute data and value.
1548 ///
1549 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1550 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1551 }
1552
1553 /// AddBlock - Add block data.
1554 ///
1555 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1556 Block->ComputeSize(*this);
1557 FoldingSetNodeID ID;
1558 Block->Profile(ID);
1559 void *Where;
1560 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1561 if (!Value) {
1562 Value = Block;
1563 ValuesSet.InsertNode(Value, Where);
1564 Values.push_back(Value);
1565 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001566 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001567 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001568 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001569 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001570
Jim Laskeyef42a012006-11-02 20:12:39 +00001571 Die->AddValue(Attribute, Block->BestForm(), Value);
1572 }
1573
Jim Laskey65195462006-10-30 13:35:07 +00001574 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001575 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001576 void AddSourceLine(DIE *Die, const DIVariable *V) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001577 // If there is no compile unit specified, don't add a line #.
1578 if (V->getCompileUnit().isNull())
1579 return;
1580
Devang Patel7a6e5a32009-01-08 02:33:41 +00001581 unsigned Line = V->getLineNumber();
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001582 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
1583 assert(FileID && "Invalid file id");
Devang Patel7a6e5a32009-01-08 02:33:41 +00001584 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1585 AddUInt(Die, DW_AT_decl_line, 0, Line);
1586 }
1587
1588 /// AddSourceLine - Add location information to specified debug information
1589 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001590 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001591 // If there is no compile unit specified, don't add a line #.
1592 if (G->getCompileUnit().isNull())
1593 return;
Devang Patel8526cc02009-01-05 22:35:52 +00001594 unsigned Line = G->getLineNumber();
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001595 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
1596 assert(FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001597 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1598 AddUInt(Die, DW_AT_decl_line, 0, Line);
1599 }
1600
Devang Patel99ec3532009-01-16 19:28:14 +00001601 void AddSourceLine(DIE *Die, const DIType *Ty) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001602 // If there is no compile unit specified, don't add a line #.
Devang Pateldd9db662009-01-30 18:20:31 +00001603 DICompileUnit CU = Ty->getCompileUnit();
1604 if (CU.isNull())
1605 return;
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001606
1607 unsigned Line = Ty->getLineNumber();
1608 unsigned FileID = FindCompileUnit(CU).getID();
1609 assert(FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001610 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1611 AddUInt(Die, DW_AT_decl_line, 0, Line);
1612 }
1613
Jim Laskey65195462006-10-30 13:35:07 +00001614 /// AddAddress - Add an address attribute to a die based on the location
1615 /// provided.
1616 void AddAddress(DIE *Die, unsigned Attribute,
Devang Patel1be3ecc2009-04-15 00:10:26 +00001617 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001618 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001619 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001620
Dan Gohmand735b802008-10-03 15:45:36 +00001621 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001622 if (Reg < 32) {
1623 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1624 } else {
1625 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1626 AddUInt(Block, 0, DW_FORM_udata, Reg);
1627 }
1628 } else {
1629 if (Reg < 32) {
1630 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1631 } else {
1632 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1633 AddUInt(Block, 0, DW_FORM_udata, Reg);
1634 }
1635 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1636 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001637
Jim Laskeyef42a012006-11-02 20:12:39 +00001638 AddBlock(Die, Attribute, 0, Block);
1639 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001640
Jim Laskeyef42a012006-11-02 20:12:39 +00001641 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +00001642 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel80303aa2009-01-23 19:13:31 +00001643 if (Ty.isNull())
Devang Patel48d190f2009-01-05 21:47:57 +00001644 return;
Devang Patel48d190f2009-01-05 21:47:57 +00001645
1646 // Check for pre-existence.
1647 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1648 // If it exists then use the existing value.
1649 if (Slot) {
1650 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1651 return;
1652 }
1653
1654 // Set up proxy.
1655 Slot = NewDIEntry();
1656
1657 // Construct type.
1658 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001659 if (Ty.isBasicType(Ty.getTag()))
1660 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1661 else if (Ty.isDerivedType(Ty.getTag()))
1662 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1663 else {
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001664 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelf193ff02009-01-15 19:26:23 +00001665 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1666 }
1667
Devang Patel7009d242009-01-27 23:22:55 +00001668 // Add debug information entry to entity and appropriate context.
1669 DIE *Die = NULL;
1670 DIDescriptor Context = Ty.getContext();
1671 if (!Context.isNull())
1672 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1673
1674 if (Die) {
1675 DIE *Child = new DIE(Buffer);
1676 Die->AddChild(Child);
1677 Buffer.Detach();
1678 SetDIEntry(Slot, Child);
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001679 } else {
Devang Patel7009d242009-01-27 23:22:55 +00001680 Die = DW_Unit->AddDie(Buffer);
1681 SetDIEntry(Slot, Die);
1682 }
1683
Devang Patel48d190f2009-01-05 21:47:57 +00001684 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1685 }
1686
Devang Patele5202732009-01-05 19:07:53 +00001687 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1688 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001689 DIBasicType BTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001690
Devang Patel08f053f2009-01-05 17:57:47 +00001691 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001692 std::string Name;
1693 BTy.getName(Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001694 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001695 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +00001696 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001697 if (!Name.empty())
Devang Patel08f053f2009-01-05 17:57:47 +00001698 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001699 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +00001700 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1701 }
1702
Devang Patele5202732009-01-05 19:07:53 +00001703 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1704 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001705 DIDerivedType DTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001706
Devang Patel08f053f2009-01-05 17:57:47 +00001707 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001708 std::string Name;
1709 DTy.getName(Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001710 uint64_t Size = DTy.getSizeInBits() >> 3;
1711 unsigned Tag = DTy.getTag();
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001712
Devang Patel08f053f2009-01-05 17:57:47 +00001713 // FIXME - Workaround for templates.
1714 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1715
1716 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001717
Devang Patel08f053f2009-01-05 17:57:47 +00001718 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +00001719 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001720 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001721
1722 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001723 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001724 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001725
1726 // Add size if non-zero (derived types might be zero-sized.)
1727 if (Size)
1728 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1729
1730 // Add source line info if available and TyDesc is not a forward
1731 // declaration.
Devang Patelad165be2009-01-27 00:45:04 +00001732 if (!DTy.isForwardDecl())
1733 AddSourceLine(&Buffer, &DTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001734 }
1735
Devang Patelf4215332009-01-05 19:55:51 +00001736 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1737 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001738 DICompositeType CTy) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001739 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001740 std::string Name;
1741 CTy.getName(Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001742
Devang Patelf193ff02009-01-15 19:26:23 +00001743 uint64_t Size = CTy.getSizeInBits() >> 3;
1744 unsigned Tag = CTy.getTag();
Devang Patel49f38cb2009-01-23 01:19:09 +00001745 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001746
Devang Patelf4215332009-01-05 19:55:51 +00001747 switch (Tag) {
1748 case DW_TAG_vector_type:
1749 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001750 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001751 break;
Devang Pateleab4a2e2009-01-20 18:35:14 +00001752 case DW_TAG_enumeration_type:
1753 {
1754 DIArray Elements = CTy.getTypeArray();
1755 // Add enumerators to enumeration type.
1756 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1757 DIE *ElemDie = NULL;
1758 DIEnumerator Enum(Elements.getElement(i).getGV());
1759 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1760 Buffer.AddChild(ElemDie);
1761 }
1762 }
1763 break;
Devang Patelf4215332009-01-05 19:55:51 +00001764 case DW_TAG_subroutine_type:
1765 {
Devang Patelf4215332009-01-05 19:55:51 +00001766 // Add return type.
Bill Wendling3f500d92009-05-06 21:21:34 +00001767 DIArray Elements = CTy.getTypeArray();
Devang Patel48d190f2009-01-05 21:47:57 +00001768 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +00001769 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +00001770
Bill Wendling3f500d92009-05-06 21:21:34 +00001771 // Add prototype flag.
1772 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1773
Devang Patelf4215332009-01-05 19:55:51 +00001774 // Add arguments.
1775 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1776 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001777 DIDescriptor Ty = Elements.getElement(i);
Devang Patel7ab24502009-01-17 06:57:25 +00001778 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +00001779 Buffer.AddChild(Arg);
1780 }
1781 }
1782 break;
1783 case DW_TAG_structure_type:
1784 case DW_TAG_union_type:
Devang Patel86bda412009-03-25 00:28:40 +00001785 case DW_TAG_class_type:
Devang Patelf4215332009-01-05 19:55:51 +00001786 {
1787 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +00001788 DIArray Elements = CTy.getTypeArray();
Devang Patel153745c2009-01-16 00:50:53 +00001789
1790 // A forward struct declared type may not have elements available.
1791 if (Elements.isNull())
1792 break;
1793
Devang Patelf4215332009-01-05 19:55:51 +00001794 // Add elements to structure type.
1795 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1796 DIDescriptor Element = Elements.getElement(i);
Devang Patel5aac3d32009-01-17 08:01:33 +00001797 DIE *ElemDie = NULL;
Devang Patelf193ff02009-01-15 19:26:23 +00001798 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel2d1768c2009-01-17 08:05:14 +00001799 ElemDie = CreateSubprogramDIE(DW_Unit,
1800 DISubprogram(Element.getGV()));
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001801 else if (Element.getTag() == dwarf::DW_TAG_variable) // ??
Devang Patel5aac3d32009-01-17 08:01:33 +00001802 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1803 DIGlobalVariable(Element.getGV()));
Devang Patel2be58932009-01-20 21:02:02 +00001804 else
1805 ElemDie = CreateMemberDIE(DW_Unit,
1806 DIDerivedType(Element.getGV()));
Devang Patel2d1768c2009-01-17 08:05:14 +00001807 Buffer.AddChild(ElemDie);
Devang Patelf4215332009-01-05 19:55:51 +00001808 }
Devang Patel13319ce2009-02-17 22:43:44 +00001809 unsigned RLang = CTy.getRunTimeLang();
1810 if (RLang)
1811 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patelf4215332009-01-05 19:55:51 +00001812 }
1813 break;
1814 default:
1815 break;
1816 }
1817
1818 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001819 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001820 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf4215332009-01-05 19:55:51 +00001821
Devang Patelad165be2009-01-27 00:45:04 +00001822 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1823 || Tag == DW_TAG_union_type) {
1824 // Add size if non-zero (derived types might be zero-sized.)
1825 if (Size)
1826 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1827 else {
1828 // Add zero size if it is not a forward declaration.
1829 if (CTy.isForwardDecl())
1830 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1831 else
1832 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1833 }
1834
1835 // Add source line info if available.
1836 if (!CTy.isForwardDecl())
1837 AddSourceLine(&Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001838 }
Devang Patelf4215332009-01-05 19:55:51 +00001839 }
1840
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001841 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1842 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelf193ff02009-01-15 19:26:23 +00001843 int64_t L = SR.getLo();
1844 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +00001845 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1846 if (L != H) {
1847 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1848 if (L)
Devang Patel2d1768c2009-01-17 08:05:14 +00001849 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1850 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel68afdc32009-01-05 18:33:01 +00001851 }
1852 Buffer.AddChild(DW_Subrange);
1853 }
1854
1855 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1856 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1857 DICompositeType *CTy) {
1858 Buffer.setTag(DW_TAG_array_type);
1859 if (CTy->getTag() == DW_TAG_vector_type)
1860 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1861
Devang Patelf9235742009-01-28 21:08:20 +00001862 // Emit derived type.
1863 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001864 DIArray Elements = CTy->getTypeArray();
Devang Patel68afdc32009-01-05 18:33:01 +00001865
1866 // Construct an anonymous type for index type.
1867 DIE IdxBuffer(DW_TAG_base_type);
1868 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1869 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1870 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1871
1872 // Add subranges to array type.
1873 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001874 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001875 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1876 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001877 }
1878 }
1879
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001880 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Pateleab4a2e2009-01-20 18:35:14 +00001881 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001882
1883 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendling0582ae92009-03-13 04:39:26 +00001884 std::string Name;
1885 ETy->getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001886 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patelc69bf2c2009-01-05 18:38:38 +00001887 int64_t Value = ETy->getEnumValue();
1888 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Pateleab4a2e2009-01-20 18:35:14 +00001889 return Enumerator;
Devang Patelc69bf2c2009-01-05 18:38:38 +00001890 }
Devang Patel68afdc32009-01-05 18:33:01 +00001891
Devang Patel5aac3d32009-01-17 08:01:33 +00001892 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001893 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patel5aac3d32009-01-17 08:01:33 +00001894 {
1895 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendling0582ae92009-03-13 04:39:26 +00001896 std::string Name;
1897 GV.getDisplayName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001898 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling0582ae92009-03-13 04:39:26 +00001899 std::string LinkageName;
1900 GV.getLinkageName(LinkageName);
1901 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001902 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1903 AddType(DW_Unit, GVDie, GV.getType());
1904 if (!GV.isLocalToUnit())
1905 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1906 AddSourceLine(GVDie, &GV);
1907 return GVDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001908 }
1909
Devang Patel2be58932009-01-20 21:02:02 +00001910 /// CreateMemberDIE - Create new member DIE.
1911 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1912 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendling0582ae92009-03-13 04:39:26 +00001913 std::string Name;
1914 DT.getName(Name);
1915 if (!Name.empty())
Devang Patel2be58932009-01-20 21:02:02 +00001916 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1917
1918 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1919
1920 AddSourceLine(MemberDie, &DT);
1921
Devang Patel36375ee2009-02-17 21:23:59 +00001922 uint64_t Size = DT.getSizeInBits();
1923 uint64_t FieldSize = DT.getOriginalTypeSize();
1924
1925 if (Size != FieldSize) {
1926 // Handle bitfield.
1927 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1928 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1929
1930 uint64_t Offset = DT.getOffsetInBits();
1931 uint64_t FieldOffset = Offset;
1932 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1933 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1934 FieldOffset = (HiMark - FieldSize);
1935 Offset -= FieldOffset;
1936 // Maybe we need to work from the other end.
1937 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1938 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1939 }
Devang Patel2be58932009-01-20 21:02:02 +00001940 DIEBlock *Block = new DIEBlock();
1941 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1942 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1943 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1944
Devang Patel47661592009-01-21 00:08:04 +00001945 if (DT.isProtected())
1946 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1947 else if (DT.isPrivate())
1948 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1949
Devang Patel2be58932009-01-20 21:02:02 +00001950 return MemberDie;
1951 }
1952
Devang Patel5aac3d32009-01-17 08:01:33 +00001953 /// CreateSubprogramDIE - Create new DIE using SP.
1954 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Bill Wendling3f500d92009-05-06 21:21:34 +00001955 const DISubprogram &SP,
Devang Patel2d1768c2009-01-17 08:05:14 +00001956 bool IsConstructor = false) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001957 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling3f500d92009-05-06 21:21:34 +00001958
Bill Wendling0582ae92009-03-13 04:39:26 +00001959 std::string Name;
1960 SP.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001961 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling3f500d92009-05-06 21:21:34 +00001962
Bill Wendling0582ae92009-03-13 04:39:26 +00001963 std::string LinkageName;
1964 SP.getLinkageName(LinkageName);
Bill Wendling3f500d92009-05-06 21:21:34 +00001965
Bill Wendling0582ae92009-03-13 04:39:26 +00001966 if (!LinkageName.empty())
Bill Wendling3f500d92009-05-06 21:21:34 +00001967 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1968
Devang Patel5aac3d32009-01-17 08:01:33 +00001969 AddSourceLine(SPDie, &SP);
Devang Patel86ae1422009-01-05 18:59:44 +00001970
Devang Patel5aac3d32009-01-17 08:01:33 +00001971 DICompositeType SPTy = SP.getType();
1972 DIArray Args = SPTy.getTypeArray();
Bill Wendling3f500d92009-05-06 21:21:34 +00001973
1974 // Add prototyped tag, if C or ObjC.
1975 unsigned Lang = SP.getCompileUnit().getLanguage();
1976 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89 || Lang == DW_LANG_ObjC)
1977 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patel5aac3d32009-01-17 08:01:33 +00001978
Devang Patel86ae1422009-01-05 18:59:44 +00001979 // Add Return Type.
Devang Patel75b27382009-04-08 22:18:45 +00001980 unsigned SPTag = SPTy.getTag();
Devang Patel9ac08d62009-02-27 18:05:21 +00001981 if (!IsConstructor) {
Devang Patel75b27382009-04-08 22:18:45 +00001982 if (Args.isNull() || SPTag != DW_TAG_subroutine_type)
Devang Patel9ac08d62009-02-27 18:05:21 +00001983 AddType(DW_Unit, SPDie, SPTy);
1984 else
1985 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1986 }
Devang Pateld234e592009-01-30 01:21:46 +00001987
Devang Pateld5863dd2009-02-02 17:51:41 +00001988 if (!SP.isDefinition()) {
1989 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
Bill Wendling3f500d92009-05-06 21:21:34 +00001990 // Add arguments. Do not add arguments for subprogram definition. They
1991 // will be handled through RecordVariable.
Devang Patel75b27382009-04-08 22:18:45 +00001992 if (SPTag == DW_TAG_subroutine_type)
Devang Pateld5863dd2009-02-02 17:51:41 +00001993 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1994 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1995 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
Bill Wendling3f500d92009-05-06 21:21:34 +00001996 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ??
Devang Pateld5863dd2009-02-02 17:51:41 +00001997 SPDie->AddChild(Arg);
1998 }
1999 }
Devang Pateld234e592009-01-30 01:21:46 +00002000
Devang Patelf193ff02009-01-15 19:26:23 +00002001 if (!SP.isLocalToUnit())
Devang Pateld234e592009-01-30 01:21:46 +00002002 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Bill Wendling3f500d92009-05-06 21:21:34 +00002003
Devang Patel1be3ecc2009-04-15 00:10:26 +00002004 // DW_TAG_inlined_subroutine may refer to this DIE.
2005 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getGV());
2006 Slot = SPDie;
Devang Patel5aac3d32009-01-17 08:01:33 +00002007 return SPDie;
Devang Patel86ae1422009-01-05 18:59:44 +00002008 }
2009
Devang Patel5aac3d32009-01-17 08:01:33 +00002010 /// FindCompileUnit - Get the compile unit for the given descriptor.
2011 ///
Chris Lattnere3f6cea2009-05-05 04:55:56 +00002012 CompileUnit &FindCompileUnit(DICompileUnit Unit) const {
2013 DenseMap<Value *, CompileUnit *>::const_iterator I =
2014 CompileUnitMap.find(Unit.getGV());
2015 assert(I != CompileUnitMap.end() && "Missing compile unit.");
2016 return *I->second;
Devang Patel8526cc02009-01-05 22:35:52 +00002017 }
2018
Devang Patelbbdc8202009-01-13 23:54:55 +00002019 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002020 ///
2021 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
2022 // Get the descriptor.
Devang Patel99ec3532009-01-16 19:28:14 +00002023 const DIVariable &VD = DV->getVariable();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002024
2025 // Translate tag to proper Dwarf tag. The result variable is dropped for
2026 // now.
2027 unsigned Tag;
Devang Patel99ec3532009-01-16 19:28:14 +00002028 switch (VD.getTag()) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00002029 case DW_TAG_return_variable: return NULL;
2030 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2031 case DW_TAG_auto_variable: // fall thru
2032 default: Tag = DW_TAG_variable; break;
2033 }
2034
2035 // Define variable debug information entry.
2036 DIE *VariableDie = new DIE(Tag);
Bill Wendling0582ae92009-03-13 04:39:26 +00002037 std::string Name;
2038 VD.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00002039 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002040
2041 // Add source line info if available.
Devang Patel99ec3532009-01-16 19:28:14 +00002042 AddSourceLine(VariableDie, &VD);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002043
2044 // Add variable type.
Devang Patel99ec3532009-01-16 19:28:14 +00002045 AddType(Unit, VariableDie, VD.getType());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002046
2047 // Add variable address.
2048 MachineLocation Location;
2049 Location.set(RI->getFrameRegister(*MF),
2050 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2051 AddAddress(VariableDie, DW_AT_location, Location);
2052
2053 return VariableDie;
2054 }
2055
Devang Patel7a6e5a32009-01-08 02:33:41 +00002056 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2057 ///
2058 DbgScope *getOrCreateScope(GlobalVariable *V) {
2059 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002060 if (Slot) return Slot;
2061
Devang Patel1be3ecc2009-04-15 00:10:26 +00002062 DbgScope *Parent = NULL;
2063 DIBlock Block(V);
Bill Wendling3f500d92009-05-06 21:21:34 +00002064
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002065 // Don't create a new scope if we already created one for an inlined
2066 // function.
2067 DenseMap<const GlobalVariable *, DbgScope *>::iterator
2068 II = AbstractInstanceRootMap.find(V);
2069 if (II != AbstractInstanceRootMap.end())
2070 return LexicalScopeStack.back();
2071
Devang Patel1be3ecc2009-04-15 00:10:26 +00002072 if (!Block.isNull()) {
2073 DIDescriptor ParentDesc = Block.getContext();
2074 Parent =
2075 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002076 }
Bill Wendling3f500d92009-05-06 21:21:34 +00002077
Devang Patel1be3ecc2009-04-15 00:10:26 +00002078 Slot = new DbgScope(Parent, DIDescriptor(V));
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002079
Devang Patel1be3ecc2009-04-15 00:10:26 +00002080 if (Parent)
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002081 Parent->AddScope(Slot);
Devang Patel1be3ecc2009-04-15 00:10:26 +00002082 else
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002083 // First function is top level function.
Devang Patel7d2f9722009-04-15 20:41:31 +00002084 FunctionDbgScope = Slot;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002085
Devang Patel7a6e5a32009-01-08 02:33:41 +00002086 return Slot;
2087 }
2088
2089 /// ConstructDbgScope - Construct the components of a scope.
2090 ///
2091 void ConstructDbgScope(DbgScope *ParentScope,
2092 unsigned ParentStartID, unsigned ParentEndID,
2093 DIE *ParentDie, CompileUnit *Unit) {
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002094 // Add variables to scope.
2095 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
2096 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2097 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2098 if (VariableDie) ParentDie->AddChild(VariableDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002099 }
2100
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002101 // Add concrete instances to scope.
2102 SmallVector<DbgConcreteScope *, 8> &ConcreteInsts = ParentScope->getConcreteInsts();
2103 for (unsigned i = 0, N = ConcreteInsts.size(); i < N; ++i) {
2104 DbgConcreteScope *ConcreteInst = ConcreteInsts[i];
2105 DIE *Die = ConcreteInst->getDie();
2106
2107 unsigned StartID = ConcreteInst->getStartLabelID();
2108 unsigned EndID = ConcreteInst->getEndLabelID();
2109
2110 // Add the scope bounds.
2111 if (StartID)
2112 AddLabel(Die, DW_AT_low_pc, DW_FORM_addr,
2113 DWLabel("label", StartID));
2114 else
2115 AddLabel(Die, DW_AT_low_pc, DW_FORM_addr,
2116 DWLabel("func_begin", SubprogramCount));
2117
2118 if (EndID)
2119 AddLabel(Die, DW_AT_high_pc, DW_FORM_addr,
2120 DWLabel("label", EndID));
2121 else
2122 AddLabel(Die, DW_AT_high_pc, DW_FORM_addr,
2123 DWLabel("func_end", SubprogramCount));
2124
2125 ParentDie->AddChild(Die);
2126 }
2127
Devang Patel7a6e5a32009-01-08 02:33:41 +00002128 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002129 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002130 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2131 // Define the Scope debug information entry.
2132 DbgScope *Scope = Scopes[j];
Devang Patel7a6e5a32009-01-08 02:33:41 +00002133
Devang Patele9bfb0f2009-01-12 18:41:00 +00002134 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2135 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002136
Devang Patel1be3ecc2009-04-15 00:10:26 +00002137 // Ignore empty scopes.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002138 if (StartID == EndID && StartID != 0) continue;
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002139
2140 // Do not ignore inlined scopes even if they don't have any variables or
2141 // scopes.
2142 if (Scope->getScopes().empty() && Scope->getVariables().empty() &&
2143 Scope->getConcreteInsts().empty())
Devang Patel0f7fef32009-04-13 17:02:03 +00002144 continue;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002145
2146 if (StartID == ParentStartID && EndID == ParentEndID) {
2147 // Just add stuff to the parent scope.
2148 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2149 } else {
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002150 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
Bill Wendling7fa81622009-05-01 08:25:13 +00002151
2152 // Add the scope bounds.
2153 if (StartID)
2154 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2155 DWLabel("label", StartID));
2156 else
2157 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2158 DWLabel("func_begin", SubprogramCount));
2159
2160 if (EndID)
2161 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2162 DWLabel("label", EndID));
2163 else
2164 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2165 DWLabel("func_end", SubprogramCount));
2166
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002167 // Add the scope's contents.
Bill Wendling7fa81622009-05-01 08:25:13 +00002168 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2169 ParentDie->AddChild(ScopeDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002170 }
2171 }
2172 }
2173
Devang Patel7d2f9722009-04-15 20:41:31 +00002174 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002175 ///
Devang Patel7d2f9722009-04-15 20:41:31 +00002176 void ConstructFunctionDbgScope(DbgScope *RootScope) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00002177 // Exit if there is no root scope.
2178 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00002179 DIDescriptor Desc = RootScope->getDesc();
2180 if (Desc.isNull())
2181 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002182
2183 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00002184 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002185
2186 // Get the compile unit context.
Devang Pateldd9db662009-01-30 18:20:31 +00002187 CompileUnit *Unit = MainCU;
2188 if (!Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00002189 Unit = &FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002190
2191 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002192 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002193 assert(SPDie && "Missing subprogram descriptor");
2194
2195 // Add the function bounds.
2196 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2197 DWLabel("func_begin", SubprogramCount));
2198 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2199 DWLabel("func_end", SubprogramCount));
2200 MachineLocation Location(RI->getFrameRegister(*MF));
2201 AddAddress(SPDie, DW_AT_frame_base, Location);
2202
2203 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2204 }
2205
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002206 /// ConstructFunctionDbgScope - Construct the scope for the abstract debug
2207 /// scope.
2208 ///
2209 void ConstructAbstractDbgScope(DbgScope *AbsScope) {
2210 // Exit if there is no root scope.
2211 if (!AbsScope) return;
2212
2213 DIDescriptor Desc = AbsScope->getDesc();
2214 if (Desc.isNull())
2215 return;
2216
2217 // Get the subprogram debug information entry.
2218 DISubprogram SPD(Desc.getGV());
2219
2220 // Get the compile unit context.
2221 CompileUnit *Unit = MainCU;
2222 if (!Unit)
2223 Unit = &FindCompileUnit(SPD.getCompileUnit());
2224
2225 // Get the subprogram die.
2226 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
2227 assert(SPDie && "Missing subprogram descriptor");
2228
2229 ConstructDbgScope(AbsScope, 0, 0, SPDie, Unit);
2230 }
2231
Devang Patel7a6e5a32009-01-08 02:33:41 +00002232 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2233 ///
2234 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Chenge3d42322009-02-25 07:04:34 +00002235 const char *FnName = MF->getFunction()->getNameStart();
2236 if (MainCU) {
Bill Wendling972bbac2009-04-09 21:49:15 +00002237 StringMap<DIE*> &Globals = MainCU->getGlobals();
2238 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002239 if (GI != Globals.end()) {
2240 DIE *SPDie = GI->second;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002241
2242 // Add the function bounds.
2243 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2244 DWLabel("func_begin", SubprogramCount));
2245 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2246 DWLabel("func_end", SubprogramCount));
2247
2248 MachineLocation Location(RI->getFrameRegister(*MF));
2249 AddAddress(SPDie, DW_AT_frame_base, Location);
2250 return;
2251 }
Evan Chenge3d42322009-02-25 07:04:34 +00002252 } else {
2253 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2254 CompileUnit *Unit = CompileUnits[i];
Bill Wendling972bbac2009-04-09 21:49:15 +00002255 StringMap<DIE*> &Globals = Unit->getGlobals();
2256 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002257 if (GI != Globals.end()) {
2258 DIE *SPDie = GI->second;
2259
2260 // Add the function bounds.
2261 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2262 DWLabel("func_begin", SubprogramCount));
2263 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2264 DWLabel("func_end", SubprogramCount));
2265
2266 MachineLocation Location(RI->getFrameRegister(*MF));
2267 AddAddress(SPDie, DW_AT_frame_base, Location);
2268 return;
2269 }
2270 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002271 }
Evan Chenge3d42322009-02-25 07:04:34 +00002272
Devang Patel7a6e5a32009-01-08 02:33:41 +00002273#if 0
2274 // FIXME: This is causing an abort because C++ mangled names are compared
2275 // with their unmangled counterparts. See PR2885. Don't do this assert.
2276 assert(0 && "Couldn't find DIE for machine function!");
2277#endif
2278 }
2279
Jim Laskeyef42a012006-11-02 20:12:39 +00002280 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2281 /// tools to recognize the object file contains Dwarf information.
2282 void EmitInitial() {
2283 // Check to see if we already emitted intial headers.
2284 if (didInitial) return;
2285 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002286
Jim Laskeyef42a012006-11-02 20:12:39 +00002287 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002288 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002289 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002290 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002291 }
2292 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2293 EmitLabel("section_info", 0);
2294 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2295 EmitLabel("section_abbrev", 0);
2296 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2297 EmitLabel("section_aranges", 0);
Scott Michel210de722009-01-26 22:32:51 +00002298 if (TAI->doesSupportMacInfoSection()) {
2299 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2300 EmitLabel("section_macinfo", 0);
2301 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002302 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2303 EmitLabel("section_line", 0);
2304 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2305 EmitLabel("section_loc", 0);
2306 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2307 EmitLabel("section_pubnames", 0);
2308 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2309 EmitLabel("section_str", 0);
2310 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2311 EmitLabel("section_ranges", 0);
2312
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002313 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002314 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002315 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002316 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002317 }
2318
Jim Laskey65195462006-10-30 13:35:07 +00002319 /// EmitDIE - Recusively Emits a debug information entry.
2320 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002321 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002322 // Get the abbreviation for this DIE.
2323 unsigned AbbrevNumber = Die->getAbbrevNumber();
2324 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002325
Jim Laskeybacd3042007-02-21 22:48:45 +00002326 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002327
2328 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002329 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002330
Evan Cheng42bf74b2009-03-25 01:47:28 +00002331 if (Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002332 Asm->EOL(std::string("Abbrev [" +
2333 utostr(AbbrevNumber) +
2334 "] 0x" + utohexstr(Die->getOffset()) +
2335 ":0x" + utohexstr(Die->getSize()) + " " +
2336 TagString(Abbrev->getTag())));
2337 else
2338 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002339
Owen Anderson873e1b52008-06-24 21:44:59 +00002340 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2341 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002342
Jim Laskeyef42a012006-11-02 20:12:39 +00002343 // Emit the DIE attribute values.
2344 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2345 unsigned Attr = AbbrevData[i].getAttribute();
2346 unsigned Form = AbbrevData[i].getForm();
2347 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002348
Jim Laskeyef42a012006-11-02 20:12:39 +00002349 switch (Attr) {
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002350 case DW_AT_sibling:
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002351 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002352 break;
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002353 case DW_AT_abstract_origin: {
2354 DIEntry *E = cast<DIEntry>(Values[i]);
2355 DIE *Origin = E->getEntry();
2356 unsigned Addr =
2357 CompileUnitOffsets[Die->getAbstractCompileUnit()] +
2358 Origin->getOffset();
2359
2360 Asm->EmitInt32(Addr);
2361 break;
Jim Laskeyef42a012006-11-02 20:12:39 +00002362 }
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002363 default:
Jim Laskeyef42a012006-11-02 20:12:39 +00002364 // Emit an attribute using the defined form.
2365 Values[i]->EmitValue(*this, Form);
2366 break;
2367 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002368
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002369 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002370 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002371
Jim Laskeyef42a012006-11-02 20:12:39 +00002372 // Emit the DIE children if any.
2373 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2374 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002375
Bill Wendlinga9519572009-05-08 20:38:02 +00002376 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Jim Laskeyef42a012006-11-02 20:12:39 +00002377 EmitDIE(Children[j]);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002378
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002379 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002380 }
2381 }
2382
Jim Laskey65195462006-10-30 13:35:07 +00002383 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2384 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002385 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2386 // Get the children.
2387 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002388
Jim Laskeyef42a012006-11-02 20:12:39 +00002389 // If not last sibling and has children then add sibling offset attribute.
2390 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2391
2392 // Record the abbreviation.
2393 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002394
Jim Laskeyef42a012006-11-02 20:12:39 +00002395 // Get the abbreviation for this DIE.
2396 unsigned AbbrevNumber = Die->getAbbrevNumber();
2397 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2398
2399 // Set DIE offset
2400 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002401
Jim Laskeyef42a012006-11-02 20:12:39 +00002402 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002403 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2404
Owen Anderson873e1b52008-06-24 21:44:59 +00002405 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2406 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002407
2408 // Size the DIE attribute values.
2409 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2410 // Size attribute value.
2411 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2412 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002413
Jim Laskeyef42a012006-11-02 20:12:39 +00002414 // Size the DIE children if any.
2415 if (!Children.empty()) {
2416 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2417 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002418
Jim Laskeyef42a012006-11-02 20:12:39 +00002419 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2420 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2421 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002422
Jim Laskeyef42a012006-11-02 20:12:39 +00002423 // End of children marker.
2424 Offset += sizeof(int8_t);
2425 }
2426
2427 Die->setSize(Offset - Die->getOffset());
2428 return Offset;
2429 }
Jim Laskey65195462006-10-30 13:35:07 +00002430
2431 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2432 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002433 void SizeAndOffsets() {
Bill Wendlinge688faf2009-05-08 21:03:15 +00002434 // Compute size of compile unit header.
2435 static unsigned Offset =
2436 sizeof(int32_t) + // Length of Compilation Unit Info
2437 sizeof(int16_t) + // DWARF version number
2438 sizeof(int32_t) + // Offset Into Abbrev. Section
2439 sizeof(int8_t); // Pointer Size (in bytes)
2440
Jim Laskey5496f012006-11-09 14:52:14 +00002441 // Process base compile unit.
Devang Pateldd9db662009-01-30 18:20:31 +00002442 if (MainCU) {
Devang Pateldd9db662009-01-30 18:20:31 +00002443 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
Bill Wendlinge688faf2009-05-08 21:03:15 +00002444 CompileUnitOffsets[MainCU] = 0;
Devang Pateldd9db662009-01-30 18:20:31 +00002445 return;
2446 }
Bill Wendlinge688faf2009-05-08 21:03:15 +00002447
2448 // Process all compile units.
2449 unsigned PrevOffset = 0;
2450
Evan Chenge3d42322009-02-25 07:04:34 +00002451 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2452 CompileUnit *Unit = CompileUnits[i];
Bill Wendlinge688faf2009-05-08 21:03:15 +00002453 CompileUnitOffsets[Unit] = PrevOffset;
2454 PrevOffset += SizeAndOffsetDie(Unit->getDie(), Offset, true)
2455 + sizeof(int32_t); // FIXME - extra pad for gdb bug.
Devang Patel72b66352009-01-12 23:05:55 +00002456 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002457 }
2458
Evan Chenge3d42322009-02-25 07:04:34 +00002459 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Jim Laskey65195462006-10-30 13:35:07 +00002460 ///
Evan Chenge3d42322009-02-25 07:04:34 +00002461 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2462 DIE *Die = Unit->getDie();
2463 // Emit the compile units header.
2464 EmitLabel("info_begin", Unit->getID());
2465 // Emit size of content not including length itself
2466 unsigned ContentSize = Die->getSize() +
2467 sizeof(int16_t) + // DWARF version number
2468 sizeof(int32_t) + // Offset Into Abbrev. Section
2469 sizeof(int8_t) + // Pointer Size (in bytes)
2470 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2471
2472 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2473 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2474 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2475 Asm->EOL("Offset Into Abbrev. Section");
2476 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2477
2478 EmitDIE(Die);
2479 // FIXME - extra padding for gdb bug.
2480 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2481 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2482 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2483 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2484 EmitLabel("info_end", Unit->getID());
2485
2486 Asm->EOL();
2487 }
2488
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002489 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002490 // Start debug info section.
2491 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002492
Evan Chenge3d42322009-02-25 07:04:34 +00002493 if (MainCU) {
2494 EmitDebugInfoPerCU(MainCU);
2495 return;
Devang Patel72b66352009-01-12 23:05:55 +00002496 }
Evan Chenge3d42322009-02-25 07:04:34 +00002497
2498 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2499 EmitDebugInfoPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002500 }
2501
Jim Laskey65195462006-10-30 13:35:07 +00002502 /// EmitAbbreviations - Emit the abbreviation section.
2503 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002504 void EmitAbbreviations() const {
2505 // Check to see if it is worth the effort.
2506 if (!Abbreviations.empty()) {
2507 // Start the debug abbrev section.
2508 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002509
Jim Laskeyef42a012006-11-02 20:12:39 +00002510 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002511
Jim Laskeyef42a012006-11-02 20:12:39 +00002512 // For each abbrevation.
2513 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2514 // Get abbreviation data
2515 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002516
Jim Laskeyef42a012006-11-02 20:12:39 +00002517 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002518 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2519 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002520
Jim Laskeyef42a012006-11-02 20:12:39 +00002521 // Emit the abbreviations data.
2522 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002523
Jim Laskeybacd3042007-02-21 22:48:45 +00002524 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002525 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002526
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002527 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002528 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002529
Jim Laskeyef42a012006-11-02 20:12:39 +00002530 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002531
Jim Laskeybacd3042007-02-21 22:48:45 +00002532 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002533 }
2534 }
2535
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002536 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2537 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002538 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002539 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2540 // Define last address of section.
2541 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2542 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2543 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2544 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2545
2546 // Mark end of matrix.
2547 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2548 Asm->EmitULEB128Bytes(1); Asm->EOL();
2549 Asm->EmitInt8(1); Asm->EOL();
2550 }
2551
Jim Laskey65195462006-10-30 13:35:07 +00002552 /// EmitDebugLines - Emit source line information.
2553 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002554 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002555 // If the target is using .loc/.file, the assembler will be emitting the
2556 // .debug_line table automatically.
2557 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002558 return;
2559
Jim Laskeyef42a012006-11-02 20:12:39 +00002560 // Minimum line delta, thus ranging from -10..(255-10).
2561 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2562 // Maximum line delta, thus ranging from -10..(255-10).
2563 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002564
Jim Laskeyef42a012006-11-02 20:12:39 +00002565 // Start the dwarf line section.
2566 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002567
Jim Laskeyef42a012006-11-02 20:12:39 +00002568 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002569
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002570 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002571 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002572 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002573
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002574 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002575
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002576 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002577 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002578 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002579
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002580 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002581
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002582 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002583
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002584 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002585
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002586 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002587
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002588 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002589
Jim Laskeyef42a012006-11-02 20:12:39 +00002590 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002591 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2592 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2593 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2594 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2595 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2596 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2597 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2598 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2599 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002600
Jim Laskeyef42a012006-11-02 20:12:39 +00002601 // Emit directories.
Evan Chenge3d42322009-02-25 07:04:34 +00002602 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2603 Asm->EmitString(getSourceDirectoryName(DI));
2604 Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002605 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002606 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002607
Jim Laskeyef42a012006-11-02 20:12:39 +00002608 // Emit files.
Evan Chenge3d42322009-02-25 07:04:34 +00002609 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2610 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002611 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Chenge3d42322009-02-25 07:04:34 +00002612 Asm->EmitString(getSourceFileName(Id.second));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002613 Asm->EOL("Source");
Evan Chenge3d42322009-02-25 07:04:34 +00002614 Asm->EmitULEB128Bytes(Id.first);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002615 Asm->EOL("Directory #");
2616 Asm->EmitULEB128Bytes(0);
2617 Asm->EOL("Mod date");
2618 Asm->EmitULEB128Bytes(0);
2619 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002620 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002621 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002622
Jim Laskeyef42a012006-11-02 20:12:39 +00002623 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002624
Jim Laskeyef42a012006-11-02 20:12:39 +00002625 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002626 unsigned SecSrcLinesSize = SectionSourceLines.size();
2627
2628 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002629 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002630 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002631
Evan Cheng42bf74b2009-03-25 01:47:28 +00002632 if (Asm->isVerbose()) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002633 const Section* S = SectionMap[j + 1];
Evan Chenge3d42322009-02-25 07:04:34 +00002634 O << '\t' << TAI->getCommentString() << " Section"
2635 << S->getName() << '\n';
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002636 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002637 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002638
2639 // Dwarf assumes we start with first line of first source file.
2640 unsigned Source = 1;
2641 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002642
Jim Laskeyef42a012006-11-02 20:12:39 +00002643 // Construct rows of the address, source, line, column matrix.
2644 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00002645 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002646 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002647 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002648
Evan Cheng42bf74b2009-03-25 01:47:28 +00002649 if (!Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002650 Asm->EOL();
Evan Chenge3d42322009-02-25 07:04:34 +00002651 else {
2652 std::pair<unsigned, unsigned> SourceID =
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002653 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Chenge3d42322009-02-25 07:04:34 +00002654 O << '\t' << TAI->getCommentString() << ' '
2655 << getSourceDirectoryName(SourceID.first) << ' '
2656 << getSourceFileName(SourceID.second)
2657 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2658 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002659
2660 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002661 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002662 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002663 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002664 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002665
Jim Laskeyef42a012006-11-02 20:12:39 +00002666 // If change of source, then switch to the new source.
2667 if (Source != LineInfo.getSourceID()) {
2668 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002669 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2670 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002671 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002672
Jim Laskeyef42a012006-11-02 20:12:39 +00002673 // If change of line.
2674 if (Line != LineInfo.getLine()) {
2675 // Determine offset.
2676 int Offset = LineInfo.getLine() - Line;
2677 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002678
Jim Laskeyef42a012006-11-02 20:12:39 +00002679 // Update line.
2680 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002681
Jim Laskeyef42a012006-11-02 20:12:39 +00002682 // If delta is small enough and in range...
2683 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2684 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002685 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002686 } else {
2687 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002688 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2689 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2690 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002691 }
2692 } else {
2693 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002694 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002695 }
2696 }
2697
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002698 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002699 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002700
2701 if (SecSrcLinesSize == 0)
2702 // Because we're emitting a debug_line section, we still need a line
2703 // table. The linker and friends expect it to exist. If there's nothing to
2704 // put into it, emit an empty table.
2705 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002706
Jim Laskeyef42a012006-11-02 20:12:39 +00002707 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002708
Jim Laskeybacd3042007-02-21 22:48:45 +00002709 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002710 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002711
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002712 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002713 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002714 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002715 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002716 return;
2717
2718 int stackGrowth =
2719 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2720 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002721 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002722
2723 // Start the dwarf frame section.
2724 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2725
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002726 EmitLabel("debug_frame_common", 0);
2727 EmitDifference("debug_frame_common_end", 0,
2728 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002729 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002730
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002731 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002732 Asm->EmitInt32((int)DW_CIE_ID);
2733 Asm->EOL("CIE Identifier Tag");
2734 Asm->EmitInt8(DW_CIE_VERSION);
2735 Asm->EOL("CIE Version");
2736 Asm->EmitString("");
2737 Asm->EOL("CIE Augmentation");
2738 Asm->EmitULEB128Bytes(1);
2739 Asm->EOL("CIE Code Alignment Factor");
2740 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002741 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002742 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002743 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002744
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002745 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002746 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002747
Dale Johannesenb97aec62007-11-13 19:13:01 +00002748 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002749
Evan Cheng05548eb2008-02-29 19:36:59 +00002750 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002751 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002752
Jim Laskeybacd3042007-02-21 22:48:45 +00002753 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002754 }
2755
Jim Laskey65195462006-10-30 13:35:07 +00002756 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2757 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002758 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002759 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002760 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002761
Jim Laskeyef42a012006-11-02 20:12:39 +00002762 // Start the dwarf frame section.
2763 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002764
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002765 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2766 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002767 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002768
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002769 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002770
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002771 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2772 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002773 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002774
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002775 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002776 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002777 EmitDifference("func_end", DebugFrameInfo.Number,
2778 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002779 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002780
Devang Patel5aac3d32009-01-17 08:01:33 +00002781 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00002782 false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002783
Evan Cheng05548eb2008-02-29 19:36:59 +00002784 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002785 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002786
Jim Laskeybacd3042007-02-21 22:48:45 +00002787 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002788 }
2789
Evan Chenge3d42322009-02-25 07:04:34 +00002790 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2791 EmitDifference("pubnames_end", Unit->getID(),
2792 "pubnames_begin", Unit->getID(), true);
2793 Asm->EOL("Length of Public Names Info");
2794
2795 EmitLabel("pubnames_begin", Unit->getID());
2796
2797 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2798
2799 EmitSectionOffset("info_begin", "section_info",
2800 Unit->getID(), 0, true, false);
2801 Asm->EOL("Offset of Compilation Unit Info");
2802
2803 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2804 true);
2805 Asm->EOL("Compilation Unit Length");
2806
Bill Wendling972bbac2009-04-09 21:49:15 +00002807 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendlingf34be822009-04-09 23:51:31 +00002808 for (StringMap<DIE*>::const_iterator
Bill Wendling972bbac2009-04-09 21:49:15 +00002809 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendlingf34be822009-04-09 23:51:31 +00002810 const char *Name = GI->getKeyData();
Evan Chenge3d42322009-02-25 07:04:34 +00002811 DIE * Entity = GI->second;
2812
2813 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendlingf34be822009-04-09 23:51:31 +00002814 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Chenge3d42322009-02-25 07:04:34 +00002815 }
2816
2817 Asm->EmitInt32(0); Asm->EOL("End Mark");
2818 EmitLabel("pubnames_end", Unit->getID());
2819
2820 Asm->EOL();
2821 }
2822
Jim Laskeyef42a012006-11-02 20:12:39 +00002823 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002824 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002825 void EmitDebugPubNames() {
2826 // Start the dwarf pubnames section.
2827 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002828
Evan Chenge3d42322009-02-25 07:04:34 +00002829 if (MainCU) {
2830 EmitDebugPubNamesPerCU(MainCU);
2831 return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002832 }
Evan Chenge3d42322009-02-25 07:04:34 +00002833
2834 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2835 EmitDebugPubNamesPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002836 }
2837
2838 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002839 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002840 void EmitDebugStr() {
2841 // Check to see if it is worth the effort.
2842 if (!StringPool.empty()) {
2843 // Start the dwarf str section.
2844 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002845
Jim Laskeyef42a012006-11-02 20:12:39 +00002846 // For each of strings in the string pool.
2847 for (unsigned StringID = 1, N = StringPool.size();
2848 StringID <= N; ++StringID) {
2849 // Emit a label for reference from debug information entries.
2850 EmitLabel("string", StringID);
2851 // Emit the string itself.
2852 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002853 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002854 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002855
Jim Laskeybacd3042007-02-21 22:48:45 +00002856 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002857 }
2858 }
2859
2860 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002861 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002862 void EmitDebugLoc() {
2863 // Start the dwarf loc section.
2864 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002865
Jim Laskeybacd3042007-02-21 22:48:45 +00002866 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002867 }
2868
2869 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002870 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002871 void EmitDebugARanges() {
2872 // Start the dwarf aranges section.
2873 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002874
Jim Laskeyef42a012006-11-02 20:12:39 +00002875 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002876#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002877 CompileUnit *Unit = GetBaseCompileUnit();
2878
Jim Laskey5496f012006-11-09 14:52:14 +00002879 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002880 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002881
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002882 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002883
Jim Laskey5496f012006-11-09 14:52:14 +00002884 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002885 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002886
Dan Gohman82482942007-09-27 23:12:31 +00002887 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002888
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002889 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002890
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002891 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2892 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002893
Jim Laskey5496f012006-11-09 14:52:14 +00002894 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002895 EmitReference("text_begin", 0); Asm->EOL("Address");
2896 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002897
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002898 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2899 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002900#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002901
Jim Laskeybacd3042007-02-21 22:48:45 +00002902 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002903 }
2904
2905 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002906 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002907 void EmitDebugRanges() {
2908 // Start the dwarf ranges section.
2909 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002910
Jim Laskeybacd3042007-02-21 22:48:45 +00002911 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002912 }
2913
2914 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002915 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002916 void EmitDebugMacInfo() {
Scott Michel210de722009-01-26 22:32:51 +00002917 if (TAI->doesSupportMacInfoSection()) {
2918 // Start the dwarf macinfo section.
2919 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002920
Scott Michel210de722009-01-26 22:32:51 +00002921 Asm->EOL();
2922 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002923 }
2924
Devang Patel0f7fef32009-04-13 17:02:03 +00002925 /// EmitDebugInlineInfo - Emit inline info using following format.
2926 /// Section Header:
2927 /// 1. length of section
2928 /// 2. Dwarf version number
2929 /// 3. address size.
2930 ///
2931 /// Entries (one "entry" for each function that was inlined):
2932 ///
2933 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2934 /// otherwise offset into __debug_str for regular function name.
2935 /// 2. offset into __debug_str section for regular function name.
2936 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2937 /// instances for the function.
2938 ///
2939 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2940 /// inlined instance; the die_offset points to the inlined_subroutine die in
2941 /// the __debug_info section, and the low_pc is the starting address for the
2942 /// inlining instance.
2943 void EmitDebugInlineInfo() {
2944 if (!TAI->doesDwarfUsesInlineInfoSection())
2945 return;
2946
2947 if (!MainCU)
2948 return;
2949
2950 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection());
2951 Asm->EOL();
2952 EmitDifference("debug_inlined_end", 1,
2953 "debug_inlined_begin", 1, true);
2954 Asm->EOL("Length of Debug Inlined Information Entry");
2955
2956 EmitLabel("debug_inlined_begin", 1);
2957
2958 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2959 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2960
2961 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2962 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2963 GlobalVariable *GV = I->first;
2964 SmallVector<unsigned, 4> &Labels = I->second;
2965 DISubprogram SP(GV);
2966 std::string Name;
2967 std::string LName;
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002968
Devang Patel0f7fef32009-04-13 17:02:03 +00002969 SP.getLinkageName(LName);
2970 SP.getName(Name);
2971
2972 Asm->EmitString(LName.empty() ? Name : LName);
2973 Asm->EOL("MIPS linkage name");
2974
2975 Asm->EmitString(Name); Asm->EOL("Function name");
2976
2977 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2978
2979 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2980 LE = Labels.end(); LI != LE; ++LI) {
2981 DIE *SP = MainCU->getDieMapSlotFor(GV);
2982 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2983
2984 if (TD->getPointerSize() == sizeof(int32_t))
2985 O << TAI->getData32bitsDirective();
2986 else
2987 O << TAI->getData64bitsDirective();
2988 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2989 }
2990 }
2991
2992 EmitLabel("debug_inlined_end", 1);
2993 Asm->EOL();
2994 }
2995
Bill Wendlingc8615e42009-03-10 21:47:45 +00002996 /// GetOrCreateSourceID - Look up the source id with the given directory and
2997 /// source file names. If none currently exists, create a new id and insert it
2998 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
2999 /// as well.
3000 unsigned GetOrCreateSourceID(const std::string &DirName,
3001 const std::string &FileName) {
3002 unsigned DId;
3003 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
3004 if (DI != DirectoryIdMap.end()) {
3005 DId = DI->getValue();
3006 } else {
3007 DId = DirectoryNames.size() + 1;
3008 DirectoryIdMap[DirName] = DId;
3009 DirectoryNames.push_back(DirName);
3010 }
3011
3012 unsigned FId;
3013 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
3014 if (FI != SourceFileIdMap.end()) {
3015 FId = FI->getValue();
3016 } else {
3017 FId = SourceFileNames.size() + 1;
3018 SourceFileIdMap[FileName] = FId;
3019 SourceFileNames.push_back(FileName);
3020 }
3021
3022 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
3023 SourceIdMap.find(std::make_pair(DId, FId));
3024 if (SI != SourceIdMap.end())
3025 return SI->second;
3026
3027 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
3028 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
3029 SourceIds.push_back(std::make_pair(DId, FId));
3030
3031 return SrcId;
3032 }
3033
Evan Chenge3d42322009-02-25 07:04:34 +00003034 void ConstructCompileUnit(GlobalVariable *GV) {
3035 DICompileUnit DIUnit(GV);
Bill Wendling0582ae92009-03-13 04:39:26 +00003036 std::string Dir, FN, Prod;
3037 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
3038 DIUnit.getFilename(FN));
Evan Chenge3d42322009-02-25 07:04:34 +00003039
3040 DIE *Die = new DIE(DW_TAG_compile_unit);
3041 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
3042 DWLabel("section_line", 0), DWLabel("section_line", 0),
3043 false);
Bill Wendling0582ae92009-03-13 04:39:26 +00003044 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Chenge3d42322009-02-25 07:04:34 +00003045 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00003046 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendling0582ae92009-03-13 04:39:26 +00003047 if (!Dir.empty())
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00003048 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Chenge3d42322009-02-25 07:04:34 +00003049 if (DIUnit.isOptimized())
3050 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendling0582ae92009-03-13 04:39:26 +00003051 std::string Flags;
3052 DIUnit.getFlags(Flags);
3053 if (!Flags.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00003054 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
3055 unsigned RVer = DIUnit.getRunTimeVersion();
3056 if (RVer)
3057 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
3058
3059 CompileUnit *Unit = new CompileUnit(ID, Die);
3060 if (DIUnit.isMain()) {
3061 assert(!MainCU && "Multiple main compile units are found!");
3062 MainCU = Unit;
3063 }
3064 CompileUnitMap[DIUnit.getGV()] = Unit;
3065 CompileUnits.push_back(Unit);
3066 }
3067
Devang Patelc4523242009-01-05 23:11:11 +00003068 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00003069 void ConstructCompileUnits() {
Evan Chenge3d42322009-02-25 07:04:34 +00003070 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
3071 if (!Root)
3072 return;
3073 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3074 "Malformed compile unit descriptor anchor type");
3075 Constant *RootC = cast<Constant>(*Root->use_begin());
3076 assert(RootC->hasNUsesOrMore(1) &&
3077 "Malformed compile unit descriptor anchor type");
3078 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3079 UI != UE; ++UI)
3080 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3081 UUI != UUE; ++UUI) {
3082 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3083 ConstructCompileUnit(GV);
Devang Pateldd9db662009-01-30 18:20:31 +00003084 }
Evan Chenge3d42322009-02-25 07:04:34 +00003085 }
3086
3087 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
3088 DIGlobalVariable DI_GV(GV);
3089 CompileUnit *DW_Unit = MainCU;
3090 if (!DW_Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00003091 DW_Unit = &FindCompileUnit(DI_GV.getCompileUnit());
Evan Chenge3d42322009-02-25 07:04:34 +00003092
3093 // Check for pre-existence.
3094 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
3095 if (Slot)
3096 return false;
3097
3098 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
3099
3100 // Add address.
3101 DIEBlock *Block = new DIEBlock();
3102 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Bill Wendling7d16e852009-04-10 00:12:49 +00003103 std::string GLN;
Evan Chenge3d42322009-02-25 07:04:34 +00003104 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling7d16e852009-04-10 00:12:49 +00003105 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Chenge3d42322009-02-25 07:04:34 +00003106 AddBlock(VariableDie, DW_AT_location, 0, Block);
3107
3108 // Add to map.
3109 Slot = VariableDie;
Bill Wendling3f500d92009-05-06 21:21:34 +00003110
Evan Chenge3d42322009-02-25 07:04:34 +00003111 // Add to context owner.
3112 DW_Unit->getDie()->AddChild(VariableDie);
Bill Wendling3f500d92009-05-06 21:21:34 +00003113
Evan Chenge3d42322009-02-25 07:04:34 +00003114 // Expose as global. FIXME - need to check external flag.
Bill Wendling0582ae92009-03-13 04:39:26 +00003115 std::string Name;
3116 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003117 return true;
Devang Pateld1ca9252009-01-05 23:03:32 +00003118 }
3119
Devang Patelc4523242009-01-05 23:11:11 +00003120 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patel53cac182009-02-24 00:02:15 +00003121 /// visible global variables. Return true if at least one global DIE is
3122 /// created.
3123 bool ConstructGlobalVariableDIEs() {
Evan Chenge3d42322009-02-25 07:04:34 +00003124 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
3125 if (!Root)
3126 return false;
Devang Patelc4523242009-01-05 23:11:11 +00003127
Evan Chenge3d42322009-02-25 07:04:34 +00003128 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3129 "Malformed global variable descriptor anchor type");
3130 Constant *RootC = cast<Constant>(*Root->use_begin());
3131 assert(RootC->hasNUsesOrMore(1) &&
3132 "Malformed global variable descriptor anchor type");
Devang Patelc4523242009-01-05 23:11:11 +00003133
Evan Chenge3d42322009-02-25 07:04:34 +00003134 bool Result = false;
3135 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3136 UI != UE; ++UI)
3137 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling3f500d92009-05-06 21:21:34 +00003138 UUI != UUE; ++UUI)
3139 Result |= ConstructGlobalVariableDIE(cast<GlobalVariable>(*UUI));
3140
Evan Chenge3d42322009-02-25 07:04:34 +00003141 return Result;
3142 }
Devang Patelc4523242009-01-05 23:11:11 +00003143
Evan Chenge3d42322009-02-25 07:04:34 +00003144 bool ConstructSubprogram(GlobalVariable *GV) {
3145 DISubprogram SP(GV);
3146 CompileUnit *Unit = MainCU;
3147 if (!Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00003148 Unit = &FindCompileUnit(SP.getCompileUnit());
Devang Patelc4523242009-01-05 23:11:11 +00003149
Evan Chenge3d42322009-02-25 07:04:34 +00003150 // Check for pre-existence.
3151 DIE *&Slot = Unit->getDieMapSlotFor(GV);
3152 if (Slot)
3153 return false;
3154
3155 if (!SP.isDefinition())
3156 // This is a method declaration which will be handled while
3157 // constructing class type.
3158 return false;
3159
3160 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
3161
3162 // Add to map.
3163 Slot = SubprogramDie;
3164 // Add to context owner.
3165 Unit->getDie()->AddChild(SubprogramDie);
3166 // Expose as global.
Bill Wendling0582ae92009-03-13 04:39:26 +00003167 std::string Name;
3168 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003169 return true;
Devang Patelc4523242009-01-05 23:11:11 +00003170 }
3171
Devang Patel78eb6ad2009-01-05 23:21:35 +00003172 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patel53cac182009-02-24 00:02:15 +00003173 /// subprograms. Return true if at least one subprogram DIE is created.
3174 bool ConstructSubprograms() {
Evan Chenge3d42322009-02-25 07:04:34 +00003175 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
3176 if (!Root)
3177 return false;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003178
Evan Chenge3d42322009-02-25 07:04:34 +00003179 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3180 "Malformed subprogram descriptor anchor type");
3181 Constant *RootC = cast<Constant>(*Root->use_begin());
3182 assert(RootC->hasNUsesOrMore(1) &&
3183 "Malformed subprogram descriptor anchor type");
Devang Patel78eb6ad2009-01-05 23:21:35 +00003184
Evan Chenge3d42322009-02-25 07:04:34 +00003185 bool Result = false;
3186 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3187 UI != UE; ++UI)
3188 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling3f500d92009-05-06 21:21:34 +00003189 UUI != UUE; ++UUI)
3190 Result |= ConstructSubprogram(cast<GlobalVariable>(*UUI));
3191
Evan Chenge3d42322009-02-25 07:04:34 +00003192 return Result;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003193 }
3194
Jim Laskey65195462006-10-30 13:35:07 +00003195public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003196 //===--------------------------------------------------------------------===//
3197 // Main entry points.
3198 //
Owen Andersoncb371882008-08-21 00:14:44 +00003199 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00003200 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3201 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3202 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3203 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel7d2f9722009-04-15 20:41:31 +00003204 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003205 if (TimePassesIsEnabled)
3206 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00003207 getDwarfTimerGroup());
Jim Laskeyef42a012006-11-02 20:12:39 +00003208 }
Jim Laskey072200c2007-01-29 18:51:14 +00003209 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003210 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3211 delete Values[j];
Bill Wendling163ba3f2009-03-10 21:23:25 +00003212
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003213 for (DenseMap<const GlobalVariable *, DbgScope *>::iterator
3214 I = AbstractInstanceRootMap.begin(),
3215 E = AbstractInstanceRootMap.end(); I != E;++I)
3216 delete I->second;
3217
Bill Wendling163ba3f2009-03-10 21:23:25 +00003218 delete DebugTimer;
Jim Laskeyef42a012006-11-02 20:12:39 +00003219 }
3220
Bill Wendlingc8615e42009-03-10 21:47:45 +00003221 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3222 /// be emitted.
3223 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3224
Devang Patel5d315982009-01-06 21:07:30 +00003225 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3226 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00003227 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003228 if (TimePassesIsEnabled)
3229 DebugTimer->startTimer();
3230
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003231 // Create all the compile unit DIEs.
3232 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00003233
Bill Wendling163ba3f2009-03-10 21:23:25 +00003234 if (CompileUnits.empty()) {
3235 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003236 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003237
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003238 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003239 }
Devang Patel3f7833a2009-01-12 23:09:42 +00003240
Devang Patel53cac182009-02-24 00:02:15 +00003241 // Create DIEs for each of the externally visible global variables.
3242 bool globalDIEs = ConstructGlobalVariableDIEs();
3243
3244 // Create DIEs for each of the externally visible subprograms.
3245 bool subprogramDIEs = ConstructSubprograms();
3246
3247 // If there is not any debug info available for any global variables
3248 // and any subprograms then there is not any debug info to emit.
Bill Wendling163ba3f2009-03-10 21:23:25 +00003249 if (!globalDIEs && !subprogramDIEs) {
3250 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003251 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003252
Devang Patel53cac182009-02-24 00:02:15 +00003253 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003254 }
Devang Patel53cac182009-02-24 00:02:15 +00003255
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003256 MMI = mmi;
3257 shouldEmit = true;
3258 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00003259
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003260 // Prime section data.
3261 SectionMap.insert(TAI->getTextSection());
Devang Patel5d315982009-01-06 21:07:30 +00003262
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003263 // Print out .file directives to specify files for .loc directives. These
3264 // are printed out early so that they precede any .loc directives.
3265 if (TAI->hasDotLocAndDotFile()) {
Evan Chenge3d42322009-02-25 07:04:34 +00003266 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3267 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00003268 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Chenge3d42322009-02-25 07:04:34 +00003269 sys::Path FullPath(getSourceDirectoryName(Id.first));
3270 bool AppendOk =
3271 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003272 assert(AppendOk && "Could not append filename to directory!");
3273 AppendOk = false;
3274 Asm->EmitFile(i, FullPath.toString());
3275 Asm->EOL();
Devang Patel5d315982009-01-06 21:07:30 +00003276 }
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003277 }
Devang Patel5d315982009-01-06 21:07:30 +00003278
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003279 // Emit initial sections
3280 EmitInitial();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003281
3282 if (TimePassesIsEnabled)
3283 DebugTimer->stopTimer();
Devang Patel5d315982009-01-06 21:07:30 +00003284 }
3285
Jim Laskey65195462006-10-30 13:35:07 +00003286 /// BeginModule - Emit all Dwarf sections that should come prior to the
3287 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003288 void BeginModule(Module *M) {
3289 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003290 }
3291
Jim Laskey65195462006-10-30 13:35:07 +00003292 /// EndModule - Emit all Dwarf sections that should come after the content.
3293 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003294 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003295 if (!ShouldEmitDwarfDebug())
3296 return;
3297
3298 if (TimePassesIsEnabled)
3299 DebugTimer->startTimer();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003300
Jim Laskeyef42a012006-11-02 20:12:39 +00003301 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003302 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003303 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003304 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003305 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003306
Jim Laskeyef42a012006-11-02 20:12:39 +00003307 // End text sections.
3308 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003309 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003310 EmitLabel("section_end", i);
3311 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003312
3313 // Emit common frame information.
3314 EmitCommonDebugFrame();
3315
3316 // Emit function debug frame information
3317 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3318 E = DebugFrames.end(); I != E; ++I)
3319 EmitFunctionDebugFrame(*I);
3320
Jim Laskeyef42a012006-11-02 20:12:39 +00003321 // Compute DIE offsets and sizes.
3322 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003323
Jim Laskeyef42a012006-11-02 20:12:39 +00003324 // Emit all the DIEs into a debug info section
3325 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003326
Jim Laskeyef42a012006-11-02 20:12:39 +00003327 // Corresponding abbreviations into a abbrev section.
3328 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003329
Jim Laskeyef42a012006-11-02 20:12:39 +00003330 // Emit source line correspondence into a debug line section.
3331 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003332
Jim Laskeyef42a012006-11-02 20:12:39 +00003333 // Emit info into a debug pubnames section.
3334 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003335
Jim Laskeyef42a012006-11-02 20:12:39 +00003336 // Emit info into a debug str section.
3337 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003338
Jim Laskeyef42a012006-11-02 20:12:39 +00003339 // Emit info into a debug loc section.
3340 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003341
Jim Laskeyef42a012006-11-02 20:12:39 +00003342 // Emit info into a debug aranges section.
3343 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003344
Jim Laskeyef42a012006-11-02 20:12:39 +00003345 // Emit info into a debug ranges section.
3346 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003347
Jim Laskeyef42a012006-11-02 20:12:39 +00003348 // Emit info into a debug macinfo section.
3349 EmitDebugMacInfo();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003350
Devang Patel0f7fef32009-04-13 17:02:03 +00003351 // Emit inline info.
3352 EmitDebugInlineInfo();
3353
Bill Wendling163ba3f2009-03-10 21:23:25 +00003354 if (TimePassesIsEnabled)
3355 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003356 }
3357
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003358 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003359 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003360 void BeginFunction(MachineFunction *MF) {
Bill Wendling1362f972009-03-11 00:03:50 +00003361 this->MF = MF;
3362
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003363 if (!ShouldEmitDwarfDebug()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003364
Bill Wendling163ba3f2009-03-10 21:23:25 +00003365 if (TimePassesIsEnabled)
3366 DebugTimer->startTimer();
3367
Jim Laskeyef42a012006-11-02 20:12:39 +00003368 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003369 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003370
Jim Laskeyef42a012006-11-02 20:12:39 +00003371 // Assumes in correct section after the entry point.
3372 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003373
Argyrios Kyrtzidis28f14122009-05-04 19:23:45 +00003374 // Emit label for the implicitly defined dbg.stoppoint at the start of
3375 // the function.
Mike Stumpfe095f32009-05-04 18:40:41 +00003376 DebugLoc FDL = MF->getDefaultDebugLoc();
3377 if (!FDL.isUnknown()) {
3378 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
3379 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col,
3380 DICompileUnit(DLT.CompileUnit));
3381 Asm->printLabel(LabelID);
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003382 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003383
3384 if (TimePassesIsEnabled)
3385 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003386 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003387
Jim Laskey65195462006-10-30 13:35:07 +00003388 /// EndFunction - Gather and emit post-function debug information.
3389 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003390 void EndFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003391 if (!ShouldEmitDwarfDebug()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003392
Bill Wendling163ba3f2009-03-10 21:23:25 +00003393 if (TimePassesIsEnabled)
3394 DebugTimer->startTimer();
3395
Jim Laskeyb82313f2007-02-01 16:31:34 +00003396 // Define end label for subprogram.
3397 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003398
Jim Laskeyef42a012006-11-02 20:12:39 +00003399 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00003400 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003401 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003402 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003403 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00003404 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00003405 // Append the function info to section info.
3406 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00003407 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00003408 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003409
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003410 // Construct the DbgScope for abstract instances.
3411 for (SmallVector<DbgScope *, 32>::iterator
3412 I = AbstractInstanceRootList.begin(),
3413 E = AbstractInstanceRootList.end(); I != E; ++I)
3414 ConstructAbstractDbgScope(*I);
3415
Jim Laskeyef42a012006-11-02 20:12:39 +00003416 // Construct scopes for subprogram.
Devang Patel7d2f9722009-04-15 20:41:31 +00003417 if (FunctionDbgScope)
3418 ConstructFunctionDbgScope(FunctionDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00003419 else
3420 // FIXME: This is wrong. We are essentially getting past a problem with
3421 // debug information not being able to handle unreachable blocks that have
3422 // debug information in them. In particular, those unreachable blocks that
3423 // have "region end" info in them. That situation results in the "root
3424 // scope" not being created. If that's the case, then emit a "default"
3425 // scope, i.e., one that encompasses the whole function. This isn't
3426 // desirable. And a better way of handling this (and all of the debugging
3427 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003428 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003429
3430 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3431 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003432
3433 // Clear debug info
Devang Patel7d2f9722009-04-15 20:41:31 +00003434 if (FunctionDbgScope) {
3435 delete FunctionDbgScope;
Devang Patel481ff5b2009-01-12 18:48:36 +00003436 DbgScopeMap.clear();
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003437 DbgConcreteScopeMap.clear();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003438 InlinedVariableScopes.clear();
Devang Patel7d2f9722009-04-15 20:41:31 +00003439 FunctionDbgScope = NULL;
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003440 LexicalScopeStack.clear();
3441 AbstractInstanceRootList.clear();
Devang Patel481ff5b2009-01-12 18:48:36 +00003442 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003443
Bill Wendling163ba3f2009-03-10 21:23:25 +00003444 Lines.clear();
3445
3446 if (TimePassesIsEnabled)
3447 DebugTimer->stopTimer();
3448 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003449
3450 /// RecordSourceLine - Records location information and associates it with a
3451 /// label. Returns a unique label ID used to generate a label and provide
3452 /// correspondence to the source line list.
3453 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003454 if (TimePassesIsEnabled)
3455 DebugTimer->startTimer();
3456
Evan Chenge3d42322009-02-25 07:04:34 +00003457 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003458 assert(Unit && "Unable to find CompileUnit");
Devang Patelccca7fe2009-01-12 19:17:34 +00003459 unsigned ID = MMI->NextLabelID();
3460 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003461
3462 if (TimePassesIsEnabled)
3463 DebugTimer->stopTimer();
3464
Devang Patelccca7fe2009-01-12 19:17:34 +00003465 return ID;
3466 }
3467
3468 /// RecordSourceLine - Records location information and associates it with a
3469 /// label. Returns a unique label ID used to generate a label and provide
3470 /// correspondence to the source line list.
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00003471 unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003472 if (TimePassesIsEnabled)
3473 DebugTimer->startTimer();
3474
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00003475 std::string Dir, Fn;
3476 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
3477 CU.getFilename(Fn));
Devang Patelccca7fe2009-01-12 19:17:34 +00003478 unsigned ID = MMI->NextLabelID();
3479 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003480
3481 if (TimePassesIsEnabled)
3482 DebugTimer->stopTimer();
3483
Devang Patelccca7fe2009-01-12 19:17:34 +00003484 return ID;
3485 }
3486
Bill Wendlingc8615e42009-03-10 21:47:45 +00003487 /// getRecordSourceLineCount - Return the number of source lines in the debug
3488 /// info.
3489 unsigned getRecordSourceLineCount() const {
Devang Patelccca7fe2009-01-12 19:17:34 +00003490 return Lines.size();
3491 }
3492
Bill Wendlingc8615e42009-03-10 21:47:45 +00003493 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3494 /// timed. Look up the source id with the given directory and source file
3495 /// names. If none currently exists, create a new id and insert it in the
3496 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3497 /// well.
Evan Chenge3d42322009-02-25 07:04:34 +00003498 unsigned getOrCreateSourceID(const std::string &DirName,
3499 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003500 if (TimePassesIsEnabled)
3501 DebugTimer->startTimer();
3502
Bill Wendlingc8615e42009-03-10 21:47:45 +00003503 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003504
3505 if (TimePassesIsEnabled)
3506 DebugTimer->stopTimer();
3507
Evan Chenge3d42322009-02-25 07:04:34 +00003508 return SrcId;
Devang Patelccca7fe2009-01-12 19:17:34 +00003509 }
3510
3511 /// RecordRegionStart - Indicate the start of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003512 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003513 if (TimePassesIsEnabled)
3514 DebugTimer->startTimer();
3515
Devang Patelccca7fe2009-01-12 19:17:34 +00003516 DbgScope *Scope = getOrCreateScope(V);
3517 unsigned ID = MMI->NextLabelID();
3518 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003519 LexicalScopeStack.push_back(Scope);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003520
3521 if (TimePassesIsEnabled)
3522 DebugTimer->stopTimer();
3523
Devang Patelccca7fe2009-01-12 19:17:34 +00003524 return ID;
3525 }
3526
3527 /// RecordRegionEnd - Indicate the end of a region.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003528 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003529 if (TimePassesIsEnabled)
3530 DebugTimer->startTimer();
3531
Bill Wendling5b8479c2009-05-07 17:26:14 +00003532 DbgScope *Scope = getOrCreateScope(V);
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003533 unsigned ID = MMI->NextLabelID();
Devang Patelccca7fe2009-01-12 19:17:34 +00003534 Scope->setEndLabelID(ID);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003535 if (LexicalScopeStack.size() != 0)
3536 LexicalScopeStack.pop_back();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003537
3538 if (TimePassesIsEnabled)
3539 DebugTimer->stopTimer();
3540
Devang Patelccca7fe2009-01-12 19:17:34 +00003541 return ID;
3542 }
3543
3544 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patel1be3ecc2009-04-15 00:10:26 +00003545 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
3546 const MachineInstr *MI) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003547 if (TimePassesIsEnabled)
3548 DebugTimer->startTimer();
3549
Devang Patel0e5200f2009-01-15 18:25:17 +00003550 DIDescriptor Desc(GV);
3551 DbgScope *Scope = NULL;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003552
Devang Patel0e5200f2009-01-15 18:25:17 +00003553 if (Desc.getTag() == DW_TAG_variable) {
3554 // GV is a global variable.
3555 DIGlobalVariable DG(GV);
3556 Scope = getOrCreateScope(DG.getContext().getGV());
3557 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003558 DenseMap<const MachineInstr *, DbgScope *>::iterator
3559 SI = InlinedVariableScopes.find(MI);
Bill Wendling3f500d92009-05-06 21:21:34 +00003560
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003561 if (SI != InlinedVariableScopes.end()) {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003562 // or GV is an inlined local variable.
3563 Scope = SI->second;
3564 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003565 DIVariable DV(GV);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003566 GlobalVariable *V = DV.getContext().getGV();
3567
3568 // FIXME: The code that checks for the inlined local variable is a hack!
3569 DenseMap<const GlobalVariable *, DbgScope *>::iterator
3570 AI = AbstractInstanceRootMap.find(V);
3571
3572 if (AI != AbstractInstanceRootMap.end())
3573 // or GV is an inlined local variable.
3574 Scope = AI->second;
3575 else
3576 // or GV is a local variable.
3577 Scope = getOrCreateScope(V);
Devang Patel1be3ecc2009-04-15 00:10:26 +00003578 }
Devang Patel0e5200f2009-01-15 18:25:17 +00003579 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003580
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003581 assert(Scope && "Unable to find variable' scope");
Devang Patel99ec3532009-01-16 19:28:14 +00003582 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelccca7fe2009-01-12 19:17:34 +00003583 Scope->AddVariable(DV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003584
3585 if (TimePassesIsEnabled)
3586 DebugTimer->stopTimer();
Devang Patelccca7fe2009-01-12 19:17:34 +00003587 }
Devang Patel0f7fef32009-04-13 17:02:03 +00003588
Devang Patel1be3ecc2009-04-15 00:10:26 +00003589 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003590 unsigned RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
3591 unsigned Line, unsigned Col) {
3592 unsigned LabelID = MMI->NextLabelID();
3593
Devang Patel1be3ecc2009-04-15 00:10:26 +00003594 if (!TAI->doesDwarfUsesInlineInfoSection())
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003595 return LabelID;
Devang Patel1be3ecc2009-04-15 00:10:26 +00003596
Bill Wendlingd5a63812009-05-01 08:40:06 +00003597 if (TimePassesIsEnabled)
3598 DebugTimer->startTimer();
3599
Devang Patel1be3ecc2009-04-15 00:10:26 +00003600 GlobalVariable *GV = SP.getGV();
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003601 DenseMap<const GlobalVariable *, DbgScope *>::iterator
3602 II = AbstractInstanceRootMap.find(GV);
Devang Patel1be3ecc2009-04-15 00:10:26 +00003603
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003604 if (II == AbstractInstanceRootMap.end()) {
3605 // Create an abstract instance entry for this inlined function if it
3606 // doesn't already exist.
3607 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(GV));
Bill Wendlingef956fc2009-05-01 08:32:14 +00003608
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003609 // Get the compile unit context.
3610 CompileUnit *Unit = &FindCompileUnit(SP.getCompileUnit());
3611 DIE *SPDie = Unit->getDieMapSlotFor(GV);
3612 if (!SPDie)
3613 SPDie = CreateSubprogramDIE(Unit, SP);
3614
3615 // Mark as being inlined. This makes this subprogram entry an abstract
3616 // instance root.
3617 // FIXME: Our debugger doesn't care about the value of DW_AT_inline, only
3618 // that it's defined. It probably won't change in the future, but this
3619 // could be more elegant.
3620 AddUInt(SPDie, DW_AT_inline, 0, DW_INL_declared_not_inlined);
3621
3622 // Track the start label for this inlined function.
3623 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
3624 I = InlineInfo.find(GV);
3625
3626 if (I == InlineInfo.end())
3627 InlineInfo[GV].push_back(LabelID);
3628 else
3629 I->second.push_back(LabelID);
3630
3631 AbstractInstanceRootMap[GV] = Scope;
3632 AbstractInstanceRootList.push_back(Scope);
3633 }
3634
3635 // Create a concrete inlined instance for this inlined function.
3636 DbgConcreteScope *ConcreteScope = new DbgConcreteScope(DIDescriptor(GV));
3637 DIE *ScopeDie = new DIE(DW_TAG_inlined_subroutine);
3638 CompileUnit *Unit = &FindCompileUnit(SP.getCompileUnit());
3639 ScopeDie->setAbstractCompileUnit(Unit);
3640
3641 DIE *Origin = Unit->getDieMapSlotFor(GV);
3642 AddDIEntry(ScopeDie, DW_AT_abstract_origin, DW_FORM_ref4, Origin);
3643 AddUInt(ScopeDie, DW_AT_call_file, 0, Unit->getID());
3644 AddUInt(ScopeDie, DW_AT_call_line, 0, Line);
3645 AddUInt(ScopeDie, DW_AT_call_column, 0, Col);
3646
3647 ConcreteScope->setDie(ScopeDie);
3648 ConcreteScope->setStartLabelID(LabelID);
3649
3650 LexicalScopeStack.back()->AddConcreteInst(ConcreteScope);
3651
3652 // Keep track of the scope that's inlined into this function.
3653 DenseMap<GlobalVariable *, SmallVector<DbgConcreteScope *, 8> >::iterator
3654 SI = DbgConcreteScopeMap.find(GV);
3655
3656 if (SI == DbgConcreteScopeMap.end())
3657 DbgConcreteScopeMap[GV].push_back(ConcreteScope);
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003658 else
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003659 SI->second.push_back(ConcreteScope);
Bill Wendlingd5a63812009-05-01 08:40:06 +00003660
3661 if (TimePassesIsEnabled)
3662 DebugTimer->stopTimer();
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003663
3664 return LabelID;
Devang Patel0f7fef32009-04-13 17:02:03 +00003665 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00003666
3667 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
3668 unsigned RecordInlinedFnEnd(DISubprogram &SP) {
3669 if (!TAI->doesDwarfUsesInlineInfoSection())
3670 return 0;
3671
Bill Wendlingd5a63812009-05-01 08:40:06 +00003672 if (TimePassesIsEnabled)
3673 DebugTimer->startTimer();
3674
Devang Patel1be3ecc2009-04-15 00:10:26 +00003675 GlobalVariable *GV = SP.getGV();
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003676 DenseMap<GlobalVariable *, SmallVector<DbgConcreteScope *, 8> >::iterator
3677 I = DbgConcreteScopeMap.find(GV);
Bill Wendling3f500d92009-05-06 21:21:34 +00003678
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003679 if (I == DbgConcreteScopeMap.end()) {
Bill Wendlingd5a63812009-05-01 08:40:06 +00003680 if (TimePassesIsEnabled)
3681 DebugTimer->stopTimer();
3682
Devang Patel1be3ecc2009-04-15 00:10:26 +00003683 return 0;
Bill Wendlingd5a63812009-05-01 08:40:06 +00003684 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00003685
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003686 SmallVector<DbgConcreteScope *, 8> &Scopes = I->second;
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00003687 assert(!Scopes.empty() && "We should have at least one debug scope!");
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003688 DbgConcreteScope *Scope = Scopes.back(); Scopes.pop_back();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003689 unsigned ID = MMI->NextLabelID();
Bill Wendling3f500d92009-05-06 21:21:34 +00003690
Devang Patel1be3ecc2009-04-15 00:10:26 +00003691 MMI->RecordUsedDbgLabel(ID);
3692 Scope->setEndLabelID(ID);
Bill Wendlingd5a63812009-05-01 08:40:06 +00003693
3694 if (TimePassesIsEnabled)
3695 DebugTimer->stopTimer();
3696
Devang Patel1be3ecc2009-04-15 00:10:26 +00003697 return ID;
3698 }
3699
3700 /// RecordVariableScope - Record scope for the variable declared by
3701 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
3702 /// Record scopes for only inlined subroutine variables. Other
3703 /// variables' scopes are determined during RecordVariable().
3704 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI) {
Bill Wendlingd5a63812009-05-01 08:40:06 +00003705 if (TimePassesIsEnabled)
3706 DebugTimer->startTimer();
3707
Devang Patel1be3ecc2009-04-15 00:10:26 +00003708 DISubprogram SP(DV.getContext().getGV());
Bill Wendlingd5a63812009-05-01 08:40:06 +00003709
3710 if (SP.isNull()) {
3711 if (TimePassesIsEnabled)
3712 DebugTimer->stopTimer();
3713
Devang Patel1be3ecc2009-04-15 00:10:26 +00003714 return;
Bill Wendlingd5a63812009-05-01 08:40:06 +00003715 }
3716
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003717 DenseMap<GlobalVariable *, SmallVector<DbgConcreteScope *, 8> >::iterator
3718 I = DbgConcreteScopeMap.find(SP.getGV());
3719 if (I != DbgConcreteScopeMap.end())
Bill Wendlingd5a63812009-05-01 08:40:06 +00003720 InlinedVariableScopes[DeclareMI] = I->second.back();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003721
Bill Wendlingd5a63812009-05-01 08:40:06 +00003722 if (TimePassesIsEnabled)
3723 DebugTimer->stopTimer();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003724 }
Jim Laskey65195462006-10-30 13:35:07 +00003725};
3726
Jim Laskey072200c2007-01-29 18:51:14 +00003727//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003728/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003729///
3730class DwarfException : public Dwarf {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003731 struct FunctionEHFrameInfo {
3732 std::string FnName;
3733 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003734 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003735 bool hasCalls;
3736 bool hasLandingPads;
3737 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003738 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003739
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003740 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3741 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003742 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003743 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003744 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003745 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003746 };
3747
3748 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003749
3750 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3751 /// be emitted.
3752 bool shouldEmitTable;
3753
3754 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3755 /// should be emitted.
3756 bool shouldEmitMoves;
3757
3758 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3759 /// should be emitted.
3760 bool shouldEmitTableModule;
3761
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003762 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003763 /// should be emitted.
3764 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003765
Bill Wendling163ba3f2009-03-10 21:23:25 +00003766 /// ExceptionTimer - Timer for the Dwarf exception writer.
3767 Timer *ExceptionTimer;
3768
Jim Laskey3f09fc22007-02-28 18:38:31 +00003769 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3770 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003771 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003772 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003773 int stackGrowth =
3774 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3775 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003776 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003777
Jim Laskeybacd3042007-02-21 22:48:45 +00003778 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003779 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003780
3781 if (!TAI->doesRequireNonLocalEHFrameLabel())
3782 O << TAI->getEHGlobalPrefix();
3783 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003784 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003785
Jim Laskeybacd3042007-02-21 22:48:45 +00003786 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003787 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003788
Jim Laskeybacd3042007-02-21 22:48:45 +00003789 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003790 EmitDifference("eh_frame_common_end", Index,
3791 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003792 Asm->EOL("Length of Common Information Entry");
3793
Jim Laskeybacd3042007-02-21 22:48:45 +00003794 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003795 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003796 Asm->EmitInt32((int)0);
3797 Asm->EOL("CIE Identifier Tag");
3798 Asm->EmitInt8(DW_CIE_VERSION);
3799 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003800
Jim Laskeybacd3042007-02-21 22:48:45 +00003801 // The personality presence indicates that language specific information
3802 // will show up in the eh frame.
3803 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003804 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003805
Jim Laskeybacd3042007-02-21 22:48:45 +00003806 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003807 Asm->EmitULEB128Bytes(1);
3808 Asm->EOL("CIE Code Alignment Factor");
3809 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003810 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003811 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003812 Asm->EOL("CIE Return Address Column");
3813
Jim Laskeybacd3042007-02-21 22:48:45 +00003814 // If there is a personality, we need to indicate the functions location.
3815 if (Personality) {
3816 Asm->EmitULEB128Bytes(7);
3817 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003818
Duncan Sands671fa972008-05-07 19:11:09 +00003819 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003820 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003821 Asm->EOL("Personality (pcrel sdata4 indirect)");
3822 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003823 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003824 Asm->EOL("Personality (pcrel sdata4)");
3825 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003826
Duncan Sands671fa972008-05-07 19:11:09 +00003827 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003828 O << TAI->getPersonalityPrefix();
3829 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3830 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003831 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3832 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003833 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003834
Duncan Sands671fa972008-05-07 19:11:09 +00003835 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3836 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003837
Bill Wendlingd60de512009-01-05 22:53:45 +00003838 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3839 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003840 } else {
3841 Asm->EmitULEB128Bytes(1);
3842 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003843
Bill Wendlingd60de512009-01-05 22:53:45 +00003844 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3845 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003846 }
3847
3848 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003849 std::vector<MachineMove> Moves;
3850 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003851 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003852
Dale Johannesen21d972a2008-04-30 00:43:29 +00003853 // On Darwin the linker honors the alignment of eh_frame, which means it
3854 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3855 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003856 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003857 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003858 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003859
Jim Laskeybacd3042007-02-21 22:48:45 +00003860 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003861 }
Duncan Sands671fa972008-05-07 19:11:09 +00003862
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003863 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003864 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003865 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003866 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
Chris Lattner266c7bb2009-04-13 05:44:34 +00003867
3868 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
3869 "Should not emit 'available externally' functions at all");
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003870
Jim Laskeybacd3042007-02-21 22:48:45 +00003871 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3872
3873 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003874 // If the corresponding function is static, this should not be
3875 // externally visible.
Rafael Espindolabb46f522009-01-15 20:18:42 +00003876 if (linkage != Function::InternalLinkage &&
Devang Patel2d1768c2009-01-17 08:05:14 +00003877 linkage != Function::PrivateLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003878 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3879 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3880 }
3881
Dale Johannesen038129d2008-01-10 02:03:30 +00003882 // If corresponding function is weak definition, this should be too.
Duncan Sands667d4b82009-03-07 15:45:40 +00003883 if ((linkage == Function::WeakAnyLinkage ||
3884 linkage == Function::WeakODRLinkage ||
3885 linkage == Function::LinkOnceAnyLinkage ||
3886 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003887 TAI->getWeakDefDirective())
3888 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3889
3890 // If there are no calls then you can't unwind. This may mean we can
3891 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003892 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003893 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003894 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003895 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003896 !UnwindTablesMandatory &&
Duncan Sands667d4b82009-03-07 15:45:40 +00003897 ((linkage != Function::WeakAnyLinkage &&
3898 linkage != Function::WeakODRLinkage &&
3899 linkage != Function::LinkOnceAnyLinkage &&
3900 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003901 !TAI->getWeakDefDirective() ||
3902 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003903 {
Bill Wendling6e198962007-09-18 01:47:22 +00003904 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003905 // This name has no connection to the function, so it might get
3906 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003907 // dead-stripping unconditionally.
3908 if (const char *UsedDirective = TAI->getUsedDirective())
3909 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003910 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003911 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003912
Jim Laskeybacd3042007-02-21 22:48:45 +00003913 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003914 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3915 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003916 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003917
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003918 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003919
Bill Wendling722f5f12008-12-24 08:05:17 +00003920 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3921 PrintRelDirective(true, true);
3922 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3923
3924 if (!TAI->isAbsoluteEHSectionOffsets())
3925 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3926 } else {
3927 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3928 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3929 true, true, false);
3930 }
3931
Jim Laskeybacd3042007-02-21 22:48:45 +00003932 Asm->EOL("FDE CIE offset");
3933
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003934 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003935 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003936 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003937 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003938 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003939
Jim Laskey3f09fc22007-02-28 18:38:31 +00003940 // If there is a personality and landing pads then point to the language
3941 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003942 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003943 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003944 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003945
3946 if (EHFrameInfo.hasLandingPads)
3947 EmitReference("exception", EHFrameInfo.Number, true, true);
3948 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003949 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003950 Asm->EOL("Language Specific Data Area");
3951 } else {
3952 Asm->EmitULEB128Bytes(0);
3953 Asm->EOL("Augmentation size");
3954 }
Duncan Sands671fa972008-05-07 19:11:09 +00003955
Jim Laskeybacd3042007-02-21 22:48:45 +00003956 // Indicate locations of function specific callee saved registers in
3957 // frame.
Devang Patel5aac3d32009-01-17 08:01:33 +00003958 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00003959 true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003960
Dale Johannesen21d972a2008-04-30 00:43:29 +00003961 // On Darwin the linker honors the alignment of eh_frame, which means it
3962 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3963 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003964 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003965 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003966 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003967
3968 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003969 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003970 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003971 // that depends on unused functions (calling undefined externals) being
3972 // dead-stripped to link correctly. Yes, there really is.
3973 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3974 if (const char *UsedDirective = TAI->getUsedDirective())
3975 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3976 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003977 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003978
Duncan Sands57810cd2007-09-05 11:27:52 +00003979 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003980 ///
3981 /// The general organization of the table is complex, but the basic concepts
3982 /// are easy. First there is a header which describes the location and
3983 /// organization of the three components that follow.
3984 /// 1. The landing pad site information describes the range of code covered
3985 /// by the try. In our case it's an accumulation of the ranges covered
3986 /// by the invokes in the try. There is also a reference to the landing
3987 /// pad that handles the exception once processed. Finally an index into
3988 /// the actions table.
3989 /// 2. The action table, in our case, is composed of pairs of type ids
3990 /// and next action offset. Starting with the action index from the
3991 /// landing pad site, each type Id is checked for a match to the current
3992 /// exception. If it matches then the exception and type id are passed
3993 /// on to the landing pad. Otherwise the next action is looked up. This
3994 /// chain is terminated with a next action of zero. If no type id is
3995 /// found the the frame is unwound and handling continues.
3996 /// 3. Type id table contains references to all the C++ typeinfo for all
3997 /// catches in the function. This tables is reversed indexed base 1.
3998
Duncan Sandsb32edb42007-06-06 15:37:31 +00003999 /// SharedTypeIds - How many leading type ids two landing pads have in common.
4000 static unsigned SharedTypeIds(const LandingPadInfo *L,
4001 const LandingPadInfo *R) {
4002 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004003 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004004 unsigned MinSize = LSize < RSize ? LSize : RSize;
4005 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004006
Duncan Sandsb32edb42007-06-06 15:37:31 +00004007 for (; Count != MinSize; ++Count)
4008 if (LIds[Count] != RIds[Count])
4009 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004010
Duncan Sandsb32edb42007-06-06 15:37:31 +00004011 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004012 }
4013
Duncan Sandsb32edb42007-06-06 15:37:31 +00004014 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00004015 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004016 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004017 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004018 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004019
Duncan Sandsb32edb42007-06-06 15:37:31 +00004020 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00004021 if (LIds[i] != RIds[i])
4022 return LIds[i] < RIds[i];
4023
Duncan Sandsb32edb42007-06-06 15:37:31 +00004024 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004025 }
4026
Duncan Sands53c3a332007-05-16 12:12:23 +00004027 struct KeyInfo {
4028 static inline unsigned getEmptyKey() { return -1U; }
4029 static inline unsigned getTombstoneKey() { return -2U; }
4030 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00004031 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00004032 static bool isPod() { return true; }
4033 };
4034
Duncan Sands57810cd2007-09-05 11:27:52 +00004035 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004036 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004037 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004038 int NextAction;
4039 struct ActionEntry *Previous;
4040 };
4041
Duncan Sands57810cd2007-09-05 11:27:52 +00004042 /// PadRange - Structure holding a try-range and the associated landing pad.
4043 struct PadRange {
4044 // The index of the landing pad.
4045 unsigned PadIndex;
4046 // The index of the begin and end labels in the landing pad's label lists.
4047 unsigned RangeIndex;
4048 };
4049
4050 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
4051
4052 /// CallSiteEntry - Structure describing an entry in the call-site table.
4053 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00004054 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004055 unsigned BeginLabel; // zero indicates the start of the function.
4056 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00004057 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004058 unsigned PadLabel; // zero indicates that there is no landing pad.
4059 unsigned Action;
4060 };
4061
Jim Laskeybacd3042007-02-21 22:48:45 +00004062 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00004063 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00004064 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00004065 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
4066 if (PadInfos.empty()) return;
4067
4068 // Sort the landing pads in order of their type ids. This is used to fold
4069 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00004070 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00004071 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00004072 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00004073 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004074 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
4075
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004076 // Negative type ids index into FilterIds, positive type ids index into
4077 // TypeInfos. The value written for a positive type id is just the type
4078 // id itself. For a negative type id, however, the value written is the
4079 // (negative) byte offset of the corresponding FilterIds entry. The byte
4080 // offset is usually equal to the type id, because the FilterIds entries
4081 // are written using a variable width encoding which outputs one byte per
4082 // entry as long as the value written is not too large, but can differ.
4083 // This kind of complication does not occur for positive type ids because
4084 // type infos are output using a fixed width encoding.
4085 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
4086 SmallVector<int, 16> FilterOffsets;
4087 FilterOffsets.reserve(FilterIds.size());
4088 int Offset = -1;
4089 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
4090 E = FilterIds.end(); I != E; ++I) {
4091 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004092 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004093 }
4094
Duncan Sands57810cd2007-09-05 11:27:52 +00004095 // Compute the actions table and gather the first action index for each
4096 // landing pad site.
4097 SmallVector<ActionEntry, 32> Actions;
4098 SmallVector<unsigned, 64> FirstActions;
4099 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00004100
Duncan Sandsb32edb42007-06-06 15:37:31 +00004101 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00004102 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004103 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004104 const LandingPadInfo *LP = LandingPads[i];
4105 const std::vector<int> &TypeIds = LP->TypeIds;
4106 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004107 unsigned SizeSiteActions = 0;
4108
Duncan Sandsb32edb42007-06-06 15:37:31 +00004109 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00004110 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00004111 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004112
Duncan Sandsb32edb42007-06-06 15:37:31 +00004113 if (NumShared) {
4114 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
4115 assert(Actions.size());
4116 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004117 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
4118 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004119 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004120 SizeAction -=
4121 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004122 SizeAction += -PrevAction->NextAction;
4123 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004124 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00004125 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00004126
Duncan Sandsb32edb42007-06-06 15:37:31 +00004127 // Compute the actions.
4128 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
4129 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004130 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4131 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004132 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004133
4134 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004135 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004136 SizeSiteActions += SizeAction;
4137
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004138 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00004139 Actions.push_back(Action);
4140
4141 PrevAction = &Actions.back();
4142 }
4143
4144 // Record the first action of the landing pad site.
4145 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4146 } // else identical - re-use previous FirstAction
4147
4148 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004149
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00004150 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00004151 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00004152 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004153
Duncan Sands481dc722007-12-19 07:36:31 +00004154 // Compute the call-site table. The entry for an invoke has a try-range
4155 // containing the call, a non-zero landing pad and an appropriate action.
4156 // The entry for an ordinary call has a try-range containing the call and
4157 // zero for the landing pad and the action. Calls marked 'nounwind' have
4158 // no entry and must not be contained in the try-range of any entry - they
4159 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004160 SmallVector<CallSiteEntry, 64> CallSites;
4161
4162 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00004163 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4164 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4165 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00004166 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4167 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00004168 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004169 unsigned BeginLabel = LandingPad->BeginLabels[j];
4170 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4171 PadRange P = { i, j };
4172 PadMap[BeginLabel] = P;
4173 }
4174 }
4175
Duncan Sands481dc722007-12-19 07:36:31 +00004176 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00004177 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00004178
4179 // Whether there is a potentially throwing instruction (currently this means
4180 // an ordinary call) between the end of the previous try-range and now.
4181 bool SawPotentiallyThrowing = false;
4182
4183 // Whether the last callsite entry was for an invoke.
4184 bool PreviousIsInvoke = false;
4185
Duncan Sands481dc722007-12-19 07:36:31 +00004186 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004187 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4188 I != E; ++I) {
4189 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4190 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00004191 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00004192 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00004193 continue;
4194 }
4195
Chris Lattner9e330492007-12-30 20:50:28 +00004196 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00004197 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00004198
Duncan Sands481dc722007-12-19 07:36:31 +00004199 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00004200 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00004201 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004202
Duncan Sands481dc722007-12-19 07:36:31 +00004203 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00004204 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00004205 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00004206 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00004207 continue;
4208
4209 PadRange P = L->second;
4210 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4211
4212 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4213 "Inconsistent landing pad map!");
4214
4215 // If some instruction between the previous try-range and this one may
4216 // throw, create a call-site entry with no landing pad for the region
4217 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00004218 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004219 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4220 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00004221 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004222 }
4223
4224 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00004225 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00004226
Duncan Sands481dc722007-12-19 07:36:31 +00004227 if (LandingPad->LandingPadLabel) {
4228 // This try-range is for an invoke.
4229 CallSiteEntry Site = {BeginLabel, LastLabel,
4230 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00004231
Duncan Sands481dc722007-12-19 07:36:31 +00004232 // Try to merge with the previous call-site.
4233 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00004234 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00004235 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4236 // Extend the range of the previous entry.
4237 Prev.EndLabel = Site.EndLabel;
4238 continue;
4239 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004240 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004241
Duncan Sands481dc722007-12-19 07:36:31 +00004242 // Otherwise, create a new call-site.
4243 CallSites.push_back(Site);
4244 PreviousIsInvoke = true;
4245 } else {
4246 // Create a gap.
4247 PreviousIsInvoke = false;
4248 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004249 }
4250 }
4251 // If some instruction between the previous try-range and the end of the
4252 // function may throw, create a call-site entry with no landing pad for the
4253 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00004254 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004255 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4256 CallSites.push_back(Site);
4257 }
4258
Jim Laskeybacd3042007-02-21 22:48:45 +00004259 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00004260
4261 // Call sites.
4262 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4263 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4264 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4265 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4266 SiteLengthSize +
4267 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00004268 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004269 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00004270
Duncan Sands671fa972008-05-07 19:11:09 +00004271 // Type infos.
4272 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4273 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004274
4275 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004276 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004277 SizeSites + SizeActions + SizeTypes;
4278
4279 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4280 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004281 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004282 TypeOffset;
4283
4284 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004285
Jim Laskeybacd3042007-02-21 22:48:45 +00004286 // Begin the exception table.
4287 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00004288 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00004289 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004290 for (unsigned i = 0; i != SizeAlign; ++i) {
4291 Asm->EmitInt8(0);
4292 Asm->EOL("Padding");
4293 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004294 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004295
Jim Laskeybacd3042007-02-21 22:48:45 +00004296 // Emit the header.
4297 Asm->EmitInt8(DW_EH_PE_omit);
4298 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4299 Asm->EmitInt8(DW_EH_PE_absptr);
4300 Asm->EOL("TType format (DW_EH_PE_absptr)");
4301 Asm->EmitULEB128Bytes(TypeOffset);
4302 Asm->EOL("TType base offset");
4303 Asm->EmitInt8(DW_EH_PE_udata4);
4304 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4305 Asm->EmitULEB128Bytes(SizeSites);
4306 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004307
Duncan Sands57810cd2007-09-05 11:27:52 +00004308 // Emit the landing pad site information.
4309 for (unsigned i = 0; i < CallSites.size(); ++i) {
4310 CallSiteEntry &S = CallSites[i];
4311 const char *BeginTag;
4312 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00004313
Duncan Sands57810cd2007-09-05 11:27:52 +00004314 if (!S.BeginLabel) {
4315 BeginTag = "eh_func_begin";
4316 BeginNumber = SubprogramCount;
4317 } else {
4318 BeginTag = "label";
4319 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004320 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004321
Duncan Sands57810cd2007-09-05 11:27:52 +00004322 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004323 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004324 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004325
Duncan Sands57810cd2007-09-05 11:27:52 +00004326 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004327 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004328 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004329 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004330 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004331 }
4332 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004333
Duncan Sands671fa972008-05-07 19:11:09 +00004334 if (!S.PadLabel)
4335 Asm->EmitInt32(0);
4336 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004337 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004338 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004339 Asm->EOL("Landing pad");
4340
4341 Asm->EmitULEB128Bytes(S.Action);
4342 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004343 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004344
Jim Laskeybacd3042007-02-21 22:48:45 +00004345 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004346 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4347 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004348
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004349 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004350 Asm->EOL("TypeInfo index");
4351 Asm->EmitSLEB128Bytes(Action.NextAction);
4352 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004353 }
4354
4355 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004356 for (unsigned M = TypeInfos.size(); M; --M) {
4357 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004358
4359 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004360
Bill Wendling7d16e852009-04-10 00:12:49 +00004361 if (GV) {
4362 std::string GLN;
4363 O << Asm->getGlobalLinkName(GV, GLN);
4364 } else {
Jim Laskeybacd3042007-02-21 22:48:45 +00004365 O << "0";
Bill Wendling7d16e852009-04-10 00:12:49 +00004366 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004367
Jim Laskeybacd3042007-02-21 22:48:45 +00004368 Asm->EOL("TypeInfo");
4369 }
4370
Duncan Sands73ef58a2007-06-02 16:53:42 +00004371 // Emit the filter typeids.
4372 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4373 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004374 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004375 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004376 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004377
Evan Cheng05548eb2008-02-29 19:36:59 +00004378 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004379 }
4380
Jim Laskey072200c2007-01-29 18:51:14 +00004381public:
4382 //===--------------------------------------------------------------------===//
4383 // Main entry points.
4384 //
Owen Andersoncb371882008-08-21 00:14:44 +00004385 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00004386 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4387 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4388 ExceptionTimer(0) {
4389 if (TimePassesIsEnabled)
4390 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00004391 getDwarfTimerGroup());
Bill Wendling163ba3f2009-03-10 21:23:25 +00004392 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004393
Bill Wendling163ba3f2009-03-10 21:23:25 +00004394 virtual ~DwarfException() {
4395 delete ExceptionTimer;
4396 }
Jim Laskey072200c2007-01-29 18:51:14 +00004397
4398 /// SetModuleInfo - Set machine module information when it's known that pass
4399 /// manager has created it. Set by the target AsmPrinter.
4400 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004401 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004402 }
4403
4404 /// BeginModule - Emit all exception information that should come prior to the
4405 /// content.
4406 void BeginModule(Module *M) {
4407 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004408 }
4409
4410 /// EndModule - Emit all exception information that should come after the
4411 /// content.
4412 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004413 if (TimePassesIsEnabled)
4414 ExceptionTimer->startTimer();
4415
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004416 if (shouldEmitMovesModule || shouldEmitTableModule) {
4417 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Chenge3d42322009-02-25 07:04:34 +00004418 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004419 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004420
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004421 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4422 E = EHFrames.end(); I != E; ++I)
4423 EmitEHFrame(*I);
4424 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004425
4426 if (TimePassesIsEnabled)
4427 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004428 }
4429
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004430 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004431 /// emitted immediately after the function entry point.
4432 void BeginFunction(MachineFunction *MF) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004433 if (TimePassesIsEnabled)
4434 ExceptionTimer->startTimer();
4435
Jim Laskey072200c2007-01-29 18:51:14 +00004436 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004437 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004438
Bill Wendling163ba3f2009-03-10 21:23:25 +00004439 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004440 // Map all labels and get rid of any dead landing pads.
4441 MMI->TidyLandingPads();
Bill Wendling163ba3f2009-03-10 21:23:25 +00004442
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004443 // If any landing pads survive, we need an EH table.
4444 if (MMI->getLandingPads().size())
4445 shouldEmitTable = true;
4446
4447 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004448 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004449 shouldEmitMoves = true;
4450
4451 if (shouldEmitMoves || shouldEmitTable)
4452 // Assumes in correct section after the entry point.
4453 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004454 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004455
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004456 shouldEmitTableModule |= shouldEmitTable;
4457 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendling163ba3f2009-03-10 21:23:25 +00004458
4459 if (TimePassesIsEnabled)
4460 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004461 }
4462
4463 /// EndFunction - Gather and emit post-function exception information.
4464 ///
4465 void EndFunction() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004466 if (TimePassesIsEnabled)
4467 ExceptionTimer->startTimer();
4468
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004469 if (shouldEmitMoves || shouldEmitTable) {
4470 EmitLabel("eh_func_end", SubprogramCount);
4471 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004472
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004473 // Save EH frame information
Bill Wendling7d16e852009-04-10 00:12:49 +00004474 std::string Name;
4475 EHFrames.push_back(
4476 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF, Name),
4477 SubprogramCount,
4478 MMI->getPersonalityIndex(),
4479 MF->getFrameInfo()->hasCalls(),
4480 !MMI->getLandingPads().empty(),
4481 MMI->getFrameMoves(),
4482 MF->getFunction()));
Bill Wendling163ba3f2009-03-10 21:23:25 +00004483 }
4484
4485 if (TimePassesIsEnabled)
4486 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004487 }
4488};
4489
Jim Laskey0d086af2006-02-27 12:43:29 +00004490} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004491
4492//===----------------------------------------------------------------------===//
4493
Jim Laskeyd18e2892006-01-20 20:34:06 +00004494/// Emit - Print the abbreviation using the specified Dwarf writer.
4495///
Jim Laskey072200c2007-01-29 18:51:14 +00004496void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004497 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004498 DD.getAsm()->EmitULEB128Bytes(Tag);
4499 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004500
Jim Laskeyd18e2892006-01-20 20:34:06 +00004501 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004502 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4503 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004504
Jim Laskeyd18e2892006-01-20 20:34:06 +00004505 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004506 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004507 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004508
Jim Laskeyd18e2892006-01-20 20:34:06 +00004509 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004510 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4511 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004512
Jim Laskeyd18e2892006-01-20 20:34:06 +00004513 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004514 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4515 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004516 }
4517
4518 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004519 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4520 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004521}
4522
4523#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004524void DIEAbbrev::print(std::ostream &O) {
4525 O << "Abbreviation @"
4526 << std::hex << (intptr_t)this << std::dec
4527 << " "
4528 << TagString(Tag)
4529 << " "
4530 << ChildrenString(ChildrenFlag)
4531 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004532
Jim Laskeya0f3d172006-09-07 22:06:40 +00004533 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4534 O << " "
4535 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004536 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004537 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004538 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004539 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004540}
Bill Wendlinge8156192006-12-07 01:30:32 +00004541void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004542#endif
4543
4544//===----------------------------------------------------------------------===//
4545
Jim Laskeyef42a012006-11-02 20:12:39 +00004546#ifndef NDEBUG
4547void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004548 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004549}
Jim Laskeyef42a012006-11-02 20:12:39 +00004550#endif
4551
4552//===----------------------------------------------------------------------===//
4553
Jim Laskey063e7652006-01-17 17:31:53 +00004554/// EmitValue - Emit integer of appropriate size.
4555///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004556void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004557 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004558 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004559 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004560 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004561 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004562 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004563 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004564 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004565 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004566 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4567 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4568 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004569 default: assert(0 && "DIE Value form not supported yet"); break;
4570 }
4571}
4572
4573/// SizeOf - Determine size of integer value in bytes.
4574///
Jim Laskey072200c2007-01-29 18:51:14 +00004575unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004576 switch (Form) {
4577 case DW_FORM_flag: // Fall thru
4578 case DW_FORM_ref1: // Fall thru
4579 case DW_FORM_data1: return sizeof(int8_t);
4580 case DW_FORM_ref2: // Fall thru
4581 case DW_FORM_data2: return sizeof(int16_t);
4582 case DW_FORM_ref4: // Fall thru
4583 case DW_FORM_data4: return sizeof(int32_t);
4584 case DW_FORM_ref8: // Fall thru
4585 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004586 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4587 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004588 default: assert(0 && "DIE Value form not supported yet"); break;
4589 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004590 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004591}
4592
Jim Laskey063e7652006-01-17 17:31:53 +00004593//===----------------------------------------------------------------------===//
4594
4595/// EmitValue - Emit string value.
4596///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004597void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Bill Wendlingdd446322009-03-10 23:57:09 +00004598 DD.getAsm()->EmitString(Str);
Jim Laskey063e7652006-01-17 17:31:53 +00004599}
4600
Jim Laskey063e7652006-01-17 17:31:53 +00004601//===----------------------------------------------------------------------===//
4602
4603/// EmitValue - Emit label value.
4604///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004605void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004606 bool IsSmall = Form == DW_FORM_data4;
4607 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004608}
4609
4610/// SizeOf - Determine size of label value in bytes.
4611///
Jim Laskey072200c2007-01-29 18:51:14 +00004612unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004613 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004614 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004615}
Jim Laskeyef42a012006-11-02 20:12:39 +00004616
Jim Laskey063e7652006-01-17 17:31:53 +00004617//===----------------------------------------------------------------------===//
4618
Jim Laskeyd18e2892006-01-20 20:34:06 +00004619/// EmitValue - Emit label value.
4620///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004621void DIEObjectLabel::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 Laskeyd18e2892006-01-20 20:34:06 +00004624}
4625
4626/// SizeOf - Determine size of label value in bytes.
4627///
Jim Laskey072200c2007-01-29 18:51:14 +00004628unsigned DIEObjectLabel::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 Laskeyd18e2892006-01-20 20:34:06 +00004631}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004632
Jim Laskeyd18e2892006-01-20 20:34:06 +00004633//===----------------------------------------------------------------------===//
4634
Jim Laskey063e7652006-01-17 17:31:53 +00004635/// EmitValue - Emit delta value.
4636///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004637void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4638 bool IsSmall = Form == DW_FORM_data4;
4639 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4640 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4641}
4642
4643/// SizeOf - Determine size of delta value in bytes.
4644///
4645unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4646 if (Form == DW_FORM_data4) return 4;
4647 return DD.getTargetData()->getPointerSize();
4648}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004649
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004650//===----------------------------------------------------------------------===//
4651
4652/// EmitValue - Emit delta value.
4653///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004654void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004655 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004656 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004657}
4658
4659/// SizeOf - Determine size of delta value in bytes.
4660///
Jim Laskey072200c2007-01-29 18:51:14 +00004661unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004662 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004663 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004664}
4665
4666//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004667
Jim Laskeyb8509c52006-03-23 18:07:55 +00004668/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004669///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004670void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004671 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004672}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004673
Jim Laskeyd18e2892006-01-20 20:34:06 +00004674//===----------------------------------------------------------------------===//
4675
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004676/// ComputeSize - calculate the size of the block.
4677///
Jim Laskey072200c2007-01-29 18:51:14 +00004678unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004679 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004680 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004681
Jim Laskeyef42a012006-11-02 20:12:39 +00004682 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004683 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004684 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004685 }
4686 return Size;
4687}
4688
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004689/// EmitValue - Emit block data.
4690///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004691void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004692 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004693 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4694 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4695 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4696 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004697 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004698 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004699
Owen Anderson873e1b52008-06-24 21:44:59 +00004700 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004701
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004702 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004703 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004704 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004705 }
4706}
4707
4708/// SizeOf - Determine size of block data in bytes.
4709///
Jim Laskey072200c2007-01-29 18:51:14 +00004710unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004711 switch (Form) {
4712 case DW_FORM_block1: return Size + sizeof(int8_t);
4713 case DW_FORM_block2: return Size + sizeof(int16_t);
4714 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004715 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004716 default: assert(0 && "Improper form for block"); break;
4717 }
4718 return 0;
4719}
4720
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004721//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004722/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004723
4724DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004725 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004726 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004727}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004728
Jim Laskeyb8509c52006-03-23 18:07:55 +00004729/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4730///
4731void DIE::AddSiblingOffset() {
4732 DIEInteger *DI = new DIEInteger(0);
4733 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004734 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004735}
4736
Jim Laskeyef42a012006-11-02 20:12:39 +00004737/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004738///
Jim Laskeyef42a012006-11-02 20:12:39 +00004739void DIE::Profile(FoldingSetNodeID &ID) {
4740 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004741
Jim Laskeyef42a012006-11-02 20:12:39 +00004742 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4743 ID.AddPointer(Children[i]);
4744
4745 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4746 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004747}
Jim Laskeyef42a012006-11-02 20:12:39 +00004748
4749#ifndef NDEBUG
4750void DIE::print(std::ostream &O, unsigned IncIndent) {
4751 static unsigned IndentCount = 0;
4752 IndentCount += IncIndent;
4753 const std::string Indent(IndentCount, ' ');
4754 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004755
Jim Laskeyef42a012006-11-02 20:12:39 +00004756 if (!isBlock) {
4757 O << Indent
4758 << "Die: "
4759 << "0x" << std::hex << (intptr_t)this << std::dec
4760 << ", Offset: " << Offset
4761 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004762 << "\n";
4763
Jim Laskeyef42a012006-11-02 20:12:39 +00004764 O << Indent
4765 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004766 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004767 << ChildrenString(Abbrev.getChildrenFlag());
4768 } else {
4769 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004770 }
4771 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004772
Owen Anderson873e1b52008-06-24 21:44:59 +00004773 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004774
Jim Laskeyef42a012006-11-02 20:12:39 +00004775 IndentCount += 2;
4776 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4777 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004778
4779 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004780 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004781 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004782 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004783
Jim Laskeyef42a012006-11-02 20:12:39 +00004784 O << " "
4785 << FormEncodingString(Data[i].getForm())
4786 << " ";
4787 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004788 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004789 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004790 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004791
Jim Laskeyef42a012006-11-02 20:12:39 +00004792 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4793 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004794 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004795
Jim Laskeyef42a012006-11-02 20:12:39 +00004796 if (!isBlock) O << "\n";
4797 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004798}
4799
Jim Laskeyef42a012006-11-02 20:12:39 +00004800void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004801 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004802}
Jim Laskeybd761842006-02-27 17:27:12 +00004803#endif
Jim Laskey65195462006-10-30 13:35:07 +00004804
4805//===----------------------------------------------------------------------===//
4806/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004807///
Jim Laskey65195462006-10-30 13:35:07 +00004808
Bill Wendling91b8b802009-03-10 20:41:52 +00004809DwarfWriter::DwarfWriter()
Bill Wendling163ba3f2009-03-10 21:23:25 +00004810 : ImmutablePass(&ID), DD(0), DE(0) {}
Jim Laskey65195462006-10-30 13:35:07 +00004811
4812DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004813 delete DE;
4814 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004815}
4816
Jim Laskey65195462006-10-30 13:35:07 +00004817/// BeginModule - Emit all Dwarf sections that should come prior to the
4818/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004819void DwarfWriter::BeginModule(Module *M,
4820 MachineModuleInfo *MMI,
4821 raw_ostream &OS, AsmPrinter *A,
4822 const TargetAsmInfo *T) {
4823 DE = new DwarfException(OS, A, T);
4824 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004825 DE->BeginModule(M);
4826 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00004827 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00004828 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004829}
4830
4831/// EndModule - Emit all Dwarf sections that should come after the content.
4832///
4833void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004834 DE->EndModule();
4835 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004836}
4837
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004838/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004839/// emitted immediately after the function entry point.
4840void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004841 DE->BeginFunction(MF);
4842 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004843}
4844
4845/// EndFunction - Gather and emit post-function debug information.
4846///
Bill Wendlingd751c642008-09-26 00:28:12 +00004847void DwarfWriter::EndFunction(MachineFunction *MF) {
4848 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004849 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004850
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004851 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004852 // Clear function debug information.
4853 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004854}
Devang Patelccca7fe2009-01-12 19:17:34 +00004855
4856/// RecordSourceLine - Records location information and associates it with a
4857/// label. Returns a unique label ID used to generate a label and provide
4858/// correspondence to the source line list.
4859unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00004860 DICompileUnit CU) {
4861 return DD->RecordSourceLine(Line, Col, CU);
Devang Patelccca7fe2009-01-12 19:17:34 +00004862}
4863
4864/// RecordRegionStart - Indicate the start of a region.
4865unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004866 return DD->RecordRegionStart(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004867}
4868
4869/// RecordRegionEnd - Indicate the end of a region.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00004870unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
4871 return DD->RecordRegionEnd(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004872}
4873
4874/// getRecordSourceLineCount - Count source lines.
4875unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004876 return DD->getRecordSourceLineCount();
Devang Patelccca7fe2009-01-12 19:17:34 +00004877}
Devang Patelbb8c5952009-01-13 21:25:00 +00004878
Devang Patelc48c5502009-01-13 21:44:10 +00004879/// RecordVariable - Indicate the declaration of a local variable.
4880///
Devang Patel1be3ecc2009-04-15 00:10:26 +00004881void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
4882 const MachineInstr *MI) {
4883 DD->RecordVariable(GV, FrameIndex, MI);
Devang Patelc48c5502009-01-13 21:44:10 +00004884}
Devang Patelbbdc8202009-01-13 23:54:55 +00004885
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004886/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4887/// be emitted.
4888bool DwarfWriter::ShouldEmitDwarfDebug() const {
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +00004889 return DD && DD->ShouldEmitDwarfDebug();
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004890}
Devang Patel0f7fef32009-04-13 17:02:03 +00004891
Devang Patel1be3ecc2009-04-15 00:10:26 +00004892//// RecordInlinedFnStart - Global variable GV is inlined at the location marked
Devang Patel0f7fef32009-04-13 17:02:03 +00004893//// by LabelID label.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00004894unsigned DwarfWriter::RecordInlinedFnStart(DISubprogram SP, DICompileUnit CU,
4895 unsigned Line, unsigned Col) {
4896 return DD->RecordInlinedFnStart(SP, CU, Line, Col);
Devang Patel0f7fef32009-04-13 17:02:03 +00004897}
4898
Devang Patel1be3ecc2009-04-15 00:10:26 +00004899/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00004900unsigned DwarfWriter::RecordInlinedFnEnd(DISubprogram SP) {
Devang Patel1be3ecc2009-04-15 00:10:26 +00004901 return DD->RecordInlinedFnEnd(SP);
4902}
4903
4904/// RecordVariableScope - Record scope for the variable declared by
4905/// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
4906void DwarfWriter::RecordVariableScope(DIVariable &DV,
4907 const MachineInstr *DeclareMI) {
4908 DD->RecordVariableScope(DV, DeclareMI);
4909}