blob: 086786bcab3dae8abfb31b8ccc8aee04fae04ca2 [file] [log] [blame]
Jim Laskeye5032892005-12-21 19:48:16 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
2//
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 Laskeye5032892005-12-21 19:48:16 +000015
Duncan Sands53c3a332007-05-16 12:12:23 +000016#include "llvm/ADT/DenseMap.h"
Jim Laskeya9c83fe2006-10-30 15:59:54 +000017#include "llvm/ADT/FoldingSet.h"
Jim Laskey063e7652006-01-17 17:31:53 +000018#include "llvm/ADT/StringExtras.h"
Jim Laskey65195462006-10-30 13:35:07 +000019#include "llvm/ADT/UniqueVector.h"
Jim Laskey52060a02006-01-24 00:49:18 +000020#include "llvm/Module.h"
21#include "llvm/Type.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000022#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000025#include "llvm/CodeGen/MachineLocation.h"
Jim Laskey3f09fc22007-02-28 18:38:31 +000026#include "llvm/Support/Debug.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000027#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000028#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000029#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000030#include "llvm/Support/Mangler.h"
Dan Gohman85496362007-09-24 21:32:18 +000031#include "llvm/System/Path.h"
Jim Laskey563321a2006-09-06 18:34:40 +000032#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000033#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000034#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000035#include "llvm/Target/TargetFrameInfo.h"
Duncan Sands53c3a332007-05-16 12:12:23 +000036#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000037#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000038#include "llvm/Target/TargetOptions.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000039#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000040#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000041using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000042using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000043
Jim Laskey0d086af2006-02-27 12:43:29 +000044namespace llvm {
Jim Laskey65195462006-10-30 13:35:07 +000045
46//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000047
48/// Configuration values for initial hash set sizes (log2).
49///
50static const unsigned InitDiesSetSize = 9; // 512
51static const unsigned InitAbbreviationsSetSize = 9; // 512
52static const unsigned InitValuesSetSize = 9; // 512
53
54//===----------------------------------------------------------------------===//
55/// Forward declarations.
56///
57class DIE;
58class DIEValue;
59
60//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000061/// DWLabel - Labels are used to track locations in the assembler file.
Reid Spencer181b6c92007-08-05 20:06:04 +000062/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
63/// where the tag is a category of label (Ex. location) and number is a value
64/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +000065class DWLabel {
66public:
Jim Laskeyef42a012006-11-02 20:12:39 +000067 /// Tag - Label category tag. Should always be a staticly declared C string.
68 ///
69 const char *Tag;
70
71 /// Number - Value to make label unique.
72 ///
73 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +000074
75 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Jim Laskeybd761842006-02-27 17:27:12 +000076
Jim Laskeyef42a012006-11-02 20:12:39 +000077 void Profile(FoldingSetNodeID &ID) const {
78 ID.AddString(std::string(Tag));
79 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +000080 }
Jim Laskeyef42a012006-11-02 20:12:39 +000081
82#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +000083 void print(std::ostream *O) const {
84 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +000085 }
Jim Laskeyef42a012006-11-02 20:12:39 +000086 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +000087 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +000088 if (Number) O << Number;
89 }
90#endif
Jim Laskeybd761842006-02-27 17:27:12 +000091};
92
93//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000094/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
95/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +000096class DIEAbbrevData {
97private:
Jim Laskeyef42a012006-11-02 20:12:39 +000098 /// Attribute - Dwarf attribute code.
99 ///
100 unsigned Attribute;
101
102 /// Form - Dwarf form code.
103 ///
104 unsigned Form;
Jim Laskey0d086af2006-02-27 12:43:29 +0000105
106public:
107 DIEAbbrevData(unsigned A, unsigned F)
108 : Attribute(A)
109 , Form(F)
110 {}
111
Jim Laskeybd761842006-02-27 17:27:12 +0000112 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000113 unsigned getAttribute() const { return Attribute; }
114 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000115
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000116 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000117 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000118 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000119 ID.AddInteger(Attribute);
120 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000121 }
122};
Jim Laskey063e7652006-01-17 17:31:53 +0000123
Jim Laskey0d086af2006-02-27 12:43:29 +0000124//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000125/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
126/// information object.
127class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000128private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000129 /// Tag - Dwarf tag code.
130 ///
131 unsigned Tag;
132
133 /// Unique number for node.
134 ///
135 unsigned Number;
136
137 /// ChildrenFlag - Dwarf children flag.
138 ///
139 unsigned ChildrenFlag;
140
141 /// Data - Raw data bytes for abbreviation.
142 ///
143 std::vector<DIEAbbrevData> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000144
Jim Laskey0d086af2006-02-27 12:43:29 +0000145public:
Jim Laskey063e7652006-01-17 17:31:53 +0000146
Jim Laskey0d086af2006-02-27 12:43:29 +0000147 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000148 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000149 , ChildrenFlag(C)
150 , Data()
151 {}
152 ~DIEAbbrev() {}
153
Jim Laskeybd761842006-02-27 17:27:12 +0000154 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000155 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000156 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000157 unsigned getChildrenFlag() const { return ChildrenFlag; }
158 const std::vector<DIEAbbrevData> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000159 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000160 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000161 void setNumber(unsigned N) { Number = N; }
162
Jim Laskey0d086af2006-02-27 12:43:29 +0000163 /// AddAttribute - Adds another set of attribute information to the
164 /// abbreviation.
165 void AddAttribute(unsigned Attribute, unsigned Form) {
166 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000167 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000168
Jim Laskeyb8509c52006-03-23 18:07:55 +0000169 /// AddFirstAttribute - Adds a set of attribute information to the front
170 /// of the abbreviation.
171 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
172 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
173 }
174
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000175 /// Profile - Used to gather unique data for the abbreviation folding set.
176 ///
177 void Profile(FoldingSetNodeID &ID) {
178 ID.AddInteger(Tag);
179 ID.AddInteger(ChildrenFlag);
180
181 // For each attribute description.
182 for (unsigned i = 0, N = Data.size(); i < N; ++i)
183 Data[i].Profile(ID);
184 }
185
Jim Laskey0d086af2006-02-27 12:43:29 +0000186 /// Emit - Print the abbreviation using the specified Dwarf writer.
187 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000188 void Emit(const DwarfDebug &DD) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000189
190#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000191 void print(std::ostream *O) {
192 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000193 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000194 void print(std::ostream &O);
195 void dump();
196#endif
197};
Jim Laskey063e7652006-01-17 17:31:53 +0000198
Jim Laskey0d086af2006-02-27 12:43:29 +0000199//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000200/// DIE - A structured debug information entry. Has an abbreviation which
201/// describes it's organization.
202class DIE : public FoldingSetNode {
203protected:
204 /// Abbrev - Buffer for constructing abbreviation.
205 ///
206 DIEAbbrev Abbrev;
207
208 /// Offset - Offset in debug info section.
209 ///
210 unsigned Offset;
211
212 /// Size - Size of instance + children.
213 ///
214 unsigned Size;
215
216 /// Children DIEs.
217 ///
218 std::vector<DIE *> Children;
219
220 /// Attributes values.
221 ///
222 std::vector<DIEValue *> Values;
223
224public:
Dan Gohman81975f62007-08-27 14:50:10 +0000225 explicit DIE(unsigned Tag)
Jim Laskeyef42a012006-11-02 20:12:39 +0000226 : Abbrev(Tag, DW_CHILDREN_no)
227 , Offset(0)
228 , Size(0)
229 , Children()
230 , Values()
231 {}
232 virtual ~DIE();
233
234 // Accessors.
235 DIEAbbrev &getAbbrev() { return Abbrev; }
236 unsigned getAbbrevNumber() const {
237 return Abbrev.getNumber();
238 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000239 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000240 unsigned getOffset() const { return Offset; }
241 unsigned getSize() const { return Size; }
242 const std::vector<DIE *> &getChildren() const { return Children; }
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000243 std::vector<DIEValue *> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000244 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
245 void setOffset(unsigned O) { Offset = O; }
246 void setSize(unsigned S) { Size = S; }
247
248 /// AddValue - Add a value and attributes to a DIE.
249 ///
250 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
251 Abbrev.AddAttribute(Attribute, Form);
252 Values.push_back(Value);
253 }
254
255 /// SiblingOffset - Return the offset of the debug information entry's
256 /// sibling.
257 unsigned SiblingOffset() const { return Offset + Size; }
258
259 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
260 ///
261 void AddSiblingOffset();
262
263 /// AddChild - Add a child to the DIE.
264 ///
265 void AddChild(DIE *Child) {
266 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
267 Children.push_back(Child);
268 }
269
270 /// Detach - Detaches objects connected to it after copying.
271 ///
272 void Detach() {
273 Children.clear();
274 }
275
276 /// Profile - Used to gather unique data for the value folding set.
277 ///
278 void Profile(FoldingSetNodeID &ID) ;
279
280#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000281 void print(std::ostream *O, unsigned IncIndent = 0) {
282 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000283 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000284 void print(std::ostream &O, unsigned IncIndent = 0);
285 void dump();
286#endif
287};
288
289//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000290/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000291///
292class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000293public:
294 enum {
295 isInteger,
296 isString,
297 isLabel,
298 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000299 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000300 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000301 isEntry,
302 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000303 };
304
Jim Laskeyef42a012006-11-02 20:12:39 +0000305 /// Type - Type of data stored in the value.
306 ///
307 unsigned Type;
Jim Laskey0d086af2006-02-27 12:43:29 +0000308
Dan Gohman81975f62007-08-27 14:50:10 +0000309 explicit DIEValue(unsigned T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000310 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000311 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000312 virtual ~DIEValue() {}
313
Jim Laskeyf6733882006-11-02 21:48:18 +0000314 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000315 unsigned getType() const { return Type; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000316
Jim Laskey0d086af2006-02-27 12:43:29 +0000317 // Implement isa/cast/dyncast.
318 static bool classof(const DIEValue *) { return true; }
319
320 /// EmitValue - Emit value via the Dwarf writer.
321 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000322 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Jim Laskey0d086af2006-02-27 12:43:29 +0000323
324 /// 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;
Jim Laskeyef42a012006-11-02 20:12:39 +0000327
328 /// Profile - Used to gather unique data for the value folding set.
329 ///
330 virtual void Profile(FoldingSetNodeID &ID) = 0;
331
332#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.
343///
Jim Laskey0d086af2006-02-27 12:43:29 +0000344class DIEInteger : public DIEValue {
345private:
346 uint64_t Integer;
347
348public:
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; }
354
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 }
369
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);
Jim Laskey0d086af2006-02-27 12:43:29 +0000373
374 /// 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;
Jim Laskeyef42a012006-11-02 20:12:39 +0000377
378 /// 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); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000385
386#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.
396///
Jim Laskeyef42a012006-11-02 20:12:39 +0000397class DIEString : public DIEValue {
398public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000399 const std::string String;
400
Dan Gohman81975f62007-08-27 14:50:10 +0000401 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000402
Jim Laskey0d086af2006-02-27 12:43:29 +0000403 // Implement isa/cast/dyncast.
404 static bool classof(const DIEString *) { return true; }
405 static bool classof(const DIEValue *S) { return S->Type == isString; }
406
407 /// EmitValue - Emit string value.
408 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000409 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000410
411 /// SizeOf - Determine size of string value in bytes.
412 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000413 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000414 return String.size() + sizeof(char); // sizeof('\0');
415 }
416
417 /// Profile - Used to gather unique data for the value folding set.
418 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000419 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000420 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000421 ID.AddString(String);
422 }
Jim Laskey5496f012006-11-09 14:52:14 +0000423 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000424
425#ifndef NDEBUG
426 virtual void print(std::ostream &O) {
427 O << "Str: \"" << String << "\"";
428 }
429#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000430};
Jim Laskey063e7652006-01-17 17:31:53 +0000431
Jim Laskey0d086af2006-02-27 12:43:29 +0000432//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000433/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000434//
Jim Laskeyef42a012006-11-02 20:12:39 +0000435class DIEDwarfLabel : public DIEValue {
436public:
437
Jim Laskey0d086af2006-02-27 12:43:29 +0000438 const DWLabel Label;
439
Dan Gohman81975f62007-08-27 14:50:10 +0000440 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000441
Jim Laskey0d086af2006-02-27 12:43:29 +0000442 // Implement isa/cast/dyncast.
443 static bool classof(const DIEDwarfLabel *) { return true; }
444 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
445
446 /// EmitValue - Emit label value.
447 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000448 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000449
450 /// SizeOf - Determine size of label value in bytes.
451 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000452 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000453
454 /// Profile - Used to gather unique data for the value folding set.
455 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000456 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000457 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000458 Label.Profile(ID);
459 }
Jim Laskey5496f012006-11-09 14:52:14 +0000460 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000461
462#ifndef NDEBUG
463 virtual void print(std::ostream &O) {
464 O << "Lbl: ";
465 Label.print(O);
466 }
467#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000468};
Jim Laskey063e7652006-01-17 17:31:53 +0000469
Jim Laskey063e7652006-01-17 17:31:53 +0000470
Jim Laskey0d086af2006-02-27 12:43:29 +0000471//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000472/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000473//
Jim Laskeyef42a012006-11-02 20:12:39 +0000474class DIEObjectLabel : public DIEValue {
475public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000476 const std::string Label;
477
Dan Gohman81975f62007-08-27 14:50:10 +0000478 explicit DIEObjectLabel(const std::string &L)
479 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000480
Jim Laskey0d086af2006-02-27 12:43:29 +0000481 // Implement isa/cast/dyncast.
482 static bool classof(const DIEObjectLabel *) { return true; }
483 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
484
485 /// EmitValue - Emit label value.
486 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000487 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000488
489 /// SizeOf - Determine size of label value in bytes.
490 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000491 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000492
493 /// Profile - Used to gather unique data for the value folding set.
494 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000495 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000496 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000497 ID.AddString(Label);
498 }
Jim Laskey5496f012006-11-09 14:52:14 +0000499 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000500
501#ifndef NDEBUG
502 virtual void print(std::ostream &O) {
503 O << "Obj: " << Label;
504 }
505#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000506};
Jim Laskey063e7652006-01-17 17:31:53 +0000507
Jim Laskey0d086af2006-02-27 12:43:29 +0000508//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000509/// DIESectionOffset - A section offset DIE.
510//
511class DIESectionOffset : public DIEValue {
512public:
513 const DWLabel Label;
514 const DWLabel Section;
515 bool IsEH : 1;
516 bool UseSet : 1;
517
518 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
519 bool isEH = false, bool useSet = true)
520 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
521 IsEH(isEH), UseSet(useSet) {}
522
523 // Implement isa/cast/dyncast.
524 static bool classof(const DIESectionOffset *) { return true; }
525 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
526
527 /// EmitValue - Emit section offset.
528 ///
529 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
530
531 /// SizeOf - Determine size of section offset value in bytes.
532 ///
533 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
534
535 /// Profile - Used to gather unique data for the value folding set.
536 ///
537 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
538 const DWLabel &Section) {
539 ID.AddInteger(isSectionOffset);
540 Label.Profile(ID);
541 Section.Profile(ID);
542 // IsEH and UseSet are specific to the Label/Section that we will emit
543 // the offset for; so Label/Section are enough for uniqueness.
544 }
545 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
546
547#ifndef NDEBUG
548 virtual void print(std::ostream &O) {
549 O << "Off: ";
550 Label.print(O);
551 O << "-";
552 Section.print(O);
553 O << "-" << IsEH << "-" << UseSet;
554 }
555#endif
556};
557
558//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000559/// DIEDelta - A simple label difference DIE.
560///
Jim Laskeyef42a012006-11-02 20:12:39 +0000561class DIEDelta : public DIEValue {
562public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000563 const DWLabel LabelHi;
564 const DWLabel LabelLo;
565
566 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
567 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000568
Jim Laskey0d086af2006-02-27 12:43:29 +0000569 // Implement isa/cast/dyncast.
570 static bool classof(const DIEDelta *) { return true; }
571 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
572
573 /// EmitValue - Emit delta value.
574 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000575 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000576
577 /// SizeOf - Determine size of delta value in bytes.
578 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000579 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000580
581 /// Profile - Used to gather unique data for the value folding set.
582 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000583 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
584 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000585 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000586 LabelHi.Profile(ID);
587 LabelLo.Profile(ID);
588 }
Jim Laskey5496f012006-11-09 14:52:14 +0000589 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000590
591#ifndef NDEBUG
592 virtual void print(std::ostream &O) {
593 O << "Del: ";
594 LabelHi.print(O);
595 O << "-";
596 LabelLo.print(O);
597 }
598#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000599};
Jim Laskey063e7652006-01-17 17:31:53 +0000600
Jim Laskey0d086af2006-02-27 12:43:29 +0000601//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000602/// DIEntry - A pointer to another debug information entry. An instance of this
603/// class can also be used as a proxy for a debug information entry not yet
604/// defined (ie. types.)
605class DIEntry : public DIEValue {
606public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000607 DIE *Entry;
608
Dan Gohman81975f62007-08-27 14:50:10 +0000609 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000610
Jim Laskey0d086af2006-02-27 12:43:29 +0000611 // Implement isa/cast/dyncast.
612 static bool classof(const DIEntry *) { return true; }
613 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
614
Jim Laskeyb8509c52006-03-23 18:07:55 +0000615 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000616 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000617 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000618
Jim Laskeyb8509c52006-03-23 18:07:55 +0000619 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000620 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000621 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000622 return sizeof(int32_t);
623 }
624
625 /// Profile - Used to gather unique data for the value folding set.
626 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000627 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
628 ID.AddInteger(isEntry);
629 ID.AddPointer(Entry);
630 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000631 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000632 ID.AddInteger(isEntry);
633
Jim Laskeyef42a012006-11-02 20:12:39 +0000634 if (Entry) {
635 ID.AddPointer(Entry);
636 } else {
637 ID.AddPointer(this);
638 }
639 }
640
641#ifndef NDEBUG
642 virtual void print(std::ostream &O) {
643 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
644 }
645#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000646};
647
648//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000649/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000650//
Jim Laskeyef42a012006-11-02 20:12:39 +0000651class DIEBlock : public DIEValue, public DIE {
652public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000653 unsigned Size; // Size in bytes excluding size header.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000654
655 DIEBlock()
656 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000657 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000658 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000659 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000660 ~DIEBlock() {
661 }
662
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000663 // Implement isa/cast/dyncast.
664 static bool classof(const DIEBlock *) { return true; }
665 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
666
667 /// ComputeSize - calculate the size of the block.
668 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000669 unsigned ComputeSize(DwarfDebug &DD);
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000670
671 /// BestForm - Choose the best form for data.
672 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000673 unsigned BestForm() const {
674 if ((unsigned char)Size == Size) return DW_FORM_block1;
675 if ((unsigned short)Size == Size) return DW_FORM_block2;
676 if ((unsigned int)Size == Size) return DW_FORM_block4;
677 return DW_FORM_block;
678 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000679
680 /// EmitValue - Emit block data.
681 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000682 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000683
684 /// SizeOf - Determine size of block data in bytes.
685 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000686 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000687
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000688
Jim Laskeyef42a012006-11-02 20:12:39 +0000689 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000690 ///
Reid Spencer97821312006-11-02 23:56:21 +0000691 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000692 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000693 DIE::Profile(ID);
694 }
695
696#ifndef NDEBUG
697 virtual void print(std::ostream &O) {
698 O << "Blk: ";
699 DIE::print(O, 5);
700 }
701#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000702};
703
704//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000705/// CompileUnit - This dwarf writer support class manages information associate
706/// with a source file.
707class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000708private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000709 /// Desc - Compile unit debug descriptor.
710 ///
711 CompileUnitDesc *Desc;
712
713 /// ID - File identifier for source.
714 ///
715 unsigned ID;
716
717 /// Die - Compile unit debug information entry.
718 ///
719 DIE *Die;
720
721 /// DescToDieMap - Tracks the mapping of unit level debug informaton
722 /// descriptors to debug information entries.
723 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
724
725 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
726 /// descriptors to debug information entries using a DIEntry proxy.
727 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
728
729 /// Globals - A map of globally visible named entities for this unit.
730 ///
731 std::map<std::string, DIE *> Globals;
732
733 /// DiesSet - Used to uniquely define dies within the compile unit.
734 ///
735 FoldingSet<DIE> DiesSet;
736
737 /// Dies - List of all dies in the compile unit.
738 ///
739 std::vector<DIE *> Dies;
Jim Laskey0d086af2006-02-27 12:43:29 +0000740
741public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000742 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
743 : Desc(CUD)
744 , ID(I)
745 , Die(D)
746 , DescToDieMap()
747 , DescToDIEntryMap()
748 , Globals()
749 , DiesSet(InitDiesSetSize)
750 , Dies()
751 {}
752
753 ~CompileUnit() {
754 delete Die;
755
756 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
757 delete Dies[i];
758 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000759
Jim Laskeybd761842006-02-27 17:27:12 +0000760 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000761 CompileUnitDesc *getDesc() const { return Desc; }
762 unsigned getID() const { return ID; }
763 DIE* getDie() const { return Die; }
764 std::map<std::string, DIE *> &getGlobals() { return Globals; }
765
766 /// hasContent - Return true if this compile unit has something to write out.
767 ///
768 bool hasContent() const {
769 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000770 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000771
772 /// AddGlobal - Add a new global entity to the compile unit.
773 ///
774 void AddGlobal(const std::string &Name, DIE *Die) {
775 Globals[Name] = Die;
776 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000777
Jim Laskeyef42a012006-11-02 20:12:39 +0000778 /// getDieMapSlotFor - Returns the debug information entry map slot for the
779 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000780 DIE *&getDieMapSlotFor(DebugInfoDesc *DID) {
781 return DescToDieMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000782 }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000783
Jim Laskeyef42a012006-11-02 20:12:39 +0000784 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
785 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000786 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DID) {
787 return DescToDIEntryMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000788 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000789
Jim Laskeyef42a012006-11-02 20:12:39 +0000790 /// AddDie - Adds or interns the DIE to the compile unit.
791 ///
792 DIE *AddDie(DIE &Buffer) {
793 FoldingSetNodeID ID;
794 Buffer.Profile(ID);
795 void *Where;
796 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
797
798 if (!Die) {
799 Die = new DIE(Buffer);
800 DiesSet.InsertNode(Die, Where);
801 this->Die->AddChild(Die);
802 Buffer.Detach();
803 }
804
805 return Die;
806 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000807};
808
Jim Laskey65195462006-10-30 13:35:07 +0000809//===----------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000810/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000811///
Jim Laskey65195462006-10-30 13:35:07 +0000812class Dwarf {
813
Jim Laskey072200c2007-01-29 18:51:14 +0000814protected:
Jim Laskey65195462006-10-30 13:35:07 +0000815
816 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000817 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000818 //
819
820 //
821 /// O - Stream to .s file.
822 ///
823 std::ostream &O;
824
825 /// Asm - Target of Dwarf emission.
826 ///
827 AsmPrinter *Asm;
828
829 /// TAI - Target Asm Printer.
830 const TargetAsmInfo *TAI;
831
832 /// TD - Target data.
833 const TargetData *TD;
834
835 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000836 const TargetRegisterInfo *RI;
Jim Laskey65195462006-10-30 13:35:07 +0000837
838 /// M - Current module.
839 ///
840 Module *M;
841
842 /// MF - Current machine function.
843 ///
844 MachineFunction *MF;
845
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000846 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000847 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000848 MachineModuleInfo *MMI;
Jim Laskey65195462006-10-30 13:35:07 +0000849
Jim Laskey65195462006-10-30 13:35:07 +0000850 /// SubprogramCount - The running count of functions being compiled.
851 ///
852 unsigned SubprogramCount;
Chris Lattner9251f132007-09-24 03:35:37 +0000853
854 /// Flavor - A unique string indicating what dwarf producer this is, used to
855 /// unique labels.
856 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000857
858 unsigned SetCounter;
Chris Lattner9251f132007-09-24 03:35:37 +0000859 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
860 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000861 : O(OS)
862 , Asm(A)
863 , TAI(T)
864 , TD(Asm->TM.getTargetData())
865 , RI(Asm->TM.getRegisterInfo())
866 , M(NULL)
867 , MF(NULL)
868 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000869 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000870 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000871 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000872 {
873 }
874
875public:
876
877 //===--------------------------------------------------------------------===//
878 // Accessors.
879 //
880 AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000881 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000882 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000883 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000884
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000885 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
886 const {
887 if (isInSection && TAI->getDwarfSectionOffsetDirective())
888 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000889 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000890 O << TAI->getData32bitsDirective();
891 else
892 O << TAI->getData64bitsDirective();
893 }
894
Jim Laskeyb82313f2007-02-01 16:31:34 +0000895 /// PrintLabelName - Print label name in form used by Dwarf writer.
896 ///
897 void PrintLabelName(DWLabel Label) const {
898 PrintLabelName(Label.Tag, Label.Number);
899 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000900 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000901 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000902 if (Number) O << Number;
903 }
904
Chris Lattner9251f132007-09-24 03:35:37 +0000905 void PrintLabelName(const char *Tag, unsigned Number,
906 const char *Suffix) const {
907 O << TAI->getPrivateGlobalPrefix() << Tag;
908 if (Number) O << Number;
909 O << Suffix;
910 }
911
Jim Laskeyb82313f2007-02-01 16:31:34 +0000912 /// EmitLabel - Emit location label for internal use by Dwarf.
913 ///
914 void EmitLabel(DWLabel Label) const {
915 EmitLabel(Label.Tag, Label.Number);
916 }
917 void EmitLabel(const char *Tag, unsigned Number) const {
918 PrintLabelName(Tag, Number);
919 O << ":\n";
920 }
921
922 /// EmitReference - Emit a reference to a label.
923 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000924 void EmitReference(DWLabel Label, bool IsPCRelative = false,
925 bool Force32Bit = false) const {
926 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000927 }
928 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000929 bool IsPCRelative = false, bool Force32Bit = false) const {
930 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000931 PrintLabelName(Tag, Number);
932
933 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
934 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000935 void EmitReference(const std::string &Name, bool IsPCRelative = false,
936 bool Force32Bit = false) const {
937 PrintRelDirective(Force32Bit);
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000938
Jim Laskeyb82313f2007-02-01 16:31:34 +0000939 O << Name;
940
941 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
942 }
943
944 /// EmitDifference - Emit the difference between two labels. Some
945 /// assemblers do not behave with absolute expressions with data directives,
946 /// so there is an option (needsSet) to use an intermediary set expression.
947 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000948 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000949 EmitDifference(LabelHi.Tag, LabelHi.Number,
950 LabelLo.Tag, LabelLo.Number,
951 IsSmall);
952 }
953 void EmitDifference(const char *TagHi, unsigned NumberHi,
954 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000955 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000956 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000957 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000958 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000959 O << ",";
960 PrintLabelName(TagHi, NumberHi);
961 O << "-";
962 PrintLabelName(TagLo, NumberLo);
963 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000964
965 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +0000966 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000967 ++SetCounter;
968 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000969 PrintRelDirective(IsSmall);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000970
971 PrintLabelName(TagHi, NumberHi);
972 O << "-";
973 PrintLabelName(TagLo, NumberLo);
974 }
975 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000976
977 void EmitSectionOffset(const char* Label, const char* Section,
978 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000979 bool IsSmall = false, bool isEH = false,
980 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000981 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000982 if (isEH)
983 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
984 else
985 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
986
987 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000988 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000989 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000990 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000991 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000992
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000993 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000994 O << "-";
995 PrintLabelName(Section, SectionNumber);
996 }
997 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000998
999 PrintRelDirective(IsSmall);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001000
Chris Lattner9251f132007-09-24 03:35:37 +00001001 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001002 ++SetCounter;
1003 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001004 PrintRelDirective(IsSmall, true);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001005
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001006 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001007
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001008 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001009 O << "-";
1010 PrintLabelName(Section, SectionNumber);
1011 }
1012 }
1013 }
1014
Jim Laskeyb82313f2007-02-01 16:31:34 +00001015 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1016 /// frame.
1017 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +00001018 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001019 int stackGrowth =
1020 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1021 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001022 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +00001023 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +00001024
1025 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001026 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +00001027 unsigned LabelID = Move.getLabelID();
1028
1029 if (LabelID) {
1030 LabelID = MMI->MappedLabel(LabelID);
1031
1032 // Throw out move if the label is invalid.
1033 if (!LabelID) continue;
1034 }
1035
1036 const MachineLocation &Dst = Move.getDestination();
1037 const MachineLocation &Src = Move.getSource();
1038
1039 // Advance row if new location.
1040 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1041 Asm->EmitInt8(DW_CFA_advance_loc4);
1042 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001043 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1044 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001045
1046 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001047 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001048 IsLocal = true;
1049 }
1050
1051 // If advancing cfa.
1052 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
1053 if (!Src.isRegister()) {
1054 if (Src.getRegister() == MachineLocation::VirtualFP) {
1055 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1056 Asm->EOL("DW_CFA_def_cfa_offset");
1057 } else {
1058 Asm->EmitInt8(DW_CFA_def_cfa);
1059 Asm->EOL("DW_CFA_def_cfa");
Dale Johannesenb97aec62007-11-13 19:13:01 +00001060 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001061 Asm->EOL("Register");
1062 }
1063
1064 int Offset = -Src.getOffset();
1065
1066 Asm->EmitULEB128Bytes(Offset);
1067 Asm->EOL("Offset");
1068 } else {
1069 assert(0 && "Machine move no supported yet.");
1070 }
1071 } else if (Src.isRegister() &&
1072 Src.getRegister() == MachineLocation::VirtualFP) {
1073 if (Dst.isRegister()) {
1074 Asm->EmitInt8(DW_CFA_def_cfa_register);
1075 Asm->EOL("DW_CFA_def_cfa_register");
Dale Johannesenb97aec62007-11-13 19:13:01 +00001076 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getRegister(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001077 Asm->EOL("Register");
1078 } else {
1079 assert(0 && "Machine move no supported yet.");
1080 }
1081 } else {
Dale Johannesenb97aec62007-11-13 19:13:01 +00001082 unsigned Reg = RI->getDwarfRegNum(Src.getRegister(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001083 int Offset = Dst.getOffset() / stackGrowth;
1084
1085 if (Offset < 0) {
1086 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1087 Asm->EOL("DW_CFA_offset_extended_sf");
1088 Asm->EmitULEB128Bytes(Reg);
1089 Asm->EOL("Reg");
1090 Asm->EmitSLEB128Bytes(Offset);
1091 Asm->EOL("Offset");
1092 } else if (Reg < 64) {
1093 Asm->EmitInt8(DW_CFA_offset + Reg);
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +00001094 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
Jim Laskeyb82313f2007-02-01 16:31:34 +00001095 Asm->EmitULEB128Bytes(Offset);
1096 Asm->EOL("Offset");
1097 } else {
1098 Asm->EmitInt8(DW_CFA_offset_extended);
1099 Asm->EOL("DW_CFA_offset_extended");
1100 Asm->EmitULEB128Bytes(Reg);
1101 Asm->EOL("Reg");
1102 Asm->EmitULEB128Bytes(Offset);
1103 Asm->EOL("Offset");
1104 }
1105 }
1106 }
1107 }
1108
Jim Laskey072200c2007-01-29 18:51:14 +00001109};
1110
1111//===----------------------------------------------------------------------===//
1112/// DwarfDebug - Emits Dwarf debug directives.
1113///
1114class DwarfDebug : public Dwarf {
1115
1116private:
Jim Laskey65195462006-10-30 13:35:07 +00001117 //===--------------------------------------------------------------------===//
1118 // Attributes used to construct specific Dwarf sections.
1119 //
1120
1121 /// CompileUnits - All the compile units involved in this build. The index
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001122 /// of each entry in this vector corresponds to the sources in MMI.
Jim Laskey65195462006-10-30 13:35:07 +00001123 std::vector<CompileUnit *> CompileUnits;
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001124
Jim Laskeyef42a012006-11-02 20:12:39 +00001125 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001126 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001127 FoldingSet<DIEAbbrev> AbbreviationsSet;
1128
1129 /// Abbreviations - A list of all the unique abbreviations in use.
1130 ///
1131 std::vector<DIEAbbrev *> Abbreviations;
Jim Laskey65195462006-10-30 13:35:07 +00001132
Jim Laskeyef42a012006-11-02 20:12:39 +00001133 /// ValuesSet - Used to uniquely define values.
1134 ///
1135 FoldingSet<DIEValue> ValuesSet;
1136
1137 /// Values - A list of all the unique values in use.
1138 ///
1139 std::vector<DIEValue *> Values;
1140
Jim Laskey65195462006-10-30 13:35:07 +00001141 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001142 ///
Jim Laskey65195462006-10-30 13:35:07 +00001143 UniqueVector<std::string> StringPool;
1144
1145 /// UnitMap - Map debug information descriptor to compile unit.
1146 ///
1147 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
1148
Jim Laskey65195462006-10-30 13:35:07 +00001149 /// SectionMap - Provides a unique id per text section.
1150 ///
1151 UniqueVector<std::string> SectionMap;
1152
1153 /// SectionSourceLines - Tracks line numbers per text section.
1154 ///
1155 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1156
Jim Laskeybacd3042007-02-21 22:48:45 +00001157 /// didInitial - Flag to indicate if initial emission has been done.
1158 ///
1159 bool didInitial;
1160
1161 /// shouldEmit - Flag to indicate if debug information should be emitted.
1162 ///
1163 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001164
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001165 struct FunctionDebugFrameInfo {
1166 unsigned Number;
1167 std::vector<MachineMove> Moves;
1168
1169 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001170 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001171 };
1172
1173 std::vector<FunctionDebugFrameInfo> DebugFrames;
1174
Jim Laskey65195462006-10-30 13:35:07 +00001175public:
Jim Laskeybacd3042007-02-21 22:48:45 +00001176
1177 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1178 ///
1179 bool ShouldEmitDwarf() const { return shouldEmit; }
Jim Laskey65195462006-10-30 13:35:07 +00001180
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001181 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Jim Laskey65195462006-10-30 13:35:07 +00001182 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001183 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1184 // Profile the node so that we can make it unique.
1185 FoldingSetNodeID ID;
1186 Abbrev.Profile(ID);
1187
1188 // Check the set for priors.
1189 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
1190
1191 // If it's newly added.
1192 if (InSet == &Abbrev) {
1193 // Add to abbreviation list.
1194 Abbreviations.push_back(&Abbrev);
1195 // Assign the vector position + 1 as its number.
1196 Abbrev.setNumber(Abbreviations.size());
1197 } else {
1198 // Assign existing abbreviation number.
1199 Abbrev.setNumber(InSet->getNumber());
1200 }
1201 }
1202
Jim Laskey65195462006-10-30 13:35:07 +00001203 /// NewString - Add a string to the constant pool and returns a label.
1204 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001205 DWLabel NewString(const std::string &String) {
1206 unsigned StringID = StringPool.insert(String);
1207 return DWLabel("string", StringID);
1208 }
Jim Laskey65195462006-10-30 13:35:07 +00001209
Jim Laskeyef42a012006-11-02 20:12:39 +00001210 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1211 /// entry.
1212 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1213 DIEntry *Value;
1214
1215 if (Entry) {
1216 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001217 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001218 void *Where;
1219 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
1220
Jim Laskeyf6733882006-11-02 21:48:18 +00001221 if (Value) return Value;
Jim Laskeyef42a012006-11-02 20:12:39 +00001222
1223 Value = new DIEntry(Entry);
1224 ValuesSet.InsertNode(Value, Where);
1225 } else {
1226 Value = new DIEntry(Entry);
1227 }
1228
1229 Values.push_back(Value);
1230 return Value;
1231 }
1232
1233 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1234 ///
1235 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1236 Value->Entry = Entry;
1237 // Add to values set if not already there. If it is, we merely have a
1238 // duplicate in the values list (no harm.)
1239 ValuesSet.GetOrInsertNode(Value);
1240 }
1241
1242 /// AddUInt - Add an unsigned integer attribute data and value.
1243 ///
1244 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1245 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1246
1247 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001248 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001249 void *Where;
1250 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1251 if (!Value) {
1252 Value = new DIEInteger(Integer);
1253 ValuesSet.InsertNode(Value, Where);
1254 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001255 }
1256
1257 Die->AddValue(Attribute, Form, Value);
1258 }
1259
1260 /// AddSInt - Add an signed integer attribute data and value.
1261 ///
1262 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1263 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1264
1265 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001266 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001267 void *Where;
1268 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1269 if (!Value) {
1270 Value = new DIEInteger(Integer);
1271 ValuesSet.InsertNode(Value, Where);
1272 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001273 }
1274
1275 Die->AddValue(Attribute, Form, Value);
1276 }
1277
1278 /// AddString - Add a std::string attribute data and value.
1279 ///
1280 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1281 const std::string &String) {
1282 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001283 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001284 void *Where;
1285 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1286 if (!Value) {
1287 Value = new DIEString(String);
1288 ValuesSet.InsertNode(Value, Where);
1289 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001290 }
1291
1292 Die->AddValue(Attribute, Form, Value);
1293 }
1294
1295 /// AddLabel - Add a Dwarf label attribute data and value.
1296 ///
1297 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1298 const DWLabel &Label) {
1299 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001300 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001301 void *Where;
1302 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1303 if (!Value) {
1304 Value = new DIEDwarfLabel(Label);
1305 ValuesSet.InsertNode(Value, Where);
1306 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001307 }
1308
1309 Die->AddValue(Attribute, Form, Value);
1310 }
1311
1312 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1313 ///
1314 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1315 const std::string &Label) {
1316 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001317 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001318 void *Where;
1319 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1320 if (!Value) {
1321 Value = new DIEObjectLabel(Label);
1322 ValuesSet.InsertNode(Value, Where);
1323 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001324 }
1325
1326 Die->AddValue(Attribute, Form, Value);
1327 }
1328
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001329 /// AddSectionOffset - Add a section offset label attribute data and value.
1330 ///
1331 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1332 const DWLabel &Label, const DWLabel &Section,
1333 bool isEH = false, bool useSet = true) {
1334 FoldingSetNodeID ID;
1335 DIESectionOffset::Profile(ID, Label, Section);
1336 void *Where;
1337 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1338 if (!Value) {
1339 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1340 ValuesSet.InsertNode(Value, Where);
1341 Values.push_back(Value);
1342 }
1343
1344 Die->AddValue(Attribute, Form, Value);
1345 }
1346
Jim Laskeyef42a012006-11-02 20:12:39 +00001347 /// AddDelta - Add a label delta attribute data and value.
1348 ///
1349 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1350 const DWLabel &Hi, const DWLabel &Lo) {
1351 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001352 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001353 void *Where;
1354 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1355 if (!Value) {
1356 Value = new DIEDelta(Hi, Lo);
1357 ValuesSet.InsertNode(Value, Where);
1358 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001359 }
1360
1361 Die->AddValue(Attribute, Form, Value);
1362 }
1363
1364 /// AddDIEntry - Add a DIE attribute data and value.
1365 ///
1366 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1367 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1368 }
1369
1370 /// AddBlock - Add block data.
1371 ///
1372 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1373 Block->ComputeSize(*this);
1374 FoldingSetNodeID ID;
1375 Block->Profile(ID);
1376 void *Where;
1377 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1378 if (!Value) {
1379 Value = Block;
1380 ValuesSet.InsertNode(Value, Where);
1381 Values.push_back(Value);
1382 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001383 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001384 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001385 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001386 }
1387
1388 Die->AddValue(Attribute, Block->BestForm(), Value);
1389 }
1390
Jim Laskey65195462006-10-30 13:35:07 +00001391private:
1392
1393 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001394 /// entry.
1395 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1396 if (File && Line) {
1397 CompileUnit *FileUnit = FindCompileUnit(File);
1398 unsigned FileID = FileUnit->getID();
1399 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1400 AddUInt(Die, DW_AT_decl_line, 0, Line);
1401 }
1402 }
Jim Laskey65195462006-10-30 13:35:07 +00001403
1404 /// AddAddress - Add an address attribute to a die based on the location
1405 /// provided.
1406 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001407 const MachineLocation &Location) {
Dale Johannesenb97aec62007-11-13 19:13:01 +00001408 unsigned Reg = RI->getDwarfRegNum(Location.getRegister(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001409 DIEBlock *Block = new DIEBlock();
1410
1411 if (Location.isRegister()) {
1412 if (Reg < 32) {
1413 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1414 } else {
1415 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1416 AddUInt(Block, 0, DW_FORM_udata, Reg);
1417 }
1418 } else {
1419 if (Reg < 32) {
1420 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1421 } else {
1422 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1423 AddUInt(Block, 0, DW_FORM_udata, Reg);
1424 }
1425 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1426 }
1427
1428 AddBlock(Die, Attribute, 0, Block);
1429 }
1430
1431 /// AddBasicType - Add a new basic type attribute to the specified entity.
1432 ///
1433 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1434 const std::string &Name,
1435 unsigned Encoding, unsigned Size) {
1436 DIE *Die = ConstructBasicType(Unit, Name, Encoding, Size);
1437 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1438 }
1439
1440 /// ConstructBasicType - Construct a new basic type.
1441 ///
1442 DIE *ConstructBasicType(CompileUnit *Unit,
1443 const std::string &Name,
1444 unsigned Encoding, unsigned Size) {
1445 DIE Buffer(DW_TAG_base_type);
1446 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1447 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1448 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1449 return Unit->AddDie(Buffer);
1450 }
1451
1452 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1453 ///
1454 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1455 DIE *Die = ConstructPointerType(Unit, Name);
1456 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1457 }
1458
1459 /// ConstructPointerType - Construct a new pointer type.
1460 ///
1461 DIE *ConstructPointerType(CompileUnit *Unit, const std::string &Name) {
1462 DIE Buffer(DW_TAG_pointer_type);
Dan Gohman82482942007-09-27 23:12:31 +00001463 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Jim Laskeyef42a012006-11-02 20:12:39 +00001464 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1465 return Unit->AddDie(Buffer);
1466 }
1467
1468 /// AddType - Add a new type attribute to the specified entity.
1469 ///
1470 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1471 if (!TyDesc) {
Jim Laskey2b935d52007-01-26 14:19:17 +00001472 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001473 } else {
1474 // Check for pre-existence.
1475 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
1476
1477 // If it exists then use the existing value.
1478 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001479 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1480 return;
1481 }
1482
1483 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1484 // FIXME - Not sure why programs and variables are coming through here.
1485 // Short cut for handling subprogram types (not really a TyDesc.)
1486 AddPointerType(Entity, Unit, SubprogramTy->getName());
1487 } else if (GlobalVariableDesc *GlobalTy =
1488 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1489 // FIXME - Not sure why programs and variables are coming through here.
1490 // Short cut for handling global variable types (not really a TyDesc.)
1491 AddPointerType(Entity, Unit, GlobalTy->getName());
1492 } else {
1493 // Set up proxy.
1494 Slot = NewDIEntry();
1495
1496 // Construct type.
1497 DIE Buffer(DW_TAG_base_type);
1498 ConstructType(Buffer, TyDesc, Unit);
1499
1500 // Add debug information entry to entity and unit.
1501 DIE *Die = Unit->AddDie(Buffer);
1502 SetDIEntry(Slot, Die);
1503 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1504 }
1505 }
1506 }
1507
1508 /// ConstructType - Adds all the required attributes to the type.
1509 ///
1510 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1511 // Get core information.
1512 const std::string &Name = TyDesc->getName();
1513 uint64_t Size = TyDesc->getSize() >> 3;
1514
1515 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1516 // Fundamental types like int, float, bool
1517 Buffer.setTag(DW_TAG_base_type);
1518 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1519 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00001520 // Fetch tag.
1521 unsigned Tag = DerivedTy->getTag();
1522 // FIXME - Workaround for templates.
1523 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1524 // Pointers, typedefs et al.
1525 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00001526 // Map to main type, void will not have a type.
1527 if (TypeDesc *FromTy = DerivedTy->getFromType())
1528 AddType(&Buffer, FromTy, Unit);
1529 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1530 // Fetch tag.
1531 unsigned Tag = CompTy->getTag();
1532
1533 // Set tag accordingly.
1534 if (Tag == DW_TAG_vector_type)
1535 Buffer.setTag(DW_TAG_array_type);
1536 else
1537 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00001538
Jim Laskeyef42a012006-11-02 20:12:39 +00001539 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1540
1541 switch (Tag) {
1542 case DW_TAG_vector_type:
1543 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1544 // Fall thru
1545 case DW_TAG_array_type: {
1546 // Add element type.
1547 if (TypeDesc *FromTy = CompTy->getFromType())
1548 AddType(&Buffer, FromTy, Unit);
1549
1550 // Don't emit size attribute.
1551 Size = 0;
1552
1553 // Construct an anonymous type for index type.
Jim Laskey2b935d52007-01-26 14:19:17 +00001554 DIE *IndexTy = ConstructBasicType(Unit, "", DW_ATE_signed,
1555 sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001556
1557 // Add subranges to array type.
1558 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1559 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1560 int64_t Lo = SRD->getLo();
1561 int64_t Hi = SRD->getHi();
1562 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1563
1564 // If a range is available.
1565 if (Lo != Hi) {
1566 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1567 // Only add low if non-zero.
1568 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1569 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1570 }
1571
1572 Buffer.AddChild(Subrange);
1573 }
1574 break;
1575 }
1576 case DW_TAG_structure_type:
1577 case DW_TAG_union_type: {
1578 // Add elements to structure type.
1579 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1580 DebugInfoDesc *Element = Elements[i];
1581
1582 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1583 // Add field or base class.
1584
1585 unsigned Tag = MemberDesc->getTag();
1586
1587 // Extract the basic information.
1588 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001589 uint64_t Size = MemberDesc->getSize();
1590 uint64_t Align = MemberDesc->getAlign();
1591 uint64_t Offset = MemberDesc->getOffset();
1592
1593 // Construct member debug information entry.
1594 DIE *Member = new DIE(Tag);
1595
1596 // Add name if not "".
1597 if (!Name.empty())
1598 AddString(Member, DW_AT_name, DW_FORM_string, Name);
1599 // Add location if available.
1600 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1601
1602 // Most of the time the field info is the same as the members.
1603 uint64_t FieldSize = Size;
1604 uint64_t FieldAlign = Align;
1605 uint64_t FieldOffset = Offset;
1606
Jim Laskeyee5f9272006-12-22 20:03:42 +00001607 // Set the member type.
1608 TypeDesc *FromTy = MemberDesc->getFromType();
1609 AddType(Member, FromTy, Unit);
1610
1611 // Walk up typedefs until a real size is found.
1612 while (FromTy) {
1613 if (FromTy->getTag() != DW_TAG_typedef) {
1614 FieldSize = FromTy->getSize();
1615 FieldAlign = FromTy->getSize();
1616 break;
1617 }
1618
Dan Gohman275769a2007-07-23 20:24:29 +00001619 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
Jim Laskeyef42a012006-11-02 20:12:39 +00001620 }
1621
1622 // Unless we have a bit field.
1623 if (Tag == DW_TAG_member && FieldSize != Size) {
1624 // Construct the alignment mask.
1625 uint64_t AlignMask = ~(FieldAlign - 1);
1626 // Determine the high bit + 1 of the declared size.
1627 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1628 // Work backwards to determine the base offset of the field.
1629 FieldOffset = HiMark - FieldSize;
1630 // Now normalize offset to the field.
1631 Offset -= FieldOffset;
1632
1633 // Maybe we need to work from the other end.
1634 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1635
1636 // Add size and offset.
1637 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1638 AddUInt(Member, DW_AT_bit_size, 0, Size);
1639 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1640 }
1641
1642 // Add computation for offset.
1643 DIEBlock *Block = new DIEBlock();
1644 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1645 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1646 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1647
1648 // Add accessibility (public default unless is base class.
1649 if (MemberDesc->isProtected()) {
1650 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1651 } else if (MemberDesc->isPrivate()) {
1652 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1653 } else if (Tag == DW_TAG_inheritance) {
1654 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1655 }
1656
1657 Buffer.AddChild(Member);
1658 } else if (GlobalVariableDesc *StaticDesc =
1659 dyn_cast<GlobalVariableDesc>(Element)) {
1660 // Add static member.
1661
1662 // Construct member debug information entry.
1663 DIE *Static = new DIE(DW_TAG_variable);
1664
1665 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001666 const std::string &Name = StaticDesc->getName();
1667 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001668 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001669 if (!LinkageName.empty()) {
1670 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1671 LinkageName);
1672 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001673
1674 // Add location.
1675 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1676
1677 // Add type.
1678 if (TypeDesc *StaticTy = StaticDesc->getType())
1679 AddType(Static, StaticTy, Unit);
1680
1681 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001682 if (!StaticDesc->isStatic())
1683 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001684 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
1685
1686 Buffer.AddChild(Static);
1687 } else if (SubprogramDesc *MethodDesc =
1688 dyn_cast<SubprogramDesc>(Element)) {
1689 // Add member function.
1690
1691 // Construct member debug information entry.
1692 DIE *Method = new DIE(DW_TAG_subprogram);
1693
1694 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001695 const std::string &Name = MethodDesc->getName();
1696 const std::string &LinkageName = MethodDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001697
Jim Laskey2172f962006-11-30 14:35:45 +00001698 AddString(Method, DW_AT_name, DW_FORM_string, Name);
1699 bool IsCTor = TyDesc->getName() == Name;
1700
1701 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001702 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001703 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001704 }
1705
1706 // Add location.
1707 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1708
1709 // Add type.
1710 if (CompositeTypeDesc *MethodTy =
1711 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1712 // Get argument information.
1713 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1714
1715 // If not a ctor.
1716 if (!IsCTor) {
1717 // Add return type.
1718 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1719 }
1720
1721 // Add arguments.
1722 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1723 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1724 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1725 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1726 Method->AddChild(Arg);
1727 }
1728 }
1729
1730 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001731 if (!MethodDesc->isStatic())
1732 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001733 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
1734
1735 Buffer.AddChild(Method);
1736 }
1737 }
1738 break;
1739 }
1740 case DW_TAG_enumeration_type: {
1741 // Add enumerators to enumeration type.
1742 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1743 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1744 const std::string &Name = ED->getName();
1745 int64_t Value = ED->getValue();
1746 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1747 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1748 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1749 Buffer.AddChild(Enumerator);
1750 }
1751
1752 break;
1753 }
1754 case DW_TAG_subroutine_type: {
1755 // Add prototype flag.
1756 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1757 // Add return type.
1758 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
1759
1760 // Add arguments.
1761 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1762 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1763 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1764 Buffer.AddChild(Arg);
1765 }
1766
1767 break;
1768 }
1769 default: break;
1770 }
1771 }
1772
1773 // Add size if non-zero (derived types don't have a size.)
1774 if (Size) AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1775 // Add name if not anonymous or intermediate type.
1776 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1777 // Add source line info if available.
1778 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
1779 }
1780
1781 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00001782 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001783 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1784 // Construct debug information entry.
1785 DIE *Die = new DIE(DW_TAG_compile_unit);
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001786 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
1787 DWLabel("section_line", 0), DWLabel("section_line", 0), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001788 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1789 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1790 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1791 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1792
1793 // Construct compile unit.
1794 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1795
1796 // Add Unit to compile unit map.
1797 DescToUnitMap[UnitDesc] = Unit;
1798
1799 return Unit;
1800 }
1801
Jim Laskey9d4209f2006-11-07 19:33:46 +00001802 /// GetBaseCompileUnit - Get the main compile unit.
1803 ///
1804 CompileUnit *GetBaseCompileUnit() const {
1805 CompileUnit *Unit = CompileUnits[0];
1806 assert(Unit && "Missing compile unit.");
1807 return Unit;
1808 }
1809
Jim Laskey65195462006-10-30 13:35:07 +00001810 /// FindCompileUnit - Get the compile unit for the given descriptor.
1811 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001812 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001813 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00001814 assert(Unit && "Missing compile unit.");
1815 return Unit;
1816 }
1817
1818 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00001819 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001820 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1821 // Get the compile unit context.
1822 CompileUnitDesc *UnitDesc =
1823 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001824 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001825
1826 // Check for pre-existence.
1827 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1828 if (Slot) return Slot;
1829
1830 // Get the global variable itself.
1831 GlobalVariable *GV = GVD->getGlobalVariable();
1832
Jim Laskey2172f962006-11-30 14:35:45 +00001833 const std::string &Name = GVD->getName();
1834 const std::string &FullName = GVD->getFullName();
1835 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001836 // Create the global's variable DIE.
1837 DIE *VariableDie = new DIE(DW_TAG_variable);
1838 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001839 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001840 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001841 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001842 }
Jim Laskey6488a072007-01-08 22:15:18 +00001843 AddType(VariableDie, GVD->getType(), Unit);
1844 if (!GVD->isStatic())
1845 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001846
1847 // Add source line info if available.
1848 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1849
Jim Laskeyef42a012006-11-02 20:12:39 +00001850 // Add address.
1851 DIEBlock *Block = new DIEBlock();
1852 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00001853 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
1854 AddBlock(VariableDie, DW_AT_location, 0, Block);
Jim Laskeyef42a012006-11-02 20:12:39 +00001855
1856 // Add to map.
1857 Slot = VariableDie;
1858
1859 // Add to context owner.
1860 Unit->getDie()->AddChild(VariableDie);
1861
1862 // Expose as global.
1863 // FIXME - need to check external flag.
Jim Laskey2172f962006-11-30 14:35:45 +00001864 Unit->AddGlobal(FullName, VariableDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001865
1866 return VariableDie;
1867 }
Jim Laskey65195462006-10-30 13:35:07 +00001868
1869 /// NewSubprogram - Add a new subprogram DIE.
1870 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001871 DIE *NewSubprogram(SubprogramDesc *SPD) {
1872 // Get the compile unit context.
1873 CompileUnitDesc *UnitDesc =
1874 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001875 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001876
1877 // Check for pre-existence.
1878 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1879 if (Slot) return Slot;
1880
1881 // Gather the details (simplify add attribute code.)
Jim Laskey2172f962006-11-30 14:35:45 +00001882 const std::string &Name = SPD->getName();
1883 const std::string &FullName = SPD->getFullName();
1884 const std::string &LinkageName = SPD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001885
1886 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1887 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001888 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001889 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001890 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001891 }
1892 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
Jim Laskey6488a072007-01-08 22:15:18 +00001893 if (!SPD->isStatic())
1894 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001895 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
1896
1897 // Add source line info if available.
1898 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1899
1900 // Add to map.
1901 Slot = SubprogramDie;
1902
1903 // Add to context owner.
1904 Unit->getDie()->AddChild(SubprogramDie);
1905
1906 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00001907 Unit->AddGlobal(FullName, SubprogramDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001908
1909 return SubprogramDie;
1910 }
Jim Laskey65195462006-10-30 13:35:07 +00001911
1912 /// NewScopeVariable - Create a new scope variable.
1913 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001914 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1915 // Get the descriptor.
1916 VariableDesc *VD = DV->getDesc();
1917
1918 // Translate tag to proper Dwarf tag. The result variable is dropped for
1919 // now.
1920 unsigned Tag;
1921 switch (VD->getTag()) {
1922 case DW_TAG_return_variable: return NULL;
1923 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1924 case DW_TAG_auto_variable: // fall thru
1925 default: Tag = DW_TAG_variable; break;
1926 }
1927
1928 // Define variable debug information entry.
1929 DIE *VariableDie = new DIE(Tag);
1930 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1931
1932 // Add source line info if available.
1933 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1934
1935 // Add variable type.
1936 AddType(VariableDie, VD->getType(), Unit);
1937
1938 // Add variable address.
1939 MachineLocation Location;
Evan Cheng72bebb92008-01-31 03:37:28 +00001940 Location.set(RI->getFrameRegister(*MF),
1941 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Jim Laskeyef42a012006-11-02 20:12:39 +00001942 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001943
Jim Laskeyef42a012006-11-02 20:12:39 +00001944 return VariableDie;
1945 }
Jim Laskey65195462006-10-30 13:35:07 +00001946
1947 /// ConstructScope - Construct the components of a scope.
1948 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001949 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00001950 unsigned ParentStartID, unsigned ParentEndID,
1951 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001952 // Add variables to scope.
1953 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1954 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1955 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1956 if (VariableDie) ParentDie->AddChild(VariableDie);
1957 }
1958
1959 // Add nested scopes.
1960 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1961 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1962 // Define the Scope debug information entry.
1963 DebugScope *Scope = Scopes[j];
1964 // FIXME - Ignore inlined functions for the time being.
1965 if (!Scope->getParent()) continue;
1966
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001967 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1968 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00001969
Jim Laskey9d4209f2006-11-07 19:33:46 +00001970 // Ignore empty scopes.
1971 if (StartID == EndID && StartID != 0) continue;
1972 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00001973
Jim Laskey36729dd2006-11-29 16:55:57 +00001974 if (StartID == ParentStartID && EndID == ParentEndID) {
1975 // Just add stuff to the parent scope.
1976 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001977 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00001978 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1979
1980 // Add the scope bounds.
1981 if (StartID) {
1982 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00001983 DWLabel("label", StartID));
Jim Laskey36729dd2006-11-29 16:55:57 +00001984 } else {
1985 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1986 DWLabel("func_begin", SubprogramCount));
1987 }
1988 if (EndID) {
1989 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00001990 DWLabel("label", EndID));
Jim Laskey36729dd2006-11-29 16:55:57 +00001991 } else {
1992 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1993 DWLabel("func_end", SubprogramCount));
1994 }
1995
1996 // Add the scope contents.
1997 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
1998 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001999 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002000 }
2001 }
Jim Laskey65195462006-10-30 13:35:07 +00002002
2003 /// ConstructRootScope - Construct the scope for the subprogram.
2004 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002005 void ConstructRootScope(DebugScope *RootScope) {
2006 // Exit if there is no root scope.
2007 if (!RootScope) return;
2008
2009 // Get the subprogram debug information entry.
2010 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
2011
2012 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00002013 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002014
2015 // Get the subprogram die.
2016 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2017 assert(SPDie && "Missing subprogram descriptor");
2018
2019 // Add the function bounds.
2020 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2021 DWLabel("func_begin", SubprogramCount));
2022 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2023 DWLabel("func_end", SubprogramCount));
2024 MachineLocation Location(RI->getFrameRegister(*MF));
2025 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002026
Jim Laskey36729dd2006-11-29 16:55:57 +00002027 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002028 }
Jim Laskey65195462006-10-30 13:35:07 +00002029
Jim Laskeyef42a012006-11-02 20:12:39 +00002030 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2031 /// tools to recognize the object file contains Dwarf information.
2032 void EmitInitial() {
2033 // Check to see if we already emitted intial headers.
2034 if (didInitial) return;
2035 didInitial = true;
2036
2037 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002038 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002039 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002040 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002041 }
2042 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2043 EmitLabel("section_info", 0);
2044 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2045 EmitLabel("section_abbrev", 0);
2046 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2047 EmitLabel("section_aranges", 0);
2048 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2049 EmitLabel("section_macinfo", 0);
2050 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2051 EmitLabel("section_line", 0);
2052 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2053 EmitLabel("section_loc", 0);
2054 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2055 EmitLabel("section_pubnames", 0);
2056 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2057 EmitLabel("section_str", 0);
2058 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2059 EmitLabel("section_ranges", 0);
2060
2061 Asm->SwitchToTextSection(TAI->getTextSection());
2062 EmitLabel("text_begin", 0);
2063 Asm->SwitchToDataSection(TAI->getDataSection());
2064 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002065 }
2066
Jim Laskey65195462006-10-30 13:35:07 +00002067 /// EmitDIE - Recusively Emits a debug information entry.
2068 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002069 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002070 // Get the abbreviation for this DIE.
2071 unsigned AbbrevNumber = Die->getAbbrevNumber();
2072 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2073
Jim Laskeybacd3042007-02-21 22:48:45 +00002074 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002075
2076 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002077 Asm->EmitULEB128Bytes(AbbrevNumber);
2078 Asm->EOL(std::string("Abbrev [" +
2079 utostr(AbbrevNumber) +
2080 "] 0x" + utohexstr(Die->getOffset()) +
2081 ":0x" + utohexstr(Die->getSize()) + " " +
2082 TagString(Abbrev->getTag())));
Jim Laskeyef42a012006-11-02 20:12:39 +00002083
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002084 std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyef42a012006-11-02 20:12:39 +00002085 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2086
2087 // Emit the DIE attribute values.
2088 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2089 unsigned Attr = AbbrevData[i].getAttribute();
2090 unsigned Form = AbbrevData[i].getForm();
2091 assert(Form && "Too many attributes for DIE (check abbreviation)");
2092
2093 switch (Attr) {
2094 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002095 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002096 break;
2097 }
2098 default: {
2099 // Emit an attribute using the defined form.
2100 Values[i]->EmitValue(*this, Form);
2101 break;
2102 }
2103 }
2104
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002105 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002106 }
2107
2108 // Emit the DIE children if any.
2109 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2110 const std::vector<DIE *> &Children = Die->getChildren();
2111
2112 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2113 EmitDIE(Children[j]);
2114 }
2115
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002116 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002117 }
2118 }
2119
Jim Laskey65195462006-10-30 13:35:07 +00002120 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2121 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002122 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2123 // Get the children.
2124 const std::vector<DIE *> &Children = Die->getChildren();
2125
2126 // If not last sibling and has children then add sibling offset attribute.
2127 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2128
2129 // Record the abbreviation.
2130 AssignAbbrevNumber(Die->getAbbrev());
2131
2132 // Get the abbreviation for this DIE.
2133 unsigned AbbrevNumber = Die->getAbbrevNumber();
2134 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2135
2136 // Set DIE offset
2137 Die->setOffset(Offset);
2138
2139 // Start the size with the size of abbreviation code.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002140 Offset += Asm->SizeULEB128(AbbrevNumber);
Jim Laskeyef42a012006-11-02 20:12:39 +00002141
2142 const std::vector<DIEValue *> &Values = Die->getValues();
2143 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2144
2145 // Size the DIE attribute values.
2146 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2147 // Size attribute value.
2148 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2149 }
2150
2151 // Size the DIE children if any.
2152 if (!Children.empty()) {
2153 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2154 "Children flag not set");
2155
2156 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2157 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2158 }
2159
2160 // End of children marker.
2161 Offset += sizeof(int8_t);
2162 }
2163
2164 Die->setSize(Offset - Die->getOffset());
2165 return Offset;
2166 }
Jim Laskey65195462006-10-30 13:35:07 +00002167
2168 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2169 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002170 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002171 // Process base compile unit.
2172 CompileUnit *Unit = GetBaseCompileUnit();
2173 // Compute size of compile unit header
2174 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2175 sizeof(int16_t) + // DWARF version number
2176 sizeof(int32_t) + // Offset Into Abbrev. Section
2177 sizeof(int8_t); // Pointer Size (in bytes)
2178 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002179 }
2180
Jim Laskey65195462006-10-30 13:35:07 +00002181 /// EmitDebugInfo - Emit the debug info section.
2182 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002183 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002184 // Start debug info section.
2185 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2186
Jim Laskey5496f012006-11-09 14:52:14 +00002187 CompileUnit *Unit = GetBaseCompileUnit();
2188 DIE *Die = Unit->getDie();
2189 // Emit the compile units header.
2190 EmitLabel("info_begin", Unit->getID());
2191 // Emit size of content not including length itself
2192 unsigned ContentSize = Die->getSize() +
2193 sizeof(int16_t) + // DWARF version number
2194 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00002195 sizeof(int8_t) + // Pointer Size (in bytes)
2196 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Laskey5496f012006-11-09 14:52:14 +00002197
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002198 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2199 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002200 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002201 Asm->EOL("Offset Into Abbrev. Section");
Dan Gohman82482942007-09-27 23:12:31 +00002202 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
Jim Laskey5496f012006-11-09 14:52:14 +00002203
2204 EmitDIE(Die);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002205 // FIXME - extra padding for gdb bug.
2206 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2207 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2208 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2209 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Jim Laskey5496f012006-11-09 14:52:14 +00002210 EmitLabel("info_end", Unit->getID());
2211
Jim Laskeybacd3042007-02-21 22:48:45 +00002212 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002213 }
2214
Jim Laskey65195462006-10-30 13:35:07 +00002215 /// EmitAbbreviations - Emit the abbreviation section.
2216 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002217 void EmitAbbreviations() const {
2218 // Check to see if it is worth the effort.
2219 if (!Abbreviations.empty()) {
2220 // Start the debug abbrev section.
2221 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2222
2223 EmitLabel("abbrev_begin", 0);
2224
2225 // For each abbrevation.
2226 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2227 // Get abbreviation data
2228 const DIEAbbrev *Abbrev = Abbreviations[i];
2229
2230 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002231 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2232 Asm->EOL("Abbreviation Code");
Jim Laskeyef42a012006-11-02 20:12:39 +00002233
2234 // Emit the abbreviations data.
2235 Abbrev->Emit(*this);
2236
Jim Laskeybacd3042007-02-21 22:48:45 +00002237 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002238 }
2239
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002240 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002241 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002242
Jim Laskeyef42a012006-11-02 20:12:39 +00002243 EmitLabel("abbrev_end", 0);
2244
Jim Laskeybacd3042007-02-21 22:48:45 +00002245 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002246 }
2247 }
2248
Jim Laskey65195462006-10-30 13:35:07 +00002249 /// EmitDebugLines - Emit source line information.
2250 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002251 void EmitDebugLines() {
Dan Gohman81a148b2007-09-24 21:43:52 +00002252 // If there are no lines to emit (such as when we're using .loc directives
2253 // to emit .debug_line information) don't emit a .debug_line header.
2254 if (SectionSourceLines.empty())
2255 return;
2256
Jim Laskeyef42a012006-11-02 20:12:39 +00002257 // Minimum line delta, thus ranging from -10..(255-10).
2258 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2259 // Maximum line delta, thus ranging from -10..(255-10).
2260 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002261
Jim Laskeyef42a012006-11-02 20:12:39 +00002262 // Start the dwarf line section.
2263 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2264
2265 // Construct the section header.
2266
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002267 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002268 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002269 EmitLabel("line_begin", 0);
2270
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002271 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Jim Laskeyef42a012006-11-02 20:12:39 +00002272
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002273 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002274 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002275 EmitLabel("line_prolog_begin", 0);
2276
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002277 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002278
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002279 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002280
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002281 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002282
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002283 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002284
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002285 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Jim Laskeyef42a012006-11-02 20:12:39 +00002286
2287 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002288 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2289 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2290 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2291 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2292 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2293 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2294 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2295 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2296 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002297
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002298 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Jim Laskeyef42a012006-11-02 20:12:39 +00002299 const UniqueVector<SourceFileInfo>
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002300 &SourceFiles = MMI->getSourceFiles();
Jim Laskeyef42a012006-11-02 20:12:39 +00002301
2302 // Emit directories.
2303 for (unsigned DirectoryID = 1, NDID = Directories.size();
2304 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002305 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002306 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002307 Asm->EmitInt8(0); Asm->EOL("End of directories");
Jim Laskeyef42a012006-11-02 20:12:39 +00002308
2309 // Emit files.
2310 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2311 SourceID <= NSID; ++SourceID) {
2312 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002313 Asm->EmitString(SourceFile.getName());
2314 Asm->EOL("Source");
2315 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2316 Asm->EOL("Directory #");
2317 Asm->EmitULEB128Bytes(0);
2318 Asm->EOL("Mod date");
2319 Asm->EmitULEB128Bytes(0);
2320 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002321 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002322 Asm->EmitInt8(0); Asm->EOL("End of files");
Jim Laskeyef42a012006-11-02 20:12:39 +00002323
2324 EmitLabel("line_prolog_end", 0);
2325
2326 // A sequence for each text section.
2327 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2328 // Isolate current sections line info.
2329 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
2330
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002331 Asm->EOL(std::string("Section ") + SectionMap[j + 1]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002332
2333 // Dwarf assumes we start with first line of first source file.
2334 unsigned Source = 1;
2335 unsigned Line = 1;
2336
2337 // Construct rows of the address, source, line, column matrix.
2338 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2339 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002340 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002341 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002342
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002343 unsigned SourceID = LineInfo.getSourceID();
2344 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2345 unsigned DirectoryID = SourceFile.getDirectoryID();
2346 Asm->EOL(Directories[DirectoryID]
2347 + SourceFile.getName()
2348 + ":"
2349 + utostr_32(LineInfo.getLine()));
Jim Laskeyef42a012006-11-02 20:12:39 +00002350
2351 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002352 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002353 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002354 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002355 EmitReference("label", LabelID); Asm->EOL("Location label");
Jim Laskeyef42a012006-11-02 20:12:39 +00002356
2357 // If change of source, then switch to the new source.
2358 if (Source != LineInfo.getSourceID()) {
2359 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002360 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2361 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002362 }
2363
2364 // If change of line.
2365 if (Line != LineInfo.getLine()) {
2366 // Determine offset.
2367 int Offset = LineInfo.getLine() - Line;
2368 int Delta = Offset - MinLineDelta;
2369
2370 // Update line.
2371 Line = LineInfo.getLine();
2372
2373 // If delta is small enough and in range...
2374 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2375 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002376 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002377 } else {
2378 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002379 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2380 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2381 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002382 }
2383 } else {
2384 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002385 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002386 }
2387 }
2388
2389 // Define last address of section.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002390 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002391 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002392 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2393 EmitReference("section_end", j + 1); Asm->EOL("Section end label");
Jim Laskeyef42a012006-11-02 20:12:39 +00002394
2395 // Mark end of matrix.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002396 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
Jim Laskeybacd3042007-02-21 22:48:45 +00002397 Asm->EmitULEB128Bytes(1); Asm->EOL();
2398 Asm->EmitInt8(1); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002399 }
2400
2401 EmitLabel("line_end", 0);
2402
Jim Laskeybacd3042007-02-21 22:48:45 +00002403 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002404 }
2405
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002406 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002407 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002408 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002409 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002410 return;
2411
2412 int stackGrowth =
2413 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2414 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002415 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002416
2417 // Start the dwarf frame section.
2418 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2419
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002420 EmitLabel("debug_frame_common", 0);
2421 EmitDifference("debug_frame_common_end", 0,
2422 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002423 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002424
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002425 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002426 Asm->EmitInt32((int)DW_CIE_ID);
2427 Asm->EOL("CIE Identifier Tag");
2428 Asm->EmitInt8(DW_CIE_VERSION);
2429 Asm->EOL("CIE Version");
2430 Asm->EmitString("");
2431 Asm->EOL("CIE Augmentation");
2432 Asm->EmitULEB128Bytes(1);
2433 Asm->EOL("CIE Code Alignment Factor");
2434 Asm->EmitSLEB128Bytes(stackGrowth);
2435 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002436 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002437 Asm->EOL("CIE RA Column");
Jim Laskey65195462006-10-30 13:35:07 +00002438
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002439 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002440 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002441
Dale Johannesenb97aec62007-11-13 19:13:01 +00002442 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002443
Evan Cheng05548eb2008-02-29 19:36:59 +00002444 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002445 EmitLabel("debug_frame_common_end", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002446
Jim Laskeybacd3042007-02-21 22:48:45 +00002447 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002448 }
2449
Jim Laskey65195462006-10-30 13:35:07 +00002450 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2451 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002452 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002453 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002454 return;
Jim Laskey9d4209f2006-11-07 19:33:46 +00002455
Jim Laskeyef42a012006-11-02 20:12:39 +00002456 // Start the dwarf frame section.
2457 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2458
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002459 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2460 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002461 Asm->EOL("Length of Frame Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002462
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002463 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002464
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002465 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2466 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002467 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002468
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002469 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002470 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002471 EmitDifference("func_end", DebugFrameInfo.Number,
2472 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002473 Asm->EOL("FDE address range");
Jim Laskeyef42a012006-11-02 20:12:39 +00002474
Dale Johannesenb97aec62007-11-13 19:13:01 +00002475 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002476
Evan Cheng05548eb2008-02-29 19:36:59 +00002477 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002478 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002479
Jim Laskeybacd3042007-02-21 22:48:45 +00002480 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002481 }
2482
2483 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002484 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002485 void EmitDebugPubNames() {
2486 // Start the dwarf pubnames section.
2487 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2488
Jim Laskey5496f012006-11-09 14:52:14 +00002489 CompileUnit *Unit = GetBaseCompileUnit();
2490
2491 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002492 "pubnames_begin", Unit->getID(), true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002493 Asm->EOL("Length of Public Names Info");
Jim Laskey5496f012006-11-09 14:52:14 +00002494
2495 EmitLabel("pubnames_begin", Unit->getID());
2496
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002497 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002498
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002499 EmitSectionOffset("info_begin", "section_info",
2500 Unit->getID(), 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002501 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002502
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002503 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002504 Asm->EOL("Compilation Unit Length");
Jim Laskey5496f012006-11-09 14:52:14 +00002505
2506 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2507
2508 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2509 GE = Globals.end();
2510 GI != GE; ++GI) {
2511 const std::string &Name = GI->first;
2512 DIE * Entity = GI->second;
Jim Laskeyef42a012006-11-02 20:12:39 +00002513
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002514 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2515 Asm->EmitString(Name); Asm->EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00002516 }
Jim Laskey5496f012006-11-09 14:52:14 +00002517
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002518 Asm->EmitInt32(0); Asm->EOL("End Mark");
Jim Laskey5496f012006-11-09 14:52:14 +00002519 EmitLabel("pubnames_end", Unit->getID());
2520
Jim Laskeybacd3042007-02-21 22:48:45 +00002521 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002522 }
2523
2524 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002525 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002526 void EmitDebugStr() {
2527 // Check to see if it is worth the effort.
2528 if (!StringPool.empty()) {
2529 // Start the dwarf str section.
2530 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2531
2532 // For each of strings in the string pool.
2533 for (unsigned StringID = 1, N = StringPool.size();
2534 StringID <= N; ++StringID) {
2535 // Emit a label for reference from debug information entries.
2536 EmitLabel("string", StringID);
2537 // Emit the string itself.
2538 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002539 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002540 }
2541
Jim Laskeybacd3042007-02-21 22:48:45 +00002542 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002543 }
2544 }
2545
2546 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002547 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002548 void EmitDebugLoc() {
2549 // Start the dwarf loc section.
2550 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2551
Jim Laskeybacd3042007-02-21 22:48:45 +00002552 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002553 }
2554
2555 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002556 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002557 void EmitDebugARanges() {
2558 // Start the dwarf aranges section.
2559 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2560
2561 // FIXME - Mock up
2562 #if 0
Jim Laskey5496f012006-11-09 14:52:14 +00002563 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002564
Jim Laskey5496f012006-11-09 14:52:14 +00002565 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002566 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Jim Laskey5496f012006-11-09 14:52:14 +00002567
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002568 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Jim Laskey5496f012006-11-09 14:52:14 +00002569
2570 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002571 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002572
Dan Gohman82482942007-09-27 23:12:31 +00002573 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002574
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002575 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002576
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002577 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2578 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002579
Jim Laskey5496f012006-11-09 14:52:14 +00002580 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002581 EmitReference("text_begin", 0); Asm->EOL("Address");
2582 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002583
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002584 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2585 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Dan Gohman44de9262007-09-24 21:36:21 +00002586 #endif
Jim Laskey5496f012006-11-09 14:52:14 +00002587
Jim Laskeybacd3042007-02-21 22:48:45 +00002588 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002589 }
2590
2591 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002592 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002593 void EmitDebugRanges() {
2594 // Start the dwarf ranges section.
2595 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2596
Jim Laskeybacd3042007-02-21 22:48:45 +00002597 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002598 }
2599
2600 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002601 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002602 void EmitDebugMacInfo() {
2603 // Start the dwarf macinfo section.
2604 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2605
Jim Laskeybacd3042007-02-21 22:48:45 +00002606 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002607 }
2608
Jim Laskey65195462006-10-30 13:35:07 +00002609 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2610 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002611 void ConstructCompileUnitDIEs() {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002612 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
Jim Laskeyef42a012006-11-02 20:12:39 +00002613
2614 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002615 unsigned ID = MMI->RecordSource(CUW[i]);
Jim Laskey9d4209f2006-11-07 19:33:46 +00002616 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002617 CompileUnits.push_back(Unit);
2618 }
2619 }
2620
Jim Laskey65195462006-10-30 13:35:07 +00002621 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2622 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002623 void ConstructGlobalDIEs() {
2624 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002625 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M);
Jim Laskeyef42a012006-11-02 20:12:39 +00002626
2627 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2628 GlobalVariableDesc *GVD = GlobalVariables[i];
2629 NewGlobalVariable(GVD);
2630 }
2631 }
Jim Laskey65195462006-10-30 13:35:07 +00002632
2633 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2634 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002635 void ConstructSubprogramDIEs() {
2636 std::vector<SubprogramDesc *> Subprograms =
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002637 MMI->getAnchoredDescriptors<SubprogramDesc>(*M);
Jim Laskeyef42a012006-11-02 20:12:39 +00002638
2639 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2640 SubprogramDesc *SPD = Subprograms[i];
2641 NewSubprogram(SPD);
2642 }
2643 }
Jim Laskey65195462006-10-30 13:35:07 +00002644
Jim Laskey65195462006-10-30 13:35:07 +00002645public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002646 //===--------------------------------------------------------------------===//
2647 // Main entry points.
2648 //
Jim Laskey072200c2007-01-29 18:51:14 +00002649 DwarfDebug(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00002650 : Dwarf(OS, A, T, "dbg")
Jim Laskeyef42a012006-11-02 20:12:39 +00002651 , CompileUnits()
2652 , AbbreviationsSet(InitAbbreviationsSetSize)
2653 , Abbreviations()
2654 , ValuesSet(InitValuesSetSize)
2655 , Values()
2656 , StringPool()
2657 , DescToUnitMap()
2658 , SectionMap()
2659 , SectionSourceLines()
Jim Laskeybacd3042007-02-21 22:48:45 +00002660 , didInitial(false)
2661 , shouldEmit(false)
Jim Laskeyef42a012006-11-02 20:12:39 +00002662 {
2663 }
Jim Laskey072200c2007-01-29 18:51:14 +00002664 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002665 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2666 delete CompileUnits[i];
2667 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2668 delete Values[j];
2669 }
2670
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002671 /// SetModuleInfo - Set machine module information when it's known that pass
2672 /// manager has created it. Set by the target AsmPrinter.
2673 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002674 // Make sure initial declarations are made.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002675 if (!MMI && mmi->hasDebugInfo()) {
2676 MMI = mmi;
Jim Laskeyef42a012006-11-02 20:12:39 +00002677 shouldEmit = true;
2678
Jim Laskeyef42a012006-11-02 20:12:39 +00002679 // Create all the compile unit DIEs.
2680 ConstructCompileUnitDIEs();
2681
2682 // Create DIEs for each of the externally visible global variables.
2683 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002684
Jim Laskeyef42a012006-11-02 20:12:39 +00002685 // Create DIEs for each of the externally visible subprograms.
2686 ConstructSubprogramDIEs();
2687
2688 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00002689 SectionMap.insert(TAI->getTextSection());
Dan Gohmand57c3882007-10-01 22:40:20 +00002690
2691 // Print out .file directives to specify files for .loc directives. These
2692 // are printed out early so that they precede any .loc directives.
2693 if (TAI->hasDotLocAndDotFile()) {
2694 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
2695 const UniqueVector<std::string> &Directories = MMI->getDirectories();
2696 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
2697 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
2698 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
2699 assert(AppendOk && "Could not append filename to directory!");
2700 Asm->EmitFile(i, FullPath.toString());
2701 Asm->EOL();
2702 }
2703 }
2704
2705 // Emit initial sections
2706 EmitInitial();
Jim Laskeyef42a012006-11-02 20:12:39 +00002707 }
2708 }
2709
Jim Laskey65195462006-10-30 13:35:07 +00002710 /// BeginModule - Emit all Dwarf sections that should come prior to the
2711 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002712 void BeginModule(Module *M) {
2713 this->M = M;
2714
2715 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002716 }
2717
Jim Laskey65195462006-10-30 13:35:07 +00002718 /// EndModule - Emit all Dwarf sections that should come after the content.
2719 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002720 void EndModule() {
2721 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002722
2723 // Standard sections final addresses.
2724 Asm->SwitchToTextSection(TAI->getTextSection());
2725 EmitLabel("text_end", 0);
2726 Asm->SwitchToDataSection(TAI->getDataSection());
2727 EmitLabel("data_end", 0);
2728
2729 // End text sections.
2730 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2731 Asm->SwitchToTextSection(SectionMap[i].c_str());
2732 EmitLabel("section_end", i);
2733 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002734
2735 // Emit common frame information.
2736 EmitCommonDebugFrame();
2737
2738 // Emit function debug frame information
2739 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2740 E = DebugFrames.end(); I != E; ++I)
2741 EmitFunctionDebugFrame(*I);
2742
Jim Laskeyef42a012006-11-02 20:12:39 +00002743 // Compute DIE offsets and sizes.
2744 SizeAndOffsets();
2745
2746 // Emit all the DIEs into a debug info section
2747 EmitDebugInfo();
2748
2749 // Corresponding abbreviations into a abbrev section.
2750 EmitAbbreviations();
2751
2752 // Emit source line correspondence into a debug line section.
2753 EmitDebugLines();
2754
2755 // Emit info into a debug pubnames section.
2756 EmitDebugPubNames();
2757
2758 // Emit info into a debug str section.
2759 EmitDebugStr();
2760
2761 // Emit info into a debug loc section.
2762 EmitDebugLoc();
2763
2764 // Emit info into a debug aranges section.
2765 EmitDebugARanges();
2766
2767 // Emit info into a debug ranges section.
2768 EmitDebugRanges();
2769
2770 // Emit info into a debug macinfo section.
2771 EmitDebugMacInfo();
2772 }
2773
Jim Laskey65195462006-10-30 13:35:07 +00002774 /// BeginFunction - Gather pre-function debug information. Assumes being
2775 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002776 void BeginFunction(MachineFunction *MF) {
2777 this->MF = MF;
2778
2779 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002780
2781 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002782 MMI->BeginFunction(MF);
Jim Laskeyef42a012006-11-02 20:12:39 +00002783
2784 // Assumes in correct section after the entry point.
2785 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00002786
2787 // Emit label for the implicitly defined dbg.stoppoint at the start of
2788 // the function.
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00002789 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
2790 if (!LineInfos.empty()) {
2791 const SourceLineInfo &LineInfo = LineInfos[0];
2792 Asm->printLabel(LineInfo.getLabelID());
2793 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002794 }
Jim Laskey072200c2007-01-29 18:51:14 +00002795
Jim Laskey65195462006-10-30 13:35:07 +00002796 /// EndFunction - Gather and emit post-function debug information.
2797 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002798 void EndFunction() {
2799 if (!ShouldEmitDwarf()) return;
Jim Laskeyb82313f2007-02-01 16:31:34 +00002800
2801 // Define end label for subprogram.
2802 EmitLabel("func_end", SubprogramCount);
Jim Laskeyef42a012006-11-02 20:12:39 +00002803
2804 // Get function line info.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002805 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
Jim Laskeyef42a012006-11-02 20:12:39 +00002806
2807 if (!LineInfos.empty()) {
2808 // Get section line info.
2809 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2810 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2811 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2812 // Append the function info to section info.
2813 SectionLineInfos.insert(SectionLineInfos.end(),
2814 LineInfos.begin(), LineInfos.end());
2815 }
2816
2817 // Construct scopes for subprogram.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002818 ConstructRootScope(MMI->getRootScope());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002819
2820 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2821 MMI->getFrameMoves()));
Jim Laskeyef42a012006-11-02 20:12:39 +00002822 }
Jim Laskey65195462006-10-30 13:35:07 +00002823};
2824
Jim Laskey072200c2007-01-29 18:51:14 +00002825//===----------------------------------------------------------------------===//
2826/// DwarfException - Emits Dwarf exception handling directives.
2827///
2828class DwarfException : public Dwarf {
2829
Jim Laskeyb82313f2007-02-01 16:31:34 +00002830private:
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002831 struct FunctionEHFrameInfo {
2832 std::string FnName;
2833 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002834 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002835 bool hasCalls;
2836 bool hasLandingPads;
2837 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002838 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00002839
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002840 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
2841 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00002842 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002843 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002844 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002845 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002846 };
2847
2848 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00002849
2850 /// shouldEmitTable - Per-function flag to indicate if EH tables should
2851 /// be emitted.
2852 bool shouldEmitTable;
2853
2854 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
2855 /// should be emitted.
2856 bool shouldEmitMoves;
2857
2858 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
2859 /// should be emitted.
2860 bool shouldEmitTableModule;
2861
2862 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
2863 /// should be emitted.
2864 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00002865
Jim Laskey3f09fc22007-02-28 18:38:31 +00002866 /// EmitCommonEHFrame - Emit the common eh unwind frame.
2867 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002868 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00002869 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002870 int stackGrowth =
2871 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2872 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002873 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00002874
Jim Laskeybacd3042007-02-21 22:48:45 +00002875 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002876 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002877 O << "EH_frame" << Index << ":\n";
2878 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002879
Jim Laskeybacd3042007-02-21 22:48:45 +00002880 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002881 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00002882
Jim Laskeybacd3042007-02-21 22:48:45 +00002883 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002884 EmitDifference("eh_frame_common_end", Index,
2885 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002886 Asm->EOL("Length of Common Information Entry");
2887
Jim Laskeybacd3042007-02-21 22:48:45 +00002888 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002889 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002890 Asm->EmitInt32((int)0);
2891 Asm->EOL("CIE Identifier Tag");
2892 Asm->EmitInt8(DW_CIE_VERSION);
2893 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00002894
Jim Laskeybacd3042007-02-21 22:48:45 +00002895 // The personality presence indicates that language specific information
2896 // will show up in the eh frame.
2897 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00002898 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00002899
Jim Laskeybacd3042007-02-21 22:48:45 +00002900 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002901 Asm->EmitULEB128Bytes(1);
2902 Asm->EOL("CIE Code Alignment Factor");
2903 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00002904 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002905 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00002906 Asm->EOL("CIE Return Address Column");
2907
Jim Laskeybacd3042007-02-21 22:48:45 +00002908 // If there is a personality, we need to indicate the functions location.
2909 if (Personality) {
2910 Asm->EmitULEB128Bytes(7);
2911 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00002912
Duncan Sands671fa972008-05-07 19:11:09 +00002913 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00002914 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00002915 Asm->EOL("Personality (pcrel sdata4 indirect)");
2916 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00002917 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00002918 Asm->EOL("Personality (pcrel sdata4)");
2919 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00002920
Duncan Sands671fa972008-05-07 19:11:09 +00002921 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00002922 O << TAI->getPersonalityPrefix();
2923 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
2924 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00002925 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
2926 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00002927 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00002928
Duncan Sands671fa972008-05-07 19:11:09 +00002929 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2930 Asm->EOL("LSDA Encoding (pcrel sdata4)");
2931 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2932 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00002933 } else {
2934 Asm->EmitULEB128Bytes(1);
2935 Asm->EOL("Augmentation Size");
Duncan Sands671fa972008-05-07 19:11:09 +00002936 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2937 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00002938 }
2939
2940 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002941 std::vector<MachineMove> Moves;
2942 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00002943 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002944
Dale Johannesen21d972a2008-04-30 00:43:29 +00002945 // On Darwin the linker honors the alignment of eh_frame, which means it
2946 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
2947 // you get holes which confuse readers of eh_frame.
Dale Johannesen7b251e02008-04-29 22:58:20 +00002948 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
2949 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002950 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00002951
Jim Laskeybacd3042007-02-21 22:48:45 +00002952 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00002953 }
Duncan Sands671fa972008-05-07 19:11:09 +00002954
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002955 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002956 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002957 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002958 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
2959
Jim Laskeybacd3042007-02-21 22:48:45 +00002960 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
2961
2962 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00002963 // If the corresponding function is static, this should not be
2964 // externally visible.
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002965 if (linkage != Function::InternalLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00002966 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
2967 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
2968 }
2969
Dale Johannesen038129d2008-01-10 02:03:30 +00002970 // If corresponding function is weak definition, this should be too.
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002971 if ((linkage == Function::WeakLinkage ||
2972 linkage == Function::LinkOnceLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00002973 TAI->getWeakDefDirective())
2974 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
2975
2976 // If there are no calls then you can't unwind. This may mean we can
2977 // omit the EH Frame, but some environments do not handle weak absolute
Dale Johannesen4e1b7942008-04-08 00:10:24 +00002978 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00002979 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00002980 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00002981 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00002982 !UnwindTablesMandatory &&
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002983 ((linkage != Function::WeakLinkage &&
2984 linkage != Function::LinkOnceLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00002985 !TAI->getWeakDefDirective() ||
2986 TAI->getSupportsWeakOmittedEHFrame()))
2987 {
Bill Wendling6e198962007-09-18 01:47:22 +00002988 O << EHFrameInfo.FnName << " = 0\n";
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002989 // This name has no connection to the function, so it might get
2990 // dead-stripped when the function is not, erroneously. Prohibit
2991 // dead-stripping unconditionally.
2992 if (const char *UsedDirective = TAI->getUsedDirective())
2993 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00002994 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00002995 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00002996
Jim Laskeybacd3042007-02-21 22:48:45 +00002997 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002998 EmitDifference("eh_frame_end", EHFrameInfo.Number,
2999 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003000 Asm->EOL("Length of Frame Information Entry");
3001
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003002 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003003
Anton Korobeynikov070280e2007-05-23 11:08:31 +00003004 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003005 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +00003006 true, true, false);
Jim Laskeybacd3042007-02-21 22:48:45 +00003007 Asm->EOL("FDE CIE offset");
3008
Duncan Sands671fa972008-05-07 19:11:09 +00003009 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003010 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003011 EmitDifference("eh_func_end", EHFrameInfo.Number,
Duncan Sands671fa972008-05-07 19:11:09 +00003012 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003013 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003014
Jim Laskey3f09fc22007-02-28 18:38:31 +00003015 // If there is a personality and landing pads then point to the language
3016 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003017 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003018 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003019 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003020
3021 if (EHFrameInfo.hasLandingPads)
3022 EmitReference("exception", EHFrameInfo.Number, true, true);
3023 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003024 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003025 Asm->EOL("Language Specific Data Area");
3026 } else {
3027 Asm->EmitULEB128Bytes(0);
3028 Asm->EOL("Augmentation size");
3029 }
Duncan Sands671fa972008-05-07 19:11:09 +00003030
Jim Laskeybacd3042007-02-21 22:48:45 +00003031 // Indicate locations of function specific callee saved registers in
3032 // frame.
Dale Johannesenb97aec62007-11-13 19:13:01 +00003033 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003034
Dale Johannesen21d972a2008-04-30 00:43:29 +00003035 // On Darwin the linker honors the alignment of eh_frame, which means it
3036 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3037 // you get holes which confuse readers of eh_frame.
Dale Johannesen7b251e02008-04-29 22:58:20 +00003038 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
3039 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003040 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003041
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003042 // If the function is marked used, this table should be also. We cannot
3043 // make the mark unconditional in this case, since retaining the table
3044 // also retains the function in this case, and there is code around
3045 // that depends on unused functions (calling undefined externals) being
3046 // dead-stripped to link correctly. Yes, there really is.
3047 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3048 if (const char *UsedDirective = TAI->getUsedDirective())
3049 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3050 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003051 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003052
Duncan Sands57810cd2007-09-05 11:27:52 +00003053 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003054 ///
3055 /// The general organization of the table is complex, but the basic concepts
3056 /// are easy. First there is a header which describes the location and
3057 /// organization of the three components that follow.
3058 /// 1. The landing pad site information describes the range of code covered
3059 /// by the try. In our case it's an accumulation of the ranges covered
3060 /// by the invokes in the try. There is also a reference to the landing
3061 /// pad that handles the exception once processed. Finally an index into
3062 /// the actions table.
3063 /// 2. The action table, in our case, is composed of pairs of type ids
3064 /// and next action offset. Starting with the action index from the
3065 /// landing pad site, each type Id is checked for a match to the current
3066 /// exception. If it matches then the exception and type id are passed
3067 /// on to the landing pad. Otherwise the next action is looked up. This
3068 /// chain is terminated with a next action of zero. If no type id is
3069 /// found the the frame is unwound and handling continues.
3070 /// 3. Type id table contains references to all the C++ typeinfo for all
3071 /// catches in the function. This tables is reversed indexed base 1.
3072
Duncan Sandsb32edb42007-06-06 15:37:31 +00003073 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3074 static unsigned SharedTypeIds(const LandingPadInfo *L,
3075 const LandingPadInfo *R) {
3076 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003077 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003078 unsigned MinSize = LSize < RSize ? LSize : RSize;
3079 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003080
Duncan Sandsb32edb42007-06-06 15:37:31 +00003081 for (; Count != MinSize; ++Count)
3082 if (LIds[Count] != RIds[Count])
3083 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003084
Duncan Sandsb32edb42007-06-06 15:37:31 +00003085 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003086 }
3087
Duncan Sandsb32edb42007-06-06 15:37:31 +00003088 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003089 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003090 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003091 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003092 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003093
Duncan Sandsb32edb42007-06-06 15:37:31 +00003094 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003095 if (LIds[i] != RIds[i])
3096 return LIds[i] < RIds[i];
3097
Duncan Sandsb32edb42007-06-06 15:37:31 +00003098 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003099 }
3100
Duncan Sands53c3a332007-05-16 12:12:23 +00003101 struct KeyInfo {
3102 static inline unsigned getEmptyKey() { return -1U; }
3103 static inline unsigned getTombstoneKey() { return -2U; }
3104 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003105 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003106 static bool isPod() { return true; }
3107 };
3108
Duncan Sands57810cd2007-09-05 11:27:52 +00003109 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003110 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003111 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003112 int NextAction;
3113 struct ActionEntry *Previous;
3114 };
3115
Duncan Sands57810cd2007-09-05 11:27:52 +00003116 /// PadRange - Structure holding a try-range and the associated landing pad.
3117 struct PadRange {
3118 // The index of the landing pad.
3119 unsigned PadIndex;
3120 // The index of the begin and end labels in the landing pad's label lists.
3121 unsigned RangeIndex;
3122 };
3123
3124 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3125
3126 /// CallSiteEntry - Structure describing an entry in the call-site table.
3127 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003128 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003129 unsigned BeginLabel; // zero indicates the start of the function.
3130 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003131 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003132 unsigned PadLabel; // zero indicates that there is no landing pad.
3133 unsigned Action;
3134 };
3135
Jim Laskeybacd3042007-02-21 22:48:45 +00003136 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003137 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003138 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003139 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3140 if (PadInfos.empty()) return;
3141
3142 // Sort the landing pads in order of their type ids. This is used to fold
3143 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003144 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003145 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003146 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003147 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003148 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3149
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003150 // Negative type ids index into FilterIds, positive type ids index into
3151 // TypeInfos. The value written for a positive type id is just the type
3152 // id itself. For a negative type id, however, the value written is the
3153 // (negative) byte offset of the corresponding FilterIds entry. The byte
3154 // offset is usually equal to the type id, because the FilterIds entries
3155 // are written using a variable width encoding which outputs one byte per
3156 // entry as long as the value written is not too large, but can differ.
3157 // This kind of complication does not occur for positive type ids because
3158 // type infos are output using a fixed width encoding.
3159 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3160 SmallVector<int, 16> FilterOffsets;
3161 FilterOffsets.reserve(FilterIds.size());
3162 int Offset = -1;
3163 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3164 E = FilterIds.end(); I != E; ++I) {
3165 FilterOffsets.push_back(Offset);
Duncan Sandsbb821dd2007-07-12 13:51:39 +00003166 Offset -= Asm->SizeULEB128(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003167 }
3168
Duncan Sands57810cd2007-09-05 11:27:52 +00003169 // Compute the actions table and gather the first action index for each
3170 // landing pad site.
3171 SmallVector<ActionEntry, 32> Actions;
3172 SmallVector<unsigned, 64> FirstActions;
3173 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003174
Duncan Sandsb32edb42007-06-06 15:37:31 +00003175 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003176 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003177 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003178 const LandingPadInfo *LP = LandingPads[i];
3179 const std::vector<int> &TypeIds = LP->TypeIds;
3180 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003181 unsigned SizeSiteActions = 0;
3182
Duncan Sandsb32edb42007-06-06 15:37:31 +00003183 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003184 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003185 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003186
Duncan Sandsb32edb42007-06-06 15:37:31 +00003187 if (NumShared) {
3188 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3189 assert(Actions.size());
3190 PrevAction = &Actions.back();
3191 SizeAction = Asm->SizeSLEB128(PrevAction->NextAction) +
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003192 Asm->SizeSLEB128(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003193 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003194 SizeAction -= Asm->SizeSLEB128(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003195 SizeAction += -PrevAction->NextAction;
3196 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003197 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003198 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003199
Duncan Sandsb32edb42007-06-06 15:37:31 +00003200 // Compute the actions.
3201 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3202 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003203 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3204 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
3205 unsigned SizeTypeID = Asm->SizeSLEB128(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003206
3207 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
3208 SizeAction = SizeTypeID + Asm->SizeSLEB128(NextAction);
3209 SizeSiteActions += SizeAction;
3210
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003211 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00003212 Actions.push_back(Action);
3213
3214 PrevAction = &Actions.back();
3215 }
3216
3217 // Record the first action of the landing pad site.
3218 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3219 } // else identical - re-use previous FirstAction
3220
3221 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003222
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00003223 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00003224 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00003225 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003226
Duncan Sands481dc722007-12-19 07:36:31 +00003227 // Compute the call-site table. The entry for an invoke has a try-range
3228 // containing the call, a non-zero landing pad and an appropriate action.
3229 // The entry for an ordinary call has a try-range containing the call and
3230 // zero for the landing pad and the action. Calls marked 'nounwind' have
3231 // no entry and must not be contained in the try-range of any entry - they
3232 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003233 SmallVector<CallSiteEntry, 64> CallSites;
3234
3235 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00003236 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3237 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3238 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00003239 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3240 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00003241 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003242 unsigned BeginLabel = LandingPad->BeginLabels[j];
3243 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3244 PadRange P = { i, j };
3245 PadMap[BeginLabel] = P;
3246 }
3247 }
3248
Duncan Sands481dc722007-12-19 07:36:31 +00003249 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00003250 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00003251
3252 // Whether there is a potentially throwing instruction (currently this means
3253 // an ordinary call) between the end of the previous try-range and now.
3254 bool SawPotentiallyThrowing = false;
3255
3256 // Whether the last callsite entry was for an invoke.
3257 bool PreviousIsInvoke = false;
3258
Duncan Sands481dc722007-12-19 07:36:31 +00003259 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003260 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3261 I != E; ++I) {
3262 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3263 MI != E; ++MI) {
3264 if (MI->getOpcode() != TargetInstrInfo::LABEL) {
Chris Lattner749c6f62008-01-07 07:27:27 +00003265 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00003266 continue;
3267 }
3268
Chris Lattner9e330492007-12-30 20:50:28 +00003269 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00003270 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00003271
Duncan Sands481dc722007-12-19 07:36:31 +00003272 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00003273 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00003274 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003275
Duncan Sands481dc722007-12-19 07:36:31 +00003276 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00003277 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00003278 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00003279 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00003280 continue;
3281
3282 PadRange P = L->second;
3283 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3284
3285 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3286 "Inconsistent landing pad map!");
3287
3288 // If some instruction between the previous try-range and this one may
3289 // throw, create a call-site entry with no landing pad for the region
3290 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00003291 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003292 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3293 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00003294 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003295 }
3296
3297 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00003298 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00003299
Duncan Sands481dc722007-12-19 07:36:31 +00003300 if (LandingPad->LandingPadLabel) {
3301 // This try-range is for an invoke.
3302 CallSiteEntry Site = {BeginLabel, LastLabel,
3303 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00003304
Duncan Sands481dc722007-12-19 07:36:31 +00003305 // Try to merge with the previous call-site.
3306 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00003307 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00003308 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3309 // Extend the range of the previous entry.
3310 Prev.EndLabel = Site.EndLabel;
3311 continue;
3312 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003313 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003314
Duncan Sands481dc722007-12-19 07:36:31 +00003315 // Otherwise, create a new call-site.
3316 CallSites.push_back(Site);
3317 PreviousIsInvoke = true;
3318 } else {
3319 // Create a gap.
3320 PreviousIsInvoke = false;
3321 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003322 }
3323 }
3324 // If some instruction between the previous try-range and the end of the
3325 // function may throw, create a call-site entry with no landing pad for the
3326 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00003327 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003328 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3329 CallSites.push_back(Site);
3330 }
3331
Jim Laskeybacd3042007-02-21 22:48:45 +00003332 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00003333
3334 // Call sites.
3335 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3336 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3337 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3338 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3339 SiteLengthSize +
3340 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00003341 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
3342 SizeSites += Asm->SizeULEB128(CallSites[i].Action);
3343
Duncan Sands671fa972008-05-07 19:11:09 +00003344 // Type infos.
3345 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3346 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003347
3348 unsigned TypeOffset = sizeof(int8_t) + // Call site format
3349 Asm->SizeULEB128(SizeSites) + // Call-site table length
3350 SizeSites + SizeActions + SizeTypes;
3351
3352 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3353 sizeof(int8_t) + // TType format
3354 Asm->SizeULEB128(TypeOffset) + // TType base offset
3355 TypeOffset;
3356
3357 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003358
Jim Laskeybacd3042007-02-21 22:48:45 +00003359 // Begin the exception table.
3360 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
3361 O << "GCC_except_table" << SubprogramCount << ":\n";
Evan Cheng05548eb2008-02-29 19:36:59 +00003362 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003363 for (unsigned i = 0; i != SizeAlign; ++i) {
3364 Asm->EmitInt8(0);
3365 Asm->EOL("Padding");
3366 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003367 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003368
Jim Laskeybacd3042007-02-21 22:48:45 +00003369 // Emit the header.
3370 Asm->EmitInt8(DW_EH_PE_omit);
3371 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3372 Asm->EmitInt8(DW_EH_PE_absptr);
3373 Asm->EOL("TType format (DW_EH_PE_absptr)");
3374 Asm->EmitULEB128Bytes(TypeOffset);
3375 Asm->EOL("TType base offset");
3376 Asm->EmitInt8(DW_EH_PE_udata4);
3377 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3378 Asm->EmitULEB128Bytes(SizeSites);
3379 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003380
Duncan Sands57810cd2007-09-05 11:27:52 +00003381 // Emit the landing pad site information.
3382 for (unsigned i = 0; i < CallSites.size(); ++i) {
3383 CallSiteEntry &S = CallSites[i];
3384 const char *BeginTag;
3385 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00003386
Duncan Sands57810cd2007-09-05 11:27:52 +00003387 if (!S.BeginLabel) {
3388 BeginTag = "eh_func_begin";
3389 BeginNumber = SubprogramCount;
3390 } else {
3391 BeginTag = "label";
3392 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00003393 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003394
Duncan Sands57810cd2007-09-05 11:27:52 +00003395 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003396 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003397 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00003398
Duncan Sands57810cd2007-09-05 11:27:52 +00003399 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00003400 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00003401 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003402 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00003403 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003404 }
3405 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003406
Duncan Sands671fa972008-05-07 19:11:09 +00003407 if (!S.PadLabel)
3408 Asm->EmitInt32(0);
3409 else
Duncan Sands57810cd2007-09-05 11:27:52 +00003410 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003411 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003412 Asm->EOL("Landing pad");
3413
3414 Asm->EmitULEB128Bytes(S.Action);
3415 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003416 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003417
Jim Laskeybacd3042007-02-21 22:48:45 +00003418 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003419 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3420 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00003421
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003422 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003423 Asm->EOL("TypeInfo index");
3424 Asm->EmitSLEB128Bytes(Action.NextAction);
3425 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003426 }
3427
3428 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00003429 for (unsigned M = TypeInfos.size(); M; --M) {
3430 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00003431
3432 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00003433
3434 if (GV)
3435 O << Asm->getGlobalLinkName(GV);
3436 else
3437 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00003438
Jim Laskeybacd3042007-02-21 22:48:45 +00003439 Asm->EOL("TypeInfo");
3440 }
3441
Duncan Sands73ef58a2007-06-02 16:53:42 +00003442 // Emit the filter typeids.
3443 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3444 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00003445 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00003446 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00003447 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003448
Evan Cheng05548eb2008-02-29 19:36:59 +00003449 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003450 }
3451
Jim Laskey072200c2007-01-29 18:51:14 +00003452public:
3453 //===--------------------------------------------------------------------===//
3454 // Main entry points.
3455 //
3456 DwarfException(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00003457 : Dwarf(OS, A, T, "eh")
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003458 , shouldEmitTable(false)
3459 , shouldEmitMoves(false)
3460 , shouldEmitTableModule(false)
3461 , shouldEmitMovesModule(false)
Jim Laskey072200c2007-01-29 18:51:14 +00003462 {}
3463
3464 virtual ~DwarfException() {}
3465
3466 /// SetModuleInfo - Set machine module information when it's known that pass
3467 /// manager has created it. Set by the target AsmPrinter.
3468 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00003469 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00003470 }
3471
3472 /// BeginModule - Emit all exception information that should come prior to the
3473 /// content.
3474 void BeginModule(Module *M) {
3475 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00003476 }
3477
3478 /// EndModule - Emit all exception information that should come after the
3479 /// content.
3480 void EndModule() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003481 if (shouldEmitMovesModule || shouldEmitTableModule) {
3482 const std::vector<Function *> Personalities = MMI->getPersonalities();
3483 for (unsigned i =0; i < Personalities.size(); ++i)
3484 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003485
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003486 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3487 E = EHFrames.end(); I != E; ++I)
3488 EmitEHFrame(*I);
3489 }
Jim Laskey072200c2007-01-29 18:51:14 +00003490 }
3491
3492 /// BeginFunction - Gather pre-function exception information. Assumes being
3493 /// emitted immediately after the function entry point.
3494 void BeginFunction(MachineFunction *MF) {
3495 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003496 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesene0040622008-04-02 17:04:45 +00003497 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003498
3499 // Map all labels and get rid of any dead landing pads.
3500 MMI->TidyLandingPads();
3501 // If any landing pads survive, we need an EH table.
3502 if (MMI->getLandingPads().size())
3503 shouldEmitTable = true;
3504
3505 // See if we need frame move info.
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003506 if (MMI->hasDebugInfo() ||
3507 !MF->getFunction()->doesNotThrow() ||
Dale Johannesen3541af72008-04-14 17:54:17 +00003508 UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003509 shouldEmitMoves = true;
3510
3511 if (shouldEmitMoves || shouldEmitTable)
3512 // Assumes in correct section after the entry point.
3513 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00003514 }
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003515 shouldEmitTableModule |= shouldEmitTable;
3516 shouldEmitMovesModule |= shouldEmitMoves;
Jim Laskey072200c2007-01-29 18:51:14 +00003517 }
3518
3519 /// EndFunction - Gather and emit post-function exception information.
3520 ///
3521 void EndFunction() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003522 if (shouldEmitMoves || shouldEmitTable) {
3523 EmitLabel("eh_func_end", SubprogramCount);
3524 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003525
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003526 // Save EH frame information
3527 EHFrames.
3528 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling6e198962007-09-18 01:47:22 +00003529 SubprogramCount,
3530 MMI->getPersonalityIndex(),
3531 MF->getFrameInfo()->hasCalls(),
3532 !MMI->getLandingPads().empty(),
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003533 MMI->getFrameMoves(),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003534 MF->getFunction()));
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003535 }
Jim Laskey072200c2007-01-29 18:51:14 +00003536 }
3537};
3538
Jim Laskey0d086af2006-02-27 12:43:29 +00003539} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00003540
3541//===----------------------------------------------------------------------===//
3542
Jim Laskeyd18e2892006-01-20 20:34:06 +00003543/// Emit - Print the abbreviation using the specified Dwarf writer.
3544///
Jim Laskey072200c2007-01-29 18:51:14 +00003545void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003546 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00003547 DD.getAsm()->EmitULEB128Bytes(Tag);
3548 DD.getAsm()->EOL(TagString(Tag));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003549
3550 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00003551 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3552 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003553
3554 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00003555 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003556 const DIEAbbrevData &AttrData = Data[i];
3557
3558 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00003559 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3560 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003561
3562 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00003563 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3564 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003565 }
3566
3567 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00003568 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3569 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00003570}
3571
3572#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00003573void DIEAbbrev::print(std::ostream &O) {
3574 O << "Abbreviation @"
3575 << std::hex << (intptr_t)this << std::dec
3576 << " "
3577 << TagString(Tag)
3578 << " "
3579 << ChildrenString(ChildrenFlag)
3580 << "\n";
3581
3582 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3583 O << " "
3584 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003585 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00003586 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003587 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00003588 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00003589}
Bill Wendlinge8156192006-12-07 01:30:32 +00003590void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00003591#endif
3592
3593//===----------------------------------------------------------------------===//
3594
Jim Laskeyef42a012006-11-02 20:12:39 +00003595#ifndef NDEBUG
3596void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003597 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003598}
Jim Laskeyef42a012006-11-02 20:12:39 +00003599#endif
3600
3601//===----------------------------------------------------------------------===//
3602
Jim Laskey063e7652006-01-17 17:31:53 +00003603/// EmitValue - Emit integer of appropriate size.
3604///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003605void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00003606 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00003607 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00003608 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003609 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003610 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003611 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003612 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003613 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003614 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003615 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
3616 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
3617 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003618 default: assert(0 && "DIE Value form not supported yet"); break;
3619 }
3620}
3621
3622/// SizeOf - Determine size of integer value in bytes.
3623///
Jim Laskey072200c2007-01-29 18:51:14 +00003624unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003625 switch (Form) {
3626 case DW_FORM_flag: // Fall thru
3627 case DW_FORM_ref1: // Fall thru
3628 case DW_FORM_data1: return sizeof(int8_t);
3629 case DW_FORM_ref2: // Fall thru
3630 case DW_FORM_data2: return sizeof(int16_t);
3631 case DW_FORM_ref4: // Fall thru
3632 case DW_FORM_data4: return sizeof(int32_t);
3633 case DW_FORM_ref8: // Fall thru
3634 case DW_FORM_data8: return sizeof(int64_t);
Jim Laskey072200c2007-01-29 18:51:14 +00003635 case DW_FORM_udata: return DD.getAsm()->SizeULEB128(Integer);
3636 case DW_FORM_sdata: return DD.getAsm()->SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00003637 default: assert(0 && "DIE Value form not supported yet"); break;
3638 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003639 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00003640}
3641
Jim Laskey063e7652006-01-17 17:31:53 +00003642//===----------------------------------------------------------------------===//
3643
3644/// EmitValue - Emit string value.
3645///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003646void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003647 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00003648}
3649
Jim Laskey063e7652006-01-17 17:31:53 +00003650//===----------------------------------------------------------------------===//
3651
3652/// EmitValue - Emit label value.
3653///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003654void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003655 bool IsSmall = Form == DW_FORM_data4;
3656 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00003657}
3658
3659/// SizeOf - Determine size of label value in bytes.
3660///
Jim Laskey072200c2007-01-29 18:51:14 +00003661unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003662 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003663 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00003664}
Jim Laskeyef42a012006-11-02 20:12:39 +00003665
Jim Laskey063e7652006-01-17 17:31:53 +00003666//===----------------------------------------------------------------------===//
3667
Jim Laskeyd18e2892006-01-20 20:34:06 +00003668/// EmitValue - Emit label value.
3669///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003670void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003671 bool IsSmall = Form == DW_FORM_data4;
3672 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00003673}
3674
3675/// SizeOf - Determine size of label value in bytes.
3676///
Jim Laskey072200c2007-01-29 18:51:14 +00003677unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003678 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003679 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00003680}
3681
3682//===----------------------------------------------------------------------===//
3683
Jim Laskey063e7652006-01-17 17:31:53 +00003684/// EmitValue - Emit delta value.
3685///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00003686void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
3687 bool IsSmall = Form == DW_FORM_data4;
3688 DD.EmitSectionOffset(Label.Tag, Section.Tag,
3689 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
3690}
3691
3692/// SizeOf - Determine size of delta value in bytes.
3693///
3694unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3695 if (Form == DW_FORM_data4) return 4;
3696 return DD.getTargetData()->getPointerSize();
3697}
3698
3699//===----------------------------------------------------------------------===//
3700
3701/// EmitValue - Emit delta value.
3702///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003703void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003704 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00003705 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00003706}
3707
3708/// SizeOf - Determine size of delta value in bytes.
3709///
Jim Laskey072200c2007-01-29 18:51:14 +00003710unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003711 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003712 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00003713}
3714
3715//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003716
Jim Laskeyb8509c52006-03-23 18:07:55 +00003717/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003718///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003719void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003720 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00003721}
Jim Laskeyd18e2892006-01-20 20:34:06 +00003722
3723//===----------------------------------------------------------------------===//
3724
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003725/// ComputeSize - calculate the size of the block.
3726///
Jim Laskey072200c2007-01-29 18:51:14 +00003727unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003728 if (!Size) {
3729 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
3730
3731 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00003732 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00003733 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003734 }
3735 return Size;
3736}
3737
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003738/// EmitValue - Emit block data.
3739///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003740void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003741 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003742 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
3743 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
3744 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
3745 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003746 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003747 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003748
3749 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
3750
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003751 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003752 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00003753 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003754 }
3755}
3756
3757/// SizeOf - Determine size of block data in bytes.
3758///
Jim Laskey072200c2007-01-29 18:51:14 +00003759unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003760 switch (Form) {
3761 case DW_FORM_block1: return Size + sizeof(int8_t);
3762 case DW_FORM_block2: return Size + sizeof(int16_t);
3763 case DW_FORM_block4: return Size + sizeof(int32_t);
Jim Laskey072200c2007-01-29 18:51:14 +00003764 case DW_FORM_block: return Size + DD.getAsm()->SizeULEB128(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003765 default: assert(0 && "Improper form for block"); break;
3766 }
3767 return 0;
3768}
3769
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003770//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003771/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00003772
3773DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003774 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00003775 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00003776}
Jim Laskeyef42a012006-11-02 20:12:39 +00003777
Jim Laskeyb8509c52006-03-23 18:07:55 +00003778/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
3779///
3780void DIE::AddSiblingOffset() {
3781 DIEInteger *DI = new DIEInteger(0);
3782 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00003783 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00003784}
3785
Jim Laskeyef42a012006-11-02 20:12:39 +00003786/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003787///
Jim Laskeyef42a012006-11-02 20:12:39 +00003788void DIE::Profile(FoldingSetNodeID &ID) {
3789 Abbrev.Profile(ID);
3790
3791 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3792 ID.AddPointer(Children[i]);
3793
3794 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3795 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003796}
Jim Laskeyef42a012006-11-02 20:12:39 +00003797
3798#ifndef NDEBUG
3799void DIE::print(std::ostream &O, unsigned IncIndent) {
3800 static unsigned IndentCount = 0;
3801 IndentCount += IncIndent;
3802 const std::string Indent(IndentCount, ' ');
3803 bool isBlock = Abbrev.getTag() == 0;
3804
3805 if (!isBlock) {
3806 O << Indent
3807 << "Die: "
3808 << "0x" << std::hex << (intptr_t)this << std::dec
3809 << ", Offset: " << Offset
3810 << ", Size: " << Size
3811 << "\n";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003812
Jim Laskeyef42a012006-11-02 20:12:39 +00003813 O << Indent
3814 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00003815 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00003816 << ChildrenString(Abbrev.getChildrenFlag());
3817 } else {
3818 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00003819 }
3820 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003821
Jim Laskeyef42a012006-11-02 20:12:39 +00003822 const std::vector<DIEAbbrevData> &Data = Abbrev.getData();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003823
Jim Laskeyef42a012006-11-02 20:12:39 +00003824 IndentCount += 2;
3825 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3826 O << Indent;
3827 if (!isBlock) {
3828 O << AttributeString(Data[i].getAttribute());
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003829 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00003830 O << "Blk[" << i << "]";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003831 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003832 O << " "
3833 << FormEncodingString(Data[i].getForm())
3834 << " ";
3835 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00003836 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00003837 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003838 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00003839
Jim Laskeyef42a012006-11-02 20:12:39 +00003840 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3841 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00003842 }
Jim Laskey063e7652006-01-17 17:31:53 +00003843
Jim Laskeyef42a012006-11-02 20:12:39 +00003844 if (!isBlock) O << "\n";
3845 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00003846}
3847
Jim Laskeyef42a012006-11-02 20:12:39 +00003848void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003849 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00003850}
Jim Laskeybd761842006-02-27 17:27:12 +00003851#endif
Jim Laskey65195462006-10-30 13:35:07 +00003852
3853//===----------------------------------------------------------------------===//
3854/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00003855///
Jim Laskey65195462006-10-30 13:35:07 +00003856
3857DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3858 const TargetAsmInfo *T) {
Jim Laskey072200c2007-01-29 18:51:14 +00003859 DE = new DwarfException(OS, A, T);
3860 DD = new DwarfDebug(OS, A, T);
Jim Laskey65195462006-10-30 13:35:07 +00003861}
3862
3863DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00003864 delete DE;
3865 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00003866}
3867
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003868/// SetModuleInfo - Set machine module info when it's known that pass manager
3869/// has created it. Set by the target AsmPrinter.
3870void DwarfWriter::SetModuleInfo(MachineModuleInfo *MMI) {
Jim Laskey072200c2007-01-29 18:51:14 +00003871 DD->SetModuleInfo(MMI);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003872 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00003873}
3874
3875/// BeginModule - Emit all Dwarf sections that should come prior to the
3876/// content.
3877void DwarfWriter::BeginModule(Module *M) {
Jim Laskey072200c2007-01-29 18:51:14 +00003878 DE->BeginModule(M);
3879 DD->BeginModule(M);
Jim Laskey65195462006-10-30 13:35:07 +00003880}
3881
3882/// EndModule - Emit all Dwarf sections that should come after the content.
3883///
3884void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00003885 DE->EndModule();
3886 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00003887}
3888
3889/// BeginFunction - Gather pre-function debug information. Assumes being
3890/// emitted immediately after the function entry point.
3891void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00003892 DE->BeginFunction(MF);
3893 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00003894}
3895
3896/// EndFunction - Gather and emit post-function debug information.
3897///
3898void DwarfWriter::EndFunction() {
Jim Laskey072200c2007-01-29 18:51:14 +00003899 DD->EndFunction();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003900 DE->EndFunction();
3901
Jim Laskey3f09fc22007-02-28 18:38:31 +00003902 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00003903 // Clear function debug information.
3904 MMI->EndFunction();
3905 }
Jim Laskey65195462006-10-30 13:35:07 +00003906}