blob: ac6987b0f07a915d338176ffe72644eaaa67e899 [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.
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001099 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
1100 bool IsSmall = false) const {
1101 EmitDifference(LabelHi.Tag, LabelHi.Number,
1102 LabelLo.Tag, LabelLo.Number,
1103 IsSmall);
Jim Laskey65195462006-10-30 13:35:07 +00001104 }
1105 void EmitDifference(const char *TagHi, unsigned NumberHi,
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001106 const char *TagLo, unsigned NumberLo,
1107 bool IsSmall = false) const {
Jim Laskeyef42a012006-11-02 20:12:39 +00001108 if (TAI->needsSet()) {
1109 static unsigned SetCounter = 0;
1110
1111 O << "\t.set\t";
1112 PrintLabelName("set", SetCounter);
1113 O << ",";
1114 PrintLabelName(TagHi, NumberHi);
1115 O << "-";
1116 PrintLabelName(TagLo, NumberLo);
1117 O << "\n";
1118
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001119 if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
Jim Laskeyef42a012006-11-02 20:12:39 +00001120 O << TAI->getData32bitsDirective();
1121 else
1122 O << TAI->getData64bitsDirective();
1123
1124 PrintLabelName("set", SetCounter);
1125
1126 ++SetCounter;
1127 } else {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001128 if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
Jim Laskeyef42a012006-11-02 20:12:39 +00001129 O << TAI->getData32bitsDirective();
1130 else
1131 O << TAI->getData64bitsDirective();
1132
1133 PrintLabelName(TagHi, NumberHi);
1134 O << "-";
1135 PrintLabelName(TagLo, NumberLo);
1136 }
1137 }
Jim Laskey65195462006-10-30 13:35:07 +00001138
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001139 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Jim Laskey65195462006-10-30 13:35:07 +00001140 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001141 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1142 // Profile the node so that we can make it unique.
1143 FoldingSetNodeID ID;
1144 Abbrev.Profile(ID);
1145
1146 // Check the set for priors.
1147 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
1148
1149 // If it's newly added.
1150 if (InSet == &Abbrev) {
1151 // Add to abbreviation list.
1152 Abbreviations.push_back(&Abbrev);
1153 // Assign the vector position + 1 as its number.
1154 Abbrev.setNumber(Abbreviations.size());
1155 } else {
1156 // Assign existing abbreviation number.
1157 Abbrev.setNumber(InSet->getNumber());
1158 }
1159 }
1160
Jim Laskey65195462006-10-30 13:35:07 +00001161 /// NewString - Add a string to the constant pool and returns a label.
1162 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001163 DWLabel NewString(const std::string &String) {
1164 unsigned StringID = StringPool.insert(String);
1165 return DWLabel("string", StringID);
1166 }
Jim Laskey65195462006-10-30 13:35:07 +00001167
Jim Laskeyef42a012006-11-02 20:12:39 +00001168 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1169 /// entry.
1170 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1171 DIEntry *Value;
1172
1173 if (Entry) {
1174 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001175 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001176 void *Where;
1177 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
1178
Jim Laskeyf6733882006-11-02 21:48:18 +00001179 if (Value) return Value;
Jim Laskeyef42a012006-11-02 20:12:39 +00001180
1181 Value = new DIEntry(Entry);
1182 ValuesSet.InsertNode(Value, Where);
1183 } else {
1184 Value = new DIEntry(Entry);
1185 }
1186
1187 Values.push_back(Value);
1188 return Value;
1189 }
1190
1191 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1192 ///
1193 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1194 Value->Entry = Entry;
1195 // Add to values set if not already there. If it is, we merely have a
1196 // duplicate in the values list (no harm.)
1197 ValuesSet.GetOrInsertNode(Value);
1198 }
1199
1200 /// AddUInt - Add an unsigned integer attribute data and value.
1201 ///
1202 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1203 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1204
1205 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001206 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001207 void *Where;
1208 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1209 if (!Value) {
1210 Value = new DIEInteger(Integer);
1211 ValuesSet.InsertNode(Value, Where);
1212 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001213 }
1214
1215 Die->AddValue(Attribute, Form, Value);
1216 }
1217
1218 /// AddSInt - Add an signed integer attribute data and value.
1219 ///
1220 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1221 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1222
1223 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001224 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001225 void *Where;
1226 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1227 if (!Value) {
1228 Value = new DIEInteger(Integer);
1229 ValuesSet.InsertNode(Value, Where);
1230 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001231 }
1232
1233 Die->AddValue(Attribute, Form, Value);
1234 }
1235
1236 /// AddString - Add a std::string attribute data and value.
1237 ///
1238 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1239 const std::string &String) {
1240 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001241 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001242 void *Where;
1243 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1244 if (!Value) {
1245 Value = new DIEString(String);
1246 ValuesSet.InsertNode(Value, Where);
1247 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001248 }
1249
1250 Die->AddValue(Attribute, Form, Value);
1251 }
1252
1253 /// AddLabel - Add a Dwarf label attribute data and value.
1254 ///
1255 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1256 const DWLabel &Label) {
1257 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001258 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001259 void *Where;
1260 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1261 if (!Value) {
1262 Value = new DIEDwarfLabel(Label);
1263 ValuesSet.InsertNode(Value, Where);
1264 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001265 }
1266
1267 Die->AddValue(Attribute, Form, Value);
1268 }
1269
1270 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1271 ///
1272 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1273 const std::string &Label) {
1274 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001275 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001276 void *Where;
1277 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1278 if (!Value) {
1279 Value = new DIEObjectLabel(Label);
1280 ValuesSet.InsertNode(Value, Where);
1281 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001282 }
1283
1284 Die->AddValue(Attribute, Form, Value);
1285 }
1286
1287 /// AddDelta - Add a label delta attribute data and value.
1288 ///
1289 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1290 const DWLabel &Hi, const DWLabel &Lo) {
1291 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001292 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001293 void *Where;
1294 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1295 if (!Value) {
1296 Value = new DIEDelta(Hi, Lo);
1297 ValuesSet.InsertNode(Value, Where);
1298 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001299 }
1300
1301 Die->AddValue(Attribute, Form, Value);
1302 }
1303
1304 /// AddDIEntry - Add a DIE attribute data and value.
1305 ///
1306 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1307 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1308 }
1309
1310 /// AddBlock - Add block data.
1311 ///
1312 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1313 Block->ComputeSize(*this);
1314 FoldingSetNodeID ID;
1315 Block->Profile(ID);
1316 void *Where;
1317 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1318 if (!Value) {
1319 Value = Block;
1320 ValuesSet.InsertNode(Value, Where);
1321 Values.push_back(Value);
1322 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001323 delete Block;
1324 }
1325
1326 Die->AddValue(Attribute, Block->BestForm(), Value);
1327 }
1328
Jim Laskey65195462006-10-30 13:35:07 +00001329private:
1330
1331 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001332 /// entry.
1333 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1334 if (File && Line) {
1335 CompileUnit *FileUnit = FindCompileUnit(File);
1336 unsigned FileID = FileUnit->getID();
1337 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1338 AddUInt(Die, DW_AT_decl_line, 0, Line);
1339 }
1340 }
Jim Laskey65195462006-10-30 13:35:07 +00001341
1342 /// AddAddress - Add an address attribute to a die based on the location
1343 /// provided.
1344 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001345 const MachineLocation &Location) {
1346 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1347 DIEBlock *Block = new DIEBlock();
1348
1349 if (Location.isRegister()) {
1350 if (Reg < 32) {
1351 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1352 } else {
1353 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1354 AddUInt(Block, 0, DW_FORM_udata, Reg);
1355 }
1356 } else {
1357 if (Reg < 32) {
1358 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1359 } else {
1360 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1361 AddUInt(Block, 0, DW_FORM_udata, Reg);
1362 }
1363 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1364 }
1365
1366 AddBlock(Die, Attribute, 0, Block);
1367 }
1368
1369 /// AddBasicType - Add a new basic type attribute to the specified entity.
1370 ///
1371 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1372 const std::string &Name,
1373 unsigned Encoding, unsigned Size) {
1374 DIE *Die = ConstructBasicType(Unit, Name, Encoding, Size);
1375 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1376 }
1377
1378 /// ConstructBasicType - Construct a new basic type.
1379 ///
1380 DIE *ConstructBasicType(CompileUnit *Unit,
1381 const std::string &Name,
1382 unsigned Encoding, unsigned Size) {
1383 DIE Buffer(DW_TAG_base_type);
1384 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1385 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1386 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1387 return Unit->AddDie(Buffer);
1388 }
1389
1390 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1391 ///
1392 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1393 DIE *Die = ConstructPointerType(Unit, Name);
1394 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1395 }
1396
1397 /// ConstructPointerType - Construct a new pointer type.
1398 ///
1399 DIE *ConstructPointerType(CompileUnit *Unit, const std::string &Name) {
1400 DIE Buffer(DW_TAG_pointer_type);
1401 AddUInt(&Buffer, DW_AT_byte_size, 0, TAI->getAddressSize());
1402 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1403 return Unit->AddDie(Buffer);
1404 }
1405
1406 /// AddType - Add a new type attribute to the specified entity.
1407 ///
1408 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1409 if (!TyDesc) {
1410 AddBasicType(Entity, Unit, "", DW_ATE_signed, 4);
1411 } else {
1412 // Check for pre-existence.
1413 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
1414
1415 // If it exists then use the existing value.
1416 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001417 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1418 return;
1419 }
1420
1421 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1422 // FIXME - Not sure why programs and variables are coming through here.
1423 // Short cut for handling subprogram types (not really a TyDesc.)
1424 AddPointerType(Entity, Unit, SubprogramTy->getName());
1425 } else if (GlobalVariableDesc *GlobalTy =
1426 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1427 // FIXME - Not sure why programs and variables are coming through here.
1428 // Short cut for handling global variable types (not really a TyDesc.)
1429 AddPointerType(Entity, Unit, GlobalTy->getName());
1430 } else {
1431 // Set up proxy.
1432 Slot = NewDIEntry();
1433
1434 // Construct type.
1435 DIE Buffer(DW_TAG_base_type);
1436 ConstructType(Buffer, TyDesc, Unit);
1437
1438 // Add debug information entry to entity and unit.
1439 DIE *Die = Unit->AddDie(Buffer);
1440 SetDIEntry(Slot, Die);
1441 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1442 }
1443 }
1444 }
1445
1446 /// ConstructType - Adds all the required attributes to the type.
1447 ///
1448 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1449 // Get core information.
1450 const std::string &Name = TyDesc->getName();
1451 uint64_t Size = TyDesc->getSize() >> 3;
1452
1453 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1454 // Fundamental types like int, float, bool
1455 Buffer.setTag(DW_TAG_base_type);
1456 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1457 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00001458 // Fetch tag.
1459 unsigned Tag = DerivedTy->getTag();
1460 // FIXME - Workaround for templates.
1461 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1462 // Pointers, typedefs et al.
1463 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00001464 // Map to main type, void will not have a type.
1465 if (TypeDesc *FromTy = DerivedTy->getFromType())
1466 AddType(&Buffer, FromTy, Unit);
1467 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1468 // Fetch tag.
1469 unsigned Tag = CompTy->getTag();
1470
1471 // Set tag accordingly.
1472 if (Tag == DW_TAG_vector_type)
1473 Buffer.setTag(DW_TAG_array_type);
1474 else
1475 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00001476
Jim Laskeyef42a012006-11-02 20:12:39 +00001477 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1478
1479 switch (Tag) {
1480 case DW_TAG_vector_type:
1481 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1482 // Fall thru
1483 case DW_TAG_array_type: {
1484 // Add element type.
1485 if (TypeDesc *FromTy = CompTy->getFromType())
1486 AddType(&Buffer, FromTy, Unit);
1487
1488 // Don't emit size attribute.
1489 Size = 0;
1490
1491 // Construct an anonymous type for index type.
1492 DIE *IndexTy = ConstructBasicType(Unit, "", DW_ATE_signed, 4);
1493
1494 // Add subranges to array type.
1495 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1496 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1497 int64_t Lo = SRD->getLo();
1498 int64_t Hi = SRD->getHi();
1499 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1500
1501 // If a range is available.
1502 if (Lo != Hi) {
1503 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1504 // Only add low if non-zero.
1505 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1506 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1507 }
1508
1509 Buffer.AddChild(Subrange);
1510 }
1511 break;
1512 }
1513 case DW_TAG_structure_type:
1514 case DW_TAG_union_type: {
1515 // Add elements to structure type.
1516 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1517 DebugInfoDesc *Element = Elements[i];
1518
1519 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1520 // Add field or base class.
1521
1522 unsigned Tag = MemberDesc->getTag();
1523
1524 // Extract the basic information.
1525 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001526 uint64_t Size = MemberDesc->getSize();
1527 uint64_t Align = MemberDesc->getAlign();
1528 uint64_t Offset = MemberDesc->getOffset();
1529
1530 // Construct member debug information entry.
1531 DIE *Member = new DIE(Tag);
1532
1533 // Add name if not "".
1534 if (!Name.empty())
1535 AddString(Member, DW_AT_name, DW_FORM_string, Name);
1536 // Add location if available.
1537 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1538
1539 // Most of the time the field info is the same as the members.
1540 uint64_t FieldSize = Size;
1541 uint64_t FieldAlign = Align;
1542 uint64_t FieldOffset = Offset;
1543
1544 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1545 AddType(Member, FromTy, Unit);
1546 FieldSize = FromTy->getSize();
1547 FieldAlign = FromTy->getSize();
1548 }
1549
1550 // Unless we have a bit field.
1551 if (Tag == DW_TAG_member && FieldSize != Size) {
1552 // Construct the alignment mask.
1553 uint64_t AlignMask = ~(FieldAlign - 1);
1554 // Determine the high bit + 1 of the declared size.
1555 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1556 // Work backwards to determine the base offset of the field.
1557 FieldOffset = HiMark - FieldSize;
1558 // Now normalize offset to the field.
1559 Offset -= FieldOffset;
1560
1561 // Maybe we need to work from the other end.
1562 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1563
1564 // Add size and offset.
1565 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1566 AddUInt(Member, DW_AT_bit_size, 0, Size);
1567 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1568 }
1569
1570 // Add computation for offset.
1571 DIEBlock *Block = new DIEBlock();
1572 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1573 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1574 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1575
1576 // Add accessibility (public default unless is base class.
1577 if (MemberDesc->isProtected()) {
1578 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1579 } else if (MemberDesc->isPrivate()) {
1580 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1581 } else if (Tag == DW_TAG_inheritance) {
1582 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1583 }
1584
1585 Buffer.AddChild(Member);
1586 } else if (GlobalVariableDesc *StaticDesc =
1587 dyn_cast<GlobalVariableDesc>(Element)) {
1588 // Add static member.
1589
1590 // Construct member debug information entry.
1591 DIE *Static = new DIE(DW_TAG_variable);
1592
1593 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001594 const std::string &Name = StaticDesc->getName();
1595 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001596 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001597 if (!LinkageName.empty()) {
1598 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1599 LinkageName);
1600 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001601
1602 // Add location.
1603 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1604
1605 // Add type.
1606 if (TypeDesc *StaticTy = StaticDesc->getType())
1607 AddType(Static, StaticTy, Unit);
1608
1609 // Add flags.
1610 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
1611 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
1612
1613 Buffer.AddChild(Static);
1614 } else if (SubprogramDesc *MethodDesc =
1615 dyn_cast<SubprogramDesc>(Element)) {
1616 // Add member function.
1617
1618 // Construct member debug information entry.
1619 DIE *Method = new DIE(DW_TAG_subprogram);
1620
1621 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001622 const std::string &Name = MethodDesc->getName();
1623 const std::string &LinkageName = MethodDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001624
Jim Laskey2172f962006-11-30 14:35:45 +00001625 AddString(Method, DW_AT_name, DW_FORM_string, Name);
1626 bool IsCTor = TyDesc->getName() == Name;
1627
1628 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001629 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001630 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001631 }
1632
1633 // Add location.
1634 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1635
1636 // Add type.
1637 if (CompositeTypeDesc *MethodTy =
1638 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1639 // Get argument information.
1640 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1641
1642 // If not a ctor.
1643 if (!IsCTor) {
1644 // Add return type.
1645 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1646 }
1647
1648 // Add arguments.
1649 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1650 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1651 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1652 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1653 Method->AddChild(Arg);
1654 }
1655 }
1656
1657 // Add flags.
1658 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
1659 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
1660
1661 Buffer.AddChild(Method);
1662 }
1663 }
1664 break;
1665 }
1666 case DW_TAG_enumeration_type: {
1667 // Add enumerators to enumeration type.
1668 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1669 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1670 const std::string &Name = ED->getName();
1671 int64_t Value = ED->getValue();
1672 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1673 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1674 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1675 Buffer.AddChild(Enumerator);
1676 }
1677
1678 break;
1679 }
1680 case DW_TAG_subroutine_type: {
1681 // Add prototype flag.
1682 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1683 // Add return type.
1684 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
1685
1686 // Add arguments.
1687 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1688 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1689 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1690 Buffer.AddChild(Arg);
1691 }
1692
1693 break;
1694 }
1695 default: break;
1696 }
1697 }
1698
1699 // Add size if non-zero (derived types don't have a size.)
1700 if (Size) AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1701 // Add name if not anonymous or intermediate type.
1702 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1703 // Add source line info if available.
1704 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
1705 }
1706
1707 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00001708 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001709 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1710 // Construct debug information entry.
1711 DIE *Die = new DIE(DW_TAG_compile_unit);
1712 AddDelta(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0),
1713 DWLabel("section_line", 0));
1714 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1715 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1716 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1717 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1718
1719 // Construct compile unit.
1720 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1721
1722 // Add Unit to compile unit map.
1723 DescToUnitMap[UnitDesc] = Unit;
1724
1725 return Unit;
1726 }
1727
Jim Laskey9d4209f2006-11-07 19:33:46 +00001728 /// GetBaseCompileUnit - Get the main compile unit.
1729 ///
1730 CompileUnit *GetBaseCompileUnit() const {
1731 CompileUnit *Unit = CompileUnits[0];
1732 assert(Unit && "Missing compile unit.");
1733 return Unit;
1734 }
1735
Jim Laskey65195462006-10-30 13:35:07 +00001736 /// FindCompileUnit - Get the compile unit for the given descriptor.
1737 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001738 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001739 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00001740 assert(Unit && "Missing compile unit.");
1741 return Unit;
1742 }
1743
1744 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00001745 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001746 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1747 // Get the compile unit context.
1748 CompileUnitDesc *UnitDesc =
1749 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001750 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001751
1752 // Check for pre-existence.
1753 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1754 if (Slot) return Slot;
1755
1756 // Get the global variable itself.
1757 GlobalVariable *GV = GVD->getGlobalVariable();
1758
Jim Laskey2172f962006-11-30 14:35:45 +00001759 const std::string &Name = GVD->getName();
1760 const std::string &FullName = GVD->getFullName();
1761 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001762 // Create the global's variable DIE.
1763 DIE *VariableDie = new DIE(DW_TAG_variable);
1764 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001765 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001766 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001767 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001768 }
1769 AddType(VariableDie, GVD->getType(), Unit);
1770 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1771
1772 // Add source line info if available.
1773 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1774
Jim Laskeyef42a012006-11-02 20:12:39 +00001775 // Add address.
1776 DIEBlock *Block = new DIEBlock();
1777 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00001778 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
1779 AddBlock(VariableDie, DW_AT_location, 0, Block);
Jim Laskeyef42a012006-11-02 20:12:39 +00001780
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.
Jim Laskey2172f962006-11-30 14:35:45 +00001789 Unit->AddGlobal(FullName, VariableDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001790
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.)
Jim Laskey2172f962006-11-30 14:35:45 +00001807 const std::string &Name = SPD->getName();
1808 const std::string &FullName = SPD->getFullName();
1809 const std::string &LinkageName = SPD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001810 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
1811
1812 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1813 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001814 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001815 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001816 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001817 }
1818 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
1819 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, IsExternal);
1820 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
1821
1822 // Add source line info if available.
1823 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1824
1825 // Add to map.
1826 Slot = SubprogramDie;
1827
1828 // Add to context owner.
1829 Unit->getDie()->AddChild(SubprogramDie);
1830
1831 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00001832 Unit->AddGlobal(FullName, SubprogramDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001833
1834 return SubprogramDie;
1835 }
Jim Laskey65195462006-10-30 13:35:07 +00001836
1837 /// NewScopeVariable - Create a new scope variable.
1838 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001839 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1840 // Get the descriptor.
1841 VariableDesc *VD = DV->getDesc();
1842
1843 // Translate tag to proper Dwarf tag. The result variable is dropped for
1844 // now.
1845 unsigned Tag;
1846 switch (VD->getTag()) {
1847 case DW_TAG_return_variable: return NULL;
1848 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1849 case DW_TAG_auto_variable: // fall thru
1850 default: Tag = DW_TAG_variable; break;
1851 }
1852
1853 // Define variable debug information entry.
1854 DIE *VariableDie = new DIE(Tag);
1855 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1856
1857 // Add source line info if available.
1858 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1859
1860 // Add variable type.
1861 AddType(VariableDie, VD->getType(), Unit);
1862
1863 // Add variable address.
1864 MachineLocation Location;
1865 RI->getLocation(*MF, DV->getFrameIndex(), Location);
1866 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001867
Jim Laskeyef42a012006-11-02 20:12:39 +00001868 return VariableDie;
1869 }
Jim Laskey65195462006-10-30 13:35:07 +00001870
1871 /// ConstructScope - Construct the components of a scope.
1872 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001873 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00001874 unsigned ParentStartID, unsigned ParentEndID,
1875 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001876 // 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
Jim Laskey36729dd2006-11-29 16:55:57 +00001898 if (StartID == ParentStartID && EndID == ParentEndID) {
1899 // Just add stuff to the parent scope.
1900 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001901 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00001902 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1903
1904 // Add the scope bounds.
1905 if (StartID) {
1906 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1907 DWLabel("loc", StartID));
1908 } else {
1909 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1910 DWLabel("func_begin", SubprogramCount));
1911 }
1912 if (EndID) {
1913 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1914 DWLabel("loc", EndID));
1915 } else {
1916 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1917 DWLabel("func_end", SubprogramCount));
1918 }
1919
1920 // Add the scope contents.
1921 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
1922 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001923 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001924 }
1925 }
Jim Laskey65195462006-10-30 13:35:07 +00001926
1927 /// ConstructRootScope - Construct the scope for the subprogram.
1928 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001929 void ConstructRootScope(DebugScope *RootScope) {
1930 // Exit if there is no root scope.
1931 if (!RootScope) return;
1932
1933 // Get the subprogram debug information entry.
1934 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1935
1936 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00001937 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001938
1939 // Get the subprogram die.
1940 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
1941 assert(SPDie && "Missing subprogram descriptor");
1942
1943 // Add the function bounds.
1944 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1945 DWLabel("func_begin", SubprogramCount));
1946 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1947 DWLabel("func_end", SubprogramCount));
1948 MachineLocation Location(RI->getFrameRegister(*MF));
1949 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001950
Jim Laskey36729dd2006-11-29 16:55:57 +00001951 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001952 }
Jim Laskey65195462006-10-30 13:35:07 +00001953
Jim Laskeyef42a012006-11-02 20:12:39 +00001954 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1955 /// tools to recognize the object file contains Dwarf information.
1956 void EmitInitial() {
1957 // Check to see if we already emitted intial headers.
1958 if (didInitial) return;
1959 didInitial = true;
1960
1961 // Dwarf sections base addresses.
1962 if (TAI->getDwarfRequiresFrameSection()) {
1963 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1964 EmitLabel("section_frame", 0);
1965 }
1966 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
1967 EmitLabel("section_info", 0);
1968 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
1969 EmitLabel("section_abbrev", 0);
1970 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
1971 EmitLabel("section_aranges", 0);
1972 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
1973 EmitLabel("section_macinfo", 0);
1974 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
1975 EmitLabel("section_line", 0);
1976 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
1977 EmitLabel("section_loc", 0);
1978 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
1979 EmitLabel("section_pubnames", 0);
1980 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
1981 EmitLabel("section_str", 0);
1982 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
1983 EmitLabel("section_ranges", 0);
1984
1985 Asm->SwitchToTextSection(TAI->getTextSection());
1986 EmitLabel("text_begin", 0);
1987 Asm->SwitchToDataSection(TAI->getDataSection());
1988 EmitLabel("data_begin", 0);
1989
1990 // Emit common frame information.
1991 EmitInitialDebugFrame();
1992 }
1993
Jim Laskey65195462006-10-30 13:35:07 +00001994 /// EmitDIE - Recusively Emits a debug information entry.
1995 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001996 void EmitDIE(DIE *Die) const {
1997 // Get the abbreviation for this DIE.
1998 unsigned AbbrevNumber = Die->getAbbrevNumber();
1999 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2000
2001 O << "\n";
2002
2003 // Emit the code (index) for the abbreviation.
2004 EmitULEB128Bytes(AbbrevNumber);
2005 EOL(std::string("Abbrev [" +
2006 utostr(AbbrevNumber) +
2007 "] 0x" + utohexstr(Die->getOffset()) +
2008 ":0x" + utohexstr(Die->getSize()) + " " +
2009 TagString(Abbrev->getTag())));
2010
2011 const std::vector<DIEValue *> &Values = Die->getValues();
2012 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2013
2014 // Emit the DIE attribute values.
2015 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2016 unsigned Attr = AbbrevData[i].getAttribute();
2017 unsigned Form = AbbrevData[i].getForm();
2018 assert(Form && "Too many attributes for DIE (check abbreviation)");
2019
2020 switch (Attr) {
2021 case DW_AT_sibling: {
2022 EmitInt32(Die->SiblingOffset());
2023 break;
2024 }
2025 default: {
2026 // Emit an attribute using the defined form.
2027 Values[i]->EmitValue(*this, Form);
2028 break;
2029 }
2030 }
2031
2032 EOL(AttributeString(Attr));
2033 }
2034
2035 // Emit the DIE children if any.
2036 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2037 const std::vector<DIE *> &Children = Die->getChildren();
2038
2039 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2040 EmitDIE(Children[j]);
2041 }
2042
2043 EmitInt8(0); EOL("End Of Children Mark");
2044 }
2045 }
2046
Jim Laskey65195462006-10-30 13:35:07 +00002047 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2048 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002049 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2050 // Get the children.
2051 const std::vector<DIE *> &Children = Die->getChildren();
2052
2053 // If not last sibling and has children then add sibling offset attribute.
2054 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2055
2056 // Record the abbreviation.
2057 AssignAbbrevNumber(Die->getAbbrev());
2058
2059 // Get the abbreviation for this DIE.
2060 unsigned AbbrevNumber = Die->getAbbrevNumber();
2061 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2062
2063 // Set DIE offset
2064 Die->setOffset(Offset);
2065
2066 // Start the size with the size of abbreviation code.
2067 Offset += SizeULEB128(AbbrevNumber);
2068
2069 const std::vector<DIEValue *> &Values = Die->getValues();
2070 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2071
2072 // Size the DIE attribute values.
2073 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2074 // Size attribute value.
2075 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2076 }
2077
2078 // Size the DIE children if any.
2079 if (!Children.empty()) {
2080 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2081 "Children flag not set");
2082
2083 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2084 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2085 }
2086
2087 // End of children marker.
2088 Offset += sizeof(int8_t);
2089 }
2090
2091 Die->setSize(Offset - Die->getOffset());
2092 return Offset;
2093 }
Jim Laskey65195462006-10-30 13:35:07 +00002094
2095 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2096 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002097 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002098 // Process base compile unit.
2099 CompileUnit *Unit = GetBaseCompileUnit();
2100 // Compute size of compile unit header
2101 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2102 sizeof(int16_t) + // DWARF version number
2103 sizeof(int32_t) + // Offset Into Abbrev. Section
2104 sizeof(int8_t); // Pointer Size (in bytes)
2105 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002106 }
2107
Jim Laskey65195462006-10-30 13:35:07 +00002108 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
2109 /// frame.
2110 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Jim Laskeyef42a012006-11-02 20:12:39 +00002111 std::vector<MachineMove *> &Moves) {
2112 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
2113 MachineMove *Move = Moves[i];
Jim Laskey9d4209f2006-11-07 19:33:46 +00002114 unsigned LabelID = DebugInfo->MappedLabel(Move->getLabelID());
Jim Laskeyef42a012006-11-02 20:12:39 +00002115
2116 // Throw out move if the label is invalid.
Jim Laskey9d4209f2006-11-07 19:33:46 +00002117 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002118
2119 const MachineLocation &Dst = Move->getDestination();
2120 const MachineLocation &Src = Move->getSource();
2121
2122 // Advance row if new location.
2123 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
2124 EmitInt8(DW_CFA_advance_loc4);
2125 EOL("DW_CFA_advance_loc4");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002126 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002127 EOL("");
2128
2129 BaseLabelID = LabelID;
2130 BaseLabel = "loc";
2131 }
2132
2133 int stackGrowth =
2134 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2135 TargetFrameInfo::StackGrowsUp ?
2136 TAI->getAddressSize() : -TAI->getAddressSize();
2137
2138 // If advancing cfa.
2139 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
2140 if (!Src.isRegister()) {
2141 if (Src.getRegister() == MachineLocation::VirtualFP) {
2142 EmitInt8(DW_CFA_def_cfa_offset);
2143 EOL("DW_CFA_def_cfa_offset");
2144 } else {
2145 EmitInt8(DW_CFA_def_cfa);
2146 EOL("DW_CFA_def_cfa");
Jim Laskeyef42a012006-11-02 20:12:39 +00002147 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
2148 EOL("Register");
2149 }
2150
2151 int Offset = Src.getOffset() / stackGrowth;
2152
2153 EmitULEB128Bytes(Offset);
2154 EOL("Offset");
2155 } else {
2156 assert(0 && "Machine move no supported yet.");
2157 }
2158 } else {
2159 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
2160 int Offset = Dst.getOffset() / stackGrowth;
2161
2162 if (Offset < 0) {
2163 EmitInt8(DW_CFA_offset_extended_sf);
2164 EOL("DW_CFA_offset_extended_sf");
2165 EmitULEB128Bytes(Reg);
2166 EOL("Reg");
2167 EmitSLEB128Bytes(Offset);
2168 EOL("Offset");
2169 } else if (Reg < 64) {
2170 EmitInt8(DW_CFA_offset + Reg);
2171 EOL("DW_CFA_offset + Reg");
2172 EmitULEB128Bytes(Offset);
2173 EOL("Offset");
2174 } else {
2175 EmitInt8(DW_CFA_offset_extended);
2176 EOL("DW_CFA_offset_extended");
2177 EmitULEB128Bytes(Reg);
2178 EOL("Reg");
2179 EmitULEB128Bytes(Offset);
2180 EOL("Offset");
2181 }
2182 }
2183 }
2184 }
Jim Laskey65195462006-10-30 13:35:07 +00002185
2186 /// EmitDebugInfo - Emit the debug info section.
2187 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002188 void EmitDebugInfo() const {
2189 // Start debug info section.
2190 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2191
Jim Laskey5496f012006-11-09 14:52:14 +00002192 CompileUnit *Unit = GetBaseCompileUnit();
2193 DIE *Die = Unit->getDie();
2194 // Emit the compile units header.
2195 EmitLabel("info_begin", Unit->getID());
2196 // Emit size of content not including length itself
2197 unsigned ContentSize = Die->getSize() +
2198 sizeof(int16_t) + // DWARF version number
2199 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00002200 sizeof(int8_t) + // Pointer Size (in bytes)
2201 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Laskey5496f012006-11-09 14:52:14 +00002202
2203 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2204 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002205 EmitDifference("abbrev_begin", 0, "section_abbrev", 0, true);
Jim Laskey5496f012006-11-09 14:52:14 +00002206 EOL("Offset Into Abbrev. Section");
2207 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
2208
2209 EmitDIE(Die);
Jim Laskey749b01d2006-11-30 11:09:42 +00002210 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
2211 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
2212 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
2213 EmitInt8(0); EOL("Extra Pad For GDB"); // FIXME - extra pad for gdb bug.
Jim Laskey5496f012006-11-09 14:52:14 +00002214 EmitLabel("info_end", Unit->getID());
2215
2216 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002217 }
2218
Jim Laskey65195462006-10-30 13:35:07 +00002219 /// EmitAbbreviations - Emit the abbreviation section.
2220 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002221 void EmitAbbreviations() const {
2222 // Check to see if it is worth the effort.
2223 if (!Abbreviations.empty()) {
2224 // Start the debug abbrev section.
2225 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2226
2227 EmitLabel("abbrev_begin", 0);
2228
2229 // For each abbrevation.
2230 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2231 // Get abbreviation data
2232 const DIEAbbrev *Abbrev = Abbreviations[i];
2233
2234 // Emit the abbrevations code (base 1 index.)
2235 EmitULEB128Bytes(Abbrev->getNumber()); EOL("Abbreviation Code");
2236
2237 // Emit the abbreviations data.
2238 Abbrev->Emit(*this);
2239
2240 O << "\n";
2241 }
2242
2243 EmitLabel("abbrev_end", 0);
2244
2245 O << "\n";
2246 }
2247 }
2248
Jim Laskey65195462006-10-30 13:35:07 +00002249 /// EmitDebugLines - Emit source line information.
2250 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002251 void EmitDebugLines() const {
2252 // Minimum line delta, thus ranging from -10..(255-10).
2253 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2254 // Maximum line delta, thus ranging from -10..(255-10).
2255 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002256
Jim Laskeyef42a012006-11-02 20:12:39 +00002257 // Start the dwarf line section.
2258 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2259
2260 // Construct the section header.
2261
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002262 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002263 EOL("Length of Source Line Info");
2264 EmitLabel("line_begin", 0);
2265
2266 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2267
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002268 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002269 EOL("Prolog Length");
2270 EmitLabel("line_prolog_begin", 0);
2271
2272 EmitInt8(1); EOL("Minimum Instruction Length");
2273
2274 EmitInt8(1); EOL("Default is_stmt_start flag");
2275
2276 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
2277
2278 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
2279
2280 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
2281
2282 // Line number standard opcode encodings argument count
2283 EmitInt8(0); EOL("DW_LNS_copy arg count");
2284 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2285 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2286 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2287 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2288 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2289 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2290 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2291 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
2292
2293 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2294 const UniqueVector<SourceFileInfo>
2295 &SourceFiles = DebugInfo->getSourceFiles();
2296
2297 // Emit directories.
2298 for (unsigned DirectoryID = 1, NDID = Directories.size();
2299 DirectoryID <= NDID; ++DirectoryID) {
2300 EmitString(Directories[DirectoryID]); EOL("Directory");
2301 }
2302 EmitInt8(0); EOL("End of directories");
2303
2304 // Emit files.
2305 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2306 SourceID <= NSID; ++SourceID) {
2307 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2308 EmitString(SourceFile.getName()); EOL("Source");
2309 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2310 EmitULEB128Bytes(0); EOL("Mod date");
2311 EmitULEB128Bytes(0); EOL("File size");
2312 }
2313 EmitInt8(0); EOL("End of files");
2314
2315 EmitLabel("line_prolog_end", 0);
2316
2317 // A sequence for each text section.
2318 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2319 // Isolate current sections line info.
2320 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
2321
2322 if (DwarfVerbose) {
2323 O << "\t"
2324 << TAI->getCommentString() << " "
2325 << "Section "
2326 << SectionMap[j + 1].c_str() << "\n";
2327 }
2328
2329 // Dwarf assumes we start with first line of first source file.
2330 unsigned Source = 1;
2331 unsigned Line = 1;
2332
2333 // Construct rows of the address, source, line, column matrix.
2334 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2335 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey9d4209f2006-11-07 19:33:46 +00002336 unsigned LabelID = DebugInfo->MappedLabel(LineInfo.getLabelID());
2337 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002338
2339 if (DwarfVerbose) {
2340 unsigned SourceID = LineInfo.getSourceID();
2341 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2342 unsigned DirectoryID = SourceFile.getDirectoryID();
2343 O << "\t"
2344 << TAI->getCommentString() << " "
2345 << Directories[DirectoryID]
2346 << SourceFile.getName() << ":"
2347 << LineInfo.getLine() << "\n";
2348 }
2349
2350 // Define the line address.
2351 EmitInt8(0); EOL("Extended Op");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002352 EmitInt8(TAI->getAddressSize() + 1); EOL("Op size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002353 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2354 EmitReference("loc", LabelID); EOL("Location label");
2355
2356 // If change of source, then switch to the new source.
2357 if (Source != LineInfo.getSourceID()) {
2358 Source = LineInfo.getSourceID();
2359 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2360 EmitULEB128Bytes(Source); EOL("New Source");
2361 }
2362
2363 // If change of line.
2364 if (Line != LineInfo.getLine()) {
2365 // Determine offset.
2366 int Offset = LineInfo.getLine() - Line;
2367 int Delta = Offset - MinLineDelta;
2368
2369 // Update line.
2370 Line = LineInfo.getLine();
2371
2372 // If delta is small enough and in range...
2373 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2374 // ... then use fast opcode.
2375 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2376 } else {
2377 // ... otherwise use long hand.
2378 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2379 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2380 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2381 }
2382 } else {
2383 // Copy the previous row (different address or source)
2384 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2385 }
2386 }
2387
2388 // Define last address of section.
2389 EmitInt8(0); EOL("Extended Op");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002390 EmitInt8(TAI->getAddressSize() + 1); EOL("Op size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002391 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2392 EmitReference("section_end", j + 1); EOL("Section end label");
2393
2394 // Mark end of matrix.
2395 EmitInt8(0); EOL("DW_LNE_end_sequence");
2396 EmitULEB128Bytes(1); O << "\n";
2397 EmitInt8(1); O << "\n";
2398 }
2399
2400 EmitLabel("line_end", 0);
2401
2402 O << "\n";
2403 }
2404
Jim Laskey65195462006-10-30 13:35:07 +00002405 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
2406 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002407 void EmitInitialDebugFrame() {
2408 if (!TAI->getDwarfRequiresFrameSection())
2409 return;
2410
2411 int stackGrowth =
2412 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2413 TargetFrameInfo::StackGrowsUp ?
2414 TAI->getAddressSize() : -TAI->getAddressSize();
2415
2416 // Start the dwarf frame section.
2417 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2418
2419 EmitLabel("frame_common", 0);
2420 EmitDifference("frame_common_end", 0,
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002421 "frame_common_begin", 0, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002422 EOL("Length of Common Information Entry");
2423
2424 EmitLabel("frame_common_begin", 0);
2425 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2426 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2427 EmitString(""); EOL("CIE Augmentation");
2428 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
2429 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
2430 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
Jim Laskey65195462006-10-30 13:35:07 +00002431
Jim Laskeyef42a012006-11-02 20:12:39 +00002432 std::vector<MachineMove *> Moves;
2433 RI->getInitialFrameState(Moves);
2434 EmitFrameMoves(NULL, 0, Moves);
2435 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2436
2437 EmitAlign(2);
2438 EmitLabel("frame_common_end", 0);
2439
2440 O << "\n";
2441 }
2442
Jim Laskey65195462006-10-30 13:35:07 +00002443 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2444 /// section.
Jim Laskeyef42a012006-11-02 20:12:39 +00002445 void EmitFunctionDebugFrame() {
Reid Spencer5a4951e2006-11-07 06:36:36 +00002446 if (!TAI->getDwarfRequiresFrameSection())
2447 return;
Jim Laskey9d4209f2006-11-07 19:33:46 +00002448
Jim Laskeyef42a012006-11-02 20:12:39 +00002449 // Start the dwarf frame section.
2450 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2451
2452 EmitDifference("frame_end", SubprogramCount,
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002453 "frame_begin", SubprogramCount, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002454 EOL("Length of Frame Information Entry");
2455
2456 EmitLabel("frame_begin", SubprogramCount);
2457
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002458 EmitDifference("frame_common", 0, "section_frame", 0, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002459 EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002460
Jim Laskeyef42a012006-11-02 20:12:39 +00002461 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2462 EmitDifference("func_end", SubprogramCount,
2463 "func_begin", SubprogramCount);
2464 EOL("FDE address range");
2465
2466 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2467
2468 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2469
2470 EmitAlign(2);
2471 EmitLabel("frame_end", SubprogramCount);
2472
2473 O << "\n";
2474 }
2475
2476 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002477 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002478 void EmitDebugPubNames() {
2479 // Start the dwarf pubnames section.
2480 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2481
Jim Laskey5496f012006-11-09 14:52:14 +00002482 CompileUnit *Unit = GetBaseCompileUnit();
2483
2484 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002485 "pubnames_begin", Unit->getID(), true);
Jim Laskey5496f012006-11-09 14:52:14 +00002486 EOL("Length of Public Names Info");
2487
2488 EmitLabel("pubnames_begin", Unit->getID());
2489
2490 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2491
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002492 EmitDifference("info_begin", Unit->getID(), "section_info", 0, true);
Jim Laskey5496f012006-11-09 14:52:14 +00002493 EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002494
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002495 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskey5496f012006-11-09 14:52:14 +00002496 EOL("Compilation Unit Length");
2497
2498 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2499
2500 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2501 GE = Globals.end();
2502 GI != GE; ++GI) {
2503 const std::string &Name = GI->first;
2504 DIE * Entity = GI->second;
Jim Laskeyef42a012006-11-02 20:12:39 +00002505
Jim Laskey5496f012006-11-09 14:52:14 +00002506 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2507 EmitString(Name); EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00002508 }
Jim Laskey5496f012006-11-09 14:52:14 +00002509
2510 EmitInt32(0); EOL("End Mark");
2511 EmitLabel("pubnames_end", Unit->getID());
2512
2513 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002514 }
2515
2516 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002517 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002518 void EmitDebugStr() {
2519 // Check to see if it is worth the effort.
2520 if (!StringPool.empty()) {
2521 // Start the dwarf str section.
2522 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2523
2524 // For each of strings in the string pool.
2525 for (unsigned StringID = 1, N = StringPool.size();
2526 StringID <= N; ++StringID) {
2527 // Emit a label for reference from debug information entries.
2528 EmitLabel("string", StringID);
2529 // Emit the string itself.
2530 const std::string &String = StringPool[StringID];
2531 EmitString(String); O << "\n";
2532 }
2533
2534 O << "\n";
2535 }
2536 }
2537
2538 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002539 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002540 void EmitDebugLoc() {
2541 // Start the dwarf loc section.
2542 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2543
2544 O << "\n";
2545 }
2546
2547 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002548 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002549 void EmitDebugARanges() {
2550 // Start the dwarf aranges section.
2551 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2552
2553 // FIXME - Mock up
2554 #if 0
Jim Laskey5496f012006-11-09 14:52:14 +00002555 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002556
Jim Laskey5496f012006-11-09 14:52:14 +00002557 // Don't include size of length
2558 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2559
2560 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2561
2562 EmitReference("info_begin", Unit->getID());
2563 EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002564
Jim Laskey5496f012006-11-09 14:52:14 +00002565 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002566
Jim Laskey5496f012006-11-09 14:52:14 +00002567 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002568
Jim Laskey5496f012006-11-09 14:52:14 +00002569 EmitInt16(0); EOL("Pad (1)");
2570 EmitInt16(0); EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002571
Jim Laskey5496f012006-11-09 14:52:14 +00002572 // Range 1
2573 EmitReference("text_begin", 0); EOL("Address");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002574 EmitDifference("text_end", 0, "text_begin", 0, true); EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002575
Jim Laskey5496f012006-11-09 14:52:14 +00002576 EmitInt32(0); EOL("EOM (1)");
2577 EmitInt32(0); EOL("EOM (2)");
2578
2579 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002580 #endif
2581 }
2582
2583 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002584 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002585 void EmitDebugRanges() {
2586 // Start the dwarf ranges section.
2587 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2588
2589 O << "\n";
2590 }
2591
2592 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002593 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002594 void EmitDebugMacInfo() {
2595 // Start the dwarf macinfo section.
2596 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2597
2598 O << "\n";
2599 }
2600
Jim Laskey65195462006-10-30 13:35:07 +00002601 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2602 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002603 void ConstructCompileUnitDIEs() {
2604 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
2605
2606 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00002607 unsigned ID = DebugInfo->RecordSource(CUW[i]);
2608 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002609 CompileUnits.push_back(Unit);
2610 }
2611 }
2612
Jim Laskey65195462006-10-30 13:35:07 +00002613 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2614 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002615 void ConstructGlobalDIEs() {
2616 std::vector<GlobalVariableDesc *> GlobalVariables =
2617 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
2618
2619 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2620 GlobalVariableDesc *GVD = GlobalVariables[i];
2621 NewGlobalVariable(GVD);
2622 }
2623 }
Jim Laskey65195462006-10-30 13:35:07 +00002624
2625 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2626 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002627 void ConstructSubprogramDIEs() {
2628 std::vector<SubprogramDesc *> Subprograms =
2629 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
2630
2631 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2632 SubprogramDesc *SPD = Subprograms[i];
2633 NewSubprogram(SPD);
2634 }
2635 }
Jim Laskey65195462006-10-30 13:35:07 +00002636
2637 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
2638 ///
2639 bool ShouldEmitDwarf() const { return shouldEmit; }
2640
2641public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002642 //===--------------------------------------------------------------------===//
2643 // Main entry points.
2644 //
2645 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
2646 : O(OS)
2647 , Asm(A)
2648 , TAI(T)
2649 , TD(Asm->TM.getTargetData())
2650 , RI(Asm->TM.getRegisterInfo())
2651 , M(NULL)
2652 , MF(NULL)
2653 , DebugInfo(NULL)
2654 , didInitial(false)
2655 , shouldEmit(false)
2656 , SubprogramCount(0)
2657 , CompileUnits()
2658 , AbbreviationsSet(InitAbbreviationsSetSize)
2659 , Abbreviations()
2660 , ValuesSet(InitValuesSetSize)
2661 , Values()
2662 , StringPool()
2663 , DescToUnitMap()
2664 , SectionMap()
2665 , SectionSourceLines()
2666 {
2667 }
2668 virtual ~Dwarf() {
2669 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2670 delete CompileUnits[i];
2671 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2672 delete Values[j];
2673 }
2674
Jim Laskey65195462006-10-30 13:35:07 +00002675 // Accessors.
2676 //
2677 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
2678
2679 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2680 /// created it. Set by the target AsmPrinter.
Jim Laskeyef42a012006-11-02 20:12:39 +00002681 void SetDebugInfo(MachineDebugInfo *DI) {
2682 // Make sure initial declarations are made.
2683 if (!DebugInfo && DI->hasInfo()) {
2684 DebugInfo = DI;
2685 shouldEmit = true;
2686
2687 // Emit initial sections
2688 EmitInitial();
2689
2690 // Create all the compile unit DIEs.
2691 ConstructCompileUnitDIEs();
2692
2693 // Create DIEs for each of the externally visible global variables.
2694 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002695
Jim Laskeyef42a012006-11-02 20:12:39 +00002696 // Create DIEs for each of the externally visible subprograms.
2697 ConstructSubprogramDIEs();
2698
2699 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00002700 SectionMap.insert(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002701 }
2702 }
2703
Jim Laskey65195462006-10-30 13:35:07 +00002704 /// BeginModule - Emit all Dwarf sections that should come prior to the
2705 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002706 void BeginModule(Module *M) {
2707 this->M = M;
2708
2709 if (!ShouldEmitDwarf()) return;
2710 EOL("Dwarf Begin Module");
2711 }
2712
Jim Laskey65195462006-10-30 13:35:07 +00002713 /// EndModule - Emit all Dwarf sections that should come after the content.
2714 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002715 void EndModule() {
2716 if (!ShouldEmitDwarf()) return;
2717 EOL("Dwarf End Module");
2718
2719 // Standard sections final addresses.
2720 Asm->SwitchToTextSection(TAI->getTextSection());
2721 EmitLabel("text_end", 0);
2722 Asm->SwitchToDataSection(TAI->getDataSection());
2723 EmitLabel("data_end", 0);
2724
2725 // End text sections.
2726 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2727 Asm->SwitchToTextSection(SectionMap[i].c_str());
2728 EmitLabel("section_end", i);
2729 }
2730
2731 // Compute DIE offsets and sizes.
2732 SizeAndOffsets();
2733
2734 // Emit all the DIEs into a debug info section
2735 EmitDebugInfo();
2736
2737 // Corresponding abbreviations into a abbrev section.
2738 EmitAbbreviations();
2739
2740 // Emit source line correspondence into a debug line section.
2741 EmitDebugLines();
2742
2743 // Emit info into a debug pubnames section.
2744 EmitDebugPubNames();
2745
2746 // Emit info into a debug str section.
2747 EmitDebugStr();
2748
2749 // Emit info into a debug loc section.
2750 EmitDebugLoc();
2751
2752 // Emit info into a debug aranges section.
2753 EmitDebugARanges();
2754
2755 // Emit info into a debug ranges section.
2756 EmitDebugRanges();
2757
2758 // Emit info into a debug macinfo section.
2759 EmitDebugMacInfo();
2760 }
2761
Jim Laskey65195462006-10-30 13:35:07 +00002762 /// BeginFunction - Gather pre-function debug information. Assumes being
2763 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002764 void BeginFunction(MachineFunction *MF) {
2765 this->MF = MF;
2766
2767 if (!ShouldEmitDwarf()) return;
2768 EOL("Dwarf Begin Function");
2769
2770 // Begin accumulating function debug information.
2771 DebugInfo->BeginFunction(MF);
2772
2773 // Assumes in correct section after the entry point.
2774 EmitLabel("func_begin", ++SubprogramCount);
2775 }
2776
Jim Laskey65195462006-10-30 13:35:07 +00002777 /// EndFunction - Gather and emit post-function debug information.
2778 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002779 void EndFunction() {
2780 if (!ShouldEmitDwarf()) return;
2781 EOL("Dwarf End Function");
2782
2783 // Define end label for subprogram.
2784 EmitLabel("func_end", SubprogramCount);
2785
2786 // Get function line info.
2787 const std::vector<SourceLineInfo> &LineInfos = DebugInfo->getSourceLines();
2788
2789 if (!LineInfos.empty()) {
2790 // Get section line info.
2791 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2792 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2793 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2794 // Append the function info to section info.
2795 SectionLineInfos.insert(SectionLineInfos.end(),
2796 LineInfos.begin(), LineInfos.end());
2797 }
2798
2799 // Construct scopes for subprogram.
2800 ConstructRootScope(DebugInfo->getRootScope());
2801
2802 // Emit function frame information.
2803 EmitFunctionDebugFrame();
2804
2805 // Reset the line numbers for the next function.
2806 DebugInfo->ClearLineInfo();
2807
2808 // Clear function debug information.
2809 DebugInfo->EndFunction();
2810 }
Jim Laskey65195462006-10-30 13:35:07 +00002811};
2812
Jim Laskey0d086af2006-02-27 12:43:29 +00002813} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00002814
2815//===----------------------------------------------------------------------===//
2816
Jim Laskeyd18e2892006-01-20 20:34:06 +00002817/// Emit - Print the abbreviation using the specified Dwarf writer.
2818///
Jim Laskey65195462006-10-30 13:35:07 +00002819void DIEAbbrev::Emit(const Dwarf &DW) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002820 // Emit its Dwarf tag type.
2821 DW.EmitULEB128Bytes(Tag);
2822 DW.EOL(TagString(Tag));
2823
2824 // Emit whether it has children DIEs.
2825 DW.EmitULEB128Bytes(ChildrenFlag);
2826 DW.EOL(ChildrenString(ChildrenFlag));
2827
2828 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00002829 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002830 const DIEAbbrevData &AttrData = Data[i];
2831
2832 // Emit attribute type.
2833 DW.EmitULEB128Bytes(AttrData.getAttribute());
2834 DW.EOL(AttributeString(AttrData.getAttribute()));
2835
2836 // Emit form type.
2837 DW.EmitULEB128Bytes(AttrData.getForm());
2838 DW.EOL(FormEncodingString(AttrData.getForm()));
2839 }
2840
2841 // Mark end of abbreviation.
2842 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
2843 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
2844}
2845
2846#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00002847void DIEAbbrev::print(std::ostream &O) {
2848 O << "Abbreviation @"
2849 << std::hex << (intptr_t)this << std::dec
2850 << " "
2851 << TagString(Tag)
2852 << " "
2853 << ChildrenString(ChildrenFlag)
2854 << "\n";
2855
2856 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
2857 O << " "
2858 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002859 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00002860 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002861 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002862 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00002863}
Bill Wendlingbdc679d2006-11-29 00:39:47 +00002864void DIEAbbrev::dump() { print(llvm_cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002865#endif
2866
2867//===----------------------------------------------------------------------===//
2868
Jim Laskeyef42a012006-11-02 20:12:39 +00002869#ifndef NDEBUG
2870void DIEValue::dump() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +00002871 print(llvm_cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002872}
Jim Laskeyef42a012006-11-02 20:12:39 +00002873#endif
2874
2875//===----------------------------------------------------------------------===//
2876
Jim Laskey063e7652006-01-17 17:31:53 +00002877/// EmitValue - Emit integer of appropriate size.
2878///
Jim Laskey65195462006-10-30 13:35:07 +00002879void DIEInteger::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +00002880 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00002881 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00002882 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002883 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002884 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002885 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002886 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002887 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002888 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002889 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +00002890 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
2891 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +00002892 default: assert(0 && "DIE Value form not supported yet"); break;
2893 }
2894}
2895
Jim Laskey063e7652006-01-17 17:31:53 +00002896//===----------------------------------------------------------------------===//
2897
2898/// EmitValue - Emit string value.
2899///
Jim Laskey65195462006-10-30 13:35:07 +00002900void DIEString::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002901 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00002902}
2903
Jim Laskey063e7652006-01-17 17:31:53 +00002904//===----------------------------------------------------------------------===//
2905
2906/// EmitValue - Emit label value.
2907///
Jim Laskey65195462006-10-30 13:35:07 +00002908void DIEDwarfLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002909 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +00002910}
2911
2912/// SizeOf - Determine size of label value in bytes.
2913///
Jim Laskey65195462006-10-30 13:35:07 +00002914unsigned DIEDwarfLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002915 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002916}
Jim Laskeyef42a012006-11-02 20:12:39 +00002917
Jim Laskey063e7652006-01-17 17:31:53 +00002918//===----------------------------------------------------------------------===//
2919
Jim Laskeyd18e2892006-01-20 20:34:06 +00002920/// EmitValue - Emit label value.
2921///
Jim Laskey65195462006-10-30 13:35:07 +00002922void DIEObjectLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002923 DW.EmitReference(Label);
2924}
2925
2926/// SizeOf - Determine size of label value in bytes.
2927///
Jim Laskey65195462006-10-30 13:35:07 +00002928unsigned DIEObjectLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002929 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00002930}
2931
2932//===----------------------------------------------------------------------===//
2933
Jim Laskey063e7652006-01-17 17:31:53 +00002934/// EmitValue - Emit delta value.
2935///
Jim Laskey65195462006-10-30 13:35:07 +00002936void DIEDelta::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002937 bool IsSmall = Form == DW_FORM_data4;
2938 DW.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00002939}
2940
2941/// SizeOf - Determine size of delta value in bytes.
2942///
Jim Laskey65195462006-10-30 13:35:07 +00002943unsigned DIEDelta::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002944 if (Form == DW_FORM_data4) return 4;
Jim Laskey563321a2006-09-06 18:34:40 +00002945 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002946}
2947
2948//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00002949
Jim Laskeyb8509c52006-03-23 18:07:55 +00002950/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002951///
Jim Laskey65195462006-10-30 13:35:07 +00002952void DIEntry::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +00002953 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00002954}
Jim Laskeyd18e2892006-01-20 20:34:06 +00002955
2956//===----------------------------------------------------------------------===//
2957
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002958/// ComputeSize - calculate the size of the block.
2959///
Jim Laskey65195462006-10-30 13:35:07 +00002960unsigned DIEBlock::ComputeSize(Dwarf &DW) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002961 if (!Size) {
2962 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2963
2964 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2965 Size += Values[i]->SizeOf(DW, AbbrevData[i].getForm());
2966 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002967 }
2968 return Size;
2969}
2970
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002971/// EmitValue - Emit block data.
2972///
Jim Laskey65195462006-10-30 13:35:07 +00002973void DIEBlock::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002974 switch (Form) {
2975 case DW_FORM_block1: DW.EmitInt8(Size); break;
2976 case DW_FORM_block2: DW.EmitInt16(Size); break;
2977 case DW_FORM_block4: DW.EmitInt32(Size); break;
2978 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
2979 default: assert(0 && "Improper form for block"); break;
2980 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002981
2982 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2983
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002984 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2985 DW.EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002986 Values[i]->EmitValue(DW, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002987 }
2988}
2989
2990/// SizeOf - Determine size of block data in bytes.
2991///
Jim Laskey65195462006-10-30 13:35:07 +00002992unsigned DIEBlock::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002993 switch (Form) {
2994 case DW_FORM_block1: return Size + sizeof(int8_t);
2995 case DW_FORM_block2: return Size + sizeof(int16_t);
2996 case DW_FORM_block4: return Size + sizeof(int32_t);
Jim Laskeyef42a012006-11-02 20:12:39 +00002997 case DW_FORM_block: return Size + SizeULEB128(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002998 default: assert(0 && "Improper form for block"); break;
2999 }
3000 return 0;
3001}
3002
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003003//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003004/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00003005
3006DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003007 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00003008 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00003009}
Jim Laskeyef42a012006-11-02 20:12:39 +00003010
Jim Laskeyb8509c52006-03-23 18:07:55 +00003011/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
3012///
3013void DIE::AddSiblingOffset() {
3014 DIEInteger *DI = new DIEInteger(0);
3015 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00003016 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00003017}
3018
Jim Laskeyef42a012006-11-02 20:12:39 +00003019/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003020///
Jim Laskeyef42a012006-11-02 20:12:39 +00003021void DIE::Profile(FoldingSetNodeID &ID) {
3022 Abbrev.Profile(ID);
3023
3024 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3025 ID.AddPointer(Children[i]);
3026
3027 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3028 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003029}
Jim Laskeyef42a012006-11-02 20:12:39 +00003030
3031#ifndef NDEBUG
3032void DIE::print(std::ostream &O, unsigned IncIndent) {
3033 static unsigned IndentCount = 0;
3034 IndentCount += IncIndent;
3035 const std::string Indent(IndentCount, ' ');
3036 bool isBlock = Abbrev.getTag() == 0;
3037
3038 if (!isBlock) {
3039 O << Indent
3040 << "Die: "
3041 << "0x" << std::hex << (intptr_t)this << std::dec
3042 << ", Offset: " << Offset
3043 << ", Size: " << Size
3044 << "\n";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003045
Jim Laskeyef42a012006-11-02 20:12:39 +00003046 O << Indent
3047 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00003048 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00003049 << ChildrenString(Abbrev.getChildrenFlag());
3050 } else {
3051 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00003052 }
3053 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003054
Jim Laskeyef42a012006-11-02 20:12:39 +00003055 const std::vector<DIEAbbrevData> &Data = Abbrev.getData();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003056
Jim Laskeyef42a012006-11-02 20:12:39 +00003057 IndentCount += 2;
3058 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3059 O << Indent;
3060 if (!isBlock) {
3061 O << AttributeString(Data[i].getAttribute());
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003062 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00003063 O << "Blk[" << i << "]";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003064 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003065 O << " "
3066 << FormEncodingString(Data[i].getForm())
3067 << " ";
3068 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00003069 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00003070 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003071 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00003072
Jim Laskeyef42a012006-11-02 20:12:39 +00003073 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3074 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00003075 }
Jim Laskey063e7652006-01-17 17:31:53 +00003076
Jim Laskeyef42a012006-11-02 20:12:39 +00003077 if (!isBlock) O << "\n";
3078 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00003079}
3080
Jim Laskeyef42a012006-11-02 20:12:39 +00003081void DIE::dump() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +00003082 print(llvm_cerr);
Jim Laskey41886992006-04-07 16:34:46 +00003083}
Jim Laskeybd761842006-02-27 17:27:12 +00003084#endif
Jim Laskey65195462006-10-30 13:35:07 +00003085
3086//===----------------------------------------------------------------------===//
3087/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00003088///
Jim Laskey65195462006-10-30 13:35:07 +00003089
3090DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3091 const TargetAsmInfo *T) {
3092 DW = new Dwarf(OS, A, T);
3093}
3094
3095DwarfWriter::~DwarfWriter() {
3096 delete DW;
3097}
3098
3099/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
3100/// created it. Set by the target AsmPrinter.
3101void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
3102 DW->SetDebugInfo(DI);
3103}
3104
3105/// BeginModule - Emit all Dwarf sections that should come prior to the
3106/// content.
3107void DwarfWriter::BeginModule(Module *M) {
3108 DW->BeginModule(M);
3109}
3110
3111/// EndModule - Emit all Dwarf sections that should come after the content.
3112///
3113void DwarfWriter::EndModule() {
3114 DW->EndModule();
3115}
3116
3117/// BeginFunction - Gather pre-function debug information. Assumes being
3118/// emitted immediately after the function entry point.
3119void DwarfWriter::BeginFunction(MachineFunction *MF) {
3120 DW->BeginFunction(MF);
3121}
3122
3123/// EndFunction - Gather and emit post-function debug information.
3124///
3125void DwarfWriter::EndFunction() {
3126 DW->EndFunction();
3127}