blob: 5d9720ba2983d3f519e2cebe95364570c69e9a3b [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
Jim Laskey65195462006-10-30 13:35:07 +0000948 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
949 /// unsigned leb128 value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000950 void EmitULEB128Bytes(unsigned Value) const {
951 if (TAI->hasLEB128()) {
952 O << "\t.uleb128\t"
953 << Value;
954 } else {
955 O << TAI->getData8bitsDirective();
956 PrintULEB128(O, Value);
957 }
958 }
Jim Laskey65195462006-10-30 13:35:07 +0000959
960 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
961 /// signed leb128 value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000962 void EmitSLEB128Bytes(int Value) const {
963 if (TAI->hasLEB128()) {
964 O << "\t.sleb128\t"
965 << Value;
966 } else {
967 O << TAI->getData8bitsDirective();
968 PrintSLEB128(O, Value);
969 }
970 }
Jim Laskey65195462006-10-30 13:35:07 +0000971
972 /// EmitInt8 - Emit a byte directive and value.
973 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000974 void EmitInt8(int Value) const {
975 O << TAI->getData8bitsDirective();
976 PrintHex(Value & 0xFF);
977 }
Jim Laskey65195462006-10-30 13:35:07 +0000978
979 /// EmitInt16 - Emit a short directive and value.
980 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000981 void EmitInt16(int Value) const {
982 O << TAI->getData16bitsDirective();
983 PrintHex(Value & 0xFFFF);
984 }
Jim Laskey65195462006-10-30 13:35:07 +0000985
986 /// EmitInt32 - Emit a long directive and value.
987 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000988 void EmitInt32(int Value) const {
989 O << TAI->getData32bitsDirective();
990 PrintHex(Value);
991 }
992
Jim Laskey65195462006-10-30 13:35:07 +0000993 /// EmitInt64 - Emit a long long directive and value.
994 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000995 void EmitInt64(uint64_t Value) const {
996 if (TAI->getData64bitsDirective()) {
997 O << TAI->getData64bitsDirective();
998 PrintHex(Value);
999 } else {
1000 if (TD->isBigEndian()) {
1001 EmitInt32(unsigned(Value >> 32)); O << "\n";
1002 EmitInt32(unsigned(Value));
1003 } else {
1004 EmitInt32(unsigned(Value)); O << "\n";
1005 EmitInt32(unsigned(Value >> 32));
1006 }
1007 }
1008 }
1009
Jim Laskey65195462006-10-30 13:35:07 +00001010 /// EmitString - Emit a string with quotes and a null terminator.
Jim Laskeyef42a012006-11-02 20:12:39 +00001011 /// Special characters are emitted properly.
Jim Laskey65195462006-10-30 13:35:07 +00001012 /// \literal (Eg. '\t') \endliteral
Jim Laskeyef42a012006-11-02 20:12:39 +00001013 void EmitString(const std::string &String) const {
1014 O << TAI->getAsciiDirective()
1015 << "\"";
1016 for (unsigned i = 0, N = String.size(); i < N; ++i) {
1017 unsigned char C = String[i];
1018
1019 if (!isascii(C) || iscntrl(C)) {
1020 switch(C) {
1021 case '\b': O << "\\b"; break;
1022 case '\f': O << "\\f"; break;
1023 case '\n': O << "\\n"; break;
1024 case '\r': O << "\\r"; break;
1025 case '\t': O << "\\t"; break;
1026 default:
1027 O << '\\';
1028 O << char('0' + ((C >> 6) & 7));
1029 O << char('0' + ((C >> 3) & 7));
1030 O << char('0' + ((C >> 0) & 7));
1031 break;
1032 }
1033 } else if (C == '\"') {
1034 O << "\\\"";
1035 } else if (C == '\'') {
1036 O << "\\\'";
1037 } else {
1038 O << C;
1039 }
1040 }
1041 O << "\\0\"";
1042 }
Jim Laskey65195462006-10-30 13:35:07 +00001043
1044 /// PrintLabelName - Print label name in form used by Dwarf writer.
1045 ///
1046 void PrintLabelName(DWLabel Label) const {
1047 PrintLabelName(Label.Tag, Label.Number);
1048 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001049 void PrintLabelName(const char *Tag, unsigned Number) const {
1050 O << TAI->getPrivateGlobalPrefix()
1051 << "debug_"
1052 << Tag;
1053 if (Number) O << Number;
1054 }
Jim Laskey65195462006-10-30 13:35:07 +00001055
1056 /// EmitLabel - Emit location label for internal use by Dwarf.
1057 ///
1058 void EmitLabel(DWLabel Label) const {
1059 EmitLabel(Label.Tag, Label.Number);
1060 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001061 void EmitLabel(const char *Tag, unsigned Number) const {
1062 PrintLabelName(Tag, Number);
1063 O << ":\n";
1064 }
Jim Laskey65195462006-10-30 13:35:07 +00001065
1066 /// EmitReference - Emit a reference to a label.
1067 ///
1068 void EmitReference(DWLabel Label) const {
1069 EmitReference(Label.Tag, Label.Number);
1070 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001071 void EmitReference(const char *Tag, unsigned Number) const {
1072 if (TAI->getAddressSize() == 4)
1073 O << TAI->getData32bitsDirective();
1074 else
1075 O << TAI->getData64bitsDirective();
1076
1077 PrintLabelName(Tag, Number);
1078 }
1079 void EmitReference(const std::string &Name) const {
1080 if (TAI->getAddressSize() == 4)
1081 O << TAI->getData32bitsDirective();
1082 else
1083 O << TAI->getData64bitsDirective();
1084
1085 O << Name;
1086 }
Jim Laskey65195462006-10-30 13:35:07 +00001087
1088 /// EmitDifference - Emit the difference between two labels. Some
1089 /// assemblers do not behave with absolute expressions with data directives,
1090 /// so there is an option (needsSet) to use an intermediary set expression.
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001091 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
1092 bool IsSmall = false) const {
1093 EmitDifference(LabelHi.Tag, LabelHi.Number,
1094 LabelLo.Tag, LabelLo.Number,
1095 IsSmall);
Jim Laskey65195462006-10-30 13:35:07 +00001096 }
1097 void EmitDifference(const char *TagHi, unsigned NumberHi,
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001098 const char *TagLo, unsigned NumberLo,
1099 bool IsSmall = false) const {
Jim Laskeyef42a012006-11-02 20:12:39 +00001100 if (TAI->needsSet()) {
1101 static unsigned SetCounter = 0;
1102
1103 O << "\t.set\t";
1104 PrintLabelName("set", SetCounter);
1105 O << ",";
1106 PrintLabelName(TagHi, NumberHi);
1107 O << "-";
1108 PrintLabelName(TagLo, NumberLo);
1109 O << "\n";
1110
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001111 if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
Jim Laskeyef42a012006-11-02 20:12:39 +00001112 O << TAI->getData32bitsDirective();
1113 else
1114 O << TAI->getData64bitsDirective();
1115
1116 PrintLabelName("set", SetCounter);
1117
1118 ++SetCounter;
1119 } else {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001120 if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
Jim Laskeyef42a012006-11-02 20:12:39 +00001121 O << TAI->getData32bitsDirective();
1122 else
1123 O << TAI->getData64bitsDirective();
1124
1125 PrintLabelName(TagHi, NumberHi);
1126 O << "-";
1127 PrintLabelName(TagLo, NumberLo);
1128 }
1129 }
Jim Laskey65195462006-10-30 13:35:07 +00001130
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001131 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Jim Laskey65195462006-10-30 13:35:07 +00001132 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001133 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1134 // Profile the node so that we can make it unique.
1135 FoldingSetNodeID ID;
1136 Abbrev.Profile(ID);
1137
1138 // Check the set for priors.
1139 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
1140
1141 // If it's newly added.
1142 if (InSet == &Abbrev) {
1143 // Add to abbreviation list.
1144 Abbreviations.push_back(&Abbrev);
1145 // Assign the vector position + 1 as its number.
1146 Abbrev.setNumber(Abbreviations.size());
1147 } else {
1148 // Assign existing abbreviation number.
1149 Abbrev.setNumber(InSet->getNumber());
1150 }
1151 }
1152
Jim Laskey65195462006-10-30 13:35:07 +00001153 /// NewString - Add a string to the constant pool and returns a label.
1154 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001155 DWLabel NewString(const std::string &String) {
1156 unsigned StringID = StringPool.insert(String);
1157 return DWLabel("string", StringID);
1158 }
Jim Laskey65195462006-10-30 13:35:07 +00001159
Jim Laskeyef42a012006-11-02 20:12:39 +00001160 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1161 /// entry.
1162 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1163 DIEntry *Value;
1164
1165 if (Entry) {
1166 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001167 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001168 void *Where;
1169 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
1170
Jim Laskeyf6733882006-11-02 21:48:18 +00001171 if (Value) return Value;
Jim Laskeyef42a012006-11-02 20:12:39 +00001172
1173 Value = new DIEntry(Entry);
1174 ValuesSet.InsertNode(Value, Where);
1175 } else {
1176 Value = new DIEntry(Entry);
1177 }
1178
1179 Values.push_back(Value);
1180 return Value;
1181 }
1182
1183 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1184 ///
1185 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1186 Value->Entry = Entry;
1187 // Add to values set if not already there. If it is, we merely have a
1188 // duplicate in the values list (no harm.)
1189 ValuesSet.GetOrInsertNode(Value);
1190 }
1191
1192 /// AddUInt - Add an unsigned integer attribute data and value.
1193 ///
1194 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1195 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1196
1197 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001198 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001199 void *Where;
1200 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1201 if (!Value) {
1202 Value = new DIEInteger(Integer);
1203 ValuesSet.InsertNode(Value, Where);
1204 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001205 }
1206
1207 Die->AddValue(Attribute, Form, Value);
1208 }
1209
1210 /// AddSInt - Add an signed integer attribute data and value.
1211 ///
1212 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1213 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1214
1215 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001216 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001217 void *Where;
1218 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1219 if (!Value) {
1220 Value = new DIEInteger(Integer);
1221 ValuesSet.InsertNode(Value, Where);
1222 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001223 }
1224
1225 Die->AddValue(Attribute, Form, Value);
1226 }
1227
1228 /// AddString - Add a std::string attribute data and value.
1229 ///
1230 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1231 const std::string &String) {
1232 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001233 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001234 void *Where;
1235 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1236 if (!Value) {
1237 Value = new DIEString(String);
1238 ValuesSet.InsertNode(Value, Where);
1239 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001240 }
1241
1242 Die->AddValue(Attribute, Form, Value);
1243 }
1244
1245 /// AddLabel - Add a Dwarf label attribute data and value.
1246 ///
1247 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1248 const DWLabel &Label) {
1249 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001250 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001251 void *Where;
1252 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1253 if (!Value) {
1254 Value = new DIEDwarfLabel(Label);
1255 ValuesSet.InsertNode(Value, Where);
1256 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001257 }
1258
1259 Die->AddValue(Attribute, Form, Value);
1260 }
1261
1262 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1263 ///
1264 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1265 const std::string &Label) {
1266 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001267 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001268 void *Where;
1269 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1270 if (!Value) {
1271 Value = new DIEObjectLabel(Label);
1272 ValuesSet.InsertNode(Value, Where);
1273 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001274 }
1275
1276 Die->AddValue(Attribute, Form, Value);
1277 }
1278
1279 /// AddDelta - Add a label delta attribute data and value.
1280 ///
1281 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1282 const DWLabel &Hi, const DWLabel &Lo) {
1283 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001284 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001285 void *Where;
1286 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1287 if (!Value) {
1288 Value = new DIEDelta(Hi, Lo);
1289 ValuesSet.InsertNode(Value, Where);
1290 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001291 }
1292
1293 Die->AddValue(Attribute, Form, Value);
1294 }
1295
1296 /// AddDIEntry - Add a DIE attribute data and value.
1297 ///
1298 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1299 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1300 }
1301
1302 /// AddBlock - Add block data.
1303 ///
1304 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1305 Block->ComputeSize(*this);
1306 FoldingSetNodeID ID;
1307 Block->Profile(ID);
1308 void *Where;
1309 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1310 if (!Value) {
1311 Value = Block;
1312 ValuesSet.InsertNode(Value, Where);
1313 Values.push_back(Value);
1314 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001315 delete Block;
1316 }
1317
1318 Die->AddValue(Attribute, Block->BestForm(), Value);
1319 }
1320
Jim Laskey65195462006-10-30 13:35:07 +00001321private:
1322
1323 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001324 /// entry.
1325 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1326 if (File && Line) {
1327 CompileUnit *FileUnit = FindCompileUnit(File);
1328 unsigned FileID = FileUnit->getID();
1329 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1330 AddUInt(Die, DW_AT_decl_line, 0, Line);
1331 }
1332 }
Jim Laskey65195462006-10-30 13:35:07 +00001333
1334 /// AddAddress - Add an address attribute to a die based on the location
1335 /// provided.
1336 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001337 const MachineLocation &Location) {
1338 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1339 DIEBlock *Block = new DIEBlock();
1340
1341 if (Location.isRegister()) {
1342 if (Reg < 32) {
1343 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1344 } else {
1345 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1346 AddUInt(Block, 0, DW_FORM_udata, Reg);
1347 }
1348 } else {
1349 if (Reg < 32) {
1350 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1351 } else {
1352 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1353 AddUInt(Block, 0, DW_FORM_udata, Reg);
1354 }
1355 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1356 }
1357
1358 AddBlock(Die, Attribute, 0, Block);
1359 }
1360
1361 /// AddBasicType - Add a new basic type attribute to the specified entity.
1362 ///
1363 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1364 const std::string &Name,
1365 unsigned Encoding, unsigned Size) {
1366 DIE *Die = ConstructBasicType(Unit, Name, Encoding, Size);
1367 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1368 }
1369
1370 /// ConstructBasicType - Construct a new basic type.
1371 ///
1372 DIE *ConstructBasicType(CompileUnit *Unit,
1373 const std::string &Name,
1374 unsigned Encoding, unsigned Size) {
1375 DIE Buffer(DW_TAG_base_type);
1376 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1377 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1378 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1379 return Unit->AddDie(Buffer);
1380 }
1381
1382 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1383 ///
1384 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1385 DIE *Die = ConstructPointerType(Unit, Name);
1386 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1387 }
1388
1389 /// ConstructPointerType - Construct a new pointer type.
1390 ///
1391 DIE *ConstructPointerType(CompileUnit *Unit, const std::string &Name) {
1392 DIE Buffer(DW_TAG_pointer_type);
1393 AddUInt(&Buffer, DW_AT_byte_size, 0, TAI->getAddressSize());
1394 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1395 return Unit->AddDie(Buffer);
1396 }
1397
1398 /// AddType - Add a new type attribute to the specified entity.
1399 ///
1400 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1401 if (!TyDesc) {
1402 AddBasicType(Entity, Unit, "", DW_ATE_signed, 4);
1403 } else {
1404 // Check for pre-existence.
1405 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
1406
1407 // If it exists then use the existing value.
1408 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001409 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1410 return;
1411 }
1412
1413 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1414 // FIXME - Not sure why programs and variables are coming through here.
1415 // Short cut for handling subprogram types (not really a TyDesc.)
1416 AddPointerType(Entity, Unit, SubprogramTy->getName());
1417 } else if (GlobalVariableDesc *GlobalTy =
1418 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1419 // FIXME - Not sure why programs and variables are coming through here.
1420 // Short cut for handling global variable types (not really a TyDesc.)
1421 AddPointerType(Entity, Unit, GlobalTy->getName());
1422 } else {
1423 // Set up proxy.
1424 Slot = NewDIEntry();
1425
1426 // Construct type.
1427 DIE Buffer(DW_TAG_base_type);
1428 ConstructType(Buffer, TyDesc, Unit);
1429
1430 // Add debug information entry to entity and unit.
1431 DIE *Die = Unit->AddDie(Buffer);
1432 SetDIEntry(Slot, Die);
1433 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1434 }
1435 }
1436 }
1437
1438 /// ConstructType - Adds all the required attributes to the type.
1439 ///
1440 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1441 // Get core information.
1442 const std::string &Name = TyDesc->getName();
1443 uint64_t Size = TyDesc->getSize() >> 3;
1444
1445 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1446 // Fundamental types like int, float, bool
1447 Buffer.setTag(DW_TAG_base_type);
1448 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1449 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00001450 // Fetch tag.
1451 unsigned Tag = DerivedTy->getTag();
1452 // FIXME - Workaround for templates.
1453 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1454 // Pointers, typedefs et al.
1455 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00001456 // Map to main type, void will not have a type.
1457 if (TypeDesc *FromTy = DerivedTy->getFromType())
1458 AddType(&Buffer, FromTy, Unit);
1459 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1460 // Fetch tag.
1461 unsigned Tag = CompTy->getTag();
1462
1463 // Set tag accordingly.
1464 if (Tag == DW_TAG_vector_type)
1465 Buffer.setTag(DW_TAG_array_type);
1466 else
1467 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00001468
Jim Laskeyef42a012006-11-02 20:12:39 +00001469 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1470
1471 switch (Tag) {
1472 case DW_TAG_vector_type:
1473 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1474 // Fall thru
1475 case DW_TAG_array_type: {
1476 // Add element type.
1477 if (TypeDesc *FromTy = CompTy->getFromType())
1478 AddType(&Buffer, FromTy, Unit);
1479
1480 // Don't emit size attribute.
1481 Size = 0;
1482
1483 // Construct an anonymous type for index type.
1484 DIE *IndexTy = ConstructBasicType(Unit, "", DW_ATE_signed, 4);
1485
1486 // Add subranges to array type.
1487 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1488 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1489 int64_t Lo = SRD->getLo();
1490 int64_t Hi = SRD->getHi();
1491 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1492
1493 // If a range is available.
1494 if (Lo != Hi) {
1495 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1496 // Only add low if non-zero.
1497 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1498 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1499 }
1500
1501 Buffer.AddChild(Subrange);
1502 }
1503 break;
1504 }
1505 case DW_TAG_structure_type:
1506 case DW_TAG_union_type: {
1507 // Add elements to structure type.
1508 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1509 DebugInfoDesc *Element = Elements[i];
1510
1511 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1512 // Add field or base class.
1513
1514 unsigned Tag = MemberDesc->getTag();
1515
1516 // Extract the basic information.
1517 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001518 uint64_t Size = MemberDesc->getSize();
1519 uint64_t Align = MemberDesc->getAlign();
1520 uint64_t Offset = MemberDesc->getOffset();
1521
1522 // Construct member debug information entry.
1523 DIE *Member = new DIE(Tag);
1524
1525 // Add name if not "".
1526 if (!Name.empty())
1527 AddString(Member, DW_AT_name, DW_FORM_string, Name);
1528 // Add location if available.
1529 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1530
1531 // Most of the time the field info is the same as the members.
1532 uint64_t FieldSize = Size;
1533 uint64_t FieldAlign = Align;
1534 uint64_t FieldOffset = Offset;
1535
Jim Laskeyee5f9272006-12-22 20:03:42 +00001536 // Set the member type.
1537 TypeDesc *FromTy = MemberDesc->getFromType();
1538 AddType(Member, FromTy, Unit);
1539
1540 // Walk up typedefs until a real size is found.
1541 while (FromTy) {
1542 if (FromTy->getTag() != DW_TAG_typedef) {
1543 FieldSize = FromTy->getSize();
1544 FieldAlign = FromTy->getSize();
1545 break;
1546 }
1547
1548 FromTy = dyn_cast<DerivedTypeDesc>(FromTy)->getFromType();
Jim Laskeyef42a012006-11-02 20:12:39 +00001549 }
1550
1551 // Unless we have a bit field.
1552 if (Tag == DW_TAG_member && FieldSize != Size) {
1553 // Construct the alignment mask.
1554 uint64_t AlignMask = ~(FieldAlign - 1);
1555 // Determine the high bit + 1 of the declared size.
1556 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1557 // Work backwards to determine the base offset of the field.
1558 FieldOffset = HiMark - FieldSize;
1559 // Now normalize offset to the field.
1560 Offset -= FieldOffset;
1561
1562 // Maybe we need to work from the other end.
1563 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1564
1565 // Add size and offset.
1566 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1567 AddUInt(Member, DW_AT_bit_size, 0, Size);
1568 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1569 }
1570
1571 // Add computation for offset.
1572 DIEBlock *Block = new DIEBlock();
1573 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1574 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1575 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1576
1577 // Add accessibility (public default unless is base class.
1578 if (MemberDesc->isProtected()) {
1579 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1580 } else if (MemberDesc->isPrivate()) {
1581 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1582 } else if (Tag == DW_TAG_inheritance) {
1583 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1584 }
1585
1586 Buffer.AddChild(Member);
1587 } else if (GlobalVariableDesc *StaticDesc =
1588 dyn_cast<GlobalVariableDesc>(Element)) {
1589 // Add static member.
1590
1591 // Construct member debug information entry.
1592 DIE *Static = new DIE(DW_TAG_variable);
1593
1594 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001595 const std::string &Name = StaticDesc->getName();
1596 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001597 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001598 if (!LinkageName.empty()) {
1599 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1600 LinkageName);
1601 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001602
1603 // Add location.
1604 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1605
1606 // Add type.
1607 if (TypeDesc *StaticTy = StaticDesc->getType())
1608 AddType(Static, StaticTy, Unit);
1609
1610 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001611 if (!StaticDesc->isStatic())
1612 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001613 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
1614
1615 Buffer.AddChild(Static);
1616 } else if (SubprogramDesc *MethodDesc =
1617 dyn_cast<SubprogramDesc>(Element)) {
1618 // Add member function.
1619
1620 // Construct member debug information entry.
1621 DIE *Method = new DIE(DW_TAG_subprogram);
1622
1623 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001624 const std::string &Name = MethodDesc->getName();
1625 const std::string &LinkageName = MethodDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001626
Jim Laskey2172f962006-11-30 14:35:45 +00001627 AddString(Method, DW_AT_name, DW_FORM_string, Name);
1628 bool IsCTor = TyDesc->getName() == Name;
1629
1630 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001631 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001632 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001633 }
1634
1635 // Add location.
1636 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1637
1638 // Add type.
1639 if (CompositeTypeDesc *MethodTy =
1640 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1641 // Get argument information.
1642 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1643
1644 // If not a ctor.
1645 if (!IsCTor) {
1646 // Add return type.
1647 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1648 }
1649
1650 // Add arguments.
1651 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1652 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1653 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1654 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1655 Method->AddChild(Arg);
1656 }
1657 }
1658
1659 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001660 if (!MethodDesc->isStatic())
1661 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001662 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
1663
1664 Buffer.AddChild(Method);
1665 }
1666 }
1667 break;
1668 }
1669 case DW_TAG_enumeration_type: {
1670 // Add enumerators to enumeration type.
1671 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1672 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1673 const std::string &Name = ED->getName();
1674 int64_t Value = ED->getValue();
1675 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1676 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1677 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1678 Buffer.AddChild(Enumerator);
1679 }
1680
1681 break;
1682 }
1683 case DW_TAG_subroutine_type: {
1684 // Add prototype flag.
1685 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1686 // Add return type.
1687 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
1688
1689 // Add arguments.
1690 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1691 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1692 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1693 Buffer.AddChild(Arg);
1694 }
1695
1696 break;
1697 }
1698 default: break;
1699 }
1700 }
1701
1702 // Add size if non-zero (derived types don't have a size.)
1703 if (Size) AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1704 // Add name if not anonymous or intermediate type.
1705 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1706 // Add source line info if available.
1707 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
1708 }
1709
1710 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00001711 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001712 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1713 // Construct debug information entry.
1714 DIE *Die = new DIE(DW_TAG_compile_unit);
1715 AddDelta(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0),
1716 DWLabel("section_line", 0));
1717 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1718 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1719 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1720 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1721
1722 // Construct compile unit.
1723 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1724
1725 // Add Unit to compile unit map.
1726 DescToUnitMap[UnitDesc] = Unit;
1727
1728 return Unit;
1729 }
1730
Jim Laskey9d4209f2006-11-07 19:33:46 +00001731 /// GetBaseCompileUnit - Get the main compile unit.
1732 ///
1733 CompileUnit *GetBaseCompileUnit() const {
1734 CompileUnit *Unit = CompileUnits[0];
1735 assert(Unit && "Missing compile unit.");
1736 return Unit;
1737 }
1738
Jim Laskey65195462006-10-30 13:35:07 +00001739 /// FindCompileUnit - Get the compile unit for the given descriptor.
1740 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001741 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001742 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00001743 assert(Unit && "Missing compile unit.");
1744 return Unit;
1745 }
1746
1747 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00001748 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001749 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1750 // Get the compile unit context.
1751 CompileUnitDesc *UnitDesc =
1752 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001753 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001754
1755 // Check for pre-existence.
1756 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1757 if (Slot) return Slot;
1758
1759 // Get the global variable itself.
1760 GlobalVariable *GV = GVD->getGlobalVariable();
1761
Jim Laskey2172f962006-11-30 14:35:45 +00001762 const std::string &Name = GVD->getName();
1763 const std::string &FullName = GVD->getFullName();
1764 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001765 // Create the global's variable DIE.
1766 DIE *VariableDie = new DIE(DW_TAG_variable);
1767 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001768 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001769 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001770 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001771 }
Jim Laskey6488a072007-01-08 22:15:18 +00001772 AddType(VariableDie, GVD->getType(), Unit);
1773 if (!GVD->isStatic())
1774 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001775
1776 // Add source line info if available.
1777 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1778
Jim Laskeyef42a012006-11-02 20:12:39 +00001779 // Add address.
1780 DIEBlock *Block = new DIEBlock();
1781 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00001782 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
1783 AddBlock(VariableDie, DW_AT_location, 0, Block);
Jim Laskeyef42a012006-11-02 20:12:39 +00001784
1785 // Add to map.
1786 Slot = VariableDie;
1787
1788 // Add to context owner.
1789 Unit->getDie()->AddChild(VariableDie);
1790
1791 // Expose as global.
1792 // FIXME - need to check external flag.
Jim Laskey2172f962006-11-30 14:35:45 +00001793 Unit->AddGlobal(FullName, VariableDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001794
1795 return VariableDie;
1796 }
Jim Laskey65195462006-10-30 13:35:07 +00001797
1798 /// NewSubprogram - Add a new subprogram DIE.
1799 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001800 DIE *NewSubprogram(SubprogramDesc *SPD) {
1801 // Get the compile unit context.
1802 CompileUnitDesc *UnitDesc =
1803 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001804 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001805
1806 // Check for pre-existence.
1807 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1808 if (Slot) return Slot;
1809
1810 // Gather the details (simplify add attribute code.)
Jim Laskey2172f962006-11-30 14:35:45 +00001811 const std::string &Name = SPD->getName();
1812 const std::string &FullName = SPD->getFullName();
1813 const std::string &LinkageName = SPD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001814
1815 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1816 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001817 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001818 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001819 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001820 }
1821 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
Jim Laskey6488a072007-01-08 22:15:18 +00001822 if (!SPD->isStatic())
1823 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001824 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
1825
1826 // Add source line info if available.
1827 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1828
1829 // Add to map.
1830 Slot = SubprogramDie;
1831
1832 // Add to context owner.
1833 Unit->getDie()->AddChild(SubprogramDie);
1834
1835 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00001836 Unit->AddGlobal(FullName, SubprogramDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001837
1838 return SubprogramDie;
1839 }
Jim Laskey65195462006-10-30 13:35:07 +00001840
1841 /// NewScopeVariable - Create a new scope variable.
1842 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001843 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1844 // Get the descriptor.
1845 VariableDesc *VD = DV->getDesc();
1846
1847 // Translate tag to proper Dwarf tag. The result variable is dropped for
1848 // now.
1849 unsigned Tag;
1850 switch (VD->getTag()) {
1851 case DW_TAG_return_variable: return NULL;
1852 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1853 case DW_TAG_auto_variable: // fall thru
1854 default: Tag = DW_TAG_variable; break;
1855 }
1856
1857 // Define variable debug information entry.
1858 DIE *VariableDie = new DIE(Tag);
1859 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1860
1861 // Add source line info if available.
1862 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1863
1864 // Add variable type.
1865 AddType(VariableDie, VD->getType(), Unit);
1866
1867 // Add variable address.
1868 MachineLocation Location;
1869 RI->getLocation(*MF, DV->getFrameIndex(), Location);
1870 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001871
Jim Laskeyef42a012006-11-02 20:12:39 +00001872 return VariableDie;
1873 }
Jim Laskey65195462006-10-30 13:35:07 +00001874
1875 /// ConstructScope - Construct the components of a scope.
1876 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001877 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00001878 unsigned ParentStartID, unsigned ParentEndID,
1879 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001880 // Add variables to scope.
1881 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1882 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1883 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1884 if (VariableDie) ParentDie->AddChild(VariableDie);
1885 }
1886
1887 // Add nested scopes.
1888 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1889 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1890 // Define the Scope debug information entry.
1891 DebugScope *Scope = Scopes[j];
1892 // FIXME - Ignore inlined functions for the time being.
1893 if (!Scope->getParent()) continue;
1894
Jim Laskey9d4209f2006-11-07 19:33:46 +00001895 unsigned StartID = DebugInfo->MappedLabel(Scope->getStartLabelID());
1896 unsigned EndID = DebugInfo->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00001897
Jim Laskey9d4209f2006-11-07 19:33:46 +00001898 // Ignore empty scopes.
1899 if (StartID == EndID && StartID != 0) continue;
1900 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00001901
Jim Laskey36729dd2006-11-29 16:55:57 +00001902 if (StartID == ParentStartID && EndID == ParentEndID) {
1903 // Just add stuff to the parent scope.
1904 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001905 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00001906 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1907
1908 // Add the scope bounds.
1909 if (StartID) {
1910 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1911 DWLabel("loc", StartID));
1912 } else {
1913 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1914 DWLabel("func_begin", SubprogramCount));
1915 }
1916 if (EndID) {
1917 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1918 DWLabel("loc", EndID));
1919 } else {
1920 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1921 DWLabel("func_end", SubprogramCount));
1922 }
1923
1924 // Add the scope contents.
1925 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
1926 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001927 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001928 }
1929 }
Jim Laskey65195462006-10-30 13:35:07 +00001930
1931 /// ConstructRootScope - Construct the scope for the subprogram.
1932 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001933 void ConstructRootScope(DebugScope *RootScope) {
1934 // Exit if there is no root scope.
1935 if (!RootScope) return;
1936
1937 // Get the subprogram debug information entry.
1938 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1939
1940 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00001941 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001942
1943 // Get the subprogram die.
1944 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
1945 assert(SPDie && "Missing subprogram descriptor");
1946
1947 // Add the function bounds.
1948 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1949 DWLabel("func_begin", SubprogramCount));
1950 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1951 DWLabel("func_end", SubprogramCount));
1952 MachineLocation Location(RI->getFrameRegister(*MF));
1953 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001954
Jim Laskey36729dd2006-11-29 16:55:57 +00001955 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001956 }
Jim Laskey65195462006-10-30 13:35:07 +00001957
Jim Laskeyef42a012006-11-02 20:12:39 +00001958 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1959 /// tools to recognize the object file contains Dwarf information.
1960 void EmitInitial() {
1961 // Check to see if we already emitted intial headers.
1962 if (didInitial) return;
1963 didInitial = true;
1964
1965 // Dwarf sections base addresses.
1966 if (TAI->getDwarfRequiresFrameSection()) {
1967 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1968 EmitLabel("section_frame", 0);
1969 }
1970 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
1971 EmitLabel("section_info", 0);
1972 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
1973 EmitLabel("section_abbrev", 0);
1974 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
1975 EmitLabel("section_aranges", 0);
1976 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
1977 EmitLabel("section_macinfo", 0);
1978 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
1979 EmitLabel("section_line", 0);
1980 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
1981 EmitLabel("section_loc", 0);
1982 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
1983 EmitLabel("section_pubnames", 0);
1984 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
1985 EmitLabel("section_str", 0);
1986 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
1987 EmitLabel("section_ranges", 0);
1988
1989 Asm->SwitchToTextSection(TAI->getTextSection());
1990 EmitLabel("text_begin", 0);
1991 Asm->SwitchToDataSection(TAI->getDataSection());
1992 EmitLabel("data_begin", 0);
1993
1994 // Emit common frame information.
1995 EmitInitialDebugFrame();
1996 }
1997
Jim Laskey65195462006-10-30 13:35:07 +00001998 /// EmitDIE - Recusively Emits a debug information entry.
1999 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002000 void EmitDIE(DIE *Die) const {
2001 // Get the abbreviation for this DIE.
2002 unsigned AbbrevNumber = Die->getAbbrevNumber();
2003 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2004
2005 O << "\n";
2006
2007 // Emit the code (index) for the abbreviation.
2008 EmitULEB128Bytes(AbbrevNumber);
2009 EOL(std::string("Abbrev [" +
2010 utostr(AbbrevNumber) +
2011 "] 0x" + utohexstr(Die->getOffset()) +
2012 ":0x" + utohexstr(Die->getSize()) + " " +
2013 TagString(Abbrev->getTag())));
2014
2015 const std::vector<DIEValue *> &Values = Die->getValues();
2016 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2017
2018 // Emit the DIE attribute values.
2019 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2020 unsigned Attr = AbbrevData[i].getAttribute();
2021 unsigned Form = AbbrevData[i].getForm();
2022 assert(Form && "Too many attributes for DIE (check abbreviation)");
2023
2024 switch (Attr) {
2025 case DW_AT_sibling: {
2026 EmitInt32(Die->SiblingOffset());
2027 break;
2028 }
2029 default: {
2030 // Emit an attribute using the defined form.
2031 Values[i]->EmitValue(*this, Form);
2032 break;
2033 }
2034 }
2035
2036 EOL(AttributeString(Attr));
2037 }
2038
2039 // Emit the DIE children if any.
2040 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2041 const std::vector<DIE *> &Children = Die->getChildren();
2042
2043 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2044 EmitDIE(Children[j]);
2045 }
2046
2047 EmitInt8(0); EOL("End Of Children Mark");
2048 }
2049 }
2050
Jim Laskey65195462006-10-30 13:35:07 +00002051 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2052 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002053 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2054 // Get the children.
2055 const std::vector<DIE *> &Children = Die->getChildren();
2056
2057 // If not last sibling and has children then add sibling offset attribute.
2058 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2059
2060 // Record the abbreviation.
2061 AssignAbbrevNumber(Die->getAbbrev());
2062
2063 // Get the abbreviation for this DIE.
2064 unsigned AbbrevNumber = Die->getAbbrevNumber();
2065 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2066
2067 // Set DIE offset
2068 Die->setOffset(Offset);
2069
2070 // Start the size with the size of abbreviation code.
2071 Offset += SizeULEB128(AbbrevNumber);
2072
2073 const std::vector<DIEValue *> &Values = Die->getValues();
2074 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2075
2076 // Size the DIE attribute values.
2077 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2078 // Size attribute value.
2079 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2080 }
2081
2082 // Size the DIE children if any.
2083 if (!Children.empty()) {
2084 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2085 "Children flag not set");
2086
2087 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2088 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2089 }
2090
2091 // End of children marker.
2092 Offset += sizeof(int8_t);
2093 }
2094
2095 Die->setSize(Offset - Die->getOffset());
2096 return Offset;
2097 }
Jim Laskey65195462006-10-30 13:35:07 +00002098
2099 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2100 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002101 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002102 // Process base compile unit.
2103 CompileUnit *Unit = GetBaseCompileUnit();
2104 // Compute size of compile unit header
2105 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2106 sizeof(int16_t) + // DWARF version number
2107 sizeof(int32_t) + // Offset Into Abbrev. Section
2108 sizeof(int8_t); // Pointer Size (in bytes)
2109 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002110 }
2111
Jim Laskey65195462006-10-30 13:35:07 +00002112 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
2113 /// frame.
2114 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002115 std::vector<MachineMove> &Moves) {
2116 int stackGrowth =
2117 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2118 TargetFrameInfo::StackGrowsUp ?
2119 TAI->getAddressSize() : -TAI->getAddressSize();
2120
Jim Laskeyef42a012006-11-02 20:12:39 +00002121 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002122 MachineMove &Move = Moves[i];
2123 unsigned LabelID = Move.getLabelID();
Jim Laskeyef42a012006-11-02 20:12:39 +00002124
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002125 if (LabelID) {
2126 LabelID = DebugInfo->MappedLabel(LabelID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002127
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002128 // Throw out move if the label is invalid.
2129 if (!LabelID) continue;
2130 }
2131
2132 const MachineLocation &Dst = Move.getDestination();
2133 const MachineLocation &Src = Move.getSource();
Jim Laskeyef42a012006-11-02 20:12:39 +00002134
2135 // Advance row if new location.
2136 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
2137 EmitInt8(DW_CFA_advance_loc4);
2138 EOL("DW_CFA_advance_loc4");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002139 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002140 EOL("");
2141
2142 BaseLabelID = LabelID;
2143 BaseLabel = "loc";
2144 }
2145
Jim Laskeyef42a012006-11-02 20:12:39 +00002146 // If advancing cfa.
2147 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
2148 if (!Src.isRegister()) {
2149 if (Src.getRegister() == MachineLocation::VirtualFP) {
2150 EmitInt8(DW_CFA_def_cfa_offset);
2151 EOL("DW_CFA_def_cfa_offset");
2152 } else {
2153 EmitInt8(DW_CFA_def_cfa);
2154 EOL("DW_CFA_def_cfa");
Jim Laskeyef42a012006-11-02 20:12:39 +00002155 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
2156 EOL("Register");
2157 }
2158
2159 int Offset = Src.getOffset() / stackGrowth;
2160
2161 EmitULEB128Bytes(Offset);
2162 EOL("Offset");
2163 } else {
2164 assert(0 && "Machine move no supported yet.");
2165 }
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002166 } else if (Src.isRegister() &&
2167 Src.getRegister() == MachineLocation::VirtualFP) {
2168 if (Dst.isRegister()) {
2169 EmitInt8(DW_CFA_def_cfa_register);
2170 EOL("DW_CFA_def_cfa_register");
2171 EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getRegister()));
2172 EOL("Register");
2173 } else {
2174 assert(0 && "Machine move no supported yet.");
2175 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002176 } else {
2177 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
2178 int Offset = Dst.getOffset() / stackGrowth;
2179
2180 if (Offset < 0) {
2181 EmitInt8(DW_CFA_offset_extended_sf);
2182 EOL("DW_CFA_offset_extended_sf");
2183 EmitULEB128Bytes(Reg);
2184 EOL("Reg");
2185 EmitSLEB128Bytes(Offset);
2186 EOL("Offset");
2187 } else if (Reg < 64) {
2188 EmitInt8(DW_CFA_offset + Reg);
2189 EOL("DW_CFA_offset + Reg");
2190 EmitULEB128Bytes(Offset);
2191 EOL("Offset");
2192 } else {
2193 EmitInt8(DW_CFA_offset_extended);
2194 EOL("DW_CFA_offset_extended");
2195 EmitULEB128Bytes(Reg);
2196 EOL("Reg");
2197 EmitULEB128Bytes(Offset);
2198 EOL("Offset");
2199 }
2200 }
2201 }
2202 }
Jim Laskey65195462006-10-30 13:35:07 +00002203
2204 /// EmitDebugInfo - Emit the debug info section.
2205 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002206 void EmitDebugInfo() const {
2207 // Start debug info section.
2208 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2209
Jim Laskey5496f012006-11-09 14:52:14 +00002210 CompileUnit *Unit = GetBaseCompileUnit();
2211 DIE *Die = Unit->getDie();
2212 // Emit the compile units header.
2213 EmitLabel("info_begin", Unit->getID());
2214 // Emit size of content not including length itself
2215 unsigned ContentSize = Die->getSize() +
2216 sizeof(int16_t) + // DWARF version number
2217 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00002218 sizeof(int8_t) + // Pointer Size (in bytes)
2219 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Laskey5496f012006-11-09 14:52:14 +00002220
2221 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2222 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002223 EmitDifference("abbrev_begin", 0, "section_abbrev", 0, true);
Jim Laskey5496f012006-11-09 14:52:14 +00002224 EOL("Offset Into Abbrev. Section");
2225 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
2226
2227 EmitDIE(Die);
Jim Laskey749b01d2006-11-30 11:09:42 +00002228 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
2229 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
2230 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
2231 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
Jim Laskey5496f012006-11-09 14:52:14 +00002232 EmitLabel("info_end", Unit->getID());
2233
2234 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002235 }
2236
Jim Laskey65195462006-10-30 13:35:07 +00002237 /// EmitAbbreviations - Emit the abbreviation section.
2238 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002239 void EmitAbbreviations() const {
2240 // Check to see if it is worth the effort.
2241 if (!Abbreviations.empty()) {
2242 // Start the debug abbrev section.
2243 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2244
2245 EmitLabel("abbrev_begin", 0);
2246
2247 // For each abbrevation.
2248 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2249 // Get abbreviation data
2250 const DIEAbbrev *Abbrev = Abbreviations[i];
2251
2252 // Emit the abbrevations code (base 1 index.)
2253 EmitULEB128Bytes(Abbrev->getNumber()); EOL("Abbreviation Code");
2254
2255 // Emit the abbreviations data.
2256 Abbrev->Emit(*this);
2257
2258 O << "\n";
2259 }
2260
2261 EmitLabel("abbrev_end", 0);
2262
2263 O << "\n";
2264 }
2265 }
2266
Jim Laskey65195462006-10-30 13:35:07 +00002267 /// EmitDebugLines - Emit source line information.
2268 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002269 void EmitDebugLines() const {
2270 // Minimum line delta, thus ranging from -10..(255-10).
2271 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2272 // Maximum line delta, thus ranging from -10..(255-10).
2273 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002274
Jim Laskeyef42a012006-11-02 20:12:39 +00002275 // Start the dwarf line section.
2276 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2277
2278 // Construct the section header.
2279
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002280 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002281 EOL("Length of Source Line Info");
2282 EmitLabel("line_begin", 0);
2283
2284 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2285
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002286 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002287 EOL("Prolog Length");
2288 EmitLabel("line_prolog_begin", 0);
2289
2290 EmitInt8(1); EOL("Minimum Instruction Length");
2291
2292 EmitInt8(1); EOL("Default is_stmt_start flag");
2293
2294 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
2295
2296 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
2297
2298 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
2299
2300 // Line number standard opcode encodings argument count
2301 EmitInt8(0); EOL("DW_LNS_copy arg count");
2302 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2303 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2304 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2305 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2306 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2307 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2308 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2309 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
2310
2311 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2312 const UniqueVector<SourceFileInfo>
2313 &SourceFiles = DebugInfo->getSourceFiles();
2314
2315 // Emit directories.
2316 for (unsigned DirectoryID = 1, NDID = Directories.size();
2317 DirectoryID <= NDID; ++DirectoryID) {
2318 EmitString(Directories[DirectoryID]); EOL("Directory");
2319 }
2320 EmitInt8(0); EOL("End of directories");
2321
2322 // Emit files.
2323 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2324 SourceID <= NSID; ++SourceID) {
2325 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2326 EmitString(SourceFile.getName()); EOL("Source");
2327 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2328 EmitULEB128Bytes(0); EOL("Mod date");
2329 EmitULEB128Bytes(0); EOL("File size");
2330 }
2331 EmitInt8(0); EOL("End of files");
2332
2333 EmitLabel("line_prolog_end", 0);
2334
2335 // A sequence for each text section.
2336 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2337 // Isolate current sections line info.
2338 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
2339
2340 if (DwarfVerbose) {
2341 O << "\t"
2342 << TAI->getCommentString() << " "
2343 << "Section "
2344 << SectionMap[j + 1].c_str() << "\n";
2345 }
2346
2347 // Dwarf assumes we start with first line of first source file.
2348 unsigned Source = 1;
2349 unsigned Line = 1;
2350
2351 // Construct rows of the address, source, line, column matrix.
2352 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2353 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey9d4209f2006-11-07 19:33:46 +00002354 unsigned LabelID = DebugInfo->MappedLabel(LineInfo.getLabelID());
2355 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002356
2357 if (DwarfVerbose) {
2358 unsigned SourceID = LineInfo.getSourceID();
2359 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2360 unsigned DirectoryID = SourceFile.getDirectoryID();
2361 O << "\t"
2362 << TAI->getCommentString() << " "
2363 << Directories[DirectoryID]
2364 << SourceFile.getName() << ":"
2365 << LineInfo.getLine() << "\n";
2366 }
2367
2368 // Define the line address.
2369 EmitInt8(0); EOL("Extended Op");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002370 EmitInt8(TAI->getAddressSize() + 1); EOL("Op size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002371 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2372 EmitReference("loc", LabelID); EOL("Location label");
2373
2374 // If change of source, then switch to the new source.
2375 if (Source != LineInfo.getSourceID()) {
2376 Source = LineInfo.getSourceID();
2377 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2378 EmitULEB128Bytes(Source); EOL("New Source");
2379 }
2380
2381 // If change of line.
2382 if (Line != LineInfo.getLine()) {
2383 // Determine offset.
2384 int Offset = LineInfo.getLine() - Line;
2385 int Delta = Offset - MinLineDelta;
2386
2387 // Update line.
2388 Line = LineInfo.getLine();
2389
2390 // If delta is small enough and in range...
2391 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2392 // ... then use fast opcode.
2393 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2394 } else {
2395 // ... otherwise use long hand.
2396 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2397 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2398 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2399 }
2400 } else {
2401 // Copy the previous row (different address or source)
2402 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2403 }
2404 }
2405
2406 // Define last address of section.
2407 EmitInt8(0); EOL("Extended Op");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002408 EmitInt8(TAI->getAddressSize() + 1); EOL("Op size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002409 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2410 EmitReference("section_end", j + 1); EOL("Section end label");
2411
2412 // Mark end of matrix.
2413 EmitInt8(0); EOL("DW_LNE_end_sequence");
2414 EmitULEB128Bytes(1); O << "\n";
2415 EmitInt8(1); O << "\n";
2416 }
2417
2418 EmitLabel("line_end", 0);
2419
2420 O << "\n";
2421 }
2422
Jim Laskey65195462006-10-30 13:35:07 +00002423 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
2424 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002425 void EmitInitialDebugFrame() {
2426 if (!TAI->getDwarfRequiresFrameSection())
2427 return;
2428
2429 int stackGrowth =
2430 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2431 TargetFrameInfo::StackGrowsUp ?
2432 TAI->getAddressSize() : -TAI->getAddressSize();
2433
2434 // Start the dwarf frame section.
2435 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2436
2437 EmitLabel("frame_common", 0);
2438 EmitDifference("frame_common_end", 0,
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002439 "frame_common_begin", 0, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002440 EOL("Length of Common Information Entry");
2441
2442 EmitLabel("frame_common_begin", 0);
Jim Laskey1e3a5772007-01-03 13:36:40 +00002443 EmitInt32((int)DW_CIE_ID); EOL("CIE Identifier Tag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002444 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2445 EmitString(""); EOL("CIE Augmentation");
2446 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
2447 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
2448 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
Jim Laskey65195462006-10-30 13:35:07 +00002449
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002450 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002451 RI->getInitialFrameState(Moves);
2452 EmitFrameMoves(NULL, 0, Moves);
Jim Laskeyef42a012006-11-02 20:12:39 +00002453
Jim Laskeyf9e56192007-01-24 13:12:32 +00002454 Asm->EmitAlignment(2);
Jim Laskeyef42a012006-11-02 20:12:39 +00002455 EmitLabel("frame_common_end", 0);
2456
2457 O << "\n";
2458 }
2459
Jim Laskey65195462006-10-30 13:35:07 +00002460 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2461 /// section.
Jim Laskeyef42a012006-11-02 20:12:39 +00002462 void EmitFunctionDebugFrame() {
Reid Spencer5a4951e2006-11-07 06:36:36 +00002463 if (!TAI->getDwarfRequiresFrameSection())
2464 return;
Jim Laskey9d4209f2006-11-07 19:33:46 +00002465
Jim Laskeyef42a012006-11-02 20:12:39 +00002466 // Start the dwarf frame section.
2467 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2468
2469 EmitDifference("frame_end", SubprogramCount,
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002470 "frame_begin", SubprogramCount, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002471 EOL("Length of Frame Information Entry");
2472
2473 EmitLabel("frame_begin", SubprogramCount);
2474
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002475 EmitDifference("frame_common", 0, "section_frame", 0, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002476 EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002477
Jim Laskeyef42a012006-11-02 20:12:39 +00002478 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2479 EmitDifference("func_end", SubprogramCount,
2480 "func_begin", SubprogramCount);
2481 EOL("FDE address range");
2482
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002483 std::vector<MachineMove> &Moves = DebugInfo->getFrameMoves();
Jim Laskeyef42a012006-11-02 20:12:39 +00002484
2485 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2486
Jim Laskeyf9e56192007-01-24 13:12:32 +00002487 Asm->EmitAlignment(2);
Jim Laskeyef42a012006-11-02 20:12:39 +00002488 EmitLabel("frame_end", SubprogramCount);
2489
2490 O << "\n";
2491 }
2492
2493 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002494 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002495 void EmitDebugPubNames() {
2496 // Start the dwarf pubnames section.
2497 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2498
Jim Laskey5496f012006-11-09 14:52:14 +00002499 CompileUnit *Unit = GetBaseCompileUnit();
2500
2501 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002502 "pubnames_begin", Unit->getID(), true);
Jim Laskey5496f012006-11-09 14:52:14 +00002503 EOL("Length of Public Names Info");
2504
2505 EmitLabel("pubnames_begin", Unit->getID());
2506
2507 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2508
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002509 EmitDifference("info_begin", Unit->getID(), "section_info", 0, true);
Jim Laskey5496f012006-11-09 14:52:14 +00002510 EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002511
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002512 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskey5496f012006-11-09 14:52:14 +00002513 EOL("Compilation Unit Length");
2514
2515 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2516
2517 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2518 GE = Globals.end();
2519 GI != GE; ++GI) {
2520 const std::string &Name = GI->first;
2521 DIE * Entity = GI->second;
Jim Laskeyef42a012006-11-02 20:12:39 +00002522
Jim Laskey5496f012006-11-09 14:52:14 +00002523 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2524 EmitString(Name); EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00002525 }
Jim Laskey5496f012006-11-09 14:52:14 +00002526
2527 EmitInt32(0); EOL("End Mark");
2528 EmitLabel("pubnames_end", Unit->getID());
2529
2530 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002531 }
2532
2533 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002534 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002535 void EmitDebugStr() {
2536 // Check to see if it is worth the effort.
2537 if (!StringPool.empty()) {
2538 // Start the dwarf str section.
2539 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2540
2541 // For each of strings in the string pool.
2542 for (unsigned StringID = 1, N = StringPool.size();
2543 StringID <= N; ++StringID) {
2544 // Emit a label for reference from debug information entries.
2545 EmitLabel("string", StringID);
2546 // Emit the string itself.
2547 const std::string &String = StringPool[StringID];
2548 EmitString(String); O << "\n";
2549 }
2550
2551 O << "\n";
2552 }
2553 }
2554
2555 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002556 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002557 void EmitDebugLoc() {
2558 // Start the dwarf loc section.
2559 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2560
2561 O << "\n";
2562 }
2563
2564 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002565 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002566 void EmitDebugARanges() {
2567 // Start the dwarf aranges section.
2568 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2569
2570 // FIXME - Mock up
2571 #if 0
Jim Laskey5496f012006-11-09 14:52:14 +00002572 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002573
Jim Laskey5496f012006-11-09 14:52:14 +00002574 // Don't include size of length
2575 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2576
2577 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2578
2579 EmitReference("info_begin", Unit->getID());
2580 EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002581
Jim Laskey5496f012006-11-09 14:52:14 +00002582 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002583
Jim Laskey5496f012006-11-09 14:52:14 +00002584 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002585
Jim Laskey5496f012006-11-09 14:52:14 +00002586 EmitInt16(0); EOL("Pad (1)");
2587 EmitInt16(0); EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002588
Jim Laskey5496f012006-11-09 14:52:14 +00002589 // Range 1
2590 EmitReference("text_begin", 0); EOL("Address");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002591 EmitDifference("text_end", 0, "text_begin", 0, true); EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002592
Jim Laskey5496f012006-11-09 14:52:14 +00002593 EmitInt32(0); EOL("EOM (1)");
2594 EmitInt32(0); EOL("EOM (2)");
2595
2596 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002597 #endif
2598 }
2599
2600 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002601 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002602 void EmitDebugRanges() {
2603 // Start the dwarf ranges section.
2604 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2605
2606 O << "\n";
2607 }
2608
2609 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002610 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002611 void EmitDebugMacInfo() {
2612 // Start the dwarf macinfo section.
2613 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2614
2615 O << "\n";
2616 }
2617
Jim Laskey65195462006-10-30 13:35:07 +00002618 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2619 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002620 void ConstructCompileUnitDIEs() {
2621 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
2622
2623 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00002624 unsigned ID = DebugInfo->RecordSource(CUW[i]);
2625 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002626 CompileUnits.push_back(Unit);
2627 }
2628 }
2629
Jim Laskey65195462006-10-30 13:35:07 +00002630 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2631 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002632 void ConstructGlobalDIEs() {
2633 std::vector<GlobalVariableDesc *> GlobalVariables =
2634 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
2635
2636 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2637 GlobalVariableDesc *GVD = GlobalVariables[i];
2638 NewGlobalVariable(GVD);
2639 }
2640 }
Jim Laskey65195462006-10-30 13:35:07 +00002641
2642 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2643 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002644 void ConstructSubprogramDIEs() {
2645 std::vector<SubprogramDesc *> Subprograms =
2646 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
2647
2648 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2649 SubprogramDesc *SPD = Subprograms[i];
2650 NewSubprogram(SPD);
2651 }
2652 }
Jim Laskey65195462006-10-30 13:35:07 +00002653
2654 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
2655 ///
2656 bool ShouldEmitDwarf() const { return shouldEmit; }
2657
2658public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002659 //===--------------------------------------------------------------------===//
2660 // Main entry points.
2661 //
2662 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
2663 : O(OS)
2664 , Asm(A)
2665 , TAI(T)
2666 , TD(Asm->TM.getTargetData())
2667 , RI(Asm->TM.getRegisterInfo())
2668 , M(NULL)
2669 , MF(NULL)
2670 , DebugInfo(NULL)
2671 , didInitial(false)
2672 , shouldEmit(false)
2673 , SubprogramCount(0)
2674 , CompileUnits()
2675 , AbbreviationsSet(InitAbbreviationsSetSize)
2676 , Abbreviations()
2677 , ValuesSet(InitValuesSetSize)
2678 , Values()
2679 , StringPool()
2680 , DescToUnitMap()
2681 , SectionMap()
2682 , SectionSourceLines()
2683 {
2684 }
2685 virtual ~Dwarf() {
2686 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2687 delete CompileUnits[i];
2688 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2689 delete Values[j];
2690 }
2691
Jim Laskey65195462006-10-30 13:35:07 +00002692 // Accessors.
2693 //
2694 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
2695
2696 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2697 /// created it. Set by the target AsmPrinter.
Jim Laskeyef42a012006-11-02 20:12:39 +00002698 void SetDebugInfo(MachineDebugInfo *DI) {
2699 // Make sure initial declarations are made.
2700 if (!DebugInfo && DI->hasInfo()) {
2701 DebugInfo = DI;
2702 shouldEmit = true;
2703
2704 // Emit initial sections
2705 EmitInitial();
2706
2707 // Create all the compile unit DIEs.
2708 ConstructCompileUnitDIEs();
2709
2710 // Create DIEs for each of the externally visible global variables.
2711 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002712
Jim Laskeyef42a012006-11-02 20:12:39 +00002713 // Create DIEs for each of the externally visible subprograms.
2714 ConstructSubprogramDIEs();
2715
2716 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00002717 SectionMap.insert(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002718 }
2719 }
2720
Jim Laskey65195462006-10-30 13:35:07 +00002721 /// BeginModule - Emit all Dwarf sections that should come prior to the
2722 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002723 void BeginModule(Module *M) {
2724 this->M = M;
2725
2726 if (!ShouldEmitDwarf()) return;
2727 EOL("Dwarf Begin Module");
2728 }
2729
Jim Laskey65195462006-10-30 13:35:07 +00002730 /// EndModule - Emit all Dwarf sections that should come after the content.
2731 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002732 void EndModule() {
2733 if (!ShouldEmitDwarf()) return;
2734 EOL("Dwarf End Module");
2735
2736 // Standard sections final addresses.
2737 Asm->SwitchToTextSection(TAI->getTextSection());
2738 EmitLabel("text_end", 0);
2739 Asm->SwitchToDataSection(TAI->getDataSection());
2740 EmitLabel("data_end", 0);
2741
2742 // End text sections.
2743 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2744 Asm->SwitchToTextSection(SectionMap[i].c_str());
2745 EmitLabel("section_end", i);
2746 }
2747
2748 // Compute DIE offsets and sizes.
2749 SizeAndOffsets();
2750
2751 // Emit all the DIEs into a debug info section
2752 EmitDebugInfo();
2753
2754 // Corresponding abbreviations into a abbrev section.
2755 EmitAbbreviations();
2756
2757 // Emit source line correspondence into a debug line section.
2758 EmitDebugLines();
2759
2760 // Emit info into a debug pubnames section.
2761 EmitDebugPubNames();
2762
2763 // Emit info into a debug str section.
2764 EmitDebugStr();
2765
2766 // Emit info into a debug loc section.
2767 EmitDebugLoc();
2768
2769 // Emit info into a debug aranges section.
2770 EmitDebugARanges();
2771
2772 // Emit info into a debug ranges section.
2773 EmitDebugRanges();
2774
2775 // Emit info into a debug macinfo section.
2776 EmitDebugMacInfo();
2777 }
2778
Jim Laskey65195462006-10-30 13:35:07 +00002779 /// BeginFunction - Gather pre-function debug information. Assumes being
2780 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002781 void BeginFunction(MachineFunction *MF) {
2782 this->MF = MF;
2783
2784 if (!ShouldEmitDwarf()) return;
2785 EOL("Dwarf Begin Function");
2786
2787 // Begin accumulating function debug information.
2788 DebugInfo->BeginFunction(MF);
2789
2790 // Assumes in correct section after the entry point.
2791 EmitLabel("func_begin", ++SubprogramCount);
2792 }
2793
Jim Laskey65195462006-10-30 13:35:07 +00002794 /// EndFunction - Gather and emit post-function debug information.
2795 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002796 void EndFunction() {
2797 if (!ShouldEmitDwarf()) return;
2798 EOL("Dwarf End Function");
2799
2800 // Define end label for subprogram.
2801 EmitLabel("func_end", SubprogramCount);
2802
2803 // Get function line info.
2804 const std::vector<SourceLineInfo> &LineInfos = DebugInfo->getSourceLines();
2805
2806 if (!LineInfos.empty()) {
2807 // Get section line info.
2808 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2809 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2810 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2811 // Append the function info to section info.
2812 SectionLineInfos.insert(SectionLineInfos.end(),
2813 LineInfos.begin(), LineInfos.end());
2814 }
2815
2816 // Construct scopes for subprogram.
2817 ConstructRootScope(DebugInfo->getRootScope());
2818
2819 // Emit function frame information.
2820 EmitFunctionDebugFrame();
2821
2822 // Reset the line numbers for the next function.
2823 DebugInfo->ClearLineInfo();
2824
2825 // Clear function debug information.
2826 DebugInfo->EndFunction();
2827 }
Jim Laskey65195462006-10-30 13:35:07 +00002828};
2829
Jim Laskey0d086af2006-02-27 12:43:29 +00002830} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00002831
2832//===----------------------------------------------------------------------===//
2833
Jim Laskeyd18e2892006-01-20 20:34:06 +00002834/// Emit - Print the abbreviation using the specified Dwarf writer.
2835///
Jim Laskey65195462006-10-30 13:35:07 +00002836void DIEAbbrev::Emit(const Dwarf &DW) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002837 // Emit its Dwarf tag type.
2838 DW.EmitULEB128Bytes(Tag);
2839 DW.EOL(TagString(Tag));
2840
2841 // Emit whether it has children DIEs.
2842 DW.EmitULEB128Bytes(ChildrenFlag);
2843 DW.EOL(ChildrenString(ChildrenFlag));
2844
2845 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00002846 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002847 const DIEAbbrevData &AttrData = Data[i];
2848
2849 // Emit attribute type.
2850 DW.EmitULEB128Bytes(AttrData.getAttribute());
2851 DW.EOL(AttributeString(AttrData.getAttribute()));
2852
2853 // Emit form type.
2854 DW.EmitULEB128Bytes(AttrData.getForm());
2855 DW.EOL(FormEncodingString(AttrData.getForm()));
2856 }
2857
2858 // Mark end of abbreviation.
2859 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
2860 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
2861}
2862
2863#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00002864void DIEAbbrev::print(std::ostream &O) {
2865 O << "Abbreviation @"
2866 << std::hex << (intptr_t)this << std::dec
2867 << " "
2868 << TagString(Tag)
2869 << " "
2870 << ChildrenString(ChildrenFlag)
2871 << "\n";
2872
2873 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
2874 O << " "
2875 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002876 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00002877 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002878 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002879 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00002880}
Bill Wendlinge8156192006-12-07 01:30:32 +00002881void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002882#endif
2883
2884//===----------------------------------------------------------------------===//
2885
Jim Laskeyef42a012006-11-02 20:12:39 +00002886#ifndef NDEBUG
2887void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00002888 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002889}
Jim Laskeyef42a012006-11-02 20:12:39 +00002890#endif
2891
2892//===----------------------------------------------------------------------===//
2893
Jim Laskey063e7652006-01-17 17:31:53 +00002894/// EmitValue - Emit integer of appropriate size.
2895///
Jim Laskey65195462006-10-30 13:35:07 +00002896void DIEInteger::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +00002897 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00002898 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00002899 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002900 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002901 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002902 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002903 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002904 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002905 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002906 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +00002907 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
2908 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +00002909 default: assert(0 && "DIE Value form not supported yet"); break;
2910 }
2911}
2912
Jim Laskey063e7652006-01-17 17:31:53 +00002913//===----------------------------------------------------------------------===//
2914
2915/// EmitValue - Emit string value.
2916///
Jim Laskey65195462006-10-30 13:35:07 +00002917void DIEString::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002918 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00002919}
2920
Jim Laskey063e7652006-01-17 17:31:53 +00002921//===----------------------------------------------------------------------===//
2922
2923/// EmitValue - Emit label value.
2924///
Jim Laskey65195462006-10-30 13:35:07 +00002925void DIEDwarfLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002926 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +00002927}
2928
2929/// SizeOf - Determine size of label value in bytes.
2930///
Jim Laskey65195462006-10-30 13:35:07 +00002931unsigned DIEDwarfLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002932 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002933}
Jim Laskeyef42a012006-11-02 20:12:39 +00002934
Jim Laskey063e7652006-01-17 17:31:53 +00002935//===----------------------------------------------------------------------===//
2936
Jim Laskeyd18e2892006-01-20 20:34:06 +00002937/// EmitValue - Emit label value.
2938///
Jim Laskey65195462006-10-30 13:35:07 +00002939void DIEObjectLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002940 DW.EmitReference(Label);
2941}
2942
2943/// SizeOf - Determine size of label value in bytes.
2944///
Jim Laskey65195462006-10-30 13:35:07 +00002945unsigned DIEObjectLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002946 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00002947}
2948
2949//===----------------------------------------------------------------------===//
2950
Jim Laskey063e7652006-01-17 17:31:53 +00002951/// EmitValue - Emit delta value.
2952///
Jim Laskey65195462006-10-30 13:35:07 +00002953void DIEDelta::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002954 bool IsSmall = Form == DW_FORM_data4;
2955 DW.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00002956}
2957
2958/// SizeOf - Determine size of delta value in bytes.
2959///
Jim Laskey65195462006-10-30 13:35:07 +00002960unsigned DIEDelta::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002961 if (Form == DW_FORM_data4) return 4;
Jim Laskey563321a2006-09-06 18:34:40 +00002962 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002963}
2964
2965//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00002966
Jim Laskeyb8509c52006-03-23 18:07:55 +00002967/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002968///
Jim Laskey65195462006-10-30 13:35:07 +00002969void DIEntry::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +00002970 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00002971}
Jim Laskeyd18e2892006-01-20 20:34:06 +00002972
2973//===----------------------------------------------------------------------===//
2974
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002975/// ComputeSize - calculate the size of the block.
2976///
Jim Laskey65195462006-10-30 13:35:07 +00002977unsigned DIEBlock::ComputeSize(Dwarf &DW) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002978 if (!Size) {
2979 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2980
2981 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2982 Size += Values[i]->SizeOf(DW, AbbrevData[i].getForm());
2983 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002984 }
2985 return Size;
2986}
2987
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002988/// EmitValue - Emit block data.
2989///
Jim Laskey65195462006-10-30 13:35:07 +00002990void DIEBlock::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002991 switch (Form) {
2992 case DW_FORM_block1: DW.EmitInt8(Size); break;
2993 case DW_FORM_block2: DW.EmitInt16(Size); break;
2994 case DW_FORM_block4: DW.EmitInt32(Size); break;
2995 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
2996 default: assert(0 && "Improper form for block"); break;
2997 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002998
2999 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
3000
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003001 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3002 DW.EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00003003 Values[i]->EmitValue(DW, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003004 }
3005}
3006
3007/// SizeOf - Determine size of block data in bytes.
3008///
Jim Laskey65195462006-10-30 13:35:07 +00003009unsigned DIEBlock::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003010 switch (Form) {
3011 case DW_FORM_block1: return Size + sizeof(int8_t);
3012 case DW_FORM_block2: return Size + sizeof(int16_t);
3013 case DW_FORM_block4: return Size + sizeof(int32_t);
Jim Laskeyef42a012006-11-02 20:12:39 +00003014 case DW_FORM_block: return Size + SizeULEB128(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003015 default: assert(0 && "Improper form for block"); break;
3016 }
3017 return 0;
3018}
3019
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003020//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003021/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00003022
3023DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003024 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00003025 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00003026}
Jim Laskeyef42a012006-11-02 20:12:39 +00003027
Jim Laskeyb8509c52006-03-23 18:07:55 +00003028/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
3029///
3030void DIE::AddSiblingOffset() {
3031 DIEInteger *DI = new DIEInteger(0);
3032 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00003033 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00003034}
3035
Jim Laskeyef42a012006-11-02 20:12:39 +00003036/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003037///
Jim Laskeyef42a012006-11-02 20:12:39 +00003038void DIE::Profile(FoldingSetNodeID &ID) {
3039 Abbrev.Profile(ID);
3040
3041 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3042 ID.AddPointer(Children[i]);
3043
3044 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3045 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003046}
Jim Laskeyef42a012006-11-02 20:12:39 +00003047
3048#ifndef NDEBUG
3049void DIE::print(std::ostream &O, unsigned IncIndent) {
3050 static unsigned IndentCount = 0;
3051 IndentCount += IncIndent;
3052 const std::string Indent(IndentCount, ' ');
3053 bool isBlock = Abbrev.getTag() == 0;
3054
3055 if (!isBlock) {
3056 O << Indent
3057 << "Die: "
3058 << "0x" << std::hex << (intptr_t)this << std::dec
3059 << ", Offset: " << Offset
3060 << ", Size: " << Size
3061 << "\n";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003062
Jim Laskeyef42a012006-11-02 20:12:39 +00003063 O << Indent
3064 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00003065 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00003066 << ChildrenString(Abbrev.getChildrenFlag());
3067 } else {
3068 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00003069 }
3070 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003071
Jim Laskeyef42a012006-11-02 20:12:39 +00003072 const std::vector<DIEAbbrevData> &Data = Abbrev.getData();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003073
Jim Laskeyef42a012006-11-02 20:12:39 +00003074 IndentCount += 2;
3075 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3076 O << Indent;
3077 if (!isBlock) {
3078 O << AttributeString(Data[i].getAttribute());
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003079 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00003080 O << "Blk[" << i << "]";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003081 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003082 O << " "
3083 << FormEncodingString(Data[i].getForm())
3084 << " ";
3085 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00003086 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00003087 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003088 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00003089
Jim Laskeyef42a012006-11-02 20:12:39 +00003090 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3091 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00003092 }
Jim Laskey063e7652006-01-17 17:31:53 +00003093
Jim Laskeyef42a012006-11-02 20:12:39 +00003094 if (!isBlock) O << "\n";
3095 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00003096}
3097
Jim Laskeyef42a012006-11-02 20:12:39 +00003098void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003099 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00003100}
Jim Laskeybd761842006-02-27 17:27:12 +00003101#endif
Jim Laskey65195462006-10-30 13:35:07 +00003102
3103//===----------------------------------------------------------------------===//
3104/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00003105///
Jim Laskey65195462006-10-30 13:35:07 +00003106
3107DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3108 const TargetAsmInfo *T) {
3109 DW = new Dwarf(OS, A, T);
3110}
3111
3112DwarfWriter::~DwarfWriter() {
3113 delete DW;
3114}
3115
3116/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
3117/// created it. Set by the target AsmPrinter.
3118void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
3119 DW->SetDebugInfo(DI);
3120}
3121
3122/// BeginModule - Emit all Dwarf sections that should come prior to the
3123/// content.
3124void DwarfWriter::BeginModule(Module *M) {
3125 DW->BeginModule(M);
3126}
3127
3128/// EndModule - Emit all Dwarf sections that should come after the content.
3129///
3130void DwarfWriter::EndModule() {
3131 DW->EndModule();
3132}
3133
3134/// BeginFunction - Gather pre-function debug information. Assumes being
3135/// emitted immediately after the function entry point.
3136void DwarfWriter::BeginFunction(MachineFunction *MF) {
3137 DW->BeginFunction(MF);
3138}
3139
3140/// EndFunction - Gather and emit post-function debug information.
3141///
3142void DwarfWriter::EndFunction() {
3143 DW->EndFunction();
3144}