blob: f16986ba017614aea804da64d91812943cbfbc7f [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
Jim Laskeyb2efb852006-01-04 22:28:25 +000035#include <iostream>
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
142 void print(std::ostream &O) const {
143 O << ".debug_" << Tag;
144 if (Number) O << Number;
145 }
146#endif
Jim Laskeybd761842006-02-27 17:27:12 +0000147};
148
149//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000150/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
151/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000152class DIEAbbrevData {
153private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000154 /// Attribute - Dwarf attribute code.
155 ///
156 unsigned Attribute;
157
158 /// Form - Dwarf form code.
159 ///
160 unsigned Form;
Jim Laskey0d086af2006-02-27 12:43:29 +0000161
162public:
163 DIEAbbrevData(unsigned A, unsigned F)
164 : Attribute(A)
165 , Form(F)
166 {}
167
Jim Laskeybd761842006-02-27 17:27:12 +0000168 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000169 unsigned getAttribute() const { return Attribute; }
170 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000171
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000172 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000173 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000174 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000175 ID.AddInteger(Attribute);
176 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000177 }
178};
Jim Laskey063e7652006-01-17 17:31:53 +0000179
Jim Laskey0d086af2006-02-27 12:43:29 +0000180//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000181/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
182/// information object.
183class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000184private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000185 /// Tag - Dwarf tag code.
186 ///
187 unsigned Tag;
188
189 /// Unique number for node.
190 ///
191 unsigned Number;
192
193 /// ChildrenFlag - Dwarf children flag.
194 ///
195 unsigned ChildrenFlag;
196
197 /// Data - Raw data bytes for abbreviation.
198 ///
199 std::vector<DIEAbbrevData> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000200
Jim Laskey0d086af2006-02-27 12:43:29 +0000201public:
Jim Laskey063e7652006-01-17 17:31:53 +0000202
Jim Laskey0d086af2006-02-27 12:43:29 +0000203 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000204 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000205 , ChildrenFlag(C)
206 , Data()
207 {}
208 ~DIEAbbrev() {}
209
Jim Laskeybd761842006-02-27 17:27:12 +0000210 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000211 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000212 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000213 unsigned getChildrenFlag() const { return ChildrenFlag; }
214 const std::vector<DIEAbbrevData> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000215 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000216 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000217 void setNumber(unsigned N) { Number = N; }
218
Jim Laskey0d086af2006-02-27 12:43:29 +0000219 /// AddAttribute - Adds another set of attribute information to the
220 /// abbreviation.
221 void AddAttribute(unsigned Attribute, unsigned Form) {
222 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000223 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000224
Jim Laskeyb8509c52006-03-23 18:07:55 +0000225 /// AddFirstAttribute - Adds a set of attribute information to the front
226 /// of the abbreviation.
227 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
228 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
229 }
230
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000231 /// Profile - Used to gather unique data for the abbreviation folding set.
232 ///
233 void Profile(FoldingSetNodeID &ID) {
234 ID.AddInteger(Tag);
235 ID.AddInteger(ChildrenFlag);
236
237 // For each attribute description.
238 for (unsigned i = 0, N = Data.size(); i < N; ++i)
239 Data[i].Profile(ID);
240 }
241
Jim Laskey0d086af2006-02-27 12:43:29 +0000242 /// Emit - Print the abbreviation using the specified Dwarf writer.
243 ///
Jim Laskey65195462006-10-30 13:35:07 +0000244 void Emit(const Dwarf &DW) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000245
246#ifndef NDEBUG
247 void print(std::ostream &O);
248 void dump();
249#endif
250};
Jim Laskey063e7652006-01-17 17:31:53 +0000251
Jim Laskey0d086af2006-02-27 12:43:29 +0000252//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000253/// DIE - A structured debug information entry. Has an abbreviation which
254/// describes it's organization.
255class DIE : public FoldingSetNode {
256protected:
257 /// Abbrev - Buffer for constructing abbreviation.
258 ///
259 DIEAbbrev Abbrev;
260
261 /// Offset - Offset in debug info section.
262 ///
263 unsigned Offset;
264
265 /// Size - Size of instance + children.
266 ///
267 unsigned Size;
268
269 /// Children DIEs.
270 ///
271 std::vector<DIE *> Children;
272
273 /// Attributes values.
274 ///
275 std::vector<DIEValue *> Values;
276
277public:
278 DIE(unsigned Tag)
279 : Abbrev(Tag, DW_CHILDREN_no)
280 , Offset(0)
281 , Size(0)
282 , Children()
283 , Values()
284 {}
285 virtual ~DIE();
286
287 // Accessors.
288 DIEAbbrev &getAbbrev() { return Abbrev; }
289 unsigned getAbbrevNumber() const {
290 return Abbrev.getNumber();
291 }
292 unsigned getOffset() const { return Offset; }
293 unsigned getSize() const { return Size; }
294 const std::vector<DIE *> &getChildren() const { return Children; }
295 const std::vector<DIEValue *> &getValues() const { return Values; }
296 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
297 void setOffset(unsigned O) { Offset = O; }
298 void setSize(unsigned S) { Size = S; }
299
300 /// AddValue - Add a value and attributes to a DIE.
301 ///
302 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
303 Abbrev.AddAttribute(Attribute, Form);
304 Values.push_back(Value);
305 }
306
307 /// SiblingOffset - Return the offset of the debug information entry's
308 /// sibling.
309 unsigned SiblingOffset() const { return Offset + Size; }
310
311 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
312 ///
313 void AddSiblingOffset();
314
315 /// AddChild - Add a child to the DIE.
316 ///
317 void AddChild(DIE *Child) {
318 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
319 Children.push_back(Child);
320 }
321
322 /// Detach - Detaches objects connected to it after copying.
323 ///
324 void Detach() {
325 Children.clear();
326 }
327
328 /// Profile - Used to gather unique data for the value folding set.
329 ///
330 void Profile(FoldingSetNodeID &ID) ;
331
332#ifndef NDEBUG
333 void print(std::ostream &O, unsigned IncIndent = 0);
334 void dump();
335#endif
336};
337
338//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000339/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000340///
341class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000342public:
343 enum {
344 isInteger,
345 isString,
346 isLabel,
347 isAsIsLabel,
348 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000349 isEntry,
350 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000351 };
352
Jim Laskeyef42a012006-11-02 20:12:39 +0000353 /// Type - Type of data stored in the value.
354 ///
355 unsigned Type;
Jim Laskey0d086af2006-02-27 12:43:29 +0000356
Jim Laskeyef42a012006-11-02 20:12:39 +0000357 DIEValue(unsigned T)
358 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000359 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000360 virtual ~DIEValue() {}
361
Jim Laskeyf6733882006-11-02 21:48:18 +0000362 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000363 unsigned getType() const { return Type; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000364
Jim Laskey0d086af2006-02-27 12:43:29 +0000365 // Implement isa/cast/dyncast.
366 static bool classof(const DIEValue *) { return true; }
367
368 /// EmitValue - Emit value via the Dwarf writer.
369 ///
Jim Laskey65195462006-10-30 13:35:07 +0000370 virtual void EmitValue(const Dwarf &DW, unsigned Form) const = 0;
Jim Laskey0d086af2006-02-27 12:43:29 +0000371
372 /// SizeOf - Return the size of a value in bytes.
373 ///
Jim Laskey65195462006-10-30 13:35:07 +0000374 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const = 0;
Jim Laskeyef42a012006-11-02 20:12:39 +0000375
376 /// Profile - Used to gather unique data for the value folding set.
377 ///
378 virtual void Profile(FoldingSetNodeID &ID) = 0;
379
380#ifndef NDEBUG
381 virtual void print(std::ostream &O) = 0;
382 void dump();
383#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000384};
Jim Laskey063e7652006-01-17 17:31:53 +0000385
Jim Laskey0d086af2006-02-27 12:43:29 +0000386//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000387/// DWInteger - An integer value DIE.
388///
Jim Laskey0d086af2006-02-27 12:43:29 +0000389class DIEInteger : public DIEValue {
390private:
391 uint64_t Integer;
392
393public:
394 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000395
Jim Laskey0d086af2006-02-27 12:43:29 +0000396 // Implement isa/cast/dyncast.
397 static bool classof(const DIEInteger *) { return true; }
398 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
399
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000400 /// BestForm - Choose the best form for integer.
401 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000402 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
403 if (IsSigned) {
404 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
405 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
406 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
407 } else {
408 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
409 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
410 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
411 }
412 return DW_FORM_data8;
413 }
414
Jim Laskey0d086af2006-02-27 12:43:29 +0000415 /// EmitValue - Emit integer of appropriate size.
416 ///
Jim Laskey65195462006-10-30 13:35:07 +0000417 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000418
419 /// SizeOf - Determine size of integer value in bytes.
420 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000421 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
422 switch (Form) {
423 case DW_FORM_flag: // Fall thru
424 case DW_FORM_ref1: // Fall thru
425 case DW_FORM_data1: return sizeof(int8_t);
426 case DW_FORM_ref2: // Fall thru
427 case DW_FORM_data2: return sizeof(int16_t);
428 case DW_FORM_ref4: // Fall thru
429 case DW_FORM_data4: return sizeof(int32_t);
430 case DW_FORM_ref8: // Fall thru
431 case DW_FORM_data8: return sizeof(int64_t);
432 case DW_FORM_udata: return SizeULEB128(Integer);
433 case DW_FORM_sdata: return SizeSLEB128(Integer);
434 default: assert(0 && "DIE Value form not supported yet"); break;
435 }
436 return 0;
437 }
438
439 /// Profile - Used to gather unique data for the value folding set.
440 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000441 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000442 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000443 ID.AddInteger(Integer);
444 }
Jim Laskey5496f012006-11-09 14:52:14 +0000445 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000446
447#ifndef NDEBUG
448 virtual void print(std::ostream &O) {
449 O << "Int: " << (int64_t)Integer
450 << " 0x" << std::hex << Integer << std::dec;
451 }
452#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000453};
Jim Laskey063e7652006-01-17 17:31:53 +0000454
Jim Laskey0d086af2006-02-27 12:43:29 +0000455//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000456/// DIEString - A string value DIE.
457///
Jim Laskeyef42a012006-11-02 20:12:39 +0000458class DIEString : public DIEValue {
459public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000460 const std::string String;
461
462 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000463
Jim Laskey0d086af2006-02-27 12:43:29 +0000464 // Implement isa/cast/dyncast.
465 static bool classof(const DIEString *) { return true; }
466 static bool classof(const DIEValue *S) { return S->Type == isString; }
467
468 /// EmitValue - Emit string value.
469 ///
Jim Laskey65195462006-10-30 13:35:07 +0000470 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000471
472 /// SizeOf - Determine size of string value in bytes.
473 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000474 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
475 return String.size() + sizeof(char); // sizeof('\0');
476 }
477
478 /// Profile - Used to gather unique data for the value folding set.
479 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000480 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000481 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000482 ID.AddString(String);
483 }
Jim Laskey5496f012006-11-09 14:52:14 +0000484 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000485
486#ifndef NDEBUG
487 virtual void print(std::ostream &O) {
488 O << "Str: \"" << String << "\"";
489 }
490#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000491};
Jim Laskey063e7652006-01-17 17:31:53 +0000492
Jim Laskey0d086af2006-02-27 12:43:29 +0000493//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000494/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000495//
Jim Laskeyef42a012006-11-02 20:12:39 +0000496class DIEDwarfLabel : public DIEValue {
497public:
498
Jim Laskey0d086af2006-02-27 12:43:29 +0000499 const DWLabel Label;
500
501 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000502
Jim Laskey0d086af2006-02-27 12:43:29 +0000503 // Implement isa/cast/dyncast.
504 static bool classof(const DIEDwarfLabel *) { return true; }
505 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
506
507 /// EmitValue - Emit label value.
508 ///
Jim Laskey65195462006-10-30 13:35:07 +0000509 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000510
511 /// SizeOf - Determine size of label value in bytes.
512 ///
Jim Laskey65195462006-10-30 13:35:07 +0000513 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000514
515 /// Profile - Used to gather unique data for the value folding set.
516 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000517 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000518 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000519 Label.Profile(ID);
520 }
Jim Laskey5496f012006-11-09 14:52:14 +0000521 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000522
523#ifndef NDEBUG
524 virtual void print(std::ostream &O) {
525 O << "Lbl: ";
526 Label.print(O);
527 }
528#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000529};
Jim Laskey063e7652006-01-17 17:31:53 +0000530
Jim Laskey063e7652006-01-17 17:31:53 +0000531
Jim Laskey0d086af2006-02-27 12:43:29 +0000532//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000533/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000534//
Jim Laskeyef42a012006-11-02 20:12:39 +0000535class DIEObjectLabel : public DIEValue {
536public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000537 const std::string Label;
538
539 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000540
Jim Laskey0d086af2006-02-27 12:43:29 +0000541 // Implement isa/cast/dyncast.
542 static bool classof(const DIEObjectLabel *) { return true; }
543 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
544
545 /// EmitValue - Emit label value.
546 ///
Jim Laskey65195462006-10-30 13:35:07 +0000547 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000548
549 /// SizeOf - Determine size of label value in bytes.
550 ///
Jim Laskey65195462006-10-30 13:35:07 +0000551 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000552
553 /// Profile - Used to gather unique data for the value folding set.
554 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000555 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000556 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000557 ID.AddString(Label);
558 }
Jim Laskey5496f012006-11-09 14:52:14 +0000559 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000560
561#ifndef NDEBUG
562 virtual void print(std::ostream &O) {
563 O << "Obj: " << Label;
564 }
565#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000566};
Jim Laskey063e7652006-01-17 17:31:53 +0000567
Jim Laskey0d086af2006-02-27 12:43:29 +0000568//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000569/// DIEDelta - A simple label difference DIE.
570///
Jim Laskeyef42a012006-11-02 20:12:39 +0000571class DIEDelta : public DIEValue {
572public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000573 const DWLabel LabelHi;
574 const DWLabel LabelLo;
575
576 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
577 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000578
Jim Laskey0d086af2006-02-27 12:43:29 +0000579 // Implement isa/cast/dyncast.
580 static bool classof(const DIEDelta *) { return true; }
581 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
582
583 /// EmitValue - Emit delta value.
584 ///
Jim Laskey65195462006-10-30 13:35:07 +0000585 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000586
587 /// SizeOf - Determine size of delta value in bytes.
588 ///
Jim Laskey65195462006-10-30 13:35:07 +0000589 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000590
591 /// Profile - Used to gather unique data for the value folding set.
592 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000593 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
594 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000595 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000596 LabelHi.Profile(ID);
597 LabelLo.Profile(ID);
598 }
Jim Laskey5496f012006-11-09 14:52:14 +0000599 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000600
601#ifndef NDEBUG
602 virtual void print(std::ostream &O) {
603 O << "Del: ";
604 LabelHi.print(O);
605 O << "-";
606 LabelLo.print(O);
607 }
608#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000609};
Jim Laskey063e7652006-01-17 17:31:53 +0000610
Jim Laskey0d086af2006-02-27 12:43:29 +0000611//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000612/// DIEntry - A pointer to another debug information entry. An instance of this
613/// class can also be used as a proxy for a debug information entry not yet
614/// defined (ie. types.)
615class DIEntry : public DIEValue {
616public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000617 DIE *Entry;
618
619 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000620
Jim Laskey0d086af2006-02-27 12:43:29 +0000621 // Implement isa/cast/dyncast.
622 static bool classof(const DIEntry *) { return true; }
623 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
624
Jim Laskeyb8509c52006-03-23 18:07:55 +0000625 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000626 ///
Jim Laskey65195462006-10-30 13:35:07 +0000627 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000628
Jim Laskeyb8509c52006-03-23 18:07:55 +0000629 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000630 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000631 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
632 return sizeof(int32_t);
633 }
634
635 /// Profile - Used to gather unique data for the value folding set.
636 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000637 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
638 ID.AddInteger(isEntry);
639 ID.AddPointer(Entry);
640 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000641 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000642 ID.AddInteger(isEntry);
643
Jim Laskeyef42a012006-11-02 20:12:39 +0000644 if (Entry) {
645 ID.AddPointer(Entry);
646 } else {
647 ID.AddPointer(this);
648 }
649 }
650
651#ifndef NDEBUG
652 virtual void print(std::ostream &O) {
653 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
654 }
655#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000656};
657
658//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000659/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000660//
Jim Laskeyef42a012006-11-02 20:12:39 +0000661class DIEBlock : public DIEValue, public DIE {
662public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000663 unsigned Size; // Size in bytes excluding size header.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000664
665 DIEBlock()
666 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000667 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000668 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000669 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000670 ~DIEBlock() {
671 }
672
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000673 // Implement isa/cast/dyncast.
674 static bool classof(const DIEBlock *) { return true; }
675 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
676
677 /// ComputeSize - calculate the size of the block.
678 ///
Jim Laskey65195462006-10-30 13:35:07 +0000679 unsigned ComputeSize(Dwarf &DW);
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000680
681 /// BestForm - Choose the best form for data.
682 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000683 unsigned BestForm() const {
684 if ((unsigned char)Size == Size) return DW_FORM_block1;
685 if ((unsigned short)Size == Size) return DW_FORM_block2;
686 if ((unsigned int)Size == Size) return DW_FORM_block4;
687 return DW_FORM_block;
688 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000689
690 /// EmitValue - Emit block data.
691 ///
Jim Laskey65195462006-10-30 13:35:07 +0000692 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000693
694 /// SizeOf - Determine size of block data in bytes.
695 ///
Jim Laskey65195462006-10-30 13:35:07 +0000696 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000697
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000698
Jim Laskeyef42a012006-11-02 20:12:39 +0000699 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000700 ///
Reid Spencer97821312006-11-02 23:56:21 +0000701 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000702 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000703 DIE::Profile(ID);
704 }
705
706#ifndef NDEBUG
707 virtual void print(std::ostream &O) {
708 O << "Blk: ";
709 DIE::print(O, 5);
710 }
711#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000712};
713
714//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000715/// CompileUnit - This dwarf writer support class manages information associate
716/// with a source file.
717class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000718private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000719 /// Desc - Compile unit debug descriptor.
720 ///
721 CompileUnitDesc *Desc;
722
723 /// ID - File identifier for source.
724 ///
725 unsigned ID;
726
727 /// Die - Compile unit debug information entry.
728 ///
729 DIE *Die;
730
731 /// DescToDieMap - Tracks the mapping of unit level debug informaton
732 /// descriptors to debug information entries.
733 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
734
735 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
736 /// descriptors to debug information entries using a DIEntry proxy.
737 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
738
739 /// Globals - A map of globally visible named entities for this unit.
740 ///
741 std::map<std::string, DIE *> Globals;
742
743 /// DiesSet - Used to uniquely define dies within the compile unit.
744 ///
745 FoldingSet<DIE> DiesSet;
746
747 /// Dies - List of all dies in the compile unit.
748 ///
749 std::vector<DIE *> Dies;
Jim Laskey0d086af2006-02-27 12:43:29 +0000750
751public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000752 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
753 : Desc(CUD)
754 , ID(I)
755 , Die(D)
756 , DescToDieMap()
757 , DescToDIEntryMap()
758 , Globals()
759 , DiesSet(InitDiesSetSize)
760 , Dies()
761 {}
762
763 ~CompileUnit() {
764 delete Die;
765
766 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
767 delete Dies[i];
768 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000769
Jim Laskeybd761842006-02-27 17:27:12 +0000770 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000771 CompileUnitDesc *getDesc() const { return Desc; }
772 unsigned getID() const { return ID; }
773 DIE* getDie() const { return Die; }
774 std::map<std::string, DIE *> &getGlobals() { return Globals; }
775
776 /// hasContent - Return true if this compile unit has something to write out.
777 ///
778 bool hasContent() const {
779 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000780 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000781
782 /// AddGlobal - Add a new global entity to the compile unit.
783 ///
784 void AddGlobal(const std::string &Name, DIE *Die) {
785 Globals[Name] = Die;
786 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000787
Jim Laskeyef42a012006-11-02 20:12:39 +0000788 /// getDieMapSlotFor - Returns the debug information entry map slot for the
789 /// specified debug descriptor.
790 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
791 return DescToDieMap[DD];
792 }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000793
Jim Laskeyef42a012006-11-02 20:12:39 +0000794 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
795 /// specified debug descriptor.
796 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DD) {
797 return DescToDIEntryMap[DD];
798 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000799
Jim Laskeyef42a012006-11-02 20:12:39 +0000800 /// AddDie - Adds or interns the DIE to the compile unit.
801 ///
802 DIE *AddDie(DIE &Buffer) {
803 FoldingSetNodeID ID;
804 Buffer.Profile(ID);
805 void *Where;
806 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
807
808 if (!Die) {
809 Die = new DIE(Buffer);
810 DiesSet.InsertNode(Die, Where);
811 this->Die->AddChild(Die);
812 Buffer.Detach();
813 }
814
815 return Die;
816 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000817};
818
Jim Laskey65195462006-10-30 13:35:07 +0000819//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000820/// Dwarf - Emits Dwarf debug and exception handling directives.
821///
Jim Laskey65195462006-10-30 13:35:07 +0000822class Dwarf {
823
824private:
825
826 //===--------------------------------------------------------------------===//
827 // Core attributes used by the Dwarf writer.
828 //
829
830 //
831 /// O - Stream to .s file.
832 ///
833 std::ostream &O;
834
835 /// Asm - Target of Dwarf emission.
836 ///
837 AsmPrinter *Asm;
838
839 /// TAI - Target Asm Printer.
840 const TargetAsmInfo *TAI;
841
842 /// TD - Target data.
843 const TargetData *TD;
844
845 /// RI - Register Information.
846 const MRegisterInfo *RI;
847
848 /// M - Current module.
849 ///
850 Module *M;
851
852 /// MF - Current machine function.
853 ///
854 MachineFunction *MF;
855
856 /// DebugInfo - Collected debug information.
857 ///
858 MachineDebugInfo *DebugInfo;
859
860 /// didInitial - Flag to indicate if initial emission has been done.
861 ///
862 bool didInitial;
863
864 /// shouldEmit - Flag to indicate if debug information should be emitted.
865 ///
866 bool shouldEmit;
867
868 /// SubprogramCount - The running count of functions being compiled.
869 ///
870 unsigned SubprogramCount;
871
872 //===--------------------------------------------------------------------===//
873 // Attributes used to construct specific Dwarf sections.
874 //
875
876 /// CompileUnits - All the compile units involved in this build. The index
877 /// of each entry in this vector corresponds to the sources in DebugInfo.
878 std::vector<CompileUnit *> CompileUnits;
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000879
Jim Laskeyef42a012006-11-02 20:12:39 +0000880 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +0000881 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000882 FoldingSet<DIEAbbrev> AbbreviationsSet;
883
884 /// Abbreviations - A list of all the unique abbreviations in use.
885 ///
886 std::vector<DIEAbbrev *> Abbreviations;
Jim Laskey65195462006-10-30 13:35:07 +0000887
Jim Laskeyef42a012006-11-02 20:12:39 +0000888 /// ValuesSet - Used to uniquely define values.
889 ///
890 FoldingSet<DIEValue> ValuesSet;
891
892 /// Values - A list of all the unique values in use.
893 ///
894 std::vector<DIEValue *> Values;
895
Jim Laskey65195462006-10-30 13:35:07 +0000896 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +0000897 ///
Jim Laskey65195462006-10-30 13:35:07 +0000898 UniqueVector<std::string> StringPool;
899
900 /// UnitMap - Map debug information descriptor to compile unit.
901 ///
902 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
903
Jim Laskey65195462006-10-30 13:35:07 +0000904 /// SectionMap - Provides a unique id per text section.
905 ///
906 UniqueVector<std::string> SectionMap;
907
908 /// SectionSourceLines - Tracks line numbers per text section.
909 ///
910 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
911
912
913public:
914
915 //===--------------------------------------------------------------------===//
916 // Emission and print routines
917 //
918
919 /// PrintHex - Print a value as a hexidecimal value.
920 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000921 void PrintHex(int Value) const {
922 O << "0x" << std::hex << Value << std::dec;
923 }
Jim Laskey65195462006-10-30 13:35:07 +0000924
925 /// EOL - Print a newline character to asm stream. If a comment is present
926 /// then it will be printed first. Comments should not contain '\n'.
Jim Laskeyef42a012006-11-02 20:12:39 +0000927 void EOL(const std::string &Comment) const {
928 if (DwarfVerbose && !Comment.empty()) {
929 O << "\t"
930 << TAI->getCommentString()
931 << " "
932 << Comment;
933 }
934 O << "\n";
935 }
Jim Laskey65195462006-10-30 13:35:07 +0000936
937 /// EmitAlign - Print a align directive.
938 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000939 void EmitAlign(unsigned Alignment) const {
940 O << TAI->getAlignDirective() << Alignment << "\n";
941 }
Jim Laskey65195462006-10-30 13:35:07 +0000942
943 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
944 /// unsigned leb128 value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000945 void EmitULEB128Bytes(unsigned Value) const {
946 if (TAI->hasLEB128()) {
947 O << "\t.uleb128\t"
948 << Value;
949 } else {
950 O << TAI->getData8bitsDirective();
951 PrintULEB128(O, Value);
952 }
953 }
Jim Laskey65195462006-10-30 13:35:07 +0000954
955 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
956 /// signed leb128 value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000957 void EmitSLEB128Bytes(int Value) const {
958 if (TAI->hasLEB128()) {
959 O << "\t.sleb128\t"
960 << Value;
961 } else {
962 O << TAI->getData8bitsDirective();
963 PrintSLEB128(O, Value);
964 }
965 }
Jim Laskey65195462006-10-30 13:35:07 +0000966
967 /// EmitInt8 - Emit a byte directive and value.
968 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000969 void EmitInt8(int Value) const {
970 O << TAI->getData8bitsDirective();
971 PrintHex(Value & 0xFF);
972 }
Jim Laskey65195462006-10-30 13:35:07 +0000973
974 /// EmitInt16 - Emit a short directive and value.
975 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000976 void EmitInt16(int Value) const {
977 O << TAI->getData16bitsDirective();
978 PrintHex(Value & 0xFFFF);
979 }
Jim Laskey65195462006-10-30 13:35:07 +0000980
981 /// EmitInt32 - Emit a long directive and value.
982 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000983 void EmitInt32(int Value) const {
984 O << TAI->getData32bitsDirective();
985 PrintHex(Value);
986 }
987
Jim Laskey65195462006-10-30 13:35:07 +0000988 /// EmitInt64 - Emit a long long directive and value.
989 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000990 void EmitInt64(uint64_t Value) const {
991 if (TAI->getData64bitsDirective()) {
992 O << TAI->getData64bitsDirective();
993 PrintHex(Value);
994 } else {
995 if (TD->isBigEndian()) {
996 EmitInt32(unsigned(Value >> 32)); O << "\n";
997 EmitInt32(unsigned(Value));
998 } else {
999 EmitInt32(unsigned(Value)); O << "\n";
1000 EmitInt32(unsigned(Value >> 32));
1001 }
1002 }
1003 }
1004
Jim Laskey65195462006-10-30 13:35:07 +00001005 /// EmitString - Emit a string with quotes and a null terminator.
Jim Laskeyef42a012006-11-02 20:12:39 +00001006 /// Special characters are emitted properly.
Jim Laskey65195462006-10-30 13:35:07 +00001007 /// \literal (Eg. '\t') \endliteral
Jim Laskeyef42a012006-11-02 20:12:39 +00001008 void EmitString(const std::string &String) const {
1009 O << TAI->getAsciiDirective()
1010 << "\"";
1011 for (unsigned i = 0, N = String.size(); i < N; ++i) {
1012 unsigned char C = String[i];
1013
1014 if (!isascii(C) || iscntrl(C)) {
1015 switch(C) {
1016 case '\b': O << "\\b"; break;
1017 case '\f': O << "\\f"; break;
1018 case '\n': O << "\\n"; break;
1019 case '\r': O << "\\r"; break;
1020 case '\t': O << "\\t"; break;
1021 default:
1022 O << '\\';
1023 O << char('0' + ((C >> 6) & 7));
1024 O << char('0' + ((C >> 3) & 7));
1025 O << char('0' + ((C >> 0) & 7));
1026 break;
1027 }
1028 } else if (C == '\"') {
1029 O << "\\\"";
1030 } else if (C == '\'') {
1031 O << "\\\'";
1032 } else {
1033 O << C;
1034 }
1035 }
1036 O << "\\0\"";
1037 }
Jim Laskey65195462006-10-30 13:35:07 +00001038
1039 /// PrintLabelName - Print label name in form used by Dwarf writer.
1040 ///
1041 void PrintLabelName(DWLabel Label) const {
1042 PrintLabelName(Label.Tag, Label.Number);
1043 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001044 void PrintLabelName(const char *Tag, unsigned Number) const {
1045 O << TAI->getPrivateGlobalPrefix()
1046 << "debug_"
1047 << Tag;
1048 if (Number) O << Number;
1049 }
Jim Laskey65195462006-10-30 13:35:07 +00001050
1051 /// EmitLabel - Emit location label for internal use by Dwarf.
1052 ///
1053 void EmitLabel(DWLabel Label) const {
1054 EmitLabel(Label.Tag, Label.Number);
1055 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001056 void EmitLabel(const char *Tag, unsigned Number) const {
1057 PrintLabelName(Tag, Number);
1058 O << ":\n";
1059 }
Jim Laskey65195462006-10-30 13:35:07 +00001060
1061 /// EmitReference - Emit a reference to a label.
1062 ///
1063 void EmitReference(DWLabel Label) const {
1064 EmitReference(Label.Tag, Label.Number);
1065 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001066 void EmitReference(const char *Tag, unsigned Number) const {
1067 if (TAI->getAddressSize() == 4)
1068 O << TAI->getData32bitsDirective();
1069 else
1070 O << TAI->getData64bitsDirective();
1071
1072 PrintLabelName(Tag, Number);
1073 }
1074 void EmitReference(const std::string &Name) const {
1075 if (TAI->getAddressSize() == 4)
1076 O << TAI->getData32bitsDirective();
1077 else
1078 O << TAI->getData64bitsDirective();
1079
1080 O << Name;
1081 }
Jim Laskey65195462006-10-30 13:35:07 +00001082
1083 /// EmitDifference - Emit the difference between two labels. Some
1084 /// assemblers do not behave with absolute expressions with data directives,
1085 /// so there is an option (needsSet) to use an intermediary set expression.
1086 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo) const {
1087 EmitDifference(LabelHi.Tag, LabelHi.Number, LabelLo.Tag, LabelLo.Number);
1088 }
1089 void EmitDifference(const char *TagHi, unsigned NumberHi,
Jim Laskeyef42a012006-11-02 20:12:39 +00001090 const char *TagLo, unsigned NumberLo) const {
1091 if (TAI->needsSet()) {
1092 static unsigned SetCounter = 0;
1093
1094 O << "\t.set\t";
1095 PrintLabelName("set", SetCounter);
1096 O << ",";
1097 PrintLabelName(TagHi, NumberHi);
1098 O << "-";
1099 PrintLabelName(TagLo, NumberLo);
1100 O << "\n";
1101
1102 if (TAI->getAddressSize() == sizeof(int32_t))
1103 O << TAI->getData32bitsDirective();
1104 else
1105 O << TAI->getData64bitsDirective();
1106
1107 PrintLabelName("set", SetCounter);
1108
1109 ++SetCounter;
1110 } else {
1111 if (TAI->getAddressSize() == sizeof(int32_t))
1112 O << TAI->getData32bitsDirective();
1113 else
1114 O << TAI->getData64bitsDirective();
1115
1116 PrintLabelName(TagHi, NumberHi);
1117 O << "-";
1118 PrintLabelName(TagLo, NumberLo);
1119 }
1120 }
Jim Laskey65195462006-10-30 13:35:07 +00001121
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001122 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Jim Laskey65195462006-10-30 13:35:07 +00001123 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001124 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1125 // Profile the node so that we can make it unique.
1126 FoldingSetNodeID ID;
1127 Abbrev.Profile(ID);
1128
1129 // Check the set for priors.
1130 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
1131
1132 // If it's newly added.
1133 if (InSet == &Abbrev) {
1134 // Add to abbreviation list.
1135 Abbreviations.push_back(&Abbrev);
1136 // Assign the vector position + 1 as its number.
1137 Abbrev.setNumber(Abbreviations.size());
1138 } else {
1139 // Assign existing abbreviation number.
1140 Abbrev.setNumber(InSet->getNumber());
1141 }
1142 }
1143
Jim Laskey65195462006-10-30 13:35:07 +00001144 /// NewString - Add a string to the constant pool and returns a label.
1145 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001146 DWLabel NewString(const std::string &String) {
1147 unsigned StringID = StringPool.insert(String);
1148 return DWLabel("string", StringID);
1149 }
Jim Laskey65195462006-10-30 13:35:07 +00001150
Jim Laskeyef42a012006-11-02 20:12:39 +00001151 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1152 /// entry.
1153 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1154 DIEntry *Value;
1155
1156 if (Entry) {
1157 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001158 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001159 void *Where;
1160 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
1161
Jim Laskeyf6733882006-11-02 21:48:18 +00001162 if (Value) return Value;
Jim Laskeyef42a012006-11-02 20:12:39 +00001163
1164 Value = new DIEntry(Entry);
1165 ValuesSet.InsertNode(Value, Where);
1166 } else {
1167 Value = new DIEntry(Entry);
1168 }
1169
1170 Values.push_back(Value);
1171 return Value;
1172 }
1173
1174 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1175 ///
1176 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1177 Value->Entry = Entry;
1178 // Add to values set if not already there. If it is, we merely have a
1179 // duplicate in the values list (no harm.)
1180 ValuesSet.GetOrInsertNode(Value);
1181 }
1182
1183 /// AddUInt - Add an unsigned integer attribute data and value.
1184 ///
1185 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1186 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1187
1188 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001189 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001190 void *Where;
1191 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1192 if (!Value) {
1193 Value = new DIEInteger(Integer);
1194 ValuesSet.InsertNode(Value, Where);
1195 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001196 }
1197
1198 Die->AddValue(Attribute, Form, Value);
1199 }
1200
1201 /// AddSInt - Add an signed integer attribute data and value.
1202 ///
1203 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1204 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1205
1206 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001207 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001208 void *Where;
1209 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1210 if (!Value) {
1211 Value = new DIEInteger(Integer);
1212 ValuesSet.InsertNode(Value, Where);
1213 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001214 }
1215
1216 Die->AddValue(Attribute, Form, Value);
1217 }
1218
1219 /// AddString - Add a std::string attribute data and value.
1220 ///
1221 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1222 const std::string &String) {
1223 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001224 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001225 void *Where;
1226 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1227 if (!Value) {
1228 Value = new DIEString(String);
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 /// AddLabel - Add a Dwarf label attribute data and value.
1237 ///
1238 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1239 const DWLabel &Label) {
1240 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001241 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001242 void *Where;
1243 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1244 if (!Value) {
1245 Value = new DIEDwarfLabel(Label);
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 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1254 ///
1255 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1256 const std::string &Label) {
1257 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001258 DIEObjectLabel::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 DIEObjectLabel(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 /// AddDelta - Add a label delta attribute data and value.
1271 ///
1272 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1273 const DWLabel &Hi, const DWLabel &Lo) {
1274 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001275 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001276 void *Where;
1277 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1278 if (!Value) {
1279 Value = new DIEDelta(Hi, Lo);
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 /// AddDIEntry - Add a DIE attribute data and value.
1288 ///
1289 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1290 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1291 }
1292
1293 /// AddBlock - Add block data.
1294 ///
1295 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1296 Block->ComputeSize(*this);
1297 FoldingSetNodeID ID;
1298 Block->Profile(ID);
1299 void *Where;
1300 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1301 if (!Value) {
1302 Value = Block;
1303 ValuesSet.InsertNode(Value, Where);
1304 Values.push_back(Value);
1305 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001306 delete Block;
1307 }
1308
1309 Die->AddValue(Attribute, Block->BestForm(), Value);
1310 }
1311
Jim Laskey65195462006-10-30 13:35:07 +00001312private:
1313
1314 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001315 /// entry.
1316 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1317 if (File && Line) {
1318 CompileUnit *FileUnit = FindCompileUnit(File);
1319 unsigned FileID = FileUnit->getID();
1320 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1321 AddUInt(Die, DW_AT_decl_line, 0, Line);
1322 }
1323 }
Jim Laskey65195462006-10-30 13:35:07 +00001324
1325 /// AddAddress - Add an address attribute to a die based on the location
1326 /// provided.
1327 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001328 const MachineLocation &Location) {
1329 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1330 DIEBlock *Block = new DIEBlock();
1331
1332 if (Location.isRegister()) {
1333 if (Reg < 32) {
1334 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1335 } else {
1336 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1337 AddUInt(Block, 0, DW_FORM_udata, Reg);
1338 }
1339 } else {
1340 if (Reg < 32) {
1341 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1342 } else {
1343 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1344 AddUInt(Block, 0, DW_FORM_udata, Reg);
1345 }
1346 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1347 }
1348
1349 AddBlock(Die, Attribute, 0, Block);
1350 }
1351
1352 /// AddBasicType - Add a new basic type attribute to the specified entity.
1353 ///
1354 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1355 const std::string &Name,
1356 unsigned Encoding, unsigned Size) {
1357 DIE *Die = ConstructBasicType(Unit, Name, Encoding, Size);
1358 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1359 }
1360
1361 /// ConstructBasicType - Construct a new basic type.
1362 ///
1363 DIE *ConstructBasicType(CompileUnit *Unit,
1364 const std::string &Name,
1365 unsigned Encoding, unsigned Size) {
1366 DIE Buffer(DW_TAG_base_type);
1367 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1368 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1369 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1370 return Unit->AddDie(Buffer);
1371 }
1372
1373 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1374 ///
1375 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1376 DIE *Die = ConstructPointerType(Unit, Name);
1377 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1378 }
1379
1380 /// ConstructPointerType - Construct a new pointer type.
1381 ///
1382 DIE *ConstructPointerType(CompileUnit *Unit, const std::string &Name) {
1383 DIE Buffer(DW_TAG_pointer_type);
1384 AddUInt(&Buffer, DW_AT_byte_size, 0, TAI->getAddressSize());
1385 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1386 return Unit->AddDie(Buffer);
1387 }
1388
1389 /// AddType - Add a new type attribute to the specified entity.
1390 ///
1391 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1392 if (!TyDesc) {
1393 AddBasicType(Entity, Unit, "", DW_ATE_signed, 4);
1394 } else {
1395 // Check for pre-existence.
1396 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
1397
1398 // If it exists then use the existing value.
1399 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001400 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1401 return;
1402 }
1403
1404 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1405 // FIXME - Not sure why programs and variables are coming through here.
1406 // Short cut for handling subprogram types (not really a TyDesc.)
1407 AddPointerType(Entity, Unit, SubprogramTy->getName());
1408 } else if (GlobalVariableDesc *GlobalTy =
1409 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1410 // FIXME - Not sure why programs and variables are coming through here.
1411 // Short cut for handling global variable types (not really a TyDesc.)
1412 AddPointerType(Entity, Unit, GlobalTy->getName());
1413 } else {
1414 // Set up proxy.
1415 Slot = NewDIEntry();
1416
1417 // Construct type.
1418 DIE Buffer(DW_TAG_base_type);
1419 ConstructType(Buffer, TyDesc, Unit);
1420
1421 // Add debug information entry to entity and unit.
1422 DIE *Die = Unit->AddDie(Buffer);
1423 SetDIEntry(Slot, Die);
1424 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1425 }
1426 }
1427 }
1428
1429 /// ConstructType - Adds all the required attributes to the type.
1430 ///
1431 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1432 // Get core information.
1433 const std::string &Name = TyDesc->getName();
1434 uint64_t Size = TyDesc->getSize() >> 3;
1435
1436 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1437 // Fundamental types like int, float, bool
1438 Buffer.setTag(DW_TAG_base_type);
1439 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1440 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1441 // Pointers, tyepdefs et al.
1442 Buffer.setTag(DerivedTy->getTag());
1443 // Map to main type, void will not have a type.
1444 if (TypeDesc *FromTy = DerivedTy->getFromType())
1445 AddType(&Buffer, FromTy, Unit);
1446 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1447 // Fetch tag.
1448 unsigned Tag = CompTy->getTag();
1449
1450 // Set tag accordingly.
1451 if (Tag == DW_TAG_vector_type)
1452 Buffer.setTag(DW_TAG_array_type);
1453 else
1454 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00001455
Jim Laskeyef42a012006-11-02 20:12:39 +00001456 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1457
1458 switch (Tag) {
1459 case DW_TAG_vector_type:
1460 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1461 // Fall thru
1462 case DW_TAG_array_type: {
1463 // Add element type.
1464 if (TypeDesc *FromTy = CompTy->getFromType())
1465 AddType(&Buffer, FromTy, Unit);
1466
1467 // Don't emit size attribute.
1468 Size = 0;
1469
1470 // Construct an anonymous type for index type.
1471 DIE *IndexTy = ConstructBasicType(Unit, "", DW_ATE_signed, 4);
1472
1473 // Add subranges to array type.
1474 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1475 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1476 int64_t Lo = SRD->getLo();
1477 int64_t Hi = SRD->getHi();
1478 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1479
1480 // If a range is available.
1481 if (Lo != Hi) {
1482 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1483 // Only add low if non-zero.
1484 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1485 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1486 }
1487
1488 Buffer.AddChild(Subrange);
1489 }
1490 break;
1491 }
1492 case DW_TAG_structure_type:
1493 case DW_TAG_union_type: {
1494 // Add elements to structure type.
1495 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1496 DebugInfoDesc *Element = Elements[i];
1497
1498 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1499 // Add field or base class.
1500
1501 unsigned Tag = MemberDesc->getTag();
1502
1503 // Extract the basic information.
1504 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001505 uint64_t Size = MemberDesc->getSize();
1506 uint64_t Align = MemberDesc->getAlign();
1507 uint64_t Offset = MemberDesc->getOffset();
1508
1509 // Construct member debug information entry.
1510 DIE *Member = new DIE(Tag);
1511
1512 // Add name if not "".
1513 if (!Name.empty())
1514 AddString(Member, DW_AT_name, DW_FORM_string, Name);
1515 // Add location if available.
1516 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1517
1518 // Most of the time the field info is the same as the members.
1519 uint64_t FieldSize = Size;
1520 uint64_t FieldAlign = Align;
1521 uint64_t FieldOffset = Offset;
1522
1523 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1524 AddType(Member, FromTy, Unit);
1525 FieldSize = FromTy->getSize();
1526 FieldAlign = FromTy->getSize();
1527 }
1528
1529 // Unless we have a bit field.
1530 if (Tag == DW_TAG_member && FieldSize != Size) {
1531 // Construct the alignment mask.
1532 uint64_t AlignMask = ~(FieldAlign - 1);
1533 // Determine the high bit + 1 of the declared size.
1534 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1535 // Work backwards to determine the base offset of the field.
1536 FieldOffset = HiMark - FieldSize;
1537 // Now normalize offset to the field.
1538 Offset -= FieldOffset;
1539
1540 // Maybe we need to work from the other end.
1541 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1542
1543 // Add size and offset.
1544 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1545 AddUInt(Member, DW_AT_bit_size, 0, Size);
1546 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1547 }
1548
1549 // Add computation for offset.
1550 DIEBlock *Block = new DIEBlock();
1551 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1552 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1553 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1554
1555 // Add accessibility (public default unless is base class.
1556 if (MemberDesc->isProtected()) {
1557 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1558 } else if (MemberDesc->isPrivate()) {
1559 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1560 } else if (Tag == DW_TAG_inheritance) {
1561 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1562 }
1563
1564 Buffer.AddChild(Member);
1565 } else if (GlobalVariableDesc *StaticDesc =
1566 dyn_cast<GlobalVariableDesc>(Element)) {
1567 // Add static member.
1568
1569 // Construct member debug information entry.
1570 DIE *Static = new DIE(DW_TAG_variable);
1571
1572 // Add name and mangled name.
1573 const std::string &Name = StaticDesc->getDisplayName();
1574 const std::string &MangledName = StaticDesc->getName();
1575 AddString(Static, DW_AT_name, DW_FORM_string, Name);
1576 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1577 MangledName);
1578
1579 // Add location.
1580 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1581
1582 // Add type.
1583 if (TypeDesc *StaticTy = StaticDesc->getType())
1584 AddType(Static, StaticTy, Unit);
1585
1586 // Add flags.
1587 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
1588 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
1589
1590 Buffer.AddChild(Static);
1591 } else if (SubprogramDesc *MethodDesc =
1592 dyn_cast<SubprogramDesc>(Element)) {
1593 // Add member function.
1594
1595 // Construct member debug information entry.
1596 DIE *Method = new DIE(DW_TAG_subprogram);
1597
1598 // Add name and mangled name.
1599 const std::string &Name = MethodDesc->getDisplayName();
1600 const std::string &MangledName = MethodDesc->getName();
1601 bool IsCTor = false;
1602
1603 if (Name.empty()) {
1604 AddString(Method, DW_AT_name, DW_FORM_string, MangledName);
1605 IsCTor = TyDesc->getName() == MangledName;
1606 } else {
1607 AddString(Method, DW_AT_name, DW_FORM_string, Name);
1608 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
1609 MangledName);
1610 }
1611
1612 // Add location.
1613 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1614
1615 // Add type.
1616 if (CompositeTypeDesc *MethodTy =
1617 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1618 // Get argument information.
1619 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1620
1621 // If not a ctor.
1622 if (!IsCTor) {
1623 // Add return type.
1624 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1625 }
1626
1627 // Add arguments.
1628 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1629 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1630 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1631 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1632 Method->AddChild(Arg);
1633 }
1634 }
1635
1636 // Add flags.
1637 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
1638 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
1639
1640 Buffer.AddChild(Method);
1641 }
1642 }
1643 break;
1644 }
1645 case DW_TAG_enumeration_type: {
1646 // Add enumerators to enumeration type.
1647 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1648 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1649 const std::string &Name = ED->getName();
1650 int64_t Value = ED->getValue();
1651 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1652 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1653 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1654 Buffer.AddChild(Enumerator);
1655 }
1656
1657 break;
1658 }
1659 case DW_TAG_subroutine_type: {
1660 // Add prototype flag.
1661 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1662 // Add return type.
1663 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
1664
1665 // Add arguments.
1666 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1667 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1668 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1669 Buffer.AddChild(Arg);
1670 }
1671
1672 break;
1673 }
1674 default: break;
1675 }
1676 }
1677
1678 // Add size if non-zero (derived types don't have a size.)
1679 if (Size) AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1680 // Add name if not anonymous or intermediate type.
1681 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1682 // Add source line info if available.
1683 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
1684 }
1685
1686 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00001687 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001688 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1689 // Construct debug information entry.
1690 DIE *Die = new DIE(DW_TAG_compile_unit);
1691 AddDelta(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0),
1692 DWLabel("section_line", 0));
1693 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1694 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1695 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1696 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1697
1698 // Construct compile unit.
1699 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1700
1701 // Add Unit to compile unit map.
1702 DescToUnitMap[UnitDesc] = Unit;
1703
1704 return Unit;
1705 }
1706
Jim Laskey9d4209f2006-11-07 19:33:46 +00001707 /// GetBaseCompileUnit - Get the main compile unit.
1708 ///
1709 CompileUnit *GetBaseCompileUnit() const {
1710 CompileUnit *Unit = CompileUnits[0];
1711 assert(Unit && "Missing compile unit.");
1712 return Unit;
1713 }
1714
Jim Laskey65195462006-10-30 13:35:07 +00001715 /// FindCompileUnit - Get the compile unit for the given descriptor.
1716 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001717 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001718 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00001719 assert(Unit && "Missing compile unit.");
1720 return Unit;
1721 }
1722
1723 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00001724 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001725 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1726 // Get the compile unit context.
1727 CompileUnitDesc *UnitDesc =
1728 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001729 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001730
1731 // Check for pre-existence.
1732 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1733 if (Slot) return Slot;
1734
1735 // Get the global variable itself.
1736 GlobalVariable *GV = GVD->getGlobalVariable();
1737
1738 const std::string &Name = GVD->hasMangledName() ? GVD->getDisplayName()
1739 : GVD->getName();
1740 const std::string &MangledName = GVD->hasMangledName() ? GVD->getName()
1741 : "";
1742 // Create the global's variable DIE.
1743 DIE *VariableDie = new DIE(DW_TAG_variable);
1744 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
1745 if (!MangledName.empty()) {
1746 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1747 MangledName);
1748 }
1749 AddType(VariableDie, GVD->getType(), Unit);
1750 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1751
1752 // Add source line info if available.
1753 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1754
1755 // Work up linkage name.
1756 const std::string LinkageName = Asm->getGlobalLinkName(GV);
1757
1758 // Add address.
1759 DIEBlock *Block = new DIEBlock();
1760 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
1761 AddObjectLabel(Block, 0, DW_FORM_udata, LinkageName);
1762 AddBlock(VariableDie, DW_AT_location, 0, Block);
1763
1764 // Add to map.
1765 Slot = VariableDie;
1766
1767 // Add to context owner.
1768 Unit->getDie()->AddChild(VariableDie);
1769
1770 // Expose as global.
1771 // FIXME - need to check external flag.
1772 Unit->AddGlobal(Name, VariableDie);
1773
1774 return VariableDie;
1775 }
Jim Laskey65195462006-10-30 13:35:07 +00001776
1777 /// NewSubprogram - Add a new subprogram DIE.
1778 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001779 DIE *NewSubprogram(SubprogramDesc *SPD) {
1780 // Get the compile unit context.
1781 CompileUnitDesc *UnitDesc =
1782 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001783 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001784
1785 // Check for pre-existence.
1786 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1787 if (Slot) return Slot;
1788
1789 // Gather the details (simplify add attribute code.)
1790 const std::string &Name = SPD->hasMangledName() ? SPD->getDisplayName()
1791 : SPD->getName();
1792 const std::string &MangledName = SPD->hasMangledName() ? SPD->getName()
1793 : "";
1794 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
1795
1796 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1797 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
1798 if (!MangledName.empty()) {
1799 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1800 MangledName);
1801 }
1802 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
1803 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, IsExternal);
1804 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
1805
1806 // Add source line info if available.
1807 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1808
1809 // Add to map.
1810 Slot = SubprogramDie;
1811
1812 // Add to context owner.
1813 Unit->getDie()->AddChild(SubprogramDie);
1814
1815 // Expose as global.
1816 Unit->AddGlobal(Name, SubprogramDie);
1817
1818 return SubprogramDie;
1819 }
Jim Laskey65195462006-10-30 13:35:07 +00001820
1821 /// NewScopeVariable - Create a new scope variable.
1822 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001823 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1824 // Get the descriptor.
1825 VariableDesc *VD = DV->getDesc();
1826
1827 // Translate tag to proper Dwarf tag. The result variable is dropped for
1828 // now.
1829 unsigned Tag;
1830 switch (VD->getTag()) {
1831 case DW_TAG_return_variable: return NULL;
1832 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1833 case DW_TAG_auto_variable: // fall thru
1834 default: Tag = DW_TAG_variable; break;
1835 }
1836
1837 // Define variable debug information entry.
1838 DIE *VariableDie = new DIE(Tag);
1839 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1840
1841 // Add source line info if available.
1842 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1843
1844 // Add variable type.
1845 AddType(VariableDie, VD->getType(), Unit);
1846
1847 // Add variable address.
1848 MachineLocation Location;
1849 RI->getLocation(*MF, DV->getFrameIndex(), Location);
1850 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001851
Jim Laskeyef42a012006-11-02 20:12:39 +00001852 return VariableDie;
1853 }
Jim Laskey65195462006-10-30 13:35:07 +00001854
1855 /// ConstructScope - Construct the components of a scope.
1856 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001857 void ConstructScope(DebugScope *ParentScope,
1858 DIE *ParentDie, CompileUnit *Unit) {
1859 // Add variables to scope.
1860 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1861 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1862 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1863 if (VariableDie) ParentDie->AddChild(VariableDie);
1864 }
1865
1866 // Add nested scopes.
1867 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1868 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1869 // Define the Scope debug information entry.
1870 DebugScope *Scope = Scopes[j];
1871 // FIXME - Ignore inlined functions for the time being.
1872 if (!Scope->getParent()) continue;
1873
Jim Laskey9d4209f2006-11-07 19:33:46 +00001874 unsigned StartID = DebugInfo->MappedLabel(Scope->getStartLabelID());
1875 unsigned EndID = DebugInfo->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00001876
Jim Laskey9d4209f2006-11-07 19:33:46 +00001877 // Ignore empty scopes.
1878 if (StartID == EndID && StartID != 0) continue;
1879 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00001880
1881 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1882
1883 // Add the scope bounds.
1884 if (StartID) {
1885 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1886 DWLabel("loc", StartID));
1887 } else {
1888 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1889 DWLabel("func_begin", SubprogramCount));
1890 }
1891 if (EndID) {
1892 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1893 DWLabel("loc", EndID));
1894 } else {
1895 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1896 DWLabel("func_end", SubprogramCount));
1897 }
1898
1899 // Add the scope contents.
1900 ConstructScope(Scope, ScopeDie, Unit);
1901 ParentDie->AddChild(ScopeDie);
1902 }
1903 }
Jim Laskey65195462006-10-30 13:35:07 +00001904
1905 /// ConstructRootScope - Construct the scope for the subprogram.
1906 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001907 void ConstructRootScope(DebugScope *RootScope) {
1908 // Exit if there is no root scope.
1909 if (!RootScope) return;
1910
1911 // Get the subprogram debug information entry.
1912 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1913
1914 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00001915 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001916
1917 // Get the subprogram die.
1918 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
1919 assert(SPDie && "Missing subprogram descriptor");
1920
1921 // Add the function bounds.
1922 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1923 DWLabel("func_begin", SubprogramCount));
1924 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1925 DWLabel("func_end", SubprogramCount));
1926 MachineLocation Location(RI->getFrameRegister(*MF));
1927 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001928
Jim Laskeyef42a012006-11-02 20:12:39 +00001929 ConstructScope(RootScope, SPDie, Unit);
1930 }
Jim Laskey65195462006-10-30 13:35:07 +00001931
Jim Laskeyef42a012006-11-02 20:12:39 +00001932 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1933 /// tools to recognize the object file contains Dwarf information.
1934 void EmitInitial() {
1935 // Check to see if we already emitted intial headers.
1936 if (didInitial) return;
1937 didInitial = true;
1938
1939 // Dwarf sections base addresses.
1940 if (TAI->getDwarfRequiresFrameSection()) {
1941 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1942 EmitLabel("section_frame", 0);
1943 }
1944 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
1945 EmitLabel("section_info", 0);
1946 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
1947 EmitLabel("section_abbrev", 0);
1948 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
1949 EmitLabel("section_aranges", 0);
1950 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
1951 EmitLabel("section_macinfo", 0);
1952 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
1953 EmitLabel("section_line", 0);
1954 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
1955 EmitLabel("section_loc", 0);
1956 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
1957 EmitLabel("section_pubnames", 0);
1958 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
1959 EmitLabel("section_str", 0);
1960 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
1961 EmitLabel("section_ranges", 0);
1962
1963 Asm->SwitchToTextSection(TAI->getTextSection());
1964 EmitLabel("text_begin", 0);
1965 Asm->SwitchToDataSection(TAI->getDataSection());
1966 EmitLabel("data_begin", 0);
1967
1968 // Emit common frame information.
1969 EmitInitialDebugFrame();
1970 }
1971
Jim Laskey65195462006-10-30 13:35:07 +00001972 /// EmitDIE - Recusively Emits a debug information entry.
1973 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001974 void EmitDIE(DIE *Die) const {
1975 // Get the abbreviation for this DIE.
1976 unsigned AbbrevNumber = Die->getAbbrevNumber();
1977 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1978
1979 O << "\n";
1980
1981 // Emit the code (index) for the abbreviation.
1982 EmitULEB128Bytes(AbbrevNumber);
1983 EOL(std::string("Abbrev [" +
1984 utostr(AbbrevNumber) +
1985 "] 0x" + utohexstr(Die->getOffset()) +
1986 ":0x" + utohexstr(Die->getSize()) + " " +
1987 TagString(Abbrev->getTag())));
1988
1989 const std::vector<DIEValue *> &Values = Die->getValues();
1990 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
1991
1992 // Emit the DIE attribute values.
1993 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1994 unsigned Attr = AbbrevData[i].getAttribute();
1995 unsigned Form = AbbrevData[i].getForm();
1996 assert(Form && "Too many attributes for DIE (check abbreviation)");
1997
1998 switch (Attr) {
1999 case DW_AT_sibling: {
2000 EmitInt32(Die->SiblingOffset());
2001 break;
2002 }
2003 default: {
2004 // Emit an attribute using the defined form.
2005 Values[i]->EmitValue(*this, Form);
2006 break;
2007 }
2008 }
2009
2010 EOL(AttributeString(Attr));
2011 }
2012
2013 // Emit the DIE children if any.
2014 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2015 const std::vector<DIE *> &Children = Die->getChildren();
2016
2017 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2018 EmitDIE(Children[j]);
2019 }
2020
2021 EmitInt8(0); EOL("End Of Children Mark");
2022 }
2023 }
2024
Jim Laskey65195462006-10-30 13:35:07 +00002025 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2026 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002027 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2028 // Get the children.
2029 const std::vector<DIE *> &Children = Die->getChildren();
2030
2031 // If not last sibling and has children then add sibling offset attribute.
2032 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2033
2034 // Record the abbreviation.
2035 AssignAbbrevNumber(Die->getAbbrev());
2036
2037 // Get the abbreviation for this DIE.
2038 unsigned AbbrevNumber = Die->getAbbrevNumber();
2039 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2040
2041 // Set DIE offset
2042 Die->setOffset(Offset);
2043
2044 // Start the size with the size of abbreviation code.
2045 Offset += SizeULEB128(AbbrevNumber);
2046
2047 const std::vector<DIEValue *> &Values = Die->getValues();
2048 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2049
2050 // Size the DIE attribute values.
2051 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2052 // Size attribute value.
2053 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2054 }
2055
2056 // Size the DIE children if any.
2057 if (!Children.empty()) {
2058 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2059 "Children flag not set");
2060
2061 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2062 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2063 }
2064
2065 // End of children marker.
2066 Offset += sizeof(int8_t);
2067 }
2068
2069 Die->setSize(Offset - Die->getOffset());
2070 return Offset;
2071 }
Jim Laskey65195462006-10-30 13:35:07 +00002072
2073 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2074 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002075 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002076 // Process base compile unit.
2077 CompileUnit *Unit = GetBaseCompileUnit();
2078 // Compute size of compile unit header
2079 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2080 sizeof(int16_t) + // DWARF version number
2081 sizeof(int32_t) + // Offset Into Abbrev. Section
2082 sizeof(int8_t); // Pointer Size (in bytes)
2083 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002084 }
2085
Jim Laskey65195462006-10-30 13:35:07 +00002086 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
2087 /// frame.
2088 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Jim Laskeyef42a012006-11-02 20:12:39 +00002089 std::vector<MachineMove *> &Moves) {
2090 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
2091 MachineMove *Move = Moves[i];
Jim Laskey9d4209f2006-11-07 19:33:46 +00002092 unsigned LabelID = DebugInfo->MappedLabel(Move->getLabelID());
Jim Laskeyef42a012006-11-02 20:12:39 +00002093
2094 // Throw out move if the label is invalid.
Jim Laskey9d4209f2006-11-07 19:33:46 +00002095 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002096
2097 const MachineLocation &Dst = Move->getDestination();
2098 const MachineLocation &Src = Move->getSource();
2099
2100 // Advance row if new location.
2101 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
2102 EmitInt8(DW_CFA_advance_loc4);
2103 EOL("DW_CFA_advance_loc4");
2104 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
2105 EOL("");
2106
2107 BaseLabelID = LabelID;
2108 BaseLabel = "loc";
2109 }
2110
2111 int stackGrowth =
2112 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2113 TargetFrameInfo::StackGrowsUp ?
2114 TAI->getAddressSize() : -TAI->getAddressSize();
2115
2116 // If advancing cfa.
2117 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
2118 if (!Src.isRegister()) {
2119 if (Src.getRegister() == MachineLocation::VirtualFP) {
2120 EmitInt8(DW_CFA_def_cfa_offset);
2121 EOL("DW_CFA_def_cfa_offset");
2122 } else {
2123 EmitInt8(DW_CFA_def_cfa);
2124 EOL("DW_CFA_def_cfa");
Jim Laskeyef42a012006-11-02 20:12:39 +00002125 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
2126 EOL("Register");
2127 }
2128
2129 int Offset = Src.getOffset() / stackGrowth;
2130
2131 EmitULEB128Bytes(Offset);
2132 EOL("Offset");
2133 } else {
2134 assert(0 && "Machine move no supported yet.");
2135 }
2136 } else {
2137 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
2138 int Offset = Dst.getOffset() / stackGrowth;
2139
2140 if (Offset < 0) {
2141 EmitInt8(DW_CFA_offset_extended_sf);
2142 EOL("DW_CFA_offset_extended_sf");
2143 EmitULEB128Bytes(Reg);
2144 EOL("Reg");
2145 EmitSLEB128Bytes(Offset);
2146 EOL("Offset");
2147 } else if (Reg < 64) {
2148 EmitInt8(DW_CFA_offset + Reg);
2149 EOL("DW_CFA_offset + Reg");
2150 EmitULEB128Bytes(Offset);
2151 EOL("Offset");
2152 } else {
2153 EmitInt8(DW_CFA_offset_extended);
2154 EOL("DW_CFA_offset_extended");
2155 EmitULEB128Bytes(Reg);
2156 EOL("Reg");
2157 EmitULEB128Bytes(Offset);
2158 EOL("Offset");
2159 }
2160 }
2161 }
2162 }
Jim Laskey65195462006-10-30 13:35:07 +00002163
2164 /// EmitDebugInfo - Emit the debug info section.
2165 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002166 void EmitDebugInfo() const {
2167 // Start debug info section.
2168 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2169
Jim Laskey5496f012006-11-09 14:52:14 +00002170 CompileUnit *Unit = GetBaseCompileUnit();
2171 DIE *Die = Unit->getDie();
2172 // Emit the compile units header.
2173 EmitLabel("info_begin", Unit->getID());
2174 // Emit size of content not including length itself
2175 unsigned ContentSize = Die->getSize() +
2176 sizeof(int16_t) + // DWARF version number
2177 sizeof(int32_t) + // Offset Into Abbrev. Section
2178 sizeof(int8_t); // Pointer Size (in bytes)
2179
2180 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2181 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2182 EmitDifference("abbrev_begin", 0, "section_abbrev", 0);
2183 EOL("Offset Into Abbrev. Section");
2184 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
2185
2186 EmitDIE(Die);
2187 EmitLabel("info_end", Unit->getID());
2188
2189 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002190 }
2191
Jim Laskey65195462006-10-30 13:35:07 +00002192 /// EmitAbbreviations - Emit the abbreviation section.
2193 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002194 void EmitAbbreviations() const {
2195 // Check to see if it is worth the effort.
2196 if (!Abbreviations.empty()) {
2197 // Start the debug abbrev section.
2198 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2199
2200 EmitLabel("abbrev_begin", 0);
2201
2202 // For each abbrevation.
2203 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2204 // Get abbreviation data
2205 const DIEAbbrev *Abbrev = Abbreviations[i];
2206
2207 // Emit the abbrevations code (base 1 index.)
2208 EmitULEB128Bytes(Abbrev->getNumber()); EOL("Abbreviation Code");
2209
2210 // Emit the abbreviations data.
2211 Abbrev->Emit(*this);
2212
2213 O << "\n";
2214 }
2215
2216 EmitLabel("abbrev_end", 0);
2217
2218 O << "\n";
2219 }
2220 }
2221
Jim Laskey65195462006-10-30 13:35:07 +00002222 /// EmitDebugLines - Emit source line information.
2223 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002224 void EmitDebugLines() const {
2225 // Minimum line delta, thus ranging from -10..(255-10).
2226 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2227 // Maximum line delta, thus ranging from -10..(255-10).
2228 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002229
Jim Laskeyef42a012006-11-02 20:12:39 +00002230 // Start the dwarf line section.
2231 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2232
2233 // Construct the section header.
2234
2235 EmitDifference("line_end", 0, "line_begin", 0);
2236 EOL("Length of Source Line Info");
2237 EmitLabel("line_begin", 0);
2238
2239 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2240
2241 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
2242 EOL("Prolog Length");
2243 EmitLabel("line_prolog_begin", 0);
2244
2245 EmitInt8(1); EOL("Minimum Instruction Length");
2246
2247 EmitInt8(1); EOL("Default is_stmt_start flag");
2248
2249 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
2250
2251 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
2252
2253 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
2254
2255 // Line number standard opcode encodings argument count
2256 EmitInt8(0); EOL("DW_LNS_copy arg count");
2257 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2258 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2259 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2260 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2261 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2262 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2263 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2264 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
2265
2266 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2267 const UniqueVector<SourceFileInfo>
2268 &SourceFiles = DebugInfo->getSourceFiles();
2269
2270 // Emit directories.
2271 for (unsigned DirectoryID = 1, NDID = Directories.size();
2272 DirectoryID <= NDID; ++DirectoryID) {
2273 EmitString(Directories[DirectoryID]); EOL("Directory");
2274 }
2275 EmitInt8(0); EOL("End of directories");
2276
2277 // Emit files.
2278 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2279 SourceID <= NSID; ++SourceID) {
2280 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2281 EmitString(SourceFile.getName()); EOL("Source");
2282 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2283 EmitULEB128Bytes(0); EOL("Mod date");
2284 EmitULEB128Bytes(0); EOL("File size");
2285 }
2286 EmitInt8(0); EOL("End of files");
2287
2288 EmitLabel("line_prolog_end", 0);
2289
2290 // A sequence for each text section.
2291 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2292 // Isolate current sections line info.
2293 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
2294
2295 if (DwarfVerbose) {
2296 O << "\t"
2297 << TAI->getCommentString() << " "
2298 << "Section "
2299 << SectionMap[j + 1].c_str() << "\n";
2300 }
2301
2302 // Dwarf assumes we start with first line of first source file.
2303 unsigned Source = 1;
2304 unsigned Line = 1;
2305
2306 // Construct rows of the address, source, line, column matrix.
2307 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2308 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey9d4209f2006-11-07 19:33:46 +00002309 unsigned LabelID = DebugInfo->MappedLabel(LineInfo.getLabelID());
2310 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002311
2312 if (DwarfVerbose) {
2313 unsigned SourceID = LineInfo.getSourceID();
2314 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2315 unsigned DirectoryID = SourceFile.getDirectoryID();
2316 O << "\t"
2317 << TAI->getCommentString() << " "
2318 << Directories[DirectoryID]
2319 << SourceFile.getName() << ":"
2320 << LineInfo.getLine() << "\n";
2321 }
2322
2323 // Define the line address.
2324 EmitInt8(0); EOL("Extended Op");
2325 EmitInt8(4 + 1); EOL("Op size");
2326 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2327 EmitReference("loc", LabelID); EOL("Location label");
2328
2329 // If change of source, then switch to the new source.
2330 if (Source != LineInfo.getSourceID()) {
2331 Source = LineInfo.getSourceID();
2332 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2333 EmitULEB128Bytes(Source); EOL("New Source");
2334 }
2335
2336 // If change of line.
2337 if (Line != LineInfo.getLine()) {
2338 // Determine offset.
2339 int Offset = LineInfo.getLine() - Line;
2340 int Delta = Offset - MinLineDelta;
2341
2342 // Update line.
2343 Line = LineInfo.getLine();
2344
2345 // If delta is small enough and in range...
2346 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2347 // ... then use fast opcode.
2348 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2349 } else {
2350 // ... otherwise use long hand.
2351 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2352 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2353 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2354 }
2355 } else {
2356 // Copy the previous row (different address or source)
2357 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2358 }
2359 }
2360
2361 // Define last address of section.
2362 EmitInt8(0); EOL("Extended Op");
2363 EmitInt8(4 + 1); EOL("Op size");
2364 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2365 EmitReference("section_end", j + 1); EOL("Section end label");
2366
2367 // Mark end of matrix.
2368 EmitInt8(0); EOL("DW_LNE_end_sequence");
2369 EmitULEB128Bytes(1); O << "\n";
2370 EmitInt8(1); O << "\n";
2371 }
2372
2373 EmitLabel("line_end", 0);
2374
2375 O << "\n";
2376 }
2377
Jim Laskey65195462006-10-30 13:35:07 +00002378 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
2379 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002380 void EmitInitialDebugFrame() {
2381 if (!TAI->getDwarfRequiresFrameSection())
2382 return;
2383
2384 int stackGrowth =
2385 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2386 TargetFrameInfo::StackGrowsUp ?
2387 TAI->getAddressSize() : -TAI->getAddressSize();
2388
2389 // Start the dwarf frame section.
2390 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2391
2392 EmitLabel("frame_common", 0);
2393 EmitDifference("frame_common_end", 0,
2394 "frame_common_begin", 0);
2395 EOL("Length of Common Information Entry");
2396
2397 EmitLabel("frame_common_begin", 0);
2398 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2399 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2400 EmitString(""); EOL("CIE Augmentation");
2401 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
2402 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
2403 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
Jim Laskey65195462006-10-30 13:35:07 +00002404
Jim Laskeyef42a012006-11-02 20:12:39 +00002405 std::vector<MachineMove *> Moves;
2406 RI->getInitialFrameState(Moves);
2407 EmitFrameMoves(NULL, 0, Moves);
2408 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2409
2410 EmitAlign(2);
2411 EmitLabel("frame_common_end", 0);
2412
2413 O << "\n";
2414 }
2415
Jim Laskey65195462006-10-30 13:35:07 +00002416 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2417 /// section.
Jim Laskeyef42a012006-11-02 20:12:39 +00002418 void EmitFunctionDebugFrame() {
Reid Spencer5a4951e2006-11-07 06:36:36 +00002419 if (!TAI->getDwarfRequiresFrameSection())
2420 return;
Jim Laskey9d4209f2006-11-07 19:33:46 +00002421
Jim Laskeyef42a012006-11-02 20:12:39 +00002422 // Start the dwarf frame section.
2423 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2424
2425 EmitDifference("frame_end", SubprogramCount,
2426 "frame_begin", SubprogramCount);
2427 EOL("Length of Frame Information Entry");
2428
2429 EmitLabel("frame_begin", SubprogramCount);
2430
2431 EmitDifference("frame_common", 0, "section_frame", 0);
2432 EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002433
Jim Laskeyef42a012006-11-02 20:12:39 +00002434 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2435 EmitDifference("func_end", SubprogramCount,
2436 "func_begin", SubprogramCount);
2437 EOL("FDE address range");
2438
2439 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2440
2441 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2442
2443 EmitAlign(2);
2444 EmitLabel("frame_end", SubprogramCount);
2445
2446 O << "\n";
2447 }
2448
2449 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002450 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002451 void EmitDebugPubNames() {
2452 // Start the dwarf pubnames section.
2453 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2454
Jim Laskey5496f012006-11-09 14:52:14 +00002455 CompileUnit *Unit = GetBaseCompileUnit();
2456
2457 EmitDifference("pubnames_end", Unit->getID(),
2458 "pubnames_begin", Unit->getID());
2459 EOL("Length of Public Names Info");
2460
2461 EmitLabel("pubnames_begin", Unit->getID());
2462
2463 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2464
2465 EmitDifference("info_begin", Unit->getID(), "section_info", 0);
2466 EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002467
Jim Laskey5496f012006-11-09 14:52:14 +00002468 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2469 EOL("Compilation Unit Length");
2470
2471 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2472
2473 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2474 GE = Globals.end();
2475 GI != GE; ++GI) {
2476 const std::string &Name = GI->first;
2477 DIE * Entity = GI->second;
Jim Laskeyef42a012006-11-02 20:12:39 +00002478
Jim Laskey5496f012006-11-09 14:52:14 +00002479 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2480 EmitString(Name); EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00002481 }
Jim Laskey5496f012006-11-09 14:52:14 +00002482
2483 EmitInt32(0); EOL("End Mark");
2484 EmitLabel("pubnames_end", Unit->getID());
2485
2486 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002487 }
2488
2489 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002490 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002491 void EmitDebugStr() {
2492 // Check to see if it is worth the effort.
2493 if (!StringPool.empty()) {
2494 // Start the dwarf str section.
2495 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2496
2497 // For each of strings in the string pool.
2498 for (unsigned StringID = 1, N = StringPool.size();
2499 StringID <= N; ++StringID) {
2500 // Emit a label for reference from debug information entries.
2501 EmitLabel("string", StringID);
2502 // Emit the string itself.
2503 const std::string &String = StringPool[StringID];
2504 EmitString(String); O << "\n";
2505 }
2506
2507 O << "\n";
2508 }
2509 }
2510
2511 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002512 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002513 void EmitDebugLoc() {
2514 // Start the dwarf loc section.
2515 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2516
2517 O << "\n";
2518 }
2519
2520 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002521 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002522 void EmitDebugARanges() {
2523 // Start the dwarf aranges section.
2524 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2525
2526 // FIXME - Mock up
2527 #if 0
Jim Laskey5496f012006-11-09 14:52:14 +00002528 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002529
Jim Laskey5496f012006-11-09 14:52:14 +00002530 // Don't include size of length
2531 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2532
2533 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2534
2535 EmitReference("info_begin", Unit->getID());
2536 EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002537
Jim Laskey5496f012006-11-09 14:52:14 +00002538 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002539
Jim Laskey5496f012006-11-09 14:52:14 +00002540 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002541
Jim Laskey5496f012006-11-09 14:52:14 +00002542 EmitInt16(0); EOL("Pad (1)");
2543 EmitInt16(0); EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002544
Jim Laskey5496f012006-11-09 14:52:14 +00002545 // Range 1
2546 EmitReference("text_begin", 0); EOL("Address");
2547 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002548
Jim Laskey5496f012006-11-09 14:52:14 +00002549 EmitInt32(0); EOL("EOM (1)");
2550 EmitInt32(0); EOL("EOM (2)");
2551
2552 O << "\n";
Jim Laskeyef42a012006-11-02 20:12:39 +00002553 #endif
2554 }
2555
2556 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002557 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002558 void EmitDebugRanges() {
2559 // Start the dwarf ranges section.
2560 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2561
2562 O << "\n";
2563 }
2564
2565 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002566 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002567 void EmitDebugMacInfo() {
2568 // Start the dwarf macinfo section.
2569 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2570
2571 O << "\n";
2572 }
2573
Jim Laskey65195462006-10-30 13:35:07 +00002574 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2575 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002576 void ConstructCompileUnitDIEs() {
2577 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
2578
2579 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00002580 unsigned ID = DebugInfo->RecordSource(CUW[i]);
2581 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002582 CompileUnits.push_back(Unit);
2583 }
2584 }
2585
Jim Laskey65195462006-10-30 13:35:07 +00002586 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2587 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002588 void ConstructGlobalDIEs() {
2589 std::vector<GlobalVariableDesc *> GlobalVariables =
2590 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
2591
2592 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2593 GlobalVariableDesc *GVD = GlobalVariables[i];
2594 NewGlobalVariable(GVD);
2595 }
2596 }
Jim Laskey65195462006-10-30 13:35:07 +00002597
2598 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2599 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002600 void ConstructSubprogramDIEs() {
2601 std::vector<SubprogramDesc *> Subprograms =
2602 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
2603
2604 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2605 SubprogramDesc *SPD = Subprograms[i];
2606 NewSubprogram(SPD);
2607 }
2608 }
Jim Laskey65195462006-10-30 13:35:07 +00002609
2610 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
2611 ///
2612 bool ShouldEmitDwarf() const { return shouldEmit; }
2613
2614public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002615 //===--------------------------------------------------------------------===//
2616 // Main entry points.
2617 //
2618 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
2619 : O(OS)
2620 , Asm(A)
2621 , TAI(T)
2622 , TD(Asm->TM.getTargetData())
2623 , RI(Asm->TM.getRegisterInfo())
2624 , M(NULL)
2625 , MF(NULL)
2626 , DebugInfo(NULL)
2627 , didInitial(false)
2628 , shouldEmit(false)
2629 , SubprogramCount(0)
2630 , CompileUnits()
2631 , AbbreviationsSet(InitAbbreviationsSetSize)
2632 , Abbreviations()
2633 , ValuesSet(InitValuesSetSize)
2634 , Values()
2635 , StringPool()
2636 , DescToUnitMap()
2637 , SectionMap()
2638 , SectionSourceLines()
2639 {
2640 }
2641 virtual ~Dwarf() {
2642 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2643 delete CompileUnits[i];
2644 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2645 delete Values[j];
2646 }
2647
Jim Laskey65195462006-10-30 13:35:07 +00002648 // Accessors.
2649 //
2650 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
2651
2652 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2653 /// created it. Set by the target AsmPrinter.
Jim Laskeyef42a012006-11-02 20:12:39 +00002654 void SetDebugInfo(MachineDebugInfo *DI) {
2655 // Make sure initial declarations are made.
2656 if (!DebugInfo && DI->hasInfo()) {
2657 DebugInfo = DI;
2658 shouldEmit = true;
2659
2660 // Emit initial sections
2661 EmitInitial();
2662
2663 // Create all the compile unit DIEs.
2664 ConstructCompileUnitDIEs();
2665
2666 // Create DIEs for each of the externally visible global variables.
2667 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002668
Jim Laskeyef42a012006-11-02 20:12:39 +00002669 // Create DIEs for each of the externally visible subprograms.
2670 ConstructSubprogramDIEs();
2671
2672 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00002673 SectionMap.insert(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002674 }
2675 }
2676
Jim Laskey65195462006-10-30 13:35:07 +00002677 /// BeginModule - Emit all Dwarf sections that should come prior to the
2678 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002679 void BeginModule(Module *M) {
2680 this->M = M;
2681
2682 if (!ShouldEmitDwarf()) return;
2683 EOL("Dwarf Begin Module");
2684 }
2685
Jim Laskey65195462006-10-30 13:35:07 +00002686 /// EndModule - Emit all Dwarf sections that should come after the content.
2687 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002688 void EndModule() {
2689 if (!ShouldEmitDwarf()) return;
2690 EOL("Dwarf End Module");
2691
2692 // Standard sections final addresses.
2693 Asm->SwitchToTextSection(TAI->getTextSection());
2694 EmitLabel("text_end", 0);
2695 Asm->SwitchToDataSection(TAI->getDataSection());
2696 EmitLabel("data_end", 0);
2697
2698 // End text sections.
2699 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2700 Asm->SwitchToTextSection(SectionMap[i].c_str());
2701 EmitLabel("section_end", i);
2702 }
2703
2704 // Compute DIE offsets and sizes.
2705 SizeAndOffsets();
2706
2707 // Emit all the DIEs into a debug info section
2708 EmitDebugInfo();
2709
2710 // Corresponding abbreviations into a abbrev section.
2711 EmitAbbreviations();
2712
2713 // Emit source line correspondence into a debug line section.
2714 EmitDebugLines();
2715
2716 // Emit info into a debug pubnames section.
2717 EmitDebugPubNames();
2718
2719 // Emit info into a debug str section.
2720 EmitDebugStr();
2721
2722 // Emit info into a debug loc section.
2723 EmitDebugLoc();
2724
2725 // Emit info into a debug aranges section.
2726 EmitDebugARanges();
2727
2728 // Emit info into a debug ranges section.
2729 EmitDebugRanges();
2730
2731 // Emit info into a debug macinfo section.
2732 EmitDebugMacInfo();
2733 }
2734
Jim Laskey65195462006-10-30 13:35:07 +00002735 /// BeginFunction - Gather pre-function debug information. Assumes being
2736 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002737 void BeginFunction(MachineFunction *MF) {
2738 this->MF = MF;
2739
2740 if (!ShouldEmitDwarf()) return;
2741 EOL("Dwarf Begin Function");
2742
2743 // Begin accumulating function debug information.
2744 DebugInfo->BeginFunction(MF);
2745
2746 // Assumes in correct section after the entry point.
2747 EmitLabel("func_begin", ++SubprogramCount);
2748 }
2749
Jim Laskey65195462006-10-30 13:35:07 +00002750 /// EndFunction - Gather and emit post-function debug information.
2751 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002752 void EndFunction() {
2753 if (!ShouldEmitDwarf()) return;
2754 EOL("Dwarf End Function");
2755
2756 // Define end label for subprogram.
2757 EmitLabel("func_end", SubprogramCount);
2758
2759 // Get function line info.
2760 const std::vector<SourceLineInfo> &LineInfos = DebugInfo->getSourceLines();
2761
2762 if (!LineInfos.empty()) {
2763 // Get section line info.
2764 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2765 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2766 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2767 // Append the function info to section info.
2768 SectionLineInfos.insert(SectionLineInfos.end(),
2769 LineInfos.begin(), LineInfos.end());
2770 }
2771
2772 // Construct scopes for subprogram.
2773 ConstructRootScope(DebugInfo->getRootScope());
2774
2775 // Emit function frame information.
2776 EmitFunctionDebugFrame();
2777
2778 // Reset the line numbers for the next function.
2779 DebugInfo->ClearLineInfo();
2780
2781 // Clear function debug information.
2782 DebugInfo->EndFunction();
2783 }
Jim Laskey65195462006-10-30 13:35:07 +00002784};
2785
Jim Laskey0d086af2006-02-27 12:43:29 +00002786} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00002787
2788//===----------------------------------------------------------------------===//
2789
Jim Laskeyd18e2892006-01-20 20:34:06 +00002790/// Emit - Print the abbreviation using the specified Dwarf writer.
2791///
Jim Laskey65195462006-10-30 13:35:07 +00002792void DIEAbbrev::Emit(const Dwarf &DW) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002793 // Emit its Dwarf tag type.
2794 DW.EmitULEB128Bytes(Tag);
2795 DW.EOL(TagString(Tag));
2796
2797 // Emit whether it has children DIEs.
2798 DW.EmitULEB128Bytes(ChildrenFlag);
2799 DW.EOL(ChildrenString(ChildrenFlag));
2800
2801 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00002802 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002803 const DIEAbbrevData &AttrData = Data[i];
2804
2805 // Emit attribute type.
2806 DW.EmitULEB128Bytes(AttrData.getAttribute());
2807 DW.EOL(AttributeString(AttrData.getAttribute()));
2808
2809 // Emit form type.
2810 DW.EmitULEB128Bytes(AttrData.getForm());
2811 DW.EOL(FormEncodingString(AttrData.getForm()));
2812 }
2813
2814 // Mark end of abbreviation.
2815 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
2816 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
2817}
2818
2819#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00002820void DIEAbbrev::print(std::ostream &O) {
2821 O << "Abbreviation @"
2822 << std::hex << (intptr_t)this << std::dec
2823 << " "
2824 << TagString(Tag)
2825 << " "
2826 << ChildrenString(ChildrenFlag)
2827 << "\n";
2828
2829 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
2830 O << " "
2831 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002832 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00002833 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002834 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002835 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00002836}
2837void DIEAbbrev::dump() { print(std::cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002838#endif
2839
2840//===----------------------------------------------------------------------===//
2841
Jim Laskeyef42a012006-11-02 20:12:39 +00002842#ifndef NDEBUG
2843void DIEValue::dump() {
2844 print(std::cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002845}
Jim Laskeyef42a012006-11-02 20:12:39 +00002846#endif
2847
2848//===----------------------------------------------------------------------===//
2849
Jim Laskey063e7652006-01-17 17:31:53 +00002850/// EmitValue - Emit integer of appropriate size.
2851///
Jim Laskey65195462006-10-30 13:35:07 +00002852void DIEInteger::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +00002853 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00002854 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00002855 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002856 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002857 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002858 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002859 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002860 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002861 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002862 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +00002863 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
2864 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +00002865 default: assert(0 && "DIE Value form not supported yet"); break;
2866 }
2867}
2868
Jim Laskey063e7652006-01-17 17:31:53 +00002869//===----------------------------------------------------------------------===//
2870
2871/// EmitValue - Emit string value.
2872///
Jim Laskey65195462006-10-30 13:35:07 +00002873void DIEString::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002874 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00002875}
2876
Jim Laskey063e7652006-01-17 17:31:53 +00002877//===----------------------------------------------------------------------===//
2878
2879/// EmitValue - Emit label value.
2880///
Jim Laskey65195462006-10-30 13:35:07 +00002881void DIEDwarfLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002882 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +00002883}
2884
2885/// SizeOf - Determine size of label value in bytes.
2886///
Jim Laskey65195462006-10-30 13:35:07 +00002887unsigned DIEDwarfLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002888 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002889}
Jim Laskeyef42a012006-11-02 20:12:39 +00002890
Jim Laskey063e7652006-01-17 17:31:53 +00002891//===----------------------------------------------------------------------===//
2892
Jim Laskeyd18e2892006-01-20 20:34:06 +00002893/// EmitValue - Emit label value.
2894///
Jim Laskey65195462006-10-30 13:35:07 +00002895void DIEObjectLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002896 DW.EmitReference(Label);
2897}
2898
2899/// SizeOf - Determine size of label value in bytes.
2900///
Jim Laskey65195462006-10-30 13:35:07 +00002901unsigned DIEObjectLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002902 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00002903}
2904
2905//===----------------------------------------------------------------------===//
2906
Jim Laskey063e7652006-01-17 17:31:53 +00002907/// EmitValue - Emit delta value.
2908///
Jim Laskey65195462006-10-30 13:35:07 +00002909void DIEDelta::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002910 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +00002911}
2912
2913/// SizeOf - Determine size of delta value in bytes.
2914///
Jim Laskey65195462006-10-30 13:35:07 +00002915unsigned DIEDelta::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002916 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002917}
2918
2919//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00002920
Jim Laskeyb8509c52006-03-23 18:07:55 +00002921/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002922///
Jim Laskey65195462006-10-30 13:35:07 +00002923void DIEntry::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +00002924 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00002925}
Jim Laskeyd18e2892006-01-20 20:34:06 +00002926
2927//===----------------------------------------------------------------------===//
2928
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002929/// ComputeSize - calculate the size of the block.
2930///
Jim Laskey65195462006-10-30 13:35:07 +00002931unsigned DIEBlock::ComputeSize(Dwarf &DW) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002932 if (!Size) {
2933 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2934
2935 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2936 Size += Values[i]->SizeOf(DW, AbbrevData[i].getForm());
2937 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002938 }
2939 return Size;
2940}
2941
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002942/// EmitValue - Emit block data.
2943///
Jim Laskey65195462006-10-30 13:35:07 +00002944void DIEBlock::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002945 switch (Form) {
2946 case DW_FORM_block1: DW.EmitInt8(Size); break;
2947 case DW_FORM_block2: DW.EmitInt16(Size); break;
2948 case DW_FORM_block4: DW.EmitInt32(Size); break;
2949 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
2950 default: assert(0 && "Improper form for block"); break;
2951 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002952
2953 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2954
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002955 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2956 DW.EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002957 Values[i]->EmitValue(DW, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002958 }
2959}
2960
2961/// SizeOf - Determine size of block data in bytes.
2962///
Jim Laskey65195462006-10-30 13:35:07 +00002963unsigned DIEBlock::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002964 switch (Form) {
2965 case DW_FORM_block1: return Size + sizeof(int8_t);
2966 case DW_FORM_block2: return Size + sizeof(int16_t);
2967 case DW_FORM_block4: return Size + sizeof(int32_t);
Jim Laskeyef42a012006-11-02 20:12:39 +00002968 case DW_FORM_block: return Size + SizeULEB128(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002969 default: assert(0 && "Improper form for block"); break;
2970 }
2971 return 0;
2972}
2973
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002974//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00002975/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00002976
2977DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002978 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00002979 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00002980}
Jim Laskeyef42a012006-11-02 20:12:39 +00002981
Jim Laskeyb8509c52006-03-23 18:07:55 +00002982/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
2983///
2984void DIE::AddSiblingOffset() {
2985 DIEInteger *DI = new DIEInteger(0);
2986 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002987 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002988}
2989
Jim Laskeyef42a012006-11-02 20:12:39 +00002990/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002991///
Jim Laskeyef42a012006-11-02 20:12:39 +00002992void DIE::Profile(FoldingSetNodeID &ID) {
2993 Abbrev.Profile(ID);
2994
2995 for (unsigned i = 0, N = Children.size(); i < N; ++i)
2996 ID.AddPointer(Children[i]);
2997
2998 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2999 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003000}
Jim Laskeyef42a012006-11-02 20:12:39 +00003001
3002#ifndef NDEBUG
3003void DIE::print(std::ostream &O, unsigned IncIndent) {
3004 static unsigned IndentCount = 0;
3005 IndentCount += IncIndent;
3006 const std::string Indent(IndentCount, ' ');
3007 bool isBlock = Abbrev.getTag() == 0;
3008
3009 if (!isBlock) {
3010 O << Indent
3011 << "Die: "
3012 << "0x" << std::hex << (intptr_t)this << std::dec
3013 << ", Offset: " << Offset
3014 << ", Size: " << Size
3015 << "\n";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003016
Jim Laskeyef42a012006-11-02 20:12:39 +00003017 O << Indent
3018 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00003019 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00003020 << ChildrenString(Abbrev.getChildrenFlag());
3021 } else {
3022 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00003023 }
3024 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003025
Jim Laskeyef42a012006-11-02 20:12:39 +00003026 const std::vector<DIEAbbrevData> &Data = Abbrev.getData();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003027
Jim Laskeyef42a012006-11-02 20:12:39 +00003028 IndentCount += 2;
3029 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3030 O << Indent;
3031 if (!isBlock) {
3032 O << AttributeString(Data[i].getAttribute());
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003033 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00003034 O << "Blk[" << i << "]";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003035 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003036 O << " "
3037 << FormEncodingString(Data[i].getForm())
3038 << " ";
3039 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00003040 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00003041 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003042 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00003043
Jim Laskeyef42a012006-11-02 20:12:39 +00003044 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3045 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00003046 }
Jim Laskey063e7652006-01-17 17:31:53 +00003047
Jim Laskeyef42a012006-11-02 20:12:39 +00003048 if (!isBlock) O << "\n";
3049 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00003050}
3051
Jim Laskeyef42a012006-11-02 20:12:39 +00003052void DIE::dump() {
3053 print(std::cerr);
Jim Laskey41886992006-04-07 16:34:46 +00003054}
Jim Laskeybd761842006-02-27 17:27:12 +00003055#endif
Jim Laskey65195462006-10-30 13:35:07 +00003056
3057//===----------------------------------------------------------------------===//
3058/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00003059///
Jim Laskey65195462006-10-30 13:35:07 +00003060
3061DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3062 const TargetAsmInfo *T) {
3063 DW = new Dwarf(OS, A, T);
3064}
3065
3066DwarfWriter::~DwarfWriter() {
3067 delete DW;
3068}
3069
3070/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
3071/// created it. Set by the target AsmPrinter.
3072void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
3073 DW->SetDebugInfo(DI);
3074}
3075
3076/// BeginModule - Emit all Dwarf sections that should come prior to the
3077/// content.
3078void DwarfWriter::BeginModule(Module *M) {
3079 DW->BeginModule(M);
3080}
3081
3082/// EndModule - Emit all Dwarf sections that should come after the content.
3083///
3084void DwarfWriter::EndModule() {
3085 DW->EndModule();
3086}
3087
3088/// BeginFunction - Gather pre-function debug information. Assumes being
3089/// emitted immediately after the function entry point.
3090void DwarfWriter::BeginFunction(MachineFunction *MF) {
3091 DW->BeginFunction(MF);
3092}
3093
3094/// EndFunction - Gather and emit post-function debug information.
3095///
3096void DwarfWriter::EndFunction() {
3097 DW->EndFunction();
3098}