blob: 3690f37620edc6db73f0aef1e250443968e17ec4 [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//
10// This file contains support for writing dwarf debug info into asm files.
11//
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 Laskeyb2efb852006-01-04 22:28:25 +000022#include "llvm/CodeGen/MachineDebugInfo.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 Laskeyb3e789a2006-01-26 20:21:46 +000025#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000026#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000027#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000028#include "llvm/Support/Mangler.h"
Jim Laskey563321a2006-09-06 18:34:40 +000029#include "llvm/Target/TargetAsmInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000030#include "llvm/Target/MRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000031#include "llvm/Target/TargetData.h"
Jim Laskey52060a02006-01-24 00:49:18 +000032#include "llvm/Target/TargetMachine.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000033#include "llvm/Target/TargetFrameInfo.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000034#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000035#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000036using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000037using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000038
39static cl::opt<bool>
40DwarfVerbose("dwarf-verbose", cl::Hidden,
Jim Laskeyce50a162006-08-29 16:24:26 +000041 cl::desc("Add comments to Dwarf directives."));
Jim Laskey063e7652006-01-17 17:31:53 +000042
Jim Laskey0d086af2006-02-27 12:43:29 +000043namespace llvm {
Jim Laskey65195462006-10-30 13:35:07 +000044
45//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000046
47/// Configuration values for initial hash set sizes (log2).
48///
49static const unsigned InitDiesSetSize = 9; // 512
50static const unsigned InitAbbreviationsSetSize = 9; // 512
51static const unsigned InitValuesSetSize = 9; // 512
52
53//===----------------------------------------------------------------------===//
54/// Forward declarations.
55///
56class DIE;
57class DIEValue;
58
59//===----------------------------------------------------------------------===//
60/// LEB 128 number encoding.
61
62/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
63/// representing an unsigned leb128 value.
64static void PrintULEB128(std::ostream &O, unsigned Value) {
65 do {
66 unsigned Byte = Value & 0x7f;
67 Value >>= 7;
68 if (Value) Byte |= 0x80;
69 O << "0x" << std::hex << Byte << std::dec;
70 if (Value) O << ", ";
71 } while (Value);
72}
73
74/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
75/// value.
76static unsigned SizeULEB128(unsigned Value) {
77 unsigned Size = 0;
78 do {
79 Value >>= 7;
80 Size += sizeof(int8_t);
81 } while (Value);
82 return Size;
83}
84
85/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
86/// representing a signed leb128 value.
87static void PrintSLEB128(std::ostream &O, int Value) {
88 int Sign = Value >> (8 * sizeof(Value) - 1);
89 bool IsMore;
90
91 do {
92 unsigned Byte = Value & 0x7f;
93 Value >>= 7;
94 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
95 if (IsMore) Byte |= 0x80;
96 O << "0x" << std::hex << Byte << std::dec;
97 if (IsMore) O << ", ";
98 } while (IsMore);
99}
100
101/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
102/// value.
103static unsigned SizeSLEB128(int Value) {
104 unsigned Size = 0;
105 int Sign = Value >> (8 * sizeof(Value) - 1);
106 bool IsMore;
107
108 do {
109 unsigned Byte = Value & 0x7f;
110 Value >>= 7;
111 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
112 Size += sizeof(int8_t);
113 } while (IsMore);
114 return Size;
115}
116
117//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000118/// DWLabel - Labels are used to track locations in the assembler file.
119/// Labels appear in the form <prefix>debug_<Tag><Number>, where the tag is a
120/// category of label (Ex. location) and number is a value unique in that
121/// category.
Jim Laskey65195462006-10-30 13:35:07 +0000122class DWLabel {
123public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000124 /// Tag - Label category tag. Should always be a staticly declared C string.
125 ///
126 const char *Tag;
127
128 /// Number - Value to make label unique.
129 ///
130 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +0000131
132 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Jim Laskeybd761842006-02-27 17:27:12 +0000133
Jim Laskeyef42a012006-11-02 20:12:39 +0000134 void Profile(FoldingSetNodeID &ID) const {
135 ID.AddString(std::string(Tag));
136 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +0000137 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000138
139#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000140 void print(std::ostream *O) const {
141 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000142 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000143 void print(std::ostream &O) const {
144 O << ".debug_" << Tag;
145 if (Number) O << Number;
146 }
147#endif
Jim Laskeybd761842006-02-27 17:27:12 +0000148};
149
150//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000151/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
152/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000153class DIEAbbrevData {
154private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000155 /// Attribute - Dwarf attribute code.
156 ///
157 unsigned Attribute;
158
159 /// Form - Dwarf form code.
160 ///
161 unsigned Form;
Jim Laskey0d086af2006-02-27 12:43:29 +0000162
163public:
164 DIEAbbrevData(unsigned A, unsigned F)
165 : Attribute(A)
166 , Form(F)
167 {}
168
Jim Laskeybd761842006-02-27 17:27:12 +0000169 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000170 unsigned getAttribute() const { return Attribute; }
171 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000172
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000173 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000174 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000175 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000176 ID.AddInteger(Attribute);
177 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000178 }
179};
Jim Laskey063e7652006-01-17 17:31:53 +0000180
Jim Laskey0d086af2006-02-27 12:43:29 +0000181//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000182/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
183/// information object.
184class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000185private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000186 /// Tag - Dwarf tag code.
187 ///
188 unsigned Tag;
189
190 /// Unique number for node.
191 ///
192 unsigned Number;
193
194 /// ChildrenFlag - Dwarf children flag.
195 ///
196 unsigned ChildrenFlag;
197
198 /// Data - Raw data bytes for abbreviation.
199 ///
200 std::vector<DIEAbbrevData> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000201
Jim Laskey0d086af2006-02-27 12:43:29 +0000202public:
Jim Laskey063e7652006-01-17 17:31:53 +0000203
Jim Laskey0d086af2006-02-27 12:43:29 +0000204 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000205 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000206 , ChildrenFlag(C)
207 , Data()
208 {}
209 ~DIEAbbrev() {}
210
Jim Laskeybd761842006-02-27 17:27:12 +0000211 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000212 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000213 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000214 unsigned getChildrenFlag() const { return ChildrenFlag; }
215 const std::vector<DIEAbbrevData> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000216 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000217 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000218 void setNumber(unsigned N) { Number = N; }
219
Jim Laskey0d086af2006-02-27 12:43:29 +0000220 /// AddAttribute - Adds another set of attribute information to the
221 /// abbreviation.
222 void AddAttribute(unsigned Attribute, unsigned Form) {
223 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000224 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000225
Jim Laskeyb8509c52006-03-23 18:07:55 +0000226 /// AddFirstAttribute - Adds a set of attribute information to the front
227 /// of the abbreviation.
228 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
229 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
230 }
231
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000232 /// Profile - Used to gather unique data for the abbreviation folding set.
233 ///
234 void Profile(FoldingSetNodeID &ID) {
235 ID.AddInteger(Tag);
236 ID.AddInteger(ChildrenFlag);
237
238 // For each attribute description.
239 for (unsigned i = 0, N = Data.size(); i < N; ++i)
240 Data[i].Profile(ID);
241 }
242
Jim Laskey0d086af2006-02-27 12:43:29 +0000243 /// Emit - Print the abbreviation using the specified Dwarf writer.
244 ///
Jim Laskey65195462006-10-30 13:35:07 +0000245 void Emit(const Dwarf &DW) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000246
247#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000248 void print(std::ostream *O) {
249 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000250 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000251 void print(std::ostream &O);
252 void dump();
253#endif
254};
Jim Laskey063e7652006-01-17 17:31:53 +0000255
Jim Laskey0d086af2006-02-27 12:43:29 +0000256//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000257/// DIE - A structured debug information entry. Has an abbreviation which
258/// describes it's organization.
259class DIE : public FoldingSetNode {
260protected:
261 /// Abbrev - Buffer for constructing abbreviation.
262 ///
263 DIEAbbrev Abbrev;
264
265 /// Offset - Offset in debug info section.
266 ///
267 unsigned Offset;
268
269 /// Size - Size of instance + children.
270 ///
271 unsigned Size;
272
273 /// Children DIEs.
274 ///
275 std::vector<DIE *> Children;
276
277 /// Attributes values.
278 ///
279 std::vector<DIEValue *> Values;
280
281public:
282 DIE(unsigned Tag)
283 : Abbrev(Tag, DW_CHILDREN_no)
284 , Offset(0)
285 , Size(0)
286 , Children()
287 , Values()
288 {}
289 virtual ~DIE();
290
291 // Accessors.
292 DIEAbbrev &getAbbrev() { return Abbrev; }
293 unsigned getAbbrevNumber() const {
294 return Abbrev.getNumber();
295 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000296 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000297 unsigned getOffset() const { return Offset; }
298 unsigned getSize() const { return Size; }
299 const std::vector<DIE *> &getChildren() const { return Children; }
300 const std::vector<DIEValue *> &getValues() const { return Values; }
301 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
302 void setOffset(unsigned O) { Offset = O; }
303 void setSize(unsigned S) { Size = S; }
304
305 /// AddValue - Add a value and attributes to a DIE.
306 ///
307 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
308 Abbrev.AddAttribute(Attribute, Form);
309 Values.push_back(Value);
310 }
311
312 /// SiblingOffset - Return the offset of the debug information entry's
313 /// sibling.
314 unsigned SiblingOffset() const { return Offset + Size; }
315
316 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
317 ///
318 void AddSiblingOffset();
319
320 /// AddChild - Add a child to the DIE.
321 ///
322 void AddChild(DIE *Child) {
323 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
324 Children.push_back(Child);
325 }
326
327 /// Detach - Detaches objects connected to it after copying.
328 ///
329 void Detach() {
330 Children.clear();
331 }
332
333 /// Profile - Used to gather unique data for the value folding set.
334 ///
335 void Profile(FoldingSetNodeID &ID) ;
336
337#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000338 void print(std::ostream *O, unsigned IncIndent = 0) {
339 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000340 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000341 void print(std::ostream &O, unsigned IncIndent = 0);
342 void dump();
343#endif
344};
345
346//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000347/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000348///
349class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000350public:
351 enum {
352 isInteger,
353 isString,
354 isLabel,
355 isAsIsLabel,
356 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000357 isEntry,
358 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000359 };
360
Jim Laskeyef42a012006-11-02 20:12:39 +0000361 /// Type - Type of data stored in the value.
362 ///
363 unsigned Type;
Jim Laskey0d086af2006-02-27 12:43:29 +0000364
Jim Laskeyef42a012006-11-02 20:12:39 +0000365 DIEValue(unsigned T)
366 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000367 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000368 virtual ~DIEValue() {}
369
Jim Laskeyf6733882006-11-02 21:48:18 +0000370 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000371 unsigned getType() const { return Type; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000372
Jim Laskey0d086af2006-02-27 12:43:29 +0000373 // Implement isa/cast/dyncast.
374 static bool classof(const DIEValue *) { return true; }
375
376 /// EmitValue - Emit value via the Dwarf writer.
377 ///
Jim Laskey65195462006-10-30 13:35:07 +0000378 virtual void EmitValue(const Dwarf &DW, unsigned Form) const = 0;
Jim Laskey0d086af2006-02-27 12:43:29 +0000379
380 /// SizeOf - Return the size of a value in bytes.
381 ///
Jim Laskey65195462006-10-30 13:35:07 +0000382 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const = 0;
Jim Laskeyef42a012006-11-02 20:12:39 +0000383
384 /// Profile - Used to gather unique data for the value folding set.
385 ///
386 virtual void Profile(FoldingSetNodeID &ID) = 0;
387
388#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000389 void print(std::ostream *O) {
390 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000391 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000392 virtual void print(std::ostream &O) = 0;
393 void dump();
394#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000395};
Jim Laskey063e7652006-01-17 17:31:53 +0000396
Jim Laskey0d086af2006-02-27 12:43:29 +0000397//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000398/// DWInteger - An integer value DIE.
399///
Jim Laskey0d086af2006-02-27 12:43:29 +0000400class DIEInteger : public DIEValue {
401private:
402 uint64_t Integer;
403
404public:
405 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000406
Jim Laskey0d086af2006-02-27 12:43:29 +0000407 // Implement isa/cast/dyncast.
408 static bool classof(const DIEInteger *) { return true; }
409 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
410
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000411 /// BestForm - Choose the best form for integer.
412 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000413 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
414 if (IsSigned) {
415 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
416 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
417 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
418 } else {
419 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
420 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
421 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
422 }
423 return DW_FORM_data8;
424 }
425
Jim Laskey0d086af2006-02-27 12:43:29 +0000426 /// EmitValue - Emit integer of appropriate size.
427 ///
Jim Laskey65195462006-10-30 13:35:07 +0000428 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000429
430 /// SizeOf - Determine size of integer value in bytes.
431 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000432 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
433 switch (Form) {
434 case DW_FORM_flag: // Fall thru
435 case DW_FORM_ref1: // Fall thru
436 case DW_FORM_data1: return sizeof(int8_t);
437 case DW_FORM_ref2: // Fall thru
438 case DW_FORM_data2: return sizeof(int16_t);
439 case DW_FORM_ref4: // Fall thru
440 case DW_FORM_data4: return sizeof(int32_t);
441 case DW_FORM_ref8: // Fall thru
442 case DW_FORM_data8: return sizeof(int64_t);
443 case DW_FORM_udata: return SizeULEB128(Integer);
444 case DW_FORM_sdata: return SizeSLEB128(Integer);
445 default: assert(0 && "DIE Value form not supported yet"); break;
446 }
447 return 0;
448 }
449
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, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000453 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000454 ID.AddInteger(Integer);
455 }
Jim Laskey5496f012006-11-09 14:52:14 +0000456 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000457
458#ifndef NDEBUG
459 virtual void print(std::ostream &O) {
460 O << "Int: " << (int64_t)Integer
461 << " 0x" << std::hex << Integer << std::dec;
462 }
463#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000464};
Jim Laskey063e7652006-01-17 17:31:53 +0000465
Jim Laskey0d086af2006-02-27 12:43:29 +0000466//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000467/// DIEString - A string value DIE.
468///
Jim Laskeyef42a012006-11-02 20:12:39 +0000469class DIEString : public DIEValue {
470public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000471 const std::string String;
472
473 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000474
Jim Laskey0d086af2006-02-27 12:43:29 +0000475 // Implement isa/cast/dyncast.
476 static bool classof(const DIEString *) { return true; }
477 static bool classof(const DIEValue *S) { return S->Type == isString; }
478
479 /// EmitValue - Emit string value.
480 ///
Jim Laskey65195462006-10-30 13:35:07 +0000481 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000482
483 /// SizeOf - Determine size of string value in bytes.
484 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000485 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
486 return String.size() + sizeof(char); // sizeof('\0');
487 }
488
489 /// Profile - Used to gather unique data for the value folding set.
490 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000491 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000492 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000493 ID.AddString(String);
494 }
Jim Laskey5496f012006-11-09 14:52:14 +0000495 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000496
497#ifndef NDEBUG
498 virtual void print(std::ostream &O) {
499 O << "Str: \"" << String << "\"";
500 }
501#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000502};
Jim Laskey063e7652006-01-17 17:31:53 +0000503
Jim Laskey0d086af2006-02-27 12:43:29 +0000504//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000505/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000506//
Jim Laskeyef42a012006-11-02 20:12:39 +0000507class DIEDwarfLabel : public DIEValue {
508public:
509
Jim Laskey0d086af2006-02-27 12:43:29 +0000510 const DWLabel Label;
511
512 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
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 DIEDwarfLabel *) { return true; }
516 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
517
518 /// EmitValue - Emit label value.
519 ///
Jim Laskey65195462006-10-30 13:35:07 +0000520 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000521
522 /// SizeOf - Determine size of label value in bytes.
523 ///
Jim Laskey65195462006-10-30 13:35:07 +0000524 virtual unsigned SizeOf(const Dwarf &DW, 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 &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000529 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000530 Label.Profile(ID);
531 }
Jim Laskey5496f012006-11-09 14:52:14 +0000532 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000533
534#ifndef NDEBUG
535 virtual void print(std::ostream &O) {
536 O << "Lbl: ";
537 Label.print(O);
538 }
539#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000540};
Jim Laskey063e7652006-01-17 17:31:53 +0000541
Jim Laskey063e7652006-01-17 17:31:53 +0000542
Jim Laskey0d086af2006-02-27 12:43:29 +0000543//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000544/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000545//
Jim Laskeyef42a012006-11-02 20:12:39 +0000546class DIEObjectLabel : public DIEValue {
547public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000548 const std::string Label;
549
550 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000551
Jim Laskey0d086af2006-02-27 12:43:29 +0000552 // Implement isa/cast/dyncast.
553 static bool classof(const DIEObjectLabel *) { return true; }
554 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
555
556 /// EmitValue - Emit label value.
557 ///
Jim Laskey65195462006-10-30 13:35:07 +0000558 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000559
560 /// SizeOf - Determine size of label value in bytes.
561 ///
Jim Laskey65195462006-10-30 13:35:07 +0000562 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000563
564 /// Profile - Used to gather unique data for the value folding set.
565 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000566 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000567 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000568 ID.AddString(Label);
569 }
Jim Laskey5496f012006-11-09 14:52:14 +0000570 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000571
572#ifndef NDEBUG
573 virtual void print(std::ostream &O) {
574 O << "Obj: " << Label;
575 }
576#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000577};
Jim Laskey063e7652006-01-17 17:31:53 +0000578
Jim Laskey0d086af2006-02-27 12:43:29 +0000579//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000580/// DIEDelta - A simple label difference DIE.
581///
Jim Laskeyef42a012006-11-02 20:12:39 +0000582class DIEDelta : public DIEValue {
583public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000584 const DWLabel LabelHi;
585 const DWLabel LabelLo;
586
587 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
588 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000589
Jim Laskey0d086af2006-02-27 12:43:29 +0000590 // Implement isa/cast/dyncast.
591 static bool classof(const DIEDelta *) { return true; }
592 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
593
594 /// EmitValue - Emit delta value.
595 ///
Jim Laskey65195462006-10-30 13:35:07 +0000596 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000597
598 /// SizeOf - Determine size of delta value in bytes.
599 ///
Jim Laskey65195462006-10-30 13:35:07 +0000600 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000601
602 /// Profile - Used to gather unique data for the value folding set.
603 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000604 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
605 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000606 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000607 LabelHi.Profile(ID);
608 LabelLo.Profile(ID);
609 }
Jim Laskey5496f012006-11-09 14:52:14 +0000610 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000611
612#ifndef NDEBUG
613 virtual void print(std::ostream &O) {
614 O << "Del: ";
615 LabelHi.print(O);
616 O << "-";
617 LabelLo.print(O);
618 }
619#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000620};
Jim Laskey063e7652006-01-17 17:31:53 +0000621
Jim Laskey0d086af2006-02-27 12:43:29 +0000622//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000623/// DIEntry - A pointer to another debug information entry. An instance of this
624/// class can also be used as a proxy for a debug information entry not yet
625/// defined (ie. types.)
626class DIEntry : public DIEValue {
627public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000628 DIE *Entry;
629
630 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000631
Jim Laskey0d086af2006-02-27 12:43:29 +0000632 // Implement isa/cast/dyncast.
633 static bool classof(const DIEntry *) { return true; }
634 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
635
Jim Laskeyb8509c52006-03-23 18:07:55 +0000636 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000637 ///
Jim Laskey65195462006-10-30 13:35:07 +0000638 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000639
Jim Laskeyb8509c52006-03-23 18:07:55 +0000640 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000641 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000642 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
643 return sizeof(int32_t);
644 }
645
646 /// Profile - Used to gather unique data for the value folding set.
647 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000648 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
649 ID.AddInteger(isEntry);
650 ID.AddPointer(Entry);
651 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000652 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000653 ID.AddInteger(isEntry);
654
Jim Laskeyef42a012006-11-02 20:12:39 +0000655 if (Entry) {
656 ID.AddPointer(Entry);
657 } else {
658 ID.AddPointer(this);
659 }
660 }
661
662#ifndef NDEBUG
663 virtual void print(std::ostream &O) {
664 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
665 }
666#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000667};
668
669//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000670/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000671//
Jim Laskeyef42a012006-11-02 20:12:39 +0000672class DIEBlock : public DIEValue, public DIE {
673public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000674 unsigned Size; // Size in bytes excluding size header.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000675
676 DIEBlock()
677 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000678 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000679 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000680 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000681 ~DIEBlock() {
682 }
683
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000684 // Implement isa/cast/dyncast.
685 static bool classof(const DIEBlock *) { return true; }
686 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
687
688 /// ComputeSize - calculate the size of the block.
689 ///
Jim Laskey65195462006-10-30 13:35:07 +0000690 unsigned ComputeSize(Dwarf &DW);
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000691
692 /// BestForm - Choose the best form for data.
693 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000694 unsigned BestForm() const {
695 if ((unsigned char)Size == Size) return DW_FORM_block1;
696 if ((unsigned short)Size == Size) return DW_FORM_block2;
697 if ((unsigned int)Size == Size) return DW_FORM_block4;
698 return DW_FORM_block;
699 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000700
701 /// EmitValue - Emit block data.
702 ///
Jim Laskey65195462006-10-30 13:35:07 +0000703 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000704
705 /// SizeOf - Determine size of block data in bytes.
706 ///
Jim Laskey65195462006-10-30 13:35:07 +0000707 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000708
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000709
Jim Laskeyef42a012006-11-02 20:12:39 +0000710 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000711 ///
Reid Spencer97821312006-11-02 23:56:21 +0000712 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000713 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000714 DIE::Profile(ID);
715 }
716
717#ifndef NDEBUG
718 virtual void print(std::ostream &O) {
719 O << "Blk: ";
720 DIE::print(O, 5);
721 }
722#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000723};
724
725//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000726/// CompileUnit - This dwarf writer support class manages information associate
727/// with a source file.
728class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000729private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000730 /// Desc - Compile unit debug descriptor.
731 ///
732 CompileUnitDesc *Desc;
733
734 /// ID - File identifier for source.
735 ///
736 unsigned ID;
737
738 /// Die - Compile unit debug information entry.
739 ///
740 DIE *Die;
741
742 /// DescToDieMap - Tracks the mapping of unit level debug informaton
743 /// descriptors to debug information entries.
744 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
745
746 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
747 /// descriptors to debug information entries using a DIEntry proxy.
748 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
749
750 /// Globals - A map of globally visible named entities for this unit.
751 ///
752 std::map<std::string, DIE *> Globals;
753
754 /// DiesSet - Used to uniquely define dies within the compile unit.
755 ///
756 FoldingSet<DIE> DiesSet;
757
758 /// Dies - List of all dies in the compile unit.
759 ///
760 std::vector<DIE *> Dies;
Jim Laskey0d086af2006-02-27 12:43:29 +0000761
762public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000763 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
764 : Desc(CUD)
765 , ID(I)
766 , Die(D)
767 , DescToDieMap()
768 , DescToDIEntryMap()
769 , Globals()
770 , DiesSet(InitDiesSetSize)
771 , Dies()
772 {}
773
774 ~CompileUnit() {
775 delete Die;
776
777 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
778 delete Dies[i];
779 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000780
Jim Laskeybd761842006-02-27 17:27:12 +0000781 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000782 CompileUnitDesc *getDesc() const { return Desc; }
783 unsigned getID() const { return ID; }
784 DIE* getDie() const { return Die; }
785 std::map<std::string, DIE *> &getGlobals() { return Globals; }
786
787 /// hasContent - Return true if this compile unit has something to write out.
788 ///
789 bool hasContent() const {
790 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000791 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000792
793 /// AddGlobal - Add a new global entity to the compile unit.
794 ///
795 void AddGlobal(const std::string &Name, DIE *Die) {
796 Globals[Name] = Die;
797 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000798
Jim Laskeyef42a012006-11-02 20:12:39 +0000799 /// getDieMapSlotFor - Returns the debug information entry map slot for the
800 /// specified debug descriptor.
801 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
802 return DescToDieMap[DD];
803 }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000804
Jim Laskeyef42a012006-11-02 20:12:39 +0000805 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
806 /// specified debug descriptor.
807 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DD) {
808 return DescToDIEntryMap[DD];
809 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000810
Jim Laskeyef42a012006-11-02 20:12:39 +0000811 /// AddDie - Adds or interns the DIE to the compile unit.
812 ///
813 DIE *AddDie(DIE &Buffer) {
814 FoldingSetNodeID ID;
815 Buffer.Profile(ID);
816 void *Where;
817 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
818
819 if (!Die) {
820 Die = new DIE(Buffer);
821 DiesSet.InsertNode(Die, Where);
822 this->Die->AddChild(Die);
823 Buffer.Detach();
824 }
825
826 return Die;
827 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000828};
829
Jim Laskey65195462006-10-30 13:35:07 +0000830//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000831/// Dwarf - Emits Dwarf debug and exception handling directives.
832///
Jim Laskey65195462006-10-30 13:35:07 +0000833class Dwarf {
834
835private:
836
837 //===--------------------------------------------------------------------===//
838 // Core attributes used by the Dwarf writer.
839 //
840
841 //
842 /// O - Stream to .s file.
843 ///
844 std::ostream &O;
845
846 /// Asm - Target of Dwarf emission.
847 ///
848 AsmPrinter *Asm;
849
850 /// TAI - Target Asm Printer.
851 const TargetAsmInfo *TAI;
852
853 /// TD - Target data.
854 const TargetData *TD;
855
856 /// RI - Register Information.
857 const MRegisterInfo *RI;
858
859 /// M - Current module.
860 ///
861 Module *M;
862
863 /// MF - Current machine function.
864 ///
865 MachineFunction *MF;
866
867 /// DebugInfo - Collected debug information.
868 ///
869 MachineDebugInfo *DebugInfo;
870
871 /// didInitial - Flag to indicate if initial emission has been done.
872 ///
873 bool didInitial;
874
875 /// shouldEmit - Flag to indicate if debug information should be emitted.
876 ///
877 bool shouldEmit;
878
879 /// SubprogramCount - The running count of functions being compiled.
880 ///
881 unsigned SubprogramCount;
882
883 //===--------------------------------------------------------------------===//
884 // Attributes used to construct specific Dwarf sections.
885 //
886
887 /// CompileUnits - All the compile units involved in this build. The index
888 /// of each entry in this vector corresponds to the sources in DebugInfo.
889 std::vector<CompileUnit *> CompileUnits;
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000890
Jim Laskeyef42a012006-11-02 20:12:39 +0000891 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +0000892 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000893 FoldingSet<DIEAbbrev> AbbreviationsSet;
894
895 /// Abbreviations - A list of all the unique abbreviations in use.
896 ///
897 std::vector<DIEAbbrev *> Abbreviations;
Jim Laskey65195462006-10-30 13:35:07 +0000898
Jim Laskeyef42a012006-11-02 20:12:39 +0000899 /// ValuesSet - Used to uniquely define values.
900 ///
901 FoldingSet<DIEValue> ValuesSet;
902
903 /// Values - A list of all the unique values in use.
904 ///
905 std::vector<DIEValue *> Values;
906
Jim Laskey65195462006-10-30 13:35:07 +0000907 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +0000908 ///
Jim Laskey65195462006-10-30 13:35:07 +0000909 UniqueVector<std::string> StringPool;
910
911 /// UnitMap - Map debug information descriptor to compile unit.
912 ///
913 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
914
Jim Laskey65195462006-10-30 13:35:07 +0000915 /// SectionMap - Provides a unique id per text section.
916 ///
917 UniqueVector<std::string> SectionMap;
918
919 /// SectionSourceLines - Tracks line numbers per text section.
920 ///
921 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
922
923
924public:
925
926 //===--------------------------------------------------------------------===//
927 // Emission and print routines
928 //
929
930 /// PrintHex - Print a value as a hexidecimal value.
931 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000932 void PrintHex(int Value) const {
933 O << "0x" << std::hex << Value << std::dec;
934 }
Jim Laskey65195462006-10-30 13:35:07 +0000935
936 /// EOL - Print a newline character to asm stream. If a comment is present
937 /// then it will be printed first. Comments should not contain '\n'.
Jim Laskeyef42a012006-11-02 20:12:39 +0000938 void EOL(const std::string &Comment) const {
939 if (DwarfVerbose && !Comment.empty()) {
940 O << "\t"
941 << TAI->getCommentString()
942 << " "
943 << Comment;
944 }
945 O << "\n";
946 }
Jim Laskey65195462006-10-30 13:35:07 +0000947
948 /// EmitAlign - Print a align directive.
949 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000950 void EmitAlign(unsigned Alignment) const {
951 O << TAI->getAlignDirective() << Alignment << "\n";
952 }
Jim Laskey65195462006-10-30 13:35:07 +0000953
954 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
955 /// unsigned leb128 value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000956 void EmitULEB128Bytes(unsigned Value) const {
957 if (TAI->hasLEB128()) {
958 O << "\t.uleb128\t"
959 << Value;
960 } else {
961 O << TAI->getData8bitsDirective();
962 PrintULEB128(O, Value);
963 }
964 }
Jim Laskey65195462006-10-30 13:35:07 +0000965
966 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
967 /// signed leb128 value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000968 void EmitSLEB128Bytes(int Value) const {
969 if (TAI->hasLEB128()) {
970 O << "\t.sleb128\t"
971 << Value;
972 } else {
973 O << TAI->getData8bitsDirective();
974 PrintSLEB128(O, Value);
975 }
976 }
Jim Laskey65195462006-10-30 13:35:07 +0000977
978 /// EmitInt8 - Emit a byte directive and value.
979 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000980 void EmitInt8(int Value) const {
981 O << TAI->getData8bitsDirective();
982 PrintHex(Value & 0xFF);
983 }
Jim Laskey65195462006-10-30 13:35:07 +0000984
985 /// EmitInt16 - Emit a short directive and value.
986 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000987 void EmitInt16(int Value) const {
988 O << TAI->getData16bitsDirective();
989 PrintHex(Value & 0xFFFF);
990 }
Jim Laskey65195462006-10-30 13:35:07 +0000991
992 /// EmitInt32 - Emit a long directive and value.
993 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000994 void EmitInt32(int Value) const {
995 O << TAI->getData32bitsDirective();
996 PrintHex(Value);
997 }
998
Jim Laskey65195462006-10-30 13:35:07 +0000999 /// EmitInt64 - Emit a long long directive and value.
1000 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001001 void EmitInt64(uint64_t Value) const {
1002 if (TAI->getData64bitsDirective()) {
1003 O << TAI->getData64bitsDirective();
1004 PrintHex(Value);
1005 } else {
1006 if (TD->isBigEndian()) {
1007 EmitInt32(unsigned(Value >> 32)); O << "\n";
1008 EmitInt32(unsigned(Value));
1009 } else {
1010 EmitInt32(unsigned(Value)); O << "\n";
1011 EmitInt32(unsigned(Value >> 32));
1012 }
1013 }
1014 }
1015
Jim Laskey65195462006-10-30 13:35:07 +00001016 /// EmitString - Emit a string with quotes and a null terminator.
Jim Laskeyef42a012006-11-02 20:12:39 +00001017 /// Special characters are emitted properly.
Jim Laskey65195462006-10-30 13:35:07 +00001018 /// \literal (Eg. '\t') \endliteral
Jim Laskeyef42a012006-11-02 20:12:39 +00001019 void EmitString(const std::string &String) const {
1020 O << TAI->getAsciiDirective()
1021 << "\"";
1022 for (unsigned i = 0, N = String.size(); i < N; ++i) {
1023 unsigned char C = String[i];
1024
1025 if (!isascii(C) || iscntrl(C)) {
1026 switch(C) {
1027 case '\b': O << "\\b"; break;
1028 case '\f': O << "\\f"; break;
1029 case '\n': O << "\\n"; break;
1030 case '\r': O << "\\r"; break;
1031 case '\t': O << "\\t"; break;
1032 default:
1033 O << '\\';
1034 O << char('0' + ((C >> 6) & 7));
1035 O << char('0' + ((C >> 3) & 7));
1036 O << char('0' + ((C >> 0) & 7));
1037 break;
1038 }
1039 } else if (C == '\"') {
1040 O << "\\\"";
1041 } else if (C == '\'') {
1042 O << "\\\'";
1043 } else {
1044 O << C;
1045 }
1046 }
1047 O << "\\0\"";
1048 }
Jim Laskey65195462006-10-30 13:35:07 +00001049
1050 /// PrintLabelName - Print label name in form used by Dwarf writer.
1051 ///
1052 void PrintLabelName(DWLabel Label) const {
1053 PrintLabelName(Label.Tag, Label.Number);
1054 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001055 void PrintLabelName(const char *Tag, unsigned Number) const {
1056 O << TAI->getPrivateGlobalPrefix()
1057 << "debug_"
1058 << Tag;
1059 if (Number) O << Number;
1060 }
Jim Laskey65195462006-10-30 13:35:07 +00001061
1062 /// EmitLabel - Emit location label for internal use by Dwarf.
1063 ///
1064 void EmitLabel(DWLabel Label) const {
1065 EmitLabel(Label.Tag, Label.Number);
1066 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001067 void EmitLabel(const char *Tag, unsigned Number) const {
1068 PrintLabelName(Tag, Number);
1069 O << ":\n";
1070 }
Jim Laskey65195462006-10-30 13:35:07 +00001071
1072 /// EmitReference - Emit a reference to a label.
1073 ///
1074 void EmitReference(DWLabel Label) const {
1075 EmitReference(Label.Tag, Label.Number);
1076 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001077 void EmitReference(const char *Tag, unsigned Number) const {
1078 if (TAI->getAddressSize() == 4)
1079 O << TAI->getData32bitsDirective();
1080 else
1081 O << TAI->getData64bitsDirective();
1082
1083 PrintLabelName(Tag, Number);
1084 }
1085 void EmitReference(const std::string &Name) const {
1086 if (TAI->getAddressSize() == 4)
1087 O << TAI->getData32bitsDirective();
1088 else
1089 O << TAI->getData64bitsDirective();
1090
1091 O << Name;
1092 }
Jim Laskey65195462006-10-30 13:35:07 +00001093
1094 /// EmitDifference - Emit the difference between two labels. Some
1095 /// assemblers do not behave with absolute expressions with data directives,
1096 /// so there is an option (needsSet) to use an intermediary set expression.
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001097 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
1098 bool IsSmall = false) const {
1099 EmitDifference(LabelHi.Tag, LabelHi.Number,
1100 LabelLo.Tag, LabelLo.Number,
1101 IsSmall);
Jim Laskey65195462006-10-30 13:35:07 +00001102 }
1103 void EmitDifference(const char *TagHi, unsigned NumberHi,
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001104 const char *TagLo, unsigned NumberLo,
1105 bool IsSmall = false) const {
Jim Laskeyef42a012006-11-02 20:12:39 +00001106 if (TAI->needsSet()) {
1107 static unsigned SetCounter = 0;
1108
1109 O << "\t.set\t";
1110 PrintLabelName("set", SetCounter);
1111 O << ",";
1112 PrintLabelName(TagHi, NumberHi);
1113 O << "-";
1114 PrintLabelName(TagLo, NumberLo);
1115 O << "\n";
1116
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001117 if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
Jim Laskeyef42a012006-11-02 20:12:39 +00001118 O << TAI->getData32bitsDirective();
1119 else
1120 O << TAI->getData64bitsDirective();
1121
1122 PrintLabelName("set", SetCounter);
1123
1124 ++SetCounter;
1125 } else {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001126 if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
Jim Laskeyef42a012006-11-02 20:12:39 +00001127 O << TAI->getData32bitsDirective();
1128 else
1129 O << TAI->getData64bitsDirective();
1130
1131 PrintLabelName(TagHi, NumberHi);
1132 O << "-";
1133 PrintLabelName(TagLo, NumberLo);
1134 }
1135 }
Jim Laskey65195462006-10-30 13:35:07 +00001136
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001137 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Jim Laskey65195462006-10-30 13:35:07 +00001138 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001139 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1140 // Profile the node so that we can make it unique.
1141 FoldingSetNodeID ID;
1142 Abbrev.Profile(ID);
1143
1144 // Check the set for priors.
1145 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
1146
1147 // If it's newly added.
1148 if (InSet == &Abbrev) {
1149 // Add to abbreviation list.
1150 Abbreviations.push_back(&Abbrev);
1151 // Assign the vector position + 1 as its number.
1152 Abbrev.setNumber(Abbreviations.size());
1153 } else {
1154 // Assign existing abbreviation number.
1155 Abbrev.setNumber(InSet->getNumber());
1156 }
1157 }
1158
Jim Laskey65195462006-10-30 13:35:07 +00001159 /// NewString - Add a string to the constant pool and returns a label.
1160 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001161 DWLabel NewString(const std::string &String) {
1162 unsigned StringID = StringPool.insert(String);
1163 return DWLabel("string", StringID);
1164 }
Jim Laskey65195462006-10-30 13:35:07 +00001165
Jim Laskeyef42a012006-11-02 20:12:39 +00001166 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1167 /// entry.
1168 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1169 DIEntry *Value;
1170
1171 if (Entry) {
1172 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001173 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001174 void *Where;
1175 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
1176
Jim Laskeyf6733882006-11-02 21:48:18 +00001177 if (Value) return Value;
Jim Laskeyef42a012006-11-02 20:12:39 +00001178
1179 Value = new DIEntry(Entry);
1180 ValuesSet.InsertNode(Value, Where);
1181 } else {
1182 Value = new DIEntry(Entry);
1183 }
1184
1185 Values.push_back(Value);
1186 return Value;
1187 }
1188
1189 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1190 ///
1191 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1192 Value->Entry = Entry;
1193 // Add to values set if not already there. If it is, we merely have a
1194 // duplicate in the values list (no harm.)
1195 ValuesSet.GetOrInsertNode(Value);
1196 }
1197
1198 /// AddUInt - Add an unsigned integer attribute data and value.
1199 ///
1200 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1201 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1202
1203 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001204 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001205 void *Where;
1206 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1207 if (!Value) {
1208 Value = new DIEInteger(Integer);
1209 ValuesSet.InsertNode(Value, Where);
1210 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001211 }
1212
1213 Die->AddValue(Attribute, Form, Value);
1214 }
1215
1216 /// AddSInt - Add an signed integer attribute data and value.
1217 ///
1218 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1219 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1220
1221 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001222 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001223 void *Where;
1224 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1225 if (!Value) {
1226 Value = new DIEInteger(Integer);
1227 ValuesSet.InsertNode(Value, Where);
1228 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001229 }
1230
1231 Die->AddValue(Attribute, Form, Value);
1232 }
1233
1234 /// AddString - Add a std::string attribute data and value.
1235 ///
1236 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1237 const std::string &String) {
1238 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001239 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001240 void *Where;
1241 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1242 if (!Value) {
1243 Value = new DIEString(String);
1244 ValuesSet.InsertNode(Value, Where);
1245 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001246 }
1247
1248 Die->AddValue(Attribute, Form, Value);
1249 }
1250
1251 /// AddLabel - Add a Dwarf label attribute data and value.
1252 ///
1253 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1254 const DWLabel &Label) {
1255 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001256 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001257 void *Where;
1258 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1259 if (!Value) {
1260 Value = new DIEDwarfLabel(Label);
1261 ValuesSet.InsertNode(Value, Where);
1262 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001263 }
1264
1265 Die->AddValue(Attribute, Form, Value);
1266 }
1267
1268 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1269 ///
1270 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1271 const std::string &Label) {
1272 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001273 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001274 void *Where;
1275 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1276 if (!Value) {
1277 Value = new DIEObjectLabel(Label);
1278 ValuesSet.InsertNode(Value, Where);
1279 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001280 }
1281
1282 Die->AddValue(Attribute, Form, Value);
1283 }
1284
1285 /// AddDelta - Add a label delta attribute data and value.
1286 ///
1287 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1288 const DWLabel &Hi, const DWLabel &Lo) {
1289 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001290 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001291 void *Where;
1292 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1293 if (!Value) {
1294 Value = new DIEDelta(Hi, Lo);
1295 ValuesSet.InsertNode(Value, Where);
1296 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001297 }
1298
1299 Die->AddValue(Attribute, Form, Value);
1300 }
1301
1302 /// AddDIEntry - Add a DIE attribute data and value.
1303 ///
1304 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1305 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1306 }
1307
1308 /// AddBlock - Add block data.
1309 ///
1310 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1311 Block->ComputeSize(*this);
1312 FoldingSetNodeID ID;
1313 Block->Profile(ID);
1314 void *Where;
1315 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1316 if (!Value) {
1317 Value = Block;
1318 ValuesSet.InsertNode(Value, Where);
1319 Values.push_back(Value);
1320 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001321 delete Block;
1322 }
1323
1324 Die->AddValue(Attribute, Block->BestForm(), Value);
1325 }
1326
Jim Laskey65195462006-10-30 13:35:07 +00001327private:
1328
1329 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001330 /// entry.
1331 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1332 if (File && Line) {
1333 CompileUnit *FileUnit = FindCompileUnit(File);
1334 unsigned FileID = FileUnit->getID();
1335 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1336 AddUInt(Die, DW_AT_decl_line, 0, Line);
1337 }
1338 }
Jim Laskey65195462006-10-30 13:35:07 +00001339
1340 /// AddAddress - Add an address attribute to a die based on the location
1341 /// provided.
1342 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001343 const MachineLocation &Location) {
1344 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1345 DIEBlock *Block = new DIEBlock();
1346
1347 if (Location.isRegister()) {
1348 if (Reg < 32) {
1349 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1350 } else {
1351 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1352 AddUInt(Block, 0, DW_FORM_udata, Reg);
1353 }
1354 } else {
1355 if (Reg < 32) {
1356 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1357 } else {
1358 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1359 AddUInt(Block, 0, DW_FORM_udata, Reg);
1360 }
1361 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1362 }
1363
1364 AddBlock(Die, Attribute, 0, Block);
1365 }
1366
1367 /// AddBasicType - Add a new basic type attribute to the specified entity.
1368 ///
1369 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1370 const std::string &Name,
1371 unsigned Encoding, unsigned Size) {
1372 DIE *Die = ConstructBasicType(Unit, Name, Encoding, Size);
1373 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1374 }
1375
1376 /// ConstructBasicType - Construct a new basic type.
1377 ///
1378 DIE *ConstructBasicType(CompileUnit *Unit,
1379 const std::string &Name,
1380 unsigned Encoding, unsigned Size) {
1381 DIE Buffer(DW_TAG_base_type);
1382 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1383 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1384 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1385 return Unit->AddDie(Buffer);
1386 }
1387
1388 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1389 ///
1390 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1391 DIE *Die = ConstructPointerType(Unit, Name);
1392 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1393 }
1394
1395 /// ConstructPointerType - Construct a new pointer type.
1396 ///
1397 DIE *ConstructPointerType(CompileUnit *Unit, const std::string &Name) {
1398 DIE Buffer(DW_TAG_pointer_type);
1399 AddUInt(&Buffer, DW_AT_byte_size, 0, TAI->getAddressSize());
1400 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1401 return Unit->AddDie(Buffer);
1402 }
1403
1404 /// AddType - Add a new type attribute to the specified entity.
1405 ///
1406 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1407 if (!TyDesc) {
1408 AddBasicType(Entity, Unit, "", DW_ATE_signed, 4);
1409 } else {
1410 // Check for pre-existence.
1411 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
1412
1413 // If it exists then use the existing value.
1414 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001415 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1416 return;
1417 }
1418
1419 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1420 // FIXME - Not sure why programs and variables are coming through here.
1421 // Short cut for handling subprogram types (not really a TyDesc.)
1422 AddPointerType(Entity, Unit, SubprogramTy->getName());
1423 } else if (GlobalVariableDesc *GlobalTy =
1424 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1425 // FIXME - Not sure why programs and variables are coming through here.
1426 // Short cut for handling global variable types (not really a TyDesc.)
1427 AddPointerType(Entity, Unit, GlobalTy->getName());
1428 } else {
1429 // Set up proxy.
1430 Slot = NewDIEntry();
1431
1432 // Construct type.
1433 DIE Buffer(DW_TAG_base_type);
1434 ConstructType(Buffer, TyDesc, Unit);
1435
1436 // Add debug information entry to entity and unit.
1437 DIE *Die = Unit->AddDie(Buffer);
1438 SetDIEntry(Slot, Die);
1439 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1440 }
1441 }
1442 }
1443
1444 /// ConstructType - Adds all the required attributes to the type.
1445 ///
1446 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1447 // Get core information.
1448 const std::string &Name = TyDesc->getName();
1449 uint64_t Size = TyDesc->getSize() >> 3;
1450
1451 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1452 // Fundamental types like int, float, bool
1453 Buffer.setTag(DW_TAG_base_type);
1454 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1455 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00001456 // Fetch tag.
1457 unsigned Tag = DerivedTy->getTag();
1458 // FIXME - Workaround for templates.
1459 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1460 // Pointers, typedefs et al.
1461 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00001462 // Map to main type, void will not have a type.
1463 if (TypeDesc *FromTy = DerivedTy->getFromType())
1464 AddType(&Buffer, FromTy, Unit);
1465 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1466 // Fetch tag.
1467 unsigned Tag = CompTy->getTag();
1468
1469 // Set tag accordingly.
1470 if (Tag == DW_TAG_vector_type)
1471 Buffer.setTag(DW_TAG_array_type);
1472 else
1473 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00001474
Jim Laskeyef42a012006-11-02 20:12:39 +00001475 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1476
1477 switch (Tag) {
1478 case DW_TAG_vector_type:
1479 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1480 // Fall thru
1481 case DW_TAG_array_type: {
1482 // Add element type.
1483 if (TypeDesc *FromTy = CompTy->getFromType())
1484 AddType(&Buffer, FromTy, Unit);
1485
1486 // Don't emit size attribute.
1487 Size = 0;
1488
1489 // Construct an anonymous type for index type.
1490 DIE *IndexTy = ConstructBasicType(Unit, "", DW_ATE_signed, 4);
1491
1492 // Add subranges to array type.
1493 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1494 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1495 int64_t Lo = SRD->getLo();
1496 int64_t Hi = SRD->getHi();
1497 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1498
1499 // If a range is available.
1500 if (Lo != Hi) {
1501 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1502 // Only add low if non-zero.
1503 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1504 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1505 }
1506
1507 Buffer.AddChild(Subrange);
1508 }
1509 break;
1510 }
1511 case DW_TAG_structure_type:
1512 case DW_TAG_union_type: {
1513 // Add elements to structure type.
1514 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1515 DebugInfoDesc *Element = Elements[i];
1516
1517 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1518 // Add field or base class.
1519
1520 unsigned Tag = MemberDesc->getTag();
1521
1522 // Extract the basic information.
1523 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001524 uint64_t Size = MemberDesc->getSize();
1525 uint64_t Align = MemberDesc->getAlign();
1526 uint64_t Offset = MemberDesc->getOffset();
1527
1528 // Construct member debug information entry.
1529 DIE *Member = new DIE(Tag);
1530
1531 // Add name if not "".
1532 if (!Name.empty())
1533 AddString(Member, DW_AT_name, DW_FORM_string, Name);
1534 // Add location if available.
1535 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1536
1537 // Most of the time the field info is the same as the members.
1538 uint64_t FieldSize = Size;
1539 uint64_t FieldAlign = Align;
1540 uint64_t FieldOffset = Offset;
1541
Jim Laskeyee5f9272006-12-22 20:03:42 +00001542 // Set the member type.
1543 TypeDesc *FromTy = MemberDesc->getFromType();
1544 AddType(Member, FromTy, Unit);
1545
1546 // Walk up typedefs until a real size is found.
1547 while (FromTy) {
1548 if (FromTy->getTag() != DW_TAG_typedef) {
1549 FieldSize = FromTy->getSize();
1550 FieldAlign = FromTy->getSize();
1551 break;
1552 }
1553
1554 FromTy = dyn_cast<DerivedTypeDesc>(FromTy)->getFromType();
Jim Laskeyef42a012006-11-02 20:12:39 +00001555 }
1556
1557 // Unless we have a bit field.
1558 if (Tag == DW_TAG_member && FieldSize != Size) {
1559 // Construct the alignment mask.
1560 uint64_t AlignMask = ~(FieldAlign - 1);
1561 // Determine the high bit + 1 of the declared size.
1562 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1563 // Work backwards to determine the base offset of the field.
1564 FieldOffset = HiMark - FieldSize;
1565 // Now normalize offset to the field.
1566 Offset -= FieldOffset;
1567
1568 // Maybe we need to work from the other end.
1569 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1570
1571 // Add size and offset.
1572 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1573 AddUInt(Member, DW_AT_bit_size, 0, Size);
1574 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1575 }
1576
1577 // Add computation for offset.
1578 DIEBlock *Block = new DIEBlock();
1579 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1580 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1581 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1582
1583 // Add accessibility (public default unless is base class.
1584 if (MemberDesc->isProtected()) {
1585 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1586 } else if (MemberDesc->isPrivate()) {
1587 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1588 } else if (Tag == DW_TAG_inheritance) {
1589 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1590 }
1591
1592 Buffer.AddChild(Member);
1593 } else if (GlobalVariableDesc *StaticDesc =
1594 dyn_cast<GlobalVariableDesc>(Element)) {
1595 // Add static member.
1596
1597 // Construct member debug information entry.
1598 DIE *Static = new DIE(DW_TAG_variable);
1599
1600 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001601 const std::string &Name = StaticDesc->getName();
1602 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001603 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001604 if (!LinkageName.empty()) {
1605 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1606 LinkageName);
1607 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001608
1609 // Add location.
1610 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1611
1612 // Add type.
1613 if (TypeDesc *StaticTy = StaticDesc->getType())
1614 AddType(Static, StaticTy, Unit);
1615
1616 // Add flags.
1617 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
1618 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
1619
1620 Buffer.AddChild(Static);
1621 } else if (SubprogramDesc *MethodDesc =
1622 dyn_cast<SubprogramDesc>(Element)) {
1623 // Add member function.
1624
1625 // Construct member debug information entry.
1626 DIE *Method = new DIE(DW_TAG_subprogram);
1627
1628 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001629 const std::string &Name = MethodDesc->getName();
1630 const std::string &LinkageName = MethodDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001631
Jim Laskey2172f962006-11-30 14:35:45 +00001632 AddString(Method, DW_AT_name, DW_FORM_string, Name);
1633 bool IsCTor = TyDesc->getName() == Name;
1634
1635 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001636 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001637 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001638 }
1639
1640 // Add location.
1641 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1642
1643 // Add type.
1644 if (CompositeTypeDesc *MethodTy =
1645 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1646 // Get argument information.
1647 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1648
1649 // If not a ctor.
1650 if (!IsCTor) {
1651 // Add return type.
1652 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1653 }
1654
1655 // Add arguments.
1656 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1657 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1658 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1659 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1660 Method->AddChild(Arg);
1661 }
1662 }
1663
1664 // Add flags.
1665 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
1666 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
1667
1668 Buffer.AddChild(Method);
1669 }
1670 }
1671 break;
1672 }
1673 case DW_TAG_enumeration_type: {
1674 // Add enumerators to enumeration type.
1675 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1676 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1677 const std::string &Name = ED->getName();
1678 int64_t Value = ED->getValue();
1679 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1680 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1681 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1682 Buffer.AddChild(Enumerator);
1683 }
1684
1685 break;
1686 }
1687 case DW_TAG_subroutine_type: {
1688 // Add prototype flag.
1689 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1690 // Add return type.
1691 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
1692
1693 // Add arguments.
1694 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1695 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1696 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1697 Buffer.AddChild(Arg);
1698 }
1699
1700 break;
1701 }
1702 default: break;
1703 }
1704 }
1705
1706 // Add size if non-zero (derived types don't have a size.)
1707 if (Size) AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1708 // Add name if not anonymous or intermediate type.
1709 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1710 // Add source line info if available.
1711 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
1712 }
1713
1714 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00001715 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001716 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1717 // Construct debug information entry.
1718 DIE *Die = new DIE(DW_TAG_compile_unit);
1719 AddDelta(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0),
1720 DWLabel("section_line", 0));
1721 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1722 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1723 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1724 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1725
1726 // Construct compile unit.
1727 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1728
1729 // Add Unit to compile unit map.
1730 DescToUnitMap[UnitDesc] = Unit;
1731
1732 return Unit;
1733 }
1734
Jim Laskey9d4209f2006-11-07 19:33:46 +00001735 /// GetBaseCompileUnit - Get the main compile unit.
1736 ///
1737 CompileUnit *GetBaseCompileUnit() const {
1738 CompileUnit *Unit = CompileUnits[0];
1739 assert(Unit && "Missing compile unit.");
1740 return Unit;
1741 }
1742
Jim Laskey65195462006-10-30 13:35:07 +00001743 /// FindCompileUnit - Get the compile unit for the given descriptor.
1744 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001745 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001746 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00001747 assert(Unit && "Missing compile unit.");
1748 return Unit;
1749 }
1750
1751 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00001752 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001753 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1754 // Get the compile unit context.
1755 CompileUnitDesc *UnitDesc =
1756 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001757 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001758
1759 // Check for pre-existence.
1760 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1761 if (Slot) return Slot;
1762
1763 // Get the global variable itself.
1764 GlobalVariable *GV = GVD->getGlobalVariable();
1765
Jim Laskey2172f962006-11-30 14:35:45 +00001766 const std::string &Name = GVD->getName();
1767 const std::string &FullName = GVD->getFullName();
1768 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001769 // Create the global's variable DIE.
1770 DIE *VariableDie = new DIE(DW_TAG_variable);
1771 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001772 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001773 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001774 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001775 }
1776 AddType(VariableDie, GVD->getType(), Unit);
1777 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1778
1779 // Add source line info if available.
1780 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1781
Jim Laskeyef42a012006-11-02 20:12:39 +00001782 // Add address.
1783 DIEBlock *Block = new DIEBlock();
1784 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00001785 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
1786 AddBlock(VariableDie, DW_AT_location, 0, Block);
Jim Laskeyef42a012006-11-02 20:12:39 +00001787
1788 // Add to map.
1789 Slot = VariableDie;
1790
1791 // Add to context owner.
1792 Unit->getDie()->AddChild(VariableDie);
1793
1794 // Expose as global.
1795 // FIXME - need to check external flag.
Jim Laskey2172f962006-11-30 14:35:45 +00001796 Unit->AddGlobal(FullName, VariableDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001797
1798 return VariableDie;
1799 }
Jim Laskey65195462006-10-30 13:35:07 +00001800
1801 /// NewSubprogram - Add a new subprogram DIE.
1802 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001803 DIE *NewSubprogram(SubprogramDesc *SPD) {
1804 // Get the compile unit context.
1805 CompileUnitDesc *UnitDesc =
1806 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001807 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001808
1809 // Check for pre-existence.
1810 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1811 if (Slot) return Slot;
1812
1813 // Gather the details (simplify add attribute code.)
Jim Laskey2172f962006-11-30 14:35:45 +00001814 const std::string &Name = SPD->getName();
1815 const std::string &FullName = SPD->getFullName();
1816 const std::string &LinkageName = SPD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001817 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
1818
1819 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1820 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001821 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001822 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001823 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001824 }
1825 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
1826 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, IsExternal);
1827 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
1828
1829 // Add source line info if available.
1830 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1831
1832 // Add to map.
1833 Slot = SubprogramDie;
1834
1835 // Add to context owner.
1836 Unit->getDie()->AddChild(SubprogramDie);
1837
1838 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00001839 Unit->AddGlobal(FullName, SubprogramDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001840
1841 return SubprogramDie;
1842 }
Jim Laskey65195462006-10-30 13:35:07 +00001843
1844 /// NewScopeVariable - Create a new scope variable.
1845 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001846 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1847 // Get the descriptor.
1848 VariableDesc *VD = DV->getDesc();
1849
1850 // Translate tag to proper Dwarf tag. The result variable is dropped for
1851 // now.
1852 unsigned Tag;
1853 switch (VD->getTag()) {
1854 case DW_TAG_return_variable: return NULL;
1855 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1856 case DW_TAG_auto_variable: // fall thru
1857 default: Tag = DW_TAG_variable; break;
1858 }
1859
1860 // Define variable debug information entry.
1861 DIE *VariableDie = new DIE(Tag);
1862 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1863
1864 // Add source line info if available.
1865 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1866
1867 // Add variable type.
1868 AddType(VariableDie, VD->getType(), Unit);
1869
1870 // Add variable address.
1871 MachineLocation Location;
1872 RI->getLocation(*MF, DV->getFrameIndex(), Location);
1873 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001874
Jim Laskeyef42a012006-11-02 20:12:39 +00001875 return VariableDie;
1876 }
Jim Laskey65195462006-10-30 13:35:07 +00001877
1878 /// ConstructScope - Construct the components of a scope.
1879 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001880 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00001881 unsigned ParentStartID, unsigned ParentEndID,
1882 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001883 // Add variables to scope.
1884 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1885 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1886 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1887 if (VariableDie) ParentDie->AddChild(VariableDie);
1888 }
1889
1890 // Add nested scopes.
1891 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1892 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1893 // Define the Scope debug information entry.
1894 DebugScope *Scope = Scopes[j];
1895 // FIXME - Ignore inlined functions for the time being.
1896 if (!Scope->getParent()) continue;
1897
Jim Laskey9d4209f2006-11-07 19:33:46 +00001898 unsigned StartID = DebugInfo->MappedLabel(Scope->getStartLabelID());
1899 unsigned EndID = DebugInfo->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00001900
Jim Laskey9d4209f2006-11-07 19:33:46 +00001901 // Ignore empty scopes.
1902 if (StartID == EndID && StartID != 0) continue;
1903 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00001904
Jim Laskey36729dd2006-11-29 16:55:57 +00001905 if (StartID == ParentStartID && EndID == ParentEndID) {
1906 // Just add stuff to the parent scope.
1907 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001908 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00001909 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1910
1911 // Add the scope bounds.
1912 if (StartID) {
1913 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1914 DWLabel("loc", StartID));
1915 } else {
1916 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1917 DWLabel("func_begin", SubprogramCount));
1918 }
1919 if (EndID) {
1920 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1921 DWLabel("loc", EndID));
1922 } else {
1923 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1924 DWLabel("func_end", SubprogramCount));
1925 }
1926
1927 // Add the scope contents.
1928 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
1929 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001930 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001931 }
1932 }
Jim Laskey65195462006-10-30 13:35:07 +00001933
1934 /// ConstructRootScope - Construct the scope for the subprogram.
1935 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001936 void ConstructRootScope(DebugScope *RootScope) {
1937 // Exit if there is no root scope.
1938 if (!RootScope) return;
1939
1940 // Get the subprogram debug information entry.
1941 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1942
1943 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00001944 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001945
1946 // Get the subprogram die.
1947 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
1948 assert(SPDie && "Missing subprogram descriptor");
1949
1950 // Add the function bounds.
1951 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1952 DWLabel("func_begin", SubprogramCount));
1953 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1954 DWLabel("func_end", SubprogramCount));
1955 MachineLocation Location(RI->getFrameRegister(*MF));
1956 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001957
Jim Laskey36729dd2006-11-29 16:55:57 +00001958 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001959 }
Jim Laskey65195462006-10-30 13:35:07 +00001960
Jim Laskeyef42a012006-11-02 20:12:39 +00001961 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1962 /// tools to recognize the object file contains Dwarf information.
1963 void EmitInitial() {
1964 // Check to see if we already emitted intial headers.
1965 if (didInitial) return;
1966 didInitial = true;
1967
1968 // Dwarf sections base addresses.
1969 if (TAI->getDwarfRequiresFrameSection()) {
1970 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1971 EmitLabel("section_frame", 0);
1972 }
1973 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
1974 EmitLabel("section_info", 0);
1975 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
1976 EmitLabel("section_abbrev", 0);
1977 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
1978 EmitLabel("section_aranges", 0);
1979 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
1980 EmitLabel("section_macinfo", 0);
1981 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
1982 EmitLabel("section_line", 0);
1983 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
1984 EmitLabel("section_loc", 0);
1985 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
1986 EmitLabel("section_pubnames", 0);
1987 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
1988 EmitLabel("section_str", 0);
1989 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
1990 EmitLabel("section_ranges", 0);
1991
1992 Asm->SwitchToTextSection(TAI->getTextSection());
1993 EmitLabel("text_begin", 0);
1994 Asm->SwitchToDataSection(TAI->getDataSection());
1995 EmitLabel("data_begin", 0);
1996
1997 // Emit common frame information.
1998 EmitInitialDebugFrame();
1999 }
2000
Jim Laskey65195462006-10-30 13:35:07 +00002001 /// EmitDIE - Recusively Emits a debug information entry.
2002 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002003 void EmitDIE(DIE *Die) const {
2004 // Get the abbreviation for this DIE.
2005 unsigned AbbrevNumber = Die->getAbbrevNumber();
2006 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2007
2008 O << "\n";
2009
2010 // Emit the code (index) for the abbreviation.
2011 EmitULEB128Bytes(AbbrevNumber);
2012 EOL(std::string("Abbrev [" +
2013 utostr(AbbrevNumber) +
2014 "] 0x" + utohexstr(Die->getOffset()) +
2015 ":0x" + utohexstr(Die->getSize()) + " " +
2016 TagString(Abbrev->getTag())));
2017
2018 const std::vector<DIEValue *> &Values = Die->getValues();
2019 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2020
2021 // Emit the DIE attribute values.
2022 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2023 unsigned Attr = AbbrevData[i].getAttribute();
2024 unsigned Form = AbbrevData[i].getForm();
2025 assert(Form && "Too many attributes for DIE (check abbreviation)");
2026
2027 switch (Attr) {
2028 case DW_AT_sibling: {
2029 EmitInt32(Die->SiblingOffset());
2030 break;
2031 }
2032 default: {
2033 // Emit an attribute using the defined form.
2034 Values[i]->EmitValue(*this, Form);
2035 break;
2036 }
2037 }
2038
2039 EOL(AttributeString(Attr));
2040 }
2041
2042 // Emit the DIE children if any.
2043 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2044 const std::vector<DIE *> &Children = Die->getChildren();
2045
2046 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2047 EmitDIE(Children[j]);
2048 }
2049
2050 EmitInt8(0); EOL("End Of Children Mark");
2051 }
2052 }
2053
Jim Laskey65195462006-10-30 13:35:07 +00002054 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2055 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002056 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2057 // Get the children.
2058 const std::vector<DIE *> &Children = Die->getChildren();
2059
2060 // If not last sibling and has children then add sibling offset attribute.
2061 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2062
2063 // Record the abbreviation.
2064 AssignAbbrevNumber(Die->getAbbrev());
2065
2066 // Get the abbreviation for this DIE.
2067 unsigned AbbrevNumber = Die->getAbbrevNumber();
2068 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2069
2070 // Set DIE offset
2071 Die->setOffset(Offset);
2072
2073 // Start the size with the size of abbreviation code.
2074 Offset += SizeULEB128(AbbrevNumber);
2075
2076 const std::vector<DIEValue *> &Values = Die->getValues();
2077 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2078
2079 // Size the DIE attribute values.
2080 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2081 // Size attribute value.
2082 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2083 }
2084
2085 // Size the DIE children if any.
2086 if (!Children.empty()) {
2087 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2088 "Children flag not set");
2089
2090 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2091 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2092 }
2093
2094 // End of children marker.
2095 Offset += sizeof(int8_t);
2096 }
2097
2098 Die->setSize(Offset - Die->getOffset());
2099 return Offset;
2100 }
Jim Laskey65195462006-10-30 13:35:07 +00002101
2102 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2103 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002104 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002105 // Process base compile unit.
2106 CompileUnit *Unit = GetBaseCompileUnit();
2107 // Compute size of compile unit header
2108 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2109 sizeof(int16_t) + // DWARF version number
2110 sizeof(int32_t) + // Offset Into Abbrev. Section
2111 sizeof(int8_t); // Pointer Size (in bytes)
2112 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002113 }
2114
Jim Laskey65195462006-10-30 13:35:07 +00002115 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
2116 /// frame.
2117 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Jim Laskeyef42a012006-11-02 20:12:39 +00002118 std::vector<MachineMove *> &Moves) {
2119 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
2120 MachineMove *Move = Moves[i];
Jim Laskey9d4209f2006-11-07 19:33:46 +00002121 unsigned LabelID = DebugInfo->MappedLabel(Move->getLabelID());
Jim Laskeyef42a012006-11-02 20:12:39 +00002122
2123 // Throw out move if the label is invalid.
Jim Laskey9d4209f2006-11-07 19:33:46 +00002124 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002125
2126 const MachineLocation &Dst = Move->getDestination();
2127 const MachineLocation &Src = Move->getSource();
2128
2129 // Advance row if new location.
2130 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
2131 EmitInt8(DW_CFA_advance_loc4);
2132 EOL("DW_CFA_advance_loc4");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002133 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002134 EOL("");
2135
2136 BaseLabelID = LabelID;
2137 BaseLabel = "loc";
2138 }
2139
2140 int stackGrowth =
2141 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2142 TargetFrameInfo::StackGrowsUp ?
2143 TAI->getAddressSize() : -TAI->getAddressSize();
2144
2145 // If advancing cfa.
2146 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
2147 if (!Src.isRegister()) {
2148 if (Src.getRegister() == MachineLocation::VirtualFP) {
2149 EmitInt8(DW_CFA_def_cfa_offset);
2150 EOL("DW_CFA_def_cfa_offset");
2151 } else {
2152 EmitInt8(DW_CFA_def_cfa);
2153 EOL("DW_CFA_def_cfa");
Jim Laskeyef42a012006-11-02 20:12:39 +00002154 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
2155 EOL("Register");
2156 }
2157
2158 int Offset = Src.getOffset() / stackGrowth;
2159
2160 EmitULEB128Bytes(Offset);
2161 EOL("Offset");
2162 } else {
2163 assert(0 && "Machine move no supported yet.");
2164 }
2165 } else {
2166 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
2167 int Offset = Dst.getOffset() / stackGrowth;
2168
2169 if (Offset < 0) {
2170 EmitInt8(DW_CFA_offset_extended_sf);
2171 EOL("DW_CFA_offset_extended_sf");
2172 EmitULEB128Bytes(Reg);
2173 EOL("Reg");
2174 EmitSLEB128Bytes(Offset);
2175 EOL("Offset");
2176 } else if (Reg < 64) {
2177 EmitInt8(DW_CFA_offset + Reg);
2178 EOL("DW_CFA_offset + Reg");
2179 EmitULEB128Bytes(Offset);
2180 EOL("Offset");
2181 } else {
2182 EmitInt8(DW_CFA_offset_extended);
2183 EOL("DW_CFA_offset_extended");
2184 EmitULEB128Bytes(Reg);
2185 EOL("Reg");
2186 EmitULEB128Bytes(Offset);
2187 EOL("Offset");
2188 }
2189 }
2190 }
2191 }
Jim Laskey65195462006-10-30 13:35:07 +00002192
2193 /// EmitDebugInfo - Emit the debug info section.
2194 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002195 void EmitDebugInfo() const {
2196 // Start debug info section.
2197 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2198
Jim Laskey5496f012006-11-09 14:52:14 +00002199 CompileUnit *Unit = GetBaseCompileUnit();
2200 DIE *Die = Unit->getDie();
2201 // Emit the compile units header.
2202 EmitLabel("info_begin", Unit->getID());
2203 // Emit size of content not including length itself
2204 unsigned ContentSize = Die->getSize() +
2205 sizeof(int16_t) + // DWARF version number
2206 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00002207 sizeof(int8_t) + // Pointer Size (in bytes)
2208 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Laskey5496f012006-11-09 14:52:14 +00002209
2210 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2211 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002212 EmitDifference("abbrev_begin", 0, "section_abbrev", 0, true);
Jim Laskey5496f012006-11-09 14:52:14 +00002213 EOL("Offset Into Abbrev. Section");
2214 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
2215
2216 EmitDIE(Die);
Jim Laskey749b01d2006-11-30 11:09:42 +00002217 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
2218 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
2219 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
2220 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
Jim Laskey5496f012006-11-09 14:52:14 +00002221 EmitLabel("info_end", Unit->getID());
2222
2223 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002224 }
2225
Jim Laskey65195462006-10-30 13:35:07 +00002226 /// EmitAbbreviations - Emit the abbreviation section.
2227 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002228 void EmitAbbreviations() const {
2229 // Check to see if it is worth the effort.
2230 if (!Abbreviations.empty()) {
2231 // Start the debug abbrev section.
2232 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2233
2234 EmitLabel("abbrev_begin", 0);
2235
2236 // For each abbrevation.
2237 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2238 // Get abbreviation data
2239 const DIEAbbrev *Abbrev = Abbreviations[i];
2240
2241 // Emit the abbrevations code (base 1 index.)
2242 EmitULEB128Bytes(Abbrev->getNumber()); EOL("Abbreviation Code");
2243
2244 // Emit the abbreviations data.
2245 Abbrev->Emit(*this);
2246
2247 O << "\n";
2248 }
2249
2250 EmitLabel("abbrev_end", 0);
2251
2252 O << "\n";
2253 }
2254 }
2255
Jim Laskey65195462006-10-30 13:35:07 +00002256 /// EmitDebugLines - Emit source line information.
2257 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002258 void EmitDebugLines() const {
2259 // Minimum line delta, thus ranging from -10..(255-10).
2260 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2261 // Maximum line delta, thus ranging from -10..(255-10).
2262 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002263
Jim Laskeyef42a012006-11-02 20:12:39 +00002264 // Start the dwarf line section.
2265 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2266
2267 // Construct the section header.
2268
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002269 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002270 EOL("Length of Source Line Info");
2271 EmitLabel("line_begin", 0);
2272
2273 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2274
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002275 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002276 EOL("Prolog Length");
2277 EmitLabel("line_prolog_begin", 0);
2278
2279 EmitInt8(1); EOL("Minimum Instruction Length");
2280
2281 EmitInt8(1); EOL("Default is_stmt_start flag");
2282
2283 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
2284
2285 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
2286
2287 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
2288
2289 // Line number standard opcode encodings argument count
2290 EmitInt8(0); EOL("DW_LNS_copy arg count");
2291 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2292 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2293 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2294 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2295 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2296 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2297 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2298 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
2299
2300 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2301 const UniqueVector<SourceFileInfo>
2302 &SourceFiles = DebugInfo->getSourceFiles();
2303
2304 // Emit directories.
2305 for (unsigned DirectoryID = 1, NDID = Directories.size();
2306 DirectoryID <= NDID; ++DirectoryID) {
2307 EmitString(Directories[DirectoryID]); EOL("Directory");
2308 }
2309 EmitInt8(0); EOL("End of directories");
2310
2311 // Emit files.
2312 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2313 SourceID <= NSID; ++SourceID) {
2314 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2315 EmitString(SourceFile.getName()); EOL("Source");
2316 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2317 EmitULEB128Bytes(0); EOL("Mod date");
2318 EmitULEB128Bytes(0); EOL("File size");
2319 }
2320 EmitInt8(0); EOL("End of files");
2321
2322 EmitLabel("line_prolog_end", 0);
2323
2324 // A sequence for each text section.
2325 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2326 // Isolate current sections line info.
2327 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
2328
2329 if (DwarfVerbose) {
2330 O << "\t"
2331 << TAI->getCommentString() << " "
2332 << "Section "
2333 << SectionMap[j + 1].c_str() << "\n";
2334 }
2335
2336 // Dwarf assumes we start with first line of first source file.
2337 unsigned Source = 1;
2338 unsigned Line = 1;
2339
2340 // Construct rows of the address, source, line, column matrix.
2341 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2342 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey9d4209f2006-11-07 19:33:46 +00002343 unsigned LabelID = DebugInfo->MappedLabel(LineInfo.getLabelID());
2344 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002345
2346 if (DwarfVerbose) {
2347 unsigned SourceID = LineInfo.getSourceID();
2348 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2349 unsigned DirectoryID = SourceFile.getDirectoryID();
2350 O << "\t"
2351 << TAI->getCommentString() << " "
2352 << Directories[DirectoryID]
2353 << SourceFile.getName() << ":"
2354 << LineInfo.getLine() << "\n";
2355 }
2356
2357 // Define the line address.
2358 EmitInt8(0); EOL("Extended Op");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002359 EmitInt8(TAI->getAddressSize() + 1); EOL("Op size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002360 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2361 EmitReference("loc", LabelID); EOL("Location label");
2362
2363 // If change of source, then switch to the new source.
2364 if (Source != LineInfo.getSourceID()) {
2365 Source = LineInfo.getSourceID();
2366 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2367 EmitULEB128Bytes(Source); EOL("New Source");
2368 }
2369
2370 // If change of line.
2371 if (Line != LineInfo.getLine()) {
2372 // Determine offset.
2373 int Offset = LineInfo.getLine() - Line;
2374 int Delta = Offset - MinLineDelta;
2375
2376 // Update line.
2377 Line = LineInfo.getLine();
2378
2379 // If delta is small enough and in range...
2380 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2381 // ... then use fast opcode.
2382 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2383 } else {
2384 // ... otherwise use long hand.
2385 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2386 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2387 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2388 }
2389 } else {
2390 // Copy the previous row (different address or source)
2391 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2392 }
2393 }
2394
2395 // Define last address of section.
2396 EmitInt8(0); EOL("Extended Op");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002397 EmitInt8(TAI->getAddressSize() + 1); EOL("Op size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002398 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2399 EmitReference("section_end", j + 1); EOL("Section end label");
2400
2401 // Mark end of matrix.
2402 EmitInt8(0); EOL("DW_LNE_end_sequence");
2403 EmitULEB128Bytes(1); O << "\n";
2404 EmitInt8(1); O << "\n";
2405 }
2406
2407 EmitLabel("line_end", 0);
2408
2409 O << "\n";
2410 }
2411
Jim Laskey65195462006-10-30 13:35:07 +00002412 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
2413 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002414 void EmitInitialDebugFrame() {
2415 if (!TAI->getDwarfRequiresFrameSection())
2416 return;
2417
2418 int stackGrowth =
2419 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2420 TargetFrameInfo::StackGrowsUp ?
2421 TAI->getAddressSize() : -TAI->getAddressSize();
2422
2423 // Start the dwarf frame section.
2424 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2425
2426 EmitLabel("frame_common", 0);
2427 EmitDifference("frame_common_end", 0,
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002428 "frame_common_begin", 0, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002429 EOL("Length of Common Information Entry");
2430
2431 EmitLabel("frame_common_begin", 0);
2432 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2433 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2434 EmitString(""); EOL("CIE Augmentation");
2435 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
2436 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
2437 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
Jim Laskey65195462006-10-30 13:35:07 +00002438
Jim Laskeyef42a012006-11-02 20:12:39 +00002439 std::vector<MachineMove *> Moves;
2440 RI->getInitialFrameState(Moves);
2441 EmitFrameMoves(NULL, 0, Moves);
2442 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2443
2444 EmitAlign(2);
2445 EmitLabel("frame_common_end", 0);
2446
2447 O << "\n";
2448 }
2449
Jim Laskey65195462006-10-30 13:35:07 +00002450 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2451 /// section.
Jim Laskeyef42a012006-11-02 20:12:39 +00002452 void EmitFunctionDebugFrame() {
Reid Spencer5a4951e2006-11-07 06:36:36 +00002453 if (!TAI->getDwarfRequiresFrameSection())
2454 return;
Jim Laskey9d4209f2006-11-07 19:33:46 +00002455
Jim Laskeyef42a012006-11-02 20:12:39 +00002456 // Start the dwarf frame section.
2457 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2458
2459 EmitDifference("frame_end", SubprogramCount,
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002460 "frame_begin", SubprogramCount, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002461 EOL("Length of Frame Information Entry");
2462
2463 EmitLabel("frame_begin", SubprogramCount);
2464
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002465 EmitDifference("frame_common", 0, "section_frame", 0, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002466 EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002467
Jim Laskeyef42a012006-11-02 20:12:39 +00002468 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2469 EmitDifference("func_end", SubprogramCount,
2470 "func_begin", SubprogramCount);
2471 EOL("FDE address range");
2472
2473 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2474
2475 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2476
2477 EmitAlign(2);
2478 EmitLabel("frame_end", SubprogramCount);
2479
2480 O << "\n";
2481 }
2482
2483 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002484 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002485 void EmitDebugPubNames() {
2486 // Start the dwarf pubnames section.
2487 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2488
Jim Laskey5496f012006-11-09 14:52:14 +00002489 CompileUnit *Unit = GetBaseCompileUnit();
2490
2491 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002492 "pubnames_begin", Unit->getID(), true);
Jim Laskey5496f012006-11-09 14:52:14 +00002493 EOL("Length of Public Names Info");
2494
2495 EmitLabel("pubnames_begin", Unit->getID());
2496
2497 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2498
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002499 EmitDifference("info_begin", Unit->getID(), "section_info", 0, true);
Jim Laskey5496f012006-11-09 14:52:14 +00002500 EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002501
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002502 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskey5496f012006-11-09 14:52:14 +00002503 EOL("Compilation Unit Length");
2504
2505 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2506
2507 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2508 GE = Globals.end();
2509 GI != GE; ++GI) {
2510 const std::string &Name = GI->first;
2511 DIE * Entity = GI->second;
Jim Laskeyef42a012006-11-02 20:12:39 +00002512
Jim Laskey5496f012006-11-09 14:52:14 +00002513 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2514 EmitString(Name); EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00002515 }
Jim Laskey5496f012006-11-09 14:52:14 +00002516
2517 EmitInt32(0); EOL("End Mark");
2518 EmitLabel("pubnames_end", Unit->getID());
2519
2520 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002521 }
2522
2523 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002524 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002525 void EmitDebugStr() {
2526 // Check to see if it is worth the effort.
2527 if (!StringPool.empty()) {
2528 // Start the dwarf str section.
2529 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2530
2531 // For each of strings in the string pool.
2532 for (unsigned StringID = 1, N = StringPool.size();
2533 StringID <= N; ++StringID) {
2534 // Emit a label for reference from debug information entries.
2535 EmitLabel("string", StringID);
2536 // Emit the string itself.
2537 const std::string &String = StringPool[StringID];
2538 EmitString(String); O << "\n";
2539 }
2540
2541 O << "\n";
2542 }
2543 }
2544
2545 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002546 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002547 void EmitDebugLoc() {
2548 // Start the dwarf loc section.
2549 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2550
2551 O << "\n";
2552 }
2553
2554 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002555 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002556 void EmitDebugARanges() {
2557 // Start the dwarf aranges section.
2558 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2559
2560 // FIXME - Mock up
2561 #if 0
Jim Laskey5496f012006-11-09 14:52:14 +00002562 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002563
Jim Laskey5496f012006-11-09 14:52:14 +00002564 // Don't include size of length
2565 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2566
2567 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2568
2569 EmitReference("info_begin", Unit->getID());
2570 EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002571
Jim Laskey5496f012006-11-09 14:52:14 +00002572 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002573
Jim Laskey5496f012006-11-09 14:52:14 +00002574 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002575
Jim Laskey5496f012006-11-09 14:52:14 +00002576 EmitInt16(0); EOL("Pad (1)");
2577 EmitInt16(0); EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002578
Jim Laskey5496f012006-11-09 14:52:14 +00002579 // Range 1
2580 EmitReference("text_begin", 0); EOL("Address");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002581 EmitDifference("text_end", 0, "text_begin", 0, true); EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002582
Jim Laskey5496f012006-11-09 14:52:14 +00002583 EmitInt32(0); EOL("EOM (1)");
2584 EmitInt32(0); EOL("EOM (2)");
2585
2586 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002587 #endif
2588 }
2589
2590 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002591 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002592 void EmitDebugRanges() {
2593 // Start the dwarf ranges section.
2594 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2595
2596 O << "\n";
2597 }
2598
2599 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002600 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002601 void EmitDebugMacInfo() {
2602 // Start the dwarf macinfo section.
2603 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2604
2605 O << "\n";
2606 }
2607
Jim Laskey65195462006-10-30 13:35:07 +00002608 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2609 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002610 void ConstructCompileUnitDIEs() {
2611 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
2612
2613 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00002614 unsigned ID = DebugInfo->RecordSource(CUW[i]);
2615 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002616 CompileUnits.push_back(Unit);
2617 }
2618 }
2619
Jim Laskey65195462006-10-30 13:35:07 +00002620 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2621 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002622 void ConstructGlobalDIEs() {
2623 std::vector<GlobalVariableDesc *> GlobalVariables =
2624 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
2625
2626 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2627 GlobalVariableDesc *GVD = GlobalVariables[i];
2628 NewGlobalVariable(GVD);
2629 }
2630 }
Jim Laskey65195462006-10-30 13:35:07 +00002631
2632 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2633 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002634 void ConstructSubprogramDIEs() {
2635 std::vector<SubprogramDesc *> Subprograms =
2636 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
2637
2638 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2639 SubprogramDesc *SPD = Subprograms[i];
2640 NewSubprogram(SPD);
2641 }
2642 }
Jim Laskey65195462006-10-30 13:35:07 +00002643
2644 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
2645 ///
2646 bool ShouldEmitDwarf() const { return shouldEmit; }
2647
2648public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002649 //===--------------------------------------------------------------------===//
2650 // Main entry points.
2651 //
2652 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
2653 : O(OS)
2654 , Asm(A)
2655 , TAI(T)
2656 , TD(Asm->TM.getTargetData())
2657 , RI(Asm->TM.getRegisterInfo())
2658 , M(NULL)
2659 , MF(NULL)
2660 , DebugInfo(NULL)
2661 , didInitial(false)
2662 , shouldEmit(false)
2663 , SubprogramCount(0)
2664 , CompileUnits()
2665 , AbbreviationsSet(InitAbbreviationsSetSize)
2666 , Abbreviations()
2667 , ValuesSet(InitValuesSetSize)
2668 , Values()
2669 , StringPool()
2670 , DescToUnitMap()
2671 , SectionMap()
2672 , SectionSourceLines()
2673 {
2674 }
2675 virtual ~Dwarf() {
2676 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2677 delete CompileUnits[i];
2678 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2679 delete Values[j];
2680 }
2681
Jim Laskey65195462006-10-30 13:35:07 +00002682 // Accessors.
2683 //
2684 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
2685
2686 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2687 /// created it. Set by the target AsmPrinter.
Jim Laskeyef42a012006-11-02 20:12:39 +00002688 void SetDebugInfo(MachineDebugInfo *DI) {
2689 // Make sure initial declarations are made.
2690 if (!DebugInfo && DI->hasInfo()) {
2691 DebugInfo = DI;
2692 shouldEmit = true;
2693
2694 // Emit initial sections
2695 EmitInitial();
2696
2697 // Create all the compile unit DIEs.
2698 ConstructCompileUnitDIEs();
2699
2700 // Create DIEs for each of the externally visible global variables.
2701 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002702
Jim Laskeyef42a012006-11-02 20:12:39 +00002703 // Create DIEs for each of the externally visible subprograms.
2704 ConstructSubprogramDIEs();
2705
2706 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00002707 SectionMap.insert(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002708 }
2709 }
2710
Jim Laskey65195462006-10-30 13:35:07 +00002711 /// BeginModule - Emit all Dwarf sections that should come prior to the
2712 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002713 void BeginModule(Module *M) {
2714 this->M = M;
2715
2716 if (!ShouldEmitDwarf()) return;
2717 EOL("Dwarf Begin Module");
2718 }
2719
Jim Laskey65195462006-10-30 13:35:07 +00002720 /// EndModule - Emit all Dwarf sections that should come after the content.
2721 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002722 void EndModule() {
2723 if (!ShouldEmitDwarf()) return;
2724 EOL("Dwarf End Module");
2725
2726 // Standard sections final addresses.
2727 Asm->SwitchToTextSection(TAI->getTextSection());
2728 EmitLabel("text_end", 0);
2729 Asm->SwitchToDataSection(TAI->getDataSection());
2730 EmitLabel("data_end", 0);
2731
2732 // End text sections.
2733 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2734 Asm->SwitchToTextSection(SectionMap[i].c_str());
2735 EmitLabel("section_end", i);
2736 }
2737
2738 // Compute DIE offsets and sizes.
2739 SizeAndOffsets();
2740
2741 // Emit all the DIEs into a debug info section
2742 EmitDebugInfo();
2743
2744 // Corresponding abbreviations into a abbrev section.
2745 EmitAbbreviations();
2746
2747 // Emit source line correspondence into a debug line section.
2748 EmitDebugLines();
2749
2750 // Emit info into a debug pubnames section.
2751 EmitDebugPubNames();
2752
2753 // Emit info into a debug str section.
2754 EmitDebugStr();
2755
2756 // Emit info into a debug loc section.
2757 EmitDebugLoc();
2758
2759 // Emit info into a debug aranges section.
2760 EmitDebugARanges();
2761
2762 // Emit info into a debug ranges section.
2763 EmitDebugRanges();
2764
2765 // Emit info into a debug macinfo section.
2766 EmitDebugMacInfo();
2767 }
2768
Jim Laskey65195462006-10-30 13:35:07 +00002769 /// BeginFunction - Gather pre-function debug information. Assumes being
2770 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002771 void BeginFunction(MachineFunction *MF) {
2772 this->MF = MF;
2773
2774 if (!ShouldEmitDwarf()) return;
2775 EOL("Dwarf Begin Function");
2776
2777 // Begin accumulating function debug information.
2778 DebugInfo->BeginFunction(MF);
2779
2780 // Assumes in correct section after the entry point.
2781 EmitLabel("func_begin", ++SubprogramCount);
2782 }
2783
Jim Laskey65195462006-10-30 13:35:07 +00002784 /// EndFunction - Gather and emit post-function debug information.
2785 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002786 void EndFunction() {
2787 if (!ShouldEmitDwarf()) return;
2788 EOL("Dwarf End Function");
2789
2790 // Define end label for subprogram.
2791 EmitLabel("func_end", SubprogramCount);
2792
2793 // Get function line info.
2794 const std::vector<SourceLineInfo> &LineInfos = DebugInfo->getSourceLines();
2795
2796 if (!LineInfos.empty()) {
2797 // Get section line info.
2798 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2799 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2800 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2801 // Append the function info to section info.
2802 SectionLineInfos.insert(SectionLineInfos.end(),
2803 LineInfos.begin(), LineInfos.end());
2804 }
2805
2806 // Construct scopes for subprogram.
2807 ConstructRootScope(DebugInfo->getRootScope());
2808
2809 // Emit function frame information.
2810 EmitFunctionDebugFrame();
2811
2812 // Reset the line numbers for the next function.
2813 DebugInfo->ClearLineInfo();
2814
2815 // Clear function debug information.
2816 DebugInfo->EndFunction();
2817 }
Jim Laskey65195462006-10-30 13:35:07 +00002818};
2819
Jim Laskey0d086af2006-02-27 12:43:29 +00002820} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00002821
2822//===----------------------------------------------------------------------===//
2823
Jim Laskeyd18e2892006-01-20 20:34:06 +00002824/// Emit - Print the abbreviation using the specified Dwarf writer.
2825///
Jim Laskey65195462006-10-30 13:35:07 +00002826void DIEAbbrev::Emit(const Dwarf &DW) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002827 // Emit its Dwarf tag type.
2828 DW.EmitULEB128Bytes(Tag);
2829 DW.EOL(TagString(Tag));
2830
2831 // Emit whether it has children DIEs.
2832 DW.EmitULEB128Bytes(ChildrenFlag);
2833 DW.EOL(ChildrenString(ChildrenFlag));
2834
2835 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00002836 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002837 const DIEAbbrevData &AttrData = Data[i];
2838
2839 // Emit attribute type.
2840 DW.EmitULEB128Bytes(AttrData.getAttribute());
2841 DW.EOL(AttributeString(AttrData.getAttribute()));
2842
2843 // Emit form type.
2844 DW.EmitULEB128Bytes(AttrData.getForm());
2845 DW.EOL(FormEncodingString(AttrData.getForm()));
2846 }
2847
2848 // Mark end of abbreviation.
2849 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
2850 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
2851}
2852
2853#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00002854void DIEAbbrev::print(std::ostream &O) {
2855 O << "Abbreviation @"
2856 << std::hex << (intptr_t)this << std::dec
2857 << " "
2858 << TagString(Tag)
2859 << " "
2860 << ChildrenString(ChildrenFlag)
2861 << "\n";
2862
2863 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
2864 O << " "
2865 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002866 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00002867 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002868 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002869 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00002870}
Bill Wendlinge8156192006-12-07 01:30:32 +00002871void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002872#endif
2873
2874//===----------------------------------------------------------------------===//
2875
Jim Laskeyef42a012006-11-02 20:12:39 +00002876#ifndef NDEBUG
2877void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00002878 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002879}
Jim Laskeyef42a012006-11-02 20:12:39 +00002880#endif
2881
2882//===----------------------------------------------------------------------===//
2883
Jim Laskey063e7652006-01-17 17:31:53 +00002884/// EmitValue - Emit integer of appropriate size.
2885///
Jim Laskey65195462006-10-30 13:35:07 +00002886void DIEInteger::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +00002887 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00002888 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00002889 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002890 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002891 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002892 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002893 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002894 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002895 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002896 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +00002897 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
2898 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +00002899 default: assert(0 && "DIE Value form not supported yet"); break;
2900 }
2901}
2902
Jim Laskey063e7652006-01-17 17:31:53 +00002903//===----------------------------------------------------------------------===//
2904
2905/// EmitValue - Emit string value.
2906///
Jim Laskey65195462006-10-30 13:35:07 +00002907void DIEString::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002908 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00002909}
2910
Jim Laskey063e7652006-01-17 17:31:53 +00002911//===----------------------------------------------------------------------===//
2912
2913/// EmitValue - Emit label value.
2914///
Jim Laskey65195462006-10-30 13:35:07 +00002915void DIEDwarfLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002916 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +00002917}
2918
2919/// SizeOf - Determine size of label value in bytes.
2920///
Jim Laskey65195462006-10-30 13:35:07 +00002921unsigned DIEDwarfLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002922 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002923}
Jim Laskeyef42a012006-11-02 20:12:39 +00002924
Jim Laskey063e7652006-01-17 17:31:53 +00002925//===----------------------------------------------------------------------===//
2926
Jim Laskeyd18e2892006-01-20 20:34:06 +00002927/// EmitValue - Emit label value.
2928///
Jim Laskey65195462006-10-30 13:35:07 +00002929void DIEObjectLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002930 DW.EmitReference(Label);
2931}
2932
2933/// SizeOf - Determine size of label value in bytes.
2934///
Jim Laskey65195462006-10-30 13:35:07 +00002935unsigned DIEObjectLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002936 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00002937}
2938
2939//===----------------------------------------------------------------------===//
2940
Jim Laskey063e7652006-01-17 17:31:53 +00002941/// EmitValue - Emit delta value.
2942///
Jim Laskey65195462006-10-30 13:35:07 +00002943void DIEDelta::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002944 bool IsSmall = Form == DW_FORM_data4;
2945 DW.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00002946}
2947
2948/// SizeOf - Determine size of delta value in bytes.
2949///
Jim Laskey65195462006-10-30 13:35:07 +00002950unsigned DIEDelta::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002951 if (Form == DW_FORM_data4) return 4;
Jim Laskey563321a2006-09-06 18:34:40 +00002952 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002953}
2954
2955//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00002956
Jim Laskeyb8509c52006-03-23 18:07:55 +00002957/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002958///
Jim Laskey65195462006-10-30 13:35:07 +00002959void DIEntry::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +00002960 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00002961}
Jim Laskeyd18e2892006-01-20 20:34:06 +00002962
2963//===----------------------------------------------------------------------===//
2964
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002965/// ComputeSize - calculate the size of the block.
2966///
Jim Laskey65195462006-10-30 13:35:07 +00002967unsigned DIEBlock::ComputeSize(Dwarf &DW) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002968 if (!Size) {
2969 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2970
2971 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2972 Size += Values[i]->SizeOf(DW, AbbrevData[i].getForm());
2973 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002974 }
2975 return Size;
2976}
2977
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002978/// EmitValue - Emit block data.
2979///
Jim Laskey65195462006-10-30 13:35:07 +00002980void DIEBlock::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002981 switch (Form) {
2982 case DW_FORM_block1: DW.EmitInt8(Size); break;
2983 case DW_FORM_block2: DW.EmitInt16(Size); break;
2984 case DW_FORM_block4: DW.EmitInt32(Size); break;
2985 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
2986 default: assert(0 && "Improper form for block"); break;
2987 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002988
2989 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2990
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002991 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2992 DW.EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002993 Values[i]->EmitValue(DW, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002994 }
2995}
2996
2997/// SizeOf - Determine size of block data in bytes.
2998///
Jim Laskey65195462006-10-30 13:35:07 +00002999unsigned DIEBlock::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003000 switch (Form) {
3001 case DW_FORM_block1: return Size + sizeof(int8_t);
3002 case DW_FORM_block2: return Size + sizeof(int16_t);
3003 case DW_FORM_block4: return Size + sizeof(int32_t);
Jim Laskeyef42a012006-11-02 20:12:39 +00003004 case DW_FORM_block: return Size + SizeULEB128(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003005 default: assert(0 && "Improper form for block"); break;
3006 }
3007 return 0;
3008}
3009
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003010//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003011/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00003012
3013DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003014 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00003015 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00003016}
Jim Laskeyef42a012006-11-02 20:12:39 +00003017
Jim Laskeyb8509c52006-03-23 18:07:55 +00003018/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
3019///
3020void DIE::AddSiblingOffset() {
3021 DIEInteger *DI = new DIEInteger(0);
3022 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00003023 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00003024}
3025
Jim Laskeyef42a012006-11-02 20:12:39 +00003026/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003027///
Jim Laskeyef42a012006-11-02 20:12:39 +00003028void DIE::Profile(FoldingSetNodeID &ID) {
3029 Abbrev.Profile(ID);
3030
3031 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3032 ID.AddPointer(Children[i]);
3033
3034 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3035 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003036}
Jim Laskeyef42a012006-11-02 20:12:39 +00003037
3038#ifndef NDEBUG
3039void DIE::print(std::ostream &O, unsigned IncIndent) {
3040 static unsigned IndentCount = 0;
3041 IndentCount += IncIndent;
3042 const std::string Indent(IndentCount, ' ');
3043 bool isBlock = Abbrev.getTag() == 0;
3044
3045 if (!isBlock) {
3046 O << Indent
3047 << "Die: "
3048 << "0x" << std::hex << (intptr_t)this << std::dec
3049 << ", Offset: " << Offset
3050 << ", Size: " << Size
3051 << "\n";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003052
Jim Laskeyef42a012006-11-02 20:12:39 +00003053 O << Indent
3054 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00003055 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00003056 << ChildrenString(Abbrev.getChildrenFlag());
3057 } else {
3058 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00003059 }
3060 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003061
Jim Laskeyef42a012006-11-02 20:12:39 +00003062 const std::vector<DIEAbbrevData> &Data = Abbrev.getData();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003063
Jim Laskeyef42a012006-11-02 20:12:39 +00003064 IndentCount += 2;
3065 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3066 O << Indent;
3067 if (!isBlock) {
3068 O << AttributeString(Data[i].getAttribute());
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003069 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00003070 O << "Blk[" << i << "]";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003071 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003072 O << " "
3073 << FormEncodingString(Data[i].getForm())
3074 << " ";
3075 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00003076 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00003077 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003078 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00003079
Jim Laskeyef42a012006-11-02 20:12:39 +00003080 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3081 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00003082 }
Jim Laskey063e7652006-01-17 17:31:53 +00003083
Jim Laskeyef42a012006-11-02 20:12:39 +00003084 if (!isBlock) O << "\n";
3085 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00003086}
3087
Jim Laskeyef42a012006-11-02 20:12:39 +00003088void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003089 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00003090}
Jim Laskeybd761842006-02-27 17:27:12 +00003091#endif
Jim Laskey65195462006-10-30 13:35:07 +00003092
3093//===----------------------------------------------------------------------===//
3094/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00003095///
Jim Laskey65195462006-10-30 13:35:07 +00003096
3097DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3098 const TargetAsmInfo *T) {
3099 DW = new Dwarf(OS, A, T);
3100}
3101
3102DwarfWriter::~DwarfWriter() {
3103 delete DW;
3104}
3105
3106/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
3107/// created it. Set by the target AsmPrinter.
3108void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
3109 DW->SetDebugInfo(DI);
3110}
3111
3112/// BeginModule - Emit all Dwarf sections that should come prior to the
3113/// content.
3114void DwarfWriter::BeginModule(Module *M) {
3115 DW->BeginModule(M);
3116}
3117
3118/// EndModule - Emit all Dwarf sections that should come after the content.
3119///
3120void DwarfWriter::EndModule() {
3121 DW->EndModule();
3122}
3123
3124/// BeginFunction - Gather pre-function debug information. Assumes being
3125/// emitted immediately after the function entry point.
3126void DwarfWriter::BeginFunction(MachineFunction *MF) {
3127 DW->BeginFunction(MF);
3128}
3129
3130/// EndFunction - Gather and emit post-function debug information.
3131///
3132void DwarfWriter::EndFunction() {
3133 DW->EndFunction();
3134}