blob: 9d8417247c8719bb6f5c56910afc2d9b74d95977 [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; }
240 const std::vector<DIEValue *> &getValues() const { return Values; }
241 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 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000318 virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const = 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 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000368 virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const;
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 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000405 virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const;
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 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000444 virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const;
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 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000482 virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const;
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 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000520 virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const;
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 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000562 virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const;
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 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000627 virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const;
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;
798
Jim Laskey072200c2007-01-29 18:51:14 +0000799 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
800 : O(OS)
801 , Asm(A)
802 , TAI(T)
803 , TD(Asm->TM.getTargetData())
804 , RI(Asm->TM.getRegisterInfo())
805 , M(NULL)
806 , MF(NULL)
807 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000808 , SubprogramCount(0)
809 {
810 }
811
812public:
813
814 //===--------------------------------------------------------------------===//
815 // Accessors.
816 //
817 AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000818 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000819 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
820
Jim Laskeyb82313f2007-02-01 16:31:34 +0000821 /// PrintLabelName - Print label name in form used by Dwarf writer.
822 ///
823 void PrintLabelName(DWLabel Label) const {
824 PrintLabelName(Label.Tag, Label.Number);
825 }
826 void PrintLabelName(const char *Tag, unsigned Number) const {
Jim Laskeybacd3042007-02-21 22:48:45 +0000827 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000828 if (Number) O << Number;
829 }
830
831 /// EmitLabel - Emit location label for internal use by Dwarf.
832 ///
833 void EmitLabel(DWLabel Label) const {
834 EmitLabel(Label.Tag, Label.Number);
835 }
836 void EmitLabel(const char *Tag, unsigned Number) const {
837 PrintLabelName(Tag, Number);
838 O << ":\n";
839 }
840
841 /// EmitReference - Emit a reference to a label.
842 ///
843 void EmitReference(DWLabel Label, bool IsPCRelative = false) const {
844 EmitReference(Label.Tag, Label.Number, IsPCRelative);
845 }
846 void EmitReference(const char *Tag, unsigned Number,
847 bool IsPCRelative = false) const {
848 if (TAI->getAddressSize() == sizeof(int32_t))
849 O << TAI->getData32bitsDirective();
850 else
851 O << TAI->getData64bitsDirective();
852
853 PrintLabelName(Tag, Number);
854
855 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
856 }
857 void EmitReference(const std::string &Name, bool IsPCRelative = false) const {
858 if (TAI->getAddressSize() == sizeof(int32_t))
859 O << TAI->getData32bitsDirective();
860 else
861 O << TAI->getData64bitsDirective();
862
863 O << Name;
864
865 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
866 }
867
868 /// EmitDifference - Emit the difference between two labels. Some
869 /// assemblers do not behave with absolute expressions with data directives,
870 /// so there is an option (needsSet) to use an intermediary set expression.
871 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
872 bool IsSmall = false) const {
873 EmitDifference(LabelHi.Tag, LabelHi.Number,
874 LabelLo.Tag, LabelLo.Number,
875 IsSmall);
876 }
877 void EmitDifference(const char *TagHi, unsigned NumberHi,
878 const char *TagLo, unsigned NumberLo,
879 bool IsSmall = false) const {
880 if (TAI->needsSet()) {
881 static unsigned SetCounter = 1;
882
883 O << "\t.set\t";
884 PrintLabelName("set", SetCounter);
885 O << ",";
886 PrintLabelName(TagHi, NumberHi);
887 O << "-";
888 PrintLabelName(TagLo, NumberLo);
889 O << "\n";
890
891 if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
892 O << TAI->getData32bitsDirective();
893 else
894 O << TAI->getData64bitsDirective();
895
896 PrintLabelName("set", SetCounter);
897
898 ++SetCounter;
899 } else {
900 if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
901 O << TAI->getData32bitsDirective();
902 else
903 O << TAI->getData64bitsDirective();
904
905 PrintLabelName(TagHi, NumberHi);
906 O << "-";
907 PrintLabelName(TagLo, NumberLo);
908 }
909 }
910
911 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
912 /// frame.
913 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
914 std::vector<MachineMove> &Moves) {
915 int stackGrowth =
916 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
917 TargetFrameInfo::StackGrowsUp ?
918 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskeybacd3042007-02-21 22:48:45 +0000919 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000920
921 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
922 MachineMove &Move = Moves[i];
923 unsigned LabelID = Move.getLabelID();
924
925 if (LabelID) {
926 LabelID = MMI->MappedLabel(LabelID);
927
928 // Throw out move if the label is invalid.
929 if (!LabelID) continue;
930 }
931
932 const MachineLocation &Dst = Move.getDestination();
933 const MachineLocation &Src = Move.getSource();
934
935 // Advance row if new location.
936 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
937 Asm->EmitInt8(DW_CFA_advance_loc4);
938 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +0000939 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
940 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +0000941
942 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +0000943 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +0000944 IsLocal = true;
945 }
946
947 // If advancing cfa.
948 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
949 if (!Src.isRegister()) {
950 if (Src.getRegister() == MachineLocation::VirtualFP) {
951 Asm->EmitInt8(DW_CFA_def_cfa_offset);
952 Asm->EOL("DW_CFA_def_cfa_offset");
953 } else {
954 Asm->EmitInt8(DW_CFA_def_cfa);
955 Asm->EOL("DW_CFA_def_cfa");
956 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
957 Asm->EOL("Register");
958 }
959
960 int Offset = -Src.getOffset();
961
962 Asm->EmitULEB128Bytes(Offset);
963 Asm->EOL("Offset");
964 } else {
965 assert(0 && "Machine move no supported yet.");
966 }
967 } else if (Src.isRegister() &&
968 Src.getRegister() == MachineLocation::VirtualFP) {
969 if (Dst.isRegister()) {
970 Asm->EmitInt8(DW_CFA_def_cfa_register);
971 Asm->EOL("DW_CFA_def_cfa_register");
972 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getRegister()));
973 Asm->EOL("Register");
974 } else {
975 assert(0 && "Machine move no supported yet.");
976 }
977 } else {
978 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
979 int Offset = Dst.getOffset() / stackGrowth;
980
981 if (Offset < 0) {
982 Asm->EmitInt8(DW_CFA_offset_extended_sf);
983 Asm->EOL("DW_CFA_offset_extended_sf");
984 Asm->EmitULEB128Bytes(Reg);
985 Asm->EOL("Reg");
986 Asm->EmitSLEB128Bytes(Offset);
987 Asm->EOL("Offset");
988 } else if (Reg < 64) {
989 Asm->EmitInt8(DW_CFA_offset + Reg);
990 Asm->EOL("DW_CFA_offset + Reg");
991 Asm->EmitULEB128Bytes(Offset);
992 Asm->EOL("Offset");
993 } else {
994 Asm->EmitInt8(DW_CFA_offset_extended);
995 Asm->EOL("DW_CFA_offset_extended");
996 Asm->EmitULEB128Bytes(Reg);
997 Asm->EOL("Reg");
998 Asm->EmitULEB128Bytes(Offset);
999 Asm->EOL("Offset");
1000 }
1001 }
1002 }
1003 }
1004
Jim Laskey072200c2007-01-29 18:51:14 +00001005};
1006
1007//===----------------------------------------------------------------------===//
1008/// DwarfDebug - Emits Dwarf debug directives.
1009///
1010class DwarfDebug : public Dwarf {
1011
1012private:
Jim Laskey65195462006-10-30 13:35:07 +00001013 //===--------------------------------------------------------------------===//
1014 // Attributes used to construct specific Dwarf sections.
1015 //
1016
1017 /// CompileUnits - All the compile units involved in this build. The index
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001018 /// of each entry in this vector corresponds to the sources in MMI.
Jim Laskey65195462006-10-30 13:35:07 +00001019 std::vector<CompileUnit *> CompileUnits;
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001020
Jim Laskeyef42a012006-11-02 20:12:39 +00001021 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001022 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001023 FoldingSet<DIEAbbrev> AbbreviationsSet;
1024
1025 /// Abbreviations - A list of all the unique abbreviations in use.
1026 ///
1027 std::vector<DIEAbbrev *> Abbreviations;
Jim Laskey65195462006-10-30 13:35:07 +00001028
Jim Laskeyef42a012006-11-02 20:12:39 +00001029 /// ValuesSet - Used to uniquely define values.
1030 ///
1031 FoldingSet<DIEValue> ValuesSet;
1032
1033 /// Values - A list of all the unique values in use.
1034 ///
1035 std::vector<DIEValue *> Values;
1036
Jim Laskey65195462006-10-30 13:35:07 +00001037 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001038 ///
Jim Laskey65195462006-10-30 13:35:07 +00001039 UniqueVector<std::string> StringPool;
1040
1041 /// UnitMap - Map debug information descriptor to compile unit.
1042 ///
1043 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
1044
Jim Laskey65195462006-10-30 13:35:07 +00001045 /// SectionMap - Provides a unique id per text section.
1046 ///
1047 UniqueVector<std::string> SectionMap;
1048
1049 /// SectionSourceLines - Tracks line numbers per text section.
1050 ///
1051 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1052
Jim Laskeybacd3042007-02-21 22:48:45 +00001053 /// didInitial - Flag to indicate if initial emission has been done.
1054 ///
1055 bool didInitial;
1056
1057 /// shouldEmit - Flag to indicate if debug information should be emitted.
1058 ///
1059 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001060
1061public:
Jim Laskeybacd3042007-02-21 22:48:45 +00001062
1063 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1064 ///
1065 bool ShouldEmitDwarf() const { return shouldEmit; }
Jim Laskey65195462006-10-30 13:35:07 +00001066
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001067 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Jim Laskey65195462006-10-30 13:35:07 +00001068 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001069 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1070 // Profile the node so that we can make it unique.
1071 FoldingSetNodeID ID;
1072 Abbrev.Profile(ID);
1073
1074 // Check the set for priors.
1075 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
1076
1077 // If it's newly added.
1078 if (InSet == &Abbrev) {
1079 // Add to abbreviation list.
1080 Abbreviations.push_back(&Abbrev);
1081 // Assign the vector position + 1 as its number.
1082 Abbrev.setNumber(Abbreviations.size());
1083 } else {
1084 // Assign existing abbreviation number.
1085 Abbrev.setNumber(InSet->getNumber());
1086 }
1087 }
1088
Jim Laskey65195462006-10-30 13:35:07 +00001089 /// NewString - Add a string to the constant pool and returns a label.
1090 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001091 DWLabel NewString(const std::string &String) {
1092 unsigned StringID = StringPool.insert(String);
1093 return DWLabel("string", StringID);
1094 }
Jim Laskey65195462006-10-30 13:35:07 +00001095
Jim Laskeyef42a012006-11-02 20:12:39 +00001096 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1097 /// entry.
1098 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1099 DIEntry *Value;
1100
1101 if (Entry) {
1102 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001103 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001104 void *Where;
1105 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
1106
Jim Laskeyf6733882006-11-02 21:48:18 +00001107 if (Value) return Value;
Jim Laskeyef42a012006-11-02 20:12:39 +00001108
1109 Value = new DIEntry(Entry);
1110 ValuesSet.InsertNode(Value, Where);
1111 } else {
1112 Value = new DIEntry(Entry);
1113 }
1114
1115 Values.push_back(Value);
1116 return Value;
1117 }
1118
1119 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1120 ///
1121 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1122 Value->Entry = Entry;
1123 // Add to values set if not already there. If it is, we merely have a
1124 // duplicate in the values list (no harm.)
1125 ValuesSet.GetOrInsertNode(Value);
1126 }
1127
1128 /// AddUInt - Add an unsigned integer attribute data and value.
1129 ///
1130 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1131 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1132
1133 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001134 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001135 void *Where;
1136 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1137 if (!Value) {
1138 Value = new DIEInteger(Integer);
1139 ValuesSet.InsertNode(Value, Where);
1140 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001141 }
1142
1143 Die->AddValue(Attribute, Form, Value);
1144 }
1145
1146 /// AddSInt - Add an signed integer attribute data and value.
1147 ///
1148 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1149 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1150
1151 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001152 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001153 void *Where;
1154 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1155 if (!Value) {
1156 Value = new DIEInteger(Integer);
1157 ValuesSet.InsertNode(Value, Where);
1158 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001159 }
1160
1161 Die->AddValue(Attribute, Form, Value);
1162 }
1163
1164 /// AddString - Add a std::string attribute data and value.
1165 ///
1166 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1167 const std::string &String) {
1168 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001169 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001170 void *Where;
1171 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1172 if (!Value) {
1173 Value = new DIEString(String);
1174 ValuesSet.InsertNode(Value, Where);
1175 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001176 }
1177
1178 Die->AddValue(Attribute, Form, Value);
1179 }
1180
1181 /// AddLabel - Add a Dwarf label attribute data and value.
1182 ///
1183 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1184 const DWLabel &Label) {
1185 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001186 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001187 void *Where;
1188 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1189 if (!Value) {
1190 Value = new DIEDwarfLabel(Label);
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 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1199 ///
1200 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1201 const std::string &Label) {
1202 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001203 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001204 void *Where;
1205 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1206 if (!Value) {
1207 Value = new DIEObjectLabel(Label);
1208 ValuesSet.InsertNode(Value, Where);
1209 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001210 }
1211
1212 Die->AddValue(Attribute, Form, Value);
1213 }
1214
1215 /// AddDelta - Add a label delta attribute data and value.
1216 ///
1217 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1218 const DWLabel &Hi, const DWLabel &Lo) {
1219 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001220 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001221 void *Where;
1222 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1223 if (!Value) {
1224 Value = new DIEDelta(Hi, Lo);
1225 ValuesSet.InsertNode(Value, Where);
1226 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001227 }
1228
1229 Die->AddValue(Attribute, Form, Value);
1230 }
1231
1232 /// AddDIEntry - Add a DIE attribute data and value.
1233 ///
1234 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1235 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1236 }
1237
1238 /// AddBlock - Add block data.
1239 ///
1240 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1241 Block->ComputeSize(*this);
1242 FoldingSetNodeID ID;
1243 Block->Profile(ID);
1244 void *Where;
1245 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1246 if (!Value) {
1247 Value = Block;
1248 ValuesSet.InsertNode(Value, Where);
1249 Values.push_back(Value);
1250 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001251 delete Block;
1252 }
1253
1254 Die->AddValue(Attribute, Block->BestForm(), Value);
1255 }
1256
Jim Laskey65195462006-10-30 13:35:07 +00001257private:
1258
1259 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001260 /// entry.
1261 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1262 if (File && Line) {
1263 CompileUnit *FileUnit = FindCompileUnit(File);
1264 unsigned FileID = FileUnit->getID();
1265 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1266 AddUInt(Die, DW_AT_decl_line, 0, Line);
1267 }
1268 }
Jim Laskey65195462006-10-30 13:35:07 +00001269
1270 /// AddAddress - Add an address attribute to a die based on the location
1271 /// provided.
1272 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001273 const MachineLocation &Location) {
1274 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1275 DIEBlock *Block = new DIEBlock();
1276
1277 if (Location.isRegister()) {
1278 if (Reg < 32) {
1279 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1280 } else {
1281 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1282 AddUInt(Block, 0, DW_FORM_udata, Reg);
1283 }
1284 } else {
1285 if (Reg < 32) {
1286 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1287 } else {
1288 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1289 AddUInt(Block, 0, DW_FORM_udata, Reg);
1290 }
1291 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1292 }
1293
1294 AddBlock(Die, Attribute, 0, Block);
1295 }
1296
1297 /// AddBasicType - Add a new basic type attribute to the specified entity.
1298 ///
1299 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1300 const std::string &Name,
1301 unsigned Encoding, unsigned Size) {
1302 DIE *Die = ConstructBasicType(Unit, Name, Encoding, Size);
1303 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1304 }
1305
1306 /// ConstructBasicType - Construct a new basic type.
1307 ///
1308 DIE *ConstructBasicType(CompileUnit *Unit,
1309 const std::string &Name,
1310 unsigned Encoding, unsigned Size) {
1311 DIE Buffer(DW_TAG_base_type);
1312 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1313 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1314 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1315 return Unit->AddDie(Buffer);
1316 }
1317
1318 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1319 ///
1320 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1321 DIE *Die = ConstructPointerType(Unit, Name);
1322 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1323 }
1324
1325 /// ConstructPointerType - Construct a new pointer type.
1326 ///
1327 DIE *ConstructPointerType(CompileUnit *Unit, const std::string &Name) {
1328 DIE Buffer(DW_TAG_pointer_type);
1329 AddUInt(&Buffer, DW_AT_byte_size, 0, TAI->getAddressSize());
1330 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1331 return Unit->AddDie(Buffer);
1332 }
1333
1334 /// AddType - Add a new type attribute to the specified entity.
1335 ///
1336 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1337 if (!TyDesc) {
Jim Laskey2b935d52007-01-26 14:19:17 +00001338 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001339 } else {
1340 // Check for pre-existence.
1341 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
1342
1343 // If it exists then use the existing value.
1344 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001345 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1346 return;
1347 }
1348
1349 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1350 // FIXME - Not sure why programs and variables are coming through here.
1351 // Short cut for handling subprogram types (not really a TyDesc.)
1352 AddPointerType(Entity, Unit, SubprogramTy->getName());
1353 } else if (GlobalVariableDesc *GlobalTy =
1354 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1355 // FIXME - Not sure why programs and variables are coming through here.
1356 // Short cut for handling global variable types (not really a TyDesc.)
1357 AddPointerType(Entity, Unit, GlobalTy->getName());
1358 } else {
1359 // Set up proxy.
1360 Slot = NewDIEntry();
1361
1362 // Construct type.
1363 DIE Buffer(DW_TAG_base_type);
1364 ConstructType(Buffer, TyDesc, Unit);
1365
1366 // Add debug information entry to entity and unit.
1367 DIE *Die = Unit->AddDie(Buffer);
1368 SetDIEntry(Slot, Die);
1369 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1370 }
1371 }
1372 }
1373
1374 /// ConstructType - Adds all the required attributes to the type.
1375 ///
1376 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1377 // Get core information.
1378 const std::string &Name = TyDesc->getName();
1379 uint64_t Size = TyDesc->getSize() >> 3;
1380
1381 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1382 // Fundamental types like int, float, bool
1383 Buffer.setTag(DW_TAG_base_type);
1384 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1385 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00001386 // Fetch tag.
1387 unsigned Tag = DerivedTy->getTag();
1388 // FIXME - Workaround for templates.
1389 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1390 // Pointers, typedefs et al.
1391 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00001392 // Map to main type, void will not have a type.
1393 if (TypeDesc *FromTy = DerivedTy->getFromType())
1394 AddType(&Buffer, FromTy, Unit);
1395 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1396 // Fetch tag.
1397 unsigned Tag = CompTy->getTag();
1398
1399 // Set tag accordingly.
1400 if (Tag == DW_TAG_vector_type)
1401 Buffer.setTag(DW_TAG_array_type);
1402 else
1403 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00001404
Jim Laskeyef42a012006-11-02 20:12:39 +00001405 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1406
1407 switch (Tag) {
1408 case DW_TAG_vector_type:
1409 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1410 // Fall thru
1411 case DW_TAG_array_type: {
1412 // Add element type.
1413 if (TypeDesc *FromTy = CompTy->getFromType())
1414 AddType(&Buffer, FromTy, Unit);
1415
1416 // Don't emit size attribute.
1417 Size = 0;
1418
1419 // Construct an anonymous type for index type.
Jim Laskey2b935d52007-01-26 14:19:17 +00001420 DIE *IndexTy = ConstructBasicType(Unit, "", DW_ATE_signed,
1421 sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001422
1423 // Add subranges to array type.
1424 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1425 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1426 int64_t Lo = SRD->getLo();
1427 int64_t Hi = SRD->getHi();
1428 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1429
1430 // If a range is available.
1431 if (Lo != Hi) {
1432 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1433 // Only add low if non-zero.
1434 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1435 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1436 }
1437
1438 Buffer.AddChild(Subrange);
1439 }
1440 break;
1441 }
1442 case DW_TAG_structure_type:
1443 case DW_TAG_union_type: {
1444 // Add elements to structure type.
1445 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1446 DebugInfoDesc *Element = Elements[i];
1447
1448 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1449 // Add field or base class.
1450
1451 unsigned Tag = MemberDesc->getTag();
1452
1453 // Extract the basic information.
1454 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001455 uint64_t Size = MemberDesc->getSize();
1456 uint64_t Align = MemberDesc->getAlign();
1457 uint64_t Offset = MemberDesc->getOffset();
1458
1459 // Construct member debug information entry.
1460 DIE *Member = new DIE(Tag);
1461
1462 // Add name if not "".
1463 if (!Name.empty())
1464 AddString(Member, DW_AT_name, DW_FORM_string, Name);
1465 // Add location if available.
1466 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1467
1468 // Most of the time the field info is the same as the members.
1469 uint64_t FieldSize = Size;
1470 uint64_t FieldAlign = Align;
1471 uint64_t FieldOffset = Offset;
1472
Jim Laskeyee5f9272006-12-22 20:03:42 +00001473 // Set the member type.
1474 TypeDesc *FromTy = MemberDesc->getFromType();
1475 AddType(Member, FromTy, Unit);
1476
1477 // Walk up typedefs until a real size is found.
1478 while (FromTy) {
1479 if (FromTy->getTag() != DW_TAG_typedef) {
1480 FieldSize = FromTy->getSize();
1481 FieldAlign = FromTy->getSize();
1482 break;
1483 }
1484
1485 FromTy = dyn_cast<DerivedTypeDesc>(FromTy)->getFromType();
Jim Laskeyef42a012006-11-02 20:12:39 +00001486 }
1487
1488 // Unless we have a bit field.
1489 if (Tag == DW_TAG_member && FieldSize != Size) {
1490 // Construct the alignment mask.
1491 uint64_t AlignMask = ~(FieldAlign - 1);
1492 // Determine the high bit + 1 of the declared size.
1493 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1494 // Work backwards to determine the base offset of the field.
1495 FieldOffset = HiMark - FieldSize;
1496 // Now normalize offset to the field.
1497 Offset -= FieldOffset;
1498
1499 // Maybe we need to work from the other end.
1500 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1501
1502 // Add size and offset.
1503 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1504 AddUInt(Member, DW_AT_bit_size, 0, Size);
1505 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1506 }
1507
1508 // Add computation for offset.
1509 DIEBlock *Block = new DIEBlock();
1510 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1511 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1512 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1513
1514 // Add accessibility (public default unless is base class.
1515 if (MemberDesc->isProtected()) {
1516 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1517 } else if (MemberDesc->isPrivate()) {
1518 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1519 } else if (Tag == DW_TAG_inheritance) {
1520 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1521 }
1522
1523 Buffer.AddChild(Member);
1524 } else if (GlobalVariableDesc *StaticDesc =
1525 dyn_cast<GlobalVariableDesc>(Element)) {
1526 // Add static member.
1527
1528 // Construct member debug information entry.
1529 DIE *Static = new DIE(DW_TAG_variable);
1530
1531 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001532 const std::string &Name = StaticDesc->getName();
1533 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001534 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001535 if (!LinkageName.empty()) {
1536 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1537 LinkageName);
1538 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001539
1540 // Add location.
1541 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1542
1543 // Add type.
1544 if (TypeDesc *StaticTy = StaticDesc->getType())
1545 AddType(Static, StaticTy, Unit);
1546
1547 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001548 if (!StaticDesc->isStatic())
1549 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001550 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
1551
1552 Buffer.AddChild(Static);
1553 } else if (SubprogramDesc *MethodDesc =
1554 dyn_cast<SubprogramDesc>(Element)) {
1555 // Add member function.
1556
1557 // Construct member debug information entry.
1558 DIE *Method = new DIE(DW_TAG_subprogram);
1559
1560 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001561 const std::string &Name = MethodDesc->getName();
1562 const std::string &LinkageName = MethodDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001563
Jim Laskey2172f962006-11-30 14:35:45 +00001564 AddString(Method, DW_AT_name, DW_FORM_string, Name);
1565 bool IsCTor = TyDesc->getName() == Name;
1566
1567 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001568 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001569 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001570 }
1571
1572 // Add location.
1573 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1574
1575 // Add type.
1576 if (CompositeTypeDesc *MethodTy =
1577 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1578 // Get argument information.
1579 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1580
1581 // If not a ctor.
1582 if (!IsCTor) {
1583 // Add return type.
1584 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1585 }
1586
1587 // Add arguments.
1588 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1589 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1590 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1591 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1592 Method->AddChild(Arg);
1593 }
1594 }
1595
1596 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001597 if (!MethodDesc->isStatic())
1598 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001599 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
1600
1601 Buffer.AddChild(Method);
1602 }
1603 }
1604 break;
1605 }
1606 case DW_TAG_enumeration_type: {
1607 // Add enumerators to enumeration type.
1608 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1609 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1610 const std::string &Name = ED->getName();
1611 int64_t Value = ED->getValue();
1612 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1613 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1614 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1615 Buffer.AddChild(Enumerator);
1616 }
1617
1618 break;
1619 }
1620 case DW_TAG_subroutine_type: {
1621 // Add prototype flag.
1622 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1623 // Add return type.
1624 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
1625
1626 // Add arguments.
1627 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1628 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1629 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1630 Buffer.AddChild(Arg);
1631 }
1632
1633 break;
1634 }
1635 default: break;
1636 }
1637 }
1638
1639 // Add size if non-zero (derived types don't have a size.)
1640 if (Size) AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1641 // Add name if not anonymous or intermediate type.
1642 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1643 // Add source line info if available.
1644 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
1645 }
1646
1647 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00001648 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001649 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1650 // Construct debug information entry.
1651 DIE *Die = new DIE(DW_TAG_compile_unit);
1652 AddDelta(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0),
1653 DWLabel("section_line", 0));
1654 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1655 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1656 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1657 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1658
1659 // Construct compile unit.
1660 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1661
1662 // Add Unit to compile unit map.
1663 DescToUnitMap[UnitDesc] = Unit;
1664
1665 return Unit;
1666 }
1667
Jim Laskey9d4209f2006-11-07 19:33:46 +00001668 /// GetBaseCompileUnit - Get the main compile unit.
1669 ///
1670 CompileUnit *GetBaseCompileUnit() const {
1671 CompileUnit *Unit = CompileUnits[0];
1672 assert(Unit && "Missing compile unit.");
1673 return Unit;
1674 }
1675
Jim Laskey65195462006-10-30 13:35:07 +00001676 /// FindCompileUnit - Get the compile unit for the given descriptor.
1677 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001678 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001679 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00001680 assert(Unit && "Missing compile unit.");
1681 return Unit;
1682 }
1683
1684 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00001685 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001686 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1687 // Get the compile unit context.
1688 CompileUnitDesc *UnitDesc =
1689 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001690 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001691
1692 // Check for pre-existence.
1693 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1694 if (Slot) return Slot;
1695
1696 // Get the global variable itself.
1697 GlobalVariable *GV = GVD->getGlobalVariable();
1698
Jim Laskey2172f962006-11-30 14:35:45 +00001699 const std::string &Name = GVD->getName();
1700 const std::string &FullName = GVD->getFullName();
1701 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001702 // Create the global's variable DIE.
1703 DIE *VariableDie = new DIE(DW_TAG_variable);
1704 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001705 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001706 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001707 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001708 }
Jim Laskey6488a072007-01-08 22:15:18 +00001709 AddType(VariableDie, GVD->getType(), Unit);
1710 if (!GVD->isStatic())
1711 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001712
1713 // Add source line info if available.
1714 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1715
Jim Laskeyef42a012006-11-02 20:12:39 +00001716 // Add address.
1717 DIEBlock *Block = new DIEBlock();
1718 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00001719 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
1720 AddBlock(VariableDie, DW_AT_location, 0, Block);
Jim Laskeyef42a012006-11-02 20:12:39 +00001721
1722 // Add to map.
1723 Slot = VariableDie;
1724
1725 // Add to context owner.
1726 Unit->getDie()->AddChild(VariableDie);
1727
1728 // Expose as global.
1729 // FIXME - need to check external flag.
Jim Laskey2172f962006-11-30 14:35:45 +00001730 Unit->AddGlobal(FullName, VariableDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001731
1732 return VariableDie;
1733 }
Jim Laskey65195462006-10-30 13:35:07 +00001734
1735 /// NewSubprogram - Add a new subprogram DIE.
1736 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001737 DIE *NewSubprogram(SubprogramDesc *SPD) {
1738 // Get the compile unit context.
1739 CompileUnitDesc *UnitDesc =
1740 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001741 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001742
1743 // Check for pre-existence.
1744 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1745 if (Slot) return Slot;
1746
1747 // Gather the details (simplify add attribute code.)
Jim Laskey2172f962006-11-30 14:35:45 +00001748 const std::string &Name = SPD->getName();
1749 const std::string &FullName = SPD->getFullName();
1750 const std::string &LinkageName = SPD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001751
1752 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1753 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001754 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001755 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001756 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001757 }
1758 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
Jim Laskey6488a072007-01-08 22:15:18 +00001759 if (!SPD->isStatic())
1760 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001761 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
1762
1763 // Add source line info if available.
1764 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1765
1766 // Add to map.
1767 Slot = SubprogramDie;
1768
1769 // Add to context owner.
1770 Unit->getDie()->AddChild(SubprogramDie);
1771
1772 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00001773 Unit->AddGlobal(FullName, SubprogramDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001774
1775 return SubprogramDie;
1776 }
Jim Laskey65195462006-10-30 13:35:07 +00001777
1778 /// NewScopeVariable - Create a new scope variable.
1779 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001780 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1781 // Get the descriptor.
1782 VariableDesc *VD = DV->getDesc();
1783
1784 // Translate tag to proper Dwarf tag. The result variable is dropped for
1785 // now.
1786 unsigned Tag;
1787 switch (VD->getTag()) {
1788 case DW_TAG_return_variable: return NULL;
1789 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1790 case DW_TAG_auto_variable: // fall thru
1791 default: Tag = DW_TAG_variable; break;
1792 }
1793
1794 // Define variable debug information entry.
1795 DIE *VariableDie = new DIE(Tag);
1796 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1797
1798 // Add source line info if available.
1799 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1800
1801 // Add variable type.
1802 AddType(VariableDie, VD->getType(), Unit);
1803
1804 // Add variable address.
1805 MachineLocation Location;
1806 RI->getLocation(*MF, DV->getFrameIndex(), Location);
1807 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001808
Jim Laskeyef42a012006-11-02 20:12:39 +00001809 return VariableDie;
1810 }
Jim Laskey65195462006-10-30 13:35:07 +00001811
1812 /// ConstructScope - Construct the components of a scope.
1813 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001814 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00001815 unsigned ParentStartID, unsigned ParentEndID,
1816 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001817 // Add variables to scope.
1818 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1819 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1820 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1821 if (VariableDie) ParentDie->AddChild(VariableDie);
1822 }
1823
1824 // Add nested scopes.
1825 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1826 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1827 // Define the Scope debug information entry.
1828 DebugScope *Scope = Scopes[j];
1829 // FIXME - Ignore inlined functions for the time being.
1830 if (!Scope->getParent()) continue;
1831
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001832 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1833 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00001834
Jim Laskey9d4209f2006-11-07 19:33:46 +00001835 // Ignore empty scopes.
1836 if (StartID == EndID && StartID != 0) continue;
1837 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00001838
Jim Laskey36729dd2006-11-29 16:55:57 +00001839 if (StartID == ParentStartID && EndID == ParentEndID) {
1840 // Just add stuff to the parent scope.
1841 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001842 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00001843 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1844
1845 // Add the scope bounds.
1846 if (StartID) {
1847 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00001848 DWLabel("label", StartID));
Jim Laskey36729dd2006-11-29 16:55:57 +00001849 } else {
1850 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1851 DWLabel("func_begin", SubprogramCount));
1852 }
1853 if (EndID) {
1854 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00001855 DWLabel("label", EndID));
Jim Laskey36729dd2006-11-29 16:55:57 +00001856 } else {
1857 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1858 DWLabel("func_end", SubprogramCount));
1859 }
1860
1861 // Add the scope contents.
1862 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
1863 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001864 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001865 }
1866 }
Jim Laskey65195462006-10-30 13:35:07 +00001867
1868 /// ConstructRootScope - Construct the scope for the subprogram.
1869 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001870 void ConstructRootScope(DebugScope *RootScope) {
1871 // Exit if there is no root scope.
1872 if (!RootScope) return;
1873
1874 // Get the subprogram debug information entry.
1875 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1876
1877 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00001878 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001879
1880 // Get the subprogram die.
1881 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
1882 assert(SPDie && "Missing subprogram descriptor");
1883
1884 // Add the function bounds.
1885 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1886 DWLabel("func_begin", SubprogramCount));
1887 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1888 DWLabel("func_end", SubprogramCount));
1889 MachineLocation Location(RI->getFrameRegister(*MF));
1890 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001891
Jim Laskey36729dd2006-11-29 16:55:57 +00001892 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001893 }
Jim Laskey65195462006-10-30 13:35:07 +00001894
Jim Laskeyef42a012006-11-02 20:12:39 +00001895 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1896 /// tools to recognize the object file contains Dwarf information.
1897 void EmitInitial() {
1898 // Check to see if we already emitted intial headers.
1899 if (didInitial) return;
1900 didInitial = true;
1901
1902 // Dwarf sections base addresses.
1903 if (TAI->getDwarfRequiresFrameSection()) {
1904 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1905 EmitLabel("section_frame", 0);
1906 }
1907 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
1908 EmitLabel("section_info", 0);
1909 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
1910 EmitLabel("section_abbrev", 0);
1911 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
1912 EmitLabel("section_aranges", 0);
1913 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
1914 EmitLabel("section_macinfo", 0);
1915 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
1916 EmitLabel("section_line", 0);
1917 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
1918 EmitLabel("section_loc", 0);
1919 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
1920 EmitLabel("section_pubnames", 0);
1921 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
1922 EmitLabel("section_str", 0);
1923 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
1924 EmitLabel("section_ranges", 0);
1925
1926 Asm->SwitchToTextSection(TAI->getTextSection());
1927 EmitLabel("text_begin", 0);
1928 Asm->SwitchToDataSection(TAI->getDataSection());
1929 EmitLabel("data_begin", 0);
1930
1931 // Emit common frame information.
1932 EmitInitialDebugFrame();
1933 }
1934
Jim Laskey65195462006-10-30 13:35:07 +00001935 /// EmitDIE - Recusively Emits a debug information entry.
1936 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001937 void EmitDIE(DIE *Die) const {
1938 // Get the abbreviation for this DIE.
1939 unsigned AbbrevNumber = Die->getAbbrevNumber();
1940 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1941
Jim Laskeybacd3042007-02-21 22:48:45 +00001942 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00001943
1944 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001945 Asm->EmitULEB128Bytes(AbbrevNumber);
1946 Asm->EOL(std::string("Abbrev [" +
1947 utostr(AbbrevNumber) +
1948 "] 0x" + utohexstr(Die->getOffset()) +
1949 ":0x" + utohexstr(Die->getSize()) + " " +
1950 TagString(Abbrev->getTag())));
Jim Laskeyef42a012006-11-02 20:12:39 +00001951
1952 const std::vector<DIEValue *> &Values = Die->getValues();
1953 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
1954
1955 // Emit the DIE attribute values.
1956 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1957 unsigned Attr = AbbrevData[i].getAttribute();
1958 unsigned Form = AbbrevData[i].getForm();
1959 assert(Form && "Too many attributes for DIE (check abbreviation)");
1960
1961 switch (Attr) {
1962 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001963 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00001964 break;
1965 }
1966 default: {
1967 // Emit an attribute using the defined form.
1968 Values[i]->EmitValue(*this, Form);
1969 break;
1970 }
1971 }
1972
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001973 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00001974 }
1975
1976 // Emit the DIE children if any.
1977 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
1978 const std::vector<DIE *> &Children = Die->getChildren();
1979
1980 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1981 EmitDIE(Children[j]);
1982 }
1983
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001984 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00001985 }
1986 }
1987
Jim Laskey65195462006-10-30 13:35:07 +00001988 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
1989 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001990 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1991 // Get the children.
1992 const std::vector<DIE *> &Children = Die->getChildren();
1993
1994 // If not last sibling and has children then add sibling offset attribute.
1995 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1996
1997 // Record the abbreviation.
1998 AssignAbbrevNumber(Die->getAbbrev());
1999
2000 // Get the abbreviation for this DIE.
2001 unsigned AbbrevNumber = Die->getAbbrevNumber();
2002 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2003
2004 // Set DIE offset
2005 Die->setOffset(Offset);
2006
2007 // Start the size with the size of abbreviation code.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002008 Offset += Asm->SizeULEB128(AbbrevNumber);
Jim Laskeyef42a012006-11-02 20:12:39 +00002009
2010 const std::vector<DIEValue *> &Values = Die->getValues();
2011 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2012
2013 // Size the DIE attribute values.
2014 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2015 // Size attribute value.
2016 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2017 }
2018
2019 // Size the DIE children if any.
2020 if (!Children.empty()) {
2021 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2022 "Children flag not set");
2023
2024 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2025 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2026 }
2027
2028 // End of children marker.
2029 Offset += sizeof(int8_t);
2030 }
2031
2032 Die->setSize(Offset - Die->getOffset());
2033 return Offset;
2034 }
Jim Laskey65195462006-10-30 13:35:07 +00002035
2036 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2037 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002038 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002039 // Process base compile unit.
2040 CompileUnit *Unit = GetBaseCompileUnit();
2041 // Compute size of compile unit header
2042 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2043 sizeof(int16_t) + // DWARF version number
2044 sizeof(int32_t) + // Offset Into Abbrev. Section
2045 sizeof(int8_t); // Pointer Size (in bytes)
2046 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002047 }
2048
Jim Laskey65195462006-10-30 13:35:07 +00002049 /// EmitDebugInfo - Emit the debug info section.
2050 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002051 void EmitDebugInfo() const {
2052 // Start debug info section.
2053 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2054
Jim Laskey5496f012006-11-09 14:52:14 +00002055 CompileUnit *Unit = GetBaseCompileUnit();
2056 DIE *Die = Unit->getDie();
2057 // Emit the compile units header.
2058 EmitLabel("info_begin", Unit->getID());
2059 // Emit size of content not including length itself
2060 unsigned ContentSize = Die->getSize() +
2061 sizeof(int16_t) + // DWARF version number
2062 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00002063 sizeof(int8_t) + // Pointer Size (in bytes)
2064 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Laskey5496f012006-11-09 14:52:14 +00002065
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002066 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2067 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002068 EmitDifference("abbrev_begin", 0, "section_abbrev", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002069 Asm->EOL("Offset Into Abbrev. Section");
2070 Asm->EmitInt8(TAI->getAddressSize()); Asm->EOL("Address Size (in bytes)");
Jim Laskey5496f012006-11-09 14:52:14 +00002071
2072 EmitDIE(Die);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002073 // FIXME - extra padding for gdb bug.
2074 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2075 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2076 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2077 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Jim Laskey5496f012006-11-09 14:52:14 +00002078 EmitLabel("info_end", Unit->getID());
2079
Jim Laskeybacd3042007-02-21 22:48:45 +00002080 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002081 }
2082
Jim Laskey65195462006-10-30 13:35:07 +00002083 /// EmitAbbreviations - Emit the abbreviation section.
2084 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002085 void EmitAbbreviations() const {
2086 // Check to see if it is worth the effort.
2087 if (!Abbreviations.empty()) {
2088 // Start the debug abbrev section.
2089 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2090
2091 EmitLabel("abbrev_begin", 0);
2092
2093 // For each abbrevation.
2094 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2095 // Get abbreviation data
2096 const DIEAbbrev *Abbrev = Abbreviations[i];
2097
2098 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002099 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2100 Asm->EOL("Abbreviation Code");
Jim Laskeyef42a012006-11-02 20:12:39 +00002101
2102 // Emit the abbreviations data.
2103 Abbrev->Emit(*this);
2104
Jim Laskeybacd3042007-02-21 22:48:45 +00002105 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002106 }
2107
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002108 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002109 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002110
Jim Laskeyef42a012006-11-02 20:12:39 +00002111 EmitLabel("abbrev_end", 0);
2112
Jim Laskeybacd3042007-02-21 22:48:45 +00002113 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002114 }
2115 }
2116
Jim Laskey65195462006-10-30 13:35:07 +00002117 /// EmitDebugLines - Emit source line information.
2118 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002119 void EmitDebugLines() const {
2120 // Minimum line delta, thus ranging from -10..(255-10).
2121 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2122 // Maximum line delta, thus ranging from -10..(255-10).
2123 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002124
Jim Laskeyef42a012006-11-02 20:12:39 +00002125 // Start the dwarf line section.
2126 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2127
2128 // Construct the section header.
2129
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002130 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002131 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002132 EmitLabel("line_begin", 0);
2133
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002134 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Jim Laskeyef42a012006-11-02 20:12:39 +00002135
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002136 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002137 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002138 EmitLabel("line_prolog_begin", 0);
2139
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002140 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002141
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002142 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002143
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002144 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002145
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002146 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002147
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002148 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Jim Laskeyef42a012006-11-02 20:12:39 +00002149
2150 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002151 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2152 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2153 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2154 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2155 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2156 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2157 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2158 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2159 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002160
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002161 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Jim Laskeyef42a012006-11-02 20:12:39 +00002162 const UniqueVector<SourceFileInfo>
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002163 &SourceFiles = MMI->getSourceFiles();
Jim Laskeyef42a012006-11-02 20:12:39 +00002164
2165 // Emit directories.
2166 for (unsigned DirectoryID = 1, NDID = Directories.size();
2167 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002168 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002169 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002170 Asm->EmitInt8(0); Asm->EOL("End of directories");
Jim Laskeyef42a012006-11-02 20:12:39 +00002171
2172 // Emit files.
2173 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2174 SourceID <= NSID; ++SourceID) {
2175 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002176 Asm->EmitString(SourceFile.getName());
2177 Asm->EOL("Source");
2178 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2179 Asm->EOL("Directory #");
2180 Asm->EmitULEB128Bytes(0);
2181 Asm->EOL("Mod date");
2182 Asm->EmitULEB128Bytes(0);
2183 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002184 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002185 Asm->EmitInt8(0); Asm->EOL("End of files");
Jim Laskeyef42a012006-11-02 20:12:39 +00002186
2187 EmitLabel("line_prolog_end", 0);
2188
2189 // A sequence for each text section.
2190 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2191 // Isolate current sections line info.
2192 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
2193
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002194 Asm->EOL(std::string("Section ") + SectionMap[j + 1]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002195
2196 // Dwarf assumes we start with first line of first source file.
2197 unsigned Source = 1;
2198 unsigned Line = 1;
2199
2200 // Construct rows of the address, source, line, column matrix.
2201 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2202 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002203 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002204 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002205
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002206 unsigned SourceID = LineInfo.getSourceID();
2207 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2208 unsigned DirectoryID = SourceFile.getDirectoryID();
2209 Asm->EOL(Directories[DirectoryID]
2210 + SourceFile.getName()
2211 + ":"
2212 + utostr_32(LineInfo.getLine()));
Jim Laskeyef42a012006-11-02 20:12:39 +00002213
2214 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002215 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2216 Asm->EmitInt8(TAI->getAddressSize() + 1); Asm->EOL("Op size");
2217 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002218 EmitReference("label", LabelID); Asm->EOL("Location label");
Jim Laskeyef42a012006-11-02 20:12:39 +00002219
2220 // If change of source, then switch to the new source.
2221 if (Source != LineInfo.getSourceID()) {
2222 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002223 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2224 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002225 }
2226
2227 // If change of line.
2228 if (Line != LineInfo.getLine()) {
2229 // Determine offset.
2230 int Offset = LineInfo.getLine() - Line;
2231 int Delta = Offset - MinLineDelta;
2232
2233 // Update line.
2234 Line = LineInfo.getLine();
2235
2236 // If delta is small enough and in range...
2237 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2238 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002239 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002240 } else {
2241 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002242 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2243 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2244 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002245 }
2246 } else {
2247 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002248 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002249 }
2250 }
2251
2252 // Define last address of section.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002253 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2254 Asm->EmitInt8(TAI->getAddressSize() + 1); Asm->EOL("Op size");
2255 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2256 EmitReference("section_end", j + 1); Asm->EOL("Section end label");
Jim Laskeyef42a012006-11-02 20:12:39 +00002257
2258 // Mark end of matrix.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002259 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
Jim Laskeybacd3042007-02-21 22:48:45 +00002260 Asm->EmitULEB128Bytes(1); Asm->EOL();
2261 Asm->EmitInt8(1); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002262 }
2263
2264 EmitLabel("line_end", 0);
2265
Jim Laskeybacd3042007-02-21 22:48:45 +00002266 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002267 }
2268
Jim Laskey65195462006-10-30 13:35:07 +00002269 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
2270 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002271 void EmitInitialDebugFrame() {
2272 if (!TAI->getDwarfRequiresFrameSection())
2273 return;
2274
2275 int stackGrowth =
2276 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2277 TargetFrameInfo::StackGrowsUp ?
2278 TAI->getAddressSize() : -TAI->getAddressSize();
2279
2280 // Start the dwarf frame section.
2281 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2282
2283 EmitLabel("frame_common", 0);
2284 EmitDifference("frame_common_end", 0,
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002285 "frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002286 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002287
2288 EmitLabel("frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002289 Asm->EmitInt32((int)DW_CIE_ID);
2290 Asm->EOL("CIE Identifier Tag");
2291 Asm->EmitInt8(DW_CIE_VERSION);
2292 Asm->EOL("CIE Version");
2293 Asm->EmitString("");
2294 Asm->EOL("CIE Augmentation");
2295 Asm->EmitULEB128Bytes(1);
2296 Asm->EOL("CIE Code Alignment Factor");
2297 Asm->EmitSLEB128Bytes(stackGrowth);
2298 Asm->EOL("CIE Data Alignment Factor");
2299 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister()));
2300 Asm->EOL("CIE RA Column");
Jim Laskey65195462006-10-30 13:35:07 +00002301
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002302 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002303 RI->getInitialFrameState(Moves);
2304 EmitFrameMoves(NULL, 0, Moves);
Jim Laskeyef42a012006-11-02 20:12:39 +00002305
Jim Laskeyf9e56192007-01-24 13:12:32 +00002306 Asm->EmitAlignment(2);
Jim Laskeyef42a012006-11-02 20:12:39 +00002307 EmitLabel("frame_common_end", 0);
2308
Jim Laskeybacd3042007-02-21 22:48:45 +00002309 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002310 }
2311
Jim Laskey65195462006-10-30 13:35:07 +00002312 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2313 /// section.
Jim Laskeyef42a012006-11-02 20:12:39 +00002314 void EmitFunctionDebugFrame() {
Reid Spencer5a4951e2006-11-07 06:36:36 +00002315 if (!TAI->getDwarfRequiresFrameSection())
2316 return;
Jim Laskey9d4209f2006-11-07 19:33:46 +00002317
Jim Laskeyef42a012006-11-02 20:12:39 +00002318 // Start the dwarf frame section.
2319 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2320
2321 EmitDifference("frame_end", SubprogramCount,
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002322 "frame_begin", SubprogramCount, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002323 Asm->EOL("Length of Frame Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002324
2325 EmitLabel("frame_begin", SubprogramCount);
2326
Jim Laskeyb82313f2007-02-01 16:31:34 +00002327 EmitDifference("frame_common_begin", 0, "section_frame", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002328 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002329
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002330 EmitReference("func_begin", SubprogramCount);
2331 Asm->EOL("FDE initial location");
Jim Laskeyef42a012006-11-02 20:12:39 +00002332 EmitDifference("func_end", SubprogramCount,
2333 "func_begin", SubprogramCount);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002334 Asm->EOL("FDE address range");
Jim Laskeyef42a012006-11-02 20:12:39 +00002335
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002336 std::vector<MachineMove> &Moves = MMI->getFrameMoves();
Jim Laskeyef42a012006-11-02 20:12:39 +00002337
2338 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2339
Jim Laskeyf9e56192007-01-24 13:12:32 +00002340 Asm->EmitAlignment(2);
Jim Laskeyef42a012006-11-02 20:12:39 +00002341 EmitLabel("frame_end", SubprogramCount);
2342
Jim Laskeybacd3042007-02-21 22:48:45 +00002343 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002344 }
2345
2346 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002347 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002348 void EmitDebugPubNames() {
2349 // Start the dwarf pubnames section.
2350 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2351
Jim Laskey5496f012006-11-09 14:52:14 +00002352 CompileUnit *Unit = GetBaseCompileUnit();
2353
2354 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002355 "pubnames_begin", Unit->getID(), true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002356 Asm->EOL("Length of Public Names Info");
Jim Laskey5496f012006-11-09 14:52:14 +00002357
2358 EmitLabel("pubnames_begin", Unit->getID());
2359
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002360 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
Jim Laskey5496f012006-11-09 14:52:14 +00002361
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002362 EmitDifference("info_begin", Unit->getID(), "section_info", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002363 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002364
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002365 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002366 Asm->EOL("Compilation Unit Length");
Jim Laskey5496f012006-11-09 14:52:14 +00002367
2368 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2369
2370 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2371 GE = Globals.end();
2372 GI != GE; ++GI) {
2373 const std::string &Name = GI->first;
2374 DIE * Entity = GI->second;
Jim Laskeyef42a012006-11-02 20:12:39 +00002375
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002376 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2377 Asm->EmitString(Name); Asm->EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00002378 }
Jim Laskey5496f012006-11-09 14:52:14 +00002379
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002380 Asm->EmitInt32(0); Asm->EOL("End Mark");
Jim Laskey5496f012006-11-09 14:52:14 +00002381 EmitLabel("pubnames_end", Unit->getID());
2382
Jim Laskeybacd3042007-02-21 22:48:45 +00002383 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002384 }
2385
2386 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002387 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002388 void EmitDebugStr() {
2389 // Check to see if it is worth the effort.
2390 if (!StringPool.empty()) {
2391 // Start the dwarf str section.
2392 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2393
2394 // For each of strings in the string pool.
2395 for (unsigned StringID = 1, N = StringPool.size();
2396 StringID <= N; ++StringID) {
2397 // Emit a label for reference from debug information entries.
2398 EmitLabel("string", StringID);
2399 // Emit the string itself.
2400 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002401 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002402 }
2403
Jim Laskeybacd3042007-02-21 22:48:45 +00002404 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002405 }
2406 }
2407
2408 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002409 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002410 void EmitDebugLoc() {
2411 // Start the dwarf loc section.
2412 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2413
Jim Laskeybacd3042007-02-21 22:48:45 +00002414 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002415 }
2416
2417 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002418 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002419 void EmitDebugARanges() {
2420 // Start the dwarf aranges section.
2421 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2422
2423 // FIXME - Mock up
2424 #if 0
Jim Laskey5496f012006-11-09 14:52:14 +00002425 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002426
Jim Laskey5496f012006-11-09 14:52:14 +00002427 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002428 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Jim Laskey5496f012006-11-09 14:52:14 +00002429
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002430 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Jim Laskey5496f012006-11-09 14:52:14 +00002431
2432 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002433 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002434
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002435 Asm->EmitInt8(TAI->getAddressSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002436
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002437 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002438
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002439 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2440 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002441
Jim Laskey5496f012006-11-09 14:52:14 +00002442 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002443 EmitReference("text_begin", 0); Asm->EOL("Address");
2444 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002445
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002446 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2447 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Jim Laskey5496f012006-11-09 14:52:14 +00002448
Jim Laskeybacd3042007-02-21 22:48:45 +00002449 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002450 #endif
2451 }
2452
2453 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002454 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002455 void EmitDebugRanges() {
2456 // Start the dwarf ranges section.
2457 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2458
Jim Laskeybacd3042007-02-21 22:48:45 +00002459 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002460 }
2461
2462 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002463 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002464 void EmitDebugMacInfo() {
2465 // Start the dwarf macinfo section.
2466 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2467
Jim Laskeybacd3042007-02-21 22:48:45 +00002468 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002469 }
2470
Jim Laskey65195462006-10-30 13:35:07 +00002471 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2472 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002473 void ConstructCompileUnitDIEs() {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002474 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
Jim Laskeyef42a012006-11-02 20:12:39 +00002475
2476 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002477 unsigned ID = MMI->RecordSource(CUW[i]);
Jim Laskey9d4209f2006-11-07 19:33:46 +00002478 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002479 CompileUnits.push_back(Unit);
2480 }
2481 }
2482
Jim Laskey65195462006-10-30 13:35:07 +00002483 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2484 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002485 void ConstructGlobalDIEs() {
2486 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002487 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M);
Jim Laskeyef42a012006-11-02 20:12:39 +00002488
2489 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2490 GlobalVariableDesc *GVD = GlobalVariables[i];
2491 NewGlobalVariable(GVD);
2492 }
2493 }
Jim Laskey65195462006-10-30 13:35:07 +00002494
2495 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2496 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002497 void ConstructSubprogramDIEs() {
2498 std::vector<SubprogramDesc *> Subprograms =
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002499 MMI->getAnchoredDescriptors<SubprogramDesc>(*M);
Jim Laskeyef42a012006-11-02 20:12:39 +00002500
2501 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2502 SubprogramDesc *SPD = Subprograms[i];
2503 NewSubprogram(SPD);
2504 }
2505 }
Jim Laskey65195462006-10-30 13:35:07 +00002506
Jim Laskey65195462006-10-30 13:35:07 +00002507public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002508 //===--------------------------------------------------------------------===//
2509 // Main entry points.
2510 //
Jim Laskey072200c2007-01-29 18:51:14 +00002511 DwarfDebug(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
2512 : Dwarf(OS, A, T)
Jim Laskeyef42a012006-11-02 20:12:39 +00002513 , CompileUnits()
2514 , AbbreviationsSet(InitAbbreviationsSetSize)
2515 , Abbreviations()
2516 , ValuesSet(InitValuesSetSize)
2517 , Values()
2518 , StringPool()
2519 , DescToUnitMap()
2520 , SectionMap()
2521 , SectionSourceLines()
Jim Laskeybacd3042007-02-21 22:48:45 +00002522 , didInitial(false)
2523 , shouldEmit(false)
Jim Laskeyef42a012006-11-02 20:12:39 +00002524 {
2525 }
Jim Laskey072200c2007-01-29 18:51:14 +00002526 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002527 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2528 delete CompileUnits[i];
2529 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2530 delete Values[j];
2531 }
2532
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002533 /// SetModuleInfo - Set machine module information when it's known that pass
2534 /// manager has created it. Set by the target AsmPrinter.
2535 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002536 // Make sure initial declarations are made.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002537 if (!MMI && mmi->hasDebugInfo()) {
2538 MMI = mmi;
Jim Laskeyef42a012006-11-02 20:12:39 +00002539 shouldEmit = true;
2540
2541 // Emit initial sections
2542 EmitInitial();
2543
2544 // Create all the compile unit DIEs.
2545 ConstructCompileUnitDIEs();
2546
2547 // Create DIEs for each of the externally visible global variables.
2548 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002549
Jim Laskeyef42a012006-11-02 20:12:39 +00002550 // Create DIEs for each of the externally visible subprograms.
2551 ConstructSubprogramDIEs();
2552
2553 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00002554 SectionMap.insert(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002555 }
2556 }
2557
Jim Laskey65195462006-10-30 13:35:07 +00002558 /// BeginModule - Emit all Dwarf sections that should come prior to the
2559 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002560 void BeginModule(Module *M) {
2561 this->M = M;
2562
2563 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002564 }
2565
Jim Laskey65195462006-10-30 13:35:07 +00002566 /// EndModule - Emit all Dwarf sections that should come after the content.
2567 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002568 void EndModule() {
2569 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002570
2571 // Standard sections final addresses.
2572 Asm->SwitchToTextSection(TAI->getTextSection());
2573 EmitLabel("text_end", 0);
2574 Asm->SwitchToDataSection(TAI->getDataSection());
2575 EmitLabel("data_end", 0);
2576
2577 // End text sections.
2578 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2579 Asm->SwitchToTextSection(SectionMap[i].c_str());
2580 EmitLabel("section_end", i);
2581 }
2582
2583 // Compute DIE offsets and sizes.
2584 SizeAndOffsets();
2585
2586 // Emit all the DIEs into a debug info section
2587 EmitDebugInfo();
2588
2589 // Corresponding abbreviations into a abbrev section.
2590 EmitAbbreviations();
2591
2592 // Emit source line correspondence into a debug line section.
2593 EmitDebugLines();
2594
2595 // Emit info into a debug pubnames section.
2596 EmitDebugPubNames();
2597
2598 // Emit info into a debug str section.
2599 EmitDebugStr();
2600
2601 // Emit info into a debug loc section.
2602 EmitDebugLoc();
2603
2604 // Emit info into a debug aranges section.
2605 EmitDebugARanges();
2606
2607 // Emit info into a debug ranges section.
2608 EmitDebugRanges();
2609
2610 // Emit info into a debug macinfo section.
2611 EmitDebugMacInfo();
2612 }
2613
Jim Laskey65195462006-10-30 13:35:07 +00002614 /// BeginFunction - Gather pre-function debug information. Assumes being
2615 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002616 void BeginFunction(MachineFunction *MF) {
2617 this->MF = MF;
2618
2619 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002620
2621 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002622 MMI->BeginFunction(MF);
Jim Laskeyef42a012006-11-02 20:12:39 +00002623
2624 // Assumes in correct section after the entry point.
2625 EmitLabel("func_begin", ++SubprogramCount);
2626 }
Jim Laskey072200c2007-01-29 18:51:14 +00002627
Jim Laskey65195462006-10-30 13:35:07 +00002628 /// EndFunction - Gather and emit post-function debug information.
2629 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002630 void EndFunction() {
2631 if (!ShouldEmitDwarf()) return;
Jim Laskeyb82313f2007-02-01 16:31:34 +00002632
2633 // Define end label for subprogram.
2634 EmitLabel("func_end", SubprogramCount);
Jim Laskeyef42a012006-11-02 20:12:39 +00002635
2636 // Get function line info.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002637 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
Jim Laskeyef42a012006-11-02 20:12:39 +00002638
2639 if (!LineInfos.empty()) {
2640 // Get section line info.
2641 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2642 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2643 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2644 // Append the function info to section info.
2645 SectionLineInfos.insert(SectionLineInfos.end(),
2646 LineInfos.begin(), LineInfos.end());
2647 }
2648
2649 // Construct scopes for subprogram.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002650 ConstructRootScope(MMI->getRootScope());
Jim Laskeyef42a012006-11-02 20:12:39 +00002651
2652 // Emit function frame information.
2653 EmitFunctionDebugFrame();
Jim Laskeyef42a012006-11-02 20:12:39 +00002654 }
Jim Laskey65195462006-10-30 13:35:07 +00002655};
2656
Jim Laskey072200c2007-01-29 18:51:14 +00002657//===----------------------------------------------------------------------===//
2658/// DwarfException - Emits Dwarf exception handling directives.
2659///
2660class DwarfException : public Dwarf {
2661
Jim Laskeyb82313f2007-02-01 16:31:34 +00002662private:
2663
Jim Laskeybacd3042007-02-21 22:48:45 +00002664 /// didInitial - Flag to indicate if initial emission has been done.
2665 ///
2666 bool didInitial;
2667
2668 /// shouldEmit - Flag to indicate if debug information should be emitted.
2669 ///
2670 bool shouldEmit;
Jim Laskey3f09fc22007-02-28 18:38:31 +00002671
2672 /// FuncCPPPersonality - C++ persoanlity function.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002673 ///
Jim Laskey3f09fc22007-02-28 18:38:31 +00002674 Function *FuncCPPPersonality;
2675
2676 /// EmitCommonEHFrame - Emit the common eh unwind frame.
2677 ///
2678 void EmitCommonEHFrame() {
2679 // Only do it once.
2680 if (didInitial) return;
2681 didInitial = true;
2682
Jim Laskeybacd3042007-02-21 22:48:45 +00002683 // If there is a personality present then we need to indicate that
2684 // in the common eh frame.
Jim Laskey3f09fc22007-02-28 18:38:31 +00002685 Function *Personality = FuncCPPPersonality;
Jim Laskeybacd3042007-02-21 22:48:45 +00002686
2687 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002688 int stackGrowth =
2689 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2690 TargetFrameInfo::StackGrowsUp ?
2691 TAI->getAddressSize() : -TAI->getAddressSize();
2692
Jim Laskeybacd3042007-02-21 22:48:45 +00002693 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002694 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
2695 O << "EH_frame:\n";
2696 EmitLabel("section_eh_frame", 0);
2697
Jim Laskeybacd3042007-02-21 22:48:45 +00002698 // Define base labels.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002699 EmitLabel("eh_frame_common", 0);
Jim Laskeybacd3042007-02-21 22:48:45 +00002700
2701 // Define the eh frame length.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002702 EmitDifference("eh_frame_common_end", 0,
2703 "eh_frame_common_begin", 0, true);
2704 Asm->EOL("Length of Common Information Entry");
2705
Jim Laskeybacd3042007-02-21 22:48:45 +00002706 // EH frame header.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002707 EmitLabel("eh_frame_common_begin", 0);
2708 Asm->EmitInt32((int)0);
2709 Asm->EOL("CIE Identifier Tag");
2710 Asm->EmitInt8(DW_CIE_VERSION);
2711 Asm->EOL("CIE Version");
Jim Laskeybacd3042007-02-21 22:48:45 +00002712
2713 // The personality presence indicates that language specific information
2714 // will show up in the eh frame.
2715 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00002716 Asm->EOL("CIE Augmentation");
Jim Laskeybacd3042007-02-21 22:48:45 +00002717
2718 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002719 Asm->EmitULEB128Bytes(1);
2720 Asm->EOL("CIE Code Alignment Factor");
2721 Asm->EmitSLEB128Bytes(stackGrowth);
2722 Asm->EOL("CIE Data Alignment Factor");
2723 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister()));
2724 Asm->EOL("CIE RA Column");
Jim Laskeyb82313f2007-02-01 16:31:34 +00002725
Jim Laskeybacd3042007-02-21 22:48:45 +00002726 // If there is a personality, we need to indicate the functions location.
2727 if (Personality) {
2728 Asm->EmitULEB128Bytes(7);
2729 Asm->EOL("Augmentation Size");
2730 Asm->EmitInt8(DW_EH_PE_indirect |
2731 DW_EH_PE_pcrel |
2732 DW_EH_PE_sdata4);
2733 Asm->EOL("Personality (indirect pcrel sdata4)");
2734
2735 O << TAI->getData32bitsDirective();
2736 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
2737 O << "-" << TAI->getPCSymbol();
2738 Asm->EOL("Personality");
2739
2740 Asm->EmitULEB128Bytes(DW_EH_PE_pcrel);
2741 Asm->EOL("LSDA Encoding (pcrel)");
2742 Asm->EmitULEB128Bytes(DW_EH_PE_pcrel);
2743 Asm->EOL("FDE Encoding (pcrel)");
2744 } else {
2745 Asm->EmitULEB128Bytes(1);
2746 Asm->EOL("Augmentation Size");
2747 Asm->EmitULEB128Bytes(DW_EH_PE_pcrel);
2748 Asm->EOL("FDE Encoding (pcrel)");
2749 }
2750
2751 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002752 std::vector<MachineMove> Moves;
2753 RI->getInitialFrameState(Moves);
2754 EmitFrameMoves(NULL, 0, Moves);
2755
2756 Asm->EmitAlignment(2);
2757 EmitLabel("eh_frame_common_end", 0);
2758
Jim Laskeybacd3042007-02-21 22:48:45 +00002759 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00002760 }
2761
Jim Laskeyb82313f2007-02-01 16:31:34 +00002762 /// EmitEHFrame - Emit initial exception information.
2763 ///
2764 void EmitEHFrame() {
Jim Laskeybacd3042007-02-21 22:48:45 +00002765 // If there is a personality present then we need to indicate that
2766 // in the common eh frame.
Jim Laskey3f09fc22007-02-28 18:38:31 +00002767 Function *Personality = FuncCPPPersonality;
2768// Function *Personality = MMI->getPersonality();
Jim Laskeybacd3042007-02-21 22:48:45 +00002769 MachineFrameInfo *MFI = MF->getFrameInfo();
Jim Laskeyb82313f2007-02-01 16:31:34 +00002770
Jim Laskeybacd3042007-02-21 22:48:45 +00002771 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
2772
2773 // Externally visible entry into the functions eh frame info.
2774 if (const char *GlobalDirective = TAI->getGlobalDirective())
2775 O << GlobalDirective << getAsm()->CurrentFnName << ".eh\n";
Jim Laskeyb82313f2007-02-01 16:31:34 +00002776
Jim Laskeybacd3042007-02-21 22:48:45 +00002777 // If there are no calls then you can't unwind.
2778 if (!MFI->hasCalls()) {
2779 O << getAsm()->CurrentFnName << ".eh = 0\n";
2780 } else {
2781 O << getAsm()->CurrentFnName << ".eh:\n";
2782
2783 // EH frame header.
2784 EmitDifference("eh_frame_end", SubprogramCount,
2785 "eh_frame_begin", SubprogramCount, true);
2786 Asm->EOL("Length of Frame Information Entry");
2787
2788 EmitLabel("eh_frame_begin", SubprogramCount);
2789
2790 EmitDifference("eh_frame_begin", SubprogramCount,
2791 "section_eh_frame", 0, true);
2792 Asm->EOL("FDE CIE offset");
2793
2794 EmitReference("eh_func_begin", SubprogramCount, true);
2795 Asm->EOL("FDE initial location");
2796 EmitDifference("eh_func_end", SubprogramCount,
2797 "eh_func_begin", SubprogramCount);
2798 Asm->EOL("FDE address range");
2799
Jim Laskey3f09fc22007-02-28 18:38:31 +00002800 // If there is a personality and landing pads then point to the language
2801 // specific data area in the exception table.
Jim Laskeybacd3042007-02-21 22:48:45 +00002802 if (Personality) {
2803 Asm->EmitULEB128Bytes(4);
2804 Asm->EOL("Augmentation size");
Jim Laskey3f09fc22007-02-28 18:38:31 +00002805
2806 if (!MMI->getLandingPads().empty()) {
2807 EmitReference("exception", SubprogramCount, true);
2808 } else if(TAI->getAddressSize() == 8) {
2809 Asm->EmitInt64((int)0);
2810 } else {
2811 Asm->EmitInt32((int)0);
2812 }
Jim Laskeybacd3042007-02-21 22:48:45 +00002813 Asm->EOL("Language Specific Data Area");
2814 } else {
2815 Asm->EmitULEB128Bytes(0);
2816 Asm->EOL("Augmentation size");
2817 }
2818
2819 // Indicate locations of function specific callee saved registers in
2820 // frame.
2821 std::vector<MachineMove> &Moves = MMI->getFrameMoves();
2822 EmitFrameMoves("eh_func_begin", SubprogramCount, Moves);
2823
2824 Asm->EmitAlignment(2);
2825 EmitLabel("eh_frame_end", SubprogramCount);
2826 }
Jim Laskeyb82313f2007-02-01 16:31:34 +00002827
Jim Laskeybacd3042007-02-21 22:48:45 +00002828 if (const char *UsedDirective = TAI->getUsedDirective())
2829 O << UsedDirective << getAsm()->CurrentFnName << ".eh\n\n";
2830 }
2831
2832 /// EmitExceptionTable - Emit landpads and actions.
2833 ///
2834 /// The general organization of the table is complex, but the basic concepts
2835 /// are easy. First there is a header which describes the location and
2836 /// organization of the three components that follow.
2837 /// 1. The landing pad site information describes the range of code covered
2838 /// by the try. In our case it's an accumulation of the ranges covered
2839 /// by the invokes in the try. There is also a reference to the landing
2840 /// pad that handles the exception once processed. Finally an index into
2841 /// the actions table.
2842 /// 2. The action table, in our case, is composed of pairs of type ids
2843 /// and next action offset. Starting with the action index from the
2844 /// landing pad site, each type Id is checked for a match to the current
2845 /// exception. If it matches then the exception and type id are passed
2846 /// on to the landing pad. Otherwise the next action is looked up. This
2847 /// chain is terminated with a next action of zero. If no type id is
2848 /// found the the frame is unwound and handling continues.
2849 /// 3. Type id table contains references to all the C++ typeinfo for all
2850 /// catches in the function. This tables is reversed indexed base 1.
2851 void EmitExceptionTable() {
2852 // Map all labels and get rid of any dead landing pads.
2853 MMI->TidyLandingPads();
Jim Laskeyb82313f2007-02-01 16:31:34 +00002854
Jim Laskeybacd3042007-02-21 22:48:45 +00002855 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
2856 const std::vector<LandingPadInfo> &LandingPads = MMI->getLandingPads();
2857 if (LandingPads.empty()) return;
Jim Laskeyb82313f2007-02-01 16:31:34 +00002858
Jim Laskeybacd3042007-02-21 22:48:45 +00002859 // FIXME - Should fold actions for multiple landing pads.
2860
2861 // Gather first action index for each landing pad site.
2862 SmallVector<unsigned, 8> Actions;
2863
2864 // Compute sizes for exception table.
2865 unsigned SizeHeader = sizeof(int8_t) + // LPStart format
2866 sizeof(int8_t) + // TType format
2867 sizeof(int8_t) + // TType base offset (NEED ULEB128)
2868 sizeof(int8_t) + // Call site format
2869 sizeof(int8_t); // Call-site table length
2870 unsigned SizeSites = 0;
2871 unsigned SizeActions = 0;
2872
2873 // Look at each landing pad site to compute size. We need the size of each
2874 // landing pad site info and the size of the landing pad's actions.
2875 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
2876 const LandingPadInfo &LandingPad = LandingPads[i];
2877 unsigned SizeSiteActions = 0;
2878 const std::vector<unsigned> &TypeIds = LandingPad.TypeIds;
2879 unsigned SizeAction = 0;
2880
2881 // Gather the action sizes
2882 for (unsigned j = 0, M = TypeIds.size(); j != M; ++j) {
2883 unsigned TypeID = TypeIds[i];
2884 unsigned SizeTypeID = Asm->SizeULEB128(TypeID);
2885 signed Action = j ? -(SizeAction + SizeTypeID) : 0;
2886 SizeAction = SizeTypeID + Asm->SizeSLEB128(Action);
2887 SizeSiteActions += SizeAction;
2888 }
2889
2890 // Record the first action of the landing pad site.
2891 signed FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
2892 Actions.push_back(FirstAction);
2893
2894 // Compute this sites contribution to size.
2895 SizeActions += SizeSiteActions;
2896 SizeSites += sizeof(int32_t) + // Site start.
2897 sizeof(int32_t) + // Site length.
2898 sizeof(int32_t) + // Landing pad.
2899 Asm->SizeULEB128(FirstAction); // Action.
2900 }
2901
2902 // Final tallies.
2903 unsigned SizeTypes = TypeInfos.size() * TAI->getAddressSize();
2904 unsigned SizePreType = SizeHeader + SizeSites + SizeActions;
2905 unsigned SizeAlign = (4 - SizePreType) & 3;
2906 unsigned TypeOffset = SizePreType +
2907 SizeTypes +
2908 SizeAlign -
2909 sizeof(int8_t) - // LPStart format
2910 sizeof(int8_t) - // TType format
2911 sizeof(int8_t); // TType base offset (NEED ULEB128)
2912
2913 // Begin the exception table.
2914 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
2915 O << "GCC_except_table" << SubprogramCount << ":\n";
2916 EmitLabel("exception", SubprogramCount);
2917
2918 // Emit the header.
2919 Asm->EmitInt8(DW_EH_PE_omit);
2920 Asm->EOL("LPStart format (DW_EH_PE_omit)");
2921 Asm->EmitInt8(DW_EH_PE_absptr);
2922 Asm->EOL("TType format (DW_EH_PE_absptr)");
2923 Asm->EmitULEB128Bytes(TypeOffset);
2924 Asm->EOL("TType base offset");
2925 Asm->EmitInt8(DW_EH_PE_udata4);
2926 Asm->EOL("Call site format (DW_EH_PE_udata4)");
2927 Asm->EmitULEB128Bytes(SizeSites);
2928 Asm->EOL("Call-site table length");
2929
2930 // Emit the landng pad site information.
2931 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
2932 const LandingPadInfo &LandingPad = LandingPads[i];
2933 EmitDifference("label", LandingPad.BeginLabel,
2934 "eh_func_begin", SubprogramCount);
2935 Asm->EOL("Region start");
2936
2937 EmitDifference("label", LandingPad.EndLabel,
2938 "label", LandingPad.BeginLabel);
2939 Asm->EOL("Region length");
2940
2941 EmitDifference("label", LandingPad.LandingPadLabel,
2942 "eh_func_begin", SubprogramCount);
2943 Asm->EOL("Landing pad");
2944
2945 Asm->EmitULEB128Bytes(Actions[i]);
2946 Asm->EOL("Action");
2947 }
2948
2949 // Emit the actions.
2950 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
2951 const LandingPadInfo &LandingPad = LandingPads[i];
2952 const std::vector<unsigned> &TypeIds = LandingPad.TypeIds;
2953 unsigned SizeAction = 0;
2954
2955 for (unsigned j = 0, M = TypeIds.size(); j < M; ++j) {
2956 unsigned TypeID = TypeIds[j];
2957 unsigned SizeTypeID = Asm->SizeULEB128(TypeID);
2958 Asm->EmitSLEB128Bytes(TypeID);
2959 Asm->EOL("TypeInfo index");
2960 signed Action = j ? -(SizeAction + SizeTypeID) : 0;
2961 SizeAction = SizeTypeID + Asm->SizeSLEB128(Action);
2962 Asm->EmitSLEB128Bytes(Action);
2963 Asm->EOL("Next action");
2964 }
2965 }
2966
2967 // Emit the type ids.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002968 Asm->EmitAlignment(2);
Jim Laskeybacd3042007-02-21 22:48:45 +00002969 for (unsigned M = TypeInfos.size(); M; --M) {
2970 GlobalVariable *GV = TypeInfos[M - 1];
2971
2972 if (TAI->getAddressSize() == sizeof(int32_t))
2973 O << TAI->getData32bitsDirective();
2974 else
2975 O << TAI->getData64bitsDirective();
2976
2977 if (GV)
2978 O << Asm->getGlobalLinkName(GV);
2979 else
2980 O << "0";
2981
2982 Asm->EOL("TypeInfo");
2983 }
2984
Jim Laskeyb82313f2007-02-01 16:31:34 +00002985 }
2986
Jim Laskey072200c2007-01-29 18:51:14 +00002987public:
2988 //===--------------------------------------------------------------------===//
2989 // Main entry points.
2990 //
2991 DwarfException(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
2992 : Dwarf(OS, A, T)
Jim Laskeybacd3042007-02-21 22:48:45 +00002993 , didInitial(false)
2994 , shouldEmit(false)
Jim Laskey3f09fc22007-02-28 18:38:31 +00002995 , FuncCPPPersonality(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +00002996 {}
2997
2998 virtual ~DwarfException() {}
2999
3000 /// SetModuleInfo - Set machine module information when it's known that pass
3001 /// manager has created it. Set by the target AsmPrinter.
3002 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00003003 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00003004 }
3005
3006 /// BeginModule - Emit all exception information that should come prior to the
3007 /// content.
3008 void BeginModule(Module *M) {
3009 this->M = M;
Jim Laskey3f09fc22007-02-28 18:38:31 +00003010 FuncCPPPersonality = M->getFunction("__gxx_personality_v0");
Jim Laskey072200c2007-01-29 18:51:14 +00003011 }
3012
3013 /// EndModule - Emit all exception information that should come after the
3014 /// content.
3015 void EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00003016 }
3017
3018 /// BeginFunction - Gather pre-function exception information. Assumes being
3019 /// emitted immediately after the function entry point.
3020 void BeginFunction(MachineFunction *MF) {
3021 this->MF = MF;
3022
Jim Laskey3f09fc22007-02-28 18:38:31 +00003023 if (MMI &&
3024 ExceptionHandling &&
3025 TAI->getSupportsExceptionHandling()) {
3026 shouldEmit = true;
3027 // Assumes in correct section after the entry point.
3028 EmitLabel("eh_func_begin", ++SubprogramCount);
3029 }
Jim Laskey072200c2007-01-29 18:51:14 +00003030 }
3031
3032 /// EndFunction - Gather and emit post-function exception information.
3033 ///
3034 void EndFunction() {
Jim Laskey3f09fc22007-02-28 18:38:31 +00003035 if (!shouldEmit) return;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003036
3037 EmitLabel("eh_func_end", SubprogramCount);
Jim Laskeybacd3042007-02-21 22:48:45 +00003038 EmitExceptionTable();
Jim Laskey3f09fc22007-02-28 18:38:31 +00003039 EmitCommonEHFrame();
Jim Laskeybacd3042007-02-21 22:48:45 +00003040 EmitEHFrame();
Jim Laskey072200c2007-01-29 18:51:14 +00003041 }
3042};
3043
Jim Laskey0d086af2006-02-27 12:43:29 +00003044} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00003045
3046//===----------------------------------------------------------------------===//
3047
Jim Laskeyd18e2892006-01-20 20:34:06 +00003048/// Emit - Print the abbreviation using the specified Dwarf writer.
3049///
Jim Laskey072200c2007-01-29 18:51:14 +00003050void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003051 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00003052 DD.getAsm()->EmitULEB128Bytes(Tag);
3053 DD.getAsm()->EOL(TagString(Tag));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003054
3055 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00003056 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3057 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003058
3059 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00003060 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003061 const DIEAbbrevData &AttrData = Data[i];
3062
3063 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00003064 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3065 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003066
3067 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00003068 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3069 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003070 }
3071
3072 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00003073 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3074 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00003075}
3076
3077#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00003078void DIEAbbrev::print(std::ostream &O) {
3079 O << "Abbreviation @"
3080 << std::hex << (intptr_t)this << std::dec
3081 << " "
3082 << TagString(Tag)
3083 << " "
3084 << ChildrenString(ChildrenFlag)
3085 << "\n";
3086
3087 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3088 O << " "
3089 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003090 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00003091 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003092 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00003093 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00003094}
Bill Wendlinge8156192006-12-07 01:30:32 +00003095void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00003096#endif
3097
3098//===----------------------------------------------------------------------===//
3099
Jim Laskeyef42a012006-11-02 20:12:39 +00003100#ifndef NDEBUG
3101void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003102 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003103}
Jim Laskeyef42a012006-11-02 20:12:39 +00003104#endif
3105
3106//===----------------------------------------------------------------------===//
3107
Jim Laskey063e7652006-01-17 17:31:53 +00003108/// EmitValue - Emit integer of appropriate size.
3109///
Jim Laskey072200c2007-01-29 18:51:14 +00003110void DIEInteger::EmitValue(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +00003111 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00003112 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00003113 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003114 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003115 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003116 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003117 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003118 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003119 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003120 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
3121 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
3122 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003123 default: assert(0 && "DIE Value form not supported yet"); break;
3124 }
3125}
3126
3127/// SizeOf - Determine size of integer value in bytes.
3128///
Jim Laskey072200c2007-01-29 18:51:14 +00003129unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003130 switch (Form) {
3131 case DW_FORM_flag: // Fall thru
3132 case DW_FORM_ref1: // Fall thru
3133 case DW_FORM_data1: return sizeof(int8_t);
3134 case DW_FORM_ref2: // Fall thru
3135 case DW_FORM_data2: return sizeof(int16_t);
3136 case DW_FORM_ref4: // Fall thru
3137 case DW_FORM_data4: return sizeof(int32_t);
3138 case DW_FORM_ref8: // Fall thru
3139 case DW_FORM_data8: return sizeof(int64_t);
Jim Laskey072200c2007-01-29 18:51:14 +00003140 case DW_FORM_udata: return DD.getAsm()->SizeULEB128(Integer);
3141 case DW_FORM_sdata: return DD.getAsm()->SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00003142 default: assert(0 && "DIE Value form not supported yet"); break;
3143 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003144 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00003145}
3146
Jim Laskey063e7652006-01-17 17:31:53 +00003147//===----------------------------------------------------------------------===//
3148
3149/// EmitValue - Emit string value.
3150///
Jim Laskey072200c2007-01-29 18:51:14 +00003151void DIEString::EmitValue(const DwarfDebug &DD, unsigned Form) const {
3152 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00003153}
3154
Jim Laskey063e7652006-01-17 17:31:53 +00003155//===----------------------------------------------------------------------===//
3156
3157/// EmitValue - Emit label value.
3158///
Jim Laskey072200c2007-01-29 18:51:14 +00003159void DIEDwarfLabel::EmitValue(const DwarfDebug &DD, unsigned Form) const {
3160 DD.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +00003161}
3162
3163/// SizeOf - Determine size of label value in bytes.
3164///
Jim Laskey072200c2007-01-29 18:51:14 +00003165unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3166 return DD.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00003167}
Jim Laskeyef42a012006-11-02 20:12:39 +00003168
Jim Laskey063e7652006-01-17 17:31:53 +00003169//===----------------------------------------------------------------------===//
3170
Jim Laskeyd18e2892006-01-20 20:34:06 +00003171/// EmitValue - Emit label value.
3172///
Jim Laskey072200c2007-01-29 18:51:14 +00003173void DIEObjectLabel::EmitValue(const DwarfDebug &DD, unsigned Form) const {
3174 DD.EmitReference(Label);
Jim Laskeyd18e2892006-01-20 20:34:06 +00003175}
3176
3177/// SizeOf - Determine size of label value in bytes.
3178///
Jim Laskey072200c2007-01-29 18:51:14 +00003179unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3180 return DD.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00003181}
3182
3183//===----------------------------------------------------------------------===//
3184
Jim Laskey063e7652006-01-17 17:31:53 +00003185/// EmitValue - Emit delta value.
3186///
Jim Laskey072200c2007-01-29 18:51:14 +00003187void DIEDelta::EmitValue(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003188 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00003189 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00003190}
3191
3192/// SizeOf - Determine size of delta value in bytes.
3193///
Jim Laskey072200c2007-01-29 18:51:14 +00003194unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003195 if (Form == DW_FORM_data4) return 4;
Jim Laskey072200c2007-01-29 18:51:14 +00003196 return DD.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00003197}
3198
3199//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003200
Jim Laskeyb8509c52006-03-23 18:07:55 +00003201/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003202///
Jim Laskey072200c2007-01-29 18:51:14 +00003203void DIEntry::EmitValue(const DwarfDebug &DD, unsigned Form) const {
3204 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00003205}
Jim Laskeyd18e2892006-01-20 20:34:06 +00003206
3207//===----------------------------------------------------------------------===//
3208
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003209/// ComputeSize - calculate the size of the block.
3210///
Jim Laskey072200c2007-01-29 18:51:14 +00003211unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003212 if (!Size) {
3213 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
3214
3215 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00003216 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00003217 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003218 }
3219 return Size;
3220}
3221
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003222/// EmitValue - Emit block data.
3223///
Jim Laskey072200c2007-01-29 18:51:14 +00003224void DIEBlock::EmitValue(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003225 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003226 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
3227 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
3228 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
3229 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003230 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003231 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003232
3233 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
3234
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003235 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003236 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00003237 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003238 }
3239}
3240
3241/// SizeOf - Determine size of block data in bytes.
3242///
Jim Laskey072200c2007-01-29 18:51:14 +00003243unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003244 switch (Form) {
3245 case DW_FORM_block1: return Size + sizeof(int8_t);
3246 case DW_FORM_block2: return Size + sizeof(int16_t);
3247 case DW_FORM_block4: return Size + sizeof(int32_t);
Jim Laskey072200c2007-01-29 18:51:14 +00003248 case DW_FORM_block: return Size + DD.getAsm()->SizeULEB128(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003249 default: assert(0 && "Improper form for block"); break;
3250 }
3251 return 0;
3252}
3253
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003254//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003255/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00003256
3257DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003258 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00003259 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00003260}
Jim Laskeyef42a012006-11-02 20:12:39 +00003261
Jim Laskeyb8509c52006-03-23 18:07:55 +00003262/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
3263///
3264void DIE::AddSiblingOffset() {
3265 DIEInteger *DI = new DIEInteger(0);
3266 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00003267 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00003268}
3269
Jim Laskeyef42a012006-11-02 20:12:39 +00003270/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003271///
Jim Laskeyef42a012006-11-02 20:12:39 +00003272void DIE::Profile(FoldingSetNodeID &ID) {
3273 Abbrev.Profile(ID);
3274
3275 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3276 ID.AddPointer(Children[i]);
3277
3278 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3279 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003280}
Jim Laskeyef42a012006-11-02 20:12:39 +00003281
3282#ifndef NDEBUG
3283void DIE::print(std::ostream &O, unsigned IncIndent) {
3284 static unsigned IndentCount = 0;
3285 IndentCount += IncIndent;
3286 const std::string Indent(IndentCount, ' ');
3287 bool isBlock = Abbrev.getTag() == 0;
3288
3289 if (!isBlock) {
3290 O << Indent
3291 << "Die: "
3292 << "0x" << std::hex << (intptr_t)this << std::dec
3293 << ", Offset: " << Offset
3294 << ", Size: " << Size
3295 << "\n";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003296
Jim Laskeyef42a012006-11-02 20:12:39 +00003297 O << Indent
3298 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00003299 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00003300 << ChildrenString(Abbrev.getChildrenFlag());
3301 } else {
3302 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00003303 }
3304 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003305
Jim Laskeyef42a012006-11-02 20:12:39 +00003306 const std::vector<DIEAbbrevData> &Data = Abbrev.getData();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003307
Jim Laskeyef42a012006-11-02 20:12:39 +00003308 IndentCount += 2;
3309 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3310 O << Indent;
3311 if (!isBlock) {
3312 O << AttributeString(Data[i].getAttribute());
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003313 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00003314 O << "Blk[" << i << "]";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003315 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003316 O << " "
3317 << FormEncodingString(Data[i].getForm())
3318 << " ";
3319 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00003320 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00003321 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003322 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00003323
Jim Laskeyef42a012006-11-02 20:12:39 +00003324 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3325 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00003326 }
Jim Laskey063e7652006-01-17 17:31:53 +00003327
Jim Laskeyef42a012006-11-02 20:12:39 +00003328 if (!isBlock) O << "\n";
3329 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00003330}
3331
Jim Laskeyef42a012006-11-02 20:12:39 +00003332void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003333 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00003334}
Jim Laskeybd761842006-02-27 17:27:12 +00003335#endif
Jim Laskey65195462006-10-30 13:35:07 +00003336
3337//===----------------------------------------------------------------------===//
3338/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00003339///
Jim Laskey65195462006-10-30 13:35:07 +00003340
3341DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3342 const TargetAsmInfo *T) {
Jim Laskey072200c2007-01-29 18:51:14 +00003343 DE = new DwarfException(OS, A, T);
3344 DD = new DwarfDebug(OS, A, T);
Jim Laskey65195462006-10-30 13:35:07 +00003345}
3346
3347DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00003348 delete DE;
3349 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00003350}
3351
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003352/// SetModuleInfo - Set machine module info when it's known that pass manager
3353/// has created it. Set by the target AsmPrinter.
3354void DwarfWriter::SetModuleInfo(MachineModuleInfo *MMI) {
Jim Laskey072200c2007-01-29 18:51:14 +00003355 DD->SetModuleInfo(MMI);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003356 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00003357}
3358
3359/// BeginModule - Emit all Dwarf sections that should come prior to the
3360/// content.
3361void DwarfWriter::BeginModule(Module *M) {
Jim Laskey072200c2007-01-29 18:51:14 +00003362 DE->BeginModule(M);
3363 DD->BeginModule(M);
Jim Laskey65195462006-10-30 13:35:07 +00003364}
3365
3366/// EndModule - Emit all Dwarf sections that should come after the content.
3367///
3368void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00003369 DE->EndModule();
3370 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00003371}
3372
3373/// BeginFunction - Gather pre-function debug information. Assumes being
3374/// emitted immediately after the function entry point.
3375void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00003376 DE->BeginFunction(MF);
3377 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00003378}
3379
3380/// EndFunction - Gather and emit post-function debug information.
3381///
3382void DwarfWriter::EndFunction() {
Jim Laskey072200c2007-01-29 18:51:14 +00003383 DD->EndFunction();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003384 DE->EndFunction();
3385
Jim Laskey3f09fc22007-02-28 18:38:31 +00003386 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00003387 // Clear function debug information.
3388 MMI->EndFunction();
3389 }
Jim Laskey65195462006-10-30 13:35:07 +00003390}