blob: 549abf0ec48684d753ad8ff46207eb6c3d657dd5 [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,
1875 DIE *ParentDie, CompileUnit *Unit) {
1876 // Add variables to scope.
1877 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1878 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1879 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1880 if (VariableDie) ParentDie->AddChild(VariableDie);
1881 }
1882
1883 // Add nested scopes.
1884 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1885 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1886 // Define the Scope debug information entry.
1887 DebugScope *Scope = Scopes[j];
1888 // FIXME - Ignore inlined functions for the time being.
1889 if (!Scope->getParent()) continue;
1890
Jim Laskey9d4209f2006-11-07 19:33:46 +00001891 unsigned StartID = DebugInfo->MappedLabel(Scope->getStartLabelID());
1892 unsigned EndID = DebugInfo->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00001893
Jim Laskey9d4209f2006-11-07 19:33:46 +00001894 // Ignore empty scopes.
1895 if (StartID == EndID && StartID != 0) continue;
1896 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00001897
1898 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1899
1900 // Add the scope bounds.
1901 if (StartID) {
1902 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1903 DWLabel("loc", StartID));
1904 } else {
1905 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1906 DWLabel("func_begin", SubprogramCount));
1907 }
1908 if (EndID) {
1909 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1910 DWLabel("loc", EndID));
1911 } else {
1912 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1913 DWLabel("func_end", SubprogramCount));
1914 }
1915
1916 // Add the scope contents.
1917 ConstructScope(Scope, ScopeDie, Unit);
1918 ParentDie->AddChild(ScopeDie);
1919 }
1920 }
Jim Laskey65195462006-10-30 13:35:07 +00001921
1922 /// ConstructRootScope - Construct the scope for the subprogram.
1923 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001924 void ConstructRootScope(DebugScope *RootScope) {
1925 // Exit if there is no root scope.
1926 if (!RootScope) return;
1927
1928 // Get the subprogram debug information entry.
1929 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1930
1931 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00001932 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001933
1934 // Get the subprogram die.
1935 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
1936 assert(SPDie && "Missing subprogram descriptor");
1937
1938 // Add the function bounds.
1939 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1940 DWLabel("func_begin", SubprogramCount));
1941 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1942 DWLabel("func_end", SubprogramCount));
1943 MachineLocation Location(RI->getFrameRegister(*MF));
1944 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001945
Jim Laskeyef42a012006-11-02 20:12:39 +00001946 ConstructScope(RootScope, SPDie, Unit);
1947 }
Jim Laskey65195462006-10-30 13:35:07 +00001948
Jim Laskeyef42a012006-11-02 20:12:39 +00001949 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1950 /// tools to recognize the object file contains Dwarf information.
1951 void EmitInitial() {
1952 // Check to see if we already emitted intial headers.
1953 if (didInitial) return;
1954 didInitial = true;
1955
1956 // Dwarf sections base addresses.
1957 if (TAI->getDwarfRequiresFrameSection()) {
1958 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1959 EmitLabel("section_frame", 0);
1960 }
1961 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
1962 EmitLabel("section_info", 0);
1963 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
1964 EmitLabel("section_abbrev", 0);
1965 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
1966 EmitLabel("section_aranges", 0);
1967 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
1968 EmitLabel("section_macinfo", 0);
1969 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
1970 EmitLabel("section_line", 0);
1971 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
1972 EmitLabel("section_loc", 0);
1973 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
1974 EmitLabel("section_pubnames", 0);
1975 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
1976 EmitLabel("section_str", 0);
1977 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
1978 EmitLabel("section_ranges", 0);
1979
1980 Asm->SwitchToTextSection(TAI->getTextSection());
1981 EmitLabel("text_begin", 0);
1982 Asm->SwitchToDataSection(TAI->getDataSection());
1983 EmitLabel("data_begin", 0);
1984
1985 // Emit common frame information.
1986 EmitInitialDebugFrame();
1987 }
1988
Jim Laskey65195462006-10-30 13:35:07 +00001989 /// EmitDIE - Recusively Emits a debug information entry.
1990 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001991 void EmitDIE(DIE *Die) const {
1992 // Get the abbreviation for this DIE.
1993 unsigned AbbrevNumber = Die->getAbbrevNumber();
1994 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1995
1996 O << "\n";
1997
1998 // Emit the code (index) for the abbreviation.
1999 EmitULEB128Bytes(AbbrevNumber);
2000 EOL(std::string("Abbrev [" +
2001 utostr(AbbrevNumber) +
2002 "] 0x" + utohexstr(Die->getOffset()) +
2003 ":0x" + utohexstr(Die->getSize()) + " " +
2004 TagString(Abbrev->getTag())));
2005
2006 const std::vector<DIEValue *> &Values = Die->getValues();
2007 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2008
2009 // Emit the DIE attribute values.
2010 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2011 unsigned Attr = AbbrevData[i].getAttribute();
2012 unsigned Form = AbbrevData[i].getForm();
2013 assert(Form && "Too many attributes for DIE (check abbreviation)");
2014
2015 switch (Attr) {
2016 case DW_AT_sibling: {
2017 EmitInt32(Die->SiblingOffset());
2018 break;
2019 }
2020 default: {
2021 // Emit an attribute using the defined form.
2022 Values[i]->EmitValue(*this, Form);
2023 break;
2024 }
2025 }
2026
2027 EOL(AttributeString(Attr));
2028 }
2029
2030 // Emit the DIE children if any.
2031 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2032 const std::vector<DIE *> &Children = Die->getChildren();
2033
2034 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2035 EmitDIE(Children[j]);
2036 }
2037
2038 EmitInt8(0); EOL("End Of Children Mark");
2039 }
2040 }
2041
Jim Laskey65195462006-10-30 13:35:07 +00002042 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2043 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002044 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2045 // Get the children.
2046 const std::vector<DIE *> &Children = Die->getChildren();
2047
2048 // If not last sibling and has children then add sibling offset attribute.
2049 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2050
2051 // Record the abbreviation.
2052 AssignAbbrevNumber(Die->getAbbrev());
2053
2054 // Get the abbreviation for this DIE.
2055 unsigned AbbrevNumber = Die->getAbbrevNumber();
2056 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2057
2058 // Set DIE offset
2059 Die->setOffset(Offset);
2060
2061 // Start the size with the size of abbreviation code.
2062 Offset += SizeULEB128(AbbrevNumber);
2063
2064 const std::vector<DIEValue *> &Values = Die->getValues();
2065 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2066
2067 // Size the DIE attribute values.
2068 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2069 // Size attribute value.
2070 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2071 }
2072
2073 // Size the DIE children if any.
2074 if (!Children.empty()) {
2075 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2076 "Children flag not set");
2077
2078 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2079 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2080 }
2081
2082 // End of children marker.
2083 Offset += sizeof(int8_t);
2084 }
2085
2086 Die->setSize(Offset - Die->getOffset());
2087 return Offset;
2088 }
Jim Laskey65195462006-10-30 13:35:07 +00002089
2090 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2091 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002092 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002093 // Process base compile unit.
2094 CompileUnit *Unit = GetBaseCompileUnit();
2095 // Compute size of compile unit header
2096 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2097 sizeof(int16_t) + // DWARF version number
2098 sizeof(int32_t) + // Offset Into Abbrev. Section
2099 sizeof(int8_t); // Pointer Size (in bytes)
2100 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002101 }
2102
Jim Laskey65195462006-10-30 13:35:07 +00002103 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
2104 /// frame.
2105 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Jim Laskeyef42a012006-11-02 20:12:39 +00002106 std::vector<MachineMove *> &Moves) {
2107 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
2108 MachineMove *Move = Moves[i];
Jim Laskey9d4209f2006-11-07 19:33:46 +00002109 unsigned LabelID = DebugInfo->MappedLabel(Move->getLabelID());
Jim Laskeyef42a012006-11-02 20:12:39 +00002110
2111 // Throw out move if the label is invalid.
Jim Laskey9d4209f2006-11-07 19:33:46 +00002112 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002113
2114 const MachineLocation &Dst = Move->getDestination();
2115 const MachineLocation &Src = Move->getSource();
2116
2117 // Advance row if new location.
2118 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
2119 EmitInt8(DW_CFA_advance_loc4);
2120 EOL("DW_CFA_advance_loc4");
2121 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
2122 EOL("");
2123
2124 BaseLabelID = LabelID;
2125 BaseLabel = "loc";
2126 }
2127
2128 int stackGrowth =
2129 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2130 TargetFrameInfo::StackGrowsUp ?
2131 TAI->getAddressSize() : -TAI->getAddressSize();
2132
2133 // If advancing cfa.
2134 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
2135 if (!Src.isRegister()) {
2136 if (Src.getRegister() == MachineLocation::VirtualFP) {
2137 EmitInt8(DW_CFA_def_cfa_offset);
2138 EOL("DW_CFA_def_cfa_offset");
2139 } else {
2140 EmitInt8(DW_CFA_def_cfa);
2141 EOL("DW_CFA_def_cfa");
Jim Laskeyef42a012006-11-02 20:12:39 +00002142 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
2143 EOL("Register");
2144 }
2145
2146 int Offset = Src.getOffset() / stackGrowth;
2147
2148 EmitULEB128Bytes(Offset);
2149 EOL("Offset");
2150 } else {
2151 assert(0 && "Machine move no supported yet.");
2152 }
2153 } else {
2154 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
2155 int Offset = Dst.getOffset() / stackGrowth;
2156
2157 if (Offset < 0) {
2158 EmitInt8(DW_CFA_offset_extended_sf);
2159 EOL("DW_CFA_offset_extended_sf");
2160 EmitULEB128Bytes(Reg);
2161 EOL("Reg");
2162 EmitSLEB128Bytes(Offset);
2163 EOL("Offset");
2164 } else if (Reg < 64) {
2165 EmitInt8(DW_CFA_offset + Reg);
2166 EOL("DW_CFA_offset + Reg");
2167 EmitULEB128Bytes(Offset);
2168 EOL("Offset");
2169 } else {
2170 EmitInt8(DW_CFA_offset_extended);
2171 EOL("DW_CFA_offset_extended");
2172 EmitULEB128Bytes(Reg);
2173 EOL("Reg");
2174 EmitULEB128Bytes(Offset);
2175 EOL("Offset");
2176 }
2177 }
2178 }
2179 }
Jim Laskey65195462006-10-30 13:35:07 +00002180
2181 /// EmitDebugInfo - Emit the debug info section.
2182 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002183 void EmitDebugInfo() const {
2184 // Start debug info section.
2185 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2186
Jim Laskey5496f012006-11-09 14:52:14 +00002187 CompileUnit *Unit = GetBaseCompileUnit();
2188 DIE *Die = Unit->getDie();
2189 // Emit the compile units header.
2190 EmitLabel("info_begin", Unit->getID());
2191 // Emit size of content not including length itself
2192 unsigned ContentSize = Die->getSize() +
2193 sizeof(int16_t) + // DWARF version number
2194 sizeof(int32_t) + // Offset Into Abbrev. Section
2195 sizeof(int8_t); // Pointer Size (in bytes)
2196
2197 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2198 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2199 EmitDifference("abbrev_begin", 0, "section_abbrev", 0);
2200 EOL("Offset Into Abbrev. Section");
2201 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
2202
2203 EmitDIE(Die);
2204 EmitLabel("info_end", Unit->getID());
2205
2206 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002207 }
2208
Jim Laskey65195462006-10-30 13:35:07 +00002209 /// EmitAbbreviations - Emit the abbreviation section.
2210 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002211 void EmitAbbreviations() const {
2212 // Check to see if it is worth the effort.
2213 if (!Abbreviations.empty()) {
2214 // Start the debug abbrev section.
2215 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2216
2217 EmitLabel("abbrev_begin", 0);
2218
2219 // For each abbrevation.
2220 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2221 // Get abbreviation data
2222 const DIEAbbrev *Abbrev = Abbreviations[i];
2223
2224 // Emit the abbrevations code (base 1 index.)
2225 EmitULEB128Bytes(Abbrev->getNumber()); EOL("Abbreviation Code");
2226
2227 // Emit the abbreviations data.
2228 Abbrev->Emit(*this);
2229
2230 O << "\n";
2231 }
2232
2233 EmitLabel("abbrev_end", 0);
2234
2235 O << "\n";
2236 }
2237 }
2238
Jim Laskey65195462006-10-30 13:35:07 +00002239 /// EmitDebugLines - Emit source line information.
2240 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002241 void EmitDebugLines() const {
2242 // Minimum line delta, thus ranging from -10..(255-10).
2243 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2244 // Maximum line delta, thus ranging from -10..(255-10).
2245 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002246
Jim Laskeyef42a012006-11-02 20:12:39 +00002247 // Start the dwarf line section.
2248 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2249
2250 // Construct the section header.
2251
2252 EmitDifference("line_end", 0, "line_begin", 0);
2253 EOL("Length of Source Line Info");
2254 EmitLabel("line_begin", 0);
2255
2256 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2257
2258 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
2259 EOL("Prolog Length");
2260 EmitLabel("line_prolog_begin", 0);
2261
2262 EmitInt8(1); EOL("Minimum Instruction Length");
2263
2264 EmitInt8(1); EOL("Default is_stmt_start flag");
2265
2266 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
2267
2268 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
2269
2270 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
2271
2272 // Line number standard opcode encodings argument count
2273 EmitInt8(0); EOL("DW_LNS_copy arg count");
2274 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2275 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2276 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2277 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2278 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2279 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2280 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2281 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
2282
2283 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2284 const UniqueVector<SourceFileInfo>
2285 &SourceFiles = DebugInfo->getSourceFiles();
2286
2287 // Emit directories.
2288 for (unsigned DirectoryID = 1, NDID = Directories.size();
2289 DirectoryID <= NDID; ++DirectoryID) {
2290 EmitString(Directories[DirectoryID]); EOL("Directory");
2291 }
2292 EmitInt8(0); EOL("End of directories");
2293
2294 // Emit files.
2295 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2296 SourceID <= NSID; ++SourceID) {
2297 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2298 EmitString(SourceFile.getName()); EOL("Source");
2299 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2300 EmitULEB128Bytes(0); EOL("Mod date");
2301 EmitULEB128Bytes(0); EOL("File size");
2302 }
2303 EmitInt8(0); EOL("End of files");
2304
2305 EmitLabel("line_prolog_end", 0);
2306
2307 // A sequence for each text section.
2308 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2309 // Isolate current sections line info.
2310 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
2311
2312 if (DwarfVerbose) {
2313 O << "\t"
2314 << TAI->getCommentString() << " "
2315 << "Section "
2316 << SectionMap[j + 1].c_str() << "\n";
2317 }
2318
2319 // Dwarf assumes we start with first line of first source file.
2320 unsigned Source = 1;
2321 unsigned Line = 1;
2322
2323 // Construct rows of the address, source, line, column matrix.
2324 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2325 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey9d4209f2006-11-07 19:33:46 +00002326 unsigned LabelID = DebugInfo->MappedLabel(LineInfo.getLabelID());
2327 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002328
2329 if (DwarfVerbose) {
2330 unsigned SourceID = LineInfo.getSourceID();
2331 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2332 unsigned DirectoryID = SourceFile.getDirectoryID();
2333 O << "\t"
2334 << TAI->getCommentString() << " "
2335 << Directories[DirectoryID]
2336 << SourceFile.getName() << ":"
2337 << LineInfo.getLine() << "\n";
2338 }
2339
2340 // Define the line address.
2341 EmitInt8(0); EOL("Extended Op");
2342 EmitInt8(4 + 1); EOL("Op size");
2343 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2344 EmitReference("loc", LabelID); EOL("Location label");
2345
2346 // If change of source, then switch to the new source.
2347 if (Source != LineInfo.getSourceID()) {
2348 Source = LineInfo.getSourceID();
2349 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2350 EmitULEB128Bytes(Source); EOL("New Source");
2351 }
2352
2353 // If change of line.
2354 if (Line != LineInfo.getLine()) {
2355 // Determine offset.
2356 int Offset = LineInfo.getLine() - Line;
2357 int Delta = Offset - MinLineDelta;
2358
2359 // Update line.
2360 Line = LineInfo.getLine();
2361
2362 // If delta is small enough and in range...
2363 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2364 // ... then use fast opcode.
2365 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2366 } else {
2367 // ... otherwise use long hand.
2368 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2369 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2370 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2371 }
2372 } else {
2373 // Copy the previous row (different address or source)
2374 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2375 }
2376 }
2377
2378 // Define last address of section.
2379 EmitInt8(0); EOL("Extended Op");
2380 EmitInt8(4 + 1); EOL("Op size");
2381 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2382 EmitReference("section_end", j + 1); EOL("Section end label");
2383
2384 // Mark end of matrix.
2385 EmitInt8(0); EOL("DW_LNE_end_sequence");
2386 EmitULEB128Bytes(1); O << "\n";
2387 EmitInt8(1); O << "\n";
2388 }
2389
2390 EmitLabel("line_end", 0);
2391
2392 O << "\n";
2393 }
2394
Jim Laskey65195462006-10-30 13:35:07 +00002395 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
2396 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002397 void EmitInitialDebugFrame() {
2398 if (!TAI->getDwarfRequiresFrameSection())
2399 return;
2400
2401 int stackGrowth =
2402 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2403 TargetFrameInfo::StackGrowsUp ?
2404 TAI->getAddressSize() : -TAI->getAddressSize();
2405
2406 // Start the dwarf frame section.
2407 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2408
2409 EmitLabel("frame_common", 0);
2410 EmitDifference("frame_common_end", 0,
2411 "frame_common_begin", 0);
2412 EOL("Length of Common Information Entry");
2413
2414 EmitLabel("frame_common_begin", 0);
2415 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2416 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2417 EmitString(""); EOL("CIE Augmentation");
2418 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
2419 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
2420 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
Jim Laskey65195462006-10-30 13:35:07 +00002421
Jim Laskeyef42a012006-11-02 20:12:39 +00002422 std::vector<MachineMove *> Moves;
2423 RI->getInitialFrameState(Moves);
2424 EmitFrameMoves(NULL, 0, Moves);
2425 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2426
2427 EmitAlign(2);
2428 EmitLabel("frame_common_end", 0);
2429
2430 O << "\n";
2431 }
2432
Jim Laskey65195462006-10-30 13:35:07 +00002433 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2434 /// section.
Jim Laskeyef42a012006-11-02 20:12:39 +00002435 void EmitFunctionDebugFrame() {
Reid Spencer5a4951e2006-11-07 06:36:36 +00002436 if (!TAI->getDwarfRequiresFrameSection())
2437 return;
Jim Laskey9d4209f2006-11-07 19:33:46 +00002438
Jim Laskeyef42a012006-11-02 20:12:39 +00002439 // Start the dwarf frame section.
2440 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2441
2442 EmitDifference("frame_end", SubprogramCount,
2443 "frame_begin", SubprogramCount);
2444 EOL("Length of Frame Information Entry");
2445
2446 EmitLabel("frame_begin", SubprogramCount);
2447
2448 EmitDifference("frame_common", 0, "section_frame", 0);
2449 EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002450
Jim Laskeyef42a012006-11-02 20:12:39 +00002451 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2452 EmitDifference("func_end", SubprogramCount,
2453 "func_begin", SubprogramCount);
2454 EOL("FDE address range");
2455
2456 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2457
2458 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2459
2460 EmitAlign(2);
2461 EmitLabel("frame_end", SubprogramCount);
2462
2463 O << "\n";
2464 }
2465
2466 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002467 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002468 void EmitDebugPubNames() {
2469 // Start the dwarf pubnames section.
2470 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2471
Jim Laskey5496f012006-11-09 14:52:14 +00002472 CompileUnit *Unit = GetBaseCompileUnit();
2473
2474 EmitDifference("pubnames_end", Unit->getID(),
2475 "pubnames_begin", Unit->getID());
2476 EOL("Length of Public Names Info");
2477
2478 EmitLabel("pubnames_begin", Unit->getID());
2479
2480 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2481
2482 EmitDifference("info_begin", Unit->getID(), "section_info", 0);
2483 EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002484
Jim Laskey5496f012006-11-09 14:52:14 +00002485 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2486 EOL("Compilation Unit Length");
2487
2488 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2489
2490 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2491 GE = Globals.end();
2492 GI != GE; ++GI) {
2493 const std::string &Name = GI->first;
2494 DIE * Entity = GI->second;
Jim Laskeyef42a012006-11-02 20:12:39 +00002495
Jim Laskey5496f012006-11-09 14:52:14 +00002496 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2497 EmitString(Name); EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00002498 }
Jim Laskey5496f012006-11-09 14:52:14 +00002499
2500 EmitInt32(0); EOL("End Mark");
2501 EmitLabel("pubnames_end", Unit->getID());
2502
2503 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002504 }
2505
2506 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002507 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002508 void EmitDebugStr() {
2509 // Check to see if it is worth the effort.
2510 if (!StringPool.empty()) {
2511 // Start the dwarf str section.
2512 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2513
2514 // For each of strings in the string pool.
2515 for (unsigned StringID = 1, N = StringPool.size();
2516 StringID <= N; ++StringID) {
2517 // Emit a label for reference from debug information entries.
2518 EmitLabel("string", StringID);
2519 // Emit the string itself.
2520 const std::string &String = StringPool[StringID];
2521 EmitString(String); O << "\n";
2522 }
2523
2524 O << "\n";
2525 }
2526 }
2527
2528 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002529 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002530 void EmitDebugLoc() {
2531 // Start the dwarf loc section.
2532 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2533
2534 O << "\n";
2535 }
2536
2537 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002538 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002539 void EmitDebugARanges() {
2540 // Start the dwarf aranges section.
2541 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2542
2543 // FIXME - Mock up
2544 #if 0
Jim Laskey5496f012006-11-09 14:52:14 +00002545 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002546
Jim Laskey5496f012006-11-09 14:52:14 +00002547 // Don't include size of length
2548 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2549
2550 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2551
2552 EmitReference("info_begin", Unit->getID());
2553 EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002554
Jim Laskey5496f012006-11-09 14:52:14 +00002555 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002556
Jim Laskey5496f012006-11-09 14:52:14 +00002557 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002558
Jim Laskey5496f012006-11-09 14:52:14 +00002559 EmitInt16(0); EOL("Pad (1)");
2560 EmitInt16(0); EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002561
Jim Laskey5496f012006-11-09 14:52:14 +00002562 // Range 1
2563 EmitReference("text_begin", 0); EOL("Address");
2564 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002565
Jim Laskey5496f012006-11-09 14:52:14 +00002566 EmitInt32(0); EOL("EOM (1)");
2567 EmitInt32(0); EOL("EOM (2)");
2568
2569 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002570 #endif
2571 }
2572
2573 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002574 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002575 void EmitDebugRanges() {
2576 // Start the dwarf ranges section.
2577 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2578
2579 O << "\n";
2580 }
2581
2582 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002583 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002584 void EmitDebugMacInfo() {
2585 // Start the dwarf macinfo section.
2586 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2587
2588 O << "\n";
2589 }
2590
Jim Laskey65195462006-10-30 13:35:07 +00002591 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2592 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002593 void ConstructCompileUnitDIEs() {
2594 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
2595
2596 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00002597 unsigned ID = DebugInfo->RecordSource(CUW[i]);
2598 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002599 CompileUnits.push_back(Unit);
2600 }
2601 }
2602
Jim Laskey65195462006-10-30 13:35:07 +00002603 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2604 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002605 void ConstructGlobalDIEs() {
2606 std::vector<GlobalVariableDesc *> GlobalVariables =
2607 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
2608
2609 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2610 GlobalVariableDesc *GVD = GlobalVariables[i];
2611 NewGlobalVariable(GVD);
2612 }
2613 }
Jim Laskey65195462006-10-30 13:35:07 +00002614
2615 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2616 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002617 void ConstructSubprogramDIEs() {
2618 std::vector<SubprogramDesc *> Subprograms =
2619 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
2620
2621 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2622 SubprogramDesc *SPD = Subprograms[i];
2623 NewSubprogram(SPD);
2624 }
2625 }
Jim Laskey65195462006-10-30 13:35:07 +00002626
2627 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
2628 ///
2629 bool ShouldEmitDwarf() const { return shouldEmit; }
2630
2631public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002632 //===--------------------------------------------------------------------===//
2633 // Main entry points.
2634 //
2635 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
2636 : O(OS)
2637 , Asm(A)
2638 , TAI(T)
2639 , TD(Asm->TM.getTargetData())
2640 , RI(Asm->TM.getRegisterInfo())
2641 , M(NULL)
2642 , MF(NULL)
2643 , DebugInfo(NULL)
2644 , didInitial(false)
2645 , shouldEmit(false)
2646 , SubprogramCount(0)
2647 , CompileUnits()
2648 , AbbreviationsSet(InitAbbreviationsSetSize)
2649 , Abbreviations()
2650 , ValuesSet(InitValuesSetSize)
2651 , Values()
2652 , StringPool()
2653 , DescToUnitMap()
2654 , SectionMap()
2655 , SectionSourceLines()
2656 {
2657 }
2658 virtual ~Dwarf() {
2659 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2660 delete CompileUnits[i];
2661 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2662 delete Values[j];
2663 }
2664
Jim Laskey65195462006-10-30 13:35:07 +00002665 // Accessors.
2666 //
2667 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
2668
2669 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2670 /// created it. Set by the target AsmPrinter.
Jim Laskeyef42a012006-11-02 20:12:39 +00002671 void SetDebugInfo(MachineDebugInfo *DI) {
2672 // Make sure initial declarations are made.
2673 if (!DebugInfo && DI->hasInfo()) {
2674 DebugInfo = DI;
2675 shouldEmit = true;
2676
2677 // Emit initial sections
2678 EmitInitial();
2679
2680 // Create all the compile unit DIEs.
2681 ConstructCompileUnitDIEs();
2682
2683 // Create DIEs for each of the externally visible global variables.
2684 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002685
Jim Laskeyef42a012006-11-02 20:12:39 +00002686 // Create DIEs for each of the externally visible subprograms.
2687 ConstructSubprogramDIEs();
2688
2689 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00002690 SectionMap.insert(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002691 }
2692 }
2693
Jim Laskey65195462006-10-30 13:35:07 +00002694 /// BeginModule - Emit all Dwarf sections that should come prior to the
2695 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002696 void BeginModule(Module *M) {
2697 this->M = M;
2698
2699 if (!ShouldEmitDwarf()) return;
2700 EOL("Dwarf Begin Module");
2701 }
2702
Jim Laskey65195462006-10-30 13:35:07 +00002703 /// EndModule - Emit all Dwarf sections that should come after the content.
2704 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002705 void EndModule() {
2706 if (!ShouldEmitDwarf()) return;
2707 EOL("Dwarf End Module");
2708
2709 // Standard sections final addresses.
2710 Asm->SwitchToTextSection(TAI->getTextSection());
2711 EmitLabel("text_end", 0);
2712 Asm->SwitchToDataSection(TAI->getDataSection());
2713 EmitLabel("data_end", 0);
2714
2715 // End text sections.
2716 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2717 Asm->SwitchToTextSection(SectionMap[i].c_str());
2718 EmitLabel("section_end", i);
2719 }
2720
2721 // Compute DIE offsets and sizes.
2722 SizeAndOffsets();
2723
2724 // Emit all the DIEs into a debug info section
2725 EmitDebugInfo();
2726
2727 // Corresponding abbreviations into a abbrev section.
2728 EmitAbbreviations();
2729
2730 // Emit source line correspondence into a debug line section.
2731 EmitDebugLines();
2732
2733 // Emit info into a debug pubnames section.
2734 EmitDebugPubNames();
2735
2736 // Emit info into a debug str section.
2737 EmitDebugStr();
2738
2739 // Emit info into a debug loc section.
2740 EmitDebugLoc();
2741
2742 // Emit info into a debug aranges section.
2743 EmitDebugARanges();
2744
2745 // Emit info into a debug ranges section.
2746 EmitDebugRanges();
2747
2748 // Emit info into a debug macinfo section.
2749 EmitDebugMacInfo();
2750 }
2751
Jim Laskey65195462006-10-30 13:35:07 +00002752 /// BeginFunction - Gather pre-function debug information. Assumes being
2753 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002754 void BeginFunction(MachineFunction *MF) {
2755 this->MF = MF;
2756
2757 if (!ShouldEmitDwarf()) return;
2758 EOL("Dwarf Begin Function");
2759
2760 // Begin accumulating function debug information.
2761 DebugInfo->BeginFunction(MF);
2762
2763 // Assumes in correct section after the entry point.
2764 EmitLabel("func_begin", ++SubprogramCount);
2765 }
2766
Jim Laskey65195462006-10-30 13:35:07 +00002767 /// EndFunction - Gather and emit post-function debug information.
2768 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002769 void EndFunction() {
2770 if (!ShouldEmitDwarf()) return;
2771 EOL("Dwarf End Function");
2772
2773 // Define end label for subprogram.
2774 EmitLabel("func_end", SubprogramCount);
2775
2776 // Get function line info.
2777 const std::vector<SourceLineInfo> &LineInfos = DebugInfo->getSourceLines();
2778
2779 if (!LineInfos.empty()) {
2780 // Get section line info.
2781 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2782 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2783 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2784 // Append the function info to section info.
2785 SectionLineInfos.insert(SectionLineInfos.end(),
2786 LineInfos.begin(), LineInfos.end());
2787 }
2788
2789 // Construct scopes for subprogram.
2790 ConstructRootScope(DebugInfo->getRootScope());
2791
2792 // Emit function frame information.
2793 EmitFunctionDebugFrame();
2794
2795 // Reset the line numbers for the next function.
2796 DebugInfo->ClearLineInfo();
2797
2798 // Clear function debug information.
2799 DebugInfo->EndFunction();
2800 }
Jim Laskey65195462006-10-30 13:35:07 +00002801};
2802
Jim Laskey0d086af2006-02-27 12:43:29 +00002803} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00002804
2805//===----------------------------------------------------------------------===//
2806
Jim Laskeyd18e2892006-01-20 20:34:06 +00002807/// Emit - Print the abbreviation using the specified Dwarf writer.
2808///
Jim Laskey65195462006-10-30 13:35:07 +00002809void DIEAbbrev::Emit(const Dwarf &DW) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002810 // Emit its Dwarf tag type.
2811 DW.EmitULEB128Bytes(Tag);
2812 DW.EOL(TagString(Tag));
2813
2814 // Emit whether it has children DIEs.
2815 DW.EmitULEB128Bytes(ChildrenFlag);
2816 DW.EOL(ChildrenString(ChildrenFlag));
2817
2818 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00002819 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002820 const DIEAbbrevData &AttrData = Data[i];
2821
2822 // Emit attribute type.
2823 DW.EmitULEB128Bytes(AttrData.getAttribute());
2824 DW.EOL(AttributeString(AttrData.getAttribute()));
2825
2826 // Emit form type.
2827 DW.EmitULEB128Bytes(AttrData.getForm());
2828 DW.EOL(FormEncodingString(AttrData.getForm()));
2829 }
2830
2831 // Mark end of abbreviation.
2832 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
2833 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
2834}
2835
2836#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00002837void DIEAbbrev::print(std::ostream &O) {
2838 O << "Abbreviation @"
2839 << std::hex << (intptr_t)this << std::dec
2840 << " "
2841 << TagString(Tag)
2842 << " "
2843 << ChildrenString(ChildrenFlag)
2844 << "\n";
2845
2846 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
2847 O << " "
2848 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002849 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00002850 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002851 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002852 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00002853}
Bill Wendlingbdc679d2006-11-29 00:39:47 +00002854void DIEAbbrev::dump() { print(llvm_cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002855#endif
2856
2857//===----------------------------------------------------------------------===//
2858
Jim Laskeyef42a012006-11-02 20:12:39 +00002859#ifndef NDEBUG
2860void DIEValue::dump() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +00002861 print(llvm_cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002862}
Jim Laskeyef42a012006-11-02 20:12:39 +00002863#endif
2864
2865//===----------------------------------------------------------------------===//
2866
Jim Laskey063e7652006-01-17 17:31:53 +00002867/// EmitValue - Emit integer of appropriate size.
2868///
Jim Laskey65195462006-10-30 13:35:07 +00002869void DIEInteger::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +00002870 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00002871 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00002872 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002873 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002874 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002875 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002876 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002877 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002878 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002879 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +00002880 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
2881 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +00002882 default: assert(0 && "DIE Value form not supported yet"); break;
2883 }
2884}
2885
Jim Laskey063e7652006-01-17 17:31:53 +00002886//===----------------------------------------------------------------------===//
2887
2888/// EmitValue - Emit string value.
2889///
Jim Laskey65195462006-10-30 13:35:07 +00002890void DIEString::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002891 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00002892}
2893
Jim Laskey063e7652006-01-17 17:31:53 +00002894//===----------------------------------------------------------------------===//
2895
2896/// EmitValue - Emit label value.
2897///
Jim Laskey65195462006-10-30 13:35:07 +00002898void DIEDwarfLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002899 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +00002900}
2901
2902/// SizeOf - Determine size of label value in bytes.
2903///
Jim Laskey65195462006-10-30 13:35:07 +00002904unsigned DIEDwarfLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002905 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002906}
Jim Laskeyef42a012006-11-02 20:12:39 +00002907
Jim Laskey063e7652006-01-17 17:31:53 +00002908//===----------------------------------------------------------------------===//
2909
Jim Laskeyd18e2892006-01-20 20:34:06 +00002910/// EmitValue - Emit label value.
2911///
Jim Laskey65195462006-10-30 13:35:07 +00002912void DIEObjectLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002913 DW.EmitReference(Label);
2914}
2915
2916/// SizeOf - Determine size of label value in bytes.
2917///
Jim Laskey65195462006-10-30 13:35:07 +00002918unsigned DIEObjectLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002919 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00002920}
2921
2922//===----------------------------------------------------------------------===//
2923
Jim Laskey063e7652006-01-17 17:31:53 +00002924/// EmitValue - Emit delta value.
2925///
Jim Laskey65195462006-10-30 13:35:07 +00002926void DIEDelta::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002927 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +00002928}
2929
2930/// SizeOf - Determine size of delta value in bytes.
2931///
Jim Laskey65195462006-10-30 13:35:07 +00002932unsigned DIEDelta::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002933 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002934}
2935
2936//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00002937
Jim Laskeyb8509c52006-03-23 18:07:55 +00002938/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002939///
Jim Laskey65195462006-10-30 13:35:07 +00002940void DIEntry::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +00002941 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00002942}
Jim Laskeyd18e2892006-01-20 20:34:06 +00002943
2944//===----------------------------------------------------------------------===//
2945
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002946/// ComputeSize - calculate the size of the block.
2947///
Jim Laskey65195462006-10-30 13:35:07 +00002948unsigned DIEBlock::ComputeSize(Dwarf &DW) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002949 if (!Size) {
2950 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2951
2952 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2953 Size += Values[i]->SizeOf(DW, AbbrevData[i].getForm());
2954 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002955 }
2956 return Size;
2957}
2958
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002959/// EmitValue - Emit block data.
2960///
Jim Laskey65195462006-10-30 13:35:07 +00002961void DIEBlock::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002962 switch (Form) {
2963 case DW_FORM_block1: DW.EmitInt8(Size); break;
2964 case DW_FORM_block2: DW.EmitInt16(Size); break;
2965 case DW_FORM_block4: DW.EmitInt32(Size); break;
2966 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
2967 default: assert(0 && "Improper form for block"); break;
2968 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002969
2970 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2971
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002972 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2973 DW.EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002974 Values[i]->EmitValue(DW, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002975 }
2976}
2977
2978/// SizeOf - Determine size of block data in bytes.
2979///
Jim Laskey65195462006-10-30 13:35:07 +00002980unsigned DIEBlock::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002981 switch (Form) {
2982 case DW_FORM_block1: return Size + sizeof(int8_t);
2983 case DW_FORM_block2: return Size + sizeof(int16_t);
2984 case DW_FORM_block4: return Size + sizeof(int32_t);
Jim Laskeyef42a012006-11-02 20:12:39 +00002985 case DW_FORM_block: return Size + SizeULEB128(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002986 default: assert(0 && "Improper form for block"); break;
2987 }
2988 return 0;
2989}
2990
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002991//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00002992/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00002993
2994DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002995 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00002996 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00002997}
Jim Laskeyef42a012006-11-02 20:12:39 +00002998
Jim Laskeyb8509c52006-03-23 18:07:55 +00002999/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
3000///
3001void DIE::AddSiblingOffset() {
3002 DIEInteger *DI = new DIEInteger(0);
3003 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00003004 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00003005}
3006
Jim Laskeyef42a012006-11-02 20:12:39 +00003007/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003008///
Jim Laskeyef42a012006-11-02 20:12:39 +00003009void DIE::Profile(FoldingSetNodeID &ID) {
3010 Abbrev.Profile(ID);
3011
3012 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3013 ID.AddPointer(Children[i]);
3014
3015 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3016 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003017}
Jim Laskeyef42a012006-11-02 20:12:39 +00003018
3019#ifndef NDEBUG
3020void DIE::print(std::ostream &O, unsigned IncIndent) {
3021 static unsigned IndentCount = 0;
3022 IndentCount += IncIndent;
3023 const std::string Indent(IndentCount, ' ');
3024 bool isBlock = Abbrev.getTag() == 0;
3025
3026 if (!isBlock) {
3027 O << Indent
3028 << "Die: "
3029 << "0x" << std::hex << (intptr_t)this << std::dec
3030 << ", Offset: " << Offset
3031 << ", Size: " << Size
3032 << "\n";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003033
Jim Laskeyef42a012006-11-02 20:12:39 +00003034 O << Indent
3035 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00003036 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00003037 << ChildrenString(Abbrev.getChildrenFlag());
3038 } else {
3039 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00003040 }
3041 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003042
Jim Laskeyef42a012006-11-02 20:12:39 +00003043 const std::vector<DIEAbbrevData> &Data = Abbrev.getData();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003044
Jim Laskeyef42a012006-11-02 20:12:39 +00003045 IndentCount += 2;
3046 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3047 O << Indent;
3048 if (!isBlock) {
3049 O << AttributeString(Data[i].getAttribute());
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003050 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00003051 O << "Blk[" << i << "]";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003052 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003053 O << " "
3054 << FormEncodingString(Data[i].getForm())
3055 << " ";
3056 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00003057 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00003058 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003059 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00003060
Jim Laskeyef42a012006-11-02 20:12:39 +00003061 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3062 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00003063 }
Jim Laskey063e7652006-01-17 17:31:53 +00003064
Jim Laskeyef42a012006-11-02 20:12:39 +00003065 if (!isBlock) O << "\n";
3066 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00003067}
3068
Jim Laskeyef42a012006-11-02 20:12:39 +00003069void DIE::dump() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +00003070 print(llvm_cerr);
Jim Laskey41886992006-04-07 16:34:46 +00003071}
Jim Laskeybd761842006-02-27 17:27:12 +00003072#endif
Jim Laskey65195462006-10-30 13:35:07 +00003073
3074//===----------------------------------------------------------------------===//
3075/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00003076///
Jim Laskey65195462006-10-30 13:35:07 +00003077
3078DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3079 const TargetAsmInfo *T) {
3080 DW = new Dwarf(OS, A, T);
3081}
3082
3083DwarfWriter::~DwarfWriter() {
3084 delete DW;
3085}
3086
3087/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
3088/// created it. Set by the target AsmPrinter.
3089void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
3090 DW->SetDebugInfo(DI);
3091}
3092
3093/// BeginModule - Emit all Dwarf sections that should come prior to the
3094/// content.
3095void DwarfWriter::BeginModule(Module *M) {
3096 DW->BeginModule(M);
3097}
3098
3099/// EndModule - Emit all Dwarf sections that should come after the content.
3100///
3101void DwarfWriter::EndModule() {
3102 DW->EndModule();
3103}
3104
3105/// BeginFunction - Gather pre-function debug information. Assumes being
3106/// emitted immediately after the function entry point.
3107void DwarfWriter::BeginFunction(MachineFunction *MF) {
3108 DW->BeginFunction(MF);
3109}
3110
3111/// EndFunction - Gather and emit post-function debug information.
3112///
3113void DwarfWriter::EndFunction() {
3114 DW->EndFunction();
3115}