blob: 04ee9e5796289fe6a4bfd01c268448ae45b9b9de [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//
5// This file was developed by James M. Laskey and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
Jim Laskeya9c83fe2006-10-30 15:59:54 +000016#include "llvm/ADT/FoldingSet.h"
Jim Laskey063e7652006-01-17 17:31:53 +000017#include "llvm/ADT/StringExtras.h"
Jim Laskey65195462006-10-30 13:35:07 +000018#include "llvm/ADT/UniqueVector.h"
Jim Laskey52060a02006-01-24 00:49:18 +000019#include "llvm/Module.h"
20#include "llvm/Type.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000021#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000024#include "llvm/CodeGen/MachineLocation.h"
Jim Laskey3f09fc22007-02-28 18:38:31 +000025#include "llvm/Support/Debug.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000026#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000027#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000028#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000029#include "llvm/Support/Mangler.h"
Jim Laskey563321a2006-09-06 18:34:40 +000030#include "llvm/Target/TargetAsmInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000031#include "llvm/Target/MRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000032#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000033#include "llvm/Target/TargetFrameInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000034#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000035#include "llvm/Target/TargetOptions.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000036#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000037#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000038using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000039using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000040
Jim Laskey0d086af2006-02-27 12:43:29 +000041namespace llvm {
Jim Laskey65195462006-10-30 13:35:07 +000042
43//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000044
45/// Configuration values for initial hash set sizes (log2).
46///
47static const unsigned InitDiesSetSize = 9; // 512
48static const unsigned InitAbbreviationsSetSize = 9; // 512
49static const unsigned InitValuesSetSize = 9; // 512
50
51//===----------------------------------------------------------------------===//
52/// Forward declarations.
53///
54class DIE;
55class DIEValue;
56
57//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000058/// DWLabel - Labels are used to track locations in the assembler file.
Jim Laskeybacd3042007-02-21 22:48:45 +000059/// Labels appear in the form <prefix><Tag><Number>, where the tag is a
Jim Laskeya9c83fe2006-10-30 15:59:54 +000060/// category of label (Ex. location) and number is a value unique in that
61/// category.
Jim Laskey65195462006-10-30 13:35:07 +000062class DWLabel {
63public:
Jim Laskeyef42a012006-11-02 20:12:39 +000064 /// Tag - Label category tag. Should always be a staticly declared C string.
65 ///
66 const char *Tag;
67
68 /// Number - Value to make label unique.
69 ///
70 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +000071
72 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Jim Laskeybd761842006-02-27 17:27:12 +000073
Jim Laskeyef42a012006-11-02 20:12:39 +000074 void Profile(FoldingSetNodeID &ID) const {
75 ID.AddString(std::string(Tag));
76 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +000077 }
Jim Laskeyef42a012006-11-02 20:12:39 +000078
79#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +000080 void print(std::ostream *O) const {
81 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +000082 }
Jim Laskeyef42a012006-11-02 20:12:39 +000083 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +000084 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +000085 if (Number) O << Number;
86 }
87#endif
Jim Laskeybd761842006-02-27 17:27:12 +000088};
89
90//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000091/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
92/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +000093class DIEAbbrevData {
94private:
Jim Laskeyef42a012006-11-02 20:12:39 +000095 /// Attribute - Dwarf attribute code.
96 ///
97 unsigned Attribute;
98
99 /// Form - Dwarf form code.
100 ///
101 unsigned Form;
Jim Laskey0d086af2006-02-27 12:43:29 +0000102
103public:
104 DIEAbbrevData(unsigned A, unsigned F)
105 : Attribute(A)
106 , Form(F)
107 {}
108
Jim Laskeybd761842006-02-27 17:27:12 +0000109 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000110 unsigned getAttribute() const { return Attribute; }
111 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000112
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000113 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000114 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000115 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000116 ID.AddInteger(Attribute);
117 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000118 }
119};
Jim Laskey063e7652006-01-17 17:31:53 +0000120
Jim Laskey0d086af2006-02-27 12:43:29 +0000121//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000122/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
123/// information object.
124class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000125private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000126 /// Tag - Dwarf tag code.
127 ///
128 unsigned Tag;
129
130 /// Unique number for node.
131 ///
132 unsigned Number;
133
134 /// ChildrenFlag - Dwarf children flag.
135 ///
136 unsigned ChildrenFlag;
137
138 /// Data - Raw data bytes for abbreviation.
139 ///
140 std::vector<DIEAbbrevData> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000141
Jim Laskey0d086af2006-02-27 12:43:29 +0000142public:
Jim Laskey063e7652006-01-17 17:31:53 +0000143
Jim Laskey0d086af2006-02-27 12:43:29 +0000144 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000145 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000146 , ChildrenFlag(C)
147 , Data()
148 {}
149 ~DIEAbbrev() {}
150
Jim Laskeybd761842006-02-27 17:27:12 +0000151 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000152 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000153 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000154 unsigned getChildrenFlag() const { return ChildrenFlag; }
155 const std::vector<DIEAbbrevData> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000156 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000157 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000158 void setNumber(unsigned N) { Number = N; }
159
Jim Laskey0d086af2006-02-27 12:43:29 +0000160 /// AddAttribute - Adds another set of attribute information to the
161 /// abbreviation.
162 void AddAttribute(unsigned Attribute, unsigned Form) {
163 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000164 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000165
Jim Laskeyb8509c52006-03-23 18:07:55 +0000166 /// AddFirstAttribute - Adds a set of attribute information to the front
167 /// of the abbreviation.
168 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
169 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
170 }
171
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000172 /// Profile - Used to gather unique data for the abbreviation folding set.
173 ///
174 void Profile(FoldingSetNodeID &ID) {
175 ID.AddInteger(Tag);
176 ID.AddInteger(ChildrenFlag);
177
178 // For each attribute description.
179 for (unsigned i = 0, N = Data.size(); i < N; ++i)
180 Data[i].Profile(ID);
181 }
182
Jim Laskey0d086af2006-02-27 12:43:29 +0000183 /// Emit - Print the abbreviation using the specified Dwarf writer.
184 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000185 void Emit(const DwarfDebug &DD) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000186
187#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000188 void print(std::ostream *O) {
189 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000190 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000191 void print(std::ostream &O);
192 void dump();
193#endif
194};
Jim Laskey063e7652006-01-17 17:31:53 +0000195
Jim Laskey0d086af2006-02-27 12:43:29 +0000196//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000197/// DIE - A structured debug information entry. Has an abbreviation which
198/// describes it's organization.
199class DIE : public FoldingSetNode {
200protected:
201 /// Abbrev - Buffer for constructing abbreviation.
202 ///
203 DIEAbbrev Abbrev;
204
205 /// Offset - Offset in debug info section.
206 ///
207 unsigned Offset;
208
209 /// Size - Size of instance + children.
210 ///
211 unsigned Size;
212
213 /// Children DIEs.
214 ///
215 std::vector<DIE *> Children;
216
217 /// Attributes values.
218 ///
219 std::vector<DIEValue *> Values;
220
221public:
222 DIE(unsigned Tag)
223 : Abbrev(Tag, DW_CHILDREN_no)
224 , Offset(0)
225 , Size(0)
226 , Children()
227 , Values()
228 {}
229 virtual ~DIE();
230
231 // Accessors.
232 DIEAbbrev &getAbbrev() { return Abbrev; }
233 unsigned getAbbrevNumber() const {
234 return Abbrev.getNumber();
235 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000236 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000237 unsigned getOffset() const { return Offset; }
238 unsigned getSize() const { return Size; }
239 const std::vector<DIE *> &getChildren() const { return Children; }
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000240 std::vector<DIEValue *> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000241 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
242 void setOffset(unsigned O) { Offset = O; }
243 void setSize(unsigned S) { Size = S; }
244
245 /// AddValue - Add a value and attributes to a DIE.
246 ///
247 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
248 Abbrev.AddAttribute(Attribute, Form);
249 Values.push_back(Value);
250 }
251
252 /// SiblingOffset - Return the offset of the debug information entry's
253 /// sibling.
254 unsigned SiblingOffset() const { return Offset + Size; }
255
256 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
257 ///
258 void AddSiblingOffset();
259
260 /// AddChild - Add a child to the DIE.
261 ///
262 void AddChild(DIE *Child) {
263 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
264 Children.push_back(Child);
265 }
266
267 /// Detach - Detaches objects connected to it after copying.
268 ///
269 void Detach() {
270 Children.clear();
271 }
272
273 /// Profile - Used to gather unique data for the value folding set.
274 ///
275 void Profile(FoldingSetNodeID &ID) ;
276
277#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000278 void print(std::ostream *O, unsigned IncIndent = 0) {
279 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000280 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000281 void print(std::ostream &O, unsigned IncIndent = 0);
282 void dump();
283#endif
284};
285
286//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000287/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000288///
289class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000290public:
291 enum {
292 isInteger,
293 isString,
294 isLabel,
295 isAsIsLabel,
296 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000297 isEntry,
298 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000299 };
300
Jim Laskeyef42a012006-11-02 20:12:39 +0000301 /// Type - Type of data stored in the value.
302 ///
303 unsigned Type;
Jim Laskey0d086af2006-02-27 12:43:29 +0000304
Jim Laskeyef42a012006-11-02 20:12:39 +0000305 DIEValue(unsigned T)
306 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000307 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000308 virtual ~DIEValue() {}
309
Jim Laskeyf6733882006-11-02 21:48:18 +0000310 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000311 unsigned getType() const { return Type; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000312
Jim Laskey0d086af2006-02-27 12:43:29 +0000313 // Implement isa/cast/dyncast.
314 static bool classof(const DIEValue *) { return true; }
315
316 /// EmitValue - Emit value via the Dwarf writer.
317 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000318 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Jim Laskey0d086af2006-02-27 12:43:29 +0000319
320 /// SizeOf - Return the size of a value in bytes.
321 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000322 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Jim Laskeyef42a012006-11-02 20:12:39 +0000323
324 /// Profile - Used to gather unique data for the value folding set.
325 ///
326 virtual void Profile(FoldingSetNodeID &ID) = 0;
327
328#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000329 void print(std::ostream *O) {
330 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000331 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000332 virtual void print(std::ostream &O) = 0;
333 void dump();
334#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000335};
Jim Laskey063e7652006-01-17 17:31:53 +0000336
Jim Laskey0d086af2006-02-27 12:43:29 +0000337//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000338/// DWInteger - An integer value DIE.
339///
Jim Laskey0d086af2006-02-27 12:43:29 +0000340class DIEInteger : public DIEValue {
341private:
342 uint64_t Integer;
343
344public:
345 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000346
Jim Laskey0d086af2006-02-27 12:43:29 +0000347 // Implement isa/cast/dyncast.
348 static bool classof(const DIEInteger *) { return true; }
349 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
350
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000351 /// BestForm - Choose the best form for integer.
352 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000353 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
354 if (IsSigned) {
355 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
356 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
357 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
358 } else {
359 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
360 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
361 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
362 }
363 return DW_FORM_data8;
364 }
365
Jim Laskey0d086af2006-02-27 12:43:29 +0000366 /// EmitValue - Emit integer of appropriate size.
367 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000368 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000369
370 /// SizeOf - Determine size of integer value in bytes.
371 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000372 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000373
374 /// Profile - Used to gather unique data for the value folding set.
375 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000376 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000377 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000378 ID.AddInteger(Integer);
379 }
Jim Laskey5496f012006-11-09 14:52:14 +0000380 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000381
382#ifndef NDEBUG
383 virtual void print(std::ostream &O) {
384 O << "Int: " << (int64_t)Integer
385 << " 0x" << std::hex << Integer << std::dec;
386 }
387#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000388};
Jim Laskey063e7652006-01-17 17:31:53 +0000389
Jim Laskey0d086af2006-02-27 12:43:29 +0000390//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000391/// DIEString - A string value DIE.
392///
Jim Laskeyef42a012006-11-02 20:12:39 +0000393class DIEString : public DIEValue {
394public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000395 const std::string String;
396
397 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000398
Jim Laskey0d086af2006-02-27 12:43:29 +0000399 // Implement isa/cast/dyncast.
400 static bool classof(const DIEString *) { return true; }
401 static bool classof(const DIEValue *S) { return S->Type == isString; }
402
403 /// EmitValue - Emit string value.
404 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000405 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000406
407 /// SizeOf - Determine size of string value in bytes.
408 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000409 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000410 return String.size() + sizeof(char); // sizeof('\0');
411 }
412
413 /// Profile - Used to gather unique data for the value folding set.
414 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000415 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000416 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000417 ID.AddString(String);
418 }
Jim Laskey5496f012006-11-09 14:52:14 +0000419 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000420
421#ifndef NDEBUG
422 virtual void print(std::ostream &O) {
423 O << "Str: \"" << String << "\"";
424 }
425#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000426};
Jim Laskey063e7652006-01-17 17:31:53 +0000427
Jim Laskey0d086af2006-02-27 12:43:29 +0000428//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000429/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000430//
Jim Laskeyef42a012006-11-02 20:12:39 +0000431class DIEDwarfLabel : public DIEValue {
432public:
433
Jim Laskey0d086af2006-02-27 12:43:29 +0000434 const DWLabel Label;
435
436 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000437
Jim Laskey0d086af2006-02-27 12:43:29 +0000438 // Implement isa/cast/dyncast.
439 static bool classof(const DIEDwarfLabel *) { return true; }
440 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
441
442 /// EmitValue - Emit label value.
443 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000444 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000445
446 /// SizeOf - Determine size of label value in bytes.
447 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000448 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000449
450 /// Profile - Used to gather unique data for the value folding set.
451 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000452 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000453 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000454 Label.Profile(ID);
455 }
Jim Laskey5496f012006-11-09 14:52:14 +0000456 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000457
458#ifndef NDEBUG
459 virtual void print(std::ostream &O) {
460 O << "Lbl: ";
461 Label.print(O);
462 }
463#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000464};
Jim Laskey063e7652006-01-17 17:31:53 +0000465
Jim Laskey063e7652006-01-17 17:31:53 +0000466
Jim Laskey0d086af2006-02-27 12:43:29 +0000467//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000468/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000469//
Jim Laskeyef42a012006-11-02 20:12:39 +0000470class DIEObjectLabel : public DIEValue {
471public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000472 const std::string Label;
473
474 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000475
Jim Laskey0d086af2006-02-27 12:43:29 +0000476 // Implement isa/cast/dyncast.
477 static bool classof(const DIEObjectLabel *) { return true; }
478 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
479
480 /// EmitValue - Emit label value.
481 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000482 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000483
484 /// SizeOf - Determine size of label value in bytes.
485 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000486 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000487
488 /// Profile - Used to gather unique data for the value folding set.
489 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000490 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000491 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000492 ID.AddString(Label);
493 }
Jim Laskey5496f012006-11-09 14:52:14 +0000494 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000495
496#ifndef NDEBUG
497 virtual void print(std::ostream &O) {
498 O << "Obj: " << Label;
499 }
500#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000501};
Jim Laskey063e7652006-01-17 17:31:53 +0000502
Jim Laskey0d086af2006-02-27 12:43:29 +0000503//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000504/// DIEDelta - A simple label difference DIE.
505///
Jim Laskeyef42a012006-11-02 20:12:39 +0000506class DIEDelta : public DIEValue {
507public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000508 const DWLabel LabelHi;
509 const DWLabel LabelLo;
510
511 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
512 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000513
Jim Laskey0d086af2006-02-27 12:43:29 +0000514 // Implement isa/cast/dyncast.
515 static bool classof(const DIEDelta *) { return true; }
516 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
517
518 /// EmitValue - Emit delta value.
519 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000520 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000521
522 /// SizeOf - Determine size of delta value in bytes.
523 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000524 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000525
526 /// Profile - Used to gather unique data for the value folding set.
527 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000528 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
529 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000530 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000531 LabelHi.Profile(ID);
532 LabelLo.Profile(ID);
533 }
Jim Laskey5496f012006-11-09 14:52:14 +0000534 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000535
536#ifndef NDEBUG
537 virtual void print(std::ostream &O) {
538 O << "Del: ";
539 LabelHi.print(O);
540 O << "-";
541 LabelLo.print(O);
542 }
543#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000544};
Jim Laskey063e7652006-01-17 17:31:53 +0000545
Jim Laskey0d086af2006-02-27 12:43:29 +0000546//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000547/// DIEntry - A pointer to another debug information entry. An instance of this
548/// class can also be used as a proxy for a debug information entry not yet
549/// defined (ie. types.)
550class DIEntry : public DIEValue {
551public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000552 DIE *Entry;
553
554 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000555
Jim Laskey0d086af2006-02-27 12:43:29 +0000556 // Implement isa/cast/dyncast.
557 static bool classof(const DIEntry *) { return true; }
558 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
559
Jim Laskeyb8509c52006-03-23 18:07:55 +0000560 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000561 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000562 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000563
Jim Laskeyb8509c52006-03-23 18:07:55 +0000564 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000565 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000566 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000567 return sizeof(int32_t);
568 }
569
570 /// Profile - Used to gather unique data for the value folding set.
571 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000572 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
573 ID.AddInteger(isEntry);
574 ID.AddPointer(Entry);
575 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000576 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000577 ID.AddInteger(isEntry);
578
Jim Laskeyef42a012006-11-02 20:12:39 +0000579 if (Entry) {
580 ID.AddPointer(Entry);
581 } else {
582 ID.AddPointer(this);
583 }
584 }
585
586#ifndef NDEBUG
587 virtual void print(std::ostream &O) {
588 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
589 }
590#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000591};
592
593//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000594/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000595//
Jim Laskeyef42a012006-11-02 20:12:39 +0000596class DIEBlock : public DIEValue, public DIE {
597public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000598 unsigned Size; // Size in bytes excluding size header.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000599
600 DIEBlock()
601 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000602 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000603 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000604 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000605 ~DIEBlock() {
606 }
607
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000608 // Implement isa/cast/dyncast.
609 static bool classof(const DIEBlock *) { return true; }
610 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
611
612 /// ComputeSize - calculate the size of the block.
613 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000614 unsigned ComputeSize(DwarfDebug &DD);
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000615
616 /// BestForm - Choose the best form for data.
617 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000618 unsigned BestForm() const {
619 if ((unsigned char)Size == Size) return DW_FORM_block1;
620 if ((unsigned short)Size == Size) return DW_FORM_block2;
621 if ((unsigned int)Size == Size) return DW_FORM_block4;
622 return DW_FORM_block;
623 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000624
625 /// EmitValue - Emit block data.
626 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000627 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000628
629 /// SizeOf - Determine size of block data in bytes.
630 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000631 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000632
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000633
Jim Laskeyef42a012006-11-02 20:12:39 +0000634 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000635 ///
Reid Spencer97821312006-11-02 23:56:21 +0000636 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000637 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000638 DIE::Profile(ID);
639 }
640
641#ifndef NDEBUG
642 virtual void print(std::ostream &O) {
643 O << "Blk: ";
644 DIE::print(O, 5);
645 }
646#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000647};
648
649//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000650/// CompileUnit - This dwarf writer support class manages information associate
651/// with a source file.
652class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000653private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000654 /// Desc - Compile unit debug descriptor.
655 ///
656 CompileUnitDesc *Desc;
657
658 /// ID - File identifier for source.
659 ///
660 unsigned ID;
661
662 /// Die - Compile unit debug information entry.
663 ///
664 DIE *Die;
665
666 /// DescToDieMap - Tracks the mapping of unit level debug informaton
667 /// descriptors to debug information entries.
668 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
669
670 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
671 /// descriptors to debug information entries using a DIEntry proxy.
672 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
673
674 /// Globals - A map of globally visible named entities for this unit.
675 ///
676 std::map<std::string, DIE *> Globals;
677
678 /// DiesSet - Used to uniquely define dies within the compile unit.
679 ///
680 FoldingSet<DIE> DiesSet;
681
682 /// Dies - List of all dies in the compile unit.
683 ///
684 std::vector<DIE *> Dies;
Jim Laskey0d086af2006-02-27 12:43:29 +0000685
686public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000687 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
688 : Desc(CUD)
689 , ID(I)
690 , Die(D)
691 , DescToDieMap()
692 , DescToDIEntryMap()
693 , Globals()
694 , DiesSet(InitDiesSetSize)
695 , Dies()
696 {}
697
698 ~CompileUnit() {
699 delete Die;
700
701 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
702 delete Dies[i];
703 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000704
Jim Laskeybd761842006-02-27 17:27:12 +0000705 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000706 CompileUnitDesc *getDesc() const { return Desc; }
707 unsigned getID() const { return ID; }
708 DIE* getDie() const { return Die; }
709 std::map<std::string, DIE *> &getGlobals() { return Globals; }
710
711 /// hasContent - Return true if this compile unit has something to write out.
712 ///
713 bool hasContent() const {
714 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000715 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000716
717 /// AddGlobal - Add a new global entity to the compile unit.
718 ///
719 void AddGlobal(const std::string &Name, DIE *Die) {
720 Globals[Name] = Die;
721 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000722
Jim Laskeyef42a012006-11-02 20:12:39 +0000723 /// getDieMapSlotFor - Returns the debug information entry map slot for the
724 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000725 DIE *&getDieMapSlotFor(DebugInfoDesc *DID) {
726 return DescToDieMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000727 }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000728
Jim Laskeyef42a012006-11-02 20:12:39 +0000729 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
730 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000731 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DID) {
732 return DescToDIEntryMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000733 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000734
Jim Laskeyef42a012006-11-02 20:12:39 +0000735 /// AddDie - Adds or interns the DIE to the compile unit.
736 ///
737 DIE *AddDie(DIE &Buffer) {
738 FoldingSetNodeID ID;
739 Buffer.Profile(ID);
740 void *Where;
741 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
742
743 if (!Die) {
744 Die = new DIE(Buffer);
745 DiesSet.InsertNode(Die, Where);
746 this->Die->AddChild(Die);
747 Buffer.Detach();
748 }
749
750 return Die;
751 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000752};
753
Jim Laskey65195462006-10-30 13:35:07 +0000754//===----------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000755/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000756///
Jim Laskey65195462006-10-30 13:35:07 +0000757class Dwarf {
758
Jim Laskey072200c2007-01-29 18:51:14 +0000759protected:
Jim Laskey65195462006-10-30 13:35:07 +0000760
761 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000762 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000763 //
764
765 //
766 /// O - Stream to .s file.
767 ///
768 std::ostream &O;
769
770 /// Asm - Target of Dwarf emission.
771 ///
772 AsmPrinter *Asm;
773
774 /// TAI - Target Asm Printer.
775 const TargetAsmInfo *TAI;
776
777 /// TD - Target data.
778 const TargetData *TD;
779
780 /// RI - Register Information.
781 const MRegisterInfo *RI;
782
783 /// M - Current module.
784 ///
785 Module *M;
786
787 /// MF - Current machine function.
788 ///
789 MachineFunction *MF;
790
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000791 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000792 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000793 MachineModuleInfo *MMI;
Jim Laskey65195462006-10-30 13:35:07 +0000794
Jim Laskey65195462006-10-30 13:35:07 +0000795 /// SubprogramCount - The running count of functions being compiled.
796 ///
797 unsigned SubprogramCount;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000798
799 unsigned SetCounter;
Jim Laskey072200c2007-01-29 18:51:14 +0000800 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
801 : O(OS)
802 , Asm(A)
803 , TAI(T)
804 , TD(Asm->TM.getTargetData())
805 , RI(Asm->TM.getRegisterInfo())
806 , M(NULL)
807 , MF(NULL)
808 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000809 , SubprogramCount(0)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000810 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000811 {
812 }
813
814public:
815
816 //===--------------------------------------------------------------------===//
817 // Accessors.
818 //
819 AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000820 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000821 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
822
Jim Laskeyb82313f2007-02-01 16:31:34 +0000823 /// PrintLabelName - Print label name in form used by Dwarf writer.
824 ///
825 void PrintLabelName(DWLabel Label) const {
826 PrintLabelName(Label.Tag, Label.Number);
827 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000828 void PrintLabelName(const char *Tag, unsigned Number,
829 bool isInSection = false) const {
830 if (isInSection && TAI->getDwarfSectionOffsetDirective())
831 O << TAI->getDwarfSectionOffsetDirective() << Tag;
832 else
833 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000834 if (Number) O << Number;
835 }
836
837 /// EmitLabel - Emit location label for internal use by Dwarf.
838 ///
839 void EmitLabel(DWLabel Label) const {
840 EmitLabel(Label.Tag, Label.Number);
841 }
842 void EmitLabel(const char *Tag, unsigned Number) const {
843 PrintLabelName(Tag, Number);
844 O << ":\n";
845 }
846
847 /// EmitReference - Emit a reference to a label.
848 ///
849 void EmitReference(DWLabel Label, bool IsPCRelative = false) const {
850 EmitReference(Label.Tag, Label.Number, IsPCRelative);
851 }
852 void EmitReference(const char *Tag, unsigned Number,
853 bool IsPCRelative = false) const {
854 if (TAI->getAddressSize() == sizeof(int32_t))
855 O << TAI->getData32bitsDirective();
856 else
857 O << TAI->getData64bitsDirective();
858
859 PrintLabelName(Tag, Number);
860
861 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
862 }
863 void EmitReference(const std::string &Name, bool IsPCRelative = false) const {
864 if (TAI->getAddressSize() == sizeof(int32_t))
865 O << TAI->getData32bitsDirective();
866 else
867 O << TAI->getData64bitsDirective();
868
869 O << Name;
870
871 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
872 }
873
874 /// EmitDifference - Emit the difference between two labels. Some
875 /// assemblers do not behave with absolute expressions with data directives,
876 /// so there is an option (needsSet) to use an intermediary set expression.
877 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000878 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000879 EmitDifference(LabelHi.Tag, LabelHi.Number,
880 LabelLo.Tag, LabelLo.Number,
881 IsSmall);
882 }
883 void EmitDifference(const char *TagHi, unsigned NumberHi,
884 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000885 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000886 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000887 O << "\t.set\t";
888 PrintLabelName("set", SetCounter);
889 O << ",";
890 PrintLabelName(TagHi, NumberHi);
891 O << "-";
892 PrintLabelName(TagLo, NumberLo);
893 O << "\n";
894
895 if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
896 O << TAI->getData32bitsDirective();
897 else
898 O << TAI->getData64bitsDirective();
899
900 PrintLabelName("set", SetCounter);
901
902 ++SetCounter;
903 } else {
904 if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
905 O << TAI->getData32bitsDirective();
906 else
907 O << TAI->getData64bitsDirective();
908
909 PrintLabelName(TagHi, NumberHi);
910 O << "-";
911 PrintLabelName(TagLo, NumberLo);
912 }
913 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000914
915 void EmitSectionOffset(const char* Label, const char* Section,
916 unsigned LabelNumber, unsigned SectionNumber,
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000917 bool IsSmall = false, bool isEH = false) {
918 bool printAbsolute = false;
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000919 if (TAI->needsSet()) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000920 O << "\t.set\t";
921 PrintLabelName("set", SetCounter);
922 O << ",";
923 PrintLabelName(Label, LabelNumber, true);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000924
925 if (isEH)
926 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
927 else
928 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
929
930 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000931 O << "-";
932 PrintLabelName(Section, SectionNumber);
933 }
934 O << "\n";
935
936 if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
937 O << TAI->getData32bitsDirective();
938 else
939 O << TAI->getData64bitsDirective();
940
941 PrintLabelName("set", SetCounter);
942 ++SetCounter;
943 } else {
944 if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
945 O << TAI->getData32bitsDirective();
946 else
947 O << TAI->getData64bitsDirective();
948
949 PrintLabelName(Label, LabelNumber, true);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000950
951 if (isEH)
952 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
953 else
954 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
955
956 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000957 O << "-";
958 PrintLabelName(Section, SectionNumber);
959 }
960 }
961 }
962
Jim Laskeyb82313f2007-02-01 16:31:34 +0000963 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
964 /// frame.
965 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
966 std::vector<MachineMove> &Moves) {
967 int stackGrowth =
968 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
969 TargetFrameInfo::StackGrowsUp ?
970 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskeybacd3042007-02-21 22:48:45 +0000971 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000972
973 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
974 MachineMove &Move = Moves[i];
975 unsigned LabelID = Move.getLabelID();
976
977 if (LabelID) {
978 LabelID = MMI->MappedLabel(LabelID);
979
980 // Throw out move if the label is invalid.
981 if (!LabelID) continue;
982 }
983
984 const MachineLocation &Dst = Move.getDestination();
985 const MachineLocation &Src = Move.getSource();
986
987 // Advance row if new location.
988 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
989 Asm->EmitInt8(DW_CFA_advance_loc4);
990 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +0000991 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
992 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +0000993
994 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +0000995 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +0000996 IsLocal = true;
997 }
998
999 // If advancing cfa.
1000 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
1001 if (!Src.isRegister()) {
1002 if (Src.getRegister() == MachineLocation::VirtualFP) {
1003 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1004 Asm->EOL("DW_CFA_def_cfa_offset");
1005 } else {
1006 Asm->EmitInt8(DW_CFA_def_cfa);
1007 Asm->EOL("DW_CFA_def_cfa");
1008 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
1009 Asm->EOL("Register");
1010 }
1011
1012 int Offset = -Src.getOffset();
1013
1014 Asm->EmitULEB128Bytes(Offset);
1015 Asm->EOL("Offset");
1016 } else {
1017 assert(0 && "Machine move no supported yet.");
1018 }
1019 } else if (Src.isRegister() &&
1020 Src.getRegister() == MachineLocation::VirtualFP) {
1021 if (Dst.isRegister()) {
1022 Asm->EmitInt8(DW_CFA_def_cfa_register);
1023 Asm->EOL("DW_CFA_def_cfa_register");
1024 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getRegister()));
1025 Asm->EOL("Register");
1026 } else {
1027 assert(0 && "Machine move no supported yet.");
1028 }
1029 } else {
1030 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
1031 int Offset = Dst.getOffset() / stackGrowth;
1032
1033 if (Offset < 0) {
1034 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1035 Asm->EOL("DW_CFA_offset_extended_sf");
1036 Asm->EmitULEB128Bytes(Reg);
1037 Asm->EOL("Reg");
1038 Asm->EmitSLEB128Bytes(Offset);
1039 Asm->EOL("Offset");
1040 } else if (Reg < 64) {
1041 Asm->EmitInt8(DW_CFA_offset + Reg);
1042 Asm->EOL("DW_CFA_offset + Reg");
1043 Asm->EmitULEB128Bytes(Offset);
1044 Asm->EOL("Offset");
1045 } else {
1046 Asm->EmitInt8(DW_CFA_offset_extended);
1047 Asm->EOL("DW_CFA_offset_extended");
1048 Asm->EmitULEB128Bytes(Reg);
1049 Asm->EOL("Reg");
1050 Asm->EmitULEB128Bytes(Offset);
1051 Asm->EOL("Offset");
1052 }
1053 }
1054 }
1055 }
1056
Jim Laskey072200c2007-01-29 18:51:14 +00001057};
1058
1059//===----------------------------------------------------------------------===//
1060/// DwarfDebug - Emits Dwarf debug directives.
1061///
1062class DwarfDebug : public Dwarf {
1063
1064private:
Jim Laskey65195462006-10-30 13:35:07 +00001065 //===--------------------------------------------------------------------===//
1066 // Attributes used to construct specific Dwarf sections.
1067 //
1068
1069 /// CompileUnits - All the compile units involved in this build. The index
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001070 /// of each entry in this vector corresponds to the sources in MMI.
Jim Laskey65195462006-10-30 13:35:07 +00001071 std::vector<CompileUnit *> CompileUnits;
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001072
Jim Laskeyef42a012006-11-02 20:12:39 +00001073 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001074 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001075 FoldingSet<DIEAbbrev> AbbreviationsSet;
1076
1077 /// Abbreviations - A list of all the unique abbreviations in use.
1078 ///
1079 std::vector<DIEAbbrev *> Abbreviations;
Jim Laskey65195462006-10-30 13:35:07 +00001080
Jim Laskeyef42a012006-11-02 20:12:39 +00001081 /// ValuesSet - Used to uniquely define values.
1082 ///
1083 FoldingSet<DIEValue> ValuesSet;
1084
1085 /// Values - A list of all the unique values in use.
1086 ///
1087 std::vector<DIEValue *> Values;
1088
Jim Laskey65195462006-10-30 13:35:07 +00001089 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001090 ///
Jim Laskey65195462006-10-30 13:35:07 +00001091 UniqueVector<std::string> StringPool;
1092
1093 /// UnitMap - Map debug information descriptor to compile unit.
1094 ///
1095 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
1096
Jim Laskey65195462006-10-30 13:35:07 +00001097 /// SectionMap - Provides a unique id per text section.
1098 ///
1099 UniqueVector<std::string> SectionMap;
1100
1101 /// SectionSourceLines - Tracks line numbers per text section.
1102 ///
1103 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1104
Jim Laskeybacd3042007-02-21 22:48:45 +00001105 /// didInitial - Flag to indicate if initial emission has been done.
1106 ///
1107 bool didInitial;
1108
1109 /// shouldEmit - Flag to indicate if debug information should be emitted.
1110 ///
1111 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001112
1113public:
Jim Laskeybacd3042007-02-21 22:48:45 +00001114
1115 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1116 ///
1117 bool ShouldEmitDwarf() const { return shouldEmit; }
Jim Laskey65195462006-10-30 13:35:07 +00001118
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001119 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Jim Laskey65195462006-10-30 13:35:07 +00001120 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001121 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1122 // Profile the node so that we can make it unique.
1123 FoldingSetNodeID ID;
1124 Abbrev.Profile(ID);
1125
1126 // Check the set for priors.
1127 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
1128
1129 // If it's newly added.
1130 if (InSet == &Abbrev) {
1131 // Add to abbreviation list.
1132 Abbreviations.push_back(&Abbrev);
1133 // Assign the vector position + 1 as its number.
1134 Abbrev.setNumber(Abbreviations.size());
1135 } else {
1136 // Assign existing abbreviation number.
1137 Abbrev.setNumber(InSet->getNumber());
1138 }
1139 }
1140
Jim Laskey65195462006-10-30 13:35:07 +00001141 /// NewString - Add a string to the constant pool and returns a label.
1142 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001143 DWLabel NewString(const std::string &String) {
1144 unsigned StringID = StringPool.insert(String);
1145 return DWLabel("string", StringID);
1146 }
Jim Laskey65195462006-10-30 13:35:07 +00001147
Jim Laskeyef42a012006-11-02 20:12:39 +00001148 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1149 /// entry.
1150 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1151 DIEntry *Value;
1152
1153 if (Entry) {
1154 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001155 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001156 void *Where;
1157 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
1158
Jim Laskeyf6733882006-11-02 21:48:18 +00001159 if (Value) return Value;
Jim Laskeyef42a012006-11-02 20:12:39 +00001160
1161 Value = new DIEntry(Entry);
1162 ValuesSet.InsertNode(Value, Where);
1163 } else {
1164 Value = new DIEntry(Entry);
1165 }
1166
1167 Values.push_back(Value);
1168 return Value;
1169 }
1170
1171 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1172 ///
1173 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1174 Value->Entry = Entry;
1175 // Add to values set if not already there. If it is, we merely have a
1176 // duplicate in the values list (no harm.)
1177 ValuesSet.GetOrInsertNode(Value);
1178 }
1179
1180 /// AddUInt - Add an unsigned integer attribute data and value.
1181 ///
1182 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1183 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1184
1185 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001186 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001187 void *Where;
1188 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1189 if (!Value) {
1190 Value = new DIEInteger(Integer);
1191 ValuesSet.InsertNode(Value, Where);
1192 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001193 }
1194
1195 Die->AddValue(Attribute, Form, Value);
1196 }
1197
1198 /// AddSInt - Add an signed integer attribute data and value.
1199 ///
1200 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1201 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1202
1203 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001204 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001205 void *Where;
1206 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1207 if (!Value) {
1208 Value = new DIEInteger(Integer);
1209 ValuesSet.InsertNode(Value, Where);
1210 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001211 }
1212
1213 Die->AddValue(Attribute, Form, Value);
1214 }
1215
1216 /// AddString - Add a std::string attribute data and value.
1217 ///
1218 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1219 const std::string &String) {
1220 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001221 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001222 void *Where;
1223 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1224 if (!Value) {
1225 Value = new DIEString(String);
1226 ValuesSet.InsertNode(Value, Where);
1227 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001228 }
1229
1230 Die->AddValue(Attribute, Form, Value);
1231 }
1232
1233 /// AddLabel - Add a Dwarf label attribute data and value.
1234 ///
1235 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1236 const DWLabel &Label) {
1237 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001238 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001239 void *Where;
1240 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1241 if (!Value) {
1242 Value = new DIEDwarfLabel(Label);
1243 ValuesSet.InsertNode(Value, Where);
1244 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001245 }
1246
1247 Die->AddValue(Attribute, Form, Value);
1248 }
1249
1250 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1251 ///
1252 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1253 const std::string &Label) {
1254 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001255 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001256 void *Where;
1257 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1258 if (!Value) {
1259 Value = new DIEObjectLabel(Label);
1260 ValuesSet.InsertNode(Value, Where);
1261 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001262 }
1263
1264 Die->AddValue(Attribute, Form, Value);
1265 }
1266
1267 /// AddDelta - Add a label delta attribute data and value.
1268 ///
1269 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1270 const DWLabel &Hi, const DWLabel &Lo) {
1271 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001272 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001273 void *Where;
1274 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1275 if (!Value) {
1276 Value = new DIEDelta(Hi, Lo);
1277 ValuesSet.InsertNode(Value, Where);
1278 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001279 }
1280
1281 Die->AddValue(Attribute, Form, Value);
1282 }
1283
1284 /// AddDIEntry - Add a DIE attribute data and value.
1285 ///
1286 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1287 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1288 }
1289
1290 /// AddBlock - Add block data.
1291 ///
1292 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1293 Block->ComputeSize(*this);
1294 FoldingSetNodeID ID;
1295 Block->Profile(ID);
1296 void *Where;
1297 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1298 if (!Value) {
1299 Value = Block;
1300 ValuesSet.InsertNode(Value, Where);
1301 Values.push_back(Value);
1302 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001303 delete Block;
1304 }
1305
1306 Die->AddValue(Attribute, Block->BestForm(), Value);
1307 }
1308
Jim Laskey65195462006-10-30 13:35:07 +00001309private:
1310
1311 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001312 /// entry.
1313 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1314 if (File && Line) {
1315 CompileUnit *FileUnit = FindCompileUnit(File);
1316 unsigned FileID = FileUnit->getID();
1317 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1318 AddUInt(Die, DW_AT_decl_line, 0, Line);
1319 }
1320 }
Jim Laskey65195462006-10-30 13:35:07 +00001321
1322 /// AddAddress - Add an address attribute to a die based on the location
1323 /// provided.
1324 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001325 const MachineLocation &Location) {
1326 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1327 DIEBlock *Block = new DIEBlock();
1328
1329 if (Location.isRegister()) {
1330 if (Reg < 32) {
1331 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1332 } else {
1333 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1334 AddUInt(Block, 0, DW_FORM_udata, Reg);
1335 }
1336 } else {
1337 if (Reg < 32) {
1338 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1339 } else {
1340 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1341 AddUInt(Block, 0, DW_FORM_udata, Reg);
1342 }
1343 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1344 }
1345
1346 AddBlock(Die, Attribute, 0, Block);
1347 }
1348
1349 /// AddBasicType - Add a new basic type attribute to the specified entity.
1350 ///
1351 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1352 const std::string &Name,
1353 unsigned Encoding, unsigned Size) {
1354 DIE *Die = ConstructBasicType(Unit, Name, Encoding, Size);
1355 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1356 }
1357
1358 /// ConstructBasicType - Construct a new basic type.
1359 ///
1360 DIE *ConstructBasicType(CompileUnit *Unit,
1361 const std::string &Name,
1362 unsigned Encoding, unsigned Size) {
1363 DIE Buffer(DW_TAG_base_type);
1364 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1365 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1366 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1367 return Unit->AddDie(Buffer);
1368 }
1369
1370 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1371 ///
1372 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1373 DIE *Die = ConstructPointerType(Unit, Name);
1374 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1375 }
1376
1377 /// ConstructPointerType - Construct a new pointer type.
1378 ///
1379 DIE *ConstructPointerType(CompileUnit *Unit, const std::string &Name) {
1380 DIE Buffer(DW_TAG_pointer_type);
1381 AddUInt(&Buffer, DW_AT_byte_size, 0, TAI->getAddressSize());
1382 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1383 return Unit->AddDie(Buffer);
1384 }
1385
1386 /// AddType - Add a new type attribute to the specified entity.
1387 ///
1388 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1389 if (!TyDesc) {
Jim Laskey2b935d52007-01-26 14:19:17 +00001390 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001391 } else {
1392 // Check for pre-existence.
1393 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
1394
1395 // If it exists then use the existing value.
1396 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001397 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1398 return;
1399 }
1400
1401 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1402 // FIXME - Not sure why programs and variables are coming through here.
1403 // Short cut for handling subprogram types (not really a TyDesc.)
1404 AddPointerType(Entity, Unit, SubprogramTy->getName());
1405 } else if (GlobalVariableDesc *GlobalTy =
1406 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1407 // FIXME - Not sure why programs and variables are coming through here.
1408 // Short cut for handling global variable types (not really a TyDesc.)
1409 AddPointerType(Entity, Unit, GlobalTy->getName());
1410 } else {
1411 // Set up proxy.
1412 Slot = NewDIEntry();
1413
1414 // Construct type.
1415 DIE Buffer(DW_TAG_base_type);
1416 ConstructType(Buffer, TyDesc, Unit);
1417
1418 // Add debug information entry to entity and unit.
1419 DIE *Die = Unit->AddDie(Buffer);
1420 SetDIEntry(Slot, Die);
1421 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1422 }
1423 }
1424 }
1425
1426 /// ConstructType - Adds all the required attributes to the type.
1427 ///
1428 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1429 // Get core information.
1430 const std::string &Name = TyDesc->getName();
1431 uint64_t Size = TyDesc->getSize() >> 3;
1432
1433 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1434 // Fundamental types like int, float, bool
1435 Buffer.setTag(DW_TAG_base_type);
1436 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1437 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00001438 // Fetch tag.
1439 unsigned Tag = DerivedTy->getTag();
1440 // FIXME - Workaround for templates.
1441 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1442 // Pointers, typedefs et al.
1443 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00001444 // Map to main type, void will not have a type.
1445 if (TypeDesc *FromTy = DerivedTy->getFromType())
1446 AddType(&Buffer, FromTy, Unit);
1447 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1448 // Fetch tag.
1449 unsigned Tag = CompTy->getTag();
1450
1451 // Set tag accordingly.
1452 if (Tag == DW_TAG_vector_type)
1453 Buffer.setTag(DW_TAG_array_type);
1454 else
1455 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00001456
Jim Laskeyef42a012006-11-02 20:12:39 +00001457 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1458
1459 switch (Tag) {
1460 case DW_TAG_vector_type:
1461 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1462 // Fall thru
1463 case DW_TAG_array_type: {
1464 // Add element type.
1465 if (TypeDesc *FromTy = CompTy->getFromType())
1466 AddType(&Buffer, FromTy, Unit);
1467
1468 // Don't emit size attribute.
1469 Size = 0;
1470
1471 // Construct an anonymous type for index type.
Jim Laskey2b935d52007-01-26 14:19:17 +00001472 DIE *IndexTy = ConstructBasicType(Unit, "", DW_ATE_signed,
1473 sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001474
1475 // Add subranges to array type.
1476 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1477 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1478 int64_t Lo = SRD->getLo();
1479 int64_t Hi = SRD->getHi();
1480 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1481
1482 // If a range is available.
1483 if (Lo != Hi) {
1484 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1485 // Only add low if non-zero.
1486 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1487 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1488 }
1489
1490 Buffer.AddChild(Subrange);
1491 }
1492 break;
1493 }
1494 case DW_TAG_structure_type:
1495 case DW_TAG_union_type: {
1496 // Add elements to structure type.
1497 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1498 DebugInfoDesc *Element = Elements[i];
1499
1500 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1501 // Add field or base class.
1502
1503 unsigned Tag = MemberDesc->getTag();
1504
1505 // Extract the basic information.
1506 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001507 uint64_t Size = MemberDesc->getSize();
1508 uint64_t Align = MemberDesc->getAlign();
1509 uint64_t Offset = MemberDesc->getOffset();
1510
1511 // Construct member debug information entry.
1512 DIE *Member = new DIE(Tag);
1513
1514 // Add name if not "".
1515 if (!Name.empty())
1516 AddString(Member, DW_AT_name, DW_FORM_string, Name);
1517 // Add location if available.
1518 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1519
1520 // Most of the time the field info is the same as the members.
1521 uint64_t FieldSize = Size;
1522 uint64_t FieldAlign = Align;
1523 uint64_t FieldOffset = Offset;
1524
Jim Laskeyee5f9272006-12-22 20:03:42 +00001525 // Set the member type.
1526 TypeDesc *FromTy = MemberDesc->getFromType();
1527 AddType(Member, FromTy, Unit);
1528
1529 // Walk up typedefs until a real size is found.
1530 while (FromTy) {
1531 if (FromTy->getTag() != DW_TAG_typedef) {
1532 FieldSize = FromTy->getSize();
1533 FieldAlign = FromTy->getSize();
1534 break;
1535 }
1536
1537 FromTy = dyn_cast<DerivedTypeDesc>(FromTy)->getFromType();
Jim Laskeyef42a012006-11-02 20:12:39 +00001538 }
1539
1540 // Unless we have a bit field.
1541 if (Tag == DW_TAG_member && FieldSize != Size) {
1542 // Construct the alignment mask.
1543 uint64_t AlignMask = ~(FieldAlign - 1);
1544 // Determine the high bit + 1 of the declared size.
1545 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1546 // Work backwards to determine the base offset of the field.
1547 FieldOffset = HiMark - FieldSize;
1548 // Now normalize offset to the field.
1549 Offset -= FieldOffset;
1550
1551 // Maybe we need to work from the other end.
1552 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1553
1554 // Add size and offset.
1555 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1556 AddUInt(Member, DW_AT_bit_size, 0, Size);
1557 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1558 }
1559
1560 // Add computation for offset.
1561 DIEBlock *Block = new DIEBlock();
1562 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1563 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1564 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1565
1566 // Add accessibility (public default unless is base class.
1567 if (MemberDesc->isProtected()) {
1568 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1569 } else if (MemberDesc->isPrivate()) {
1570 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1571 } else if (Tag == DW_TAG_inheritance) {
1572 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1573 }
1574
1575 Buffer.AddChild(Member);
1576 } else if (GlobalVariableDesc *StaticDesc =
1577 dyn_cast<GlobalVariableDesc>(Element)) {
1578 // Add static member.
1579
1580 // Construct member debug information entry.
1581 DIE *Static = new DIE(DW_TAG_variable);
1582
1583 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001584 const std::string &Name = StaticDesc->getName();
1585 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001586 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001587 if (!LinkageName.empty()) {
1588 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1589 LinkageName);
1590 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001591
1592 // Add location.
1593 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1594
1595 // Add type.
1596 if (TypeDesc *StaticTy = StaticDesc->getType())
1597 AddType(Static, StaticTy, Unit);
1598
1599 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001600 if (!StaticDesc->isStatic())
1601 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001602 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
1603
1604 Buffer.AddChild(Static);
1605 } else if (SubprogramDesc *MethodDesc =
1606 dyn_cast<SubprogramDesc>(Element)) {
1607 // Add member function.
1608
1609 // Construct member debug information entry.
1610 DIE *Method = new DIE(DW_TAG_subprogram);
1611
1612 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001613 const std::string &Name = MethodDesc->getName();
1614 const std::string &LinkageName = MethodDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001615
Jim Laskey2172f962006-11-30 14:35:45 +00001616 AddString(Method, DW_AT_name, DW_FORM_string, Name);
1617 bool IsCTor = TyDesc->getName() == Name;
1618
1619 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001620 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001621 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001622 }
1623
1624 // Add location.
1625 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1626
1627 // Add type.
1628 if (CompositeTypeDesc *MethodTy =
1629 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1630 // Get argument information.
1631 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1632
1633 // If not a ctor.
1634 if (!IsCTor) {
1635 // Add return type.
1636 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1637 }
1638
1639 // Add arguments.
1640 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1641 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1642 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1643 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1644 Method->AddChild(Arg);
1645 }
1646 }
1647
1648 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001649 if (!MethodDesc->isStatic())
1650 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001651 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
1652
1653 Buffer.AddChild(Method);
1654 }
1655 }
1656 break;
1657 }
1658 case DW_TAG_enumeration_type: {
1659 // Add enumerators to enumeration type.
1660 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1661 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1662 const std::string &Name = ED->getName();
1663 int64_t Value = ED->getValue();
1664 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1665 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1666 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1667 Buffer.AddChild(Enumerator);
1668 }
1669
1670 break;
1671 }
1672 case DW_TAG_subroutine_type: {
1673 // Add prototype flag.
1674 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1675 // Add return type.
1676 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
1677
1678 // Add arguments.
1679 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1680 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1681 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1682 Buffer.AddChild(Arg);
1683 }
1684
1685 break;
1686 }
1687 default: break;
1688 }
1689 }
1690
1691 // Add size if non-zero (derived types don't have a size.)
1692 if (Size) AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1693 // Add name if not anonymous or intermediate type.
1694 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1695 // Add source line info if available.
1696 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
1697 }
1698
1699 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00001700 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001701 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1702 // Construct debug information entry.
1703 DIE *Die = new DIE(DW_TAG_compile_unit);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001704 if (TAI->isAbsoluteDebugSectionOffsets())
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001705 AddLabel(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0));
1706 else
1707 AddDelta(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0),
1708 DWLabel("section_line", 0));
Jim Laskeyef42a012006-11-02 20:12:39 +00001709 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1710 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1711 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1712 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1713
1714 // Construct compile unit.
1715 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1716
1717 // Add Unit to compile unit map.
1718 DescToUnitMap[UnitDesc] = Unit;
1719
1720 return Unit;
1721 }
1722
Jim Laskey9d4209f2006-11-07 19:33:46 +00001723 /// GetBaseCompileUnit - Get the main compile unit.
1724 ///
1725 CompileUnit *GetBaseCompileUnit() const {
1726 CompileUnit *Unit = CompileUnits[0];
1727 assert(Unit && "Missing compile unit.");
1728 return Unit;
1729 }
1730
Jim Laskey65195462006-10-30 13:35:07 +00001731 /// FindCompileUnit - Get the compile unit for the given descriptor.
1732 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001733 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001734 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00001735 assert(Unit && "Missing compile unit.");
1736 return Unit;
1737 }
1738
1739 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00001740 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001741 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1742 // Get the compile unit context.
1743 CompileUnitDesc *UnitDesc =
1744 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001745 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001746
1747 // Check for pre-existence.
1748 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1749 if (Slot) return Slot;
1750
1751 // Get the global variable itself.
1752 GlobalVariable *GV = GVD->getGlobalVariable();
1753
Jim Laskey2172f962006-11-30 14:35:45 +00001754 const std::string &Name = GVD->getName();
1755 const std::string &FullName = GVD->getFullName();
1756 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001757 // Create the global's variable DIE.
1758 DIE *VariableDie = new DIE(DW_TAG_variable);
1759 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001760 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001761 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001762 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001763 }
Jim Laskey6488a072007-01-08 22:15:18 +00001764 AddType(VariableDie, GVD->getType(), Unit);
1765 if (!GVD->isStatic())
1766 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001767
1768 // Add source line info if available.
1769 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1770
Jim Laskeyef42a012006-11-02 20:12:39 +00001771 // Add address.
1772 DIEBlock *Block = new DIEBlock();
1773 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00001774 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
1775 AddBlock(VariableDie, DW_AT_location, 0, Block);
Jim Laskeyef42a012006-11-02 20:12:39 +00001776
1777 // Add to map.
1778 Slot = VariableDie;
1779
1780 // Add to context owner.
1781 Unit->getDie()->AddChild(VariableDie);
1782
1783 // Expose as global.
1784 // FIXME - need to check external flag.
Jim Laskey2172f962006-11-30 14:35:45 +00001785 Unit->AddGlobal(FullName, VariableDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001786
1787 return VariableDie;
1788 }
Jim Laskey65195462006-10-30 13:35:07 +00001789
1790 /// NewSubprogram - Add a new subprogram DIE.
1791 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001792 DIE *NewSubprogram(SubprogramDesc *SPD) {
1793 // Get the compile unit context.
1794 CompileUnitDesc *UnitDesc =
1795 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001796 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001797
1798 // Check for pre-existence.
1799 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1800 if (Slot) return Slot;
1801
1802 // Gather the details (simplify add attribute code.)
Jim Laskey2172f962006-11-30 14:35:45 +00001803 const std::string &Name = SPD->getName();
1804 const std::string &FullName = SPD->getFullName();
1805 const std::string &LinkageName = SPD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001806
1807 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1808 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001809 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001810 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001811 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001812 }
1813 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
Jim Laskey6488a072007-01-08 22:15:18 +00001814 if (!SPD->isStatic())
1815 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001816 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
1817
1818 // Add source line info if available.
1819 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1820
1821 // Add to map.
1822 Slot = SubprogramDie;
1823
1824 // Add to context owner.
1825 Unit->getDie()->AddChild(SubprogramDie);
1826
1827 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00001828 Unit->AddGlobal(FullName, SubprogramDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001829
1830 return SubprogramDie;
1831 }
Jim Laskey65195462006-10-30 13:35:07 +00001832
1833 /// NewScopeVariable - Create a new scope variable.
1834 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001835 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1836 // Get the descriptor.
1837 VariableDesc *VD = DV->getDesc();
1838
1839 // Translate tag to proper Dwarf tag. The result variable is dropped for
1840 // now.
1841 unsigned Tag;
1842 switch (VD->getTag()) {
1843 case DW_TAG_return_variable: return NULL;
1844 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1845 case DW_TAG_auto_variable: // fall thru
1846 default: Tag = DW_TAG_variable; break;
1847 }
1848
1849 // Define variable debug information entry.
1850 DIE *VariableDie = new DIE(Tag);
1851 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1852
1853 // Add source line info if available.
1854 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1855
1856 // Add variable type.
1857 AddType(VariableDie, VD->getType(), Unit);
1858
1859 // Add variable address.
1860 MachineLocation Location;
1861 RI->getLocation(*MF, DV->getFrameIndex(), Location);
1862 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001863
Jim Laskeyef42a012006-11-02 20:12:39 +00001864 return VariableDie;
1865 }
Jim Laskey65195462006-10-30 13:35:07 +00001866
1867 /// ConstructScope - Construct the components of a scope.
1868 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001869 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00001870 unsigned ParentStartID, unsigned ParentEndID,
1871 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001872 // Add variables to scope.
1873 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1874 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1875 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1876 if (VariableDie) ParentDie->AddChild(VariableDie);
1877 }
1878
1879 // Add nested scopes.
1880 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1881 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1882 // Define the Scope debug information entry.
1883 DebugScope *Scope = Scopes[j];
1884 // FIXME - Ignore inlined functions for the time being.
1885 if (!Scope->getParent()) continue;
1886
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001887 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1888 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00001889
Jim Laskey9d4209f2006-11-07 19:33:46 +00001890 // Ignore empty scopes.
1891 if (StartID == EndID && StartID != 0) continue;
1892 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00001893
Jim Laskey36729dd2006-11-29 16:55:57 +00001894 if (StartID == ParentStartID && EndID == ParentEndID) {
1895 // Just add stuff to the parent scope.
1896 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001897 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00001898 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1899
1900 // Add the scope bounds.
1901 if (StartID) {
1902 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00001903 DWLabel("label", StartID));
Jim Laskey36729dd2006-11-29 16:55:57 +00001904 } else {
1905 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1906 DWLabel("func_begin", SubprogramCount));
1907 }
1908 if (EndID) {
1909 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00001910 DWLabel("label", EndID));
Jim Laskey36729dd2006-11-29 16:55:57 +00001911 } else {
1912 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1913 DWLabel("func_end", SubprogramCount));
1914 }
1915
1916 // Add the scope contents.
1917 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
1918 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001919 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001920 }
1921 }
Jim Laskey65195462006-10-30 13:35:07 +00001922
1923 /// ConstructRootScope - Construct the scope for the subprogram.
1924 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001925 void ConstructRootScope(DebugScope *RootScope) {
1926 // Exit if there is no root scope.
1927 if (!RootScope) return;
1928
1929 // Get the subprogram debug information entry.
1930 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1931
1932 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00001933 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001934
1935 // Get the subprogram die.
1936 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
1937 assert(SPDie && "Missing subprogram descriptor");
1938
1939 // Add the function bounds.
1940 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1941 DWLabel("func_begin", SubprogramCount));
1942 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1943 DWLabel("func_end", SubprogramCount));
1944 MachineLocation Location(RI->getFrameRegister(*MF));
1945 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001946
Jim Laskey36729dd2006-11-29 16:55:57 +00001947 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001948 }
Jim Laskey65195462006-10-30 13:35:07 +00001949
Jim Laskeyef42a012006-11-02 20:12:39 +00001950 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1951 /// tools to recognize the object file contains Dwarf information.
1952 void EmitInitial() {
1953 // Check to see if we already emitted intial headers.
1954 if (didInitial) return;
1955 didInitial = true;
1956
1957 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00001958 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001959 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1960 EmitLabel("section_frame", 0);
1961 }
1962 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
1963 EmitLabel("section_info", 0);
1964 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
1965 EmitLabel("section_abbrev", 0);
1966 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
1967 EmitLabel("section_aranges", 0);
1968 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
1969 EmitLabel("section_macinfo", 0);
1970 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
1971 EmitLabel("section_line", 0);
1972 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
1973 EmitLabel("section_loc", 0);
1974 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
1975 EmitLabel("section_pubnames", 0);
1976 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
1977 EmitLabel("section_str", 0);
1978 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
1979 EmitLabel("section_ranges", 0);
1980
1981 Asm->SwitchToTextSection(TAI->getTextSection());
1982 EmitLabel("text_begin", 0);
1983 Asm->SwitchToDataSection(TAI->getDataSection());
1984 EmitLabel("data_begin", 0);
1985
1986 // Emit common frame information.
1987 EmitInitialDebugFrame();
1988 }
1989
Jim Laskey65195462006-10-30 13:35:07 +00001990 /// EmitDIE - Recusively Emits a debug information entry.
1991 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00001992 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001993 // Get the abbreviation for this DIE.
1994 unsigned AbbrevNumber = Die->getAbbrevNumber();
1995 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1996
Jim Laskeybacd3042007-02-21 22:48:45 +00001997 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001998
1999 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002000 Asm->EmitULEB128Bytes(AbbrevNumber);
2001 Asm->EOL(std::string("Abbrev [" +
2002 utostr(AbbrevNumber) +
2003 "] 0x" + utohexstr(Die->getOffset()) +
2004 ":0x" + utohexstr(Die->getSize()) + " " +
2005 TagString(Abbrev->getTag())));
Jim Laskeyef42a012006-11-02 20:12:39 +00002006
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002007 std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyef42a012006-11-02 20:12:39 +00002008 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2009
2010 // Emit the DIE attribute values.
2011 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2012 unsigned Attr = AbbrevData[i].getAttribute();
2013 unsigned Form = AbbrevData[i].getForm();
2014 assert(Form && "Too many attributes for DIE (check abbreviation)");
2015
2016 switch (Attr) {
2017 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002018 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002019 break;
2020 }
2021 default: {
2022 // Emit an attribute using the defined form.
2023 Values[i]->EmitValue(*this, Form);
2024 break;
2025 }
2026 }
2027
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002028 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002029 }
2030
2031 // Emit the DIE children if any.
2032 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2033 const std::vector<DIE *> &Children = Die->getChildren();
2034
2035 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2036 EmitDIE(Children[j]);
2037 }
2038
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002039 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002040 }
2041 }
2042
Jim Laskey65195462006-10-30 13:35:07 +00002043 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2044 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002045 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2046 // Get the children.
2047 const std::vector<DIE *> &Children = Die->getChildren();
2048
2049 // If not last sibling and has children then add sibling offset attribute.
2050 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2051
2052 // Record the abbreviation.
2053 AssignAbbrevNumber(Die->getAbbrev());
2054
2055 // Get the abbreviation for this DIE.
2056 unsigned AbbrevNumber = Die->getAbbrevNumber();
2057 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2058
2059 // Set DIE offset
2060 Die->setOffset(Offset);
2061
2062 // Start the size with the size of abbreviation code.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002063 Offset += Asm->SizeULEB128(AbbrevNumber);
Jim Laskeyef42a012006-11-02 20:12:39 +00002064
2065 const std::vector<DIEValue *> &Values = Die->getValues();
2066 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2067
2068 // Size the DIE attribute values.
2069 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2070 // Size attribute value.
2071 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2072 }
2073
2074 // Size the DIE children if any.
2075 if (!Children.empty()) {
2076 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2077 "Children flag not set");
2078
2079 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2080 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2081 }
2082
2083 // End of children marker.
2084 Offset += sizeof(int8_t);
2085 }
2086
2087 Die->setSize(Offset - Die->getOffset());
2088 return Offset;
2089 }
Jim Laskey65195462006-10-30 13:35:07 +00002090
2091 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2092 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002093 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002094 // Process base compile unit.
2095 CompileUnit *Unit = GetBaseCompileUnit();
2096 // Compute size of compile unit header
2097 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2098 sizeof(int16_t) + // DWARF version number
2099 sizeof(int32_t) + // Offset Into Abbrev. Section
2100 sizeof(int8_t); // Pointer Size (in bytes)
2101 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002102 }
2103
Jim Laskey65195462006-10-30 13:35:07 +00002104 /// EmitDebugInfo - Emit the debug info section.
2105 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002106 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002107 // Start debug info section.
2108 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2109
Jim Laskey5496f012006-11-09 14:52:14 +00002110 CompileUnit *Unit = GetBaseCompileUnit();
2111 DIE *Die = Unit->getDie();
2112 // Emit the compile units header.
2113 EmitLabel("info_begin", Unit->getID());
2114 // Emit size of content not including length itself
2115 unsigned ContentSize = Die->getSize() +
2116 sizeof(int16_t) + // DWARF version number
2117 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00002118 sizeof(int8_t) + // Pointer Size (in bytes)
2119 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Laskey5496f012006-11-09 14:52:14 +00002120
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002121 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2122 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002123 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002124 Asm->EOL("Offset Into Abbrev. Section");
2125 Asm->EmitInt8(TAI->getAddressSize()); Asm->EOL("Address Size (in bytes)");
Jim Laskey5496f012006-11-09 14:52:14 +00002126
2127 EmitDIE(Die);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002128 // FIXME - extra padding for gdb bug.
2129 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2130 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2131 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2132 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Jim Laskey5496f012006-11-09 14:52:14 +00002133 EmitLabel("info_end", Unit->getID());
2134
Jim Laskeybacd3042007-02-21 22:48:45 +00002135 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002136 }
2137
Jim Laskey65195462006-10-30 13:35:07 +00002138 /// EmitAbbreviations - Emit the abbreviation section.
2139 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002140 void EmitAbbreviations() const {
2141 // Check to see if it is worth the effort.
2142 if (!Abbreviations.empty()) {
2143 // Start the debug abbrev section.
2144 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2145
2146 EmitLabel("abbrev_begin", 0);
2147
2148 // For each abbrevation.
2149 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2150 // Get abbreviation data
2151 const DIEAbbrev *Abbrev = Abbreviations[i];
2152
2153 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002154 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2155 Asm->EOL("Abbreviation Code");
Jim Laskeyef42a012006-11-02 20:12:39 +00002156
2157 // Emit the abbreviations data.
2158 Abbrev->Emit(*this);
2159
Jim Laskeybacd3042007-02-21 22:48:45 +00002160 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002161 }
2162
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002163 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002164 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002165
Jim Laskeyef42a012006-11-02 20:12:39 +00002166 EmitLabel("abbrev_end", 0);
2167
Jim Laskeybacd3042007-02-21 22:48:45 +00002168 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002169 }
2170 }
2171
Jim Laskey65195462006-10-30 13:35:07 +00002172 /// EmitDebugLines - Emit source line information.
2173 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002174 void EmitDebugLines() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002175 // Minimum line delta, thus ranging from -10..(255-10).
2176 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2177 // Maximum line delta, thus ranging from -10..(255-10).
2178 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002179
Jim Laskeyef42a012006-11-02 20:12:39 +00002180 // Start the dwarf line section.
2181 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2182
2183 // Construct the section header.
2184
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002185 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002186 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002187 EmitLabel("line_begin", 0);
2188
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002189 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Jim Laskeyef42a012006-11-02 20:12:39 +00002190
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002191 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002192 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002193 EmitLabel("line_prolog_begin", 0);
2194
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002195 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002196
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002197 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002198
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002199 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002200
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002201 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002202
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002203 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Jim Laskeyef42a012006-11-02 20:12:39 +00002204
2205 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002206 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2207 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2208 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2209 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2210 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2211 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2212 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2213 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2214 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002215
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002216 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Jim Laskeyef42a012006-11-02 20:12:39 +00002217 const UniqueVector<SourceFileInfo>
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002218 &SourceFiles = MMI->getSourceFiles();
Jim Laskeyef42a012006-11-02 20:12:39 +00002219
2220 // Emit directories.
2221 for (unsigned DirectoryID = 1, NDID = Directories.size();
2222 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002223 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002224 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002225 Asm->EmitInt8(0); Asm->EOL("End of directories");
Jim Laskeyef42a012006-11-02 20:12:39 +00002226
2227 // Emit files.
2228 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2229 SourceID <= NSID; ++SourceID) {
2230 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002231 Asm->EmitString(SourceFile.getName());
2232 Asm->EOL("Source");
2233 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2234 Asm->EOL("Directory #");
2235 Asm->EmitULEB128Bytes(0);
2236 Asm->EOL("Mod date");
2237 Asm->EmitULEB128Bytes(0);
2238 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002239 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002240 Asm->EmitInt8(0); Asm->EOL("End of files");
Jim Laskeyef42a012006-11-02 20:12:39 +00002241
2242 EmitLabel("line_prolog_end", 0);
2243
2244 // A sequence for each text section.
2245 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2246 // Isolate current sections line info.
2247 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
2248
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002249 Asm->EOL(std::string("Section ") + SectionMap[j + 1]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002250
2251 // Dwarf assumes we start with first line of first source file.
2252 unsigned Source = 1;
2253 unsigned Line = 1;
2254
2255 // Construct rows of the address, source, line, column matrix.
2256 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2257 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002258 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002259 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002260
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002261 unsigned SourceID = LineInfo.getSourceID();
2262 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2263 unsigned DirectoryID = SourceFile.getDirectoryID();
2264 Asm->EOL(Directories[DirectoryID]
2265 + SourceFile.getName()
2266 + ":"
2267 + utostr_32(LineInfo.getLine()));
Jim Laskeyef42a012006-11-02 20:12:39 +00002268
2269 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002270 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2271 Asm->EmitInt8(TAI->getAddressSize() + 1); Asm->EOL("Op size");
2272 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002273 EmitReference("label", LabelID); Asm->EOL("Location label");
Jim Laskeyef42a012006-11-02 20:12:39 +00002274
2275 // If change of source, then switch to the new source.
2276 if (Source != LineInfo.getSourceID()) {
2277 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002278 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2279 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002280 }
2281
2282 // If change of line.
2283 if (Line != LineInfo.getLine()) {
2284 // Determine offset.
2285 int Offset = LineInfo.getLine() - Line;
2286 int Delta = Offset - MinLineDelta;
2287
2288 // Update line.
2289 Line = LineInfo.getLine();
2290
2291 // If delta is small enough and in range...
2292 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2293 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002294 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002295 } else {
2296 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002297 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2298 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2299 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002300 }
2301 } else {
2302 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002303 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002304 }
2305 }
2306
2307 // Define last address of section.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002308 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2309 Asm->EmitInt8(TAI->getAddressSize() + 1); Asm->EOL("Op size");
2310 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2311 EmitReference("section_end", j + 1); Asm->EOL("Section end label");
Jim Laskeyef42a012006-11-02 20:12:39 +00002312
2313 // Mark end of matrix.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002314 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
Jim Laskeybacd3042007-02-21 22:48:45 +00002315 Asm->EmitULEB128Bytes(1); Asm->EOL();
2316 Asm->EmitInt8(1); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002317 }
2318
2319 EmitLabel("line_end", 0);
2320
Jim Laskeybacd3042007-02-21 22:48:45 +00002321 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002322 }
2323
Jim Laskey65195462006-10-30 13:35:07 +00002324 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
2325 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002326 void EmitInitialDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002327 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002328 return;
2329
2330 int stackGrowth =
2331 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2332 TargetFrameInfo::StackGrowsUp ?
2333 TAI->getAddressSize() : -TAI->getAddressSize();
2334
2335 // Start the dwarf frame section.
2336 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2337
2338 EmitLabel("frame_common", 0);
2339 EmitDifference("frame_common_end", 0,
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002340 "frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002341 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002342
2343 EmitLabel("frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002344 Asm->EmitInt32((int)DW_CIE_ID);
2345 Asm->EOL("CIE Identifier Tag");
2346 Asm->EmitInt8(DW_CIE_VERSION);
2347 Asm->EOL("CIE Version");
2348 Asm->EmitString("");
2349 Asm->EOL("CIE Augmentation");
2350 Asm->EmitULEB128Bytes(1);
2351 Asm->EOL("CIE Code Alignment Factor");
2352 Asm->EmitSLEB128Bytes(stackGrowth);
2353 Asm->EOL("CIE Data Alignment Factor");
2354 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister()));
2355 Asm->EOL("CIE RA Column");
Jim Laskey65195462006-10-30 13:35:07 +00002356
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002357 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002358 RI->getInitialFrameState(Moves);
2359 EmitFrameMoves(NULL, 0, Moves);
Jim Laskeyef42a012006-11-02 20:12:39 +00002360
Jim Laskeyf9e56192007-01-24 13:12:32 +00002361 Asm->EmitAlignment(2);
Jim Laskeyef42a012006-11-02 20:12:39 +00002362 EmitLabel("frame_common_end", 0);
2363
Jim Laskeybacd3042007-02-21 22:48:45 +00002364 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002365 }
2366
Jim Laskey65195462006-10-30 13:35:07 +00002367 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2368 /// section.
Jim Laskeyef42a012006-11-02 20:12:39 +00002369 void EmitFunctionDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002370 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002371 return;
Jim Laskey9d4209f2006-11-07 19:33:46 +00002372
Jim Laskeyef42a012006-11-02 20:12:39 +00002373 // Start the dwarf frame section.
2374 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2375
2376 EmitDifference("frame_end", SubprogramCount,
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002377 "frame_begin", SubprogramCount, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002378 Asm->EOL("Length of Frame Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002379
2380 EmitLabel("frame_begin", SubprogramCount);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002381
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002382 EmitSectionOffset("frame_common_begin", "section_frame", 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002383 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002384
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002385 EmitReference("func_begin", SubprogramCount);
2386 Asm->EOL("FDE initial location");
Jim Laskeyef42a012006-11-02 20:12:39 +00002387 EmitDifference("func_end", SubprogramCount,
2388 "func_begin", SubprogramCount);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002389 Asm->EOL("FDE address range");
Jim Laskeyef42a012006-11-02 20:12:39 +00002390
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002391 std::vector<MachineMove> &Moves = MMI->getFrameMoves();
Jim Laskeyef42a012006-11-02 20:12:39 +00002392
2393 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2394
Jim Laskeyf9e56192007-01-24 13:12:32 +00002395 Asm->EmitAlignment(2);
Jim Laskeyef42a012006-11-02 20:12:39 +00002396 EmitLabel("frame_end", SubprogramCount);
2397
Jim Laskeybacd3042007-02-21 22:48:45 +00002398 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002399 }
2400
2401 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002402 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002403 void EmitDebugPubNames() {
2404 // Start the dwarf pubnames section.
2405 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2406
Jim Laskey5496f012006-11-09 14:52:14 +00002407 CompileUnit *Unit = GetBaseCompileUnit();
2408
2409 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002410 "pubnames_begin", Unit->getID(), true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002411 Asm->EOL("Length of Public Names Info");
Jim Laskey5496f012006-11-09 14:52:14 +00002412
2413 EmitLabel("pubnames_begin", Unit->getID());
2414
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002415 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002416
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002417 EmitSectionOffset("info_begin", "section_info",
2418 Unit->getID(), 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002419 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002420
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002421 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002422 Asm->EOL("Compilation Unit Length");
Jim Laskey5496f012006-11-09 14:52:14 +00002423
2424 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2425
2426 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2427 GE = Globals.end();
2428 GI != GE; ++GI) {
2429 const std::string &Name = GI->first;
2430 DIE * Entity = GI->second;
Jim Laskeyef42a012006-11-02 20:12:39 +00002431
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002432 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2433 Asm->EmitString(Name); Asm->EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00002434 }
Jim Laskey5496f012006-11-09 14:52:14 +00002435
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002436 Asm->EmitInt32(0); Asm->EOL("End Mark");
Jim Laskey5496f012006-11-09 14:52:14 +00002437 EmitLabel("pubnames_end", Unit->getID());
2438
Jim Laskeybacd3042007-02-21 22:48:45 +00002439 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002440 }
2441
2442 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002443 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002444 void EmitDebugStr() {
2445 // Check to see if it is worth the effort.
2446 if (!StringPool.empty()) {
2447 // Start the dwarf str section.
2448 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2449
2450 // For each of strings in the string pool.
2451 for (unsigned StringID = 1, N = StringPool.size();
2452 StringID <= N; ++StringID) {
2453 // Emit a label for reference from debug information entries.
2454 EmitLabel("string", StringID);
2455 // Emit the string itself.
2456 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002457 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002458 }
2459
Jim Laskeybacd3042007-02-21 22:48:45 +00002460 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002461 }
2462 }
2463
2464 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002465 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002466 void EmitDebugLoc() {
2467 // Start the dwarf loc section.
2468 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2469
Jim Laskeybacd3042007-02-21 22:48:45 +00002470 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002471 }
2472
2473 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002474 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002475 void EmitDebugARanges() {
2476 // Start the dwarf aranges section.
2477 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2478
2479 // FIXME - Mock up
2480 #if 0
Jim Laskey5496f012006-11-09 14:52:14 +00002481 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002482
Jim Laskey5496f012006-11-09 14:52:14 +00002483 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002484 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Jim Laskey5496f012006-11-09 14:52:14 +00002485
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002486 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Jim Laskey5496f012006-11-09 14:52:14 +00002487
2488 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002489 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002490
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002491 Asm->EmitInt8(TAI->getAddressSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002492
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002493 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002494
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002495 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2496 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002497
Jim Laskey5496f012006-11-09 14:52:14 +00002498 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002499 EmitReference("text_begin", 0); Asm->EOL("Address");
2500 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002501
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002502 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2503 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Jim Laskey5496f012006-11-09 14:52:14 +00002504
Jim Laskeybacd3042007-02-21 22:48:45 +00002505 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002506 #endif
2507 }
2508
2509 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002510 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002511 void EmitDebugRanges() {
2512 // Start the dwarf ranges section.
2513 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2514
Jim Laskeybacd3042007-02-21 22:48:45 +00002515 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002516 }
2517
2518 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002519 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002520 void EmitDebugMacInfo() {
2521 // Start the dwarf macinfo section.
2522 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2523
Jim Laskeybacd3042007-02-21 22:48:45 +00002524 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002525 }
2526
Jim Laskey65195462006-10-30 13:35:07 +00002527 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2528 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002529 void ConstructCompileUnitDIEs() {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002530 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
Jim Laskeyef42a012006-11-02 20:12:39 +00002531
2532 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002533 unsigned ID = MMI->RecordSource(CUW[i]);
Jim Laskey9d4209f2006-11-07 19:33:46 +00002534 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002535 CompileUnits.push_back(Unit);
2536 }
2537 }
2538
Jim Laskey65195462006-10-30 13:35:07 +00002539 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2540 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002541 void ConstructGlobalDIEs() {
2542 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002543 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M);
Jim Laskeyef42a012006-11-02 20:12:39 +00002544
2545 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2546 GlobalVariableDesc *GVD = GlobalVariables[i];
2547 NewGlobalVariable(GVD);
2548 }
2549 }
Jim Laskey65195462006-10-30 13:35:07 +00002550
2551 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2552 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002553 void ConstructSubprogramDIEs() {
2554 std::vector<SubprogramDesc *> Subprograms =
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002555 MMI->getAnchoredDescriptors<SubprogramDesc>(*M);
Jim Laskeyef42a012006-11-02 20:12:39 +00002556
2557 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2558 SubprogramDesc *SPD = Subprograms[i];
2559 NewSubprogram(SPD);
2560 }
2561 }
Jim Laskey65195462006-10-30 13:35:07 +00002562
Jim Laskey65195462006-10-30 13:35:07 +00002563public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002564 //===--------------------------------------------------------------------===//
2565 // Main entry points.
2566 //
Jim Laskey072200c2007-01-29 18:51:14 +00002567 DwarfDebug(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
2568 : Dwarf(OS, A, T)
Jim Laskeyef42a012006-11-02 20:12:39 +00002569 , CompileUnits()
2570 , AbbreviationsSet(InitAbbreviationsSetSize)
2571 , Abbreviations()
2572 , ValuesSet(InitValuesSetSize)
2573 , Values()
2574 , StringPool()
2575 , DescToUnitMap()
2576 , SectionMap()
2577 , SectionSourceLines()
Jim Laskeybacd3042007-02-21 22:48:45 +00002578 , didInitial(false)
2579 , shouldEmit(false)
Jim Laskeyef42a012006-11-02 20:12:39 +00002580 {
2581 }
Jim Laskey072200c2007-01-29 18:51:14 +00002582 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002583 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2584 delete CompileUnits[i];
2585 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2586 delete Values[j];
2587 }
2588
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002589 /// SetModuleInfo - Set machine module information when it's known that pass
2590 /// manager has created it. Set by the target AsmPrinter.
2591 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002592 // Make sure initial declarations are made.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002593 if (!MMI && mmi->hasDebugInfo()) {
2594 MMI = mmi;
Jim Laskeyef42a012006-11-02 20:12:39 +00002595 shouldEmit = true;
2596
2597 // Emit initial sections
2598 EmitInitial();
2599
2600 // Create all the compile unit DIEs.
2601 ConstructCompileUnitDIEs();
2602
2603 // Create DIEs for each of the externally visible global variables.
2604 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002605
Jim Laskeyef42a012006-11-02 20:12:39 +00002606 // Create DIEs for each of the externally visible subprograms.
2607 ConstructSubprogramDIEs();
2608
2609 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00002610 SectionMap.insert(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002611 }
2612 }
2613
Jim Laskey65195462006-10-30 13:35:07 +00002614 /// BeginModule - Emit all Dwarf sections that should come prior to the
2615 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002616 void BeginModule(Module *M) {
2617 this->M = M;
2618
2619 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002620 }
2621
Jim Laskey65195462006-10-30 13:35:07 +00002622 /// EndModule - Emit all Dwarf sections that should come after the content.
2623 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002624 void EndModule() {
2625 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002626
2627 // Standard sections final addresses.
2628 Asm->SwitchToTextSection(TAI->getTextSection());
2629 EmitLabel("text_end", 0);
2630 Asm->SwitchToDataSection(TAI->getDataSection());
2631 EmitLabel("data_end", 0);
2632
2633 // End text sections.
2634 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2635 Asm->SwitchToTextSection(SectionMap[i].c_str());
2636 EmitLabel("section_end", i);
2637 }
2638
2639 // Compute DIE offsets and sizes.
2640 SizeAndOffsets();
2641
2642 // Emit all the DIEs into a debug info section
2643 EmitDebugInfo();
2644
2645 // Corresponding abbreviations into a abbrev section.
2646 EmitAbbreviations();
2647
2648 // Emit source line correspondence into a debug line section.
2649 EmitDebugLines();
2650
2651 // Emit info into a debug pubnames section.
2652 EmitDebugPubNames();
2653
2654 // Emit info into a debug str section.
2655 EmitDebugStr();
2656
2657 // Emit info into a debug loc section.
2658 EmitDebugLoc();
2659
2660 // Emit info into a debug aranges section.
2661 EmitDebugARanges();
2662
2663 // Emit info into a debug ranges section.
2664 EmitDebugRanges();
2665
2666 // Emit info into a debug macinfo section.
2667 EmitDebugMacInfo();
2668 }
2669
Jim Laskey65195462006-10-30 13:35:07 +00002670 /// BeginFunction - Gather pre-function debug information. Assumes being
2671 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002672 void BeginFunction(MachineFunction *MF) {
2673 this->MF = MF;
2674
2675 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002676
2677 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002678 MMI->BeginFunction(MF);
Jim Laskeyef42a012006-11-02 20:12:39 +00002679
2680 // Assumes in correct section after the entry point.
2681 EmitLabel("func_begin", ++SubprogramCount);
2682 }
Jim Laskey072200c2007-01-29 18:51:14 +00002683
Jim Laskey65195462006-10-30 13:35:07 +00002684 /// EndFunction - Gather and emit post-function debug information.
2685 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002686 void EndFunction() {
2687 if (!ShouldEmitDwarf()) return;
Jim Laskeyb82313f2007-02-01 16:31:34 +00002688
2689 // Define end label for subprogram.
2690 EmitLabel("func_end", SubprogramCount);
Jim Laskeyef42a012006-11-02 20:12:39 +00002691
2692 // Get function line info.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002693 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
Jim Laskeyef42a012006-11-02 20:12:39 +00002694
2695 if (!LineInfos.empty()) {
2696 // Get section line info.
2697 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2698 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2699 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2700 // Append the function info to section info.
2701 SectionLineInfos.insert(SectionLineInfos.end(),
2702 LineInfos.begin(), LineInfos.end());
2703 }
2704
2705 // Construct scopes for subprogram.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002706 ConstructRootScope(MMI->getRootScope());
Jim Laskeyef42a012006-11-02 20:12:39 +00002707
2708 // Emit function frame information.
2709 EmitFunctionDebugFrame();
Jim Laskeyef42a012006-11-02 20:12:39 +00002710 }
Jim Laskey65195462006-10-30 13:35:07 +00002711};
2712
Jim Laskey072200c2007-01-29 18:51:14 +00002713//===----------------------------------------------------------------------===//
2714/// DwarfException - Emits Dwarf exception handling directives.
2715///
2716class DwarfException : public Dwarf {
2717
Jim Laskeyb82313f2007-02-01 16:31:34 +00002718private:
2719
Jim Laskeybacd3042007-02-21 22:48:45 +00002720 /// didInitial - Flag to indicate if initial emission has been done.
2721 ///
2722 bool didInitial;
2723
2724 /// shouldEmit - Flag to indicate if debug information should be emitted.
2725 ///
2726 bool shouldEmit;
Jim Laskey3f09fc22007-02-28 18:38:31 +00002727
Jim Laskey3f09fc22007-02-28 18:38:31 +00002728 /// EmitCommonEHFrame - Emit the common eh unwind frame.
2729 ///
2730 void EmitCommonEHFrame() {
2731 // Only do it once.
2732 if (didInitial) return;
2733 didInitial = true;
2734
Jim Laskeybacd3042007-02-21 22:48:45 +00002735 // If there is a personality present then we need to indicate that
2736 // in the common eh frame.
Duncan Sands3194f572007-05-05 20:27:00 +00002737 Function *Personality = MMI->getPersonality();
Jim Laskeybacd3042007-02-21 22:48:45 +00002738
2739 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002740 int stackGrowth =
2741 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2742 TargetFrameInfo::StackGrowsUp ?
2743 TAI->getAddressSize() : -TAI->getAddressSize();
2744
Jim Laskeybacd3042007-02-21 22:48:45 +00002745 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002746 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
2747 O << "EH_frame:\n";
2748 EmitLabel("section_eh_frame", 0);
2749
Jim Laskeybacd3042007-02-21 22:48:45 +00002750 // Define base labels.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002751 EmitLabel("eh_frame_common", 0);
Jim Laskeybacd3042007-02-21 22:48:45 +00002752
2753 // Define the eh frame length.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002754 EmitDifference("eh_frame_common_end", 0,
2755 "eh_frame_common_begin", 0, true);
2756 Asm->EOL("Length of Common Information Entry");
2757
Jim Laskeybacd3042007-02-21 22:48:45 +00002758 // EH frame header.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002759 EmitLabel("eh_frame_common_begin", 0);
2760 Asm->EmitInt32((int)0);
2761 Asm->EOL("CIE Identifier Tag");
2762 Asm->EmitInt8(DW_CIE_VERSION);
2763 Asm->EOL("CIE Version");
Jim Laskeybacd3042007-02-21 22:48:45 +00002764
2765 // The personality presence indicates that language specific information
2766 // will show up in the eh frame.
2767 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00002768 Asm->EOL("CIE Augmentation");
Jim Laskeybacd3042007-02-21 22:48:45 +00002769
2770 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002771 Asm->EmitULEB128Bytes(1);
2772 Asm->EOL("CIE Code Alignment Factor");
2773 Asm->EmitSLEB128Bytes(stackGrowth);
2774 Asm->EOL("CIE Data Alignment Factor");
2775 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister()));
2776 Asm->EOL("CIE RA Column");
Jim Laskeyb82313f2007-02-01 16:31:34 +00002777
Jim Laskeybacd3042007-02-21 22:48:45 +00002778 // If there is a personality, we need to indicate the functions location.
2779 if (Personality) {
2780 Asm->EmitULEB128Bytes(7);
2781 Asm->EOL("Augmentation Size");
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002782 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2783 Asm->EOL("Personality (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00002784
2785 O << TAI->getData32bitsDirective();
2786 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
2787 O << "-" << TAI->getPCSymbol();
2788 Asm->EOL("Personality");
2789
2790 Asm->EmitULEB128Bytes(DW_EH_PE_pcrel);
2791 Asm->EOL("LSDA Encoding (pcrel)");
2792 Asm->EmitULEB128Bytes(DW_EH_PE_pcrel);
2793 Asm->EOL("FDE Encoding (pcrel)");
2794 } else {
2795 Asm->EmitULEB128Bytes(1);
2796 Asm->EOL("Augmentation Size");
2797 Asm->EmitULEB128Bytes(DW_EH_PE_pcrel);
2798 Asm->EOL("FDE Encoding (pcrel)");
2799 }
2800
2801 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002802 std::vector<MachineMove> Moves;
2803 RI->getInitialFrameState(Moves);
2804 EmitFrameMoves(NULL, 0, Moves);
2805
2806 Asm->EmitAlignment(2);
2807 EmitLabel("eh_frame_common_end", 0);
2808
Jim Laskeybacd3042007-02-21 22:48:45 +00002809 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00002810 }
2811
Jim Laskeyb82313f2007-02-01 16:31:34 +00002812 /// EmitEHFrame - Emit initial exception information.
2813 ///
2814 void EmitEHFrame() {
Jim Laskeybacd3042007-02-21 22:48:45 +00002815 // If there is a personality present then we need to indicate that
2816 // in the common eh frame.
Duncan Sands3194f572007-05-05 20:27:00 +00002817 Function *Personality = MMI->getPersonality();
Jim Laskeybacd3042007-02-21 22:48:45 +00002818 MachineFrameInfo *MFI = MF->getFrameInfo();
Jim Laskeyb82313f2007-02-01 16:31:34 +00002819
Jim Laskeybacd3042007-02-21 22:48:45 +00002820 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
2821
2822 // Externally visible entry into the functions eh frame info.
2823 if (const char *GlobalDirective = TAI->getGlobalDirective())
2824 O << GlobalDirective << getAsm()->CurrentFnName << ".eh\n";
Jim Laskeyb82313f2007-02-01 16:31:34 +00002825
Jim Laskeybacd3042007-02-21 22:48:45 +00002826 // If there are no calls then you can't unwind.
2827 if (!MFI->hasCalls()) {
2828 O << getAsm()->CurrentFnName << ".eh = 0\n";
2829 } else {
2830 O << getAsm()->CurrentFnName << ".eh:\n";
2831
2832 // EH frame header.
2833 EmitDifference("eh_frame_end", SubprogramCount,
2834 "eh_frame_begin", SubprogramCount, true);
2835 Asm->EOL("Length of Frame Information Entry");
2836
2837 EmitLabel("eh_frame_begin", SubprogramCount);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002838
2839 EmitSectionOffset("eh_frame_begin", "section_eh_frame",
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002840 SubprogramCount, 0, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00002841 Asm->EOL("FDE CIE offset");
2842
2843 EmitReference("eh_func_begin", SubprogramCount, true);
2844 Asm->EOL("FDE initial location");
2845 EmitDifference("eh_func_end", SubprogramCount,
2846 "eh_func_begin", SubprogramCount);
2847 Asm->EOL("FDE address range");
2848
Jim Laskey3f09fc22007-02-28 18:38:31 +00002849 // If there is a personality and landing pads then point to the language
2850 // specific data area in the exception table.
Jim Laskeybacd3042007-02-21 22:48:45 +00002851 if (Personality) {
2852 Asm->EmitULEB128Bytes(4);
2853 Asm->EOL("Augmentation size");
Jim Laskey3f09fc22007-02-28 18:38:31 +00002854
2855 if (!MMI->getLandingPads().empty()) {
2856 EmitReference("exception", SubprogramCount, true);
2857 } else if(TAI->getAddressSize() == 8) {
2858 Asm->EmitInt64((int)0);
2859 } else {
2860 Asm->EmitInt32((int)0);
2861 }
Jim Laskeybacd3042007-02-21 22:48:45 +00002862 Asm->EOL("Language Specific Data Area");
2863 } else {
2864 Asm->EmitULEB128Bytes(0);
2865 Asm->EOL("Augmentation size");
2866 }
2867
2868 // Indicate locations of function specific callee saved registers in
2869 // frame.
2870 std::vector<MachineMove> &Moves = MMI->getFrameMoves();
2871 EmitFrameMoves("eh_func_begin", SubprogramCount, Moves);
2872
2873 Asm->EmitAlignment(2);
2874 EmitLabel("eh_frame_end", SubprogramCount);
2875 }
Jim Laskeyb82313f2007-02-01 16:31:34 +00002876
Jim Laskeybacd3042007-02-21 22:48:45 +00002877 if (const char *UsedDirective = TAI->getUsedDirective())
2878 O << UsedDirective << getAsm()->CurrentFnName << ".eh\n\n";
2879 }
2880
2881 /// EmitExceptionTable - Emit landpads and actions.
2882 ///
2883 /// The general organization of the table is complex, but the basic concepts
2884 /// are easy. First there is a header which describes the location and
2885 /// organization of the three components that follow.
2886 /// 1. The landing pad site information describes the range of code covered
2887 /// by the try. In our case it's an accumulation of the ranges covered
2888 /// by the invokes in the try. There is also a reference to the landing
2889 /// pad that handles the exception once processed. Finally an index into
2890 /// the actions table.
2891 /// 2. The action table, in our case, is composed of pairs of type ids
2892 /// and next action offset. Starting with the action index from the
2893 /// landing pad site, each type Id is checked for a match to the current
2894 /// exception. If it matches then the exception and type id are passed
2895 /// on to the landing pad. Otherwise the next action is looked up. This
2896 /// chain is terminated with a next action of zero. If no type id is
2897 /// found the the frame is unwound and handling continues.
2898 /// 3. Type id table contains references to all the C++ typeinfo for all
2899 /// catches in the function. This tables is reversed indexed base 1.
2900 void EmitExceptionTable() {
2901 // Map all labels and get rid of any dead landing pads.
2902 MMI->TidyLandingPads();
Jim Laskeyb82313f2007-02-01 16:31:34 +00002903
Jim Laskeybacd3042007-02-21 22:48:45 +00002904 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
2905 const std::vector<LandingPadInfo> &LandingPads = MMI->getLandingPads();
2906 if (LandingPads.empty()) return;
Jim Laskeyb82313f2007-02-01 16:31:34 +00002907
Jim Laskeybacd3042007-02-21 22:48:45 +00002908 // FIXME - Should fold actions for multiple landing pads.
2909
2910 // Gather first action index for each landing pad site.
2911 SmallVector<unsigned, 8> Actions;
Jim Laskey0102ca82007-03-01 20:26:43 +00002912
2913 // FIXME - Assume there is only one filter typeinfo list per function
2914 // time being. I.E., Each call to eh_filter will have the same list.
2915 // This can change if a function is inlined.
2916 const LandingPadInfo *Filter = 0;
Jim Laskeybacd3042007-02-21 22:48:45 +00002917
2918 // Compute sizes for exception table.
2919 unsigned SizeHeader = sizeof(int8_t) + // LPStart format
2920 sizeof(int8_t) + // TType format
2921 sizeof(int8_t) + // TType base offset (NEED ULEB128)
2922 sizeof(int8_t) + // Call site format
2923 sizeof(int8_t); // Call-site table length
2924 unsigned SizeSites = 0;
2925 unsigned SizeActions = 0;
2926
2927 // Look at each landing pad site to compute size. We need the size of each
2928 // landing pad site info and the size of the landing pad's actions.
2929 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
2930 const LandingPadInfo &LandingPad = LandingPads[i];
Jim Laskey0102ca82007-03-01 20:26:43 +00002931 bool IsFilter = LandingPad.IsFilter;
Jim Laskeybacd3042007-02-21 22:48:45 +00002932 unsigned SizeSiteActions = 0;
2933 const std::vector<unsigned> &TypeIds = LandingPad.TypeIds;
2934 unsigned SizeAction = 0;
Jim Laskey0102ca82007-03-01 20:26:43 +00002935 signed FirstAction;
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00002936
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00002937 if (IsFilter) {
2938 // FIXME - Assume there is only one filter typeinfo list per function
2939 // time being. I.E., Each call to eh_filter will have the same list.
2940 // This can change if a function is inlined.
2941 Filter = &LandingPad;
2942 SizeAction = Asm->SizeSLEB128(-1) + Asm->SizeSLEB128(0);
2943 SizeSiteActions += SizeAction;
2944 // Record the first action of the landing pad site.
2945 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
2946 } else if (TypeIds.empty()) {
2947 FirstAction = 0;
2948 } else {
2949 // Gather the action sizes
2950 for (unsigned j = 0, M = TypeIds.size(); j != M; ++j) {
2951 unsigned TypeID = TypeIds[j];
2952 unsigned SizeTypeID = Asm->SizeSLEB128(TypeID);
2953 signed Action = j ? -(SizeAction + SizeTypeID) : 0;
2954 SizeAction = SizeTypeID + Asm->SizeSLEB128(Action);
Jim Laskey0102ca82007-03-01 20:26:43 +00002955 SizeSiteActions += SizeAction;
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00002956 }
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00002957
2958 // Record the first action of the landing pad site.
2959 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
2960 }
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00002961 Actions.push_back(FirstAction);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00002962
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00002963 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00002964 SizeActions += SizeSiteActions;
2965 unsigned M = LandingPad.BeginLabels.size();
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00002966 SizeSites += M*(sizeof(int32_t) + // Site start.
2967 sizeof(int32_t) + // Site length.
2968 sizeof(int32_t) + // Landing pad.
2969 Asm->SizeSLEB128(FirstAction)); // Action.
Jim Laskeybacd3042007-02-21 22:48:45 +00002970 }
2971
2972 // Final tallies.
2973 unsigned SizeTypes = TypeInfos.size() * TAI->getAddressSize();
2974 unsigned SizePreType = SizeHeader + SizeSites + SizeActions;
2975 unsigned SizeAlign = (4 - SizePreType) & 3;
2976 unsigned TypeOffset = SizePreType +
2977 SizeTypes +
2978 SizeAlign -
2979 sizeof(int8_t) - // LPStart format
2980 sizeof(int8_t) - // TType format
2981 sizeof(int8_t); // TType base offset (NEED ULEB128)
Duncan Sandsc1fe1662007-05-10 18:40:24 +00002982
Jim Laskeybacd3042007-02-21 22:48:45 +00002983 // Begin the exception table.
2984 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
2985 O << "GCC_except_table" << SubprogramCount << ":\n";
Duncan Sandsc1fe1662007-05-10 18:40:24 +00002986 Asm->EmitAlignment(2);
Jim Laskeybacd3042007-02-21 22:48:45 +00002987 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00002988
Jim Laskeybacd3042007-02-21 22:48:45 +00002989 // Emit the header.
2990 Asm->EmitInt8(DW_EH_PE_omit);
2991 Asm->EOL("LPStart format (DW_EH_PE_omit)");
2992 Asm->EmitInt8(DW_EH_PE_absptr);
2993 Asm->EOL("TType format (DW_EH_PE_absptr)");
2994 Asm->EmitULEB128Bytes(TypeOffset);
2995 Asm->EOL("TType base offset");
2996 Asm->EmitInt8(DW_EH_PE_udata4);
2997 Asm->EOL("Call site format (DW_EH_PE_udata4)");
2998 Asm->EmitULEB128Bytes(SizeSites);
2999 Asm->EOL("Call-site table length");
3000
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003001 // Emit the landing pad site information.
Jim Laskeybacd3042007-02-21 22:48:45 +00003002 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3003 const LandingPadInfo &LandingPad = LandingPads[i];
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003004 for (unsigned j=0, E = LandingPad.BeginLabels.size(); j != E; ++j) {
3005 EmitSectionOffset("label", "eh_func_begin",
3006 LandingPad.BeginLabels[j], SubprogramCount,
3007 false, true);
3008 Asm->EOL("Region start");
Jim Laskeybacd3042007-02-21 22:48:45 +00003009
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003010 EmitDifference("label", LandingPad.EndLabels[j],
3011 "label", LandingPad.BeginLabels[j]);
3012 Asm->EOL("Region length");
Jim Laskeybacd3042007-02-21 22:48:45 +00003013
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003014 if (LandingPad.TypeIds.empty()) {
3015 if (TAI->getAddressSize() == sizeof(int32_t))
3016 Asm->EmitInt32(0);
3017 else
3018 Asm->EmitInt64(0);
3019 } else {
3020 EmitSectionOffset("label", "eh_func_begin", LandingPad.LandingPadLabel,
3021 SubprogramCount, false, true);
3022 }
3023 Asm->EOL("Landing pad");
Jim Laskeybacd3042007-02-21 22:48:45 +00003024
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003025 Asm->EmitULEB128Bytes(Actions[i]);
3026 Asm->EOL("Action");
3027 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003028 }
3029
3030 // Emit the actions.
3031 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3032 const LandingPadInfo &LandingPad = LandingPads[i];
3033 const std::vector<unsigned> &TypeIds = LandingPad.TypeIds;
3034 unsigned SizeAction = 0;
3035
Jim Laskey0102ca82007-03-01 20:26:43 +00003036 if (LandingPad.IsFilter) {
3037 Asm->EmitSLEB128Bytes(-1);
Jim Laskeybacd3042007-02-21 22:48:45 +00003038 Asm->EOL("TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00003039 Asm->EmitSLEB128Bytes(0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003040 Asm->EOL("Next action");
Jim Laskey0102ca82007-03-01 20:26:43 +00003041 } else {
3042 for (unsigned j = 0, M = TypeIds.size(); j < M; ++j) {
3043 unsigned TypeID = TypeIds[j];
3044 unsigned SizeTypeID = Asm->SizeSLEB128(TypeID);
3045 Asm->EmitSLEB128Bytes(TypeID);
3046 Asm->EOL("TypeInfo index");
3047 signed Action = j ? -(SizeAction + SizeTypeID) : 0;
3048 SizeAction = SizeTypeID + Asm->SizeSLEB128(Action);
3049 Asm->EmitSLEB128Bytes(Action);
3050 Asm->EOL("Next action");
3051 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003052 }
3053 }
3054
3055 // Emit the type ids.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003056 Asm->EmitAlignment(2);
Jim Laskeybacd3042007-02-21 22:48:45 +00003057 for (unsigned M = TypeInfos.size(); M; --M) {
3058 GlobalVariable *GV = TypeInfos[M - 1];
3059
3060 if (TAI->getAddressSize() == sizeof(int32_t))
3061 O << TAI->getData32bitsDirective();
3062 else
3063 O << TAI->getData64bitsDirective();
3064
3065 if (GV)
3066 O << Asm->getGlobalLinkName(GV);
3067 else
3068 O << "0";
3069
3070 Asm->EOL("TypeInfo");
3071 }
3072
Jim Laskey0102ca82007-03-01 20:26:43 +00003073 // Emit the filter typeinfo.
3074 if (Filter) {
3075 const std::vector<unsigned> &TypeIds = Filter->TypeIds;
3076 for (unsigned j = 0, M = TypeIds.size(); j < M; ++j) {
3077 unsigned TypeID = TypeIds[j];
3078 Asm->EmitSLEB128Bytes(TypeID);
3079 Asm->EOL("TypeInfo index");
3080 }
3081 Asm->EmitSLEB128Bytes(0);
3082 Asm->EOL("End of filter typeinfo");
3083 }
3084
3085 Asm->EmitAlignment(2);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003086 }
3087
Jim Laskey072200c2007-01-29 18:51:14 +00003088public:
3089 //===--------------------------------------------------------------------===//
3090 // Main entry points.
3091 //
3092 DwarfException(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
3093 : Dwarf(OS, A, T)
Jim Laskeybacd3042007-02-21 22:48:45 +00003094 , didInitial(false)
3095 , shouldEmit(false)
Jim Laskey072200c2007-01-29 18:51:14 +00003096 {}
3097
3098 virtual ~DwarfException() {}
3099
3100 /// SetModuleInfo - Set machine module information when it's known that pass
3101 /// manager has created it. Set by the target AsmPrinter.
3102 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00003103 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00003104 }
3105
3106 /// BeginModule - Emit all exception information that should come prior to the
3107 /// content.
3108 void BeginModule(Module *M) {
3109 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00003110 }
3111
3112 /// EndModule - Emit all exception information that should come after the
3113 /// content.
3114 void EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00003115 }
3116
3117 /// BeginFunction - Gather pre-function exception information. Assumes being
3118 /// emitted immediately after the function entry point.
3119 void BeginFunction(MachineFunction *MF) {
3120 this->MF = MF;
3121
Jim Laskey3f09fc22007-02-28 18:38:31 +00003122 if (MMI &&
3123 ExceptionHandling &&
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00003124 TAI->doesSupportExceptionHandling()) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00003125 shouldEmit = true;
3126 // Assumes in correct section after the entry point.
3127 EmitLabel("eh_func_begin", ++SubprogramCount);
3128 }
Jim Laskey072200c2007-01-29 18:51:14 +00003129 }
3130
3131 /// EndFunction - Gather and emit post-function exception information.
3132 ///
3133 void EndFunction() {
Jim Laskey3f09fc22007-02-28 18:38:31 +00003134 if (!shouldEmit) return;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003135
3136 EmitLabel("eh_func_end", SubprogramCount);
Jim Laskeybacd3042007-02-21 22:48:45 +00003137 EmitExceptionTable();
Jim Laskey3f09fc22007-02-28 18:38:31 +00003138 EmitCommonEHFrame();
Jim Laskeybacd3042007-02-21 22:48:45 +00003139 EmitEHFrame();
Jim Laskey072200c2007-01-29 18:51:14 +00003140 }
3141};
3142
Jim Laskey0d086af2006-02-27 12:43:29 +00003143} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00003144
3145//===----------------------------------------------------------------------===//
3146
Jim Laskeyd18e2892006-01-20 20:34:06 +00003147/// Emit - Print the abbreviation using the specified Dwarf writer.
3148///
Jim Laskey072200c2007-01-29 18:51:14 +00003149void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003150 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00003151 DD.getAsm()->EmitULEB128Bytes(Tag);
3152 DD.getAsm()->EOL(TagString(Tag));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003153
3154 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00003155 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3156 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003157
3158 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00003159 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003160 const DIEAbbrevData &AttrData = Data[i];
3161
3162 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00003163 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3164 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003165
3166 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00003167 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3168 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003169 }
3170
3171 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00003172 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3173 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00003174}
3175
3176#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00003177void DIEAbbrev::print(std::ostream &O) {
3178 O << "Abbreviation @"
3179 << std::hex << (intptr_t)this << std::dec
3180 << " "
3181 << TagString(Tag)
3182 << " "
3183 << ChildrenString(ChildrenFlag)
3184 << "\n";
3185
3186 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3187 O << " "
3188 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003189 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00003190 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003191 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00003192 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00003193}
Bill Wendlinge8156192006-12-07 01:30:32 +00003194void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00003195#endif
3196
3197//===----------------------------------------------------------------------===//
3198
Jim Laskeyef42a012006-11-02 20:12:39 +00003199#ifndef NDEBUG
3200void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003201 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003202}
Jim Laskeyef42a012006-11-02 20:12:39 +00003203#endif
3204
3205//===----------------------------------------------------------------------===//
3206
Jim Laskey063e7652006-01-17 17:31:53 +00003207/// EmitValue - Emit integer of appropriate size.
3208///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003209void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00003210 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00003211 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00003212 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003213 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003214 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003215 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003216 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003217 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003218 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003219 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
3220 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
3221 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003222 default: assert(0 && "DIE Value form not supported yet"); break;
3223 }
3224}
3225
3226/// SizeOf - Determine size of integer value in bytes.
3227///
Jim Laskey072200c2007-01-29 18:51:14 +00003228unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003229 switch (Form) {
3230 case DW_FORM_flag: // Fall thru
3231 case DW_FORM_ref1: // Fall thru
3232 case DW_FORM_data1: return sizeof(int8_t);
3233 case DW_FORM_ref2: // Fall thru
3234 case DW_FORM_data2: return sizeof(int16_t);
3235 case DW_FORM_ref4: // Fall thru
3236 case DW_FORM_data4: return sizeof(int32_t);
3237 case DW_FORM_ref8: // Fall thru
3238 case DW_FORM_data8: return sizeof(int64_t);
Jim Laskey072200c2007-01-29 18:51:14 +00003239 case DW_FORM_udata: return DD.getAsm()->SizeULEB128(Integer);
3240 case DW_FORM_sdata: return DD.getAsm()->SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00003241 default: assert(0 && "DIE Value form not supported yet"); break;
3242 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003243 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00003244}
3245
Jim Laskey063e7652006-01-17 17:31:53 +00003246//===----------------------------------------------------------------------===//
3247
3248/// EmitValue - Emit string value.
3249///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003250void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003251 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00003252}
3253
Jim Laskey063e7652006-01-17 17:31:53 +00003254//===----------------------------------------------------------------------===//
3255
3256/// EmitValue - Emit label value.
3257///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003258void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003259 DD.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +00003260}
3261
3262/// SizeOf - Determine size of label value in bytes.
3263///
Jim Laskey072200c2007-01-29 18:51:14 +00003264unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3265 return DD.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00003266}
Jim Laskeyef42a012006-11-02 20:12:39 +00003267
Jim Laskey063e7652006-01-17 17:31:53 +00003268//===----------------------------------------------------------------------===//
3269
Jim Laskeyd18e2892006-01-20 20:34:06 +00003270/// EmitValue - Emit label value.
3271///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003272void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003273 DD.EmitReference(Label);
Jim Laskeyd18e2892006-01-20 20:34:06 +00003274}
3275
3276/// SizeOf - Determine size of label value in bytes.
3277///
Jim Laskey072200c2007-01-29 18:51:14 +00003278unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3279 return DD.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00003280}
3281
3282//===----------------------------------------------------------------------===//
3283
Jim Laskey063e7652006-01-17 17:31:53 +00003284/// EmitValue - Emit delta value.
3285///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003286void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003287 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00003288 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00003289}
3290
3291/// SizeOf - Determine size of delta value in bytes.
3292///
Jim Laskey072200c2007-01-29 18:51:14 +00003293unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003294 if (Form == DW_FORM_data4) return 4;
Jim Laskey072200c2007-01-29 18:51:14 +00003295 return DD.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00003296}
3297
3298//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003299
Jim Laskeyb8509c52006-03-23 18:07:55 +00003300/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003301///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003302void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003303 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00003304}
Jim Laskeyd18e2892006-01-20 20:34:06 +00003305
3306//===----------------------------------------------------------------------===//
3307
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003308/// ComputeSize - calculate the size of the block.
3309///
Jim Laskey072200c2007-01-29 18:51:14 +00003310unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003311 if (!Size) {
3312 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
3313
3314 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00003315 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00003316 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003317 }
3318 return Size;
3319}
3320
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003321/// EmitValue - Emit block data.
3322///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003323void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003324 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003325 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
3326 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
3327 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
3328 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003329 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003330 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003331
3332 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
3333
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003334 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003335 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00003336 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003337 }
3338}
3339
3340/// SizeOf - Determine size of block data in bytes.
3341///
Jim Laskey072200c2007-01-29 18:51:14 +00003342unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003343 switch (Form) {
3344 case DW_FORM_block1: return Size + sizeof(int8_t);
3345 case DW_FORM_block2: return Size + sizeof(int16_t);
3346 case DW_FORM_block4: return Size + sizeof(int32_t);
Jim Laskey072200c2007-01-29 18:51:14 +00003347 case DW_FORM_block: return Size + DD.getAsm()->SizeULEB128(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003348 default: assert(0 && "Improper form for block"); break;
3349 }
3350 return 0;
3351}
3352
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003353//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003354/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00003355
3356DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003357 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00003358 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00003359}
Jim Laskeyef42a012006-11-02 20:12:39 +00003360
Jim Laskeyb8509c52006-03-23 18:07:55 +00003361/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
3362///
3363void DIE::AddSiblingOffset() {
3364 DIEInteger *DI = new DIEInteger(0);
3365 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00003366 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00003367}
3368
Jim Laskeyef42a012006-11-02 20:12:39 +00003369/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003370///
Jim Laskeyef42a012006-11-02 20:12:39 +00003371void DIE::Profile(FoldingSetNodeID &ID) {
3372 Abbrev.Profile(ID);
3373
3374 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3375 ID.AddPointer(Children[i]);
3376
3377 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3378 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003379}
Jim Laskeyef42a012006-11-02 20:12:39 +00003380
3381#ifndef NDEBUG
3382void DIE::print(std::ostream &O, unsigned IncIndent) {
3383 static unsigned IndentCount = 0;
3384 IndentCount += IncIndent;
3385 const std::string Indent(IndentCount, ' ');
3386 bool isBlock = Abbrev.getTag() == 0;
3387
3388 if (!isBlock) {
3389 O << Indent
3390 << "Die: "
3391 << "0x" << std::hex << (intptr_t)this << std::dec
3392 << ", Offset: " << Offset
3393 << ", Size: " << Size
3394 << "\n";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003395
Jim Laskeyef42a012006-11-02 20:12:39 +00003396 O << Indent
3397 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00003398 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00003399 << ChildrenString(Abbrev.getChildrenFlag());
3400 } else {
3401 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00003402 }
3403 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003404
Jim Laskeyef42a012006-11-02 20:12:39 +00003405 const std::vector<DIEAbbrevData> &Data = Abbrev.getData();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003406
Jim Laskeyef42a012006-11-02 20:12:39 +00003407 IndentCount += 2;
3408 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3409 O << Indent;
3410 if (!isBlock) {
3411 O << AttributeString(Data[i].getAttribute());
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003412 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00003413 O << "Blk[" << i << "]";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003414 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003415 O << " "
3416 << FormEncodingString(Data[i].getForm())
3417 << " ";
3418 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00003419 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00003420 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003421 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00003422
Jim Laskeyef42a012006-11-02 20:12:39 +00003423 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3424 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00003425 }
Jim Laskey063e7652006-01-17 17:31:53 +00003426
Jim Laskeyef42a012006-11-02 20:12:39 +00003427 if (!isBlock) O << "\n";
3428 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00003429}
3430
Jim Laskeyef42a012006-11-02 20:12:39 +00003431void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003432 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00003433}
Jim Laskeybd761842006-02-27 17:27:12 +00003434#endif
Jim Laskey65195462006-10-30 13:35:07 +00003435
3436//===----------------------------------------------------------------------===//
3437/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00003438///
Jim Laskey65195462006-10-30 13:35:07 +00003439
3440DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3441 const TargetAsmInfo *T) {
Jim Laskey072200c2007-01-29 18:51:14 +00003442 DE = new DwarfException(OS, A, T);
3443 DD = new DwarfDebug(OS, A, T);
Jim Laskey65195462006-10-30 13:35:07 +00003444}
3445
3446DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00003447 delete DE;
3448 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00003449}
3450
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003451/// SetModuleInfo - Set machine module info when it's known that pass manager
3452/// has created it. Set by the target AsmPrinter.
3453void DwarfWriter::SetModuleInfo(MachineModuleInfo *MMI) {
Jim Laskey072200c2007-01-29 18:51:14 +00003454 DD->SetModuleInfo(MMI);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003455 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00003456}
3457
3458/// BeginModule - Emit all Dwarf sections that should come prior to the
3459/// content.
3460void DwarfWriter::BeginModule(Module *M) {
Jim Laskey072200c2007-01-29 18:51:14 +00003461 DE->BeginModule(M);
3462 DD->BeginModule(M);
Jim Laskey65195462006-10-30 13:35:07 +00003463}
3464
3465/// EndModule - Emit all Dwarf sections that should come after the content.
3466///
3467void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00003468 DE->EndModule();
3469 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00003470}
3471
3472/// BeginFunction - Gather pre-function debug information. Assumes being
3473/// emitted immediately after the function entry point.
3474void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00003475 DE->BeginFunction(MF);
3476 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00003477}
3478
3479/// EndFunction - Gather and emit post-function debug information.
3480///
3481void DwarfWriter::EndFunction() {
Jim Laskey072200c2007-01-29 18:51:14 +00003482 DD->EndFunction();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003483 DE->EndFunction();
3484
Jim Laskey3f09fc22007-02-28 18:38:31 +00003485 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00003486 // Clear function debug information.
3487 MMI->EndFunction();
3488 }
Jim Laskey65195462006-10-30 13:35:07 +00003489}