blob: f76316937d8f29acc590be5d0ce5411d5c45276a [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"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000034
Bill Wendlingbdc679d2006-11-29 00:39:47 +000035#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000036#include <string>
Jim Laskeya7cea6f2006-01-04 13:52:30 +000037
Jim Laskeyb2efb852006-01-04 22:28:25 +000038using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000039using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000040
41static cl::opt<bool>
42DwarfVerbose("dwarf-verbose", cl::Hidden,
Jim Laskeyce50a162006-08-29 16:24:26 +000043 cl::desc("Add comments to Dwarf directives."));
Jim Laskey063e7652006-01-17 17:31:53 +000044
Jim Laskey0d086af2006-02-27 12:43:29 +000045namespace llvm {
Jim Laskey65195462006-10-30 13:35:07 +000046
47//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000048
49/// Configuration values for initial hash set sizes (log2).
50///
51static const unsigned InitDiesSetSize = 9; // 512
52static const unsigned InitAbbreviationsSetSize = 9; // 512
53static const unsigned InitValuesSetSize = 9; // 512
54
55//===----------------------------------------------------------------------===//
56/// Forward declarations.
57///
58class DIE;
59class DIEValue;
60
61//===----------------------------------------------------------------------===//
62/// LEB 128 number encoding.
63
64/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
65/// representing an unsigned leb128 value.
66static void PrintULEB128(std::ostream &O, unsigned Value) {
67 do {
68 unsigned Byte = Value & 0x7f;
69 Value >>= 7;
70 if (Value) Byte |= 0x80;
71 O << "0x" << std::hex << Byte << std::dec;
72 if (Value) O << ", ";
73 } while (Value);
74}
75
76/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
77/// value.
78static unsigned SizeULEB128(unsigned Value) {
79 unsigned Size = 0;
80 do {
81 Value >>= 7;
82 Size += sizeof(int8_t);
83 } while (Value);
84 return Size;
85}
86
87/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
88/// representing a signed leb128 value.
89static void PrintSLEB128(std::ostream &O, int Value) {
90 int Sign = Value >> (8 * sizeof(Value) - 1);
91 bool IsMore;
92
93 do {
94 unsigned Byte = Value & 0x7f;
95 Value >>= 7;
96 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
97 if (IsMore) Byte |= 0x80;
98 O << "0x" << std::hex << Byte << std::dec;
99 if (IsMore) O << ", ";
100 } while (IsMore);
101}
102
103/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
104/// value.
105static unsigned SizeSLEB128(int Value) {
106 unsigned Size = 0;
107 int Sign = Value >> (8 * sizeof(Value) - 1);
108 bool IsMore;
109
110 do {
111 unsigned Byte = Value & 0x7f;
112 Value >>= 7;
113 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
114 Size += sizeof(int8_t);
115 } while (IsMore);
116 return Size;
117}
118
119//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000120/// DWLabel - Labels are used to track locations in the assembler file.
121/// Labels appear in the form <prefix>debug_<Tag><Number>, where the tag is a
122/// category of label (Ex. location) and number is a value unique in that
123/// category.
Jim Laskey65195462006-10-30 13:35:07 +0000124class DWLabel {
125public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000126 /// Tag - Label category tag. Should always be a staticly declared C string.
127 ///
128 const char *Tag;
129
130 /// Number - Value to make label unique.
131 ///
132 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +0000133
134 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Jim Laskeybd761842006-02-27 17:27:12 +0000135
Jim Laskeyef42a012006-11-02 20:12:39 +0000136 void Profile(FoldingSetNodeID &ID) const {
137 ID.AddString(std::string(Tag));
138 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +0000139 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000140
141#ifndef NDEBUG
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000142 void print(llvm_ostream &O) const {
143 if (O.stream()) print(*O.stream());
144 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000145 void print(std::ostream &O) const {
146 O << ".debug_" << Tag;
147 if (Number) O << Number;
148 }
149#endif
Jim Laskeybd761842006-02-27 17:27:12 +0000150};
151
152//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000153/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
154/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000155class DIEAbbrevData {
156private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000157 /// Attribute - Dwarf attribute code.
158 ///
159 unsigned Attribute;
160
161 /// Form - Dwarf form code.
162 ///
163 unsigned Form;
Jim Laskey0d086af2006-02-27 12:43:29 +0000164
165public:
166 DIEAbbrevData(unsigned A, unsigned F)
167 : Attribute(A)
168 , Form(F)
169 {}
170
Jim Laskeybd761842006-02-27 17:27:12 +0000171 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000172 unsigned getAttribute() const { return Attribute; }
173 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000174
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000175 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000176 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000177 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000178 ID.AddInteger(Attribute);
179 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000180 }
181};
Jim Laskey063e7652006-01-17 17:31:53 +0000182
Jim Laskey0d086af2006-02-27 12:43:29 +0000183//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000184/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
185/// information object.
186class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000187private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000188 /// Tag - Dwarf tag code.
189 ///
190 unsigned Tag;
191
192 /// Unique number for node.
193 ///
194 unsigned Number;
195
196 /// ChildrenFlag - Dwarf children flag.
197 ///
198 unsigned ChildrenFlag;
199
200 /// Data - Raw data bytes for abbreviation.
201 ///
202 std::vector<DIEAbbrevData> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000203
Jim Laskey0d086af2006-02-27 12:43:29 +0000204public:
Jim Laskey063e7652006-01-17 17:31:53 +0000205
Jim Laskey0d086af2006-02-27 12:43:29 +0000206 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000207 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000208 , ChildrenFlag(C)
209 , Data()
210 {}
211 ~DIEAbbrev() {}
212
Jim Laskeybd761842006-02-27 17:27:12 +0000213 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000214 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000215 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000216 unsigned getChildrenFlag() const { return ChildrenFlag; }
217 const std::vector<DIEAbbrevData> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000218 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000219 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000220 void setNumber(unsigned N) { Number = N; }
221
Jim Laskey0d086af2006-02-27 12:43:29 +0000222 /// AddAttribute - Adds another set of attribute information to the
223 /// abbreviation.
224 void AddAttribute(unsigned Attribute, unsigned Form) {
225 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000226 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000227
Jim Laskeyb8509c52006-03-23 18:07:55 +0000228 /// AddFirstAttribute - Adds a set of attribute information to the front
229 /// of the abbreviation.
230 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
231 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
232 }
233
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000234 /// Profile - Used to gather unique data for the abbreviation folding set.
235 ///
236 void Profile(FoldingSetNodeID &ID) {
237 ID.AddInteger(Tag);
238 ID.AddInteger(ChildrenFlag);
239
240 // For each attribute description.
241 for (unsigned i = 0, N = Data.size(); i < N; ++i)
242 Data[i].Profile(ID);
243 }
244
Jim Laskey0d086af2006-02-27 12:43:29 +0000245 /// Emit - Print the abbreviation using the specified Dwarf writer.
246 ///
Jim Laskey65195462006-10-30 13:35:07 +0000247 void Emit(const Dwarf &DW) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000248
249#ifndef NDEBUG
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000250 void print(llvm_ostream &O) {
251 if (O.stream()) print(*O.stream());
252 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000253 void print(std::ostream &O);
254 void dump();
255#endif
256};
Jim Laskey063e7652006-01-17 17:31:53 +0000257
Jim Laskey0d086af2006-02-27 12:43:29 +0000258//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000259/// DIE - A structured debug information entry. Has an abbreviation which
260/// describes it's organization.
261class DIE : public FoldingSetNode {
262protected:
263 /// Abbrev - Buffer for constructing abbreviation.
264 ///
265 DIEAbbrev Abbrev;
266
267 /// Offset - Offset in debug info section.
268 ///
269 unsigned Offset;
270
271 /// Size - Size of instance + children.
272 ///
273 unsigned Size;
274
275 /// Children DIEs.
276 ///
277 std::vector<DIE *> Children;
278
279 /// Attributes values.
280 ///
281 std::vector<DIEValue *> Values;
282
283public:
284 DIE(unsigned Tag)
285 : Abbrev(Tag, DW_CHILDREN_no)
286 , Offset(0)
287 , Size(0)
288 , Children()
289 , Values()
290 {}
291 virtual ~DIE();
292
293 // Accessors.
294 DIEAbbrev &getAbbrev() { return Abbrev; }
295 unsigned getAbbrevNumber() const {
296 return Abbrev.getNumber();
297 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000298 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000299 unsigned getOffset() const { return Offset; }
300 unsigned getSize() const { return Size; }
301 const std::vector<DIE *> &getChildren() const { return Children; }
302 const std::vector<DIEValue *> &getValues() const { return Values; }
303 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
304 void setOffset(unsigned O) { Offset = O; }
305 void setSize(unsigned S) { Size = S; }
306
307 /// AddValue - Add a value and attributes to a DIE.
308 ///
309 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
310 Abbrev.AddAttribute(Attribute, Form);
311 Values.push_back(Value);
312 }
313
314 /// SiblingOffset - Return the offset of the debug information entry's
315 /// sibling.
316 unsigned SiblingOffset() const { return Offset + Size; }
317
318 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
319 ///
320 void AddSiblingOffset();
321
322 /// AddChild - Add a child to the DIE.
323 ///
324 void AddChild(DIE *Child) {
325 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
326 Children.push_back(Child);
327 }
328
329 /// Detach - Detaches objects connected to it after copying.
330 ///
331 void Detach() {
332 Children.clear();
333 }
334
335 /// Profile - Used to gather unique data for the value folding set.
336 ///
337 void Profile(FoldingSetNodeID &ID) ;
338
339#ifndef NDEBUG
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000340 void print(llvm_ostream &O, unsigned IncIndent = 0) {
341 if (O.stream()) print(*O.stream(), IncIndent);
342 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000343 void print(std::ostream &O, unsigned IncIndent = 0);
344 void dump();
345#endif
346};
347
348//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000349/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000350///
351class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000352public:
353 enum {
354 isInteger,
355 isString,
356 isLabel,
357 isAsIsLabel,
358 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000359 isEntry,
360 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000361 };
362
Jim Laskeyef42a012006-11-02 20:12:39 +0000363 /// Type - Type of data stored in the value.
364 ///
365 unsigned Type;
Jim Laskey0d086af2006-02-27 12:43:29 +0000366
Jim Laskeyef42a012006-11-02 20:12:39 +0000367 DIEValue(unsigned T)
368 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000369 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000370 virtual ~DIEValue() {}
371
Jim Laskeyf6733882006-11-02 21:48:18 +0000372 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000373 unsigned getType() const { return Type; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000374
Jim Laskey0d086af2006-02-27 12:43:29 +0000375 // Implement isa/cast/dyncast.
376 static bool classof(const DIEValue *) { return true; }
377
378 /// EmitValue - Emit value via the Dwarf writer.
379 ///
Jim Laskey65195462006-10-30 13:35:07 +0000380 virtual void EmitValue(const Dwarf &DW, unsigned Form) const = 0;
Jim Laskey0d086af2006-02-27 12:43:29 +0000381
382 /// SizeOf - Return the size of a value in bytes.
383 ///
Jim Laskey65195462006-10-30 13:35:07 +0000384 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const = 0;
Jim Laskeyef42a012006-11-02 20:12:39 +0000385
386 /// Profile - Used to gather unique data for the value folding set.
387 ///
388 virtual void Profile(FoldingSetNodeID &ID) = 0;
389
390#ifndef NDEBUG
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000391 void print(llvm_ostream &O) {
392 if (O.stream()) print(*O.stream());
393 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000394 virtual void print(std::ostream &O) = 0;
395 void dump();
396#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000397};
Jim Laskey063e7652006-01-17 17:31:53 +0000398
Jim Laskey0d086af2006-02-27 12:43:29 +0000399//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000400/// DWInteger - An integer value DIE.
401///
Jim Laskey0d086af2006-02-27 12:43:29 +0000402class DIEInteger : public DIEValue {
403private:
404 uint64_t Integer;
405
406public:
407 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000408
Jim Laskey0d086af2006-02-27 12:43:29 +0000409 // Implement isa/cast/dyncast.
410 static bool classof(const DIEInteger *) { return true; }
411 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
412
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000413 /// BestForm - Choose the best form for integer.
414 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000415 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
416 if (IsSigned) {
417 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
418 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
419 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
420 } else {
421 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
422 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
423 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
424 }
425 return DW_FORM_data8;
426 }
427
Jim Laskey0d086af2006-02-27 12:43:29 +0000428 /// EmitValue - Emit integer of appropriate size.
429 ///
Jim Laskey65195462006-10-30 13:35:07 +0000430 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000431
432 /// SizeOf - Determine size of integer value in bytes.
433 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000434 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
435 switch (Form) {
436 case DW_FORM_flag: // Fall thru
437 case DW_FORM_ref1: // Fall thru
438 case DW_FORM_data1: return sizeof(int8_t);
439 case DW_FORM_ref2: // Fall thru
440 case DW_FORM_data2: return sizeof(int16_t);
441 case DW_FORM_ref4: // Fall thru
442 case DW_FORM_data4: return sizeof(int32_t);
443 case DW_FORM_ref8: // Fall thru
444 case DW_FORM_data8: return sizeof(int64_t);
445 case DW_FORM_udata: return SizeULEB128(Integer);
446 case DW_FORM_sdata: return SizeSLEB128(Integer);
447 default: assert(0 && "DIE Value form not supported yet"); break;
448 }
449 return 0;
450 }
451
452 /// Profile - Used to gather unique data for the value folding set.
453 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000454 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000455 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000456 ID.AddInteger(Integer);
457 }
Jim Laskey5496f012006-11-09 14:52:14 +0000458 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000459
460#ifndef NDEBUG
461 virtual void print(std::ostream &O) {
462 O << "Int: " << (int64_t)Integer
463 << " 0x" << std::hex << Integer << std::dec;
464 }
465#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000466};
Jim Laskey063e7652006-01-17 17:31:53 +0000467
Jim Laskey0d086af2006-02-27 12:43:29 +0000468//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000469/// DIEString - A string value DIE.
470///
Jim Laskeyef42a012006-11-02 20:12:39 +0000471class DIEString : public DIEValue {
472public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000473 const std::string String;
474
475 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000476
Jim Laskey0d086af2006-02-27 12:43:29 +0000477 // Implement isa/cast/dyncast.
478 static bool classof(const DIEString *) { return true; }
479 static bool classof(const DIEValue *S) { return S->Type == isString; }
480
481 /// EmitValue - Emit string value.
482 ///
Jim Laskey65195462006-10-30 13:35:07 +0000483 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000484
485 /// SizeOf - Determine size of string value in bytes.
486 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000487 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
488 return String.size() + sizeof(char); // sizeof('\0');
489 }
490
491 /// Profile - Used to gather unique data for the value folding set.
492 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000493 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000494 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000495 ID.AddString(String);
496 }
Jim Laskey5496f012006-11-09 14:52:14 +0000497 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000498
499#ifndef NDEBUG
500 virtual void print(std::ostream &O) {
501 O << "Str: \"" << String << "\"";
502 }
503#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000504};
Jim Laskey063e7652006-01-17 17:31:53 +0000505
Jim Laskey0d086af2006-02-27 12:43:29 +0000506//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000507/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000508//
Jim Laskeyef42a012006-11-02 20:12:39 +0000509class DIEDwarfLabel : public DIEValue {
510public:
511
Jim Laskey0d086af2006-02-27 12:43:29 +0000512 const DWLabel Label;
513
514 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000515
Jim Laskey0d086af2006-02-27 12:43:29 +0000516 // Implement isa/cast/dyncast.
517 static bool classof(const DIEDwarfLabel *) { return true; }
518 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
519
520 /// EmitValue - Emit label value.
521 ///
Jim Laskey65195462006-10-30 13:35:07 +0000522 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000523
524 /// SizeOf - Determine size of label value in bytes.
525 ///
Jim Laskey65195462006-10-30 13:35:07 +0000526 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000527
528 /// Profile - Used to gather unique data for the value folding set.
529 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000530 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000531 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000532 Label.Profile(ID);
533 }
Jim Laskey5496f012006-11-09 14:52:14 +0000534 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000535
536#ifndef NDEBUG
537 virtual void print(std::ostream &O) {
538 O << "Lbl: ";
539 Label.print(O);
540 }
541#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000542};
Jim Laskey063e7652006-01-17 17:31:53 +0000543
Jim Laskey063e7652006-01-17 17:31:53 +0000544
Jim Laskey0d086af2006-02-27 12:43:29 +0000545//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000546/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000547//
Jim Laskeyef42a012006-11-02 20:12:39 +0000548class DIEObjectLabel : public DIEValue {
549public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000550 const std::string Label;
551
552 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000553
Jim Laskey0d086af2006-02-27 12:43:29 +0000554 // Implement isa/cast/dyncast.
555 static bool classof(const DIEObjectLabel *) { return true; }
556 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
557
558 /// EmitValue - Emit label value.
559 ///
Jim Laskey65195462006-10-30 13:35:07 +0000560 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000561
562 /// SizeOf - Determine size of label value in bytes.
563 ///
Jim Laskey65195462006-10-30 13:35:07 +0000564 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000565
566 /// Profile - Used to gather unique data for the value folding set.
567 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000568 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000569 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000570 ID.AddString(Label);
571 }
Jim Laskey5496f012006-11-09 14:52:14 +0000572 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000573
574#ifndef NDEBUG
575 virtual void print(std::ostream &O) {
576 O << "Obj: " << Label;
577 }
578#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000579};
Jim Laskey063e7652006-01-17 17:31:53 +0000580
Jim Laskey0d086af2006-02-27 12:43:29 +0000581//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000582/// DIEDelta - A simple label difference DIE.
583///
Jim Laskeyef42a012006-11-02 20:12:39 +0000584class DIEDelta : public DIEValue {
585public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000586 const DWLabel LabelHi;
587 const DWLabel LabelLo;
588
589 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
590 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000591
Jim Laskey0d086af2006-02-27 12:43:29 +0000592 // Implement isa/cast/dyncast.
593 static bool classof(const DIEDelta *) { return true; }
594 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
595
596 /// EmitValue - Emit delta value.
597 ///
Jim Laskey65195462006-10-30 13:35:07 +0000598 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000599
600 /// SizeOf - Determine size of delta value in bytes.
601 ///
Jim Laskey65195462006-10-30 13:35:07 +0000602 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000603
604 /// Profile - Used to gather unique data for the value folding set.
605 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000606 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
607 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000608 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000609 LabelHi.Profile(ID);
610 LabelLo.Profile(ID);
611 }
Jim Laskey5496f012006-11-09 14:52:14 +0000612 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000613
614#ifndef NDEBUG
615 virtual void print(std::ostream &O) {
616 O << "Del: ";
617 LabelHi.print(O);
618 O << "-";
619 LabelLo.print(O);
620 }
621#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000622};
Jim Laskey063e7652006-01-17 17:31:53 +0000623
Jim Laskey0d086af2006-02-27 12:43:29 +0000624//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000625/// DIEntry - A pointer to another debug information entry. An instance of this
626/// class can also be used as a proxy for a debug information entry not yet
627/// defined (ie. types.)
628class DIEntry : public DIEValue {
629public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000630 DIE *Entry;
631
632 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000633
Jim Laskey0d086af2006-02-27 12:43:29 +0000634 // Implement isa/cast/dyncast.
635 static bool classof(const DIEntry *) { return true; }
636 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
637
Jim Laskeyb8509c52006-03-23 18:07:55 +0000638 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000639 ///
Jim Laskey65195462006-10-30 13:35:07 +0000640 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000641
Jim Laskeyb8509c52006-03-23 18:07:55 +0000642 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000643 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000644 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
645 return sizeof(int32_t);
646 }
647
648 /// Profile - Used to gather unique data for the value folding set.
649 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000650 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
651 ID.AddInteger(isEntry);
652 ID.AddPointer(Entry);
653 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000654 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000655 ID.AddInteger(isEntry);
656
Jim Laskeyef42a012006-11-02 20:12:39 +0000657 if (Entry) {
658 ID.AddPointer(Entry);
659 } else {
660 ID.AddPointer(this);
661 }
662 }
663
664#ifndef NDEBUG
665 virtual void print(std::ostream &O) {
666 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
667 }
668#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000669};
670
671//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000672/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000673//
Jim Laskeyef42a012006-11-02 20:12:39 +0000674class DIEBlock : public DIEValue, public DIE {
675public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000676 unsigned Size; // Size in bytes excluding size header.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000677
678 DIEBlock()
679 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000680 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000681 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000682 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000683 ~DIEBlock() {
684 }
685
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000686 // Implement isa/cast/dyncast.
687 static bool classof(const DIEBlock *) { return true; }
688 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
689
690 /// ComputeSize - calculate the size of the block.
691 ///
Jim Laskey65195462006-10-30 13:35:07 +0000692 unsigned ComputeSize(Dwarf &DW);
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000693
694 /// BestForm - Choose the best form for data.
695 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000696 unsigned BestForm() const {
697 if ((unsigned char)Size == Size) return DW_FORM_block1;
698 if ((unsigned short)Size == Size) return DW_FORM_block2;
699 if ((unsigned int)Size == Size) return DW_FORM_block4;
700 return DW_FORM_block;
701 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000702
703 /// EmitValue - Emit block data.
704 ///
Jim Laskey65195462006-10-30 13:35:07 +0000705 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000706
707 /// SizeOf - Determine size of block data in bytes.
708 ///
Jim Laskey65195462006-10-30 13:35:07 +0000709 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000710
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000711
Jim Laskeyef42a012006-11-02 20:12:39 +0000712 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000713 ///
Reid Spencer97821312006-11-02 23:56:21 +0000714 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000715 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000716 DIE::Profile(ID);
717 }
718
719#ifndef NDEBUG
720 virtual void print(std::ostream &O) {
721 O << "Blk: ";
722 DIE::print(O, 5);
723 }
724#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000725};
726
727//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000728/// CompileUnit - This dwarf writer support class manages information associate
729/// with a source file.
730class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000731private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000732 /// Desc - Compile unit debug descriptor.
733 ///
734 CompileUnitDesc *Desc;
735
736 /// ID - File identifier for source.
737 ///
738 unsigned ID;
739
740 /// Die - Compile unit debug information entry.
741 ///
742 DIE *Die;
743
744 /// DescToDieMap - Tracks the mapping of unit level debug informaton
745 /// descriptors to debug information entries.
746 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
747
748 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
749 /// descriptors to debug information entries using a DIEntry proxy.
750 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
751
752 /// Globals - A map of globally visible named entities for this unit.
753 ///
754 std::map<std::string, DIE *> Globals;
755
756 /// DiesSet - Used to uniquely define dies within the compile unit.
757 ///
758 FoldingSet<DIE> DiesSet;
759
760 /// Dies - List of all dies in the compile unit.
761 ///
762 std::vector<DIE *> Dies;
Jim Laskey0d086af2006-02-27 12:43:29 +0000763
764public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000765 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
766 : Desc(CUD)
767 , ID(I)
768 , Die(D)
769 , DescToDieMap()
770 , DescToDIEntryMap()
771 , Globals()
772 , DiesSet(InitDiesSetSize)
773 , Dies()
774 {}
775
776 ~CompileUnit() {
777 delete Die;
778
779 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
780 delete Dies[i];
781 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000782
Jim Laskeybd761842006-02-27 17:27:12 +0000783 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000784 CompileUnitDesc *getDesc() const { return Desc; }
785 unsigned getID() const { return ID; }
786 DIE* getDie() const { return Die; }
787 std::map<std::string, DIE *> &getGlobals() { return Globals; }
788
789 /// hasContent - Return true if this compile unit has something to write out.
790 ///
791 bool hasContent() const {
792 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000793 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000794
795 /// AddGlobal - Add a new global entity to the compile unit.
796 ///
797 void AddGlobal(const std::string &Name, DIE *Die) {
798 Globals[Name] = Die;
799 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000800
Jim Laskeyef42a012006-11-02 20:12:39 +0000801 /// getDieMapSlotFor - Returns the debug information entry map slot for the
802 /// specified debug descriptor.
803 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
804 return DescToDieMap[DD];
805 }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000806
Jim Laskeyef42a012006-11-02 20:12:39 +0000807 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
808 /// specified debug descriptor.
809 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DD) {
810 return DescToDIEntryMap[DD];
811 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000812
Jim Laskeyef42a012006-11-02 20:12:39 +0000813 /// AddDie - Adds or interns the DIE to the compile unit.
814 ///
815 DIE *AddDie(DIE &Buffer) {
816 FoldingSetNodeID ID;
817 Buffer.Profile(ID);
818 void *Where;
819 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
820
821 if (!Die) {
822 Die = new DIE(Buffer);
823 DiesSet.InsertNode(Die, Where);
824 this->Die->AddChild(Die);
825 Buffer.Detach();
826 }
827
828 return Die;
829 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000830};
831
Jim Laskey65195462006-10-30 13:35:07 +0000832//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000833/// Dwarf - Emits Dwarf debug and exception handling directives.
834///
Jim Laskey65195462006-10-30 13:35:07 +0000835class Dwarf {
836
837private:
838
839 //===--------------------------------------------------------------------===//
840 // Core attributes used by the Dwarf writer.
841 //
842
843 //
844 /// O - Stream to .s file.
845 ///
846 std::ostream &O;
847
848 /// Asm - Target of Dwarf emission.
849 ///
850 AsmPrinter *Asm;
851
852 /// TAI - Target Asm Printer.
853 const TargetAsmInfo *TAI;
854
855 /// TD - Target data.
856 const TargetData *TD;
857
858 /// RI - Register Information.
859 const MRegisterInfo *RI;
860
861 /// M - Current module.
862 ///
863 Module *M;
864
865 /// MF - Current machine function.
866 ///
867 MachineFunction *MF;
868
869 /// DebugInfo - Collected debug information.
870 ///
871 MachineDebugInfo *DebugInfo;
872
873 /// didInitial - Flag to indicate if initial emission has been done.
874 ///
875 bool didInitial;
876
877 /// shouldEmit - Flag to indicate if debug information should be emitted.
878 ///
879 bool shouldEmit;
880
881 /// SubprogramCount - The running count of functions being compiled.
882 ///
883 unsigned SubprogramCount;
884
885 //===--------------------------------------------------------------------===//
886 // Attributes used to construct specific Dwarf sections.
887 //
888
889 /// CompileUnits - All the compile units involved in this build. The index
890 /// of each entry in this vector corresponds to the sources in DebugInfo.
891 std::vector<CompileUnit *> CompileUnits;
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000892
Jim Laskeyef42a012006-11-02 20:12:39 +0000893 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +0000894 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000895 FoldingSet<DIEAbbrev> AbbreviationsSet;
896
897 /// Abbreviations - A list of all the unique abbreviations in use.
898 ///
899 std::vector<DIEAbbrev *> Abbreviations;
Jim Laskey65195462006-10-30 13:35:07 +0000900
Jim Laskeyef42a012006-11-02 20:12:39 +0000901 /// ValuesSet - Used to uniquely define values.
902 ///
903 FoldingSet<DIEValue> ValuesSet;
904
905 /// Values - A list of all the unique values in use.
906 ///
907 std::vector<DIEValue *> Values;
908
Jim Laskey65195462006-10-30 13:35:07 +0000909 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +0000910 ///
Jim Laskey65195462006-10-30 13:35:07 +0000911 UniqueVector<std::string> StringPool;
912
913 /// UnitMap - Map debug information descriptor to compile unit.
914 ///
915 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
916
Jim Laskey65195462006-10-30 13:35:07 +0000917 /// SectionMap - Provides a unique id per text section.
918 ///
919 UniqueVector<std::string> SectionMap;
920
921 /// SectionSourceLines - Tracks line numbers per text section.
922 ///
923 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
924
925
926public:
927
928 //===--------------------------------------------------------------------===//
929 // Emission and print routines
930 //
931
932 /// PrintHex - Print a value as a hexidecimal value.
933 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000934 void PrintHex(int Value) const {
935 O << "0x" << std::hex << Value << std::dec;
936 }
Jim Laskey65195462006-10-30 13:35:07 +0000937
938 /// EOL - Print a newline character to asm stream. If a comment is present
939 /// then it will be printed first. Comments should not contain '\n'.
Jim Laskeyef42a012006-11-02 20:12:39 +0000940 void EOL(const std::string &Comment) const {
941 if (DwarfVerbose && !Comment.empty()) {
942 O << "\t"
943 << TAI->getCommentString()
944 << " "
945 << Comment;
946 }
947 O << "\n";
948 }
Jim Laskey65195462006-10-30 13:35:07 +0000949
950 /// EmitAlign - Print a align directive.
951 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000952 void EmitAlign(unsigned Alignment) const {
953 O << TAI->getAlignDirective() << Alignment << "\n";
954 }
Jim Laskey65195462006-10-30 13:35:07 +0000955
956 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
957 /// unsigned leb128 value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000958 void EmitULEB128Bytes(unsigned Value) const {
959 if (TAI->hasLEB128()) {
960 O << "\t.uleb128\t"
961 << Value;
962 } else {
963 O << TAI->getData8bitsDirective();
964 PrintULEB128(O, Value);
965 }
966 }
Jim Laskey65195462006-10-30 13:35:07 +0000967
968 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
969 /// signed leb128 value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000970 void EmitSLEB128Bytes(int Value) const {
971 if (TAI->hasLEB128()) {
972 O << "\t.sleb128\t"
973 << Value;
974 } else {
975 O << TAI->getData8bitsDirective();
976 PrintSLEB128(O, Value);
977 }
978 }
Jim Laskey65195462006-10-30 13:35:07 +0000979
980 /// EmitInt8 - Emit a byte directive and value.
981 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000982 void EmitInt8(int Value) const {
983 O << TAI->getData8bitsDirective();
984 PrintHex(Value & 0xFF);
985 }
Jim Laskey65195462006-10-30 13:35:07 +0000986
987 /// EmitInt16 - Emit a short directive and value.
988 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000989 void EmitInt16(int Value) const {
990 O << TAI->getData16bitsDirective();
991 PrintHex(Value & 0xFFFF);
992 }
Jim Laskey65195462006-10-30 13:35:07 +0000993
994 /// EmitInt32 - Emit a long directive and value.
995 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000996 void EmitInt32(int Value) const {
997 O << TAI->getData32bitsDirective();
998 PrintHex(Value);
999 }
1000
Jim Laskey65195462006-10-30 13:35:07 +00001001 /// EmitInt64 - Emit a long long directive and value.
1002 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001003 void EmitInt64(uint64_t Value) const {
1004 if (TAI->getData64bitsDirective()) {
1005 O << TAI->getData64bitsDirective();
1006 PrintHex(Value);
1007 } else {
1008 if (TD->isBigEndian()) {
1009 EmitInt32(unsigned(Value >> 32)); O << "\n";
1010 EmitInt32(unsigned(Value));
1011 } else {
1012 EmitInt32(unsigned(Value)); O << "\n";
1013 EmitInt32(unsigned(Value >> 32));
1014 }
1015 }
1016 }
1017
Jim Laskey65195462006-10-30 13:35:07 +00001018 /// EmitString - Emit a string with quotes and a null terminator.
Jim Laskeyef42a012006-11-02 20:12:39 +00001019 /// Special characters are emitted properly.
Jim Laskey65195462006-10-30 13:35:07 +00001020 /// \literal (Eg. '\t') \endliteral
Jim Laskeyef42a012006-11-02 20:12:39 +00001021 void EmitString(const std::string &String) const {
1022 O << TAI->getAsciiDirective()
1023 << "\"";
1024 for (unsigned i = 0, N = String.size(); i < N; ++i) {
1025 unsigned char C = String[i];
1026
1027 if (!isascii(C) || iscntrl(C)) {
1028 switch(C) {
1029 case '\b': O << "\\b"; break;
1030 case '\f': O << "\\f"; break;
1031 case '\n': O << "\\n"; break;
1032 case '\r': O << "\\r"; break;
1033 case '\t': O << "\\t"; break;
1034 default:
1035 O << '\\';
1036 O << char('0' + ((C >> 6) & 7));
1037 O << char('0' + ((C >> 3) & 7));
1038 O << char('0' + ((C >> 0) & 7));
1039 break;
1040 }
1041 } else if (C == '\"') {
1042 O << "\\\"";
1043 } else if (C == '\'') {
1044 O << "\\\'";
1045 } else {
1046 O << C;
1047 }
1048 }
1049 O << "\\0\"";
1050 }
Jim Laskey65195462006-10-30 13:35:07 +00001051
1052 /// PrintLabelName - Print label name in form used by Dwarf writer.
1053 ///
1054 void PrintLabelName(DWLabel Label) const {
1055 PrintLabelName(Label.Tag, Label.Number);
1056 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001057 void PrintLabelName(const char *Tag, unsigned Number) const {
1058 O << TAI->getPrivateGlobalPrefix()
1059 << "debug_"
1060 << Tag;
1061 if (Number) O << Number;
1062 }
Jim Laskey65195462006-10-30 13:35:07 +00001063
1064 /// EmitLabel - Emit location label for internal use by Dwarf.
1065 ///
1066 void EmitLabel(DWLabel Label) const {
1067 EmitLabel(Label.Tag, Label.Number);
1068 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001069 void EmitLabel(const char *Tag, unsigned Number) const {
1070 PrintLabelName(Tag, Number);
1071 O << ":\n";
1072 }
Jim Laskey65195462006-10-30 13:35:07 +00001073
1074 /// EmitReference - Emit a reference to a label.
1075 ///
1076 void EmitReference(DWLabel Label) const {
1077 EmitReference(Label.Tag, Label.Number);
1078 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001079 void EmitReference(const char *Tag, unsigned Number) const {
1080 if (TAI->getAddressSize() == 4)
1081 O << TAI->getData32bitsDirective();
1082 else
1083 O << TAI->getData64bitsDirective();
1084
1085 PrintLabelName(Tag, Number);
1086 }
1087 void EmitReference(const std::string &Name) const {
1088 if (TAI->getAddressSize() == 4)
1089 O << TAI->getData32bitsDirective();
1090 else
1091 O << TAI->getData64bitsDirective();
1092
1093 O << Name;
1094 }
Jim Laskey65195462006-10-30 13:35:07 +00001095
1096 /// EmitDifference - Emit the difference between two labels. Some
1097 /// assemblers do not behave with absolute expressions with data directives,
1098 /// so there is an option (needsSet) to use an intermediary set expression.
1099 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo) const {
1100 EmitDifference(LabelHi.Tag, LabelHi.Number, LabelLo.Tag, LabelLo.Number);
1101 }
1102 void EmitDifference(const char *TagHi, unsigned NumberHi,
Jim Laskeyef42a012006-11-02 20:12:39 +00001103 const char *TagLo, unsigned NumberLo) const {
1104 if (TAI->needsSet()) {
1105 static unsigned SetCounter = 0;
1106
1107 O << "\t.set\t";
1108 PrintLabelName("set", SetCounter);
1109 O << ",";
1110 PrintLabelName(TagHi, NumberHi);
1111 O << "-";
1112 PrintLabelName(TagLo, NumberLo);
1113 O << "\n";
1114
1115 if (TAI->getAddressSize() == sizeof(int32_t))
1116 O << TAI->getData32bitsDirective();
1117 else
1118 O << TAI->getData64bitsDirective();
1119
1120 PrintLabelName("set", SetCounter);
1121
1122 ++SetCounter;
1123 } else {
1124 if (TAI->getAddressSize() == sizeof(int32_t))
1125 O << TAI->getData32bitsDirective();
1126 else
1127 O << TAI->getData64bitsDirective();
1128
1129 PrintLabelName(TagHi, NumberHi);
1130 O << "-";
1131 PrintLabelName(TagLo, NumberLo);
1132 }
1133 }
Jim Laskey65195462006-10-30 13:35:07 +00001134
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001135 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Jim Laskey65195462006-10-30 13:35:07 +00001136 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001137 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1138 // Profile the node so that we can make it unique.
1139 FoldingSetNodeID ID;
1140 Abbrev.Profile(ID);
1141
1142 // Check the set for priors.
1143 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
1144
1145 // If it's newly added.
1146 if (InSet == &Abbrev) {
1147 // Add to abbreviation list.
1148 Abbreviations.push_back(&Abbrev);
1149 // Assign the vector position + 1 as its number.
1150 Abbrev.setNumber(Abbreviations.size());
1151 } else {
1152 // Assign existing abbreviation number.
1153 Abbrev.setNumber(InSet->getNumber());
1154 }
1155 }
1156
Jim Laskey65195462006-10-30 13:35:07 +00001157 /// NewString - Add a string to the constant pool and returns a label.
1158 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001159 DWLabel NewString(const std::string &String) {
1160 unsigned StringID = StringPool.insert(String);
1161 return DWLabel("string", StringID);
1162 }
Jim Laskey65195462006-10-30 13:35:07 +00001163
Jim Laskeyef42a012006-11-02 20:12:39 +00001164 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1165 /// entry.
1166 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1167 DIEntry *Value;
1168
1169 if (Entry) {
1170 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001171 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001172 void *Where;
1173 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
1174
Jim Laskeyf6733882006-11-02 21:48:18 +00001175 if (Value) return Value;
Jim Laskeyef42a012006-11-02 20:12:39 +00001176
1177 Value = new DIEntry(Entry);
1178 ValuesSet.InsertNode(Value, Where);
1179 } else {
1180 Value = new DIEntry(Entry);
1181 }
1182
1183 Values.push_back(Value);
1184 return Value;
1185 }
1186
1187 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1188 ///
1189 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1190 Value->Entry = Entry;
1191 // Add to values set if not already there. If it is, we merely have a
1192 // duplicate in the values list (no harm.)
1193 ValuesSet.GetOrInsertNode(Value);
1194 }
1195
1196 /// AddUInt - Add an unsigned integer attribute data and value.
1197 ///
1198 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1199 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1200
1201 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001202 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001203 void *Where;
1204 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1205 if (!Value) {
1206 Value = new DIEInteger(Integer);
1207 ValuesSet.InsertNode(Value, Where);
1208 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001209 }
1210
1211 Die->AddValue(Attribute, Form, Value);
1212 }
1213
1214 /// AddSInt - Add an signed integer attribute data and value.
1215 ///
1216 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1217 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1218
1219 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001220 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001221 void *Where;
1222 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1223 if (!Value) {
1224 Value = new DIEInteger(Integer);
1225 ValuesSet.InsertNode(Value, Where);
1226 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001227 }
1228
1229 Die->AddValue(Attribute, Form, Value);
1230 }
1231
1232 /// AddString - Add a std::string attribute data and value.
1233 ///
1234 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1235 const std::string &String) {
1236 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001237 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001238 void *Where;
1239 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1240 if (!Value) {
1241 Value = new DIEString(String);
1242 ValuesSet.InsertNode(Value, Where);
1243 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001244 }
1245
1246 Die->AddValue(Attribute, Form, Value);
1247 }
1248
1249 /// AddLabel - Add a Dwarf label attribute data and value.
1250 ///
1251 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1252 const DWLabel &Label) {
1253 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001254 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001255 void *Where;
1256 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1257 if (!Value) {
1258 Value = new DIEDwarfLabel(Label);
1259 ValuesSet.InsertNode(Value, Where);
1260 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001261 }
1262
1263 Die->AddValue(Attribute, Form, Value);
1264 }
1265
1266 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1267 ///
1268 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1269 const std::string &Label) {
1270 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001271 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001272 void *Where;
1273 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1274 if (!Value) {
1275 Value = new DIEObjectLabel(Label);
1276 ValuesSet.InsertNode(Value, Where);
1277 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001278 }
1279
1280 Die->AddValue(Attribute, Form, Value);
1281 }
1282
1283 /// AddDelta - Add a label delta attribute data and value.
1284 ///
1285 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1286 const DWLabel &Hi, const DWLabel &Lo) {
1287 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001288 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001289 void *Where;
1290 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1291 if (!Value) {
1292 Value = new DIEDelta(Hi, Lo);
1293 ValuesSet.InsertNode(Value, Where);
1294 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001295 }
1296
1297 Die->AddValue(Attribute, Form, Value);
1298 }
1299
1300 /// AddDIEntry - Add a DIE attribute data and value.
1301 ///
1302 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1303 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1304 }
1305
1306 /// AddBlock - Add block data.
1307 ///
1308 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1309 Block->ComputeSize(*this);
1310 FoldingSetNodeID ID;
1311 Block->Profile(ID);
1312 void *Where;
1313 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1314 if (!Value) {
1315 Value = Block;
1316 ValuesSet.InsertNode(Value, Where);
1317 Values.push_back(Value);
1318 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001319 delete Block;
1320 }
1321
1322 Die->AddValue(Attribute, Block->BestForm(), Value);
1323 }
1324
Jim Laskey65195462006-10-30 13:35:07 +00001325private:
1326
1327 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001328 /// entry.
1329 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1330 if (File && Line) {
1331 CompileUnit *FileUnit = FindCompileUnit(File);
1332 unsigned FileID = FileUnit->getID();
1333 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1334 AddUInt(Die, DW_AT_decl_line, 0, Line);
1335 }
1336 }
Jim Laskey65195462006-10-30 13:35:07 +00001337
1338 /// AddAddress - Add an address attribute to a die based on the location
1339 /// provided.
1340 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001341 const MachineLocation &Location) {
1342 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1343 DIEBlock *Block = new DIEBlock();
1344
1345 if (Location.isRegister()) {
1346 if (Reg < 32) {
1347 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1348 } else {
1349 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1350 AddUInt(Block, 0, DW_FORM_udata, Reg);
1351 }
1352 } else {
1353 if (Reg < 32) {
1354 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1355 } else {
1356 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1357 AddUInt(Block, 0, DW_FORM_udata, Reg);
1358 }
1359 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1360 }
1361
1362 AddBlock(Die, Attribute, 0, Block);
1363 }
1364
1365 /// AddBasicType - Add a new basic type attribute to the specified entity.
1366 ///
1367 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1368 const std::string &Name,
1369 unsigned Encoding, unsigned Size) {
1370 DIE *Die = ConstructBasicType(Unit, Name, Encoding, Size);
1371 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1372 }
1373
1374 /// ConstructBasicType - Construct a new basic type.
1375 ///
1376 DIE *ConstructBasicType(CompileUnit *Unit,
1377 const std::string &Name,
1378 unsigned Encoding, unsigned Size) {
1379 DIE Buffer(DW_TAG_base_type);
1380 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1381 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1382 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1383 return Unit->AddDie(Buffer);
1384 }
1385
1386 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1387 ///
1388 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1389 DIE *Die = ConstructPointerType(Unit, Name);
1390 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1391 }
1392
1393 /// ConstructPointerType - Construct a new pointer type.
1394 ///
1395 DIE *ConstructPointerType(CompileUnit *Unit, const std::string &Name) {
1396 DIE Buffer(DW_TAG_pointer_type);
1397 AddUInt(&Buffer, DW_AT_byte_size, 0, TAI->getAddressSize());
1398 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1399 return Unit->AddDie(Buffer);
1400 }
1401
1402 /// AddType - Add a new type attribute to the specified entity.
1403 ///
1404 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1405 if (!TyDesc) {
1406 AddBasicType(Entity, Unit, "", DW_ATE_signed, 4);
1407 } else {
1408 // Check for pre-existence.
1409 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
1410
1411 // If it exists then use the existing value.
1412 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001413 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1414 return;
1415 }
1416
1417 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1418 // FIXME - Not sure why programs and variables are coming through here.
1419 // Short cut for handling subprogram types (not really a TyDesc.)
1420 AddPointerType(Entity, Unit, SubprogramTy->getName());
1421 } else if (GlobalVariableDesc *GlobalTy =
1422 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1423 // FIXME - Not sure why programs and variables are coming through here.
1424 // Short cut for handling global variable types (not really a TyDesc.)
1425 AddPointerType(Entity, Unit, GlobalTy->getName());
1426 } else {
1427 // Set up proxy.
1428 Slot = NewDIEntry();
1429
1430 // Construct type.
1431 DIE Buffer(DW_TAG_base_type);
1432 ConstructType(Buffer, TyDesc, Unit);
1433
1434 // Add debug information entry to entity and unit.
1435 DIE *Die = Unit->AddDie(Buffer);
1436 SetDIEntry(Slot, Die);
1437 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1438 }
1439 }
1440 }
1441
1442 /// ConstructType - Adds all the required attributes to the type.
1443 ///
1444 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1445 // Get core information.
1446 const std::string &Name = TyDesc->getName();
1447 uint64_t Size = TyDesc->getSize() >> 3;
1448
1449 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1450 // Fundamental types like int, float, bool
1451 Buffer.setTag(DW_TAG_base_type);
1452 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1453 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00001454 // Fetch tag.
1455 unsigned Tag = DerivedTy->getTag();
1456 // FIXME - Workaround for templates.
1457 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1458 // Pointers, typedefs et al.
1459 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00001460 // Map to main type, void will not have a type.
1461 if (TypeDesc *FromTy = DerivedTy->getFromType())
1462 AddType(&Buffer, FromTy, Unit);
1463 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1464 // Fetch tag.
1465 unsigned Tag = CompTy->getTag();
1466
1467 // Set tag accordingly.
1468 if (Tag == DW_TAG_vector_type)
1469 Buffer.setTag(DW_TAG_array_type);
1470 else
1471 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00001472
Jim Laskeyef42a012006-11-02 20:12:39 +00001473 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1474
1475 switch (Tag) {
1476 case DW_TAG_vector_type:
1477 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1478 // Fall thru
1479 case DW_TAG_array_type: {
1480 // Add element type.
1481 if (TypeDesc *FromTy = CompTy->getFromType())
1482 AddType(&Buffer, FromTy, Unit);
1483
1484 // Don't emit size attribute.
1485 Size = 0;
1486
1487 // Construct an anonymous type for index type.
1488 DIE *IndexTy = ConstructBasicType(Unit, "", DW_ATE_signed, 4);
1489
1490 // Add subranges to array type.
1491 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1492 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1493 int64_t Lo = SRD->getLo();
1494 int64_t Hi = SRD->getHi();
1495 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1496
1497 // If a range is available.
1498 if (Lo != Hi) {
1499 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1500 // Only add low if non-zero.
1501 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1502 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1503 }
1504
1505 Buffer.AddChild(Subrange);
1506 }
1507 break;
1508 }
1509 case DW_TAG_structure_type:
1510 case DW_TAG_union_type: {
1511 // Add elements to structure type.
1512 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1513 DebugInfoDesc *Element = Elements[i];
1514
1515 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1516 // Add field or base class.
1517
1518 unsigned Tag = MemberDesc->getTag();
1519
1520 // Extract the basic information.
1521 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001522 uint64_t Size = MemberDesc->getSize();
1523 uint64_t Align = MemberDesc->getAlign();
1524 uint64_t Offset = MemberDesc->getOffset();
1525
1526 // Construct member debug information entry.
1527 DIE *Member = new DIE(Tag);
1528
1529 // Add name if not "".
1530 if (!Name.empty())
1531 AddString(Member, DW_AT_name, DW_FORM_string, Name);
1532 // Add location if available.
1533 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1534
1535 // Most of the time the field info is the same as the members.
1536 uint64_t FieldSize = Size;
1537 uint64_t FieldAlign = Align;
1538 uint64_t FieldOffset = Offset;
1539
1540 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1541 AddType(Member, FromTy, Unit);
1542 FieldSize = FromTy->getSize();
1543 FieldAlign = FromTy->getSize();
1544 }
1545
1546 // Unless we have a bit field.
1547 if (Tag == DW_TAG_member && FieldSize != Size) {
1548 // Construct the alignment mask.
1549 uint64_t AlignMask = ~(FieldAlign - 1);
1550 // Determine the high bit + 1 of the declared size.
1551 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1552 // Work backwards to determine the base offset of the field.
1553 FieldOffset = HiMark - FieldSize;
1554 // Now normalize offset to the field.
1555 Offset -= FieldOffset;
1556
1557 // Maybe we need to work from the other end.
1558 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1559
1560 // Add size and offset.
1561 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1562 AddUInt(Member, DW_AT_bit_size, 0, Size);
1563 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1564 }
1565
1566 // Add computation for offset.
1567 DIEBlock *Block = new DIEBlock();
1568 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1569 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1570 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1571
1572 // Add accessibility (public default unless is base class.
1573 if (MemberDesc->isProtected()) {
1574 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1575 } else if (MemberDesc->isPrivate()) {
1576 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1577 } else if (Tag == DW_TAG_inheritance) {
1578 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1579 }
1580
1581 Buffer.AddChild(Member);
1582 } else if (GlobalVariableDesc *StaticDesc =
1583 dyn_cast<GlobalVariableDesc>(Element)) {
1584 // Add static member.
1585
1586 // Construct member debug information entry.
1587 DIE *Static = new DIE(DW_TAG_variable);
1588
1589 // Add name and mangled name.
1590 const std::string &Name = StaticDesc->getDisplayName();
1591 const std::string &MangledName = StaticDesc->getName();
1592 AddString(Static, DW_AT_name, DW_FORM_string, Name);
1593 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1594 MangledName);
1595
1596 // Add location.
1597 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1598
1599 // Add type.
1600 if (TypeDesc *StaticTy = StaticDesc->getType())
1601 AddType(Static, StaticTy, Unit);
1602
1603 // Add flags.
1604 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
1605 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
1606
1607 Buffer.AddChild(Static);
1608 } else if (SubprogramDesc *MethodDesc =
1609 dyn_cast<SubprogramDesc>(Element)) {
1610 // Add member function.
1611
1612 // Construct member debug information entry.
1613 DIE *Method = new DIE(DW_TAG_subprogram);
1614
1615 // Add name and mangled name.
1616 const std::string &Name = MethodDesc->getDisplayName();
1617 const std::string &MangledName = MethodDesc->getName();
1618 bool IsCTor = false;
1619
1620 if (Name.empty()) {
1621 AddString(Method, DW_AT_name, DW_FORM_string, MangledName);
1622 IsCTor = TyDesc->getName() == MangledName;
1623 } else {
1624 AddString(Method, DW_AT_name, DW_FORM_string, Name);
1625 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
1626 MangledName);
1627 }
1628
1629 // Add location.
1630 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1631
1632 // Add type.
1633 if (CompositeTypeDesc *MethodTy =
1634 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1635 // Get argument information.
1636 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1637
1638 // If not a ctor.
1639 if (!IsCTor) {
1640 // Add return type.
1641 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1642 }
1643
1644 // Add arguments.
1645 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1646 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1647 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1648 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1649 Method->AddChild(Arg);
1650 }
1651 }
1652
1653 // Add flags.
1654 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
1655 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
1656
1657 Buffer.AddChild(Method);
1658 }
1659 }
1660 break;
1661 }
1662 case DW_TAG_enumeration_type: {
1663 // Add enumerators to enumeration type.
1664 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1665 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1666 const std::string &Name = ED->getName();
1667 int64_t Value = ED->getValue();
1668 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1669 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1670 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1671 Buffer.AddChild(Enumerator);
1672 }
1673
1674 break;
1675 }
1676 case DW_TAG_subroutine_type: {
1677 // Add prototype flag.
1678 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1679 // Add return type.
1680 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
1681
1682 // Add arguments.
1683 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1684 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1685 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1686 Buffer.AddChild(Arg);
1687 }
1688
1689 break;
1690 }
1691 default: break;
1692 }
1693 }
1694
1695 // Add size if non-zero (derived types don't have a size.)
1696 if (Size) AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1697 // Add name if not anonymous or intermediate type.
1698 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1699 // Add source line info if available.
1700 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
1701 }
1702
1703 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00001704 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001705 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1706 // Construct debug information entry.
1707 DIE *Die = new DIE(DW_TAG_compile_unit);
1708 AddDelta(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0),
1709 DWLabel("section_line", 0));
1710 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1711 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1712 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1713 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1714
1715 // Construct compile unit.
1716 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1717
1718 // Add Unit to compile unit map.
1719 DescToUnitMap[UnitDesc] = Unit;
1720
1721 return Unit;
1722 }
1723
Jim Laskey9d4209f2006-11-07 19:33:46 +00001724 /// GetBaseCompileUnit - Get the main compile unit.
1725 ///
1726 CompileUnit *GetBaseCompileUnit() const {
1727 CompileUnit *Unit = CompileUnits[0];
1728 assert(Unit && "Missing compile unit.");
1729 return Unit;
1730 }
1731
Jim Laskey65195462006-10-30 13:35:07 +00001732 /// FindCompileUnit - Get the compile unit for the given descriptor.
1733 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001734 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001735 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00001736 assert(Unit && "Missing compile unit.");
1737 return Unit;
1738 }
1739
1740 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00001741 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001742 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1743 // Get the compile unit context.
1744 CompileUnitDesc *UnitDesc =
1745 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001746 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001747
1748 // Check for pre-existence.
1749 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1750 if (Slot) return Slot;
1751
1752 // Get the global variable itself.
1753 GlobalVariable *GV = GVD->getGlobalVariable();
1754
1755 const std::string &Name = GVD->hasMangledName() ? GVD->getDisplayName()
1756 : GVD->getName();
1757 const std::string &MangledName = GVD->hasMangledName() ? GVD->getName()
1758 : "";
1759 // Create the global's variable DIE.
1760 DIE *VariableDie = new DIE(DW_TAG_variable);
1761 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
1762 if (!MangledName.empty()) {
1763 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1764 MangledName);
1765 }
1766 AddType(VariableDie, GVD->getType(), Unit);
1767 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1768
1769 // Add source line info if available.
1770 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1771
1772 // Work up linkage name.
1773 const std::string LinkageName = Asm->getGlobalLinkName(GV);
1774
1775 // Add address.
1776 DIEBlock *Block = new DIEBlock();
1777 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
1778 AddObjectLabel(Block, 0, DW_FORM_udata, LinkageName);
1779 AddBlock(VariableDie, DW_AT_location, 0, Block);
1780
1781 // Add to map.
1782 Slot = VariableDie;
1783
1784 // Add to context owner.
1785 Unit->getDie()->AddChild(VariableDie);
1786
1787 // Expose as global.
1788 // FIXME - need to check external flag.
1789 Unit->AddGlobal(Name, VariableDie);
1790
1791 return VariableDie;
1792 }
Jim Laskey65195462006-10-30 13:35:07 +00001793
1794 /// NewSubprogram - Add a new subprogram DIE.
1795 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001796 DIE *NewSubprogram(SubprogramDesc *SPD) {
1797 // Get the compile unit context.
1798 CompileUnitDesc *UnitDesc =
1799 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001800 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001801
1802 // Check for pre-existence.
1803 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1804 if (Slot) return Slot;
1805
1806 // Gather the details (simplify add attribute code.)
1807 const std::string &Name = SPD->hasMangledName() ? SPD->getDisplayName()
1808 : SPD->getName();
1809 const std::string &MangledName = SPD->hasMangledName() ? SPD->getName()
1810 : "";
1811 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
1812
1813 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1814 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
1815 if (!MangledName.empty()) {
1816 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1817 MangledName);
1818 }
1819 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
1820 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, IsExternal);
1821 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
1822
1823 // Add source line info if available.
1824 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1825
1826 // Add to map.
1827 Slot = SubprogramDie;
1828
1829 // Add to context owner.
1830 Unit->getDie()->AddChild(SubprogramDie);
1831
1832 // Expose as global.
1833 Unit->AddGlobal(Name, SubprogramDie);
1834
1835 return SubprogramDie;
1836 }
Jim Laskey65195462006-10-30 13:35:07 +00001837
1838 /// NewScopeVariable - Create a new scope variable.
1839 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001840 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1841 // Get the descriptor.
1842 VariableDesc *VD = DV->getDesc();
1843
1844 // Translate tag to proper Dwarf tag. The result variable is dropped for
1845 // now.
1846 unsigned Tag;
1847 switch (VD->getTag()) {
1848 case DW_TAG_return_variable: return NULL;
1849 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1850 case DW_TAG_auto_variable: // fall thru
1851 default: Tag = DW_TAG_variable; break;
1852 }
1853
1854 // Define variable debug information entry.
1855 DIE *VariableDie = new DIE(Tag);
1856 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1857
1858 // Add source line info if available.
1859 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1860
1861 // Add variable type.
1862 AddType(VariableDie, VD->getType(), Unit);
1863
1864 // Add variable address.
1865 MachineLocation Location;
1866 RI->getLocation(*MF, DV->getFrameIndex(), Location);
1867 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001868
Jim Laskeyef42a012006-11-02 20:12:39 +00001869 return VariableDie;
1870 }
Jim Laskey65195462006-10-30 13:35:07 +00001871
1872 /// ConstructScope - Construct the components of a scope.
1873 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001874 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00001875 unsigned ParentStartID, unsigned ParentEndID,
1876 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001877 // Add variables to scope.
1878 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1879 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1880 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1881 if (VariableDie) ParentDie->AddChild(VariableDie);
1882 }
1883
1884 // Add nested scopes.
1885 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1886 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1887 // Define the Scope debug information entry.
1888 DebugScope *Scope = Scopes[j];
1889 // FIXME - Ignore inlined functions for the time being.
1890 if (!Scope->getParent()) continue;
1891
Jim Laskey9d4209f2006-11-07 19:33:46 +00001892 unsigned StartID = DebugInfo->MappedLabel(Scope->getStartLabelID());
1893 unsigned EndID = DebugInfo->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00001894
Jim Laskey9d4209f2006-11-07 19:33:46 +00001895 // Ignore empty scopes.
1896 if (StartID == EndID && StartID != 0) continue;
1897 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00001898
Jim Laskey36729dd2006-11-29 16:55:57 +00001899 if (StartID == ParentStartID && EndID == ParentEndID) {
1900 // Just add stuff to the parent scope.
1901 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001902 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00001903 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1904
1905 // Add the scope bounds.
1906 if (StartID) {
1907 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1908 DWLabel("loc", StartID));
1909 } else {
1910 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1911 DWLabel("func_begin", SubprogramCount));
1912 }
1913 if (EndID) {
1914 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1915 DWLabel("loc", EndID));
1916 } else {
1917 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1918 DWLabel("func_end", SubprogramCount));
1919 }
1920
1921 // Add the scope contents.
1922 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
1923 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001924 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001925 }
1926 }
Jim Laskey65195462006-10-30 13:35:07 +00001927
1928 /// ConstructRootScope - Construct the scope for the subprogram.
1929 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001930 void ConstructRootScope(DebugScope *RootScope) {
1931 // Exit if there is no root scope.
1932 if (!RootScope) return;
1933
1934 // Get the subprogram debug information entry.
1935 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1936
1937 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00001938 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001939
1940 // Get the subprogram die.
1941 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
1942 assert(SPDie && "Missing subprogram descriptor");
1943
1944 // Add the function bounds.
1945 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1946 DWLabel("func_begin", SubprogramCount));
1947 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1948 DWLabel("func_end", SubprogramCount));
1949 MachineLocation Location(RI->getFrameRegister(*MF));
1950 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001951
Jim Laskey36729dd2006-11-29 16:55:57 +00001952 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001953 }
Jim Laskey65195462006-10-30 13:35:07 +00001954
Jim Laskeyef42a012006-11-02 20:12:39 +00001955 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1956 /// tools to recognize the object file contains Dwarf information.
1957 void EmitInitial() {
1958 // Check to see if we already emitted intial headers.
1959 if (didInitial) return;
1960 didInitial = true;
1961
1962 // Dwarf sections base addresses.
1963 if (TAI->getDwarfRequiresFrameSection()) {
1964 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1965 EmitLabel("section_frame", 0);
1966 }
1967 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
1968 EmitLabel("section_info", 0);
1969 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
1970 EmitLabel("section_abbrev", 0);
1971 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
1972 EmitLabel("section_aranges", 0);
1973 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
1974 EmitLabel("section_macinfo", 0);
1975 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
1976 EmitLabel("section_line", 0);
1977 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
1978 EmitLabel("section_loc", 0);
1979 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
1980 EmitLabel("section_pubnames", 0);
1981 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
1982 EmitLabel("section_str", 0);
1983 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
1984 EmitLabel("section_ranges", 0);
1985
1986 Asm->SwitchToTextSection(TAI->getTextSection());
1987 EmitLabel("text_begin", 0);
1988 Asm->SwitchToDataSection(TAI->getDataSection());
1989 EmitLabel("data_begin", 0);
1990
1991 // Emit common frame information.
1992 EmitInitialDebugFrame();
1993 }
1994
Jim Laskey65195462006-10-30 13:35:07 +00001995 /// EmitDIE - Recusively Emits a debug information entry.
1996 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001997 void EmitDIE(DIE *Die) const {
1998 // Get the abbreviation for this DIE.
1999 unsigned AbbrevNumber = Die->getAbbrevNumber();
2000 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2001
2002 O << "\n";
2003
2004 // Emit the code (index) for the abbreviation.
2005 EmitULEB128Bytes(AbbrevNumber);
2006 EOL(std::string("Abbrev [" +
2007 utostr(AbbrevNumber) +
2008 "] 0x" + utohexstr(Die->getOffset()) +
2009 ":0x" + utohexstr(Die->getSize()) + " " +
2010 TagString(Abbrev->getTag())));
2011
2012 const std::vector<DIEValue *> &Values = Die->getValues();
2013 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2014
2015 // Emit the DIE attribute values.
2016 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2017 unsigned Attr = AbbrevData[i].getAttribute();
2018 unsigned Form = AbbrevData[i].getForm();
2019 assert(Form && "Too many attributes for DIE (check abbreviation)");
2020
2021 switch (Attr) {
2022 case DW_AT_sibling: {
2023 EmitInt32(Die->SiblingOffset());
2024 break;
2025 }
2026 default: {
2027 // Emit an attribute using the defined form.
2028 Values[i]->EmitValue(*this, Form);
2029 break;
2030 }
2031 }
2032
2033 EOL(AttributeString(Attr));
2034 }
2035
2036 // Emit the DIE children if any.
2037 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2038 const std::vector<DIE *> &Children = Die->getChildren();
2039
2040 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2041 EmitDIE(Children[j]);
2042 }
2043
2044 EmitInt8(0); EOL("End Of Children Mark");
2045 }
2046 }
2047
Jim Laskey65195462006-10-30 13:35:07 +00002048 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2049 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002050 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2051 // Get the children.
2052 const std::vector<DIE *> &Children = Die->getChildren();
2053
2054 // If not last sibling and has children then add sibling offset attribute.
2055 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2056
2057 // Record the abbreviation.
2058 AssignAbbrevNumber(Die->getAbbrev());
2059
2060 // Get the abbreviation for this DIE.
2061 unsigned AbbrevNumber = Die->getAbbrevNumber();
2062 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2063
2064 // Set DIE offset
2065 Die->setOffset(Offset);
2066
2067 // Start the size with the size of abbreviation code.
2068 Offset += SizeULEB128(AbbrevNumber);
2069
2070 const std::vector<DIEValue *> &Values = Die->getValues();
2071 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2072
2073 // Size the DIE attribute values.
2074 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2075 // Size attribute value.
2076 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2077 }
2078
2079 // Size the DIE children if any.
2080 if (!Children.empty()) {
2081 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2082 "Children flag not set");
2083
2084 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2085 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2086 }
2087
2088 // End of children marker.
2089 Offset += sizeof(int8_t);
2090 }
2091
2092 Die->setSize(Offset - Die->getOffset());
2093 return Offset;
2094 }
Jim Laskey65195462006-10-30 13:35:07 +00002095
2096 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2097 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002098 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002099 // Process base compile unit.
2100 CompileUnit *Unit = GetBaseCompileUnit();
2101 // Compute size of compile unit header
2102 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2103 sizeof(int16_t) + // DWARF version number
2104 sizeof(int32_t) + // Offset Into Abbrev. Section
2105 sizeof(int8_t); // Pointer Size (in bytes)
2106 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002107 }
2108
Jim Laskey65195462006-10-30 13:35:07 +00002109 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
2110 /// frame.
2111 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Jim Laskeyef42a012006-11-02 20:12:39 +00002112 std::vector<MachineMove *> &Moves) {
2113 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
2114 MachineMove *Move = Moves[i];
Jim Laskey9d4209f2006-11-07 19:33:46 +00002115 unsigned LabelID = DebugInfo->MappedLabel(Move->getLabelID());
Jim Laskeyef42a012006-11-02 20:12:39 +00002116
2117 // Throw out move if the label is invalid.
Jim Laskey9d4209f2006-11-07 19:33:46 +00002118 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002119
2120 const MachineLocation &Dst = Move->getDestination();
2121 const MachineLocation &Src = Move->getSource();
2122
2123 // Advance row if new location.
2124 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
2125 EmitInt8(DW_CFA_advance_loc4);
2126 EOL("DW_CFA_advance_loc4");
2127 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
2128 EOL("");
2129
2130 BaseLabelID = LabelID;
2131 BaseLabel = "loc";
2132 }
2133
2134 int stackGrowth =
2135 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2136 TargetFrameInfo::StackGrowsUp ?
2137 TAI->getAddressSize() : -TAI->getAddressSize();
2138
2139 // If advancing cfa.
2140 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
2141 if (!Src.isRegister()) {
2142 if (Src.getRegister() == MachineLocation::VirtualFP) {
2143 EmitInt8(DW_CFA_def_cfa_offset);
2144 EOL("DW_CFA_def_cfa_offset");
2145 } else {
2146 EmitInt8(DW_CFA_def_cfa);
2147 EOL("DW_CFA_def_cfa");
Jim Laskeyef42a012006-11-02 20:12:39 +00002148 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
2149 EOL("Register");
2150 }
2151
2152 int Offset = Src.getOffset() / stackGrowth;
2153
2154 EmitULEB128Bytes(Offset);
2155 EOL("Offset");
2156 } else {
2157 assert(0 && "Machine move no supported yet.");
2158 }
2159 } else {
2160 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
2161 int Offset = Dst.getOffset() / stackGrowth;
2162
2163 if (Offset < 0) {
2164 EmitInt8(DW_CFA_offset_extended_sf);
2165 EOL("DW_CFA_offset_extended_sf");
2166 EmitULEB128Bytes(Reg);
2167 EOL("Reg");
2168 EmitSLEB128Bytes(Offset);
2169 EOL("Offset");
2170 } else if (Reg < 64) {
2171 EmitInt8(DW_CFA_offset + Reg);
2172 EOL("DW_CFA_offset + Reg");
2173 EmitULEB128Bytes(Offset);
2174 EOL("Offset");
2175 } else {
2176 EmitInt8(DW_CFA_offset_extended);
2177 EOL("DW_CFA_offset_extended");
2178 EmitULEB128Bytes(Reg);
2179 EOL("Reg");
2180 EmitULEB128Bytes(Offset);
2181 EOL("Offset");
2182 }
2183 }
2184 }
2185 }
Jim Laskey65195462006-10-30 13:35:07 +00002186
2187 /// EmitDebugInfo - Emit the debug info section.
2188 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002189 void EmitDebugInfo() const {
2190 // Start debug info section.
2191 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2192
Jim Laskey5496f012006-11-09 14:52:14 +00002193 CompileUnit *Unit = GetBaseCompileUnit();
2194 DIE *Die = Unit->getDie();
2195 // Emit the compile units header.
2196 EmitLabel("info_begin", Unit->getID());
2197 // Emit size of content not including length itself
2198 unsigned ContentSize = Die->getSize() +
2199 sizeof(int16_t) + // DWARF version number
2200 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00002201 sizeof(int8_t) + // Pointer Size (in bytes)
2202 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Laskey5496f012006-11-09 14:52:14 +00002203
2204 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2205 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2206 EmitDifference("abbrev_begin", 0, "section_abbrev", 0);
2207 EOL("Offset Into Abbrev. Section");
2208 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
2209
2210 EmitDIE(Die);
Jim Laskey749b01d2006-11-30 11:09:42 +00002211 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
2212 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
2213 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
2214 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
Jim Laskey5496f012006-11-09 14:52:14 +00002215 EmitLabel("info_end", Unit->getID());
2216
2217 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002218 }
2219
Jim Laskey65195462006-10-30 13:35:07 +00002220 /// EmitAbbreviations - Emit the abbreviation section.
2221 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002222 void EmitAbbreviations() const {
2223 // Check to see if it is worth the effort.
2224 if (!Abbreviations.empty()) {
2225 // Start the debug abbrev section.
2226 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2227
2228 EmitLabel("abbrev_begin", 0);
2229
2230 // For each abbrevation.
2231 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2232 // Get abbreviation data
2233 const DIEAbbrev *Abbrev = Abbreviations[i];
2234
2235 // Emit the abbrevations code (base 1 index.)
2236 EmitULEB128Bytes(Abbrev->getNumber()); EOL("Abbreviation Code");
2237
2238 // Emit the abbreviations data.
2239 Abbrev->Emit(*this);
2240
2241 O << "\n";
2242 }
2243
2244 EmitLabel("abbrev_end", 0);
2245
2246 O << "\n";
2247 }
2248 }
2249
Jim Laskey65195462006-10-30 13:35:07 +00002250 /// EmitDebugLines - Emit source line information.
2251 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002252 void EmitDebugLines() const {
2253 // Minimum line delta, thus ranging from -10..(255-10).
2254 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2255 // Maximum line delta, thus ranging from -10..(255-10).
2256 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002257
Jim Laskeyef42a012006-11-02 20:12:39 +00002258 // Start the dwarf line section.
2259 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2260
2261 // Construct the section header.
2262
2263 EmitDifference("line_end", 0, "line_begin", 0);
2264 EOL("Length of Source Line Info");
2265 EmitLabel("line_begin", 0);
2266
2267 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2268
2269 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
2270 EOL("Prolog Length");
2271 EmitLabel("line_prolog_begin", 0);
2272
2273 EmitInt8(1); EOL("Minimum Instruction Length");
2274
2275 EmitInt8(1); EOL("Default is_stmt_start flag");
2276
2277 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
2278
2279 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
2280
2281 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
2282
2283 // Line number standard opcode encodings argument count
2284 EmitInt8(0); EOL("DW_LNS_copy arg count");
2285 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2286 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2287 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2288 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2289 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2290 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2291 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2292 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
2293
2294 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2295 const UniqueVector<SourceFileInfo>
2296 &SourceFiles = DebugInfo->getSourceFiles();
2297
2298 // Emit directories.
2299 for (unsigned DirectoryID = 1, NDID = Directories.size();
2300 DirectoryID <= NDID; ++DirectoryID) {
2301 EmitString(Directories[DirectoryID]); EOL("Directory");
2302 }
2303 EmitInt8(0); EOL("End of directories");
2304
2305 // Emit files.
2306 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2307 SourceID <= NSID; ++SourceID) {
2308 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2309 EmitString(SourceFile.getName()); EOL("Source");
2310 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2311 EmitULEB128Bytes(0); EOL("Mod date");
2312 EmitULEB128Bytes(0); EOL("File size");
2313 }
2314 EmitInt8(0); EOL("End of files");
2315
2316 EmitLabel("line_prolog_end", 0);
2317
2318 // A sequence for each text section.
2319 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2320 // Isolate current sections line info.
2321 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
2322
2323 if (DwarfVerbose) {
2324 O << "\t"
2325 << TAI->getCommentString() << " "
2326 << "Section "
2327 << SectionMap[j + 1].c_str() << "\n";
2328 }
2329
2330 // Dwarf assumes we start with first line of first source file.
2331 unsigned Source = 1;
2332 unsigned Line = 1;
2333
2334 // Construct rows of the address, source, line, column matrix.
2335 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2336 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey9d4209f2006-11-07 19:33:46 +00002337 unsigned LabelID = DebugInfo->MappedLabel(LineInfo.getLabelID());
2338 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002339
2340 if (DwarfVerbose) {
2341 unsigned SourceID = LineInfo.getSourceID();
2342 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2343 unsigned DirectoryID = SourceFile.getDirectoryID();
2344 O << "\t"
2345 << TAI->getCommentString() << " "
2346 << Directories[DirectoryID]
2347 << SourceFile.getName() << ":"
2348 << LineInfo.getLine() << "\n";
2349 }
2350
2351 // Define the line address.
2352 EmitInt8(0); EOL("Extended Op");
2353 EmitInt8(4 + 1); EOL("Op size");
2354 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2355 EmitReference("loc", LabelID); EOL("Location label");
2356
2357 // If change of source, then switch to the new source.
2358 if (Source != LineInfo.getSourceID()) {
2359 Source = LineInfo.getSourceID();
2360 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2361 EmitULEB128Bytes(Source); EOL("New Source");
2362 }
2363
2364 // If change of line.
2365 if (Line != LineInfo.getLine()) {
2366 // Determine offset.
2367 int Offset = LineInfo.getLine() - Line;
2368 int Delta = Offset - MinLineDelta;
2369
2370 // Update line.
2371 Line = LineInfo.getLine();
2372
2373 // If delta is small enough and in range...
2374 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2375 // ... then use fast opcode.
2376 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2377 } else {
2378 // ... otherwise use long hand.
2379 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2380 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2381 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2382 }
2383 } else {
2384 // Copy the previous row (different address or source)
2385 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2386 }
2387 }
2388
2389 // Define last address of section.
2390 EmitInt8(0); EOL("Extended Op");
2391 EmitInt8(4 + 1); EOL("Op size");
2392 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2393 EmitReference("section_end", j + 1); EOL("Section end label");
2394
2395 // Mark end of matrix.
2396 EmitInt8(0); EOL("DW_LNE_end_sequence");
2397 EmitULEB128Bytes(1); O << "\n";
2398 EmitInt8(1); O << "\n";
2399 }
2400
2401 EmitLabel("line_end", 0);
2402
2403 O << "\n";
2404 }
2405
Jim Laskey65195462006-10-30 13:35:07 +00002406 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
2407 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002408 void EmitInitialDebugFrame() {
2409 if (!TAI->getDwarfRequiresFrameSection())
2410 return;
2411
2412 int stackGrowth =
2413 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2414 TargetFrameInfo::StackGrowsUp ?
2415 TAI->getAddressSize() : -TAI->getAddressSize();
2416
2417 // Start the dwarf frame section.
2418 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2419
2420 EmitLabel("frame_common", 0);
2421 EmitDifference("frame_common_end", 0,
2422 "frame_common_begin", 0);
2423 EOL("Length of Common Information Entry");
2424
2425 EmitLabel("frame_common_begin", 0);
2426 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2427 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2428 EmitString(""); EOL("CIE Augmentation");
2429 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
2430 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
2431 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
Jim Laskey65195462006-10-30 13:35:07 +00002432
Jim Laskeyef42a012006-11-02 20:12:39 +00002433 std::vector<MachineMove *> Moves;
2434 RI->getInitialFrameState(Moves);
2435 EmitFrameMoves(NULL, 0, Moves);
2436 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2437
2438 EmitAlign(2);
2439 EmitLabel("frame_common_end", 0);
2440
2441 O << "\n";
2442 }
2443
Jim Laskey65195462006-10-30 13:35:07 +00002444 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2445 /// section.
Jim Laskeyef42a012006-11-02 20:12:39 +00002446 void EmitFunctionDebugFrame() {
Reid Spencer5a4951e2006-11-07 06:36:36 +00002447 if (!TAI->getDwarfRequiresFrameSection())
2448 return;
Jim Laskey9d4209f2006-11-07 19:33:46 +00002449
Jim Laskeyef42a012006-11-02 20:12:39 +00002450 // Start the dwarf frame section.
2451 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2452
2453 EmitDifference("frame_end", SubprogramCount,
2454 "frame_begin", SubprogramCount);
2455 EOL("Length of Frame Information Entry");
2456
2457 EmitLabel("frame_begin", SubprogramCount);
2458
2459 EmitDifference("frame_common", 0, "section_frame", 0);
2460 EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002461
Jim Laskeyef42a012006-11-02 20:12:39 +00002462 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2463 EmitDifference("func_end", SubprogramCount,
2464 "func_begin", SubprogramCount);
2465 EOL("FDE address range");
2466
2467 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2468
2469 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2470
2471 EmitAlign(2);
2472 EmitLabel("frame_end", SubprogramCount);
2473
2474 O << "\n";
2475 }
2476
2477 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002478 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002479 void EmitDebugPubNames() {
2480 // Start the dwarf pubnames section.
2481 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2482
Jim Laskey5496f012006-11-09 14:52:14 +00002483 CompileUnit *Unit = GetBaseCompileUnit();
2484
2485 EmitDifference("pubnames_end", Unit->getID(),
2486 "pubnames_begin", Unit->getID());
2487 EOL("Length of Public Names Info");
2488
2489 EmitLabel("pubnames_begin", Unit->getID());
2490
2491 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2492
2493 EmitDifference("info_begin", Unit->getID(), "section_info", 0);
2494 EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002495
Jim Laskey5496f012006-11-09 14:52:14 +00002496 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2497 EOL("Compilation Unit Length");
2498
2499 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2500
2501 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2502 GE = Globals.end();
2503 GI != GE; ++GI) {
2504 const std::string &Name = GI->first;
2505 DIE * Entity = GI->second;
Jim Laskeyef42a012006-11-02 20:12:39 +00002506
Jim Laskey5496f012006-11-09 14:52:14 +00002507 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2508 EmitString(Name); EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00002509 }
Jim Laskey5496f012006-11-09 14:52:14 +00002510
2511 EmitInt32(0); EOL("End Mark");
2512 EmitLabel("pubnames_end", Unit->getID());
2513
2514 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002515 }
2516
2517 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002518 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002519 void EmitDebugStr() {
2520 // Check to see if it is worth the effort.
2521 if (!StringPool.empty()) {
2522 // Start the dwarf str section.
2523 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2524
2525 // For each of strings in the string pool.
2526 for (unsigned StringID = 1, N = StringPool.size();
2527 StringID <= N; ++StringID) {
2528 // Emit a label for reference from debug information entries.
2529 EmitLabel("string", StringID);
2530 // Emit the string itself.
2531 const std::string &String = StringPool[StringID];
2532 EmitString(String); O << "\n";
2533 }
2534
2535 O << "\n";
2536 }
2537 }
2538
2539 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002540 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002541 void EmitDebugLoc() {
2542 // Start the dwarf loc section.
2543 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2544
2545 O << "\n";
2546 }
2547
2548 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002549 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002550 void EmitDebugARanges() {
2551 // Start the dwarf aranges section.
2552 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2553
2554 // FIXME - Mock up
2555 #if 0
Jim Laskey5496f012006-11-09 14:52:14 +00002556 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002557
Jim Laskey5496f012006-11-09 14:52:14 +00002558 // Don't include size of length
2559 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2560
2561 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2562
2563 EmitReference("info_begin", Unit->getID());
2564 EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002565
Jim Laskey5496f012006-11-09 14:52:14 +00002566 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002567
Jim Laskey5496f012006-11-09 14:52:14 +00002568 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002569
Jim Laskey5496f012006-11-09 14:52:14 +00002570 EmitInt16(0); EOL("Pad (1)");
2571 EmitInt16(0); EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002572
Jim Laskey5496f012006-11-09 14:52:14 +00002573 // Range 1
2574 EmitReference("text_begin", 0); EOL("Address");
2575 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002576
Jim Laskey5496f012006-11-09 14:52:14 +00002577 EmitInt32(0); EOL("EOM (1)");
2578 EmitInt32(0); EOL("EOM (2)");
2579
2580 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002581 #endif
2582 }
2583
2584 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002585 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002586 void EmitDebugRanges() {
2587 // Start the dwarf ranges section.
2588 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2589
2590 O << "\n";
2591 }
2592
2593 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002594 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002595 void EmitDebugMacInfo() {
2596 // Start the dwarf macinfo section.
2597 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2598
2599 O << "\n";
2600 }
2601
Jim Laskey65195462006-10-30 13:35:07 +00002602 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2603 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002604 void ConstructCompileUnitDIEs() {
2605 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
2606
2607 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00002608 unsigned ID = DebugInfo->RecordSource(CUW[i]);
2609 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002610 CompileUnits.push_back(Unit);
2611 }
2612 }
2613
Jim Laskey65195462006-10-30 13:35:07 +00002614 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2615 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002616 void ConstructGlobalDIEs() {
2617 std::vector<GlobalVariableDesc *> GlobalVariables =
2618 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
2619
2620 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2621 GlobalVariableDesc *GVD = GlobalVariables[i];
2622 NewGlobalVariable(GVD);
2623 }
2624 }
Jim Laskey65195462006-10-30 13:35:07 +00002625
2626 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2627 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002628 void ConstructSubprogramDIEs() {
2629 std::vector<SubprogramDesc *> Subprograms =
2630 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
2631
2632 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2633 SubprogramDesc *SPD = Subprograms[i];
2634 NewSubprogram(SPD);
2635 }
2636 }
Jim Laskey65195462006-10-30 13:35:07 +00002637
2638 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
2639 ///
2640 bool ShouldEmitDwarf() const { return shouldEmit; }
2641
2642public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002643 //===--------------------------------------------------------------------===//
2644 // Main entry points.
2645 //
2646 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
2647 : O(OS)
2648 , Asm(A)
2649 , TAI(T)
2650 , TD(Asm->TM.getTargetData())
2651 , RI(Asm->TM.getRegisterInfo())
2652 , M(NULL)
2653 , MF(NULL)
2654 , DebugInfo(NULL)
2655 , didInitial(false)
2656 , shouldEmit(false)
2657 , SubprogramCount(0)
2658 , CompileUnits()
2659 , AbbreviationsSet(InitAbbreviationsSetSize)
2660 , Abbreviations()
2661 , ValuesSet(InitValuesSetSize)
2662 , Values()
2663 , StringPool()
2664 , DescToUnitMap()
2665 , SectionMap()
2666 , SectionSourceLines()
2667 {
2668 }
2669 virtual ~Dwarf() {
2670 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2671 delete CompileUnits[i];
2672 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2673 delete Values[j];
2674 }
2675
Jim Laskey65195462006-10-30 13:35:07 +00002676 // Accessors.
2677 //
2678 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
2679
2680 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2681 /// created it. Set by the target AsmPrinter.
Jim Laskeyef42a012006-11-02 20:12:39 +00002682 void SetDebugInfo(MachineDebugInfo *DI) {
2683 // Make sure initial declarations are made.
2684 if (!DebugInfo && DI->hasInfo()) {
2685 DebugInfo = DI;
2686 shouldEmit = true;
2687
2688 // Emit initial sections
2689 EmitInitial();
2690
2691 // Create all the compile unit DIEs.
2692 ConstructCompileUnitDIEs();
2693
2694 // Create DIEs for each of the externally visible global variables.
2695 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002696
Jim Laskeyef42a012006-11-02 20:12:39 +00002697 // Create DIEs for each of the externally visible subprograms.
2698 ConstructSubprogramDIEs();
2699
2700 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00002701 SectionMap.insert(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002702 }
2703 }
2704
Jim Laskey65195462006-10-30 13:35:07 +00002705 /// BeginModule - Emit all Dwarf sections that should come prior to the
2706 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002707 void BeginModule(Module *M) {
2708 this->M = M;
2709
2710 if (!ShouldEmitDwarf()) return;
2711 EOL("Dwarf Begin Module");
2712 }
2713
Jim Laskey65195462006-10-30 13:35:07 +00002714 /// EndModule - Emit all Dwarf sections that should come after the content.
2715 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002716 void EndModule() {
2717 if (!ShouldEmitDwarf()) return;
2718 EOL("Dwarf End Module");
2719
2720 // Standard sections final addresses.
2721 Asm->SwitchToTextSection(TAI->getTextSection());
2722 EmitLabel("text_end", 0);
2723 Asm->SwitchToDataSection(TAI->getDataSection());
2724 EmitLabel("data_end", 0);
2725
2726 // End text sections.
2727 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2728 Asm->SwitchToTextSection(SectionMap[i].c_str());
2729 EmitLabel("section_end", i);
2730 }
2731
2732 // Compute DIE offsets and sizes.
2733 SizeAndOffsets();
2734
2735 // Emit all the DIEs into a debug info section
2736 EmitDebugInfo();
2737
2738 // Corresponding abbreviations into a abbrev section.
2739 EmitAbbreviations();
2740
2741 // Emit source line correspondence into a debug line section.
2742 EmitDebugLines();
2743
2744 // Emit info into a debug pubnames section.
2745 EmitDebugPubNames();
2746
2747 // Emit info into a debug str section.
2748 EmitDebugStr();
2749
2750 // Emit info into a debug loc section.
2751 EmitDebugLoc();
2752
2753 // Emit info into a debug aranges section.
2754 EmitDebugARanges();
2755
2756 // Emit info into a debug ranges section.
2757 EmitDebugRanges();
2758
2759 // Emit info into a debug macinfo section.
2760 EmitDebugMacInfo();
2761 }
2762
Jim Laskey65195462006-10-30 13:35:07 +00002763 /// BeginFunction - Gather pre-function debug information. Assumes being
2764 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002765 void BeginFunction(MachineFunction *MF) {
2766 this->MF = MF;
2767
2768 if (!ShouldEmitDwarf()) return;
2769 EOL("Dwarf Begin Function");
2770
2771 // Begin accumulating function debug information.
2772 DebugInfo->BeginFunction(MF);
2773
2774 // Assumes in correct section after the entry point.
2775 EmitLabel("func_begin", ++SubprogramCount);
2776 }
2777
Jim Laskey65195462006-10-30 13:35:07 +00002778 /// EndFunction - Gather and emit post-function debug information.
2779 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002780 void EndFunction() {
2781 if (!ShouldEmitDwarf()) return;
2782 EOL("Dwarf End Function");
2783
2784 // Define end label for subprogram.
2785 EmitLabel("func_end", SubprogramCount);
2786
2787 // Get function line info.
2788 const std::vector<SourceLineInfo> &LineInfos = DebugInfo->getSourceLines();
2789
2790 if (!LineInfos.empty()) {
2791 // Get section line info.
2792 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2793 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2794 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2795 // Append the function info to section info.
2796 SectionLineInfos.insert(SectionLineInfos.end(),
2797 LineInfos.begin(), LineInfos.end());
2798 }
2799
2800 // Construct scopes for subprogram.
2801 ConstructRootScope(DebugInfo->getRootScope());
2802
2803 // Emit function frame information.
2804 EmitFunctionDebugFrame();
2805
2806 // Reset the line numbers for the next function.
2807 DebugInfo->ClearLineInfo();
2808
2809 // Clear function debug information.
2810 DebugInfo->EndFunction();
2811 }
Jim Laskey65195462006-10-30 13:35:07 +00002812};
2813
Jim Laskey0d086af2006-02-27 12:43:29 +00002814} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00002815
2816//===----------------------------------------------------------------------===//
2817
Jim Laskeyd18e2892006-01-20 20:34:06 +00002818/// Emit - Print the abbreviation using the specified Dwarf writer.
2819///
Jim Laskey65195462006-10-30 13:35:07 +00002820void DIEAbbrev::Emit(const Dwarf &DW) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002821 // Emit its Dwarf tag type.
2822 DW.EmitULEB128Bytes(Tag);
2823 DW.EOL(TagString(Tag));
2824
2825 // Emit whether it has children DIEs.
2826 DW.EmitULEB128Bytes(ChildrenFlag);
2827 DW.EOL(ChildrenString(ChildrenFlag));
2828
2829 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00002830 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002831 const DIEAbbrevData &AttrData = Data[i];
2832
2833 // Emit attribute type.
2834 DW.EmitULEB128Bytes(AttrData.getAttribute());
2835 DW.EOL(AttributeString(AttrData.getAttribute()));
2836
2837 // Emit form type.
2838 DW.EmitULEB128Bytes(AttrData.getForm());
2839 DW.EOL(FormEncodingString(AttrData.getForm()));
2840 }
2841
2842 // Mark end of abbreviation.
2843 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
2844 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
2845}
2846
2847#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00002848void DIEAbbrev::print(std::ostream &O) {
2849 O << "Abbreviation @"
2850 << std::hex << (intptr_t)this << std::dec
2851 << " "
2852 << TagString(Tag)
2853 << " "
2854 << ChildrenString(ChildrenFlag)
2855 << "\n";
2856
2857 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
2858 O << " "
2859 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002860 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00002861 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002862 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002863 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00002864}
Bill Wendlingbdc679d2006-11-29 00:39:47 +00002865void DIEAbbrev::dump() { print(llvm_cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002866#endif
2867
2868//===----------------------------------------------------------------------===//
2869
Jim Laskeyef42a012006-11-02 20:12:39 +00002870#ifndef NDEBUG
2871void DIEValue::dump() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +00002872 print(llvm_cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002873}
Jim Laskeyef42a012006-11-02 20:12:39 +00002874#endif
2875
2876//===----------------------------------------------------------------------===//
2877
Jim Laskey063e7652006-01-17 17:31:53 +00002878/// EmitValue - Emit integer of appropriate size.
2879///
Jim Laskey65195462006-10-30 13:35:07 +00002880void DIEInteger::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +00002881 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00002882 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00002883 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002884 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002885 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002886 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002887 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002888 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002889 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002890 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +00002891 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
2892 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +00002893 default: assert(0 && "DIE Value form not supported yet"); break;
2894 }
2895}
2896
Jim Laskey063e7652006-01-17 17:31:53 +00002897//===----------------------------------------------------------------------===//
2898
2899/// EmitValue - Emit string value.
2900///
Jim Laskey65195462006-10-30 13:35:07 +00002901void DIEString::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002902 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00002903}
2904
Jim Laskey063e7652006-01-17 17:31:53 +00002905//===----------------------------------------------------------------------===//
2906
2907/// EmitValue - Emit label value.
2908///
Jim Laskey65195462006-10-30 13:35:07 +00002909void DIEDwarfLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002910 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +00002911}
2912
2913/// SizeOf - Determine size of label value in bytes.
2914///
Jim Laskey65195462006-10-30 13:35:07 +00002915unsigned DIEDwarfLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002916 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002917}
Jim Laskeyef42a012006-11-02 20:12:39 +00002918
Jim Laskey063e7652006-01-17 17:31:53 +00002919//===----------------------------------------------------------------------===//
2920
Jim Laskeyd18e2892006-01-20 20:34:06 +00002921/// EmitValue - Emit label value.
2922///
Jim Laskey65195462006-10-30 13:35:07 +00002923void DIEObjectLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002924 DW.EmitReference(Label);
2925}
2926
2927/// SizeOf - Determine size of label value in bytes.
2928///
Jim Laskey65195462006-10-30 13:35:07 +00002929unsigned DIEObjectLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002930 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00002931}
2932
2933//===----------------------------------------------------------------------===//
2934
Jim Laskey063e7652006-01-17 17:31:53 +00002935/// EmitValue - Emit delta value.
2936///
Jim Laskey65195462006-10-30 13:35:07 +00002937void DIEDelta::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002938 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +00002939}
2940
2941/// SizeOf - Determine size of delta value in bytes.
2942///
Jim Laskey65195462006-10-30 13:35:07 +00002943unsigned DIEDelta::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002944 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002945}
2946
2947//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00002948
Jim Laskeyb8509c52006-03-23 18:07:55 +00002949/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002950///
Jim Laskey65195462006-10-30 13:35:07 +00002951void DIEntry::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +00002952 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00002953}
Jim Laskeyd18e2892006-01-20 20:34:06 +00002954
2955//===----------------------------------------------------------------------===//
2956
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002957/// ComputeSize - calculate the size of the block.
2958///
Jim Laskey65195462006-10-30 13:35:07 +00002959unsigned DIEBlock::ComputeSize(Dwarf &DW) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002960 if (!Size) {
2961 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2962
2963 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2964 Size += Values[i]->SizeOf(DW, AbbrevData[i].getForm());
2965 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002966 }
2967 return Size;
2968}
2969
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002970/// EmitValue - Emit block data.
2971///
Jim Laskey65195462006-10-30 13:35:07 +00002972void DIEBlock::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002973 switch (Form) {
2974 case DW_FORM_block1: DW.EmitInt8(Size); break;
2975 case DW_FORM_block2: DW.EmitInt16(Size); break;
2976 case DW_FORM_block4: DW.EmitInt32(Size); break;
2977 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
2978 default: assert(0 && "Improper form for block"); break;
2979 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002980
2981 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2982
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002983 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2984 DW.EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002985 Values[i]->EmitValue(DW, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002986 }
2987}
2988
2989/// SizeOf - Determine size of block data in bytes.
2990///
Jim Laskey65195462006-10-30 13:35:07 +00002991unsigned DIEBlock::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002992 switch (Form) {
2993 case DW_FORM_block1: return Size + sizeof(int8_t);
2994 case DW_FORM_block2: return Size + sizeof(int16_t);
2995 case DW_FORM_block4: return Size + sizeof(int32_t);
Jim Laskeyef42a012006-11-02 20:12:39 +00002996 case DW_FORM_block: return Size + SizeULEB128(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002997 default: assert(0 && "Improper form for block"); break;
2998 }
2999 return 0;
3000}
3001
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003002//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003003/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00003004
3005DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003006 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00003007 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00003008}
Jim Laskeyef42a012006-11-02 20:12:39 +00003009
Jim Laskeyb8509c52006-03-23 18:07:55 +00003010/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
3011///
3012void DIE::AddSiblingOffset() {
3013 DIEInteger *DI = new DIEInteger(0);
3014 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00003015 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00003016}
3017
Jim Laskeyef42a012006-11-02 20:12:39 +00003018/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003019///
Jim Laskeyef42a012006-11-02 20:12:39 +00003020void DIE::Profile(FoldingSetNodeID &ID) {
3021 Abbrev.Profile(ID);
3022
3023 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3024 ID.AddPointer(Children[i]);
3025
3026 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3027 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003028}
Jim Laskeyef42a012006-11-02 20:12:39 +00003029
3030#ifndef NDEBUG
3031void DIE::print(std::ostream &O, unsigned IncIndent) {
3032 static unsigned IndentCount = 0;
3033 IndentCount += IncIndent;
3034 const std::string Indent(IndentCount, ' ');
3035 bool isBlock = Abbrev.getTag() == 0;
3036
3037 if (!isBlock) {
3038 O << Indent
3039 << "Die: "
3040 << "0x" << std::hex << (intptr_t)this << std::dec
3041 << ", Offset: " << Offset
3042 << ", Size: " << Size
3043 << "\n";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003044
Jim Laskeyef42a012006-11-02 20:12:39 +00003045 O << Indent
3046 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00003047 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00003048 << ChildrenString(Abbrev.getChildrenFlag());
3049 } else {
3050 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00003051 }
3052 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003053
Jim Laskeyef42a012006-11-02 20:12:39 +00003054 const std::vector<DIEAbbrevData> &Data = Abbrev.getData();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003055
Jim Laskeyef42a012006-11-02 20:12:39 +00003056 IndentCount += 2;
3057 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3058 O << Indent;
3059 if (!isBlock) {
3060 O << AttributeString(Data[i].getAttribute());
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003061 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00003062 O << "Blk[" << i << "]";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003063 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003064 O << " "
3065 << FormEncodingString(Data[i].getForm())
3066 << " ";
3067 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00003068 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00003069 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003070 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00003071
Jim Laskeyef42a012006-11-02 20:12:39 +00003072 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3073 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00003074 }
Jim Laskey063e7652006-01-17 17:31:53 +00003075
Jim Laskeyef42a012006-11-02 20:12:39 +00003076 if (!isBlock) O << "\n";
3077 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00003078}
3079
Jim Laskeyef42a012006-11-02 20:12:39 +00003080void DIE::dump() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +00003081 print(llvm_cerr);
Jim Laskey41886992006-04-07 16:34:46 +00003082}
Jim Laskeybd761842006-02-27 17:27:12 +00003083#endif
Jim Laskey65195462006-10-30 13:35:07 +00003084
3085//===----------------------------------------------------------------------===//
3086/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00003087///
Jim Laskey65195462006-10-30 13:35:07 +00003088
3089DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3090 const TargetAsmInfo *T) {
3091 DW = new Dwarf(OS, A, T);
3092}
3093
3094DwarfWriter::~DwarfWriter() {
3095 delete DW;
3096}
3097
3098/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
3099/// created it. Set by the target AsmPrinter.
3100void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
3101 DW->SetDebugInfo(DI);
3102}
3103
3104/// BeginModule - Emit all Dwarf sections that should come prior to the
3105/// content.
3106void DwarfWriter::BeginModule(Module *M) {
3107 DW->BeginModule(M);
3108}
3109
3110/// EndModule - Emit all Dwarf sections that should come after the content.
3111///
3112void DwarfWriter::EndModule() {
3113 DW->EndModule();
3114}
3115
3116/// BeginFunction - Gather pre-function debug information. Assumes being
3117/// emitted immediately after the function entry point.
3118void DwarfWriter::BeginFunction(MachineFunction *MF) {
3119 DW->BeginFunction(MF);
3120}
3121
3122/// EndFunction - Gather and emit post-function debug information.
3123///
3124void DwarfWriter::EndFunction() {
3125 DW->EndFunction();
3126}