blob: bc83b8f026dca5bb3ad551d0c31d3b052c33cb42 [file] [log] [blame]
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework --------------------===//
Jim Laskeye5032892005-12-21 19:48:16 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskeye5032892005-12-21 19:48:16 +00007//
8//===----------------------------------------------------------------------===//
9//
Jim Laskey072200c2007-01-29 18:51:14 +000010// This file contains support for writing dwarf info into asm files.
Jim Laskeye5032892005-12-21 19:48:16 +000011//
12//===----------------------------------------------------------------------===//
Jim Laskey3ea0e0e2006-01-27 18:32:41 +000013
Jim Laskeyb2efb852006-01-04 22:28:25 +000014#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskey52060a02006-01-24 00:49:18 +000015#include "llvm/Module.h"
Devang Pateld1ca9252009-01-05 23:03:32 +000016#include "llvm/DerivedTypes.h"
Devang Patelcf3a4482009-01-15 23:41:32 +000017#include "llvm/Constants.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000018#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000021#include "llvm/CodeGen/MachineLocation.h"
Devang Patel08f053f2009-01-05 17:57:47 +000022#include "llvm/Analysis/DebugInfo.h"
Jim Laskey3f09fc22007-02-28 18:38:31 +000023#include "llvm/Support/Debug.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000024#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000025#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000026#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000027#include "llvm/Support/Mangler.h"
Bill Wendling91b8b802009-03-10 20:41:52 +000028#include "llvm/Support/Timer.h"
Owen Andersoncb371882008-08-21 00:14:44 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohman85496362007-09-24 21:32:18 +000030#include "llvm/System/Path.h"
Jim Laskey563321a2006-09-06 18:34:40 +000031#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000033#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000034#include "llvm/Target/TargetFrameInfo.h"
Duncan Sands53c3a332007-05-16 12:12:23 +000035#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000036#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000037#include "llvm/Target/TargetOptions.h"
Evan Chenge3d42322009-02-25 07:04:34 +000038#include "llvm/ADT/DenseMap.h"
39#include "llvm/ADT/FoldingSet.h"
40#include "llvm/ADT/StringExtras.h"
41#include "llvm/ADT/StringMap.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000042#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000043#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000044using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000045using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000046
Devang Pateleb3fc282009-01-08 23:40:34 +000047static RegisterPass<DwarfWriter>
48X("dwarfwriter", "DWARF Information Writer");
49char DwarfWriter::ID = 0;
50
Bill Wendling68edf5f2009-03-10 22:58:53 +000051static TimerGroup &getDwarfTimerGroup() {
52 static TimerGroup DwarfTimerGroup("Dwarf Exception and Debugging");
53 return DwarfTimerGroup;
Bill Wendling91b8b802009-03-10 20:41:52 +000054}
55
Jim Laskey0d086af2006-02-27 12:43:29 +000056namespace llvm {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000057
Jim Laskey65195462006-10-30 13:35:07 +000058//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000059
60/// Configuration values for initial hash set sizes (log2).
61///
Bill Wendlingb9dcef22009-02-03 21:17:20 +000062static const unsigned InitDiesSetSize = 9; // log2(512)
63static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
64static const unsigned InitValuesSetSize = 9; // log2(512)
Jim Laskeyef42a012006-11-02 20:12:39 +000065
66//===----------------------------------------------------------------------===//
67/// Forward declarations.
68///
69class DIE;
70class DIEValue;
71
72//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000073/// DWLabel - Labels are used to track locations in the assembler file.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000074/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
75/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer181b6c92007-08-05 20:06:04 +000076/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +000077class DWLabel {
78public:
Jim Laskeyef42a012006-11-02 20:12:39 +000079 /// Tag - Label category tag. Should always be a staticly declared C string.
80 ///
81 const char *Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000082
Jim Laskeyef42a012006-11-02 20:12:39 +000083 /// Number - Value to make label unique.
84 ///
85 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +000086
87 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000088
Jim Laskeyef42a012006-11-02 20:12:39 +000089 void Profile(FoldingSetNodeID &ID) const {
Evan Chenge3d42322009-02-25 07:04:34 +000090 ID.AddString(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +000091 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +000092 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000093
Jim Laskeyef42a012006-11-02 20:12:39 +000094#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +000095 void print(std::ostream *O) const {
96 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +000097 }
Jim Laskeyef42a012006-11-02 20:12:39 +000098 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +000099 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +0000100 if (Number) O << Number;
101 }
102#endif
Jim Laskeybd761842006-02-27 17:27:12 +0000103};
104
105//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000106/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
107/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000108class DIEAbbrevData {
Jim Laskeyef42a012006-11-02 20:12:39 +0000109 /// Attribute - Dwarf attribute code.
110 ///
111 unsigned Attribute;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000112
Jim Laskeyef42a012006-11-02 20:12:39 +0000113 /// Form - Dwarf form code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000114 ///
115 unsigned Form;
Jim Laskey0d086af2006-02-27 12:43:29 +0000116public:
Bill Wendlingdd446322009-03-10 23:57:09 +0000117 DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000118
Jim Laskeybd761842006-02-27 17:27:12 +0000119 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000120 unsigned getAttribute() const { return Attribute; }
121 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000122
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000123 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000124 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000125 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000126 ID.AddInteger(Attribute);
127 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000128 }
129};
Jim Laskey063e7652006-01-17 17:31:53 +0000130
Jim Laskey0d086af2006-02-27 12:43:29 +0000131//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000132/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
133/// information object.
134class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000135private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000136 /// Tag - Dwarf tag code.
137 ///
138 unsigned Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000139
Jim Laskeyef42a012006-11-02 20:12:39 +0000140 /// Unique number for node.
141 ///
142 unsigned Number;
143
144 /// ChildrenFlag - Dwarf children flag.
145 ///
146 unsigned ChildrenFlag;
147
148 /// Data - Raw data bytes for abbreviation.
149 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000150 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey0d086af2006-02-27 12:43:29 +0000151public:
Bill Wendlingdd446322009-03-10 23:57:09 +0000152 DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
153 virtual ~DIEAbbrev() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000154
Jim Laskeybd761842006-02-27 17:27:12 +0000155 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000156 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000157 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000158 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000159 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000160 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000161 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000162 void setNumber(unsigned N) { Number = N; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000163
Jim Laskey0d086af2006-02-27 12:43:29 +0000164 /// AddAttribute - Adds another set of attribute information to the
165 /// abbreviation.
166 void AddAttribute(unsigned Attribute, unsigned Form) {
167 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000168 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000169
Jim Laskeyb8509c52006-03-23 18:07:55 +0000170 /// AddFirstAttribute - Adds a set of attribute information to the front
171 /// of the abbreviation.
172 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
173 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
174 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000175
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000176 /// Profile - Used to gather unique data for the abbreviation folding set.
177 ///
178 void Profile(FoldingSetNodeID &ID) {
179 ID.AddInteger(Tag);
180 ID.AddInteger(ChildrenFlag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000181
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000182 // For each attribute description.
183 for (unsigned i = 0, N = Data.size(); i < N; ++i)
184 Data[i].Profile(ID);
185 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000186
Jim Laskey0d086af2006-02-27 12:43:29 +0000187 /// Emit - Print the abbreviation using the specified Dwarf writer.
188 ///
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000189 void Emit(const DwarfDebug &DD) const;
190
Jim Laskey0d086af2006-02-27 12:43:29 +0000191#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000192 void print(std::ostream *O) {
193 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000194 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000195 void print(std::ostream &O);
196 void dump();
197#endif
198};
Jim Laskey063e7652006-01-17 17:31:53 +0000199
Jim Laskey0d086af2006-02-27 12:43:29 +0000200//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000201/// DIE - A structured debug information entry. Has an abbreviation which
202/// describes it's organization.
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000203class CompileUnit;
Jim Laskeyef42a012006-11-02 20:12:39 +0000204class DIE : public FoldingSetNode {
205protected:
206 /// Abbrev - Buffer for constructing abbreviation.
207 ///
208 DIEAbbrev Abbrev;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000209
Jim Laskeyef42a012006-11-02 20:12:39 +0000210 /// Offset - Offset in debug info section.
211 ///
212 unsigned Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000213
Jim Laskeyef42a012006-11-02 20:12:39 +0000214 /// Size - Size of instance + children.
215 ///
216 unsigned Size;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000217
Jim Laskeyef42a012006-11-02 20:12:39 +0000218 /// Children DIEs.
219 ///
220 std::vector<DIE *> Children;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000221
Jim Laskeyef42a012006-11-02 20:12:39 +0000222 /// Attributes values.
223 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000224 SmallVector<DIEValue*, 32> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000225
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000226 /// Abstract compile unit.
227 CompileUnit *AbstractCU;
Jim Laskeyef42a012006-11-02 20:12:39 +0000228public:
Dan Gohman81975f62007-08-27 14:50:10 +0000229 explicit DIE(unsigned Tag)
Bill Wendlinga9519572009-05-08 20:38:02 +0000230 : Abbrev(Tag, DW_CHILDREN_no), Offset(0), Size(0) {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000231 virtual ~DIE();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000232
Jim Laskeyef42a012006-11-02 20:12:39 +0000233 // Accessors.
234 DIEAbbrev &getAbbrev() { return Abbrev; }
235 unsigned getAbbrevNumber() const {
236 return Abbrev.getNumber();
237 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000238 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000239 unsigned getOffset() const { return Offset; }
240 unsigned getSize() const { return Size; }
241 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000242 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000243 CompileUnit *getAbstractCompileUnit() const { return AbstractCU; }
244
Jim Laskeyef42a012006-11-02 20:12:39 +0000245 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
246 void setOffset(unsigned O) { Offset = O; }
247 void setSize(unsigned S) { Size = S; }
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +0000248 void setAbstractCompileUnit(CompileUnit *CU) { AbstractCU = CU; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000249
Jim Laskeyef42a012006-11-02 20:12:39 +0000250 /// AddValue - Add a value and attributes to a DIE.
251 ///
252 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
253 Abbrev.AddAttribute(Attribute, Form);
254 Values.push_back(Value);
255 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000256
Jim Laskeyef42a012006-11-02 20:12:39 +0000257 /// SiblingOffset - Return the offset of the debug information entry's
258 /// sibling.
259 unsigned SiblingOffset() const { return Offset + Size; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000260
Jim Laskeyef42a012006-11-02 20:12:39 +0000261 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
262 ///
263 void AddSiblingOffset();
264
265 /// AddChild - Add a child to the DIE.
266 ///
267 void AddChild(DIE *Child) {
268 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
269 Children.push_back(Child);
270 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000271
Jim Laskeyef42a012006-11-02 20:12:39 +0000272 /// Detach - Detaches objects connected to it after copying.
273 ///
274 void Detach() {
275 Children.clear();
276 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000277
Jim Laskeyef42a012006-11-02 20:12:39 +0000278 /// Profile - Used to gather unique data for the value folding set.
279 ///
280 void Profile(FoldingSetNodeID &ID) ;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000281
Jim Laskeyef42a012006-11-02 20:12:39 +0000282#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000283 void print(std::ostream *O, unsigned IncIndent = 0) {
284 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000285 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000286 void print(std::ostream &O, unsigned IncIndent = 0);
287 void dump();
288#endif
289};
290
291//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000292/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000293///
294class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000295public:
296 enum {
297 isInteger,
298 isString,
299 isLabel,
300 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000301 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000302 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000303 isEntry,
304 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000305 };
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000306
Jim Laskeyef42a012006-11-02 20:12:39 +0000307 /// Type - Type of data stored in the value.
308 ///
309 unsigned Type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000310
Bill Wendlingdd446322009-03-10 23:57:09 +0000311 explicit DIEValue(unsigned T) : Type(T) {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000312 virtual ~DIEValue() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000313
Jim Laskeyf6733882006-11-02 21:48:18 +0000314 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000315 unsigned getType() const { return Type; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000316
Jim Laskey0d086af2006-02-27 12:43:29 +0000317 // Implement isa/cast/dyncast.
318 static bool classof(const DIEValue *) { return true; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000319
Jim Laskey0d086af2006-02-27 12:43:29 +0000320 /// EmitValue - Emit value via the Dwarf writer.
321 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000322 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000323
Jim Laskey0d086af2006-02-27 12:43:29 +0000324 /// SizeOf - Return the size of a value in bytes.
325 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000326 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000327
Jim Laskeyef42a012006-11-02 20:12:39 +0000328 /// Profile - Used to gather unique data for the value folding set.
329 ///
330 virtual void Profile(FoldingSetNodeID &ID) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000331
Jim Laskeyef42a012006-11-02 20:12:39 +0000332#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000333 void print(std::ostream *O) {
334 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000335 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000336 virtual void print(std::ostream &O) = 0;
337 void dump();
338#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000339};
Jim Laskey063e7652006-01-17 17:31:53 +0000340
Jim Laskey0d086af2006-02-27 12:43:29 +0000341//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000342/// DWInteger - An integer value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000343///
Jim Laskey0d086af2006-02-27 12:43:29 +0000344class DIEInteger : public DIEValue {
345private:
346 uint64_t Integer;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000347
Jim Laskey0d086af2006-02-27 12:43:29 +0000348public:
Dan Gohman81975f62007-08-27 14:50:10 +0000349 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000350
Jim Laskey0d086af2006-02-27 12:43:29 +0000351 // Implement isa/cast/dyncast.
352 static bool classof(const DIEInteger *) { return true; }
353 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000354
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000355 /// BestForm - Choose the best form for integer.
356 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000357 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
358 if (IsSigned) {
359 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
360 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
361 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
362 } else {
363 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
364 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
365 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
366 }
367 return DW_FORM_data8;
368 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000369
Jim Laskey0d086af2006-02-27 12:43:29 +0000370 /// EmitValue - Emit integer of appropriate size.
371 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000372 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000373
Jim Laskey0d086af2006-02-27 12:43:29 +0000374 /// SizeOf - Determine size of integer value in bytes.
375 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000376 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000377
Jim Laskeyef42a012006-11-02 20:12:39 +0000378 /// Profile - Used to gather unique data for the value folding set.
379 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000380 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000381 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000382 ID.AddInteger(Integer);
383 }
Jim Laskey5496f012006-11-09 14:52:14 +0000384 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000385
Jim Laskeyef42a012006-11-02 20:12:39 +0000386#ifndef NDEBUG
387 virtual void print(std::ostream &O) {
388 O << "Int: " << (int64_t)Integer
389 << " 0x" << std::hex << Integer << std::dec;
390 }
391#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000392};
Jim Laskey063e7652006-01-17 17:31:53 +0000393
Jim Laskey0d086af2006-02-27 12:43:29 +0000394//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000395/// DIEString - A string value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000396///
Jim Laskeyef42a012006-11-02 20:12:39 +0000397class DIEString : public DIEValue {
Bill Wendlingdd446322009-03-10 23:57:09 +0000398 const std::string Str;
Jim Laskeyef42a012006-11-02 20:12:39 +0000399public:
Bill Wendlingdd446322009-03-10 23:57:09 +0000400 explicit DIEString(const std::string &S) : DIEValue(isString), Str(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000401
Jim Laskey0d086af2006-02-27 12:43:29 +0000402 // Implement isa/cast/dyncast.
403 static bool classof(const DIEString *) { return true; }
404 static bool classof(const DIEValue *S) { return S->Type == isString; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000405
Jim Laskey0d086af2006-02-27 12:43:29 +0000406 /// EmitValue - Emit string value.
407 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000408 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000409
Jim Laskey0d086af2006-02-27 12:43:29 +0000410 /// SizeOf - Determine size of string value in bytes.
411 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000412 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Bill Wendlingdd446322009-03-10 23:57:09 +0000413 return Str.size() + sizeof(char); // sizeof('\0');
Jim Laskeyef42a012006-11-02 20:12:39 +0000414 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000415
Jim Laskeyef42a012006-11-02 20:12:39 +0000416 /// Profile - Used to gather unique data for the value folding set.
417 ///
Bill Wendlingdd446322009-03-10 23:57:09 +0000418 static void Profile(FoldingSetNodeID &ID, const std::string &Str) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000419 ID.AddInteger(isString);
Bill Wendlingdd446322009-03-10 23:57:09 +0000420 ID.AddString(Str);
Jim Laskeyef42a012006-11-02 20:12:39 +0000421 }
Bill Wendlingdd446322009-03-10 23:57:09 +0000422 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Str); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000423
Jim Laskeyef42a012006-11-02 20:12:39 +0000424#ifndef NDEBUG
425 virtual void print(std::ostream &O) {
Bill Wendlingdd446322009-03-10 23:57:09 +0000426 O << "Str: \"" << Str << "\"";
Jim Laskeyef42a012006-11-02 20:12:39 +0000427 }
428#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000429};
Jim Laskey063e7652006-01-17 17:31:53 +0000430
Jim Laskey0d086af2006-02-27 12:43:29 +0000431//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000432/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000433//
Jim Laskeyef42a012006-11-02 20:12:39 +0000434class DIEDwarfLabel : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000435 const DWLabel Label;
Bill Wendlingdd446322009-03-10 23:57:09 +0000436public:
Dan Gohman81975f62007-08-27 14:50:10 +0000437 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000438
Jim Laskey0d086af2006-02-27 12:43:29 +0000439 // Implement isa/cast/dyncast.
440 static bool classof(const DIEDwarfLabel *) { return true; }
441 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000442
Jim Laskey0d086af2006-02-27 12:43:29 +0000443 /// EmitValue - Emit label value.
444 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000445 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000446
Jim Laskey0d086af2006-02-27 12:43:29 +0000447 /// SizeOf - Determine size of label value in bytes.
448 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000449 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000450
Jim Laskeyef42a012006-11-02 20:12:39 +0000451 /// Profile - Used to gather unique data for the value folding set.
452 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000453 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000454 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000455 Label.Profile(ID);
456 }
Jim Laskey5496f012006-11-09 14:52:14 +0000457 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000458
Jim Laskeyef42a012006-11-02 20:12:39 +0000459#ifndef NDEBUG
460 virtual void print(std::ostream &O) {
461 O << "Lbl: ";
462 Label.print(O);
463 }
464#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000465};
Jim Laskey063e7652006-01-17 17:31:53 +0000466
Jim Laskey0d086af2006-02-27 12:43:29 +0000467//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000468/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000469//
Jim Laskeyef42a012006-11-02 20:12:39 +0000470class DIEObjectLabel : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000471 const std::string Label;
Bill Wendlingdd446322009-03-10 23:57:09 +0000472public:
Dan Gohman81975f62007-08-27 14:50:10 +0000473 explicit DIEObjectLabel(const std::string &L)
474 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000475
Jim Laskey0d086af2006-02-27 12:43:29 +0000476 // Implement isa/cast/dyncast.
477 static bool classof(const DIEObjectLabel *) { return true; }
478 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000479
Jim Laskey0d086af2006-02-27 12:43:29 +0000480 /// EmitValue - Emit label value.
481 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000482 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000483
Jim Laskey0d086af2006-02-27 12:43:29 +0000484 /// SizeOf - Determine size of label value in bytes.
485 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000486 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000487
Jim Laskeyef42a012006-11-02 20:12:39 +0000488 /// Profile - Used to gather unique data for the value folding set.
489 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000490 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000491 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000492 ID.AddString(Label);
493 }
Evan Chenge3d42322009-02-25 07:04:34 +0000494 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label.c_str()); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000495
496#ifndef NDEBUG
497 virtual void print(std::ostream &O) {
498 O << "Obj: " << Label;
499 }
500#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000501};
Jim Laskey063e7652006-01-17 17:31:53 +0000502
Jim Laskey0d086af2006-02-27 12:43:29 +0000503//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000504/// DIESectionOffset - A section offset DIE.
505//
506class DIESectionOffset : public DIEValue {
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000507 const DWLabel Label;
508 const DWLabel Section;
509 bool IsEH : 1;
510 bool UseSet : 1;
Bill Wendlingdd446322009-03-10 23:57:09 +0000511public:
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000512 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
513 bool isEH = false, bool useSet = true)
Bill Wendlingdd446322009-03-10 23:57:09 +0000514 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
515 IsEH(isEH), UseSet(useSet) {}
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000516
517 // Implement isa/cast/dyncast.
518 static bool classof(const DIESectionOffset *) { return true; }
519 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000520
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000521 /// EmitValue - Emit section offset.
522 ///
523 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000524
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000525 /// SizeOf - Determine size of section offset value in bytes.
526 ///
527 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000528
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000529 /// Profile - Used to gather unique data for the value folding set.
530 ///
531 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
532 const DWLabel &Section) {
533 ID.AddInteger(isSectionOffset);
534 Label.Profile(ID);
535 Section.Profile(ID);
536 // IsEH and UseSet are specific to the Label/Section that we will emit
537 // the offset for; so Label/Section are enough for uniqueness.
538 }
539 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
540
541#ifndef NDEBUG
542 virtual void print(std::ostream &O) {
543 O << "Off: ";
544 Label.print(O);
545 O << "-";
546 Section.print(O);
547 O << "-" << IsEH << "-" << UseSet;
548 }
549#endif
550};
551
552//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000553/// DIEDelta - A simple label difference DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000554///
Jim Laskeyef42a012006-11-02 20:12:39 +0000555class DIEDelta : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000556 const DWLabel LabelHi;
557 const DWLabel LabelLo;
Bill Wendlingdd446322009-03-10 23:57:09 +0000558public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000559 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
Bill Wendlingdd446322009-03-10 23:57:09 +0000560 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000561
Jim Laskey0d086af2006-02-27 12:43:29 +0000562 // Implement isa/cast/dyncast.
563 static bool classof(const DIEDelta *) { return true; }
564 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000565
Jim Laskey0d086af2006-02-27 12:43:29 +0000566 /// EmitValue - Emit delta value.
567 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000568 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000569
Jim Laskey0d086af2006-02-27 12:43:29 +0000570 /// SizeOf - Determine size of delta value in bytes.
571 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000572 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000573
Jim Laskeyef42a012006-11-02 20:12:39 +0000574 /// Profile - Used to gather unique data for the value folding set.
575 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000576 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
577 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000578 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000579 LabelHi.Profile(ID);
580 LabelLo.Profile(ID);
581 }
Jim Laskey5496f012006-11-09 14:52:14 +0000582 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000583
584#ifndef NDEBUG
585 virtual void print(std::ostream &O) {
586 O << "Del: ";
587 LabelHi.print(O);
588 O << "-";
589 LabelLo.print(O);
590 }
591#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000592};
Jim Laskey063e7652006-01-17 17:31:53 +0000593
Jim Laskey0d086af2006-02-27 12:43:29 +0000594//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000595/// DIEntry - A pointer to another debug information entry. An instance of this
596/// class can also be used as a proxy for a debug information entry not yet
597/// defined (ie. types.)
598class DIEntry : public DIEValue {
Jim Laskey0d086af2006-02-27 12:43:29 +0000599 DIE *Entry;
Bill Wendlingdd446322009-03-10 23:57:09 +0000600public:
Dan Gohman81975f62007-08-27 14:50:10 +0000601 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000602
Bill Wendlinga9519572009-05-08 20:38:02 +0000603 DIE *getEntry() const { return Entry; }
Bill Wendlingdd446322009-03-10 23:57:09 +0000604 void setEntry(DIE *E) { Entry = E; }
605
Jim Laskey0d086af2006-02-27 12:43:29 +0000606 // Implement isa/cast/dyncast.
607 static bool classof(const DIEntry *) { return true; }
608 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000609
Jim Laskeyb8509c52006-03-23 18:07:55 +0000610 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000611 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000612 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000613
Jim Laskeyb8509c52006-03-23 18:07:55 +0000614 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000615 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000616 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000617 return sizeof(int32_t);
618 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000619
Jim Laskeyef42a012006-11-02 20:12:39 +0000620 /// Profile - Used to gather unique data for the value folding set.
621 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000622 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
623 ID.AddInteger(isEntry);
624 ID.AddPointer(Entry);
625 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000626 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000627 ID.AddInteger(isEntry);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000628
Jim Laskeyef42a012006-11-02 20:12:39 +0000629 if (Entry) {
630 ID.AddPointer(Entry);
631 } else {
632 ID.AddPointer(this);
633 }
634 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000635
Jim Laskeyef42a012006-11-02 20:12:39 +0000636#ifndef NDEBUG
637 virtual void print(std::ostream &O) {
638 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
639 }
640#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000641};
642
643//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000644/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000645//
Jim Laskeyef42a012006-11-02 20:12:39 +0000646class DIEBlock : public DIEValue, public DIE {
Bill Wendlingdd446322009-03-10 23:57:09 +0000647 unsigned Size; // Size in bytes excluding size header.
Jim Laskeyef42a012006-11-02 20:12:39 +0000648public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000649 DIEBlock()
Bill Wendlingdd446322009-03-10 23:57:09 +0000650 : DIEValue(isBlock), DIE(0), Size(0) {}
651 virtual ~DIEBlock() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000652
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000653 // Implement isa/cast/dyncast.
654 static bool classof(const DIEBlock *) { return true; }
655 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000656
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000657 /// ComputeSize - calculate the size of the block.
658 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000659 unsigned ComputeSize(DwarfDebug &DD);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000660
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000661 /// BestForm - Choose the best form for data.
662 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000663 unsigned BestForm() const {
664 if ((unsigned char)Size == Size) return DW_FORM_block1;
665 if ((unsigned short)Size == Size) return DW_FORM_block2;
666 if ((unsigned int)Size == Size) return DW_FORM_block4;
667 return DW_FORM_block;
668 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000669
670 /// EmitValue - Emit block data.
671 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000672 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000673
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000674 /// SizeOf - Determine size of block data in bytes.
675 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000676 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000677
Jim Laskeyef42a012006-11-02 20:12:39 +0000678 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000679 ///
Reid Spencer97821312006-11-02 23:56:21 +0000680 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000681 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000682 DIE::Profile(ID);
683 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000684
Jim Laskeyef42a012006-11-02 20:12:39 +0000685#ifndef NDEBUG
686 virtual void print(std::ostream &O) {
687 O << "Blk: ";
688 DIE::print(O, 5);
689 }
690#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000691};
692
693//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000694/// CompileUnit - This dwarf writer support class manages information associate
695/// with a source file.
696class CompileUnit {
Jim Laskeyef42a012006-11-02 20:12:39 +0000697 /// ID - File identifier for source.
698 ///
699 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000700
Jim Laskeyef42a012006-11-02 20:12:39 +0000701 /// Die - Compile unit debug information entry.
702 ///
703 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000704
Devang Patelbbdc8202009-01-13 23:54:55 +0000705 /// GVToDieMap - Tracks the mapping of unit level debug informaton
706 /// variables to debug information entries.
Devang Patelc2997f42009-01-20 00:58:55 +0000707 std::map<GlobalVariable *, DIE *> GVToDieMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000708
Devang Patelbbdc8202009-01-13 23:54:55 +0000709 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Jim Laskeyef42a012006-11-02 20:12:39 +0000710 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patelc2997f42009-01-20 00:58:55 +0000711 std::map<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000712
713 /// Globals - A map of globally visible named entities for this unit.
714 ///
Bill Wendling972bbac2009-04-09 21:49:15 +0000715 StringMap<DIE*> Globals;
Jim Laskeyef42a012006-11-02 20:12:39 +0000716
717 /// DiesSet - Used to uniquely define dies within the compile unit.
718 ///
719 FoldingSet<DIE> DiesSet;
Jim Laskey0d086af2006-02-27 12:43:29 +0000720public:
Devang Pateld1ca9252009-01-05 23:03:32 +0000721 CompileUnit(unsigned I, DIE *D)
Devang Patelbbdc8202009-01-13 23:54:55 +0000722 : ID(I), Die(D), GVToDieMap(),
Devang Patel7e55b3a2009-01-17 06:51:37 +0000723 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Pateld1ca9252009-01-05 23:03:32 +0000724 {}
725
Jim Laskeyef42a012006-11-02 20:12:39 +0000726 ~CompileUnit() {
727 delete Die;
Jim Laskeyef42a012006-11-02 20:12:39 +0000728 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000729
Jim Laskeybd761842006-02-27 17:27:12 +0000730 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000731 unsigned getID() const { return ID; }
732 DIE* getDie() const { return Die; }
Bill Wendling972bbac2009-04-09 21:49:15 +0000733 StringMap<DIE*> &getGlobals() { return Globals; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000734
735 /// hasContent - Return true if this compile unit has something to write out.
736 ///
737 bool hasContent() const {
738 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000739 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000740
741 /// AddGlobal - Add a new global entity to the compile unit.
742 ///
743 void AddGlobal(const std::string &Name, DIE *Die) {
744 Globals[Name] = Die;
745 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000746
Jim Laskeyef42a012006-11-02 20:12:39 +0000747 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000748 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000749 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
750 return GVToDieMap[GV];
751 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000752
Jim Laskeyef42a012006-11-02 20:12:39 +0000753 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000754 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000755 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
756 return GVToDIEntryMap[GV];
757 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000758
Jim Laskeyef42a012006-11-02 20:12:39 +0000759 /// AddDie - Adds or interns the DIE to the compile unit.
760 ///
761 DIE *AddDie(DIE &Buffer) {
762 FoldingSetNodeID ID;
763 Buffer.Profile(ID);
764 void *Where;
765 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000766
Jim Laskeyef42a012006-11-02 20:12:39 +0000767 if (!Die) {
768 Die = new DIE(Buffer);
769 DiesSet.InsertNode(Die, Where);
770 this->Die->AddChild(Die);
771 Buffer.Detach();
772 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000773
Jim Laskeyef42a012006-11-02 20:12:39 +0000774 return Die;
775 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000776};
777
Jim Laskey65195462006-10-30 13:35:07 +0000778//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000779/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000780///
Jim Laskey65195462006-10-30 13:35:07 +0000781class Dwarf {
Jim Laskey072200c2007-01-29 18:51:14 +0000782protected:
Jim Laskey65195462006-10-30 13:35:07 +0000783 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000784 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000785 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000786
Jim Laskey65195462006-10-30 13:35:07 +0000787 //
788 /// O - Stream to .s file.
789 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000790 raw_ostream &O;
Jim Laskey65195462006-10-30 13:35:07 +0000791
792 /// Asm - Target of Dwarf emission.
793 ///
794 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000795
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000796 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000797 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000798
Jim Laskey65195462006-10-30 13:35:07 +0000799 /// TD - Target data.
800 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000801
Jim Laskey65195462006-10-30 13:35:07 +0000802 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000803 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000804
Jim Laskey65195462006-10-30 13:35:07 +0000805 /// M - Current module.
806 ///
807 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000808
Jim Laskey65195462006-10-30 13:35:07 +0000809 /// MF - Current machine function.
810 ///
811 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000812
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000813 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000814 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000815 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000816
Jim Laskey65195462006-10-30 13:35:07 +0000817 /// SubprogramCount - The running count of functions being compiled.
818 ///
819 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000820
Chris Lattner9251f132007-09-24 03:35:37 +0000821 /// Flavor - A unique string indicating what dwarf producer this is, used to
822 /// unique labels.
823 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000824
825 unsigned SetCounter;
Owen Andersoncb371882008-08-21 00:14:44 +0000826 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattner9251f132007-09-24 03:35:37 +0000827 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000828 : O(OS)
829 , Asm(A)
830 , TAI(T)
831 , TD(Asm->TM.getTargetData())
832 , RI(Asm->TM.getRegisterInfo())
833 , M(NULL)
834 , MF(NULL)
835 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000836 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000837 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000838 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000839 {
840 }
841
842public:
Jim Laskey072200c2007-01-29 18:51:14 +0000843 //===--------------------------------------------------------------------===//
844 // Accessors.
845 //
Bill Wendlinga6f02fb2009-04-10 00:00:25 +0000846 const AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000847 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000848 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000849 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000850
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000851 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
852 const {
853 if (isInSection && TAI->getDwarfSectionOffsetDirective())
854 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000855 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000856 O << TAI->getData32bitsDirective();
857 else
858 O << TAI->getData64bitsDirective();
859 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000860
Jim Laskeyb82313f2007-02-01 16:31:34 +0000861 /// PrintLabelName - Print label name in form used by Dwarf writer.
862 ///
863 void PrintLabelName(DWLabel Label) const {
864 PrintLabelName(Label.Tag, Label.Number);
865 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000866 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000867 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000868 if (Number) O << Number;
869 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000870
Chris Lattner9251f132007-09-24 03:35:37 +0000871 void PrintLabelName(const char *Tag, unsigned Number,
872 const char *Suffix) const {
873 O << TAI->getPrivateGlobalPrefix() << Tag;
874 if (Number) O << Number;
875 O << Suffix;
876 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000877
Jim Laskeyb82313f2007-02-01 16:31:34 +0000878 /// EmitLabel - Emit location label for internal use by Dwarf.
879 ///
880 void EmitLabel(DWLabel Label) const {
881 EmitLabel(Label.Tag, Label.Number);
882 }
883 void EmitLabel(const char *Tag, unsigned Number) const {
884 PrintLabelName(Tag, Number);
885 O << ":\n";
886 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000887
Jim Laskeyb82313f2007-02-01 16:31:34 +0000888 /// EmitReference - Emit a reference to a label.
889 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000890 void EmitReference(DWLabel Label, bool IsPCRelative = false,
891 bool Force32Bit = false) const {
892 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000893 }
894 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000895 bool IsPCRelative = false, bool Force32Bit = false) const {
896 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000897 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000898
Jim Laskeyb82313f2007-02-01 16:31:34 +0000899 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
900 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000901 void EmitReference(const std::string &Name, bool IsPCRelative = false,
902 bool Force32Bit = false) const {
903 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000904
Jim Laskeyb82313f2007-02-01 16:31:34 +0000905 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000906
Jim Laskeyb82313f2007-02-01 16:31:34 +0000907 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
908 }
909
910 /// EmitDifference - Emit the difference between two labels. Some
911 /// assemblers do not behave with absolute expressions with data directives,
912 /// so there is an option (needsSet) to use an intermediary set expression.
913 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000914 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000915 EmitDifference(LabelHi.Tag, LabelHi.Number,
916 LabelLo.Tag, LabelLo.Number,
917 IsSmall);
918 }
919 void EmitDifference(const char *TagHi, unsigned NumberHi,
920 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000921 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000922 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000923 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000924 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000925 O << ",";
926 PrintLabelName(TagHi, NumberHi);
927 O << "-";
928 PrintLabelName(TagLo, NumberLo);
929 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000930
931 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +0000932 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000933 ++SetCounter;
934 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000935 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000936
Jim Laskeyb82313f2007-02-01 16:31:34 +0000937 PrintLabelName(TagHi, NumberHi);
938 O << "-";
939 PrintLabelName(TagLo, NumberLo);
940 }
941 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000942
943 void EmitSectionOffset(const char* Label, const char* Section,
944 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000945 bool IsSmall = false, bool isEH = false,
946 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000947 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000948 if (isEH)
949 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
950 else
951 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
952
953 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000954 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000955 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000956 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000957 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000958
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000959 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000960 O << "-";
961 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000962 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000963 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000964
965 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000966
Chris Lattner9251f132007-09-24 03:35:37 +0000967 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000968 ++SetCounter;
969 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000970 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000971
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000972 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000973
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000974 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000975 O << "-";
976 PrintLabelName(Section, SectionNumber);
977 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000978 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000979 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000980
Jim Laskeyb82313f2007-02-01 16:31:34 +0000981 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
982 /// frame.
983 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +0000984 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000985 int stackGrowth =
986 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
987 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +0000988 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +0000989 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000990
991 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +0000992 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +0000993 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000994
Jim Laskeyb82313f2007-02-01 16:31:34 +0000995 if (LabelID) {
996 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000997
Jim Laskeyb82313f2007-02-01 16:31:34 +0000998 // Throw out move if the label is invalid.
999 if (!LabelID) continue;
1000 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001001
Jim Laskeyb82313f2007-02-01 16:31:34 +00001002 const MachineLocation &Dst = Move.getDestination();
1003 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001004
Jim Laskeyb82313f2007-02-01 16:31:34 +00001005 // Advance row if new location.
1006 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1007 Asm->EmitInt8(DW_CFA_advance_loc4);
1008 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001009 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1010 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001011
Jim Laskeyb82313f2007-02-01 16:31:34 +00001012 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001013 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001014 IsLocal = true;
1015 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001016
Jim Laskeyb82313f2007-02-01 16:31:34 +00001017 // If advancing cfa.
Dan Gohmand735b802008-10-03 15:45:36 +00001018 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1019 if (!Src.isReg()) {
1020 if (Src.getReg() == MachineLocation::VirtualFP) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001021 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1022 Asm->EOL("DW_CFA_def_cfa_offset");
1023 } else {
1024 Asm->EmitInt8(DW_CFA_def_cfa);
1025 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmand735b802008-10-03 15:45:36 +00001026 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001027 Asm->EOL("Register");
1028 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001029
Jim Laskeyb82313f2007-02-01 16:31:34 +00001030 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001031
Jim Laskeyb82313f2007-02-01 16:31:34 +00001032 Asm->EmitULEB128Bytes(Offset);
1033 Asm->EOL("Offset");
1034 } else {
1035 assert(0 && "Machine move no supported yet.");
1036 }
Dan Gohmand735b802008-10-03 15:45:36 +00001037 } else if (Src.isReg() &&
1038 Src.getReg() == MachineLocation::VirtualFP) {
1039 if (Dst.isReg()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001040 Asm->EmitInt8(DW_CFA_def_cfa_register);
1041 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmand735b802008-10-03 15:45:36 +00001042 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001043 Asm->EOL("Register");
1044 } else {
1045 assert(0 && "Machine move no supported yet.");
1046 }
1047 } else {
Dan Gohmand735b802008-10-03 15:45:36 +00001048 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001049 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001050
Jim Laskeyb82313f2007-02-01 16:31:34 +00001051 if (Offset < 0) {
1052 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1053 Asm->EOL("DW_CFA_offset_extended_sf");
1054 Asm->EmitULEB128Bytes(Reg);
1055 Asm->EOL("Reg");
1056 Asm->EmitSLEB128Bytes(Offset);
1057 Asm->EOL("Offset");
1058 } else if (Reg < 64) {
1059 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng42bf74b2009-03-25 01:47:28 +00001060 if (Asm->isVerbose())
Evan Chengd4114192008-07-09 21:53:02 +00001061 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1062 else
1063 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001064 Asm->EmitULEB128Bytes(Offset);
1065 Asm->EOL("Offset");
1066 } else {
1067 Asm->EmitInt8(DW_CFA_offset_extended);
1068 Asm->EOL("DW_CFA_offset_extended");
1069 Asm->EmitULEB128Bytes(Reg);
1070 Asm->EOL("Reg");
1071 Asm->EmitULEB128Bytes(Offset);
1072 Asm->EOL("Offset");
1073 }
1074 }
1075 }
1076 }
1077
Jim Laskey072200c2007-01-29 18:51:14 +00001078};
1079
1080//===----------------------------------------------------------------------===//
Devang Patelf3ee5142009-01-12 22:54:42 +00001081/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001082///
1083class SrcLineInfo {
1084 unsigned Line; // Source line number.
1085 unsigned Column; // Source column.
1086 unsigned SourceID; // Source ID number.
1087 unsigned LabelID; // Label in code ID number.
1088public:
1089 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001090 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00001091
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001092 // Accessors
1093 unsigned getLine() const { return Line; }
1094 unsigned getColumn() const { return Column; }
1095 unsigned getSourceID() const { return SourceID; }
1096 unsigned getLabelID() const { return LabelID; }
1097};
1098
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001099//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +00001100/// DbgVariable - This class is used to track local variable information.
1101///
1102class DbgVariable {
Devang Patel99ec3532009-01-16 19:28:14 +00001103 DIVariable Var; // Variable Descriptor.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00001104 unsigned FrameIndex; // Variable frame index.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001105public:
Devang Patel99ec3532009-01-16 19:28:14 +00001106 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel7a6e5a32009-01-08 02:33:41 +00001107
1108 // Accessors.
Devang Patel99ec3532009-01-16 19:28:14 +00001109 DIVariable getVariable() const { return Var; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001110 unsigned getFrameIndex() const { return FrameIndex; }
1111};
1112
1113//===----------------------------------------------------------------------===//
1114/// DbgScope - This class is used to track scope information.
1115///
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001116class DbgConcreteScope;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001117class DbgScope {
Devang Patel7a6e5a32009-01-08 02:33:41 +00001118 DbgScope *Parent; // Parent to this scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001119 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001120 // Either subprogram or block.
1121 unsigned StartLabelID; // Label ID of the beginning of scope.
1122 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel7103c6a2009-01-16 18:01:58 +00001123 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel9795da52009-01-10 02:42:49 +00001124 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001125 SmallVector<DbgConcreteScope *, 8> ConcreteInsts;// Concrete insts of funcs.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001126public:
Devang Patel0e5200f2009-01-15 18:25:17 +00001127 DbgScope(DbgScope *P, DIDescriptor D)
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001128 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0) {}
1129 virtual ~DbgScope();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001130
1131 // Accessors.
Devang Patel7103c6a2009-01-16 18:01:58 +00001132 DbgScope *getParent() const { return Parent; }
1133 DIDescriptor getDesc() const { return Desc; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001134 unsigned getStartLabelID() const { return StartLabelID; }
1135 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel9795da52009-01-10 02:42:49 +00001136 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1137 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001138 SmallVector<DbgConcreteScope*,8> &getConcreteInsts() { return ConcreteInsts; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001139 void setStartLabelID(unsigned S) { StartLabelID = S; }
1140 void setEndLabelID(unsigned E) { EndLabelID = E; }
1141
1142 /// AddScope - Add a scope to the scope.
1143 ///
1144 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1145
1146 /// AddVariable - Add a variable to the scope.
1147 ///
1148 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
Devang Patel1be3ecc2009-04-15 00:10:26 +00001149
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001150 /// AddConcreteInst - Add a concrete instance to the scope.
1151 ///
1152 void AddConcreteInst(DbgConcreteScope *C) { ConcreteInsts.push_back(C); }
Bill Wendling3f500d92009-05-06 21:21:34 +00001153
1154#ifndef NDEBUG
1155 void dump() const;
1156#endif
Devang Patel1be3ecc2009-04-15 00:10:26 +00001157};
1158
Bill Wendling3f500d92009-05-06 21:21:34 +00001159#ifndef NDEBUG
1160void DbgScope::dump() const {
1161 static unsigned IndentLevel = 0;
1162 std::string Indent(IndentLevel, ' ');
1163
1164 cerr << Indent; Desc.dump();
1165 cerr << " [" << StartLabelID << ", " << EndLabelID << "]\n";
1166
1167 IndentLevel += 2;
1168
1169 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
1170 if (Scopes[i] != this)
1171 Scopes[i]->dump();
1172
1173 IndentLevel -= 2;
1174}
1175#endif
Devang Patel1be3ecc2009-04-15 00:10:26 +00001176
1177//===----------------------------------------------------------------------===//
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001178/// DbgConcreteScope - This class is used to track a scope that holds concrete
1179/// instance information.
1180///
1181class DbgConcreteScope : public DbgScope {
1182 CompileUnit *Unit;
1183 DIE *Die; // Debug info for this concrete scope.
Devang Patel1be3ecc2009-04-15 00:10:26 +00001184public:
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001185 DbgConcreteScope(DIDescriptor D) : DbgScope(NULL, D) {}
Devang Patel1be3ecc2009-04-15 00:10:26 +00001186
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001187 // Accessors.
1188 DIE *getDie() const { return Die; }
1189 void setDie(DIE *D) { Die = D; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001190};
1191
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001192DbgScope::~DbgScope() {
1193 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
1194 delete Scopes[i];
1195 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
1196 delete Variables[j];
1197 for (unsigned k = 0, O = ConcreteInsts.size(); k < O; ++k)
1198 delete ConcreteInsts[k];
1199}
1200
Devang Patel7a6e5a32009-01-08 02:33:41 +00001201//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001202/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001203///
1204class DwarfDebug : public Dwarf {
Jim Laskey65195462006-10-30 13:35:07 +00001205 //===--------------------------------------------------------------------===//
1206 // Attributes used to construct specific Dwarf sections.
1207 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001208
Evan Chenge3d42322009-02-25 07:04:34 +00001209 /// CompileUnitMap - A map of global variables representing compile units to
1210 /// compile units.
1211 DenseMap<Value *, CompileUnit *> CompileUnitMap;
1212
1213 /// CompileUnits - All the compile units in this module.
1214 ///
1215 SmallVector<CompileUnit *, 8> CompileUnits;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001216
Devang Pateldd9db662009-01-30 18:20:31 +00001217 /// MainCU - Some platform prefers one compile unit per .o file. In such
1218 /// cases, all dies are inserted in MainCU.
1219 CompileUnit *MainCU;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00001220
Jim Laskeyef42a012006-11-02 20:12:39 +00001221 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001222 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001223 FoldingSet<DIEAbbrev> AbbreviationsSet;
1224
1225 /// Abbreviations - A list of all the unique abbreviations in use.
1226 ///
1227 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001228
Evan Chenge3d42322009-02-25 07:04:34 +00001229 /// DirectoryIdMap - Directory name to directory id map.
1230 ///
1231 StringMap<unsigned> DirectoryIdMap;
Devang Patel8526cc02009-01-05 22:35:52 +00001232
Evan Chenge3d42322009-02-25 07:04:34 +00001233 /// DirectoryNames - A list of directory names.
1234 SmallVector<std::string, 8> DirectoryNames;
1235
1236 /// SourceFileIdMap - Source file name to source file id map.
1237 ///
1238 StringMap<unsigned> SourceFileIdMap;
1239
1240 /// SourceFileNames - A list of source file names.
1241 SmallVector<std::string, 8> SourceFileNames;
1242
1243 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
1244 /// id mapped to a unique id.
1245 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
1246
1247 /// SourceIds - Reverse map from source id to directory id + file id pair.
1248 ///
1249 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
Devang Patel8526cc02009-01-05 22:35:52 +00001250
Devang Patel07354e52009-01-16 21:07:53 +00001251 /// Lines - List of of source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001252 std::vector<SrcLineInfo> Lines;
1253
Devang Patel07354e52009-01-16 21:07:53 +00001254 /// ValuesSet - Used to uniquely define values.
1255 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001256 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001257
Jim Laskeyef42a012006-11-02 20:12:39 +00001258 /// Values - A list of all the unique values in use.
1259 ///
1260 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001261
Jim Laskey65195462006-10-30 13:35:07 +00001262 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001263 ///
Jim Laskey65195462006-10-30 13:35:07 +00001264 UniqueVector<std::string> StringPool;
1265
Jim Laskey65195462006-10-30 13:35:07 +00001266 /// SectionMap - Provides a unique id per text section.
1267 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001268 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001269
Jim Laskey65195462006-10-30 13:35:07 +00001270 /// SectionSourceLines - Tracks line numbers per text section.
1271 ///
Devang Patelf3ee5142009-01-12 22:54:42 +00001272 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Jim Laskey65195462006-10-30 13:35:07 +00001273
Jim Laskeybacd3042007-02-21 22:48:45 +00001274 /// didInitial - Flag to indicate if initial emission has been done.
1275 ///
1276 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001277
Jim Laskeybacd3042007-02-21 22:48:45 +00001278 /// shouldEmit - Flag to indicate if debug information should be emitted.
1279 ///
1280 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001281
Devang Patel7d2f9722009-04-15 20:41:31 +00001282 // FunctionDbgScope - Top level scope for the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001283 //
Devang Patel7d2f9722009-04-15 20:41:31 +00001284 DbgScope *FunctionDbgScope;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001285
Bill Wendling163ba3f2009-03-10 21:23:25 +00001286 /// DbgScopeMap - Tracks the scopes in the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001287 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
Bill Wendling163ba3f2009-03-10 21:23:25 +00001288
Bill Wendlingf59d10f2009-05-13 23:55:49 +00001289 /// DbgAbstractScopeMap - Tracks abstract instance scopes in the current
1290 /// function.
1291 DenseMap<GlobalVariable *, DbgScope *> DbgAbstractScopeMap;
1292
1293 /// DbgConcreteScopeMap - Tracks concrete instance scopes in the current
1294 /// function.
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001295 DenseMap<GlobalVariable *,
Bill Wendlingf59d10f2009-05-13 23:55:49 +00001296 SmallVector<DbgScope *, 8> > DbgConcreteScopeMap;
Devang Patel1be3ecc2009-04-15 00:10:26 +00001297
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001298 /// InlineInfo - Keep track of inlined functions and their location. This
1299 /// information is used to populate debug_inlined section.
Devang Patel0f7fef32009-04-13 17:02:03 +00001300 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
1301
Devang Patel1be3ecc2009-04-15 00:10:26 +00001302 /// InlinedVariableScopes - Scopes information for the inlined subroutine
1303 /// variables.
1304 DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
1305
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001306 /// AbstractInstanceRootMap - Map of abstract instance roots of inlined
1307 /// functions. These are subroutine entries that contain a DW_AT_inline
1308 /// attribute.
1309 DenseMap<const GlobalVariable *, DbgScope *> AbstractInstanceRootMap;
1310
1311 /// AbstractInstanceRootList - List of abstract instance roots of inlined
1312 /// functions. These are subroutine entries that contain a DW_AT_inline
1313 /// attribute.
1314 SmallVector<DbgScope *, 32> AbstractInstanceRootList;
1315
1316 /// LexicalScopeStack - A stack of lexical scopes. The top one is the current
1317 /// scope.
1318 SmallVector<DbgScope *, 16> LexicalScopeStack;
1319
Bill Wendlinge688faf2009-05-08 21:03:15 +00001320 /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
1321 /// used when calculating the "origin" of a concrete instance of an inlined
1322 /// function.
1323 DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
1324
Bill Wendling163ba3f2009-03-10 21:23:25 +00001325 /// DebugTimer - Timer for the Dwarf debug writer.
1326 Timer *DebugTimer;
Devang Patel7a6e5a32009-01-08 02:33:41 +00001327
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001328 struct FunctionDebugFrameInfo {
1329 unsigned Number;
1330 std::vector<MachineMove> Moves;
1331
1332 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001333 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001334 };
1335
1336 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001337
Bill Wendling163ba3f2009-03-10 21:23:25 +00001338private:
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001339 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
Bill Wendlingc8615e42009-03-10 21:47:45 +00001340 /// maps to the source id. Source id starts at 1.
1341 std::pair<unsigned, unsigned>
Bill Wendlinge9e960f2009-03-10 21:59:25 +00001342 getSourceDirectoryAndFileIds(unsigned SId) const {
Bill Wendlingc8615e42009-03-10 21:47:45 +00001343 return SourceIds[SId-1];
1344 }
1345
1346 /// getNumSourceDirectories - Return the number of source directories in the
1347 /// debug info.
1348 unsigned getNumSourceDirectories() const {
1349 return DirectoryNames.size();
1350 }
1351
1352 /// getSourceDirectoryName - Return the name of the directory corresponding
1353 /// to the id.
1354 const std::string &getSourceDirectoryName(unsigned Id) const {
1355 return DirectoryNames[Id - 1];
1356 }
1357
1358 /// getSourceFileName - Return the name of the source file corresponding
1359 /// to the id.
1360 const std::string &getSourceFileName(unsigned Id) const {
1361 return SourceFileNames[Id - 1];
1362 }
1363
1364 /// getNumSourceIds - Return the number of unique source ids.
Bill Wendlingc8615e42009-03-10 21:47:45 +00001365 unsigned getNumSourceIds() const {
1366 return SourceIds.size();
1367 }
1368
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001369 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001370 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001371 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1372 // Profile the node so that we can make it unique.
1373 FoldingSetNodeID ID;
1374 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001375
Jim Laskeyef42a012006-11-02 20:12:39 +00001376 // Check the set for priors.
1377 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001378
Jim Laskeyef42a012006-11-02 20:12:39 +00001379 // If it's newly added.
1380 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001381 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001382 Abbreviations.push_back(&Abbrev);
1383 // Assign the vector position + 1 as its number.
1384 Abbrev.setNumber(Abbreviations.size());
1385 } else {
1386 // Assign existing abbreviation number.
1387 Abbrev.setNumber(InSet->getNumber());
1388 }
1389 }
1390
Jim Laskey65195462006-10-30 13:35:07 +00001391 /// NewString - Add a string to the constant pool and returns a label.
1392 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001393 DWLabel NewString(const std::string &String) {
1394 unsigned StringID = StringPool.insert(String);
1395 return DWLabel("string", StringID);
1396 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001397
Jim Laskeyef42a012006-11-02 20:12:39 +00001398 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1399 /// entry.
1400 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1401 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001402
Jim Laskeyef42a012006-11-02 20:12:39 +00001403 if (Entry) {
1404 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001405 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001406 void *Where;
1407 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001408
Jim Laskeyf6733882006-11-02 21:48:18 +00001409 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001410
Jim Laskeyef42a012006-11-02 20:12:39 +00001411 Value = new DIEntry(Entry);
1412 ValuesSet.InsertNode(Value, Where);
1413 } else {
1414 Value = new DIEntry(Entry);
1415 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001416
Jim Laskeyef42a012006-11-02 20:12:39 +00001417 Values.push_back(Value);
1418 return Value;
1419 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001420
Jim Laskeyef42a012006-11-02 20:12:39 +00001421 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1422 ///
1423 void SetDIEntry(DIEntry *Value, DIE *Entry) {
Bill Wendlingdd446322009-03-10 23:57:09 +00001424 Value->setEntry(Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001425 // Add to values set if not already there. If it is, we merely have a
1426 // duplicate in the values list (no harm.)
1427 ValuesSet.GetOrInsertNode(Value);
1428 }
1429
1430 /// AddUInt - Add an unsigned integer attribute data and value.
1431 ///
1432 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1433 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1434
1435 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001436 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001437 void *Where;
1438 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1439 if (!Value) {
1440 Value = new DIEInteger(Integer);
1441 ValuesSet.InsertNode(Value, Where);
1442 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001443 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001444
Jim Laskeyef42a012006-11-02 20:12:39 +00001445 Die->AddValue(Attribute, Form, Value);
1446 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001447
Jim Laskeyef42a012006-11-02 20:12:39 +00001448 /// AddSInt - Add an signed integer attribute data and value.
1449 ///
1450 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1451 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1452
1453 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001454 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001455 void *Where;
1456 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1457 if (!Value) {
1458 Value = new DIEInteger(Integer);
1459 ValuesSet.InsertNode(Value, Where);
1460 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001461 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001462
Jim Laskeyef42a012006-11-02 20:12:39 +00001463 Die->AddValue(Attribute, Form, Value);
1464 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001465
Evan Chenge3d42322009-02-25 07:04:34 +00001466 /// AddString - Add a string attribute data and value.
Jim Laskeyef42a012006-11-02 20:12:39 +00001467 ///
1468 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1469 const std::string &String) {
1470 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001471 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001472 void *Where;
1473 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1474 if (!Value) {
1475 Value = new DIEString(String);
1476 ValuesSet.InsertNode(Value, Where);
1477 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001478 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001479
Jim Laskeyef42a012006-11-02 20:12:39 +00001480 Die->AddValue(Attribute, Form, Value);
1481 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001482
Jim Laskeyef42a012006-11-02 20:12:39 +00001483 /// AddLabel - Add a Dwarf label attribute data and value.
1484 ///
1485 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1486 const DWLabel &Label) {
1487 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001488 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001489 void *Where;
1490 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1491 if (!Value) {
1492 Value = new DIEDwarfLabel(Label);
1493 ValuesSet.InsertNode(Value, Where);
1494 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001495 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001496
Jim Laskeyef42a012006-11-02 20:12:39 +00001497 Die->AddValue(Attribute, Form, Value);
1498 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001499
Jim Laskeyef42a012006-11-02 20:12:39 +00001500 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1501 ///
1502 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1503 const std::string &Label) {
1504 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001505 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001506 void *Where;
1507 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1508 if (!Value) {
1509 Value = new DIEObjectLabel(Label);
1510 ValuesSet.InsertNode(Value, Where);
1511 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001512 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001513
Jim Laskeyef42a012006-11-02 20:12:39 +00001514 Die->AddValue(Attribute, Form, Value);
1515 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001516
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001517 /// AddSectionOffset - Add a section offset label attribute data and value.
1518 ///
1519 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1520 const DWLabel &Label, const DWLabel &Section,
1521 bool isEH = false, bool useSet = true) {
1522 FoldingSetNodeID ID;
1523 DIESectionOffset::Profile(ID, Label, Section);
1524 void *Where;
1525 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1526 if (!Value) {
1527 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1528 ValuesSet.InsertNode(Value, Where);
1529 Values.push_back(Value);
1530 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001531
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001532 Die->AddValue(Attribute, Form, Value);
1533 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001534
Jim Laskeyef42a012006-11-02 20:12:39 +00001535 /// AddDelta - Add a label delta attribute data and value.
1536 ///
1537 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patel1be3ecc2009-04-15 00:10:26 +00001538 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001539 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001540 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001541 void *Where;
1542 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1543 if (!Value) {
1544 Value = new DIEDelta(Hi, Lo);
1545 ValuesSet.InsertNode(Value, Where);
1546 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001547 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001548
Jim Laskeyef42a012006-11-02 20:12:39 +00001549 Die->AddValue(Attribute, Form, Value);
1550 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001551
Jim Laskeyef42a012006-11-02 20:12:39 +00001552 /// AddDIEntry - Add a DIE attribute data and value.
1553 ///
1554 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1555 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1556 }
1557
1558 /// AddBlock - Add block data.
1559 ///
1560 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1561 Block->ComputeSize(*this);
1562 FoldingSetNodeID ID;
1563 Block->Profile(ID);
1564 void *Where;
1565 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1566 if (!Value) {
1567 Value = Block;
1568 ValuesSet.InsertNode(Value, Where);
1569 Values.push_back(Value);
1570 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001571 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001572 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001573 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001574 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001575
Jim Laskeyef42a012006-11-02 20:12:39 +00001576 Die->AddValue(Attribute, Block->BestForm(), Value);
1577 }
1578
Jim Laskey65195462006-10-30 13:35:07 +00001579 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001580 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001581 void AddSourceLine(DIE *Die, const DIVariable *V) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001582 // If there is no compile unit specified, don't add a line #.
1583 if (V->getCompileUnit().isNull())
1584 return;
1585
Devang Patel7a6e5a32009-01-08 02:33:41 +00001586 unsigned Line = V->getLineNumber();
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001587 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID();
1588 assert(FileID && "Invalid file id");
Devang Patel7a6e5a32009-01-08 02:33:41 +00001589 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1590 AddUInt(Die, DW_AT_decl_line, 0, Line);
1591 }
1592
1593 /// AddSourceLine - Add location information to specified debug information
1594 /// entry.
Devang Patel99ec3532009-01-16 19:28:14 +00001595 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001596 // If there is no compile unit specified, don't add a line #.
1597 if (G->getCompileUnit().isNull())
1598 return;
Devang Patel8526cc02009-01-05 22:35:52 +00001599 unsigned Line = G->getLineNumber();
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001600 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID();
1601 assert(FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001602 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1603 AddUInt(Die, DW_AT_decl_line, 0, Line);
1604 }
1605
Devang Patel99ec3532009-01-16 19:28:14 +00001606 void AddSourceLine(DIE *Die, const DIType *Ty) {
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001607 // If there is no compile unit specified, don't add a line #.
Devang Pateldd9db662009-01-30 18:20:31 +00001608 DICompileUnit CU = Ty->getCompileUnit();
1609 if (CU.isNull())
1610 return;
Chris Lattnere3f6cea2009-05-05 04:55:56 +00001611
1612 unsigned Line = Ty->getLineNumber();
1613 unsigned FileID = FindCompileUnit(CU).getID();
1614 assert(FileID && "Invalid file id");
Devang Patel8526cc02009-01-05 22:35:52 +00001615 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1616 AddUInt(Die, DW_AT_decl_line, 0, Line);
1617 }
1618
Jim Laskey65195462006-10-30 13:35:07 +00001619 /// AddAddress - Add an address attribute to a die based on the location
1620 /// provided.
1621 void AddAddress(DIE *Die, unsigned Attribute,
Devang Patel1be3ecc2009-04-15 00:10:26 +00001622 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001623 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001624 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001625
Dan Gohmand735b802008-10-03 15:45:36 +00001626 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001627 if (Reg < 32) {
1628 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1629 } else {
1630 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1631 AddUInt(Block, 0, DW_FORM_udata, Reg);
1632 }
1633 } else {
1634 if (Reg < 32) {
1635 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1636 } else {
1637 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1638 AddUInt(Block, 0, DW_FORM_udata, Reg);
1639 }
1640 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1641 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001642
Jim Laskeyef42a012006-11-02 20:12:39 +00001643 AddBlock(Die, Attribute, 0, Block);
1644 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001645
Jim Laskeyef42a012006-11-02 20:12:39 +00001646 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +00001647 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel80303aa2009-01-23 19:13:31 +00001648 if (Ty.isNull())
Devang Patel48d190f2009-01-05 21:47:57 +00001649 return;
Devang Patel48d190f2009-01-05 21:47:57 +00001650
1651 // Check for pre-existence.
1652 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1653 // If it exists then use the existing value.
1654 if (Slot) {
1655 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1656 return;
1657 }
1658
1659 // Set up proxy.
1660 Slot = NewDIEntry();
1661
1662 // Construct type.
1663 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001664 if (Ty.isBasicType(Ty.getTag()))
1665 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1666 else if (Ty.isDerivedType(Ty.getTag()))
1667 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1668 else {
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001669 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelf193ff02009-01-15 19:26:23 +00001670 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1671 }
1672
Devang Patel7009d242009-01-27 23:22:55 +00001673 // Add debug information entry to entity and appropriate context.
1674 DIE *Die = NULL;
1675 DIDescriptor Context = Ty.getContext();
1676 if (!Context.isNull())
1677 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1678
1679 if (Die) {
1680 DIE *Child = new DIE(Buffer);
1681 Die->AddChild(Child);
1682 Buffer.Detach();
1683 SetDIEntry(Slot, Child);
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001684 } else {
Devang Patel7009d242009-01-27 23:22:55 +00001685 Die = DW_Unit->AddDie(Buffer);
1686 SetDIEntry(Slot, Die);
1687 }
1688
Devang Patel48d190f2009-01-05 21:47:57 +00001689 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1690 }
1691
Devang Patele5202732009-01-05 19:07:53 +00001692 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1693 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001694 DIBasicType BTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001695
Devang Patel08f053f2009-01-05 17:57:47 +00001696 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001697 std::string Name;
1698 BTy.getName(Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001699 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001700 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +00001701 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001702 if (!Name.empty())
Devang Patel08f053f2009-01-05 17:57:47 +00001703 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001704 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +00001705 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1706 }
1707
Devang Patele5202732009-01-05 19:07:53 +00001708 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1709 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001710 DIDerivedType DTy) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001711
Devang Patel08f053f2009-01-05 17:57:47 +00001712 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001713 std::string Name;
1714 DTy.getName(Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001715 uint64_t Size = DTy.getSizeInBits() >> 3;
1716 unsigned Tag = DTy.getTag();
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001717
Devang Patel08f053f2009-01-05 17:57:47 +00001718 // FIXME - Workaround for templates.
1719 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1720
1721 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001722
Devang Patel08f053f2009-01-05 17:57:47 +00001723 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +00001724 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001725 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001726
1727 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001728 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001729 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel08f053f2009-01-05 17:57:47 +00001730
1731 // Add size if non-zero (derived types might be zero-sized.)
1732 if (Size)
1733 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1734
1735 // Add source line info if available and TyDesc is not a forward
1736 // declaration.
Devang Patelad165be2009-01-27 00:45:04 +00001737 if (!DTy.isForwardDecl())
1738 AddSourceLine(&Buffer, &DTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001739 }
1740
Devang Patelf4215332009-01-05 19:55:51 +00001741 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1742 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001743 DICompositeType CTy) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001744 // Get core information.
Bill Wendling0582ae92009-03-13 04:39:26 +00001745 std::string Name;
1746 CTy.getName(Name);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001747
Devang Patelf193ff02009-01-15 19:26:23 +00001748 uint64_t Size = CTy.getSizeInBits() >> 3;
1749 unsigned Tag = CTy.getTag();
Devang Patel49f38cb2009-01-23 01:19:09 +00001750 Buffer.setTag(Tag);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001751
Devang Patelf4215332009-01-05 19:55:51 +00001752 switch (Tag) {
1753 case DW_TAG_vector_type:
1754 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001755 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001756 break;
Devang Pateleab4a2e2009-01-20 18:35:14 +00001757 case DW_TAG_enumeration_type:
1758 {
1759 DIArray Elements = CTy.getTypeArray();
1760 // Add enumerators to enumeration type.
1761 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1762 DIE *ElemDie = NULL;
1763 DIEnumerator Enum(Elements.getElement(i).getGV());
1764 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1765 Buffer.AddChild(ElemDie);
1766 }
1767 }
1768 break;
Devang Patelf4215332009-01-05 19:55:51 +00001769 case DW_TAG_subroutine_type:
1770 {
Devang Patelf4215332009-01-05 19:55:51 +00001771 // Add return type.
Bill Wendling3f500d92009-05-06 21:21:34 +00001772 DIArray Elements = CTy.getTypeArray();
Devang Patel48d190f2009-01-05 21:47:57 +00001773 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +00001774 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +00001775
Bill Wendling3f500d92009-05-06 21:21:34 +00001776 // Add prototype flag.
1777 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1778
Devang Patelf4215332009-01-05 19:55:51 +00001779 // Add arguments.
1780 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1781 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001782 DIDescriptor Ty = Elements.getElement(i);
Devang Patel7ab24502009-01-17 06:57:25 +00001783 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +00001784 Buffer.AddChild(Arg);
1785 }
1786 }
1787 break;
1788 case DW_TAG_structure_type:
1789 case DW_TAG_union_type:
Devang Patel86bda412009-03-25 00:28:40 +00001790 case DW_TAG_class_type:
Devang Patelf4215332009-01-05 19:55:51 +00001791 {
1792 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +00001793 DIArray Elements = CTy.getTypeArray();
Devang Patel153745c2009-01-16 00:50:53 +00001794
1795 // A forward struct declared type may not have elements available.
1796 if (Elements.isNull())
1797 break;
1798
Devang Patelf4215332009-01-05 19:55:51 +00001799 // Add elements to structure type.
1800 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1801 DIDescriptor Element = Elements.getElement(i);
Devang Patel5aac3d32009-01-17 08:01:33 +00001802 DIE *ElemDie = NULL;
Devang Patelf193ff02009-01-15 19:26:23 +00001803 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel2d1768c2009-01-17 08:05:14 +00001804 ElemDie = CreateSubprogramDIE(DW_Unit,
1805 DISubprogram(Element.getGV()));
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00001806 else if (Element.getTag() == dwarf::DW_TAG_variable) // ??
Devang Patel5aac3d32009-01-17 08:01:33 +00001807 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1808 DIGlobalVariable(Element.getGV()));
Devang Patel2be58932009-01-20 21:02:02 +00001809 else
1810 ElemDie = CreateMemberDIE(DW_Unit,
1811 DIDerivedType(Element.getGV()));
Devang Patel2d1768c2009-01-17 08:05:14 +00001812 Buffer.AddChild(ElemDie);
Devang Patelf4215332009-01-05 19:55:51 +00001813 }
Devang Patel13319ce2009-02-17 22:43:44 +00001814 unsigned RLang = CTy.getRunTimeLang();
1815 if (RLang)
1816 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patelf4215332009-01-05 19:55:51 +00001817 }
1818 break;
1819 default:
1820 break;
1821 }
1822
1823 // Add name if not anonymous or intermediate type.
Bill Wendling0582ae92009-03-13 04:39:26 +00001824 if (!Name.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00001825 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf4215332009-01-05 19:55:51 +00001826
Devang Patelad165be2009-01-27 00:45:04 +00001827 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1828 || Tag == DW_TAG_union_type) {
1829 // Add size if non-zero (derived types might be zero-sized.)
1830 if (Size)
1831 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1832 else {
1833 // Add zero size if it is not a forward declaration.
1834 if (CTy.isForwardDecl())
1835 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1836 else
1837 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1838 }
1839
1840 // Add source line info if available.
1841 if (!CTy.isForwardDecl())
1842 AddSourceLine(&Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001843 }
Devang Patelf4215332009-01-05 19:55:51 +00001844 }
1845
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001846 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1847 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelf193ff02009-01-15 19:26:23 +00001848 int64_t L = SR.getLo();
1849 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +00001850 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1851 if (L != H) {
1852 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1853 if (L)
Devang Patel2d1768c2009-01-17 08:05:14 +00001854 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1855 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel68afdc32009-01-05 18:33:01 +00001856 }
1857 Buffer.AddChild(DW_Subrange);
1858 }
1859
1860 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1861 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1862 DICompositeType *CTy) {
1863 Buffer.setTag(DW_TAG_array_type);
1864 if (CTy->getTag() == DW_TAG_vector_type)
1865 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1866
Devang Patelf9235742009-01-28 21:08:20 +00001867 // Emit derived type.
1868 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001869 DIArray Elements = CTy->getTypeArray();
Devang Patel68afdc32009-01-05 18:33:01 +00001870
1871 // Construct an anonymous type for index type.
1872 DIE IdxBuffer(DW_TAG_base_type);
1873 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1874 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1875 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1876
1877 // Add subranges to array type.
1878 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001879 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001880 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1881 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001882 }
1883 }
1884
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001885 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Pateleab4a2e2009-01-20 18:35:14 +00001886 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001887
1888 DIE *Enumerator = new DIE(DW_TAG_enumerator);
Bill Wendling0582ae92009-03-13 04:39:26 +00001889 std::string Name;
1890 ETy->getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001891 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
Devang Patelc69bf2c2009-01-05 18:38:38 +00001892 int64_t Value = ETy->getEnumValue();
1893 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Pateleab4a2e2009-01-20 18:35:14 +00001894 return Enumerator;
Devang Patelc69bf2c2009-01-05 18:38:38 +00001895 }
Devang Patel68afdc32009-01-05 18:33:01 +00001896
Devang Patel5aac3d32009-01-17 08:01:33 +00001897 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendlingb9dcef22009-02-03 21:17:20 +00001898 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patel5aac3d32009-01-17 08:01:33 +00001899 {
1900 DIE *GVDie = new DIE(DW_TAG_variable);
Bill Wendling0582ae92009-03-13 04:39:26 +00001901 std::string Name;
1902 GV.getDisplayName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001903 AddString(GVDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling0582ae92009-03-13 04:39:26 +00001904 std::string LinkageName;
1905 GV.getLinkageName(LinkageName);
1906 if (!LinkageName.empty())
Devang Patel5aac3d32009-01-17 08:01:33 +00001907 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1908 AddType(DW_Unit, GVDie, GV.getType());
1909 if (!GV.isLocalToUnit())
1910 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1911 AddSourceLine(GVDie, &GV);
1912 return GVDie;
Devang Patel86ae1422009-01-05 18:59:44 +00001913 }
1914
Devang Patel2be58932009-01-20 21:02:02 +00001915 /// CreateMemberDIE - Create new member DIE.
1916 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1917 DIE *MemberDie = new DIE(DT.getTag());
Bill Wendling0582ae92009-03-13 04:39:26 +00001918 std::string Name;
1919 DT.getName(Name);
1920 if (!Name.empty())
Devang Patel2be58932009-01-20 21:02:02 +00001921 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1922
1923 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1924
1925 AddSourceLine(MemberDie, &DT);
1926
Devang Patel36375ee2009-02-17 21:23:59 +00001927 uint64_t Size = DT.getSizeInBits();
1928 uint64_t FieldSize = DT.getOriginalTypeSize();
1929
1930 if (Size != FieldSize) {
1931 // Handle bitfield.
1932 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1933 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1934
1935 uint64_t Offset = DT.getOffsetInBits();
1936 uint64_t FieldOffset = Offset;
1937 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1938 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1939 FieldOffset = (HiMark - FieldSize);
1940 Offset -= FieldOffset;
1941 // Maybe we need to work from the other end.
1942 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1943 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1944 }
Devang Patel2be58932009-01-20 21:02:02 +00001945 DIEBlock *Block = new DIEBlock();
1946 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1947 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1948 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1949
Devang Patel47661592009-01-21 00:08:04 +00001950 if (DT.isProtected())
1951 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1952 else if (DT.isPrivate())
1953 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1954
Devang Patel2be58932009-01-20 21:02:02 +00001955 return MemberDie;
1956 }
1957
Devang Patel5aac3d32009-01-17 08:01:33 +00001958 /// CreateSubprogramDIE - Create new DIE using SP.
1959 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Bill Wendling3f500d92009-05-06 21:21:34 +00001960 const DISubprogram &SP,
Devang Patel2d1768c2009-01-17 08:05:14 +00001961 bool IsConstructor = false) {
Devang Patel5aac3d32009-01-17 08:01:33 +00001962 DIE *SPDie = new DIE(DW_TAG_subprogram);
Bill Wendling3f500d92009-05-06 21:21:34 +00001963
Bill Wendling0582ae92009-03-13 04:39:26 +00001964 std::string Name;
1965 SP.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00001966 AddString(SPDie, DW_AT_name, DW_FORM_string, Name);
Bill Wendling3f500d92009-05-06 21:21:34 +00001967
Bill Wendling0582ae92009-03-13 04:39:26 +00001968 std::string LinkageName;
1969 SP.getLinkageName(LinkageName);
Bill Wendling3f500d92009-05-06 21:21:34 +00001970
Bill Wendling0582ae92009-03-13 04:39:26 +00001971 if (!LinkageName.empty())
Bill Wendling3f500d92009-05-06 21:21:34 +00001972 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1973
Devang Patel5aac3d32009-01-17 08:01:33 +00001974 AddSourceLine(SPDie, &SP);
Devang Patel86ae1422009-01-05 18:59:44 +00001975
Devang Patel5aac3d32009-01-17 08:01:33 +00001976 DICompositeType SPTy = SP.getType();
1977 DIArray Args = SPTy.getTypeArray();
Bill Wendling3f500d92009-05-06 21:21:34 +00001978
1979 // Add prototyped tag, if C or ObjC.
1980 unsigned Lang = SP.getCompileUnit().getLanguage();
1981 if (Lang == DW_LANG_C99 || Lang == DW_LANG_C89 || Lang == DW_LANG_ObjC)
1982 AddUInt(SPDie, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patel5aac3d32009-01-17 08:01:33 +00001983
Devang Patel86ae1422009-01-05 18:59:44 +00001984 // Add Return Type.
Devang Patel75b27382009-04-08 22:18:45 +00001985 unsigned SPTag = SPTy.getTag();
Devang Patel9ac08d62009-02-27 18:05:21 +00001986 if (!IsConstructor) {
Devang Patel75b27382009-04-08 22:18:45 +00001987 if (Args.isNull() || SPTag != DW_TAG_subroutine_type)
Devang Patel9ac08d62009-02-27 18:05:21 +00001988 AddType(DW_Unit, SPDie, SPTy);
1989 else
1990 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1991 }
Devang Pateld234e592009-01-30 01:21:46 +00001992
Devang Pateld5863dd2009-02-02 17:51:41 +00001993 if (!SP.isDefinition()) {
1994 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
Bill Wendling3f500d92009-05-06 21:21:34 +00001995 // Add arguments. Do not add arguments for subprogram definition. They
1996 // will be handled through RecordVariable.
Devang Patel75b27382009-04-08 22:18:45 +00001997 if (SPTag == DW_TAG_subroutine_type)
Devang Pateld5863dd2009-02-02 17:51:41 +00001998 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1999 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2000 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
Bill Wendling3f500d92009-05-06 21:21:34 +00002001 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ??
Devang Pateld5863dd2009-02-02 17:51:41 +00002002 SPDie->AddChild(Arg);
2003 }
2004 }
Devang Pateld234e592009-01-30 01:21:46 +00002005
Devang Patelf193ff02009-01-15 19:26:23 +00002006 if (!SP.isLocalToUnit())
Devang Pateld234e592009-01-30 01:21:46 +00002007 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Bill Wendling3f500d92009-05-06 21:21:34 +00002008
Devang Patel1be3ecc2009-04-15 00:10:26 +00002009 // DW_TAG_inlined_subroutine may refer to this DIE.
2010 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getGV());
2011 Slot = SPDie;
Devang Patel5aac3d32009-01-17 08:01:33 +00002012 return SPDie;
Devang Patel86ae1422009-01-05 18:59:44 +00002013 }
2014
Devang Patel5aac3d32009-01-17 08:01:33 +00002015 /// FindCompileUnit - Get the compile unit for the given descriptor.
2016 ///
Chris Lattnere3f6cea2009-05-05 04:55:56 +00002017 CompileUnit &FindCompileUnit(DICompileUnit Unit) const {
2018 DenseMap<Value *, CompileUnit *>::const_iterator I =
2019 CompileUnitMap.find(Unit.getGV());
2020 assert(I != CompileUnitMap.end() && "Missing compile unit.");
2021 return *I->second;
Devang Patel8526cc02009-01-05 22:35:52 +00002022 }
2023
Devang Patelbbdc8202009-01-13 23:54:55 +00002024 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002025 ///
2026 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
2027 // Get the descriptor.
Devang Patel99ec3532009-01-16 19:28:14 +00002028 const DIVariable &VD = DV->getVariable();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002029
2030 // Translate tag to proper Dwarf tag. The result variable is dropped for
2031 // now.
2032 unsigned Tag;
Devang Patel99ec3532009-01-16 19:28:14 +00002033 switch (VD.getTag()) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00002034 case DW_TAG_return_variable: return NULL;
2035 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2036 case DW_TAG_auto_variable: // fall thru
2037 default: Tag = DW_TAG_variable; break;
2038 }
2039
2040 // Define variable debug information entry.
2041 DIE *VariableDie = new DIE(Tag);
Bill Wendling0582ae92009-03-13 04:39:26 +00002042 std::string Name;
2043 VD.getName(Name);
Evan Chenge3d42322009-02-25 07:04:34 +00002044 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002045
2046 // Add source line info if available.
Devang Patel99ec3532009-01-16 19:28:14 +00002047 AddSourceLine(VariableDie, &VD);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002048
2049 // Add variable type.
Devang Patel99ec3532009-01-16 19:28:14 +00002050 AddType(Unit, VariableDie, VD.getType());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002051
2052 // Add variable address.
2053 MachineLocation Location;
2054 Location.set(RI->getFrameRegister(*MF),
2055 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2056 AddAddress(VariableDie, DW_AT_location, Location);
2057
2058 return VariableDie;
2059 }
2060
Devang Patel7a6e5a32009-01-08 02:33:41 +00002061 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2062 ///
2063 DbgScope *getOrCreateScope(GlobalVariable *V) {
2064 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002065 if (Slot) return Slot;
2066
Devang Patel1be3ecc2009-04-15 00:10:26 +00002067 DbgScope *Parent = NULL;
2068 DIBlock Block(V);
Bill Wendling3f500d92009-05-06 21:21:34 +00002069
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002070 // Don't create a new scope if we already created one for an inlined
2071 // function.
2072 DenseMap<const GlobalVariable *, DbgScope *>::iterator
2073 II = AbstractInstanceRootMap.find(V);
2074 if (II != AbstractInstanceRootMap.end())
2075 return LexicalScopeStack.back();
2076
Devang Patel1be3ecc2009-04-15 00:10:26 +00002077 if (!Block.isNull()) {
2078 DIDescriptor ParentDesc = Block.getContext();
2079 Parent =
2080 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002081 }
Bill Wendling3f500d92009-05-06 21:21:34 +00002082
Devang Patel1be3ecc2009-04-15 00:10:26 +00002083 Slot = new DbgScope(Parent, DIDescriptor(V));
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002084
Devang Patel1be3ecc2009-04-15 00:10:26 +00002085 if (Parent)
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002086 Parent->AddScope(Slot);
Devang Patel1be3ecc2009-04-15 00:10:26 +00002087 else
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002088 // First function is top level function.
Devang Patel7d2f9722009-04-15 20:41:31 +00002089 FunctionDbgScope = Slot;
Bill Wendling9a65cfe2009-02-20 20:40:28 +00002090
Devang Patel7a6e5a32009-01-08 02:33:41 +00002091 return Slot;
2092 }
2093
2094 /// ConstructDbgScope - Construct the components of a scope.
2095 ///
2096 void ConstructDbgScope(DbgScope *ParentScope,
2097 unsigned ParentStartID, unsigned ParentEndID,
2098 DIE *ParentDie, CompileUnit *Unit) {
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002099 // Add variables to scope.
2100 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
2101 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2102 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2103 if (VariableDie) ParentDie->AddChild(VariableDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002104 }
2105
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002106 // Add concrete instances to scope.
2107 SmallVector<DbgConcreteScope *, 8> &ConcreteInsts = ParentScope->getConcreteInsts();
2108 for (unsigned i = 0, N = ConcreteInsts.size(); i < N; ++i) {
2109 DbgConcreteScope *ConcreteInst = ConcreteInsts[i];
2110 DIE *Die = ConcreteInst->getDie();
2111
2112 unsigned StartID = ConcreteInst->getStartLabelID();
2113 unsigned EndID = ConcreteInst->getEndLabelID();
2114
2115 // Add the scope bounds.
2116 if (StartID)
2117 AddLabel(Die, DW_AT_low_pc, DW_FORM_addr,
2118 DWLabel("label", StartID));
2119 else
2120 AddLabel(Die, DW_AT_low_pc, DW_FORM_addr,
2121 DWLabel("func_begin", SubprogramCount));
2122
2123 if (EndID)
2124 AddLabel(Die, DW_AT_high_pc, DW_FORM_addr,
2125 DWLabel("label", EndID));
2126 else
2127 AddLabel(Die, DW_AT_high_pc, DW_FORM_addr,
2128 DWLabel("func_end", SubprogramCount));
2129
2130 ParentDie->AddChild(Die);
2131 }
2132
Devang Patel7a6e5a32009-01-08 02:33:41 +00002133 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002134 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002135 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2136 // Define the Scope debug information entry.
2137 DbgScope *Scope = Scopes[j];
Devang Patel7a6e5a32009-01-08 02:33:41 +00002138
Devang Patele9bfb0f2009-01-12 18:41:00 +00002139 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2140 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002141
Devang Patel1be3ecc2009-04-15 00:10:26 +00002142 // Ignore empty scopes.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002143 if (StartID == EndID && StartID != 0) continue;
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002144
2145 // Do not ignore inlined scopes even if they don't have any variables or
2146 // scopes.
2147 if (Scope->getScopes().empty() && Scope->getVariables().empty() &&
2148 Scope->getConcreteInsts().empty())
Devang Patel0f7fef32009-04-13 17:02:03 +00002149 continue;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002150
2151 if (StartID == ParentStartID && EndID == ParentEndID) {
2152 // Just add stuff to the parent scope.
2153 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2154 } else {
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002155 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
Bill Wendling7fa81622009-05-01 08:25:13 +00002156
2157 // Add the scope bounds.
2158 if (StartID)
2159 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2160 DWLabel("label", StartID));
2161 else
2162 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2163 DWLabel("func_begin", SubprogramCount));
2164
2165 if (EndID)
2166 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2167 DWLabel("label", EndID));
2168 else
2169 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2170 DWLabel("func_end", SubprogramCount));
2171
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002172 // Add the scope's contents.
Bill Wendling7fa81622009-05-01 08:25:13 +00002173 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2174 ParentDie->AddChild(ScopeDie);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002175 }
2176 }
2177 }
2178
Devang Patel7d2f9722009-04-15 20:41:31 +00002179 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
Devang Patel7a6e5a32009-01-08 02:33:41 +00002180 ///
Devang Patel7d2f9722009-04-15 20:41:31 +00002181 void ConstructFunctionDbgScope(DbgScope *RootScope) {
Devang Patel7a6e5a32009-01-08 02:33:41 +00002182 // Exit if there is no root scope.
2183 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00002184 DIDescriptor Desc = RootScope->getDesc();
2185 if (Desc.isNull())
2186 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002187
2188 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00002189 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002190
2191 // Get the compile unit context.
Devang Pateldd9db662009-01-30 18:20:31 +00002192 CompileUnit *Unit = MainCU;
2193 if (!Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00002194 Unit = &FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002195
2196 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002197 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002198 assert(SPDie && "Missing subprogram descriptor");
2199
2200 // Add the function bounds.
2201 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2202 DWLabel("func_begin", SubprogramCount));
2203 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2204 DWLabel("func_end", SubprogramCount));
2205 MachineLocation Location(RI->getFrameRegister(*MF));
2206 AddAddress(SPDie, DW_AT_frame_base, Location);
2207
2208 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2209 }
2210
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002211 /// ConstructFunctionDbgScope - Construct the scope for the abstract debug
2212 /// scope.
2213 ///
2214 void ConstructAbstractDbgScope(DbgScope *AbsScope) {
2215 // Exit if there is no root scope.
2216 if (!AbsScope) return;
2217
2218 DIDescriptor Desc = AbsScope->getDesc();
2219 if (Desc.isNull())
2220 return;
2221
2222 // Get the subprogram debug information entry.
2223 DISubprogram SPD(Desc.getGV());
2224
2225 // Get the compile unit context.
2226 CompileUnit *Unit = MainCU;
2227 if (!Unit)
2228 Unit = &FindCompileUnit(SPD.getCompileUnit());
2229
2230 // Get the subprogram die.
2231 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
2232 assert(SPDie && "Missing subprogram descriptor");
2233
2234 ConstructDbgScope(AbsScope, 0, 0, SPDie, Unit);
2235 }
2236
Devang Patel7a6e5a32009-01-08 02:33:41 +00002237 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2238 ///
2239 void ConstructDefaultDbgScope(MachineFunction *MF) {
Evan Chenge3d42322009-02-25 07:04:34 +00002240 const char *FnName = MF->getFunction()->getNameStart();
2241 if (MainCU) {
Bill Wendling972bbac2009-04-09 21:49:15 +00002242 StringMap<DIE*> &Globals = MainCU->getGlobals();
2243 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002244 if (GI != Globals.end()) {
2245 DIE *SPDie = GI->second;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002246
2247 // Add the function bounds.
2248 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2249 DWLabel("func_begin", SubprogramCount));
2250 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2251 DWLabel("func_end", SubprogramCount));
2252
2253 MachineLocation Location(RI->getFrameRegister(*MF));
2254 AddAddress(SPDie, DW_AT_frame_base, Location);
2255 return;
2256 }
Evan Chenge3d42322009-02-25 07:04:34 +00002257 } else {
2258 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2259 CompileUnit *Unit = CompileUnits[i];
Bill Wendling972bbac2009-04-09 21:49:15 +00002260 StringMap<DIE*> &Globals = Unit->getGlobals();
2261 StringMap<DIE*>::iterator GI = Globals.find(FnName);
Evan Chenge3d42322009-02-25 07:04:34 +00002262 if (GI != Globals.end()) {
2263 DIE *SPDie = GI->second;
2264
2265 // Add the function bounds.
2266 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2267 DWLabel("func_begin", SubprogramCount));
2268 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2269 DWLabel("func_end", SubprogramCount));
2270
2271 MachineLocation Location(RI->getFrameRegister(*MF));
2272 AddAddress(SPDie, DW_AT_frame_base, Location);
2273 return;
2274 }
2275 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002276 }
Evan Chenge3d42322009-02-25 07:04:34 +00002277
Devang Patel7a6e5a32009-01-08 02:33:41 +00002278#if 0
2279 // FIXME: This is causing an abort because C++ mangled names are compared
2280 // with their unmangled counterparts. See PR2885. Don't do this assert.
2281 assert(0 && "Couldn't find DIE for machine function!");
2282#endif
2283 }
2284
Jim Laskeyef42a012006-11-02 20:12:39 +00002285 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2286 /// tools to recognize the object file contains Dwarf information.
2287 void EmitInitial() {
2288 // Check to see if we already emitted intial headers.
2289 if (didInitial) return;
2290 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002291
Jim Laskeyef42a012006-11-02 20:12:39 +00002292 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002293 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002294 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002295 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002296 }
2297 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2298 EmitLabel("section_info", 0);
2299 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2300 EmitLabel("section_abbrev", 0);
2301 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2302 EmitLabel("section_aranges", 0);
Scott Michel210de722009-01-26 22:32:51 +00002303 if (TAI->doesSupportMacInfoSection()) {
2304 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2305 EmitLabel("section_macinfo", 0);
2306 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002307 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2308 EmitLabel("section_line", 0);
2309 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2310 EmitLabel("section_loc", 0);
2311 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2312 EmitLabel("section_pubnames", 0);
2313 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2314 EmitLabel("section_str", 0);
2315 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2316 EmitLabel("section_ranges", 0);
2317
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002318 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002319 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002320 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002321 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002322 }
2323
Jim Laskey65195462006-10-30 13:35:07 +00002324 /// EmitDIE - Recusively Emits a debug information entry.
2325 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002326 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002327 // Get the abbreviation for this DIE.
2328 unsigned AbbrevNumber = Die->getAbbrevNumber();
2329 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002330
Jim Laskeybacd3042007-02-21 22:48:45 +00002331 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002332
2333 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002334 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002335
Evan Cheng42bf74b2009-03-25 01:47:28 +00002336 if (Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002337 Asm->EOL(std::string("Abbrev [" +
2338 utostr(AbbrevNumber) +
2339 "] 0x" + utohexstr(Die->getOffset()) +
2340 ":0x" + utohexstr(Die->getSize()) + " " +
2341 TagString(Abbrev->getTag())));
2342 else
2343 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002344
Owen Anderson873e1b52008-06-24 21:44:59 +00002345 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2346 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002347
Jim Laskeyef42a012006-11-02 20:12:39 +00002348 // Emit the DIE attribute values.
2349 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2350 unsigned Attr = AbbrevData[i].getAttribute();
2351 unsigned Form = AbbrevData[i].getForm();
2352 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002353
Jim Laskeyef42a012006-11-02 20:12:39 +00002354 switch (Attr) {
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002355 case DW_AT_sibling:
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002356 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002357 break;
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002358 case DW_AT_abstract_origin: {
2359 DIEntry *E = cast<DIEntry>(Values[i]);
2360 DIE *Origin = E->getEntry();
2361 unsigned Addr =
2362 CompileUnitOffsets[Die->getAbstractCompileUnit()] +
2363 Origin->getOffset();
2364
2365 Asm->EmitInt32(Addr);
2366 break;
Jim Laskeyef42a012006-11-02 20:12:39 +00002367 }
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00002368 default:
Jim Laskeyef42a012006-11-02 20:12:39 +00002369 // Emit an attribute using the defined form.
2370 Values[i]->EmitValue(*this, Form);
2371 break;
2372 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002373
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002374 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002375 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002376
Jim Laskeyef42a012006-11-02 20:12:39 +00002377 // Emit the DIE children if any.
2378 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2379 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002380
Bill Wendlinga9519572009-05-08 20:38:02 +00002381 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Jim Laskeyef42a012006-11-02 20:12:39 +00002382 EmitDIE(Children[j]);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002383
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002384 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002385 }
2386 }
2387
Jim Laskey65195462006-10-30 13:35:07 +00002388 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2389 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002390 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2391 // Get the children.
2392 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002393
Jim Laskeyef42a012006-11-02 20:12:39 +00002394 // If not last sibling and has children then add sibling offset attribute.
2395 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2396
2397 // Record the abbreviation.
2398 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002399
Jim Laskeyef42a012006-11-02 20:12:39 +00002400 // Get the abbreviation for this DIE.
2401 unsigned AbbrevNumber = Die->getAbbrevNumber();
2402 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2403
2404 // Set DIE offset
2405 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002406
Jim Laskeyef42a012006-11-02 20:12:39 +00002407 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002408 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2409
Owen Anderson873e1b52008-06-24 21:44:59 +00002410 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2411 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002412
2413 // Size the DIE attribute values.
2414 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2415 // Size attribute value.
2416 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2417 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002418
Jim Laskeyef42a012006-11-02 20:12:39 +00002419 // Size the DIE children if any.
2420 if (!Children.empty()) {
2421 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2422 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002423
Jim Laskeyef42a012006-11-02 20:12:39 +00002424 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2425 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2426 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002427
Jim Laskeyef42a012006-11-02 20:12:39 +00002428 // End of children marker.
2429 Offset += sizeof(int8_t);
2430 }
2431
2432 Die->setSize(Offset - Die->getOffset());
2433 return Offset;
2434 }
Jim Laskey65195462006-10-30 13:35:07 +00002435
2436 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2437 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002438 void SizeAndOffsets() {
Bill Wendlinge688faf2009-05-08 21:03:15 +00002439 // Compute size of compile unit header.
2440 static unsigned Offset =
2441 sizeof(int32_t) + // Length of Compilation Unit Info
2442 sizeof(int16_t) + // DWARF version number
2443 sizeof(int32_t) + // Offset Into Abbrev. Section
2444 sizeof(int8_t); // Pointer Size (in bytes)
2445
Jim Laskey5496f012006-11-09 14:52:14 +00002446 // Process base compile unit.
Devang Pateldd9db662009-01-30 18:20:31 +00002447 if (MainCU) {
Devang Pateldd9db662009-01-30 18:20:31 +00002448 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
Bill Wendlinge688faf2009-05-08 21:03:15 +00002449 CompileUnitOffsets[MainCU] = 0;
Devang Pateldd9db662009-01-30 18:20:31 +00002450 return;
2451 }
Bill Wendlinge688faf2009-05-08 21:03:15 +00002452
2453 // Process all compile units.
2454 unsigned PrevOffset = 0;
2455
Evan Chenge3d42322009-02-25 07:04:34 +00002456 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i) {
2457 CompileUnit *Unit = CompileUnits[i];
Bill Wendlinge688faf2009-05-08 21:03:15 +00002458 CompileUnitOffsets[Unit] = PrevOffset;
2459 PrevOffset += SizeAndOffsetDie(Unit->getDie(), Offset, true)
2460 + sizeof(int32_t); // FIXME - extra pad for gdb bug.
Devang Patel72b66352009-01-12 23:05:55 +00002461 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002462 }
2463
Evan Chenge3d42322009-02-25 07:04:34 +00002464 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
Jim Laskey65195462006-10-30 13:35:07 +00002465 ///
Evan Chenge3d42322009-02-25 07:04:34 +00002466 void EmitDebugInfoPerCU(CompileUnit *Unit) {
2467 DIE *Die = Unit->getDie();
2468 // Emit the compile units header.
2469 EmitLabel("info_begin", Unit->getID());
2470 // Emit size of content not including length itself
2471 unsigned ContentSize = Die->getSize() +
2472 sizeof(int16_t) + // DWARF version number
2473 sizeof(int32_t) + // Offset Into Abbrev. Section
2474 sizeof(int8_t) + // Pointer Size (in bytes)
2475 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2476
2477 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2478 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2479 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2480 Asm->EOL("Offset Into Abbrev. Section");
2481 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2482
2483 EmitDIE(Die);
2484 // FIXME - extra padding for gdb bug.
2485 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2486 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2487 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2488 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2489 EmitLabel("info_end", Unit->getID());
2490
2491 Asm->EOL();
2492 }
2493
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002494 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002495 // Start debug info section.
2496 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002497
Evan Chenge3d42322009-02-25 07:04:34 +00002498 if (MainCU) {
2499 EmitDebugInfoPerCU(MainCU);
2500 return;
Devang Patel72b66352009-01-12 23:05:55 +00002501 }
Evan Chenge3d42322009-02-25 07:04:34 +00002502
2503 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2504 EmitDebugInfoPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002505 }
2506
Jim Laskey65195462006-10-30 13:35:07 +00002507 /// EmitAbbreviations - Emit the abbreviation section.
2508 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002509 void EmitAbbreviations() const {
2510 // Check to see if it is worth the effort.
2511 if (!Abbreviations.empty()) {
2512 // Start the debug abbrev section.
2513 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002514
Jim Laskeyef42a012006-11-02 20:12:39 +00002515 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002516
Jim Laskeyef42a012006-11-02 20:12:39 +00002517 // For each abbrevation.
2518 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2519 // Get abbreviation data
2520 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002521
Jim Laskeyef42a012006-11-02 20:12:39 +00002522 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002523 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2524 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002525
Jim Laskeyef42a012006-11-02 20:12:39 +00002526 // Emit the abbreviations data.
2527 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002528
Jim Laskeybacd3042007-02-21 22:48:45 +00002529 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002530 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002531
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002532 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002533 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002534
Jim Laskeyef42a012006-11-02 20:12:39 +00002535 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002536
Jim Laskeybacd3042007-02-21 22:48:45 +00002537 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002538 }
2539 }
2540
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002541 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2542 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002543 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002544 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2545 // Define last address of section.
2546 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2547 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2548 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2549 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2550
2551 // Mark end of matrix.
2552 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2553 Asm->EmitULEB128Bytes(1); Asm->EOL();
2554 Asm->EmitInt8(1); Asm->EOL();
2555 }
2556
Jim Laskey65195462006-10-30 13:35:07 +00002557 /// EmitDebugLines - Emit source line information.
2558 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002559 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002560 // If the target is using .loc/.file, the assembler will be emitting the
2561 // .debug_line table automatically.
2562 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002563 return;
2564
Jim Laskeyef42a012006-11-02 20:12:39 +00002565 // Minimum line delta, thus ranging from -10..(255-10).
2566 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2567 // Maximum line delta, thus ranging from -10..(255-10).
2568 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002569
Jim Laskeyef42a012006-11-02 20:12:39 +00002570 // Start the dwarf line section.
2571 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002572
Jim Laskeyef42a012006-11-02 20:12:39 +00002573 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002574
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002575 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002576 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002577 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002578
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002579 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002580
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002581 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002582 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002583 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002584
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002585 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002586
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002587 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002588
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002589 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002590
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002591 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002592
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002593 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002594
Jim Laskeyef42a012006-11-02 20:12:39 +00002595 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002596 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2597 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2598 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2599 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2600 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2601 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2602 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2603 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2604 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002605
Jim Laskeyef42a012006-11-02 20:12:39 +00002606 // Emit directories.
Evan Chenge3d42322009-02-25 07:04:34 +00002607 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2608 Asm->EmitString(getSourceDirectoryName(DI));
2609 Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002610 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002611 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002612
Jim Laskeyef42a012006-11-02 20:12:39 +00002613 // Emit files.
Evan Chenge3d42322009-02-25 07:04:34 +00002614 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2615 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002616 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Evan Chenge3d42322009-02-25 07:04:34 +00002617 Asm->EmitString(getSourceFileName(Id.second));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002618 Asm->EOL("Source");
Evan Chenge3d42322009-02-25 07:04:34 +00002619 Asm->EmitULEB128Bytes(Id.first);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002620 Asm->EOL("Directory #");
2621 Asm->EmitULEB128Bytes(0);
2622 Asm->EOL("Mod date");
2623 Asm->EmitULEB128Bytes(0);
2624 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002625 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002626 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002627
Jim Laskeyef42a012006-11-02 20:12:39 +00002628 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002629
Jim Laskeyef42a012006-11-02 20:12:39 +00002630 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002631 unsigned SecSrcLinesSize = SectionSourceLines.size();
2632
2633 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002634 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002635 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002636
Evan Cheng42bf74b2009-03-25 01:47:28 +00002637 if (Asm->isVerbose()) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002638 const Section* S = SectionMap[j + 1];
Evan Chenge3d42322009-02-25 07:04:34 +00002639 O << '\t' << TAI->getCommentString() << " Section"
2640 << S->getName() << '\n';
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002641 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002642 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002643
2644 // Dwarf assumes we start with first line of first source file.
2645 unsigned Source = 1;
2646 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002647
Jim Laskeyef42a012006-11-02 20:12:39 +00002648 // Construct rows of the address, source, line, column matrix.
2649 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00002650 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002651 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002652 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002653
Evan Cheng42bf74b2009-03-25 01:47:28 +00002654 if (!Asm->isVerbose())
Evan Cheng6547e402008-07-01 23:18:29 +00002655 Asm->EOL();
Evan Chenge3d42322009-02-25 07:04:34 +00002656 else {
2657 std::pair<unsigned, unsigned> SourceID =
Bill Wendlinge9e960f2009-03-10 21:59:25 +00002658 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Evan Chenge3d42322009-02-25 07:04:34 +00002659 O << '\t' << TAI->getCommentString() << ' '
2660 << getSourceDirectoryName(SourceID.first) << ' '
2661 << getSourceFileName(SourceID.second)
2662 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2663 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002664
2665 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002666 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002667 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002668 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002669 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002670
Jim Laskeyef42a012006-11-02 20:12:39 +00002671 // If change of source, then switch to the new source.
2672 if (Source != LineInfo.getSourceID()) {
2673 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002674 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2675 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002676 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002677
Jim Laskeyef42a012006-11-02 20:12:39 +00002678 // If change of line.
2679 if (Line != LineInfo.getLine()) {
2680 // Determine offset.
2681 int Offset = LineInfo.getLine() - Line;
2682 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002683
Jim Laskeyef42a012006-11-02 20:12:39 +00002684 // Update line.
2685 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002686
Jim Laskeyef42a012006-11-02 20:12:39 +00002687 // If delta is small enough and in range...
2688 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2689 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002690 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002691 } else {
2692 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002693 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2694 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2695 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002696 }
2697 } else {
2698 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002699 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002700 }
2701 }
2702
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002703 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002704 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002705
2706 if (SecSrcLinesSize == 0)
2707 // Because we're emitting a debug_line section, we still need a line
2708 // table. The linker and friends expect it to exist. If there's nothing to
2709 // put into it, emit an empty table.
2710 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002711
Jim Laskeyef42a012006-11-02 20:12:39 +00002712 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002713
Jim Laskeybacd3042007-02-21 22:48:45 +00002714 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002715 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002716
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002717 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002718 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002719 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002720 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002721 return;
2722
2723 int stackGrowth =
2724 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2725 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002726 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002727
2728 // Start the dwarf frame section.
2729 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2730
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002731 EmitLabel("debug_frame_common", 0);
2732 EmitDifference("debug_frame_common_end", 0,
2733 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002734 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002735
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002736 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002737 Asm->EmitInt32((int)DW_CIE_ID);
2738 Asm->EOL("CIE Identifier Tag");
2739 Asm->EmitInt8(DW_CIE_VERSION);
2740 Asm->EOL("CIE Version");
2741 Asm->EmitString("");
2742 Asm->EOL("CIE Augmentation");
2743 Asm->EmitULEB128Bytes(1);
2744 Asm->EOL("CIE Code Alignment Factor");
2745 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002746 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002747 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002748 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002749
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002750 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002751 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002752
Dale Johannesenb97aec62007-11-13 19:13:01 +00002753 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002754
Evan Cheng05548eb2008-02-29 19:36:59 +00002755 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002756 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002757
Jim Laskeybacd3042007-02-21 22:48:45 +00002758 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002759 }
2760
Jim Laskey65195462006-10-30 13:35:07 +00002761 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2762 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002763 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002764 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002765 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002766
Jim Laskeyef42a012006-11-02 20:12:39 +00002767 // Start the dwarf frame section.
2768 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002769
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002770 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2771 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002772 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002773
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002774 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002775
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002776 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2777 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002778 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002779
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002780 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002781 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002782 EmitDifference("func_end", DebugFrameInfo.Number,
2783 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002784 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002785
Devang Patel5aac3d32009-01-17 08:01:33 +00002786 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00002787 false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002788
Evan Cheng05548eb2008-02-29 19:36:59 +00002789 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002790 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002791
Jim Laskeybacd3042007-02-21 22:48:45 +00002792 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002793 }
2794
Evan Chenge3d42322009-02-25 07:04:34 +00002795 void EmitDebugPubNamesPerCU(CompileUnit *Unit) {
2796 EmitDifference("pubnames_end", Unit->getID(),
2797 "pubnames_begin", Unit->getID(), true);
2798 Asm->EOL("Length of Public Names Info");
2799
2800 EmitLabel("pubnames_begin", Unit->getID());
2801
2802 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2803
2804 EmitSectionOffset("info_begin", "section_info",
2805 Unit->getID(), 0, true, false);
2806 Asm->EOL("Offset of Compilation Unit Info");
2807
2808 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2809 true);
2810 Asm->EOL("Compilation Unit Length");
2811
Bill Wendling972bbac2009-04-09 21:49:15 +00002812 StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendlingf34be822009-04-09 23:51:31 +00002813 for (StringMap<DIE*>::const_iterator
Bill Wendling972bbac2009-04-09 21:49:15 +00002814 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
Bill Wendlingf34be822009-04-09 23:51:31 +00002815 const char *Name = GI->getKeyData();
Evan Chenge3d42322009-02-25 07:04:34 +00002816 DIE * Entity = GI->second;
2817
2818 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
Bill Wendlingf34be822009-04-09 23:51:31 +00002819 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
Evan Chenge3d42322009-02-25 07:04:34 +00002820 }
2821
2822 Asm->EmitInt32(0); Asm->EOL("End Mark");
2823 EmitLabel("pubnames_end", Unit->getID());
2824
2825 Asm->EOL();
2826 }
2827
Jim Laskeyef42a012006-11-02 20:12:39 +00002828 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002829 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002830 void EmitDebugPubNames() {
2831 // Start the dwarf pubnames section.
2832 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002833
Evan Chenge3d42322009-02-25 07:04:34 +00002834 if (MainCU) {
2835 EmitDebugPubNamesPerCU(MainCU);
2836 return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002837 }
Evan Chenge3d42322009-02-25 07:04:34 +00002838
2839 for (unsigned i = 0, e = CompileUnits.size(); i != e; ++i)
2840 EmitDebugPubNamesPerCU(CompileUnits[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002841 }
2842
2843 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002844 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002845 void EmitDebugStr() {
2846 // Check to see if it is worth the effort.
2847 if (!StringPool.empty()) {
2848 // Start the dwarf str section.
2849 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002850
Jim Laskeyef42a012006-11-02 20:12:39 +00002851 // For each of strings in the string pool.
2852 for (unsigned StringID = 1, N = StringPool.size();
2853 StringID <= N; ++StringID) {
2854 // Emit a label for reference from debug information entries.
2855 EmitLabel("string", StringID);
2856 // Emit the string itself.
2857 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002858 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002859 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002860
Jim Laskeybacd3042007-02-21 22:48:45 +00002861 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002862 }
2863 }
2864
2865 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002866 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002867 void EmitDebugLoc() {
2868 // Start the dwarf loc section.
2869 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002870
Jim Laskeybacd3042007-02-21 22:48:45 +00002871 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002872 }
2873
2874 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002875 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002876 void EmitDebugARanges() {
2877 // Start the dwarf aranges section.
2878 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002879
Jim Laskeyef42a012006-11-02 20:12:39 +00002880 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002881#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002882 CompileUnit *Unit = GetBaseCompileUnit();
2883
Jim Laskey5496f012006-11-09 14:52:14 +00002884 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002885 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002886
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002887 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002888
Jim Laskey5496f012006-11-09 14:52:14 +00002889 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002890 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002891
Dan Gohman82482942007-09-27 23:12:31 +00002892 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002893
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002894 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002895
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002896 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2897 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002898
Jim Laskey5496f012006-11-09 14:52:14 +00002899 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002900 EmitReference("text_begin", 0); Asm->EOL("Address");
2901 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002902
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002903 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2904 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002905#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002906
Jim Laskeybacd3042007-02-21 22:48:45 +00002907 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002908 }
2909
2910 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002911 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002912 void EmitDebugRanges() {
2913 // Start the dwarf ranges section.
2914 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002915
Jim Laskeybacd3042007-02-21 22:48:45 +00002916 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002917 }
2918
2919 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002920 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002921 void EmitDebugMacInfo() {
Scott Michel210de722009-01-26 22:32:51 +00002922 if (TAI->doesSupportMacInfoSection()) {
2923 // Start the dwarf macinfo section.
2924 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002925
Scott Michel210de722009-01-26 22:32:51 +00002926 Asm->EOL();
2927 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002928 }
2929
Devang Patel0f7fef32009-04-13 17:02:03 +00002930 /// EmitDebugInlineInfo - Emit inline info using following format.
2931 /// Section Header:
2932 /// 1. length of section
2933 /// 2. Dwarf version number
2934 /// 3. address size.
2935 ///
2936 /// Entries (one "entry" for each function that was inlined):
2937 ///
2938 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2939 /// otherwise offset into __debug_str for regular function name.
2940 /// 2. offset into __debug_str section for regular function name.
2941 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2942 /// instances for the function.
2943 ///
2944 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2945 /// inlined instance; the die_offset points to the inlined_subroutine die in
2946 /// the __debug_info section, and the low_pc is the starting address for the
2947 /// inlining instance.
2948 void EmitDebugInlineInfo() {
2949 if (!TAI->doesDwarfUsesInlineInfoSection())
2950 return;
2951
2952 if (!MainCU)
2953 return;
2954
2955 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection());
2956 Asm->EOL();
2957 EmitDifference("debug_inlined_end", 1,
2958 "debug_inlined_begin", 1, true);
2959 Asm->EOL("Length of Debug Inlined Information Entry");
2960
2961 EmitLabel("debug_inlined_begin", 1);
2962
2963 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2964 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2965
2966 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
2967 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2968 GlobalVariable *GV = I->first;
2969 SmallVector<unsigned, 4> &Labels = I->second;
2970 DISubprogram SP(GV);
2971 std::string Name;
2972 std::string LName;
Dan Gohman9a38e3e2009-05-07 19:46:24 +00002973
Devang Patel0f7fef32009-04-13 17:02:03 +00002974 SP.getLinkageName(LName);
2975 SP.getName(Name);
2976
2977 Asm->EmitString(LName.empty() ? Name : LName);
2978 Asm->EOL("MIPS linkage name");
2979
2980 Asm->EmitString(Name); Asm->EOL("Function name");
2981
2982 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2983
2984 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(),
2985 LE = Labels.end(); LI != LE; ++LI) {
2986 DIE *SP = MainCU->getDieMapSlotFor(GV);
2987 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2988
2989 if (TD->getPointerSize() == sizeof(int32_t))
2990 O << TAI->getData32bitsDirective();
2991 else
2992 O << TAI->getData64bitsDirective();
2993 PrintLabelName("label", *LI); Asm->EOL("low_pc");
2994 }
2995 }
2996
2997 EmitLabel("debug_inlined_end", 1);
2998 Asm->EOL();
2999 }
3000
Bill Wendlingc8615e42009-03-10 21:47:45 +00003001 /// GetOrCreateSourceID - Look up the source id with the given directory and
3002 /// source file names. If none currently exists, create a new id and insert it
3003 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
3004 /// as well.
3005 unsigned GetOrCreateSourceID(const std::string &DirName,
3006 const std::string &FileName) {
3007 unsigned DId;
3008 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
3009 if (DI != DirectoryIdMap.end()) {
3010 DId = DI->getValue();
3011 } else {
3012 DId = DirectoryNames.size() + 1;
3013 DirectoryIdMap[DirName] = DId;
3014 DirectoryNames.push_back(DirName);
3015 }
3016
3017 unsigned FId;
3018 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
3019 if (FI != SourceFileIdMap.end()) {
3020 FId = FI->getValue();
3021 } else {
3022 FId = SourceFileNames.size() + 1;
3023 SourceFileIdMap[FileName] = FId;
3024 SourceFileNames.push_back(FileName);
3025 }
3026
3027 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
3028 SourceIdMap.find(std::make_pair(DId, FId));
3029 if (SI != SourceIdMap.end())
3030 return SI->second;
3031
3032 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
3033 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
3034 SourceIds.push_back(std::make_pair(DId, FId));
3035
3036 return SrcId;
3037 }
3038
Evan Chenge3d42322009-02-25 07:04:34 +00003039 void ConstructCompileUnit(GlobalVariable *GV) {
3040 DICompileUnit DIUnit(GV);
Bill Wendling0582ae92009-03-13 04:39:26 +00003041 std::string Dir, FN, Prod;
3042 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir),
3043 DIUnit.getFilename(FN));
Evan Chenge3d42322009-02-25 07:04:34 +00003044
3045 DIE *Die = new DIE(DW_TAG_compile_unit);
3046 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
3047 DWLabel("section_line", 0), DWLabel("section_line", 0),
3048 false);
Bill Wendling0582ae92009-03-13 04:39:26 +00003049 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer(Prod));
Evan Chenge3d42322009-02-25 07:04:34 +00003050 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00003051 AddString(Die, DW_AT_name, DW_FORM_string, FN);
Bill Wendling0582ae92009-03-13 04:39:26 +00003052 if (!Dir.empty())
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00003053 AddString(Die, DW_AT_comp_dir, DW_FORM_string, Dir);
Evan Chenge3d42322009-02-25 07:04:34 +00003054 if (DIUnit.isOptimized())
3055 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
Bill Wendling0582ae92009-03-13 04:39:26 +00003056 std::string Flags;
3057 DIUnit.getFlags(Flags);
3058 if (!Flags.empty())
Evan Chenge3d42322009-02-25 07:04:34 +00003059 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
3060 unsigned RVer = DIUnit.getRunTimeVersion();
3061 if (RVer)
3062 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
3063
3064 CompileUnit *Unit = new CompileUnit(ID, Die);
3065 if (DIUnit.isMain()) {
3066 assert(!MainCU && "Multiple main compile units are found!");
3067 MainCU = Unit;
3068 }
3069 CompileUnitMap[DIUnit.getGV()] = Unit;
3070 CompileUnits.push_back(Unit);
3071 }
3072
Devang Patelc4523242009-01-05 23:11:11 +00003073 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00003074 void ConstructCompileUnits() {
Evan Chenge3d42322009-02-25 07:04:34 +00003075 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.compile_units");
3076 if (!Root)
3077 return;
3078 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3079 "Malformed compile unit descriptor anchor type");
3080 Constant *RootC = cast<Constant>(*Root->use_begin());
3081 assert(RootC->hasNUsesOrMore(1) &&
3082 "Malformed compile unit descriptor anchor type");
3083 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3084 UI != UE; ++UI)
3085 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
3086 UUI != UUE; ++UUI) {
3087 GlobalVariable *GV = cast<GlobalVariable>(*UUI);
3088 ConstructCompileUnit(GV);
Devang Pateldd9db662009-01-30 18:20:31 +00003089 }
Evan Chenge3d42322009-02-25 07:04:34 +00003090 }
3091
3092 bool ConstructGlobalVariableDIE(GlobalVariable *GV) {
3093 DIGlobalVariable DI_GV(GV);
3094 CompileUnit *DW_Unit = MainCU;
3095 if (!DW_Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00003096 DW_Unit = &FindCompileUnit(DI_GV.getCompileUnit());
Evan Chenge3d42322009-02-25 07:04:34 +00003097
3098 // Check for pre-existence.
3099 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
3100 if (Slot)
3101 return false;
3102
3103 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
3104
3105 // Add address.
3106 DIEBlock *Block = new DIEBlock();
3107 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Bill Wendling7d16e852009-04-10 00:12:49 +00003108 std::string GLN;
Evan Chenge3d42322009-02-25 07:04:34 +00003109 AddObjectLabel(Block, 0, DW_FORM_udata,
Bill Wendling7d16e852009-04-10 00:12:49 +00003110 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN));
Evan Chenge3d42322009-02-25 07:04:34 +00003111 AddBlock(VariableDie, DW_AT_location, 0, Block);
3112
3113 // Add to map.
3114 Slot = VariableDie;
Bill Wendling3f500d92009-05-06 21:21:34 +00003115
Evan Chenge3d42322009-02-25 07:04:34 +00003116 // Add to context owner.
3117 DW_Unit->getDie()->AddChild(VariableDie);
Bill Wendling3f500d92009-05-06 21:21:34 +00003118
Evan Chenge3d42322009-02-25 07:04:34 +00003119 // Expose as global. FIXME - need to check external flag.
Bill Wendling0582ae92009-03-13 04:39:26 +00003120 std::string Name;
3121 DW_Unit->AddGlobal(DI_GV.getName(Name), VariableDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003122 return true;
Devang Pateld1ca9252009-01-05 23:03:32 +00003123 }
3124
Devang Patelc4523242009-01-05 23:11:11 +00003125 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patel53cac182009-02-24 00:02:15 +00003126 /// visible global variables. Return true if at least one global DIE is
3127 /// created.
3128 bool ConstructGlobalVariableDIEs() {
Evan Chenge3d42322009-02-25 07:04:34 +00003129 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.global_variables");
3130 if (!Root)
3131 return false;
Devang Patelc4523242009-01-05 23:11:11 +00003132
Evan Chenge3d42322009-02-25 07:04:34 +00003133 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3134 "Malformed global variable descriptor anchor type");
3135 Constant *RootC = cast<Constant>(*Root->use_begin());
3136 assert(RootC->hasNUsesOrMore(1) &&
3137 "Malformed global variable descriptor anchor type");
Devang Patelc4523242009-01-05 23:11:11 +00003138
Evan Chenge3d42322009-02-25 07:04:34 +00003139 bool Result = false;
3140 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3141 UI != UE; ++UI)
3142 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling3f500d92009-05-06 21:21:34 +00003143 UUI != UUE; ++UUI)
3144 Result |= ConstructGlobalVariableDIE(cast<GlobalVariable>(*UUI));
3145
Evan Chenge3d42322009-02-25 07:04:34 +00003146 return Result;
3147 }
Devang Patelc4523242009-01-05 23:11:11 +00003148
Evan Chenge3d42322009-02-25 07:04:34 +00003149 bool ConstructSubprogram(GlobalVariable *GV) {
3150 DISubprogram SP(GV);
3151 CompileUnit *Unit = MainCU;
3152 if (!Unit)
Chris Lattnere3f6cea2009-05-05 04:55:56 +00003153 Unit = &FindCompileUnit(SP.getCompileUnit());
Devang Patelc4523242009-01-05 23:11:11 +00003154
Evan Chenge3d42322009-02-25 07:04:34 +00003155 // Check for pre-existence.
3156 DIE *&Slot = Unit->getDieMapSlotFor(GV);
3157 if (Slot)
3158 return false;
3159
3160 if (!SP.isDefinition())
3161 // This is a method declaration which will be handled while
3162 // constructing class type.
3163 return false;
3164
3165 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
3166
3167 // Add to map.
3168 Slot = SubprogramDie;
3169 // Add to context owner.
3170 Unit->getDie()->AddChild(SubprogramDie);
3171 // Expose as global.
Bill Wendling0582ae92009-03-13 04:39:26 +00003172 std::string Name;
3173 Unit->AddGlobal(SP.getName(Name), SubprogramDie);
Evan Chenge3d42322009-02-25 07:04:34 +00003174 return true;
Devang Patelc4523242009-01-05 23:11:11 +00003175 }
3176
Devang Patel78eb6ad2009-01-05 23:21:35 +00003177 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patel53cac182009-02-24 00:02:15 +00003178 /// subprograms. Return true if at least one subprogram DIE is created.
3179 bool ConstructSubprograms() {
Evan Chenge3d42322009-02-25 07:04:34 +00003180 GlobalVariable *Root = M->getGlobalVariable("llvm.dbg.subprograms");
3181 if (!Root)
3182 return false;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003183
Evan Chenge3d42322009-02-25 07:04:34 +00003184 assert(Root->hasLinkOnceLinkage() && Root->hasOneUse() &&
3185 "Malformed subprogram descriptor anchor type");
3186 Constant *RootC = cast<Constant>(*Root->use_begin());
3187 assert(RootC->hasNUsesOrMore(1) &&
3188 "Malformed subprogram descriptor anchor type");
Devang Patel78eb6ad2009-01-05 23:21:35 +00003189
Evan Chenge3d42322009-02-25 07:04:34 +00003190 bool Result = false;
3191 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
3192 UI != UE; ++UI)
3193 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
Bill Wendling3f500d92009-05-06 21:21:34 +00003194 UUI != UUE; ++UUI)
3195 Result |= ConstructSubprogram(cast<GlobalVariable>(*UUI));
3196
Evan Chenge3d42322009-02-25 07:04:34 +00003197 return Result;
Devang Patel78eb6ad2009-01-05 23:21:35 +00003198 }
3199
Jim Laskey65195462006-10-30 13:35:07 +00003200public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003201 //===--------------------------------------------------------------------===//
3202 // Main entry points.
3203 //
Owen Andersoncb371882008-08-21 00:14:44 +00003204 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00003205 : Dwarf(OS, A, T, "dbg"), MainCU(0),
3206 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
3207 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(),
3208 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel7d2f9722009-04-15 20:41:31 +00003209 FunctionDbgScope(0), DebugTimer(0) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003210 if (TimePassesIsEnabled)
3211 DebugTimer = new Timer("Dwarf Debug Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00003212 getDwarfTimerGroup());
Jim Laskeyef42a012006-11-02 20:12:39 +00003213 }
Jim Laskey072200c2007-01-29 18:51:14 +00003214 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003215 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3216 delete Values[j];
Bill Wendling163ba3f2009-03-10 21:23:25 +00003217
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003218 for (DenseMap<const GlobalVariable *, DbgScope *>::iterator
3219 I = AbstractInstanceRootMap.begin(),
3220 E = AbstractInstanceRootMap.end(); I != E;++I)
3221 delete I->second;
3222
Bill Wendling163ba3f2009-03-10 21:23:25 +00003223 delete DebugTimer;
Jim Laskeyef42a012006-11-02 20:12:39 +00003224 }
3225
Bill Wendlingc8615e42009-03-10 21:47:45 +00003226 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
3227 /// be emitted.
3228 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
3229
Devang Patel5d315982009-01-06 21:07:30 +00003230 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3231 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00003232 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003233 if (TimePassesIsEnabled)
3234 DebugTimer->startTimer();
3235
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003236 // Create all the compile unit DIEs.
3237 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00003238
Bill Wendling163ba3f2009-03-10 21:23:25 +00003239 if (CompileUnits.empty()) {
3240 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003241 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003242
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003243 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003244 }
Devang Patel3f7833a2009-01-12 23:09:42 +00003245
Devang Patel53cac182009-02-24 00:02:15 +00003246 // Create DIEs for each of the externally visible global variables.
3247 bool globalDIEs = ConstructGlobalVariableDIEs();
3248
3249 // Create DIEs for each of the externally visible subprograms.
3250 bool subprogramDIEs = ConstructSubprograms();
3251
3252 // If there is not any debug info available for any global variables
3253 // and any subprograms then there is not any debug info to emit.
Bill Wendling163ba3f2009-03-10 21:23:25 +00003254 if (!globalDIEs && !subprogramDIEs) {
3255 if (TimePassesIsEnabled)
Bill Wendling7b697202009-03-10 22:02:13 +00003256 DebugTimer->stopTimer();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003257
Devang Patel53cac182009-02-24 00:02:15 +00003258 return;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003259 }
Devang Patel53cac182009-02-24 00:02:15 +00003260
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003261 MMI = mmi;
3262 shouldEmit = true;
3263 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00003264
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003265 // Prime section data.
3266 SectionMap.insert(TAI->getTextSection());
Devang Patel5d315982009-01-06 21:07:30 +00003267
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003268 // Print out .file directives to specify files for .loc directives. These
3269 // are printed out early so that they precede any .loc directives.
3270 if (TAI->hasDotLocAndDotFile()) {
Evan Chenge3d42322009-02-25 07:04:34 +00003271 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
3272 // Remember source id starts at 1.
Bill Wendlinge9e960f2009-03-10 21:59:25 +00003273 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Evan Chenge3d42322009-02-25 07:04:34 +00003274 sys::Path FullPath(getSourceDirectoryName(Id.first));
3275 bool AppendOk =
3276 FullPath.appendComponent(getSourceFileName(Id.second));
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003277 assert(AppendOk && "Could not append filename to directory!");
3278 AppendOk = false;
3279 Asm->EmitFile(i, FullPath.toString());
3280 Asm->EOL();
Devang Patel5d315982009-01-06 21:07:30 +00003281 }
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003282 }
Devang Patel5d315982009-01-06 21:07:30 +00003283
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003284 // Emit initial sections
3285 EmitInitial();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003286
3287 if (TimePassesIsEnabled)
3288 DebugTimer->stopTimer();
Devang Patel5d315982009-01-06 21:07:30 +00003289 }
3290
Jim Laskey65195462006-10-30 13:35:07 +00003291 /// BeginModule - Emit all Dwarf sections that should come prior to the
3292 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003293 void BeginModule(Module *M) {
3294 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003295 }
3296
Jim Laskey65195462006-10-30 13:35:07 +00003297 /// EndModule - Emit all Dwarf sections that should come after the content.
3298 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003299 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003300 if (!ShouldEmitDwarfDebug())
3301 return;
3302
3303 if (TimePassesIsEnabled)
3304 DebugTimer->startTimer();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003305
Jim Laskeyef42a012006-11-02 20:12:39 +00003306 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003307 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003308 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003309 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003310 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003311
Jim Laskeyef42a012006-11-02 20:12:39 +00003312 // End text sections.
3313 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003314 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003315 EmitLabel("section_end", i);
3316 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003317
3318 // Emit common frame information.
3319 EmitCommonDebugFrame();
3320
3321 // Emit function debug frame information
3322 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3323 E = DebugFrames.end(); I != E; ++I)
3324 EmitFunctionDebugFrame(*I);
3325
Jim Laskeyef42a012006-11-02 20:12:39 +00003326 // Compute DIE offsets and sizes.
3327 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003328
Jim Laskeyef42a012006-11-02 20:12:39 +00003329 // Emit all the DIEs into a debug info section
3330 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003331
Jim Laskeyef42a012006-11-02 20:12:39 +00003332 // Corresponding abbreviations into a abbrev section.
3333 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003334
Jim Laskeyef42a012006-11-02 20:12:39 +00003335 // Emit source line correspondence into a debug line section.
3336 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003337
Jim Laskeyef42a012006-11-02 20:12:39 +00003338 // Emit info into a debug pubnames section.
3339 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003340
Jim Laskeyef42a012006-11-02 20:12:39 +00003341 // Emit info into a debug str section.
3342 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003343
Jim Laskeyef42a012006-11-02 20:12:39 +00003344 // Emit info into a debug loc section.
3345 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003346
Jim Laskeyef42a012006-11-02 20:12:39 +00003347 // Emit info into a debug aranges section.
3348 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003349
Jim Laskeyef42a012006-11-02 20:12:39 +00003350 // Emit info into a debug ranges section.
3351 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003352
Jim Laskeyef42a012006-11-02 20:12:39 +00003353 // Emit info into a debug macinfo section.
3354 EmitDebugMacInfo();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003355
Devang Patel0f7fef32009-04-13 17:02:03 +00003356 // Emit inline info.
3357 EmitDebugInlineInfo();
3358
Bill Wendling163ba3f2009-03-10 21:23:25 +00003359 if (TimePassesIsEnabled)
3360 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003361 }
3362
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003363 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003364 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003365 void BeginFunction(MachineFunction *MF) {
Bill Wendling1362f972009-03-11 00:03:50 +00003366 this->MF = MF;
3367
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003368 if (!ShouldEmitDwarfDebug()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003369
Bill Wendling163ba3f2009-03-10 21:23:25 +00003370 if (TimePassesIsEnabled)
3371 DebugTimer->startTimer();
3372
Jim Laskeyef42a012006-11-02 20:12:39 +00003373 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003374 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003375
Jim Laskeyef42a012006-11-02 20:12:39 +00003376 // Assumes in correct section after the entry point.
3377 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003378
Argyrios Kyrtzidis28f14122009-05-04 19:23:45 +00003379 // Emit label for the implicitly defined dbg.stoppoint at the start of
3380 // the function.
Mike Stumpfe095f32009-05-04 18:40:41 +00003381 DebugLoc FDL = MF->getDefaultDebugLoc();
3382 if (!FDL.isUnknown()) {
3383 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
3384 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col,
3385 DICompileUnit(DLT.CompileUnit));
3386 Asm->printLabel(LabelID);
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003387 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003388
3389 if (TimePassesIsEnabled)
3390 DebugTimer->stopTimer();
Jim Laskeyef42a012006-11-02 20:12:39 +00003391 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003392
Jim Laskey65195462006-10-30 13:35:07 +00003393 /// EndFunction - Gather and emit post-function debug information.
3394 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003395 void EndFunction(MachineFunction *MF) {
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00003396 if (!ShouldEmitDwarfDebug()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003397
Bill Wendling163ba3f2009-03-10 21:23:25 +00003398 if (TimePassesIsEnabled)
3399 DebugTimer->startTimer();
3400
Jim Laskeyb82313f2007-02-01 16:31:34 +00003401 // Define end label for subprogram.
3402 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003403
Jim Laskeyef42a012006-11-02 20:12:39 +00003404 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00003405 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003406 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003407 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003408 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00003409 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00003410 // Append the function info to section info.
3411 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00003412 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00003413 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003414
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003415 // Construct the DbgScope for abstract instances.
3416 for (SmallVector<DbgScope *, 32>::iterator
3417 I = AbstractInstanceRootList.begin(),
3418 E = AbstractInstanceRootList.end(); I != E; ++I)
3419 ConstructAbstractDbgScope(*I);
3420
Jim Laskeyef42a012006-11-02 20:12:39 +00003421 // Construct scopes for subprogram.
Devang Patel7d2f9722009-04-15 20:41:31 +00003422 if (FunctionDbgScope)
3423 ConstructFunctionDbgScope(FunctionDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00003424 else
3425 // FIXME: This is wrong. We are essentially getting past a problem with
3426 // debug information not being able to handle unreachable blocks that have
3427 // debug information in them. In particular, those unreachable blocks that
3428 // have "region end" info in them. That situation results in the "root
3429 // scope" not being created. If that's the case, then emit a "default"
3430 // scope, i.e., one that encompasses the whole function. This isn't
3431 // desirable. And a better way of handling this (and all of the debugging
3432 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003433 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003434
3435 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3436 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003437
3438 // Clear debug info
Devang Patel7d2f9722009-04-15 20:41:31 +00003439 if (FunctionDbgScope) {
3440 delete FunctionDbgScope;
Devang Patel481ff5b2009-01-12 18:48:36 +00003441 DbgScopeMap.clear();
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003442 DbgAbstractScopeMap.clear();
3443 DbgConcreteScopeMap.clear();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003444 InlinedVariableScopes.clear();
Devang Patel7d2f9722009-04-15 20:41:31 +00003445 FunctionDbgScope = NULL;
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003446 LexicalScopeStack.clear();
3447 AbstractInstanceRootList.clear();
Devang Patel481ff5b2009-01-12 18:48:36 +00003448 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003449
Bill Wendling163ba3f2009-03-10 21:23:25 +00003450 Lines.clear();
3451
3452 if (TimePassesIsEnabled)
3453 DebugTimer->stopTimer();
3454 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003455
3456 /// RecordSourceLine - Records location information and associates it with a
3457 /// label. Returns a unique label ID used to generate a label and provide
3458 /// correspondence to the source line list.
3459 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003460 if (TimePassesIsEnabled)
3461 DebugTimer->startTimer();
3462
Evan Chenge3d42322009-02-25 07:04:34 +00003463 CompileUnit *Unit = CompileUnitMap[V];
Bill Wendlingef7e18e2009-02-03 21:38:21 +00003464 assert(Unit && "Unable to find CompileUnit");
Devang Patelccca7fe2009-01-12 19:17:34 +00003465 unsigned ID = MMI->NextLabelID();
3466 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003467
3468 if (TimePassesIsEnabled)
3469 DebugTimer->stopTimer();
3470
Devang Patelccca7fe2009-01-12 19:17:34 +00003471 return ID;
3472 }
3473
3474 /// RecordSourceLine - Records location information and associates it with a
3475 /// label. Returns a unique label ID used to generate a label and provide
3476 /// correspondence to the source line list.
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00003477 unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003478 if (TimePassesIsEnabled)
3479 DebugTimer->startTimer();
3480
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00003481 std::string Dir, Fn;
3482 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir),
3483 CU.getFilename(Fn));
Devang Patelccca7fe2009-01-12 19:17:34 +00003484 unsigned ID = MMI->NextLabelID();
3485 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
Bill Wendling163ba3f2009-03-10 21:23:25 +00003486
3487 if (TimePassesIsEnabled)
3488 DebugTimer->stopTimer();
3489
Devang Patelccca7fe2009-01-12 19:17:34 +00003490 return ID;
3491 }
3492
Bill Wendlingc8615e42009-03-10 21:47:45 +00003493 /// getRecordSourceLineCount - Return the number of source lines in the debug
3494 /// info.
3495 unsigned getRecordSourceLineCount() const {
Devang Patelccca7fe2009-01-12 19:17:34 +00003496 return Lines.size();
3497 }
3498
Bill Wendlingc8615e42009-03-10 21:47:45 +00003499 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
3500 /// timed. Look up the source id with the given directory and source file
3501 /// names. If none currently exists, create a new id and insert it in the
3502 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
3503 /// well.
Evan Chenge3d42322009-02-25 07:04:34 +00003504 unsigned getOrCreateSourceID(const std::string &DirName,
3505 const std::string &FileName) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003506 if (TimePassesIsEnabled)
3507 DebugTimer->startTimer();
3508
Bill Wendlingc8615e42009-03-10 21:47:45 +00003509 unsigned SrcId = GetOrCreateSourceID(DirName, FileName);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003510
3511 if (TimePassesIsEnabled)
3512 DebugTimer->stopTimer();
3513
Evan Chenge3d42322009-02-25 07:04:34 +00003514 return SrcId;
Devang Patelccca7fe2009-01-12 19:17:34 +00003515 }
3516
3517 /// RecordRegionStart - Indicate the start of a region.
Devang Patelccca7fe2009-01-12 19:17:34 +00003518 unsigned RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003519 if (TimePassesIsEnabled)
3520 DebugTimer->startTimer();
3521
Devang Patelccca7fe2009-01-12 19:17:34 +00003522 DbgScope *Scope = getOrCreateScope(V);
3523 unsigned ID = MMI->NextLabelID();
3524 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003525 LexicalScopeStack.push_back(Scope);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003526
3527 if (TimePassesIsEnabled)
3528 DebugTimer->stopTimer();
3529
Devang Patelccca7fe2009-01-12 19:17:34 +00003530 return ID;
3531 }
3532
3533 /// RecordRegionEnd - Indicate the end of a region.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003534 unsigned RecordRegionEnd(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003535 if (TimePassesIsEnabled)
3536 DebugTimer->startTimer();
3537
Bill Wendling5b8479c2009-05-07 17:26:14 +00003538 DbgScope *Scope = getOrCreateScope(V);
Dan Gohman9a38e3e2009-05-07 19:46:24 +00003539 unsigned ID = MMI->NextLabelID();
Devang Patelccca7fe2009-01-12 19:17:34 +00003540 Scope->setEndLabelID(ID);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003541 if (LexicalScopeStack.size() != 0)
3542 LexicalScopeStack.pop_back();
Bill Wendling163ba3f2009-03-10 21:23:25 +00003543
3544 if (TimePassesIsEnabled)
3545 DebugTimer->stopTimer();
3546
Devang Patelccca7fe2009-01-12 19:17:34 +00003547 return ID;
3548 }
3549
3550 /// RecordVariable - Indicate the declaration of a local variable.
Devang Patel1be3ecc2009-04-15 00:10:26 +00003551 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
3552 const MachineInstr *MI) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00003553 if (TimePassesIsEnabled)
3554 DebugTimer->startTimer();
3555
Devang Patel0e5200f2009-01-15 18:25:17 +00003556 DIDescriptor Desc(GV);
3557 DbgScope *Scope = NULL;
Bill Wendling163ba3f2009-03-10 21:23:25 +00003558
Devang Patel0e5200f2009-01-15 18:25:17 +00003559 if (Desc.getTag() == DW_TAG_variable) {
3560 // GV is a global variable.
3561 DIGlobalVariable DG(GV);
3562 Scope = getOrCreateScope(DG.getContext().getGV());
3563 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003564 DenseMap<const MachineInstr *, DbgScope *>::iterator
3565 SI = InlinedVariableScopes.find(MI);
Bill Wendling3f500d92009-05-06 21:21:34 +00003566
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003567 if (SI != InlinedVariableScopes.end()) {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003568 // or GV is an inlined local variable.
3569 Scope = SI->second;
3570 } else {
Devang Patel1be3ecc2009-04-15 00:10:26 +00003571 DIVariable DV(GV);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003572 GlobalVariable *V = DV.getContext().getGV();
3573
3574 // FIXME: The code that checks for the inlined local variable is a hack!
3575 DenseMap<const GlobalVariable *, DbgScope *>::iterator
3576 AI = AbstractInstanceRootMap.find(V);
3577
3578 if (AI != AbstractInstanceRootMap.end())
3579 // or GV is an inlined local variable.
3580 Scope = AI->second;
3581 else
3582 // or GV is a local variable.
3583 Scope = getOrCreateScope(V);
Devang Patel1be3ecc2009-04-15 00:10:26 +00003584 }
Devang Patel0e5200f2009-01-15 18:25:17 +00003585 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00003586
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003587 assert(Scope && "Unable to find the variable's scope");
Devang Patel99ec3532009-01-16 19:28:14 +00003588 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelccca7fe2009-01-12 19:17:34 +00003589 Scope->AddVariable(DV);
Bill Wendling163ba3f2009-03-10 21:23:25 +00003590
3591 if (TimePassesIsEnabled)
3592 DebugTimer->stopTimer();
Devang Patelccca7fe2009-01-12 19:17:34 +00003593 }
Devang Patel0f7fef32009-04-13 17:02:03 +00003594
Devang Patel1be3ecc2009-04-15 00:10:26 +00003595 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003596 unsigned RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
3597 unsigned Line, unsigned Col) {
3598 unsigned LabelID = MMI->NextLabelID();
3599
Devang Patel1be3ecc2009-04-15 00:10:26 +00003600 if (!TAI->doesDwarfUsesInlineInfoSection())
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003601 return LabelID;
Devang Patel1be3ecc2009-04-15 00:10:26 +00003602
Bill Wendlingd5a63812009-05-01 08:40:06 +00003603 if (TimePassesIsEnabled)
3604 DebugTimer->startTimer();
3605
Devang Patel1be3ecc2009-04-15 00:10:26 +00003606 GlobalVariable *GV = SP.getGV();
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003607 DenseMap<const GlobalVariable *, DbgScope *>::iterator
3608 II = AbstractInstanceRootMap.find(GV);
Devang Patel1be3ecc2009-04-15 00:10:26 +00003609
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003610 if (II == AbstractInstanceRootMap.end()) {
3611 // Create an abstract instance entry for this inlined function if it
3612 // doesn't already exist.
3613 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(GV));
Bill Wendlingef956fc2009-05-01 08:32:14 +00003614
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003615 // Get the compile unit context.
3616 CompileUnit *Unit = &FindCompileUnit(SP.getCompileUnit());
3617 DIE *SPDie = Unit->getDieMapSlotFor(GV);
3618 if (!SPDie)
3619 SPDie = CreateSubprogramDIE(Unit, SP);
3620
3621 // Mark as being inlined. This makes this subprogram entry an abstract
3622 // instance root.
3623 // FIXME: Our debugger doesn't care about the value of DW_AT_inline, only
3624 // that it's defined. It probably won't change in the future, but this
3625 // could be more elegant.
3626 AddUInt(SPDie, DW_AT_inline, 0, DW_INL_declared_not_inlined);
3627
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003628 // Keep track of the abstract scope for this function.
3629 DbgAbstractScopeMap[GV] = Scope;
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00003630
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003631 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);
Bill Wendlingbc12c2b2009-05-12 00:06:59 +00003649 MMI->RecordUsedDbgLabel(LabelID);
Bill Wendlinga5c8a4e2009-05-10 23:14:38 +00003650
3651 LexicalScopeStack.back()->AddConcreteInst(ConcreteScope);
3652
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003653 // Keep track of the concrete scope that's inlined into this function.
3654 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 8> >::iterator
3655 SI = DbgConcreteScopeMap.find(GV);
3656
3657 if (SI == DbgConcreteScopeMap.end())
3658 DbgConcreteScopeMap[GV].push_back(ConcreteScope);
3659 else
3660 SI->second.push_back(ConcreteScope);
3661
Bill Wendlingbc12c2b2009-05-12 00:06:59 +00003662 // Track the start label for this inlined function.
3663 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator
3664 I = InlineInfo.find(GV);
3665
3666 if (I == InlineInfo.end())
3667 InlineInfo[GV].push_back(LabelID);
3668 else
3669 I->second.push_back(LabelID);
3670
Bill Wendlingd5a63812009-05-01 08:40:06 +00003671 if (TimePassesIsEnabled)
3672 DebugTimer->stopTimer();
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00003673
3674 return LabelID;
Devang Patel0f7fef32009-04-13 17:02:03 +00003675 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00003676
3677 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
3678 unsigned RecordInlinedFnEnd(DISubprogram &SP) {
3679 if (!TAI->doesDwarfUsesInlineInfoSection())
3680 return 0;
3681
Bill Wendlingd5a63812009-05-01 08:40:06 +00003682 if (TimePassesIsEnabled)
3683 DebugTimer->startTimer();
3684
Devang Patel1be3ecc2009-04-15 00:10:26 +00003685 GlobalVariable *GV = SP.getGV();
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00003686 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 8> >::iterator
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003687 I = DbgConcreteScopeMap.find(GV);
Bill Wendling3f500d92009-05-06 21:21:34 +00003688
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003689 if (I == DbgConcreteScopeMap.end()) {
Bill Wendlingd5a63812009-05-01 08:40:06 +00003690 if (TimePassesIsEnabled)
3691 DebugTimer->stopTimer();
3692
Devang Patel1be3ecc2009-04-15 00:10:26 +00003693 return 0;
Bill Wendlingd5a63812009-05-01 08:40:06 +00003694 }
Devang Patel1be3ecc2009-04-15 00:10:26 +00003695
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00003696 SmallVector<DbgScope *, 8> &Scopes = I->second;
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00003697 assert(!Scopes.empty() && "We should have at least one debug scope!");
Bill Wendlingf3cc96e2009-05-13 20:33:33 +00003698 DbgScope *Scope = Scopes.back(); Scopes.pop_back();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003699 unsigned ID = MMI->NextLabelID();
3700 MMI->RecordUsedDbgLabel(ID);
3701 Scope->setEndLabelID(ID);
Bill Wendlingd5a63812009-05-01 08:40:06 +00003702
3703 if (TimePassesIsEnabled)
3704 DebugTimer->stopTimer();
3705
Devang Patel1be3ecc2009-04-15 00:10:26 +00003706 return ID;
3707 }
3708
3709 /// RecordVariableScope - Record scope for the variable declared by
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003710 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE. Record scopes
3711 /// for only inlined subroutine variables. Other variables's scopes are
3712 /// determined during RecordVariable().
Devang Patel1be3ecc2009-04-15 00:10:26 +00003713 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI) {
Bill Wendlingd5a63812009-05-01 08:40:06 +00003714 if (TimePassesIsEnabled)
3715 DebugTimer->startTimer();
3716
Devang Patel1be3ecc2009-04-15 00:10:26 +00003717 DISubprogram SP(DV.getContext().getGV());
Bill Wendlingd5a63812009-05-01 08:40:06 +00003718
3719 if (SP.isNull()) {
3720 if (TimePassesIsEnabled)
3721 DebugTimer->stopTimer();
3722
Devang Patel1be3ecc2009-04-15 00:10:26 +00003723 return;
Bill Wendlingd5a63812009-05-01 08:40:06 +00003724 }
3725
Bill Wendlingf59d10f2009-05-13 23:55:49 +00003726 DenseMap<GlobalVariable *, DbgScope *>::iterator
3727 I = DbgAbstractScopeMap.find(SP.getGV());
3728 if (I != DbgAbstractScopeMap.end())
3729 InlinedVariableScopes[DeclareMI] = I->second;
Devang Patel1be3ecc2009-04-15 00:10:26 +00003730
Bill Wendlingd5a63812009-05-01 08:40:06 +00003731 if (TimePassesIsEnabled)
3732 DebugTimer->stopTimer();
Devang Patel1be3ecc2009-04-15 00:10:26 +00003733 }
Jim Laskey65195462006-10-30 13:35:07 +00003734};
3735
Jim Laskey072200c2007-01-29 18:51:14 +00003736//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003737/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003738///
3739class DwarfException : public Dwarf {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003740 struct FunctionEHFrameInfo {
3741 std::string FnName;
3742 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003743 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003744 bool hasCalls;
3745 bool hasLandingPads;
3746 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003747 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003748
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003749 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3750 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003751 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003752 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003753 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003754 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003755 };
3756
3757 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003758
3759 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3760 /// be emitted.
3761 bool shouldEmitTable;
3762
3763 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3764 /// should be emitted.
3765 bool shouldEmitMoves;
3766
3767 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3768 /// should be emitted.
3769 bool shouldEmitTableModule;
3770
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003771 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003772 /// should be emitted.
3773 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003774
Bill Wendling163ba3f2009-03-10 21:23:25 +00003775 /// ExceptionTimer - Timer for the Dwarf exception writer.
3776 Timer *ExceptionTimer;
3777
Jim Laskey3f09fc22007-02-28 18:38:31 +00003778 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3779 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003780 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003781 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003782 int stackGrowth =
3783 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3784 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003785 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003786
Jim Laskeybacd3042007-02-21 22:48:45 +00003787 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003788 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003789
3790 if (!TAI->doesRequireNonLocalEHFrameLabel())
3791 O << TAI->getEHGlobalPrefix();
3792 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003793 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003794
Jim Laskeybacd3042007-02-21 22:48:45 +00003795 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003796 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003797
Jim Laskeybacd3042007-02-21 22:48:45 +00003798 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003799 EmitDifference("eh_frame_common_end", Index,
3800 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003801 Asm->EOL("Length of Common Information Entry");
3802
Jim Laskeybacd3042007-02-21 22:48:45 +00003803 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003804 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003805 Asm->EmitInt32((int)0);
3806 Asm->EOL("CIE Identifier Tag");
3807 Asm->EmitInt8(DW_CIE_VERSION);
3808 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003809
Jim Laskeybacd3042007-02-21 22:48:45 +00003810 // The personality presence indicates that language specific information
3811 // will show up in the eh frame.
3812 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003813 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003814
Jim Laskeybacd3042007-02-21 22:48:45 +00003815 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003816 Asm->EmitULEB128Bytes(1);
3817 Asm->EOL("CIE Code Alignment Factor");
3818 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003819 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003820 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003821 Asm->EOL("CIE Return Address Column");
3822
Jim Laskeybacd3042007-02-21 22:48:45 +00003823 // If there is a personality, we need to indicate the functions location.
3824 if (Personality) {
3825 Asm->EmitULEB128Bytes(7);
3826 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003827
Duncan Sands671fa972008-05-07 19:11:09 +00003828 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003829 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003830 Asm->EOL("Personality (pcrel sdata4 indirect)");
3831 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003832 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003833 Asm->EOL("Personality (pcrel sdata4)");
3834 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003835
Duncan Sands671fa972008-05-07 19:11:09 +00003836 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003837 O << TAI->getPersonalityPrefix();
3838 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3839 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003840 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3841 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003842 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003843
Duncan Sands671fa972008-05-07 19:11:09 +00003844 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3845 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003846
Bill Wendlingd60de512009-01-05 22:53:45 +00003847 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3848 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003849 } else {
3850 Asm->EmitULEB128Bytes(1);
3851 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003852
Bill Wendlingd60de512009-01-05 22:53:45 +00003853 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3854 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003855 }
3856
3857 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003858 std::vector<MachineMove> Moves;
3859 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003860 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003861
Dale Johannesen21d972a2008-04-30 00:43:29 +00003862 // On Darwin the linker honors the alignment of eh_frame, which means it
3863 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3864 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003865 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003866 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003867 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003868
Jim Laskeybacd3042007-02-21 22:48:45 +00003869 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003870 }
Duncan Sands671fa972008-05-07 19:11:09 +00003871
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003872 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003873 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003874 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003875 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
Chris Lattner266c7bb2009-04-13 05:44:34 +00003876
3877 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
3878 "Should not emit 'available externally' functions at all");
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003879
Jim Laskeybacd3042007-02-21 22:48:45 +00003880 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3881
3882 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003883 // If the corresponding function is static, this should not be
3884 // externally visible.
Rafael Espindolabb46f522009-01-15 20:18:42 +00003885 if (linkage != Function::InternalLinkage &&
Devang Patel2d1768c2009-01-17 08:05:14 +00003886 linkage != Function::PrivateLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003887 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3888 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3889 }
3890
Dale Johannesen038129d2008-01-10 02:03:30 +00003891 // If corresponding function is weak definition, this should be too.
Duncan Sands667d4b82009-03-07 15:45:40 +00003892 if ((linkage == Function::WeakAnyLinkage ||
3893 linkage == Function::WeakODRLinkage ||
3894 linkage == Function::LinkOnceAnyLinkage ||
3895 linkage == Function::LinkOnceODRLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003896 TAI->getWeakDefDirective())
3897 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3898
3899 // If there are no calls then you can't unwind. This may mean we can
3900 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003901 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003902 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003903 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003904 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003905 !UnwindTablesMandatory &&
Duncan Sands667d4b82009-03-07 15:45:40 +00003906 ((linkage != Function::WeakAnyLinkage &&
3907 linkage != Function::WeakODRLinkage &&
3908 linkage != Function::LinkOnceAnyLinkage &&
3909 linkage != Function::LinkOnceODRLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003910 !TAI->getWeakDefDirective() ||
3911 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003912 {
Bill Wendling6e198962007-09-18 01:47:22 +00003913 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003914 // This name has no connection to the function, so it might get
3915 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003916 // dead-stripping unconditionally.
3917 if (const char *UsedDirective = TAI->getUsedDirective())
3918 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003919 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003920 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003921
Jim Laskeybacd3042007-02-21 22:48:45 +00003922 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003923 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3924 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003925 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003926
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003927 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003928
Bill Wendling722f5f12008-12-24 08:05:17 +00003929 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3930 PrintRelDirective(true, true);
3931 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3932
3933 if (!TAI->isAbsoluteEHSectionOffsets())
3934 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3935 } else {
3936 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3937 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3938 true, true, false);
3939 }
3940
Jim Laskeybacd3042007-02-21 22:48:45 +00003941 Asm->EOL("FDE CIE offset");
3942
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003943 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003944 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003945 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003946 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003947 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003948
Jim Laskey3f09fc22007-02-28 18:38:31 +00003949 // If there is a personality and landing pads then point to the language
3950 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003951 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003952 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003953 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003954
3955 if (EHFrameInfo.hasLandingPads)
3956 EmitReference("exception", EHFrameInfo.Number, true, true);
3957 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003958 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003959 Asm->EOL("Language Specific Data Area");
3960 } else {
3961 Asm->EmitULEB128Bytes(0);
3962 Asm->EOL("Augmentation size");
3963 }
Duncan Sands671fa972008-05-07 19:11:09 +00003964
Jim Laskeybacd3042007-02-21 22:48:45 +00003965 // Indicate locations of function specific callee saved registers in
3966 // frame.
Devang Patel5aac3d32009-01-17 08:01:33 +00003967 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel2d1768c2009-01-17 08:05:14 +00003968 true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003969
Dale Johannesen21d972a2008-04-30 00:43:29 +00003970 // On Darwin the linker honors the alignment of eh_frame, which means it
3971 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3972 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003973 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003974 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003975 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003976
3977 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003978 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003979 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003980 // that depends on unused functions (calling undefined externals) being
3981 // dead-stripped to link correctly. Yes, there really is.
3982 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3983 if (const char *UsedDirective = TAI->getUsedDirective())
3984 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3985 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003986 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003987
Duncan Sands57810cd2007-09-05 11:27:52 +00003988 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003989 ///
3990 /// The general organization of the table is complex, but the basic concepts
3991 /// are easy. First there is a header which describes the location and
3992 /// organization of the three components that follow.
3993 /// 1. The landing pad site information describes the range of code covered
3994 /// by the try. In our case it's an accumulation of the ranges covered
3995 /// by the invokes in the try. There is also a reference to the landing
3996 /// pad that handles the exception once processed. Finally an index into
3997 /// the actions table.
3998 /// 2. The action table, in our case, is composed of pairs of type ids
3999 /// and next action offset. Starting with the action index from the
4000 /// landing pad site, each type Id is checked for a match to the current
4001 /// exception. If it matches then the exception and type id are passed
4002 /// on to the landing pad. Otherwise the next action is looked up. This
4003 /// chain is terminated with a next action of zero. If no type id is
4004 /// found the the frame is unwound and handling continues.
4005 /// 3. Type id table contains references to all the C++ typeinfo for all
4006 /// catches in the function. This tables is reversed indexed base 1.
4007
Duncan Sandsb32edb42007-06-06 15:37:31 +00004008 /// SharedTypeIds - How many leading type ids two landing pads have in common.
4009 static unsigned SharedTypeIds(const LandingPadInfo *L,
4010 const LandingPadInfo *R) {
4011 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004012 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004013 unsigned MinSize = LSize < RSize ? LSize : RSize;
4014 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004015
Duncan Sandsb32edb42007-06-06 15:37:31 +00004016 for (; Count != MinSize; ++Count)
4017 if (LIds[Count] != RIds[Count])
4018 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004019
Duncan Sandsb32edb42007-06-06 15:37:31 +00004020 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004021 }
4022
Duncan Sandsb32edb42007-06-06 15:37:31 +00004023 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00004024 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004025 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004026 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004027 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004028
Duncan Sandsb32edb42007-06-06 15:37:31 +00004029 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00004030 if (LIds[i] != RIds[i])
4031 return LIds[i] < RIds[i];
4032
Duncan Sandsb32edb42007-06-06 15:37:31 +00004033 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004034 }
4035
Duncan Sands53c3a332007-05-16 12:12:23 +00004036 struct KeyInfo {
4037 static inline unsigned getEmptyKey() { return -1U; }
4038 static inline unsigned getTombstoneKey() { return -2U; }
4039 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00004040 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00004041 static bool isPod() { return true; }
4042 };
4043
Duncan Sands57810cd2007-09-05 11:27:52 +00004044 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004045 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004046 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004047 int NextAction;
4048 struct ActionEntry *Previous;
4049 };
4050
Duncan Sands57810cd2007-09-05 11:27:52 +00004051 /// PadRange - Structure holding a try-range and the associated landing pad.
4052 struct PadRange {
4053 // The index of the landing pad.
4054 unsigned PadIndex;
4055 // The index of the begin and end labels in the landing pad's label lists.
4056 unsigned RangeIndex;
4057 };
4058
4059 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
4060
4061 /// CallSiteEntry - Structure describing an entry in the call-site table.
4062 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00004063 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004064 unsigned BeginLabel; // zero indicates the start of the function.
4065 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00004066 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004067 unsigned PadLabel; // zero indicates that there is no landing pad.
4068 unsigned Action;
4069 };
4070
Jim Laskeybacd3042007-02-21 22:48:45 +00004071 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00004072 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00004073 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00004074 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
4075 if (PadInfos.empty()) return;
4076
4077 // Sort the landing pads in order of their type ids. This is used to fold
4078 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00004079 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00004080 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00004081 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00004082 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004083 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
4084
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004085 // Negative type ids index into FilterIds, positive type ids index into
4086 // TypeInfos. The value written for a positive type id is just the type
4087 // id itself. For a negative type id, however, the value written is the
4088 // (negative) byte offset of the corresponding FilterIds entry. The byte
4089 // offset is usually equal to the type id, because the FilterIds entries
4090 // are written using a variable width encoding which outputs one byte per
4091 // entry as long as the value written is not too large, but can differ.
4092 // This kind of complication does not occur for positive type ids because
4093 // type infos are output using a fixed width encoding.
4094 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
4095 SmallVector<int, 16> FilterOffsets;
4096 FilterOffsets.reserve(FilterIds.size());
4097 int Offset = -1;
4098 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
4099 E = FilterIds.end(); I != E; ++I) {
4100 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004101 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004102 }
4103
Duncan Sands57810cd2007-09-05 11:27:52 +00004104 // Compute the actions table and gather the first action index for each
4105 // landing pad site.
4106 SmallVector<ActionEntry, 32> Actions;
4107 SmallVector<unsigned, 64> FirstActions;
4108 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00004109
Duncan Sandsb32edb42007-06-06 15:37:31 +00004110 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00004111 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004112 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004113 const LandingPadInfo *LP = LandingPads[i];
4114 const std::vector<int> &TypeIds = LP->TypeIds;
4115 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004116 unsigned SizeSiteActions = 0;
4117
Duncan Sandsb32edb42007-06-06 15:37:31 +00004118 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00004119 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00004120 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004121
Duncan Sandsb32edb42007-06-06 15:37:31 +00004122 if (NumShared) {
4123 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
4124 assert(Actions.size());
4125 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004126 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
4127 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004128 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004129 SizeAction -=
4130 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004131 SizeAction += -PrevAction->NextAction;
4132 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004133 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00004134 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00004135
Duncan Sandsb32edb42007-06-06 15:37:31 +00004136 // Compute the actions.
4137 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
4138 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004139 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4140 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004141 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004142
4143 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004144 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004145 SizeSiteActions += SizeAction;
4146
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004147 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00004148 Actions.push_back(Action);
4149
4150 PrevAction = &Actions.back();
4151 }
4152
4153 // Record the first action of the landing pad site.
4154 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4155 } // else identical - re-use previous FirstAction
4156
4157 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004158
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00004159 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00004160 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00004161 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004162
Duncan Sands481dc722007-12-19 07:36:31 +00004163 // Compute the call-site table. The entry for an invoke has a try-range
4164 // containing the call, a non-zero landing pad and an appropriate action.
4165 // The entry for an ordinary call has a try-range containing the call and
4166 // zero for the landing pad and the action. Calls marked 'nounwind' have
4167 // no entry and must not be contained in the try-range of any entry - they
4168 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004169 SmallVector<CallSiteEntry, 64> CallSites;
4170
4171 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00004172 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4173 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4174 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00004175 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4176 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00004177 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004178 unsigned BeginLabel = LandingPad->BeginLabels[j];
4179 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4180 PadRange P = { i, j };
4181 PadMap[BeginLabel] = P;
4182 }
4183 }
4184
Duncan Sands481dc722007-12-19 07:36:31 +00004185 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00004186 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00004187
4188 // Whether there is a potentially throwing instruction (currently this means
4189 // an ordinary call) between the end of the previous try-range and now.
4190 bool SawPotentiallyThrowing = false;
4191
4192 // Whether the last callsite entry was for an invoke.
4193 bool PreviousIsInvoke = false;
4194
Duncan Sands481dc722007-12-19 07:36:31 +00004195 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004196 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4197 I != E; ++I) {
4198 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4199 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00004200 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00004201 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00004202 continue;
4203 }
4204
Chris Lattner9e330492007-12-30 20:50:28 +00004205 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00004206 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00004207
Duncan Sands481dc722007-12-19 07:36:31 +00004208 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00004209 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00004210 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004211
Duncan Sands481dc722007-12-19 07:36:31 +00004212 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00004213 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00004214 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00004215 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00004216 continue;
4217
4218 PadRange P = L->second;
4219 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4220
4221 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4222 "Inconsistent landing pad map!");
4223
4224 // If some instruction between the previous try-range and this one may
4225 // throw, create a call-site entry with no landing pad for the region
4226 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00004227 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004228 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4229 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00004230 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004231 }
4232
4233 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00004234 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00004235
Duncan Sands481dc722007-12-19 07:36:31 +00004236 if (LandingPad->LandingPadLabel) {
4237 // This try-range is for an invoke.
4238 CallSiteEntry Site = {BeginLabel, LastLabel,
4239 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00004240
Duncan Sands481dc722007-12-19 07:36:31 +00004241 // Try to merge with the previous call-site.
4242 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00004243 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00004244 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4245 // Extend the range of the previous entry.
4246 Prev.EndLabel = Site.EndLabel;
4247 continue;
4248 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004249 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004250
Duncan Sands481dc722007-12-19 07:36:31 +00004251 // Otherwise, create a new call-site.
4252 CallSites.push_back(Site);
4253 PreviousIsInvoke = true;
4254 } else {
4255 // Create a gap.
4256 PreviousIsInvoke = false;
4257 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004258 }
4259 }
4260 // If some instruction between the previous try-range and the end of the
4261 // function may throw, create a call-site entry with no landing pad for the
4262 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00004263 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004264 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4265 CallSites.push_back(Site);
4266 }
4267
Jim Laskeybacd3042007-02-21 22:48:45 +00004268 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00004269
4270 // Call sites.
4271 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4272 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4273 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4274 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4275 SiteLengthSize +
4276 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00004277 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004278 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00004279
Duncan Sands671fa972008-05-07 19:11:09 +00004280 // Type infos.
4281 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4282 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004283
4284 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004285 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004286 SizeSites + SizeActions + SizeTypes;
4287
4288 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4289 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004290 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004291 TypeOffset;
4292
4293 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004294
Jim Laskeybacd3042007-02-21 22:48:45 +00004295 // Begin the exception table.
4296 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00004297 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00004298 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004299 for (unsigned i = 0; i != SizeAlign; ++i) {
4300 Asm->EmitInt8(0);
4301 Asm->EOL("Padding");
4302 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004303 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004304
Jim Laskeybacd3042007-02-21 22:48:45 +00004305 // Emit the header.
4306 Asm->EmitInt8(DW_EH_PE_omit);
4307 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4308 Asm->EmitInt8(DW_EH_PE_absptr);
4309 Asm->EOL("TType format (DW_EH_PE_absptr)");
4310 Asm->EmitULEB128Bytes(TypeOffset);
4311 Asm->EOL("TType base offset");
4312 Asm->EmitInt8(DW_EH_PE_udata4);
4313 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4314 Asm->EmitULEB128Bytes(SizeSites);
4315 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004316
Duncan Sands57810cd2007-09-05 11:27:52 +00004317 // Emit the landing pad site information.
4318 for (unsigned i = 0; i < CallSites.size(); ++i) {
4319 CallSiteEntry &S = CallSites[i];
4320 const char *BeginTag;
4321 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00004322
Duncan Sands57810cd2007-09-05 11:27:52 +00004323 if (!S.BeginLabel) {
4324 BeginTag = "eh_func_begin";
4325 BeginNumber = SubprogramCount;
4326 } else {
4327 BeginTag = "label";
4328 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004329 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004330
Duncan Sands57810cd2007-09-05 11:27:52 +00004331 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004332 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004333 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004334
Duncan Sands57810cd2007-09-05 11:27:52 +00004335 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004336 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004337 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004338 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004339 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004340 }
4341 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004342
Duncan Sands671fa972008-05-07 19:11:09 +00004343 if (!S.PadLabel)
4344 Asm->EmitInt32(0);
4345 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004346 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004347 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004348 Asm->EOL("Landing pad");
4349
4350 Asm->EmitULEB128Bytes(S.Action);
4351 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004352 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004353
Jim Laskeybacd3042007-02-21 22:48:45 +00004354 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004355 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4356 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004357
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004358 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004359 Asm->EOL("TypeInfo index");
4360 Asm->EmitSLEB128Bytes(Action.NextAction);
4361 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004362 }
4363
4364 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004365 for (unsigned M = TypeInfos.size(); M; --M) {
4366 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004367
4368 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004369
Bill Wendling7d16e852009-04-10 00:12:49 +00004370 if (GV) {
4371 std::string GLN;
4372 O << Asm->getGlobalLinkName(GV, GLN);
4373 } else {
Jim Laskeybacd3042007-02-21 22:48:45 +00004374 O << "0";
Bill Wendling7d16e852009-04-10 00:12:49 +00004375 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004376
Jim Laskeybacd3042007-02-21 22:48:45 +00004377 Asm->EOL("TypeInfo");
4378 }
4379
Duncan Sands73ef58a2007-06-02 16:53:42 +00004380 // Emit the filter typeids.
4381 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4382 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004383 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004384 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004385 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004386
Evan Cheng05548eb2008-02-29 19:36:59 +00004387 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004388 }
4389
Jim Laskey072200c2007-01-29 18:51:14 +00004390public:
4391 //===--------------------------------------------------------------------===//
4392 // Main entry points.
4393 //
Owen Andersoncb371882008-08-21 00:14:44 +00004394 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Bill Wendling163ba3f2009-03-10 21:23:25 +00004395 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
4396 shouldEmitTableModule(false), shouldEmitMovesModule(false),
4397 ExceptionTimer(0) {
4398 if (TimePassesIsEnabled)
4399 ExceptionTimer = new Timer("Dwarf Exception Writer",
Bill Wendling68edf5f2009-03-10 22:58:53 +00004400 getDwarfTimerGroup());
Bill Wendling163ba3f2009-03-10 21:23:25 +00004401 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004402
Bill Wendling163ba3f2009-03-10 21:23:25 +00004403 virtual ~DwarfException() {
4404 delete ExceptionTimer;
4405 }
Jim Laskey072200c2007-01-29 18:51:14 +00004406
4407 /// SetModuleInfo - Set machine module information when it's known that pass
4408 /// manager has created it. Set by the target AsmPrinter.
4409 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004410 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004411 }
4412
4413 /// BeginModule - Emit all exception information that should come prior to the
4414 /// content.
4415 void BeginModule(Module *M) {
4416 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004417 }
4418
4419 /// EndModule - Emit all exception information that should come after the
4420 /// content.
4421 void EndModule() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004422 if (TimePassesIsEnabled)
4423 ExceptionTimer->startTimer();
4424
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004425 if (shouldEmitMovesModule || shouldEmitTableModule) {
4426 const std::vector<Function *> Personalities = MMI->getPersonalities();
Evan Chenge3d42322009-02-25 07:04:34 +00004427 for (unsigned i = 0; i < Personalities.size(); ++i)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004428 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004429
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004430 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4431 E = EHFrames.end(); I != E; ++I)
4432 EmitEHFrame(*I);
4433 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004434
4435 if (TimePassesIsEnabled)
4436 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004437 }
4438
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004439 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004440 /// emitted immediately after the function entry point.
4441 void BeginFunction(MachineFunction *MF) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004442 if (TimePassesIsEnabled)
4443 ExceptionTimer->startTimer();
4444
Jim Laskey072200c2007-01-29 18:51:14 +00004445 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004446 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004447
Bill Wendling163ba3f2009-03-10 21:23:25 +00004448 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004449 // Map all labels and get rid of any dead landing pads.
4450 MMI->TidyLandingPads();
Bill Wendling163ba3f2009-03-10 21:23:25 +00004451
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004452 // If any landing pads survive, we need an EH table.
4453 if (MMI->getLandingPads().size())
4454 shouldEmitTable = true;
4455
4456 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004457 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004458 shouldEmitMoves = true;
4459
4460 if (shouldEmitMoves || shouldEmitTable)
4461 // Assumes in correct section after the entry point.
4462 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004463 }
Bill Wendling163ba3f2009-03-10 21:23:25 +00004464
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004465 shouldEmitTableModule |= shouldEmitTable;
4466 shouldEmitMovesModule |= shouldEmitMoves;
Bill Wendling163ba3f2009-03-10 21:23:25 +00004467
4468 if (TimePassesIsEnabled)
4469 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004470 }
4471
4472 /// EndFunction - Gather and emit post-function exception information.
4473 ///
4474 void EndFunction() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004475 if (TimePassesIsEnabled)
4476 ExceptionTimer->startTimer();
4477
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004478 if (shouldEmitMoves || shouldEmitTable) {
4479 EmitLabel("eh_func_end", SubprogramCount);
4480 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004481
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004482 // Save EH frame information
Bill Wendling7d16e852009-04-10 00:12:49 +00004483 std::string Name;
4484 EHFrames.push_back(
4485 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF, Name),
4486 SubprogramCount,
4487 MMI->getPersonalityIndex(),
4488 MF->getFrameInfo()->hasCalls(),
4489 !MMI->getLandingPads().empty(),
4490 MMI->getFrameMoves(),
4491 MF->getFunction()));
Bill Wendling163ba3f2009-03-10 21:23:25 +00004492 }
4493
4494 if (TimePassesIsEnabled)
4495 ExceptionTimer->stopTimer();
Jim Laskey072200c2007-01-29 18:51:14 +00004496 }
4497};
4498
Jim Laskey0d086af2006-02-27 12:43:29 +00004499} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004500
4501//===----------------------------------------------------------------------===//
4502
Jim Laskeyd18e2892006-01-20 20:34:06 +00004503/// Emit - Print the abbreviation using the specified Dwarf writer.
4504///
Jim Laskey072200c2007-01-29 18:51:14 +00004505void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004506 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004507 DD.getAsm()->EmitULEB128Bytes(Tag);
4508 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004509
Jim Laskeyd18e2892006-01-20 20:34:06 +00004510 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004511 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4512 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004513
Jim Laskeyd18e2892006-01-20 20:34:06 +00004514 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004515 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004516 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004517
Jim Laskeyd18e2892006-01-20 20:34:06 +00004518 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004519 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4520 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004521
Jim Laskeyd18e2892006-01-20 20:34:06 +00004522 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004523 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4524 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004525 }
4526
4527 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004528 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4529 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004530}
4531
4532#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004533void DIEAbbrev::print(std::ostream &O) {
4534 O << "Abbreviation @"
4535 << std::hex << (intptr_t)this << std::dec
4536 << " "
4537 << TagString(Tag)
4538 << " "
4539 << ChildrenString(ChildrenFlag)
4540 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004541
Jim Laskeya0f3d172006-09-07 22:06:40 +00004542 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4543 O << " "
4544 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004545 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004546 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004547 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004548 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004549}
Bill Wendlinge8156192006-12-07 01:30:32 +00004550void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004551#endif
4552
4553//===----------------------------------------------------------------------===//
4554
Jim Laskeyef42a012006-11-02 20:12:39 +00004555#ifndef NDEBUG
4556void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004557 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004558}
Jim Laskeyef42a012006-11-02 20:12:39 +00004559#endif
4560
4561//===----------------------------------------------------------------------===//
4562
Jim Laskey063e7652006-01-17 17:31:53 +00004563/// EmitValue - Emit integer of appropriate size.
4564///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004565void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004566 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004567 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004568 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004569 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004570 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004571 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004572 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004573 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004574 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004575 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4576 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4577 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004578 default: assert(0 && "DIE Value form not supported yet"); break;
4579 }
4580}
4581
4582/// SizeOf - Determine size of integer value in bytes.
4583///
Jim Laskey072200c2007-01-29 18:51:14 +00004584unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004585 switch (Form) {
4586 case DW_FORM_flag: // Fall thru
4587 case DW_FORM_ref1: // Fall thru
4588 case DW_FORM_data1: return sizeof(int8_t);
4589 case DW_FORM_ref2: // Fall thru
4590 case DW_FORM_data2: return sizeof(int16_t);
4591 case DW_FORM_ref4: // Fall thru
4592 case DW_FORM_data4: return sizeof(int32_t);
4593 case DW_FORM_ref8: // Fall thru
4594 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004595 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4596 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004597 default: assert(0 && "DIE Value form not supported yet"); break;
4598 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004599 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004600}
4601
Jim Laskey063e7652006-01-17 17:31:53 +00004602//===----------------------------------------------------------------------===//
4603
4604/// EmitValue - Emit string value.
4605///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004606void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Bill Wendlingdd446322009-03-10 23:57:09 +00004607 DD.getAsm()->EmitString(Str);
Jim Laskey063e7652006-01-17 17:31:53 +00004608}
4609
Jim Laskey063e7652006-01-17 17:31:53 +00004610//===----------------------------------------------------------------------===//
4611
4612/// EmitValue - Emit label value.
4613///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004614void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004615 bool IsSmall = Form == DW_FORM_data4;
4616 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004617}
4618
4619/// SizeOf - Determine size of label value in bytes.
4620///
Jim Laskey072200c2007-01-29 18:51:14 +00004621unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004622 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004623 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004624}
Jim Laskeyef42a012006-11-02 20:12:39 +00004625
Jim Laskey063e7652006-01-17 17:31:53 +00004626//===----------------------------------------------------------------------===//
4627
Jim Laskeyd18e2892006-01-20 20:34:06 +00004628/// EmitValue - Emit label value.
4629///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004630void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004631 bool IsSmall = Form == DW_FORM_data4;
4632 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004633}
4634
4635/// SizeOf - Determine size of label value in bytes.
4636///
Jim Laskey072200c2007-01-29 18:51:14 +00004637unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004638 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004639 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004640}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004641
Jim Laskeyd18e2892006-01-20 20:34:06 +00004642//===----------------------------------------------------------------------===//
4643
Jim Laskey063e7652006-01-17 17:31:53 +00004644/// EmitValue - Emit delta value.
4645///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004646void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4647 bool IsSmall = Form == DW_FORM_data4;
4648 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4649 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4650}
4651
4652/// SizeOf - Determine size of delta value in bytes.
4653///
4654unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4655 if (Form == DW_FORM_data4) return 4;
4656 return DD.getTargetData()->getPointerSize();
4657}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004658
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004659//===----------------------------------------------------------------------===//
4660
4661/// EmitValue - Emit delta value.
4662///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004663void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004664 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004665 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004666}
4667
4668/// SizeOf - Determine size of delta value in bytes.
4669///
Jim Laskey072200c2007-01-29 18:51:14 +00004670unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004671 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004672 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004673}
4674
4675//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004676
Jim Laskeyb8509c52006-03-23 18:07:55 +00004677/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004678///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004679void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004680 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004681}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004682
Jim Laskeyd18e2892006-01-20 20:34:06 +00004683//===----------------------------------------------------------------------===//
4684
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004685/// ComputeSize - calculate the size of the block.
4686///
Jim Laskey072200c2007-01-29 18:51:14 +00004687unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004688 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004689 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004690
Jim Laskeyef42a012006-11-02 20:12:39 +00004691 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004692 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004693 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004694 }
4695 return Size;
4696}
4697
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004698/// EmitValue - Emit block data.
4699///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004700void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004701 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004702 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4703 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4704 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4705 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004706 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004707 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004708
Owen Anderson873e1b52008-06-24 21:44:59 +00004709 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004710
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004711 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004712 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004713 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004714 }
4715}
4716
4717/// SizeOf - Determine size of block data in bytes.
4718///
Jim Laskey072200c2007-01-29 18:51:14 +00004719unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004720 switch (Form) {
4721 case DW_FORM_block1: return Size + sizeof(int8_t);
4722 case DW_FORM_block2: return Size + sizeof(int16_t);
4723 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004724 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004725 default: assert(0 && "Improper form for block"); break;
4726 }
4727 return 0;
4728}
4729
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004730//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004731/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004732
4733DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004734 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004735 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004736}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004737
Jim Laskeyb8509c52006-03-23 18:07:55 +00004738/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4739///
4740void DIE::AddSiblingOffset() {
4741 DIEInteger *DI = new DIEInteger(0);
4742 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004743 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004744}
4745
Jim Laskeyef42a012006-11-02 20:12:39 +00004746/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004747///
Jim Laskeyef42a012006-11-02 20:12:39 +00004748void DIE::Profile(FoldingSetNodeID &ID) {
4749 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004750
Jim Laskeyef42a012006-11-02 20:12:39 +00004751 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4752 ID.AddPointer(Children[i]);
4753
4754 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4755 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004756}
Jim Laskeyef42a012006-11-02 20:12:39 +00004757
4758#ifndef NDEBUG
4759void DIE::print(std::ostream &O, unsigned IncIndent) {
4760 static unsigned IndentCount = 0;
4761 IndentCount += IncIndent;
4762 const std::string Indent(IndentCount, ' ');
4763 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004764
Jim Laskeyef42a012006-11-02 20:12:39 +00004765 if (!isBlock) {
4766 O << Indent
4767 << "Die: "
4768 << "0x" << std::hex << (intptr_t)this << std::dec
4769 << ", Offset: " << Offset
4770 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004771 << "\n";
4772
Jim Laskeyef42a012006-11-02 20:12:39 +00004773 O << Indent
4774 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004775 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004776 << ChildrenString(Abbrev.getChildrenFlag());
4777 } else {
4778 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004779 }
4780 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004781
Owen Anderson873e1b52008-06-24 21:44:59 +00004782 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004783
Jim Laskeyef42a012006-11-02 20:12:39 +00004784 IndentCount += 2;
4785 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4786 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004787
4788 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004789 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004790 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004791 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004792
Jim Laskeyef42a012006-11-02 20:12:39 +00004793 O << " "
4794 << FormEncodingString(Data[i].getForm())
4795 << " ";
4796 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004797 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004798 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004799 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004800
Jim Laskeyef42a012006-11-02 20:12:39 +00004801 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4802 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004803 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004804
Jim Laskeyef42a012006-11-02 20:12:39 +00004805 if (!isBlock) O << "\n";
4806 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004807}
4808
Jim Laskeyef42a012006-11-02 20:12:39 +00004809void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004810 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004811}
Jim Laskeybd761842006-02-27 17:27:12 +00004812#endif
Jim Laskey65195462006-10-30 13:35:07 +00004813
4814//===----------------------------------------------------------------------===//
4815/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004816///
Jim Laskey65195462006-10-30 13:35:07 +00004817
Bill Wendling91b8b802009-03-10 20:41:52 +00004818DwarfWriter::DwarfWriter()
Bill Wendling163ba3f2009-03-10 21:23:25 +00004819 : ImmutablePass(&ID), DD(0), DE(0) {}
Jim Laskey65195462006-10-30 13:35:07 +00004820
4821DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004822 delete DE;
4823 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004824}
4825
Jim Laskey65195462006-10-30 13:35:07 +00004826/// BeginModule - Emit all Dwarf sections that should come prior to the
4827/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004828void DwarfWriter::BeginModule(Module *M,
4829 MachineModuleInfo *MMI,
4830 raw_ostream &OS, AsmPrinter *A,
4831 const TargetAsmInfo *T) {
4832 DE = new DwarfException(OS, A, T);
4833 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004834 DE->BeginModule(M);
4835 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00004836 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00004837 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004838}
4839
4840/// EndModule - Emit all Dwarf sections that should come after the content.
4841///
4842void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004843 DE->EndModule();
4844 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004845}
4846
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004847/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004848/// emitted immediately after the function entry point.
4849void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004850 DE->BeginFunction(MF);
4851 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004852}
4853
4854/// EndFunction - Gather and emit post-function debug information.
4855///
Bill Wendlingd751c642008-09-26 00:28:12 +00004856void DwarfWriter::EndFunction(MachineFunction *MF) {
4857 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004858 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004859
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004860 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004861 // Clear function debug information.
4862 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004863}
Devang Patelccca7fe2009-01-12 19:17:34 +00004864
4865/// RecordSourceLine - Records location information and associates it with a
4866/// label. Returns a unique label ID used to generate a label and provide
4867/// correspondence to the source line list.
4868unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00004869 DICompileUnit CU) {
4870 return DD->RecordSourceLine(Line, Col, CU);
Devang Patelccca7fe2009-01-12 19:17:34 +00004871}
4872
4873/// RecordRegionStart - Indicate the start of a region.
4874unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004875 return DD->RecordRegionStart(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004876}
4877
4878/// RecordRegionEnd - Indicate the end of a region.
Dan Gohman9a38e3e2009-05-07 19:46:24 +00004879unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
4880 return DD->RecordRegionEnd(V);
Devang Patelccca7fe2009-01-12 19:17:34 +00004881}
4882
4883/// getRecordSourceLineCount - Count source lines.
4884unsigned DwarfWriter::getRecordSourceLineCount() {
Bill Wendling163ba3f2009-03-10 21:23:25 +00004885 return DD->getRecordSourceLineCount();
Devang Patelccca7fe2009-01-12 19:17:34 +00004886}
Devang Patelbb8c5952009-01-13 21:25:00 +00004887
Devang Patelc48c5502009-01-13 21:44:10 +00004888/// RecordVariable - Indicate the declaration of a local variable.
4889///
Devang Patel1be3ecc2009-04-15 00:10:26 +00004890void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
4891 const MachineInstr *MI) {
4892 DD->RecordVariable(GV, FrameIndex, MI);
Devang Patelc48c5502009-01-13 21:44:10 +00004893}
Devang Patelbbdc8202009-01-13 23:54:55 +00004894
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004895/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4896/// be emitted.
4897bool DwarfWriter::ShouldEmitDwarfDebug() const {
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +00004898 return DD && DD->ShouldEmitDwarfDebug();
Bill Wendling4ed0c5f2009-02-20 00:44:43 +00004899}
Devang Patel0f7fef32009-04-13 17:02:03 +00004900
Devang Patel1be3ecc2009-04-15 00:10:26 +00004901//// RecordInlinedFnStart - Global variable GV is inlined at the location marked
Devang Patel0f7fef32009-04-13 17:02:03 +00004902//// by LabelID label.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00004903unsigned DwarfWriter::RecordInlinedFnStart(DISubprogram SP, DICompileUnit CU,
4904 unsigned Line, unsigned Col) {
4905 return DD->RecordInlinedFnStart(SP, CU, Line, Col);
Devang Patel0f7fef32009-04-13 17:02:03 +00004906}
4907
Devang Patel1be3ecc2009-04-15 00:10:26 +00004908/// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
Argyrios Kyrtzidis116b2742009-05-07 00:16:31 +00004909unsigned DwarfWriter::RecordInlinedFnEnd(DISubprogram SP) {
Devang Patel1be3ecc2009-04-15 00:10:26 +00004910 return DD->RecordInlinedFnEnd(SP);
4911}
4912
4913/// RecordVariableScope - Record scope for the variable declared by
4914/// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE.
4915void DwarfWriter::RecordVariableScope(DIVariable &DV,
4916 const MachineInstr *DeclareMI) {
4917 DD->RecordVariableScope(DV, DeclareMI);
4918}