blob: c8a1333d6c4dfc0c0da7919aff7e67551b09d1cf [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 ///
441 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000442 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000443 ID.AddInteger(Integer);
444 }
445
446#ifndef NDEBUG
447 virtual void print(std::ostream &O) {
448 O << "Int: " << (int64_t)Integer
449 << " 0x" << std::hex << Integer << std::dec;
450 }
451#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000452};
Jim Laskey063e7652006-01-17 17:31:53 +0000453
Jim Laskey0d086af2006-02-27 12:43:29 +0000454//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000455/// DIEString - A string value DIE.
456///
Jim Laskeyef42a012006-11-02 20:12:39 +0000457class DIEString : public DIEValue {
458public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000459 const std::string String;
460
461 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000462
Jim Laskey0d086af2006-02-27 12:43:29 +0000463 // Implement isa/cast/dyncast.
464 static bool classof(const DIEString *) { return true; }
465 static bool classof(const DIEValue *S) { return S->Type == isString; }
466
467 /// EmitValue - Emit string value.
468 ///
Jim Laskey65195462006-10-30 13:35:07 +0000469 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000470
471 /// SizeOf - Determine size of string value in bytes.
472 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000473 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
474 return String.size() + sizeof(char); // sizeof('\0');
475 }
476
477 /// Profile - Used to gather unique data for the value folding set.
478 ///
479 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000480 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000481 ID.AddString(String);
482 }
483
484#ifndef NDEBUG
485 virtual void print(std::ostream &O) {
486 O << "Str: \"" << String << "\"";
487 }
488#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000489};
Jim Laskey063e7652006-01-17 17:31:53 +0000490
Jim Laskey0d086af2006-02-27 12:43:29 +0000491//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000492/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000493//
Jim Laskeyef42a012006-11-02 20:12:39 +0000494class DIEDwarfLabel : public DIEValue {
495public:
496
Jim Laskey0d086af2006-02-27 12:43:29 +0000497 const DWLabel Label;
498
499 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000500
Jim Laskey0d086af2006-02-27 12:43:29 +0000501 // Implement isa/cast/dyncast.
502 static bool classof(const DIEDwarfLabel *) { return true; }
503 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
504
505 /// EmitValue - Emit label value.
506 ///
Jim Laskey65195462006-10-30 13:35:07 +0000507 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000508
509 /// SizeOf - Determine size of label value in bytes.
510 ///
Jim Laskey65195462006-10-30 13:35:07 +0000511 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000512
513 /// Profile - Used to gather unique data for the value folding set.
514 ///
515 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000516 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000517 Label.Profile(ID);
518 }
519
520#ifndef NDEBUG
521 virtual void print(std::ostream &O) {
522 O << "Lbl: ";
523 Label.print(O);
524 }
525#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000526};
Jim Laskey063e7652006-01-17 17:31:53 +0000527
Jim Laskey063e7652006-01-17 17:31:53 +0000528
Jim Laskey0d086af2006-02-27 12:43:29 +0000529//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000530/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000531//
Jim Laskeyef42a012006-11-02 20:12:39 +0000532class DIEObjectLabel : public DIEValue {
533public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000534 const std::string Label;
535
536 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000537
Jim Laskey0d086af2006-02-27 12:43:29 +0000538 // Implement isa/cast/dyncast.
539 static bool classof(const DIEObjectLabel *) { return true; }
540 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
541
542 /// EmitValue - Emit label value.
543 ///
Jim Laskey65195462006-10-30 13:35:07 +0000544 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000545
546 /// SizeOf - Determine size of label value in bytes.
547 ///
Jim Laskey65195462006-10-30 13:35:07 +0000548 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000549
550 /// Profile - Used to gather unique data for the value folding set.
551 ///
552 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000553 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000554 ID.AddString(Label);
555 }
556
557#ifndef NDEBUG
558 virtual void print(std::ostream &O) {
559 O << "Obj: " << Label;
560 }
561#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000562};
Jim Laskey063e7652006-01-17 17:31:53 +0000563
Jim Laskey0d086af2006-02-27 12:43:29 +0000564//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000565/// DIEDelta - A simple label difference DIE.
566///
Jim Laskeyef42a012006-11-02 20:12:39 +0000567class DIEDelta : public DIEValue {
568public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000569 const DWLabel LabelHi;
570 const DWLabel LabelLo;
571
572 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
573 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000574
Jim Laskey0d086af2006-02-27 12:43:29 +0000575 // Implement isa/cast/dyncast.
576 static bool classof(const DIEDelta *) { return true; }
577 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
578
579 /// EmitValue - Emit delta value.
580 ///
Jim Laskey65195462006-10-30 13:35:07 +0000581 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000582
583 /// SizeOf - Determine size of delta value in bytes.
584 ///
Jim Laskey65195462006-10-30 13:35:07 +0000585 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000586
587 /// Profile - Used to gather unique data for the value folding set.
588 ///
589 virtual void Profile(FoldingSetNodeID &ID){
Jim Laskeyf6733882006-11-02 21:48:18 +0000590 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000591 LabelHi.Profile(ID);
592 LabelLo.Profile(ID);
593 }
594
595#ifndef NDEBUG
596 virtual void print(std::ostream &O) {
597 O << "Del: ";
598 LabelHi.print(O);
599 O << "-";
600 LabelLo.print(O);
601 }
602#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000603};
Jim Laskey063e7652006-01-17 17:31:53 +0000604
Jim Laskey0d086af2006-02-27 12:43:29 +0000605//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000606/// DIEntry - A pointer to another debug information entry. An instance of this
607/// class can also be used as a proxy for a debug information entry not yet
608/// defined (ie. types.)
609class DIEntry : public DIEValue {
610public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000611 DIE *Entry;
612
613 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000614
Jim Laskey0d086af2006-02-27 12:43:29 +0000615 // Implement isa/cast/dyncast.
616 static bool classof(const DIEntry *) { return true; }
617 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
618
Jim Laskeyb8509c52006-03-23 18:07:55 +0000619 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000620 ///
Jim Laskey65195462006-10-30 13:35:07 +0000621 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000622
Jim Laskeyb8509c52006-03-23 18:07:55 +0000623 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000624 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000625 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
626 return sizeof(int32_t);
627 }
628
629 /// Profile - Used to gather unique data for the value folding set.
630 ///
631 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000632 ID.AddInteger(isEntry);
633
Jim Laskeyef42a012006-11-02 20:12:39 +0000634 if (Entry) {
635 ID.AddPointer(Entry);
636 } else {
637 ID.AddPointer(this);
638 }
639 }
640
641#ifndef NDEBUG
642 virtual void print(std::ostream &O) {
643 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
644 }
645#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000646};
647
648//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000649/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000650//
Jim Laskeyef42a012006-11-02 20:12:39 +0000651class DIEBlock : public DIEValue, public DIE {
652public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000653 unsigned Size; // Size in bytes excluding size header.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000654
655 DIEBlock()
656 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000657 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000658 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000659 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000660 ~DIEBlock() {
661 }
662
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000663 // Implement isa/cast/dyncast.
664 static bool classof(const DIEBlock *) { return true; }
665 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
666
667 /// ComputeSize - calculate the size of the block.
668 ///
Jim Laskey65195462006-10-30 13:35:07 +0000669 unsigned ComputeSize(Dwarf &DW);
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000670
671 /// BestForm - Choose the best form for data.
672 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000673 unsigned BestForm() const {
674 if ((unsigned char)Size == Size) return DW_FORM_block1;
675 if ((unsigned short)Size == Size) return DW_FORM_block2;
676 if ((unsigned int)Size == Size) return DW_FORM_block4;
677 return DW_FORM_block;
678 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000679
680 /// EmitValue - Emit block data.
681 ///
Jim Laskey65195462006-10-30 13:35:07 +0000682 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000683
684 /// SizeOf - Determine size of block data in bytes.
685 ///
Jim Laskey65195462006-10-30 13:35:07 +0000686 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000687
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000688
Jim Laskeyef42a012006-11-02 20:12:39 +0000689 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000690 ///
Reid Spencer97821312006-11-02 23:56:21 +0000691 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000692 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000693 DIE::Profile(ID);
694 }
695
696#ifndef NDEBUG
697 virtual void print(std::ostream &O) {
698 O << "Blk: ";
699 DIE::print(O, 5);
700 }
701#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000702};
703
704//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000705/// CompileUnit - This dwarf writer support class manages information associate
706/// with a source file.
707class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000708private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000709 /// Desc - Compile unit debug descriptor.
710 ///
711 CompileUnitDesc *Desc;
712
713 /// ID - File identifier for source.
714 ///
715 unsigned ID;
716
717 /// Die - Compile unit debug information entry.
718 ///
719 DIE *Die;
720
721 /// DescToDieMap - Tracks the mapping of unit level debug informaton
722 /// descriptors to debug information entries.
723 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
724
725 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
726 /// descriptors to debug information entries using a DIEntry proxy.
727 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
728
729 /// Globals - A map of globally visible named entities for this unit.
730 ///
731 std::map<std::string, DIE *> Globals;
732
733 /// DiesSet - Used to uniquely define dies within the compile unit.
734 ///
735 FoldingSet<DIE> DiesSet;
736
737 /// Dies - List of all dies in the compile unit.
738 ///
739 std::vector<DIE *> Dies;
Jim Laskey0d086af2006-02-27 12:43:29 +0000740
741public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000742 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
743 : Desc(CUD)
744 , ID(I)
745 , Die(D)
746 , DescToDieMap()
747 , DescToDIEntryMap()
748 , Globals()
749 , DiesSet(InitDiesSetSize)
750 , Dies()
751 {}
752
753 ~CompileUnit() {
754 delete Die;
755
756 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
757 delete Dies[i];
758 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000759
Jim Laskeybd761842006-02-27 17:27:12 +0000760 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000761 CompileUnitDesc *getDesc() const { return Desc; }
762 unsigned getID() const { return ID; }
763 DIE* getDie() const { return Die; }
764 std::map<std::string, DIE *> &getGlobals() { return Globals; }
765
766 /// hasContent - Return true if this compile unit has something to write out.
767 ///
768 bool hasContent() const {
769 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000770 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000771
772 /// AddGlobal - Add a new global entity to the compile unit.
773 ///
774 void AddGlobal(const std::string &Name, DIE *Die) {
775 Globals[Name] = Die;
776 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000777
Jim Laskeyef42a012006-11-02 20:12:39 +0000778 /// getDieMapSlotFor - Returns the debug information entry map slot for the
779 /// specified debug descriptor.
780 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
781 return DescToDieMap[DD];
782 }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000783
Jim Laskeyef42a012006-11-02 20:12:39 +0000784 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
785 /// specified debug descriptor.
786 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DD) {
787 return DescToDIEntryMap[DD];
788 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000789
Jim Laskeyef42a012006-11-02 20:12:39 +0000790 /// AddDie - Adds or interns the DIE to the compile unit.
791 ///
792 DIE *AddDie(DIE &Buffer) {
793 FoldingSetNodeID ID;
794 Buffer.Profile(ID);
795 void *Where;
796 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
797
798 if (!Die) {
799 Die = new DIE(Buffer);
800 DiesSet.InsertNode(Die, Where);
801 this->Die->AddChild(Die);
802 Buffer.Detach();
803 }
804
805 return Die;
806 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000807};
808
Jim Laskey65195462006-10-30 13:35:07 +0000809//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000810/// Dwarf - Emits Dwarf debug and exception handling directives.
811///
Jim Laskey65195462006-10-30 13:35:07 +0000812class Dwarf {
813
814private:
815
816 //===--------------------------------------------------------------------===//
817 // Core attributes used by the Dwarf writer.
818 //
819
820 //
821 /// O - Stream to .s file.
822 ///
823 std::ostream &O;
824
825 /// Asm - Target of Dwarf emission.
826 ///
827 AsmPrinter *Asm;
828
829 /// TAI - Target Asm Printer.
830 const TargetAsmInfo *TAI;
831
832 /// TD - Target data.
833 const TargetData *TD;
834
835 /// RI - Register Information.
836 const MRegisterInfo *RI;
837
838 /// M - Current module.
839 ///
840 Module *M;
841
842 /// MF - Current machine function.
843 ///
844 MachineFunction *MF;
845
846 /// DebugInfo - Collected debug information.
847 ///
848 MachineDebugInfo *DebugInfo;
849
850 /// didInitial - Flag to indicate if initial emission has been done.
851 ///
852 bool didInitial;
853
854 /// shouldEmit - Flag to indicate if debug information should be emitted.
855 ///
856 bool shouldEmit;
857
858 /// SubprogramCount - The running count of functions being compiled.
859 ///
860 unsigned SubprogramCount;
861
862 //===--------------------------------------------------------------------===//
863 // Attributes used to construct specific Dwarf sections.
864 //
865
866 /// CompileUnits - All the compile units involved in this build. The index
867 /// of each entry in this vector corresponds to the sources in DebugInfo.
868 std::vector<CompileUnit *> CompileUnits;
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000869
Jim Laskeyef42a012006-11-02 20:12:39 +0000870 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +0000871 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000872 FoldingSet<DIEAbbrev> AbbreviationsSet;
873
874 /// Abbreviations - A list of all the unique abbreviations in use.
875 ///
876 std::vector<DIEAbbrev *> Abbreviations;
Jim Laskey65195462006-10-30 13:35:07 +0000877
Jim Laskeyef42a012006-11-02 20:12:39 +0000878 /// ValuesSet - Used to uniquely define values.
879 ///
880 FoldingSet<DIEValue> ValuesSet;
881
882 /// Values - A list of all the unique values in use.
883 ///
884 std::vector<DIEValue *> Values;
885
Jim Laskey65195462006-10-30 13:35:07 +0000886 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +0000887 ///
Jim Laskey65195462006-10-30 13:35:07 +0000888 UniqueVector<std::string> StringPool;
889
890 /// UnitMap - Map debug information descriptor to compile unit.
891 ///
892 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
893
Jim Laskey65195462006-10-30 13:35:07 +0000894 /// SectionMap - Provides a unique id per text section.
895 ///
896 UniqueVector<std::string> SectionMap;
897
898 /// SectionSourceLines - Tracks line numbers per text section.
899 ///
900 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
901
902
903public:
904
905 //===--------------------------------------------------------------------===//
906 // Emission and print routines
907 //
908
909 /// PrintHex - Print a value as a hexidecimal value.
910 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000911 void PrintHex(int Value) const {
912 O << "0x" << std::hex << Value << std::dec;
913 }
Jim Laskey65195462006-10-30 13:35:07 +0000914
915 /// EOL - Print a newline character to asm stream. If a comment is present
916 /// then it will be printed first. Comments should not contain '\n'.
Jim Laskeyef42a012006-11-02 20:12:39 +0000917 void EOL(const std::string &Comment) const {
918 if (DwarfVerbose && !Comment.empty()) {
919 O << "\t"
920 << TAI->getCommentString()
921 << " "
922 << Comment;
923 }
924 O << "\n";
925 }
Jim Laskey65195462006-10-30 13:35:07 +0000926
927 /// EmitAlign - Print a align directive.
928 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000929 void EmitAlign(unsigned Alignment) const {
930 O << TAI->getAlignDirective() << Alignment << "\n";
931 }
Jim Laskey65195462006-10-30 13:35:07 +0000932
933 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
934 /// unsigned leb128 value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000935 void EmitULEB128Bytes(unsigned Value) const {
936 if (TAI->hasLEB128()) {
937 O << "\t.uleb128\t"
938 << Value;
939 } else {
940 O << TAI->getData8bitsDirective();
941 PrintULEB128(O, Value);
942 }
943 }
Jim Laskey65195462006-10-30 13:35:07 +0000944
945 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
946 /// signed leb128 value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000947 void EmitSLEB128Bytes(int Value) const {
948 if (TAI->hasLEB128()) {
949 O << "\t.sleb128\t"
950 << Value;
951 } else {
952 O << TAI->getData8bitsDirective();
953 PrintSLEB128(O, Value);
954 }
955 }
Jim Laskey65195462006-10-30 13:35:07 +0000956
957 /// EmitInt8 - Emit a byte directive and value.
958 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000959 void EmitInt8(int Value) const {
960 O << TAI->getData8bitsDirective();
961 PrintHex(Value & 0xFF);
962 }
Jim Laskey65195462006-10-30 13:35:07 +0000963
964 /// EmitInt16 - Emit a short directive and value.
965 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000966 void EmitInt16(int Value) const {
967 O << TAI->getData16bitsDirective();
968 PrintHex(Value & 0xFFFF);
969 }
Jim Laskey65195462006-10-30 13:35:07 +0000970
971 /// EmitInt32 - Emit a long directive and value.
972 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000973 void EmitInt32(int Value) const {
974 O << TAI->getData32bitsDirective();
975 PrintHex(Value);
976 }
977
Jim Laskey65195462006-10-30 13:35:07 +0000978 /// EmitInt64 - Emit a long long directive and value.
979 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000980 void EmitInt64(uint64_t Value) const {
981 if (TAI->getData64bitsDirective()) {
982 O << TAI->getData64bitsDirective();
983 PrintHex(Value);
984 } else {
985 if (TD->isBigEndian()) {
986 EmitInt32(unsigned(Value >> 32)); O << "\n";
987 EmitInt32(unsigned(Value));
988 } else {
989 EmitInt32(unsigned(Value)); O << "\n";
990 EmitInt32(unsigned(Value >> 32));
991 }
992 }
993 }
994
Jim Laskey65195462006-10-30 13:35:07 +0000995 /// EmitString - Emit a string with quotes and a null terminator.
Jim Laskeyef42a012006-11-02 20:12:39 +0000996 /// Special characters are emitted properly.
Jim Laskey65195462006-10-30 13:35:07 +0000997 /// \literal (Eg. '\t') \endliteral
Jim Laskeyef42a012006-11-02 20:12:39 +0000998 void EmitString(const std::string &String) const {
999 O << TAI->getAsciiDirective()
1000 << "\"";
1001 for (unsigned i = 0, N = String.size(); i < N; ++i) {
1002 unsigned char C = String[i];
1003
1004 if (!isascii(C) || iscntrl(C)) {
1005 switch(C) {
1006 case '\b': O << "\\b"; break;
1007 case '\f': O << "\\f"; break;
1008 case '\n': O << "\\n"; break;
1009 case '\r': O << "\\r"; break;
1010 case '\t': O << "\\t"; break;
1011 default:
1012 O << '\\';
1013 O << char('0' + ((C >> 6) & 7));
1014 O << char('0' + ((C >> 3) & 7));
1015 O << char('0' + ((C >> 0) & 7));
1016 break;
1017 }
1018 } else if (C == '\"') {
1019 O << "\\\"";
1020 } else if (C == '\'') {
1021 O << "\\\'";
1022 } else {
1023 O << C;
1024 }
1025 }
1026 O << "\\0\"";
1027 }
Jim Laskey65195462006-10-30 13:35:07 +00001028
1029 /// PrintLabelName - Print label name in form used by Dwarf writer.
1030 ///
1031 void PrintLabelName(DWLabel Label) const {
1032 PrintLabelName(Label.Tag, Label.Number);
1033 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001034 void PrintLabelName(const char *Tag, unsigned Number) const {
1035 O << TAI->getPrivateGlobalPrefix()
1036 << "debug_"
1037 << Tag;
1038 if (Number) O << Number;
1039 }
Jim Laskey65195462006-10-30 13:35:07 +00001040
1041 /// EmitLabel - Emit location label for internal use by Dwarf.
1042 ///
1043 void EmitLabel(DWLabel Label) const {
1044 EmitLabel(Label.Tag, Label.Number);
1045 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001046 void EmitLabel(const char *Tag, unsigned Number) const {
1047 PrintLabelName(Tag, Number);
1048 O << ":\n";
1049 }
Jim Laskey65195462006-10-30 13:35:07 +00001050
1051 /// EmitReference - Emit a reference to a label.
1052 ///
1053 void EmitReference(DWLabel Label) const {
1054 EmitReference(Label.Tag, Label.Number);
1055 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001056 void EmitReference(const char *Tag, unsigned Number) const {
1057 if (TAI->getAddressSize() == 4)
1058 O << TAI->getData32bitsDirective();
1059 else
1060 O << TAI->getData64bitsDirective();
1061
1062 PrintLabelName(Tag, Number);
1063 }
1064 void EmitReference(const std::string &Name) const {
1065 if (TAI->getAddressSize() == 4)
1066 O << TAI->getData32bitsDirective();
1067 else
1068 O << TAI->getData64bitsDirective();
1069
1070 O << Name;
1071 }
Jim Laskey65195462006-10-30 13:35:07 +00001072
1073 /// EmitDifference - Emit the difference between two labels. Some
1074 /// assemblers do not behave with absolute expressions with data directives,
1075 /// so there is an option (needsSet) to use an intermediary set expression.
1076 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo) const {
1077 EmitDifference(LabelHi.Tag, LabelHi.Number, LabelLo.Tag, LabelLo.Number);
1078 }
1079 void EmitDifference(const char *TagHi, unsigned NumberHi,
Jim Laskeyef42a012006-11-02 20:12:39 +00001080 const char *TagLo, unsigned NumberLo) const {
1081 if (TAI->needsSet()) {
1082 static unsigned SetCounter = 0;
1083
1084 O << "\t.set\t";
1085 PrintLabelName("set", SetCounter);
1086 O << ",";
1087 PrintLabelName(TagHi, NumberHi);
1088 O << "-";
1089 PrintLabelName(TagLo, NumberLo);
1090 O << "\n";
1091
1092 if (TAI->getAddressSize() == sizeof(int32_t))
1093 O << TAI->getData32bitsDirective();
1094 else
1095 O << TAI->getData64bitsDirective();
1096
1097 PrintLabelName("set", SetCounter);
1098
1099 ++SetCounter;
1100 } else {
1101 if (TAI->getAddressSize() == sizeof(int32_t))
1102 O << TAI->getData32bitsDirective();
1103 else
1104 O << TAI->getData64bitsDirective();
1105
1106 PrintLabelName(TagHi, NumberHi);
1107 O << "-";
1108 PrintLabelName(TagLo, NumberLo);
1109 }
1110 }
Jim Laskey65195462006-10-30 13:35:07 +00001111
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001112 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Jim Laskey65195462006-10-30 13:35:07 +00001113 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001114 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1115 // Profile the node so that we can make it unique.
1116 FoldingSetNodeID ID;
1117 Abbrev.Profile(ID);
1118
1119 // Check the set for priors.
1120 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
1121
1122 // If it's newly added.
1123 if (InSet == &Abbrev) {
1124 // Add to abbreviation list.
1125 Abbreviations.push_back(&Abbrev);
1126 // Assign the vector position + 1 as its number.
1127 Abbrev.setNumber(Abbreviations.size());
1128 } else {
1129 // Assign existing abbreviation number.
1130 Abbrev.setNumber(InSet->getNumber());
1131 }
1132 }
1133
Jim Laskey65195462006-10-30 13:35:07 +00001134 /// NewString - Add a string to the constant pool and returns a label.
1135 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001136 DWLabel NewString(const std::string &String) {
1137 unsigned StringID = StringPool.insert(String);
1138 return DWLabel("string", StringID);
1139 }
Jim Laskey65195462006-10-30 13:35:07 +00001140
Jim Laskeyef42a012006-11-02 20:12:39 +00001141 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1142 /// entry.
1143 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1144 DIEntry *Value;
1145
1146 if (Entry) {
1147 FoldingSetNodeID ID;
1148 ID.AddPointer(Entry);
1149 void *Where;
1150 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
1151
Jim Laskeyf6733882006-11-02 21:48:18 +00001152 if (Value) return Value;
Jim Laskeyef42a012006-11-02 20:12:39 +00001153
1154 Value = new DIEntry(Entry);
1155 ValuesSet.InsertNode(Value, Where);
1156 } else {
1157 Value = new DIEntry(Entry);
1158 }
1159
1160 Values.push_back(Value);
1161 return Value;
1162 }
1163
1164 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1165 ///
1166 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1167 Value->Entry = Entry;
1168 // Add to values set if not already there. If it is, we merely have a
1169 // duplicate in the values list (no harm.)
1170 ValuesSet.GetOrInsertNode(Value);
1171 }
1172
1173 /// AddUInt - Add an unsigned integer attribute data and value.
1174 ///
1175 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1176 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1177
1178 FoldingSetNodeID ID;
1179 ID.AddInteger(Integer);
1180 void *Where;
1181 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1182 if (!Value) {
1183 Value = new DIEInteger(Integer);
1184 ValuesSet.InsertNode(Value, Where);
1185 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001186 }
1187
1188 Die->AddValue(Attribute, Form, Value);
1189 }
1190
1191 /// AddSInt - Add an signed integer attribute data and value.
1192 ///
1193 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1194 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1195
1196 FoldingSetNodeID ID;
1197 ID.AddInteger((uint64_t)Integer);
1198 void *Where;
1199 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1200 if (!Value) {
1201 Value = new DIEInteger(Integer);
1202 ValuesSet.InsertNode(Value, Where);
1203 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001204 }
1205
1206 Die->AddValue(Attribute, Form, Value);
1207 }
1208
1209 /// AddString - Add a std::string attribute data and value.
1210 ///
1211 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1212 const std::string &String) {
1213 FoldingSetNodeID ID;
1214 ID.AddString(String);
1215 void *Where;
1216 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1217 if (!Value) {
1218 Value = new DIEString(String);
1219 ValuesSet.InsertNode(Value, Where);
1220 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001221 }
1222
1223 Die->AddValue(Attribute, Form, Value);
1224 }
1225
1226 /// AddLabel - Add a Dwarf label attribute data and value.
1227 ///
1228 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1229 const DWLabel &Label) {
1230 FoldingSetNodeID ID;
1231 Label.Profile(ID);
1232 void *Where;
1233 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1234 if (!Value) {
1235 Value = new DIEDwarfLabel(Label);
1236 ValuesSet.InsertNode(Value, Where);
1237 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001238 }
1239
1240 Die->AddValue(Attribute, Form, Value);
1241 }
1242
1243 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1244 ///
1245 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1246 const std::string &Label) {
1247 FoldingSetNodeID ID;
1248 ID.AddString(Label);
1249 void *Where;
1250 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1251 if (!Value) {
1252 Value = new DIEObjectLabel(Label);
1253 ValuesSet.InsertNode(Value, Where);
1254 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001255 }
1256
1257 Die->AddValue(Attribute, Form, Value);
1258 }
1259
1260 /// AddDelta - Add a label delta attribute data and value.
1261 ///
1262 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1263 const DWLabel &Hi, const DWLabel &Lo) {
1264 FoldingSetNodeID ID;
1265 Hi.Profile(ID);
1266 Lo.Profile(ID);
1267 void *Where;
1268 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1269 if (!Value) {
1270 Value = new DIEDelta(Hi, Lo);
1271 ValuesSet.InsertNode(Value, Where);
1272 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001273 }
1274
1275 Die->AddValue(Attribute, Form, Value);
1276 }
1277
1278 /// AddDIEntry - Add a DIE attribute data and value.
1279 ///
1280 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1281 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1282 }
1283
1284 /// AddBlock - Add block data.
1285 ///
1286 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1287 Block->ComputeSize(*this);
1288 FoldingSetNodeID ID;
1289 Block->Profile(ID);
1290 void *Where;
1291 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1292 if (!Value) {
1293 Value = Block;
1294 ValuesSet.InsertNode(Value, Where);
1295 Values.push_back(Value);
1296 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001297 delete Block;
1298 }
1299
1300 Die->AddValue(Attribute, Block->BestForm(), Value);
1301 }
1302
Jim Laskey65195462006-10-30 13:35:07 +00001303private:
1304
1305 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001306 /// entry.
1307 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1308 if (File && Line) {
1309 CompileUnit *FileUnit = FindCompileUnit(File);
1310 unsigned FileID = FileUnit->getID();
1311 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1312 AddUInt(Die, DW_AT_decl_line, 0, Line);
1313 }
1314 }
Jim Laskey65195462006-10-30 13:35:07 +00001315
1316 /// AddAddress - Add an address attribute to a die based on the location
1317 /// provided.
1318 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001319 const MachineLocation &Location) {
1320 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1321 DIEBlock *Block = new DIEBlock();
1322
1323 if (Location.isRegister()) {
1324 if (Reg < 32) {
1325 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1326 } else {
1327 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1328 AddUInt(Block, 0, DW_FORM_udata, Reg);
1329 }
1330 } else {
1331 if (Reg < 32) {
1332 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1333 } else {
1334 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1335 AddUInt(Block, 0, DW_FORM_udata, Reg);
1336 }
1337 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1338 }
1339
1340 AddBlock(Die, Attribute, 0, Block);
1341 }
1342
1343 /// AddBasicType - Add a new basic type attribute to the specified entity.
1344 ///
1345 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1346 const std::string &Name,
1347 unsigned Encoding, unsigned Size) {
1348 DIE *Die = ConstructBasicType(Unit, Name, Encoding, Size);
1349 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1350 }
1351
1352 /// ConstructBasicType - Construct a new basic type.
1353 ///
1354 DIE *ConstructBasicType(CompileUnit *Unit,
1355 const std::string &Name,
1356 unsigned Encoding, unsigned Size) {
1357 DIE Buffer(DW_TAG_base_type);
1358 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1359 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1360 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1361 return Unit->AddDie(Buffer);
1362 }
1363
1364 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1365 ///
1366 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1367 DIE *Die = ConstructPointerType(Unit, Name);
1368 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1369 }
1370
1371 /// ConstructPointerType - Construct a new pointer type.
1372 ///
1373 DIE *ConstructPointerType(CompileUnit *Unit, const std::string &Name) {
1374 DIE Buffer(DW_TAG_pointer_type);
1375 AddUInt(&Buffer, DW_AT_byte_size, 0, TAI->getAddressSize());
1376 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1377 return Unit->AddDie(Buffer);
1378 }
1379
1380 /// AddType - Add a new type attribute to the specified entity.
1381 ///
1382 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1383 if (!TyDesc) {
1384 AddBasicType(Entity, Unit, "", DW_ATE_signed, 4);
1385 } else {
1386 // Check for pre-existence.
1387 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
1388
1389 // If it exists then use the existing value.
1390 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001391 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1392 return;
1393 }
1394
1395 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1396 // FIXME - Not sure why programs and variables are coming through here.
1397 // Short cut for handling subprogram types (not really a TyDesc.)
1398 AddPointerType(Entity, Unit, SubprogramTy->getName());
1399 } else if (GlobalVariableDesc *GlobalTy =
1400 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1401 // FIXME - Not sure why programs and variables are coming through here.
1402 // Short cut for handling global variable types (not really a TyDesc.)
1403 AddPointerType(Entity, Unit, GlobalTy->getName());
1404 } else {
1405 // Set up proxy.
1406 Slot = NewDIEntry();
1407
1408 // Construct type.
1409 DIE Buffer(DW_TAG_base_type);
1410 ConstructType(Buffer, TyDesc, Unit);
1411
1412 // Add debug information entry to entity and unit.
1413 DIE *Die = Unit->AddDie(Buffer);
1414 SetDIEntry(Slot, Die);
1415 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1416 }
1417 }
1418 }
1419
1420 /// ConstructType - Adds all the required attributes to the type.
1421 ///
1422 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1423 // Get core information.
1424 const std::string &Name = TyDesc->getName();
1425 uint64_t Size = TyDesc->getSize() >> 3;
1426
1427 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1428 // Fundamental types like int, float, bool
1429 Buffer.setTag(DW_TAG_base_type);
1430 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1431 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1432 // Pointers, tyepdefs et al.
1433 Buffer.setTag(DerivedTy->getTag());
1434 // Map to main type, void will not have a type.
1435 if (TypeDesc *FromTy = DerivedTy->getFromType())
1436 AddType(&Buffer, FromTy, Unit);
1437 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1438 // Fetch tag.
1439 unsigned Tag = CompTy->getTag();
1440
1441 // Set tag accordingly.
1442 if (Tag == DW_TAG_vector_type)
1443 Buffer.setTag(DW_TAG_array_type);
1444 else
1445 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00001446
Jim Laskeyef42a012006-11-02 20:12:39 +00001447 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1448
1449 switch (Tag) {
1450 case DW_TAG_vector_type:
1451 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1452 // Fall thru
1453 case DW_TAG_array_type: {
1454 // Add element type.
1455 if (TypeDesc *FromTy = CompTy->getFromType())
1456 AddType(&Buffer, FromTy, Unit);
1457
1458 // Don't emit size attribute.
1459 Size = 0;
1460
1461 // Construct an anonymous type for index type.
1462 DIE *IndexTy = ConstructBasicType(Unit, "", DW_ATE_signed, 4);
1463
1464 // Add subranges to array type.
1465 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1466 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1467 int64_t Lo = SRD->getLo();
1468 int64_t Hi = SRD->getHi();
1469 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1470
1471 // If a range is available.
1472 if (Lo != Hi) {
1473 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1474 // Only add low if non-zero.
1475 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1476 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1477 }
1478
1479 Buffer.AddChild(Subrange);
1480 }
1481 break;
1482 }
1483 case DW_TAG_structure_type:
1484 case DW_TAG_union_type: {
1485 // Add elements to structure type.
1486 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1487 DebugInfoDesc *Element = Elements[i];
1488
1489 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1490 // Add field or base class.
1491
1492 unsigned Tag = MemberDesc->getTag();
1493
1494 // Extract the basic information.
1495 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001496 uint64_t Size = MemberDesc->getSize();
1497 uint64_t Align = MemberDesc->getAlign();
1498 uint64_t Offset = MemberDesc->getOffset();
1499
1500 // Construct member debug information entry.
1501 DIE *Member = new DIE(Tag);
1502
1503 // Add name if not "".
1504 if (!Name.empty())
1505 AddString(Member, DW_AT_name, DW_FORM_string, Name);
1506 // Add location if available.
1507 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1508
1509 // Most of the time the field info is the same as the members.
1510 uint64_t FieldSize = Size;
1511 uint64_t FieldAlign = Align;
1512 uint64_t FieldOffset = Offset;
1513
1514 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1515 AddType(Member, FromTy, Unit);
1516 FieldSize = FromTy->getSize();
1517 FieldAlign = FromTy->getSize();
1518 }
1519
1520 // Unless we have a bit field.
1521 if (Tag == DW_TAG_member && FieldSize != Size) {
1522 // Construct the alignment mask.
1523 uint64_t AlignMask = ~(FieldAlign - 1);
1524 // Determine the high bit + 1 of the declared size.
1525 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1526 // Work backwards to determine the base offset of the field.
1527 FieldOffset = HiMark - FieldSize;
1528 // Now normalize offset to the field.
1529 Offset -= FieldOffset;
1530
1531 // Maybe we need to work from the other end.
1532 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1533
1534 // Add size and offset.
1535 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1536 AddUInt(Member, DW_AT_bit_size, 0, Size);
1537 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1538 }
1539
1540 // Add computation for offset.
1541 DIEBlock *Block = new DIEBlock();
1542 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1543 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1544 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1545
1546 // Add accessibility (public default unless is base class.
1547 if (MemberDesc->isProtected()) {
1548 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1549 } else if (MemberDesc->isPrivate()) {
1550 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1551 } else if (Tag == DW_TAG_inheritance) {
1552 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1553 }
1554
1555 Buffer.AddChild(Member);
1556 } else if (GlobalVariableDesc *StaticDesc =
1557 dyn_cast<GlobalVariableDesc>(Element)) {
1558 // Add static member.
1559
1560 // Construct member debug information entry.
1561 DIE *Static = new DIE(DW_TAG_variable);
1562
1563 // Add name and mangled name.
1564 const std::string &Name = StaticDesc->getDisplayName();
1565 const std::string &MangledName = StaticDesc->getName();
1566 AddString(Static, DW_AT_name, DW_FORM_string, Name);
1567 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1568 MangledName);
1569
1570 // Add location.
1571 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1572
1573 // Add type.
1574 if (TypeDesc *StaticTy = StaticDesc->getType())
1575 AddType(Static, StaticTy, Unit);
1576
1577 // Add flags.
1578 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
1579 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
1580
1581 Buffer.AddChild(Static);
1582 } else if (SubprogramDesc *MethodDesc =
1583 dyn_cast<SubprogramDesc>(Element)) {
1584 // Add member function.
1585
1586 // Construct member debug information entry.
1587 DIE *Method = new DIE(DW_TAG_subprogram);
1588
1589 // Add name and mangled name.
1590 const std::string &Name = MethodDesc->getDisplayName();
1591 const std::string &MangledName = MethodDesc->getName();
1592 bool IsCTor = false;
1593
1594 if (Name.empty()) {
1595 AddString(Method, DW_AT_name, DW_FORM_string, MangledName);
1596 IsCTor = TyDesc->getName() == MangledName;
1597 } else {
1598 AddString(Method, DW_AT_name, DW_FORM_string, Name);
1599 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
1600 MangledName);
1601 }
1602
1603 // Add location.
1604 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1605
1606 // Add type.
1607 if (CompositeTypeDesc *MethodTy =
1608 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1609 // Get argument information.
1610 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1611
1612 // If not a ctor.
1613 if (!IsCTor) {
1614 // Add return type.
1615 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1616 }
1617
1618 // Add arguments.
1619 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1620 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1621 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1622 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1623 Method->AddChild(Arg);
1624 }
1625 }
1626
1627 // Add flags.
1628 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
1629 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
1630
1631 Buffer.AddChild(Method);
1632 }
1633 }
1634 break;
1635 }
1636 case DW_TAG_enumeration_type: {
1637 // Add enumerators to enumeration type.
1638 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1639 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1640 const std::string &Name = ED->getName();
1641 int64_t Value = ED->getValue();
1642 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1643 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1644 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1645 Buffer.AddChild(Enumerator);
1646 }
1647
1648 break;
1649 }
1650 case DW_TAG_subroutine_type: {
1651 // Add prototype flag.
1652 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1653 // Add return type.
1654 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
1655
1656 // Add arguments.
1657 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1658 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1659 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1660 Buffer.AddChild(Arg);
1661 }
1662
1663 break;
1664 }
1665 default: break;
1666 }
1667 }
1668
1669 // Add size if non-zero (derived types don't have a size.)
1670 if (Size) AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1671 // Add name if not anonymous or intermediate type.
1672 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1673 // Add source line info if available.
1674 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
1675 }
1676
1677 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00001678 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001679 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1680 // Construct debug information entry.
1681 DIE *Die = new DIE(DW_TAG_compile_unit);
1682 AddDelta(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0),
1683 DWLabel("section_line", 0));
1684 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1685 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1686 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1687 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1688
1689 // Construct compile unit.
1690 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1691
1692 // Add Unit to compile unit map.
1693 DescToUnitMap[UnitDesc] = Unit;
1694
1695 return Unit;
1696 }
1697
Jim Laskey9d4209f2006-11-07 19:33:46 +00001698 /// GetBaseCompileUnit - Get the main compile unit.
1699 ///
1700 CompileUnit *GetBaseCompileUnit() const {
1701 CompileUnit *Unit = CompileUnits[0];
1702 assert(Unit && "Missing compile unit.");
1703 return Unit;
1704 }
1705
Jim Laskey65195462006-10-30 13:35:07 +00001706 /// FindCompileUnit - Get the compile unit for the given descriptor.
1707 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001708 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001709 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00001710 assert(Unit && "Missing compile unit.");
1711 return Unit;
1712 }
1713
1714 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00001715 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001716 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1717 // Get the compile unit context.
1718 CompileUnitDesc *UnitDesc =
1719 static_cast<CompileUnitDesc *>(GVD->getContext());
1720 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1721
1722 // Check for pre-existence.
1723 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1724 if (Slot) return Slot;
1725
1726 // Get the global variable itself.
1727 GlobalVariable *GV = GVD->getGlobalVariable();
1728
1729 const std::string &Name = GVD->hasMangledName() ? GVD->getDisplayName()
1730 : GVD->getName();
1731 const std::string &MangledName = GVD->hasMangledName() ? GVD->getName()
1732 : "";
1733 // Create the global's variable DIE.
1734 DIE *VariableDie = new DIE(DW_TAG_variable);
1735 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
1736 if (!MangledName.empty()) {
1737 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1738 MangledName);
1739 }
1740 AddType(VariableDie, GVD->getType(), Unit);
1741 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1742
1743 // Add source line info if available.
1744 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1745
1746 // Work up linkage name.
1747 const std::string LinkageName = Asm->getGlobalLinkName(GV);
1748
1749 // Add address.
1750 DIEBlock *Block = new DIEBlock();
1751 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
1752 AddObjectLabel(Block, 0, DW_FORM_udata, LinkageName);
1753 AddBlock(VariableDie, DW_AT_location, 0, Block);
1754
1755 // Add to map.
1756 Slot = VariableDie;
1757
1758 // Add to context owner.
1759 Unit->getDie()->AddChild(VariableDie);
1760
1761 // Expose as global.
1762 // FIXME - need to check external flag.
1763 Unit->AddGlobal(Name, VariableDie);
1764
1765 return VariableDie;
1766 }
Jim Laskey65195462006-10-30 13:35:07 +00001767
1768 /// NewSubprogram - Add a new subprogram DIE.
1769 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001770 DIE *NewSubprogram(SubprogramDesc *SPD) {
1771 // Get the compile unit context.
1772 CompileUnitDesc *UnitDesc =
1773 static_cast<CompileUnitDesc *>(SPD->getContext());
1774 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1775
1776 // Check for pre-existence.
1777 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1778 if (Slot) return Slot;
1779
1780 // Gather the details (simplify add attribute code.)
1781 const std::string &Name = SPD->hasMangledName() ? SPD->getDisplayName()
1782 : SPD->getName();
1783 const std::string &MangledName = SPD->hasMangledName() ? SPD->getName()
1784 : "";
1785 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
1786
1787 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1788 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
1789 if (!MangledName.empty()) {
1790 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1791 MangledName);
1792 }
1793 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
1794 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, IsExternal);
1795 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
1796
1797 // Add source line info if available.
1798 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1799
1800 // Add to map.
1801 Slot = SubprogramDie;
1802
1803 // Add to context owner.
1804 Unit->getDie()->AddChild(SubprogramDie);
1805
1806 // Expose as global.
1807 Unit->AddGlobal(Name, SubprogramDie);
1808
1809 return SubprogramDie;
1810 }
Jim Laskey65195462006-10-30 13:35:07 +00001811
1812 /// NewScopeVariable - Create a new scope variable.
1813 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001814 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1815 // Get the descriptor.
1816 VariableDesc *VD = DV->getDesc();
1817
1818 // Translate tag to proper Dwarf tag. The result variable is dropped for
1819 // now.
1820 unsigned Tag;
1821 switch (VD->getTag()) {
1822 case DW_TAG_return_variable: return NULL;
1823 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1824 case DW_TAG_auto_variable: // fall thru
1825 default: Tag = DW_TAG_variable; break;
1826 }
1827
1828 // Define variable debug information entry.
1829 DIE *VariableDie = new DIE(Tag);
1830 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1831
1832 // Add source line info if available.
1833 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1834
1835 // Add variable type.
1836 AddType(VariableDie, VD->getType(), Unit);
1837
1838 // Add variable address.
1839 MachineLocation Location;
1840 RI->getLocation(*MF, DV->getFrameIndex(), Location);
1841 AddAddress(VariableDie, DW_AT_location, Location);
1842
1843 return VariableDie;
1844 }
Jim Laskey65195462006-10-30 13:35:07 +00001845
1846 /// ConstructScope - Construct the components of a scope.
1847 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001848 void ConstructScope(DebugScope *ParentScope,
1849 DIE *ParentDie, CompileUnit *Unit) {
1850 // Add variables to scope.
1851 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1852 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1853 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1854 if (VariableDie) ParentDie->AddChild(VariableDie);
1855 }
1856
1857 // Add nested scopes.
1858 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1859 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1860 // Define the Scope debug information entry.
1861 DebugScope *Scope = Scopes[j];
1862 // FIXME - Ignore inlined functions for the time being.
1863 if (!Scope->getParent()) continue;
1864
Jim Laskey9d4209f2006-11-07 19:33:46 +00001865 unsigned StartID = DebugInfo->MappedLabel(Scope->getStartLabelID());
1866 unsigned EndID = DebugInfo->MappedLabel(Scope->getEndLabelID());
Jim Laskeyef42a012006-11-02 20:12:39 +00001867
Jim Laskey9d4209f2006-11-07 19:33:46 +00001868 // Ignore empty scopes.
1869 if (StartID == EndID && StartID != 0) continue;
1870 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00001871
1872 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1873
1874 // Add the scope bounds.
1875 if (StartID) {
1876 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1877 DWLabel("loc", StartID));
1878 } else {
1879 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1880 DWLabel("func_begin", SubprogramCount));
1881 }
1882 if (EndID) {
1883 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1884 DWLabel("loc", EndID));
1885 } else {
1886 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1887 DWLabel("func_end", SubprogramCount));
1888 }
1889
1890 // Add the scope contents.
1891 ConstructScope(Scope, ScopeDie, Unit);
1892 ParentDie->AddChild(ScopeDie);
1893 }
1894 }
Jim Laskey65195462006-10-30 13:35:07 +00001895
1896 /// ConstructRootScope - Construct the scope for the subprogram.
1897 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001898 void ConstructRootScope(DebugScope *RootScope) {
1899 // Exit if there is no root scope.
1900 if (!RootScope) return;
1901
1902 // Get the subprogram debug information entry.
1903 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1904
1905 // Get the compile unit context.
1906 CompileUnitDesc *UnitDesc =
1907 static_cast<CompileUnitDesc *>(SPD->getContext());
1908 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1909
1910 // Get the subprogram die.
1911 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
1912 assert(SPDie && "Missing subprogram descriptor");
1913
1914 // Add the function bounds.
1915 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1916 DWLabel("func_begin", SubprogramCount));
1917 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1918 DWLabel("func_end", SubprogramCount));
1919 MachineLocation Location(RI->getFrameRegister(*MF));
1920 AddAddress(SPDie, DW_AT_frame_base, Location);
1921
1922 ConstructScope(RootScope, SPDie, Unit);
1923 }
Jim Laskey65195462006-10-30 13:35:07 +00001924
Jim Laskeyef42a012006-11-02 20:12:39 +00001925 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1926 /// tools to recognize the object file contains Dwarf information.
1927 void EmitInitial() {
1928 // Check to see if we already emitted intial headers.
1929 if (didInitial) return;
1930 didInitial = true;
1931
1932 // Dwarf sections base addresses.
1933 if (TAI->getDwarfRequiresFrameSection()) {
1934 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1935 EmitLabel("section_frame", 0);
1936 }
1937 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
1938 EmitLabel("section_info", 0);
1939 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
1940 EmitLabel("section_abbrev", 0);
1941 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
1942 EmitLabel("section_aranges", 0);
1943 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
1944 EmitLabel("section_macinfo", 0);
1945 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
1946 EmitLabel("section_line", 0);
1947 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
1948 EmitLabel("section_loc", 0);
1949 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
1950 EmitLabel("section_pubnames", 0);
1951 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
1952 EmitLabel("section_str", 0);
1953 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
1954 EmitLabel("section_ranges", 0);
1955
1956 Asm->SwitchToTextSection(TAI->getTextSection());
1957 EmitLabel("text_begin", 0);
1958 Asm->SwitchToDataSection(TAI->getDataSection());
1959 EmitLabel("data_begin", 0);
1960
1961 // Emit common frame information.
1962 EmitInitialDebugFrame();
1963 }
1964
Jim Laskey65195462006-10-30 13:35:07 +00001965 /// EmitDIE - Recusively Emits a debug information entry.
1966 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001967 void EmitDIE(DIE *Die) const {
1968 // Get the abbreviation for this DIE.
1969 unsigned AbbrevNumber = Die->getAbbrevNumber();
1970 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1971
1972 O << "\n";
1973
1974 // Emit the code (index) for the abbreviation.
1975 EmitULEB128Bytes(AbbrevNumber);
1976 EOL(std::string("Abbrev [" +
1977 utostr(AbbrevNumber) +
1978 "] 0x" + utohexstr(Die->getOffset()) +
1979 ":0x" + utohexstr(Die->getSize()) + " " +
1980 TagString(Abbrev->getTag())));
1981
1982 const std::vector<DIEValue *> &Values = Die->getValues();
1983 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
1984
1985 // Emit the DIE attribute values.
1986 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1987 unsigned Attr = AbbrevData[i].getAttribute();
1988 unsigned Form = AbbrevData[i].getForm();
1989 assert(Form && "Too many attributes for DIE (check abbreviation)");
1990
1991 switch (Attr) {
1992 case DW_AT_sibling: {
1993 EmitInt32(Die->SiblingOffset());
1994 break;
1995 }
1996 default: {
1997 // Emit an attribute using the defined form.
1998 Values[i]->EmitValue(*this, Form);
1999 break;
2000 }
2001 }
2002
2003 EOL(AttributeString(Attr));
2004 }
2005
2006 // Emit the DIE children if any.
2007 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2008 const std::vector<DIE *> &Children = Die->getChildren();
2009
2010 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2011 EmitDIE(Children[j]);
2012 }
2013
2014 EmitInt8(0); EOL("End Of Children Mark");
2015 }
2016 }
2017
Jim Laskey65195462006-10-30 13:35:07 +00002018 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2019 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002020 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2021 // Get the children.
2022 const std::vector<DIE *> &Children = Die->getChildren();
2023
2024 // If not last sibling and has children then add sibling offset attribute.
2025 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2026
2027 // Record the abbreviation.
2028 AssignAbbrevNumber(Die->getAbbrev());
2029
2030 // Get the abbreviation for this DIE.
2031 unsigned AbbrevNumber = Die->getAbbrevNumber();
2032 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2033
2034 // Set DIE offset
2035 Die->setOffset(Offset);
2036
2037 // Start the size with the size of abbreviation code.
2038 Offset += SizeULEB128(AbbrevNumber);
2039
2040 const std::vector<DIEValue *> &Values = Die->getValues();
2041 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2042
2043 // Size the DIE attribute values.
2044 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2045 // Size attribute value.
2046 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2047 }
2048
2049 // Size the DIE children if any.
2050 if (!Children.empty()) {
2051 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2052 "Children flag not set");
2053
2054 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2055 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2056 }
2057
2058 // End of children marker.
2059 Offset += sizeof(int8_t);
2060 }
2061
2062 Die->setSize(Offset - Die->getOffset());
2063 return Offset;
2064 }
Jim Laskey65195462006-10-30 13:35:07 +00002065
2066 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2067 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002068 void SizeAndOffsets() {
2069 // Process each compile unit.
2070 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2071 CompileUnit *Unit = CompileUnits[i];
2072 if (Unit->hasContent()) {
2073 // Compute size of compile unit header
2074 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2075 sizeof(int16_t) + // DWARF version number
2076 sizeof(int32_t) + // Offset Into Abbrev. Section
2077 sizeof(int8_t); // Pointer Size (in bytes)
2078 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
2079 }
2080 }
2081 }
2082
Jim Laskey65195462006-10-30 13:35:07 +00002083 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
2084 /// frame.
2085 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Jim Laskeyef42a012006-11-02 20:12:39 +00002086 std::vector<MachineMove *> &Moves) {
2087 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
2088 MachineMove *Move = Moves[i];
Jim Laskey9d4209f2006-11-07 19:33:46 +00002089 unsigned LabelID = DebugInfo->MappedLabel(Move->getLabelID());
Jim Laskeyef42a012006-11-02 20:12:39 +00002090
2091 // Throw out move if the label is invalid.
Jim Laskey9d4209f2006-11-07 19:33:46 +00002092 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002093
2094 const MachineLocation &Dst = Move->getDestination();
2095 const MachineLocation &Src = Move->getSource();
2096
2097 // Advance row if new location.
2098 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
2099 EmitInt8(DW_CFA_advance_loc4);
2100 EOL("DW_CFA_advance_loc4");
2101 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
2102 EOL("");
2103
2104 BaseLabelID = LabelID;
2105 BaseLabel = "loc";
2106 }
2107
2108 int stackGrowth =
2109 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2110 TargetFrameInfo::StackGrowsUp ?
2111 TAI->getAddressSize() : -TAI->getAddressSize();
2112
2113 // If advancing cfa.
2114 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
2115 if (!Src.isRegister()) {
2116 if (Src.getRegister() == MachineLocation::VirtualFP) {
2117 EmitInt8(DW_CFA_def_cfa_offset);
2118 EOL("DW_CFA_def_cfa_offset");
2119 } else {
2120 EmitInt8(DW_CFA_def_cfa);
2121 EOL("DW_CFA_def_cfa");
Jim Laskeyef42a012006-11-02 20:12:39 +00002122 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
2123 EOL("Register");
2124 }
2125
2126 int Offset = Src.getOffset() / stackGrowth;
2127
2128 EmitULEB128Bytes(Offset);
2129 EOL("Offset");
2130 } else {
2131 assert(0 && "Machine move no supported yet.");
2132 }
2133 } else {
2134 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
2135 int Offset = Dst.getOffset() / stackGrowth;
2136
2137 if (Offset < 0) {
2138 EmitInt8(DW_CFA_offset_extended_sf);
2139 EOL("DW_CFA_offset_extended_sf");
2140 EmitULEB128Bytes(Reg);
2141 EOL("Reg");
2142 EmitSLEB128Bytes(Offset);
2143 EOL("Offset");
2144 } else if (Reg < 64) {
2145 EmitInt8(DW_CFA_offset + Reg);
2146 EOL("DW_CFA_offset + Reg");
2147 EmitULEB128Bytes(Offset);
2148 EOL("Offset");
2149 } else {
2150 EmitInt8(DW_CFA_offset_extended);
2151 EOL("DW_CFA_offset_extended");
2152 EmitULEB128Bytes(Reg);
2153 EOL("Reg");
2154 EmitULEB128Bytes(Offset);
2155 EOL("Offset");
2156 }
2157 }
2158 }
2159 }
Jim Laskey65195462006-10-30 13:35:07 +00002160
2161 /// EmitDebugInfo - Emit the debug info section.
2162 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002163 void EmitDebugInfo() const {
2164 // Start debug info section.
2165 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2166
2167 // Process each compile unit.
2168 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2169 CompileUnit *Unit = CompileUnits[i];
2170
2171 if (Unit->hasContent()) {
2172 DIE *Die = Unit->getDie();
2173 // Emit the compile units header.
2174 EmitLabel("info_begin", Unit->getID());
2175 // Emit size of content not including length itself
2176 unsigned ContentSize = Die->getSize() +
2177 sizeof(int16_t) + // DWARF version number
2178 sizeof(int32_t) + // Offset Into Abbrev. Section
2179 sizeof(int8_t); // Pointer Size (in bytes)
2180
2181 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2182 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2183 EmitDifference("abbrev_begin", 0, "section_abbrev", 0);
2184 EOL("Offset Into Abbrev. Section");
2185 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
2186
2187 EmitDIE(Die);
2188 EmitLabel("info_end", Unit->getID());
2189 }
2190
2191 O << "\n";
2192 }
2193 }
2194
Jim Laskey65195462006-10-30 13:35:07 +00002195 /// EmitAbbreviations - Emit the abbreviation section.
2196 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002197 void EmitAbbreviations() const {
2198 // Check to see if it is worth the effort.
2199 if (!Abbreviations.empty()) {
2200 // Start the debug abbrev section.
2201 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2202
2203 EmitLabel("abbrev_begin", 0);
2204
2205 // For each abbrevation.
2206 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2207 // Get abbreviation data
2208 const DIEAbbrev *Abbrev = Abbreviations[i];
2209
2210 // Emit the abbrevations code (base 1 index.)
2211 EmitULEB128Bytes(Abbrev->getNumber()); EOL("Abbreviation Code");
2212
2213 // Emit the abbreviations data.
2214 Abbrev->Emit(*this);
2215
2216 O << "\n";
2217 }
2218
2219 EmitLabel("abbrev_end", 0);
2220
2221 O << "\n";
2222 }
2223 }
2224
Jim Laskey65195462006-10-30 13:35:07 +00002225 /// EmitDebugLines - Emit source line information.
2226 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002227 void EmitDebugLines() const {
2228 // Minimum line delta, thus ranging from -10..(255-10).
2229 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2230 // Maximum line delta, thus ranging from -10..(255-10).
2231 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002232
Jim Laskeyef42a012006-11-02 20:12:39 +00002233 // Start the dwarf line section.
2234 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2235
2236 // Construct the section header.
2237
2238 EmitDifference("line_end", 0, "line_begin", 0);
2239 EOL("Length of Source Line Info");
2240 EmitLabel("line_begin", 0);
2241
2242 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2243
2244 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
2245 EOL("Prolog Length");
2246 EmitLabel("line_prolog_begin", 0);
2247
2248 EmitInt8(1); EOL("Minimum Instruction Length");
2249
2250 EmitInt8(1); EOL("Default is_stmt_start flag");
2251
2252 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
2253
2254 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
2255
2256 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
2257
2258 // Line number standard opcode encodings argument count
2259 EmitInt8(0); EOL("DW_LNS_copy arg count");
2260 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2261 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2262 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2263 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2264 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2265 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2266 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2267 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
2268
2269 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2270 const UniqueVector<SourceFileInfo>
2271 &SourceFiles = DebugInfo->getSourceFiles();
2272
2273 // Emit directories.
2274 for (unsigned DirectoryID = 1, NDID = Directories.size();
2275 DirectoryID <= NDID; ++DirectoryID) {
2276 EmitString(Directories[DirectoryID]); EOL("Directory");
2277 }
2278 EmitInt8(0); EOL("End of directories");
2279
2280 // Emit files.
2281 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2282 SourceID <= NSID; ++SourceID) {
2283 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2284 EmitString(SourceFile.getName()); EOL("Source");
2285 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2286 EmitULEB128Bytes(0); EOL("Mod date");
2287 EmitULEB128Bytes(0); EOL("File size");
2288 }
2289 EmitInt8(0); EOL("End of files");
2290
2291 EmitLabel("line_prolog_end", 0);
2292
2293 // A sequence for each text section.
2294 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2295 // Isolate current sections line info.
2296 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
2297
2298 if (DwarfVerbose) {
2299 O << "\t"
2300 << TAI->getCommentString() << " "
2301 << "Section "
2302 << SectionMap[j + 1].c_str() << "\n";
2303 }
2304
2305 // Dwarf assumes we start with first line of first source file.
2306 unsigned Source = 1;
2307 unsigned Line = 1;
2308
2309 // Construct rows of the address, source, line, column matrix.
2310 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2311 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey9d4209f2006-11-07 19:33:46 +00002312 unsigned LabelID = DebugInfo->MappedLabel(LineInfo.getLabelID());
2313 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002314
2315 if (DwarfVerbose) {
2316 unsigned SourceID = LineInfo.getSourceID();
2317 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2318 unsigned DirectoryID = SourceFile.getDirectoryID();
2319 O << "\t"
2320 << TAI->getCommentString() << " "
2321 << Directories[DirectoryID]
2322 << SourceFile.getName() << ":"
2323 << LineInfo.getLine() << "\n";
2324 }
2325
2326 // Define the line address.
2327 EmitInt8(0); EOL("Extended Op");
2328 EmitInt8(4 + 1); EOL("Op size");
2329 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2330 EmitReference("loc", LabelID); EOL("Location label");
2331
2332 // If change of source, then switch to the new source.
2333 if (Source != LineInfo.getSourceID()) {
2334 Source = LineInfo.getSourceID();
2335 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2336 EmitULEB128Bytes(Source); EOL("New Source");
2337 }
2338
2339 // If change of line.
2340 if (Line != LineInfo.getLine()) {
2341 // Determine offset.
2342 int Offset = LineInfo.getLine() - Line;
2343 int Delta = Offset - MinLineDelta;
2344
2345 // Update line.
2346 Line = LineInfo.getLine();
2347
2348 // If delta is small enough and in range...
2349 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2350 // ... then use fast opcode.
2351 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2352 } else {
2353 // ... otherwise use long hand.
2354 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2355 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2356 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2357 }
2358 } else {
2359 // Copy the previous row (different address or source)
2360 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2361 }
2362 }
2363
2364 // Define last address of section.
2365 EmitInt8(0); EOL("Extended Op");
2366 EmitInt8(4 + 1); EOL("Op size");
2367 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2368 EmitReference("section_end", j + 1); EOL("Section end label");
2369
2370 // Mark end of matrix.
2371 EmitInt8(0); EOL("DW_LNE_end_sequence");
2372 EmitULEB128Bytes(1); O << "\n";
2373 EmitInt8(1); O << "\n";
2374 }
2375
2376 EmitLabel("line_end", 0);
2377
2378 O << "\n";
2379 }
2380
Jim Laskey65195462006-10-30 13:35:07 +00002381 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
2382 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002383 void EmitInitialDebugFrame() {
2384 if (!TAI->getDwarfRequiresFrameSection())
2385 return;
2386
2387 int stackGrowth =
2388 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2389 TargetFrameInfo::StackGrowsUp ?
2390 TAI->getAddressSize() : -TAI->getAddressSize();
2391
2392 // Start the dwarf frame section.
2393 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2394
2395 EmitLabel("frame_common", 0);
2396 EmitDifference("frame_common_end", 0,
2397 "frame_common_begin", 0);
2398 EOL("Length of Common Information Entry");
2399
2400 EmitLabel("frame_common_begin", 0);
2401 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2402 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2403 EmitString(""); EOL("CIE Augmentation");
2404 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
2405 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
2406 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
Jim Laskey65195462006-10-30 13:35:07 +00002407
Jim Laskeyef42a012006-11-02 20:12:39 +00002408 std::vector<MachineMove *> Moves;
2409 RI->getInitialFrameState(Moves);
2410 EmitFrameMoves(NULL, 0, Moves);
2411 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2412
2413 EmitAlign(2);
2414 EmitLabel("frame_common_end", 0);
2415
2416 O << "\n";
2417 }
2418
Jim Laskey65195462006-10-30 13:35:07 +00002419 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2420 /// section.
Jim Laskeyef42a012006-11-02 20:12:39 +00002421 void EmitFunctionDebugFrame() {
Reid Spencer5a4951e2006-11-07 06:36:36 +00002422 if (!TAI->getDwarfRequiresFrameSection())
2423 return;
Jim Laskey9d4209f2006-11-07 19:33:46 +00002424
Jim Laskeyef42a012006-11-02 20:12:39 +00002425 // Start the dwarf frame section.
2426 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2427
2428 EmitDifference("frame_end", SubprogramCount,
2429 "frame_begin", SubprogramCount);
2430 EOL("Length of Frame Information Entry");
2431
2432 EmitLabel("frame_begin", SubprogramCount);
2433
2434 EmitDifference("frame_common", 0, "section_frame", 0);
2435 EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002436
Jim Laskeyef42a012006-11-02 20:12:39 +00002437 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2438 EmitDifference("func_end", SubprogramCount,
2439 "func_begin", SubprogramCount);
2440 EOL("FDE address range");
2441
2442 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2443
2444 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2445
2446 EmitAlign(2);
2447 EmitLabel("frame_end", SubprogramCount);
2448
2449 O << "\n";
2450 }
2451
2452 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002453 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002454 void EmitDebugPubNames() {
2455 // Start the dwarf pubnames section.
2456 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2457
2458 // Process each compile unit.
2459 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2460 CompileUnit *Unit = CompileUnits[i];
2461
2462 if (Unit->hasContent()) {
2463 EmitDifference("pubnames_end", Unit->getID(),
2464 "pubnames_begin", Unit->getID());
2465 EOL("Length of Public Names Info");
2466
2467 EmitLabel("pubnames_begin", Unit->getID());
2468
2469 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2470
2471 EmitDifference("info_begin", Unit->getID(), "section_info", 0);
2472 EOL("Offset of Compilation Unit Info");
2473
2474 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2475 EOL("Compilation Unit Length");
2476
2477 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2478
2479 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2480 GE = Globals.end();
2481 GI != GE; ++GI) {
2482 const std::string &Name = GI->first;
2483 DIE * Entity = GI->second;
2484
2485 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2486 EmitString(Name); EOL("External Name");
2487 }
2488
2489 EmitInt32(0); EOL("End Mark");
2490 EmitLabel("pubnames_end", Unit->getID());
2491
2492 O << "\n";
2493 }
2494 }
2495 }
2496
2497 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002498 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002499 void EmitDebugStr() {
2500 // Check to see if it is worth the effort.
2501 if (!StringPool.empty()) {
2502 // Start the dwarf str section.
2503 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2504
2505 // For each of strings in the string pool.
2506 for (unsigned StringID = 1, N = StringPool.size();
2507 StringID <= N; ++StringID) {
2508 // Emit a label for reference from debug information entries.
2509 EmitLabel("string", StringID);
2510 // Emit the string itself.
2511 const std::string &String = StringPool[StringID];
2512 EmitString(String); O << "\n";
2513 }
2514
2515 O << "\n";
2516 }
2517 }
2518
2519 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002520 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002521 void EmitDebugLoc() {
2522 // Start the dwarf loc section.
2523 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2524
2525 O << "\n";
2526 }
2527
2528 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002529 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002530 void EmitDebugARanges() {
2531 // Start the dwarf aranges section.
2532 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2533
2534 // FIXME - Mock up
2535 #if 0
2536 // Process each compile unit.
2537 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2538 CompileUnit *Unit = CompileUnits[i];
2539
2540 if (Unit->hasContent()) {
2541 // Don't include size of length
2542 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2543
2544 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2545
2546 EmitReference("info_begin", Unit->getID());
2547 EOL("Offset of Compilation Unit Info");
2548
2549 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
2550
2551 EmitInt8(0); EOL("Size of Segment Descriptor");
2552
2553 EmitInt16(0); EOL("Pad (1)");
2554 EmitInt16(0); EOL("Pad (2)");
2555
2556 // Range 1
2557 EmitReference("text_begin", 0); EOL("Address");
2558 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
2559
2560 EmitInt32(0); EOL("EOM (1)");
2561 EmitInt32(0); EOL("EOM (2)");
2562
2563 O << "\n";
2564 }
2565 }
2566 #endif
2567 }
2568
2569 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002570 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002571 void EmitDebugRanges() {
2572 // Start the dwarf ranges section.
2573 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2574
2575 O << "\n";
2576 }
2577
2578 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002579 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002580 void EmitDebugMacInfo() {
2581 // Start the dwarf macinfo section.
2582 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2583
2584 O << "\n";
2585 }
2586
Jim Laskey65195462006-10-30 13:35:07 +00002587 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2588 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002589 void ConstructCompileUnitDIEs() {
2590 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
2591
2592 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00002593 unsigned ID = DebugInfo->RecordSource(CUW[i]);
2594 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002595 CompileUnits.push_back(Unit);
2596 }
2597 }
2598
Jim Laskey65195462006-10-30 13:35:07 +00002599 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2600 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002601 void ConstructGlobalDIEs() {
2602 std::vector<GlobalVariableDesc *> GlobalVariables =
2603 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
2604
2605 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2606 GlobalVariableDesc *GVD = GlobalVariables[i];
2607 NewGlobalVariable(GVD);
2608 }
2609 }
Jim Laskey65195462006-10-30 13:35:07 +00002610
2611 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2612 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002613 void ConstructSubprogramDIEs() {
2614 std::vector<SubprogramDesc *> Subprograms =
2615 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
2616
2617 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2618 SubprogramDesc *SPD = Subprograms[i];
2619 NewSubprogram(SPD);
2620 }
2621 }
Jim Laskey65195462006-10-30 13:35:07 +00002622
2623 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
2624 ///
2625 bool ShouldEmitDwarf() const { return shouldEmit; }
2626
2627public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002628 //===--------------------------------------------------------------------===//
2629 // Main entry points.
2630 //
2631 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
2632 : O(OS)
2633 , Asm(A)
2634 , TAI(T)
2635 , TD(Asm->TM.getTargetData())
2636 , RI(Asm->TM.getRegisterInfo())
2637 , M(NULL)
2638 , MF(NULL)
2639 , DebugInfo(NULL)
2640 , didInitial(false)
2641 , shouldEmit(false)
2642 , SubprogramCount(0)
2643 , CompileUnits()
2644 , AbbreviationsSet(InitAbbreviationsSetSize)
2645 , Abbreviations()
2646 , ValuesSet(InitValuesSetSize)
2647 , Values()
2648 , StringPool()
2649 , DescToUnitMap()
2650 , SectionMap()
2651 , SectionSourceLines()
2652 {
2653 }
2654 virtual ~Dwarf() {
2655 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2656 delete CompileUnits[i];
2657 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2658 delete Values[j];
2659 }
2660
Jim Laskey65195462006-10-30 13:35:07 +00002661 // Accessors.
2662 //
2663 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
2664
2665 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2666 /// created it. Set by the target AsmPrinter.
Jim Laskeyef42a012006-11-02 20:12:39 +00002667 void SetDebugInfo(MachineDebugInfo *DI) {
2668 // Make sure initial declarations are made.
2669 if (!DebugInfo && DI->hasInfo()) {
2670 DebugInfo = DI;
2671 shouldEmit = true;
2672
2673 // Emit initial sections
2674 EmitInitial();
2675
2676 // Create all the compile unit DIEs.
2677 ConstructCompileUnitDIEs();
2678
2679 // Create DIEs for each of the externally visible global variables.
2680 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002681
Jim Laskeyef42a012006-11-02 20:12:39 +00002682 // Create DIEs for each of the externally visible subprograms.
2683 ConstructSubprogramDIEs();
2684
2685 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00002686 SectionMap.insert(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002687 }
2688 }
2689
Jim Laskey65195462006-10-30 13:35:07 +00002690 /// BeginModule - Emit all Dwarf sections that should come prior to the
2691 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002692 void BeginModule(Module *M) {
2693 this->M = M;
2694
2695 if (!ShouldEmitDwarf()) return;
2696 EOL("Dwarf Begin Module");
2697 }
2698
Jim Laskey65195462006-10-30 13:35:07 +00002699 /// EndModule - Emit all Dwarf sections that should come after the content.
2700 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002701 void EndModule() {
2702 if (!ShouldEmitDwarf()) return;
2703 EOL("Dwarf End Module");
2704
2705 // Standard sections final addresses.
2706 Asm->SwitchToTextSection(TAI->getTextSection());
2707 EmitLabel("text_end", 0);
2708 Asm->SwitchToDataSection(TAI->getDataSection());
2709 EmitLabel("data_end", 0);
2710
2711 // End text sections.
2712 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2713 Asm->SwitchToTextSection(SectionMap[i].c_str());
2714 EmitLabel("section_end", i);
2715 }
2716
2717 // Compute DIE offsets and sizes.
2718 SizeAndOffsets();
2719
2720 // Emit all the DIEs into a debug info section
2721 EmitDebugInfo();
2722
2723 // Corresponding abbreviations into a abbrev section.
2724 EmitAbbreviations();
2725
2726 // Emit source line correspondence into a debug line section.
2727 EmitDebugLines();
2728
2729 // Emit info into a debug pubnames section.
2730 EmitDebugPubNames();
2731
2732 // Emit info into a debug str section.
2733 EmitDebugStr();
2734
2735 // Emit info into a debug loc section.
2736 EmitDebugLoc();
2737
2738 // Emit info into a debug aranges section.
2739 EmitDebugARanges();
2740
2741 // Emit info into a debug ranges section.
2742 EmitDebugRanges();
2743
2744 // Emit info into a debug macinfo section.
2745 EmitDebugMacInfo();
2746 }
2747
Jim Laskey65195462006-10-30 13:35:07 +00002748 /// BeginFunction - Gather pre-function debug information. Assumes being
2749 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002750 void BeginFunction(MachineFunction *MF) {
2751 this->MF = MF;
2752
2753 if (!ShouldEmitDwarf()) return;
2754 EOL("Dwarf Begin Function");
2755
2756 // Begin accumulating function debug information.
2757 DebugInfo->BeginFunction(MF);
2758
2759 // Assumes in correct section after the entry point.
2760 EmitLabel("func_begin", ++SubprogramCount);
2761 }
2762
Jim Laskey65195462006-10-30 13:35:07 +00002763 /// EndFunction - Gather and emit post-function debug information.
2764 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002765 void EndFunction() {
2766 if (!ShouldEmitDwarf()) return;
2767 EOL("Dwarf End Function");
2768
2769 // Define end label for subprogram.
2770 EmitLabel("func_end", SubprogramCount);
2771
2772 // Get function line info.
2773 const std::vector<SourceLineInfo> &LineInfos = DebugInfo->getSourceLines();
2774
2775 if (!LineInfos.empty()) {
2776 // Get section line info.
2777 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2778 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2779 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2780 // Append the function info to section info.
2781 SectionLineInfos.insert(SectionLineInfos.end(),
2782 LineInfos.begin(), LineInfos.end());
2783 }
2784
2785 // Construct scopes for subprogram.
2786 ConstructRootScope(DebugInfo->getRootScope());
2787
2788 // Emit function frame information.
2789 EmitFunctionDebugFrame();
2790
2791 // Reset the line numbers for the next function.
2792 DebugInfo->ClearLineInfo();
2793
2794 // Clear function debug information.
2795 DebugInfo->EndFunction();
2796 }
Jim Laskey65195462006-10-30 13:35:07 +00002797};
2798
Jim Laskey0d086af2006-02-27 12:43:29 +00002799} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00002800
2801//===----------------------------------------------------------------------===//
2802
Jim Laskeyd18e2892006-01-20 20:34:06 +00002803/// Emit - Print the abbreviation using the specified Dwarf writer.
2804///
Jim Laskey65195462006-10-30 13:35:07 +00002805void DIEAbbrev::Emit(const Dwarf &DW) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002806 // Emit its Dwarf tag type.
2807 DW.EmitULEB128Bytes(Tag);
2808 DW.EOL(TagString(Tag));
2809
2810 // Emit whether it has children DIEs.
2811 DW.EmitULEB128Bytes(ChildrenFlag);
2812 DW.EOL(ChildrenString(ChildrenFlag));
2813
2814 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00002815 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002816 const DIEAbbrevData &AttrData = Data[i];
2817
2818 // Emit attribute type.
2819 DW.EmitULEB128Bytes(AttrData.getAttribute());
2820 DW.EOL(AttributeString(AttrData.getAttribute()));
2821
2822 // Emit form type.
2823 DW.EmitULEB128Bytes(AttrData.getForm());
2824 DW.EOL(FormEncodingString(AttrData.getForm()));
2825 }
2826
2827 // Mark end of abbreviation.
2828 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
2829 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
2830}
2831
2832#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00002833void DIEAbbrev::print(std::ostream &O) {
2834 O << "Abbreviation @"
2835 << std::hex << (intptr_t)this << std::dec
2836 << " "
2837 << TagString(Tag)
2838 << " "
2839 << ChildrenString(ChildrenFlag)
2840 << "\n";
2841
2842 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
2843 O << " "
2844 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002845 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00002846 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002847 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002848 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00002849}
2850void DIEAbbrev::dump() { print(std::cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002851#endif
2852
2853//===----------------------------------------------------------------------===//
2854
Jim Laskeyef42a012006-11-02 20:12:39 +00002855#ifndef NDEBUG
2856void DIEValue::dump() {
2857 print(std::cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002858}
Jim Laskeyef42a012006-11-02 20:12:39 +00002859#endif
2860
2861//===----------------------------------------------------------------------===//
2862
Jim Laskey063e7652006-01-17 17:31:53 +00002863/// EmitValue - Emit integer of appropriate size.
2864///
Jim Laskey65195462006-10-30 13:35:07 +00002865void DIEInteger::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +00002866 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00002867 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00002868 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002869 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002870 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002871 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002872 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002873 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002874 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002875 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +00002876 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
2877 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +00002878 default: assert(0 && "DIE Value form not supported yet"); break;
2879 }
2880}
2881
Jim Laskey063e7652006-01-17 17:31:53 +00002882//===----------------------------------------------------------------------===//
2883
2884/// EmitValue - Emit string value.
2885///
Jim Laskey65195462006-10-30 13:35:07 +00002886void DIEString::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002887 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00002888}
2889
Jim Laskey063e7652006-01-17 17:31:53 +00002890//===----------------------------------------------------------------------===//
2891
2892/// EmitValue - Emit label value.
2893///
Jim Laskey65195462006-10-30 13:35:07 +00002894void DIEDwarfLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002895 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +00002896}
2897
2898/// SizeOf - Determine size of label value in bytes.
2899///
Jim Laskey65195462006-10-30 13:35:07 +00002900unsigned DIEDwarfLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002901 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002902}
Jim Laskeyef42a012006-11-02 20:12:39 +00002903
Jim Laskey063e7652006-01-17 17:31:53 +00002904//===----------------------------------------------------------------------===//
2905
Jim Laskeyd18e2892006-01-20 20:34:06 +00002906/// EmitValue - Emit label value.
2907///
Jim Laskey65195462006-10-30 13:35:07 +00002908void DIEObjectLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002909 DW.EmitReference(Label);
2910}
2911
2912/// SizeOf - Determine size of label value in bytes.
2913///
Jim Laskey65195462006-10-30 13:35:07 +00002914unsigned DIEObjectLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002915 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00002916}
2917
2918//===----------------------------------------------------------------------===//
2919
Jim Laskey063e7652006-01-17 17:31:53 +00002920/// EmitValue - Emit delta value.
2921///
Jim Laskey65195462006-10-30 13:35:07 +00002922void DIEDelta::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002923 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +00002924}
2925
2926/// SizeOf - Determine size of delta value in bytes.
2927///
Jim Laskey65195462006-10-30 13:35:07 +00002928unsigned DIEDelta::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002929 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002930}
2931
2932//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00002933
Jim Laskeyb8509c52006-03-23 18:07:55 +00002934/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002935///
Jim Laskey65195462006-10-30 13:35:07 +00002936void DIEntry::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +00002937 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00002938}
Jim Laskeyd18e2892006-01-20 20:34:06 +00002939
2940//===----------------------------------------------------------------------===//
2941
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002942/// ComputeSize - calculate the size of the block.
2943///
Jim Laskey65195462006-10-30 13:35:07 +00002944unsigned DIEBlock::ComputeSize(Dwarf &DW) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002945 if (!Size) {
2946 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2947
2948 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2949 Size += Values[i]->SizeOf(DW, AbbrevData[i].getForm());
2950 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002951 }
2952 return Size;
2953}
2954
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002955/// EmitValue - Emit block data.
2956///
Jim Laskey65195462006-10-30 13:35:07 +00002957void DIEBlock::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002958 switch (Form) {
2959 case DW_FORM_block1: DW.EmitInt8(Size); break;
2960 case DW_FORM_block2: DW.EmitInt16(Size); break;
2961 case DW_FORM_block4: DW.EmitInt32(Size); break;
2962 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
2963 default: assert(0 && "Improper form for block"); break;
2964 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002965
2966 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2967
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002968 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2969 DW.EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002970 Values[i]->EmitValue(DW, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002971 }
2972}
2973
2974/// SizeOf - Determine size of block data in bytes.
2975///
Jim Laskey65195462006-10-30 13:35:07 +00002976unsigned DIEBlock::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002977 switch (Form) {
2978 case DW_FORM_block1: return Size + sizeof(int8_t);
2979 case DW_FORM_block2: return Size + sizeof(int16_t);
2980 case DW_FORM_block4: return Size + sizeof(int32_t);
Jim Laskeyef42a012006-11-02 20:12:39 +00002981 case DW_FORM_block: return Size + SizeULEB128(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002982 default: assert(0 && "Improper form for block"); break;
2983 }
2984 return 0;
2985}
2986
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002987//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00002988/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00002989
2990DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002991 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00002992 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00002993}
Jim Laskeyef42a012006-11-02 20:12:39 +00002994
Jim Laskeyb8509c52006-03-23 18:07:55 +00002995/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
2996///
2997void DIE::AddSiblingOffset() {
2998 DIEInteger *DI = new DIEInteger(0);
2999 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00003000 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00003001}
3002
Jim Laskeyef42a012006-11-02 20:12:39 +00003003/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003004///
Jim Laskeyef42a012006-11-02 20:12:39 +00003005void DIE::Profile(FoldingSetNodeID &ID) {
3006 Abbrev.Profile(ID);
3007
3008 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3009 ID.AddPointer(Children[i]);
3010
3011 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3012 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003013}
Jim Laskeyef42a012006-11-02 20:12:39 +00003014
3015#ifndef NDEBUG
3016void DIE::print(std::ostream &O, unsigned IncIndent) {
3017 static unsigned IndentCount = 0;
3018 IndentCount += IncIndent;
3019 const std::string Indent(IndentCount, ' ');
3020 bool isBlock = Abbrev.getTag() == 0;
3021
3022 if (!isBlock) {
3023 O << Indent
3024 << "Die: "
3025 << "0x" << std::hex << (intptr_t)this << std::dec
3026 << ", Offset: " << Offset
3027 << ", Size: " << Size
3028 << "\n";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003029
Jim Laskeyef42a012006-11-02 20:12:39 +00003030 O << Indent
3031 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00003032 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00003033 << ChildrenString(Abbrev.getChildrenFlag());
3034 } else {
3035 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00003036 }
3037 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003038
Jim Laskeyef42a012006-11-02 20:12:39 +00003039 const std::vector<DIEAbbrevData> &Data = Abbrev.getData();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003040
Jim Laskeyef42a012006-11-02 20:12:39 +00003041 IndentCount += 2;
3042 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3043 O << Indent;
3044 if (!isBlock) {
3045 O << AttributeString(Data[i].getAttribute());
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003046 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00003047 O << "Blk[" << i << "]";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003048 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003049 O << " "
3050 << FormEncodingString(Data[i].getForm())
3051 << " ";
3052 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00003053 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00003054 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003055 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00003056
Jim Laskeyef42a012006-11-02 20:12:39 +00003057 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3058 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00003059 }
Jim Laskey063e7652006-01-17 17:31:53 +00003060
Jim Laskeyef42a012006-11-02 20:12:39 +00003061 if (!isBlock) O << "\n";
3062 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00003063}
3064
Jim Laskeyef42a012006-11-02 20:12:39 +00003065void DIE::dump() {
3066 print(std::cerr);
Jim Laskey41886992006-04-07 16:34:46 +00003067}
Jim Laskeybd761842006-02-27 17:27:12 +00003068#endif
Jim Laskey65195462006-10-30 13:35:07 +00003069
3070//===----------------------------------------------------------------------===//
3071/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00003072///
Jim Laskey65195462006-10-30 13:35:07 +00003073
3074DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3075 const TargetAsmInfo *T) {
3076 DW = new Dwarf(OS, A, T);
3077}
3078
3079DwarfWriter::~DwarfWriter() {
3080 delete DW;
3081}
3082
3083/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
3084/// created it. Set by the target AsmPrinter.
3085void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
3086 DW->SetDebugInfo(DI);
3087}
3088
3089/// BeginModule - Emit all Dwarf sections that should come prior to the
3090/// content.
3091void DwarfWriter::BeginModule(Module *M) {
3092 DW->BeginModule(M);
3093}
3094
3095/// EndModule - Emit all Dwarf sections that should come after the content.
3096///
3097void DwarfWriter::EndModule() {
3098 DW->EndModule();
3099}
3100
3101/// BeginFunction - Gather pre-function debug information. Assumes being
3102/// emitted immediately after the function entry point.
3103void DwarfWriter::BeginFunction(MachineFunction *MF) {
3104 DW->BeginFunction(MF);
3105}
3106
3107/// EndFunction - Gather and emit post-function debug information.
3108///
3109void DwarfWriter::EndFunction() {
3110 DW->EndFunction();
3111}