blob: 39de9b3d12a6d163f136e62e57fce78f048a3643 [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 /// Usage - Number of uses of this value.
358 ///
359 unsigned Usage;
360
361 DIEValue(unsigned T)
362 : Type(T)
363 , Usage(1)
364 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000365 virtual ~DIEValue() {}
366
Jim Laskeyef42a012006-11-02 20:12:39 +0000367 unsigned getType() const { return Type; }
368 unsigned getUsage() const { return Usage; }
369 void IncUsage() { ++Usage; }
370
Jim Laskey0d086af2006-02-27 12:43:29 +0000371 // Implement isa/cast/dyncast.
372 static bool classof(const DIEValue *) { return true; }
373
374 /// EmitValue - Emit value via the Dwarf writer.
375 ///
Jim Laskey65195462006-10-30 13:35:07 +0000376 virtual void EmitValue(const Dwarf &DW, unsigned Form) const = 0;
Jim Laskey0d086af2006-02-27 12:43:29 +0000377
378 /// SizeOf - Return the size of a value in bytes.
379 ///
Jim Laskey65195462006-10-30 13:35:07 +0000380 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const = 0;
Jim Laskeyef42a012006-11-02 20:12:39 +0000381
382 /// Profile - Used to gather unique data for the value folding set.
383 ///
384 virtual void Profile(FoldingSetNodeID &ID) = 0;
385
386#ifndef NDEBUG
387 virtual void print(std::ostream &O) = 0;
388 void dump();
389#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000390};
Jim Laskey063e7652006-01-17 17:31:53 +0000391
Jim Laskey0d086af2006-02-27 12:43:29 +0000392//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000393/// DWInteger - An integer value DIE.
394///
Jim Laskey0d086af2006-02-27 12:43:29 +0000395class DIEInteger : public DIEValue {
396private:
397 uint64_t Integer;
398
399public:
400 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000401
Jim Laskey0d086af2006-02-27 12:43:29 +0000402 // Implement isa/cast/dyncast.
403 static bool classof(const DIEInteger *) { return true; }
404 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
405
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000406 /// BestForm - Choose the best form for integer.
407 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000408 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
409 if (IsSigned) {
410 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
411 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
412 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
413 } else {
414 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
415 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
416 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
417 }
418 return DW_FORM_data8;
419 }
420
Jim Laskey0d086af2006-02-27 12:43:29 +0000421 /// EmitValue - Emit integer of appropriate size.
422 ///
Jim Laskey65195462006-10-30 13:35:07 +0000423 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000424
425 /// SizeOf - Determine size of integer value in bytes.
426 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000427 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
428 switch (Form) {
429 case DW_FORM_flag: // Fall thru
430 case DW_FORM_ref1: // Fall thru
431 case DW_FORM_data1: return sizeof(int8_t);
432 case DW_FORM_ref2: // Fall thru
433 case DW_FORM_data2: return sizeof(int16_t);
434 case DW_FORM_ref4: // Fall thru
435 case DW_FORM_data4: return sizeof(int32_t);
436 case DW_FORM_ref8: // Fall thru
437 case DW_FORM_data8: return sizeof(int64_t);
438 case DW_FORM_udata: return SizeULEB128(Integer);
439 case DW_FORM_sdata: return SizeSLEB128(Integer);
440 default: assert(0 && "DIE Value form not supported yet"); break;
441 }
442 return 0;
443 }
444
445 /// Profile - Used to gather unique data for the value folding set.
446 ///
447 virtual void Profile(FoldingSetNodeID &ID) {
448 ID.AddInteger(Integer);
449 }
450
451#ifndef NDEBUG
452 virtual void print(std::ostream &O) {
453 O << "Int: " << (int64_t)Integer
454 << " 0x" << std::hex << Integer << std::dec;
455 }
456#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000457};
Jim Laskey063e7652006-01-17 17:31:53 +0000458
Jim Laskey0d086af2006-02-27 12:43:29 +0000459//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000460/// DIEString - A string value DIE.
461///
Jim Laskeyef42a012006-11-02 20:12:39 +0000462class DIEString : public DIEValue {
463public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000464 const std::string String;
465
466 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000467
Jim Laskey0d086af2006-02-27 12:43:29 +0000468 // Implement isa/cast/dyncast.
469 static bool classof(const DIEString *) { return true; }
470 static bool classof(const DIEValue *S) { return S->Type == isString; }
471
472 /// EmitValue - Emit string value.
473 ///
Jim Laskey65195462006-10-30 13:35:07 +0000474 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000475
476 /// SizeOf - Determine size of string value in bytes.
477 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000478 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
479 return String.size() + sizeof(char); // sizeof('\0');
480 }
481
482 /// Profile - Used to gather unique data for the value folding set.
483 ///
484 virtual void Profile(FoldingSetNodeID &ID) {
485 ID.AddString(String);
486 }
487
488#ifndef NDEBUG
489 virtual void print(std::ostream &O) {
490 O << "Str: \"" << String << "\"";
491 }
492#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000493};
Jim Laskey063e7652006-01-17 17:31:53 +0000494
Jim Laskey0d086af2006-02-27 12:43:29 +0000495//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000496/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000497//
Jim Laskeyef42a012006-11-02 20:12:39 +0000498class DIEDwarfLabel : public DIEValue {
499public:
500
Jim Laskey0d086af2006-02-27 12:43:29 +0000501 const DWLabel Label;
502
503 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000504
Jim Laskey0d086af2006-02-27 12:43:29 +0000505 // Implement isa/cast/dyncast.
506 static bool classof(const DIEDwarfLabel *) { return true; }
507 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
508
509 /// EmitValue - Emit label value.
510 ///
Jim Laskey65195462006-10-30 13:35:07 +0000511 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000512
513 /// SizeOf - Determine size of label value in bytes.
514 ///
Jim Laskey65195462006-10-30 13:35:07 +0000515 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000516
517 /// Profile - Used to gather unique data for the value folding set.
518 ///
519 virtual void Profile(FoldingSetNodeID &ID) {
520 Label.Profile(ID);
521 }
522
523#ifndef NDEBUG
524 virtual void print(std::ostream &O) {
525 O << "Lbl: ";
526 Label.print(O);
527 }
528#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000529};
Jim Laskey063e7652006-01-17 17:31:53 +0000530
Jim Laskey063e7652006-01-17 17:31:53 +0000531
Jim Laskey0d086af2006-02-27 12:43:29 +0000532//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000533/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000534//
Jim Laskeyef42a012006-11-02 20:12:39 +0000535class DIEObjectLabel : public DIEValue {
536public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000537 const std::string Label;
538
539 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000540
Jim Laskey0d086af2006-02-27 12:43:29 +0000541 // Implement isa/cast/dyncast.
542 static bool classof(const DIEObjectLabel *) { return true; }
543 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
544
545 /// EmitValue - Emit label value.
546 ///
Jim Laskey65195462006-10-30 13:35:07 +0000547 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000548
549 /// SizeOf - Determine size of label value in bytes.
550 ///
Jim Laskey65195462006-10-30 13:35:07 +0000551 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000552
553 /// Profile - Used to gather unique data for the value folding set.
554 ///
555 virtual void Profile(FoldingSetNodeID &ID) {
556 ID.AddString(Label);
557 }
558
559#ifndef NDEBUG
560 virtual void print(std::ostream &O) {
561 O << "Obj: " << Label;
562 }
563#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000564};
Jim Laskey063e7652006-01-17 17:31:53 +0000565
Jim Laskey0d086af2006-02-27 12:43:29 +0000566//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000567/// DIEDelta - A simple label difference DIE.
568///
Jim Laskeyef42a012006-11-02 20:12:39 +0000569class DIEDelta : public DIEValue {
570public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000571 const DWLabel LabelHi;
572 const DWLabel LabelLo;
573
574 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
575 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000576
Jim Laskey0d086af2006-02-27 12:43:29 +0000577 // Implement isa/cast/dyncast.
578 static bool classof(const DIEDelta *) { return true; }
579 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
580
581 /// EmitValue - Emit delta value.
582 ///
Jim Laskey65195462006-10-30 13:35:07 +0000583 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000584
585 /// SizeOf - Determine size of delta value in bytes.
586 ///
Jim Laskey65195462006-10-30 13:35:07 +0000587 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000588
589 /// Profile - Used to gather unique data for the value folding set.
590 ///
591 virtual void Profile(FoldingSetNodeID &ID){
592 LabelHi.Profile(ID);
593 LabelLo.Profile(ID);
594 }
595
596#ifndef NDEBUG
597 virtual void print(std::ostream &O) {
598 O << "Del: ";
599 LabelHi.print(O);
600 O << "-";
601 LabelLo.print(O);
602 }
603#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000604};
Jim Laskey063e7652006-01-17 17:31:53 +0000605
Jim Laskey0d086af2006-02-27 12:43:29 +0000606//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000607/// DIEntry - A pointer to another debug information entry. An instance of this
608/// class can also be used as a proxy for a debug information entry not yet
609/// defined (ie. types.)
610class DIEntry : public DIEValue {
611public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000612 DIE *Entry;
613
614 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000615
Jim Laskey0d086af2006-02-27 12:43:29 +0000616 // Implement isa/cast/dyncast.
617 static bool classof(const DIEntry *) { return true; }
618 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
619
Jim Laskeyb8509c52006-03-23 18:07:55 +0000620 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000621 ///
Jim Laskey65195462006-10-30 13:35:07 +0000622 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000623
Jim Laskeyb8509c52006-03-23 18:07:55 +0000624 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000625 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000626 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
627 return sizeof(int32_t);
628 }
629
630 /// Profile - Used to gather unique data for the value folding set.
631 ///
632 virtual void Profile(FoldingSetNodeID &ID) {
633 if (Entry) {
634 ID.AddPointer(Entry);
635 } else {
636 ID.AddPointer(this);
637 }
638 }
639
640#ifndef NDEBUG
641 virtual void print(std::ostream &O) {
642 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
643 }
644#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000645};
646
647//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000648/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000649//
Jim Laskeyef42a012006-11-02 20:12:39 +0000650class DIEBlock : public DIEValue, public DIE {
651public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000652 unsigned Size; // Size in bytes excluding size header.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000653
654 DIEBlock()
655 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000656 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000657 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000658 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000659 ~DIEBlock() {
660 }
661
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000662 // Implement isa/cast/dyncast.
663 static bool classof(const DIEBlock *) { return true; }
664 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
665
666 /// ComputeSize - calculate the size of the block.
667 ///
Jim Laskey65195462006-10-30 13:35:07 +0000668 unsigned ComputeSize(Dwarf &DW);
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000669
670 /// BestForm - Choose the best form for data.
671 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000672 unsigned BestForm() const {
673 if ((unsigned char)Size == Size) return DW_FORM_block1;
674 if ((unsigned short)Size == Size) return DW_FORM_block2;
675 if ((unsigned int)Size == Size) return DW_FORM_block4;
676 return DW_FORM_block;
677 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000678
679 /// EmitValue - Emit block data.
680 ///
Jim Laskey65195462006-10-30 13:35:07 +0000681 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000682
683 /// SizeOf - Determine size of block data in bytes.
684 ///
Jim Laskey65195462006-10-30 13:35:07 +0000685 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000686
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000687
Jim Laskeyef42a012006-11-02 20:12:39 +0000688 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000689 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000690 virtual void DIEBlock::Profile(FoldingSetNodeID &ID) {
691 DIE::Profile(ID);
692 }
693
694#ifndef NDEBUG
695 virtual void print(std::ostream &O) {
696 O << "Blk: ";
697 DIE::print(O, 5);
698 }
699#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000700};
701
702//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000703/// CompileUnit - This dwarf writer support class manages information associate
704/// with a source file.
705class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000706private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000707 /// Desc - Compile unit debug descriptor.
708 ///
709 CompileUnitDesc *Desc;
710
711 /// ID - File identifier for source.
712 ///
713 unsigned ID;
714
715 /// Die - Compile unit debug information entry.
716 ///
717 DIE *Die;
718
719 /// DescToDieMap - Tracks the mapping of unit level debug informaton
720 /// descriptors to debug information entries.
721 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
722
723 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
724 /// descriptors to debug information entries using a DIEntry proxy.
725 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
726
727 /// Globals - A map of globally visible named entities for this unit.
728 ///
729 std::map<std::string, DIE *> Globals;
730
731 /// DiesSet - Used to uniquely define dies within the compile unit.
732 ///
733 FoldingSet<DIE> DiesSet;
734
735 /// Dies - List of all dies in the compile unit.
736 ///
737 std::vector<DIE *> Dies;
Jim Laskey0d086af2006-02-27 12:43:29 +0000738
739public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000740 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
741 : Desc(CUD)
742 , ID(I)
743 , Die(D)
744 , DescToDieMap()
745 , DescToDIEntryMap()
746 , Globals()
747 , DiesSet(InitDiesSetSize)
748 , Dies()
749 {}
750
751 ~CompileUnit() {
752 delete Die;
753
754 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
755 delete Dies[i];
756 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000757
Jim Laskeybd761842006-02-27 17:27:12 +0000758 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000759 CompileUnitDesc *getDesc() const { return Desc; }
760 unsigned getID() const { return ID; }
761 DIE* getDie() const { return Die; }
762 std::map<std::string, DIE *> &getGlobals() { return Globals; }
763
764 /// hasContent - Return true if this compile unit has something to write out.
765 ///
766 bool hasContent() const {
767 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000768 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000769
770 /// AddGlobal - Add a new global entity to the compile unit.
771 ///
772 void AddGlobal(const std::string &Name, DIE *Die) {
773 Globals[Name] = Die;
774 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000775
Jim Laskeyef42a012006-11-02 20:12:39 +0000776 /// getDieMapSlotFor - Returns the debug information entry map slot for the
777 /// specified debug descriptor.
778 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
779 return DescToDieMap[DD];
780 }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000781
Jim Laskeyef42a012006-11-02 20:12:39 +0000782 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
783 /// specified debug descriptor.
784 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DD) {
785 return DescToDIEntryMap[DD];
786 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000787
Jim Laskeyef42a012006-11-02 20:12:39 +0000788 /// AddDie - Adds or interns the DIE to the compile unit.
789 ///
790 DIE *AddDie(DIE &Buffer) {
791 FoldingSetNodeID ID;
792 Buffer.Profile(ID);
793 void *Where;
794 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
795
796 if (!Die) {
797 Die = new DIE(Buffer);
798 DiesSet.InsertNode(Die, Where);
799 this->Die->AddChild(Die);
800 Buffer.Detach();
801 }
802
803 return Die;
804 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000805};
806
Jim Laskey65195462006-10-30 13:35:07 +0000807//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000808/// Dwarf - Emits Dwarf debug and exception handling directives.
809///
Jim Laskey65195462006-10-30 13:35:07 +0000810class Dwarf {
811
812private:
813
814 //===--------------------------------------------------------------------===//
815 // Core attributes used by the Dwarf writer.
816 //
817
818 //
819 /// O - Stream to .s file.
820 ///
821 std::ostream &O;
822
823 /// Asm - Target of Dwarf emission.
824 ///
825 AsmPrinter *Asm;
826
827 /// TAI - Target Asm Printer.
828 const TargetAsmInfo *TAI;
829
830 /// TD - Target data.
831 const TargetData *TD;
832
833 /// RI - Register Information.
834 const MRegisterInfo *RI;
835
836 /// M - Current module.
837 ///
838 Module *M;
839
840 /// MF - Current machine function.
841 ///
842 MachineFunction *MF;
843
844 /// DebugInfo - Collected debug information.
845 ///
846 MachineDebugInfo *DebugInfo;
847
848 /// didInitial - Flag to indicate if initial emission has been done.
849 ///
850 bool didInitial;
851
852 /// shouldEmit - Flag to indicate if debug information should be emitted.
853 ///
854 bool shouldEmit;
855
856 /// SubprogramCount - The running count of functions being compiled.
857 ///
858 unsigned SubprogramCount;
859
860 //===--------------------------------------------------------------------===//
861 // Attributes used to construct specific Dwarf sections.
862 //
863
864 /// CompileUnits - All the compile units involved in this build. The index
865 /// of each entry in this vector corresponds to the sources in DebugInfo.
866 std::vector<CompileUnit *> CompileUnits;
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000867
Jim Laskeyef42a012006-11-02 20:12:39 +0000868 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +0000869 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000870 FoldingSet<DIEAbbrev> AbbreviationsSet;
871
872 /// Abbreviations - A list of all the unique abbreviations in use.
873 ///
874 std::vector<DIEAbbrev *> Abbreviations;
Jim Laskey65195462006-10-30 13:35:07 +0000875
Jim Laskeyef42a012006-11-02 20:12:39 +0000876 /// ValuesSet - Used to uniquely define values.
877 ///
878 FoldingSet<DIEValue> ValuesSet;
879
880 /// Values - A list of all the unique values in use.
881 ///
882 std::vector<DIEValue *> Values;
883
Jim Laskey65195462006-10-30 13:35:07 +0000884 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +0000885 ///
Jim Laskey65195462006-10-30 13:35:07 +0000886 UniqueVector<std::string> StringPool;
887
888 /// UnitMap - Map debug information descriptor to compile unit.
889 ///
890 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
891
Jim Laskey65195462006-10-30 13:35:07 +0000892 /// SectionMap - Provides a unique id per text section.
893 ///
894 UniqueVector<std::string> SectionMap;
895
896 /// SectionSourceLines - Tracks line numbers per text section.
897 ///
898 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
899
900
901public:
902
903 //===--------------------------------------------------------------------===//
904 // Emission and print routines
905 //
906
907 /// PrintHex - Print a value as a hexidecimal value.
908 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000909 void PrintHex(int Value) const {
910 O << "0x" << std::hex << Value << std::dec;
911 }
Jim Laskey65195462006-10-30 13:35:07 +0000912
913 /// EOL - Print a newline character to asm stream. If a comment is present
914 /// then it will be printed first. Comments should not contain '\n'.
Jim Laskeyef42a012006-11-02 20:12:39 +0000915 void EOL(const std::string &Comment) const {
916 if (DwarfVerbose && !Comment.empty()) {
917 O << "\t"
918 << TAI->getCommentString()
919 << " "
920 << Comment;
921 }
922 O << "\n";
923 }
Jim Laskey65195462006-10-30 13:35:07 +0000924
925 /// EmitAlign - Print a align directive.
926 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000927 void EmitAlign(unsigned Alignment) const {
928 O << TAI->getAlignDirective() << Alignment << "\n";
929 }
Jim Laskey65195462006-10-30 13:35:07 +0000930
931 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
932 /// unsigned leb128 value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000933 void EmitULEB128Bytes(unsigned Value) const {
934 if (TAI->hasLEB128()) {
935 O << "\t.uleb128\t"
936 << Value;
937 } else {
938 O << TAI->getData8bitsDirective();
939 PrintULEB128(O, Value);
940 }
941 }
Jim Laskey65195462006-10-30 13:35:07 +0000942
943 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
944 /// signed leb128 value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000945 void EmitSLEB128Bytes(int Value) const {
946 if (TAI->hasLEB128()) {
947 O << "\t.sleb128\t"
948 << Value;
949 } else {
950 O << TAI->getData8bitsDirective();
951 PrintSLEB128(O, Value);
952 }
953 }
Jim Laskey65195462006-10-30 13:35:07 +0000954
955 /// EmitInt8 - Emit a byte directive and value.
956 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000957 void EmitInt8(int Value) const {
958 O << TAI->getData8bitsDirective();
959 PrintHex(Value & 0xFF);
960 }
Jim Laskey65195462006-10-30 13:35:07 +0000961
962 /// EmitInt16 - Emit a short directive and value.
963 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000964 void EmitInt16(int Value) const {
965 O << TAI->getData16bitsDirective();
966 PrintHex(Value & 0xFFFF);
967 }
Jim Laskey65195462006-10-30 13:35:07 +0000968
969 /// EmitInt32 - Emit a long directive and value.
970 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000971 void EmitInt32(int Value) const {
972 O << TAI->getData32bitsDirective();
973 PrintHex(Value);
974 }
975
Jim Laskey65195462006-10-30 13:35:07 +0000976 /// EmitInt64 - Emit a long long directive and value.
977 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000978 void EmitInt64(uint64_t Value) const {
979 if (TAI->getData64bitsDirective()) {
980 O << TAI->getData64bitsDirective();
981 PrintHex(Value);
982 } else {
983 if (TD->isBigEndian()) {
984 EmitInt32(unsigned(Value >> 32)); O << "\n";
985 EmitInt32(unsigned(Value));
986 } else {
987 EmitInt32(unsigned(Value)); O << "\n";
988 EmitInt32(unsigned(Value >> 32));
989 }
990 }
991 }
992
Jim Laskey65195462006-10-30 13:35:07 +0000993 /// EmitString - Emit a string with quotes and a null terminator.
Jim Laskeyef42a012006-11-02 20:12:39 +0000994 /// Special characters are emitted properly.
Jim Laskey65195462006-10-30 13:35:07 +0000995 /// \literal (Eg. '\t') \endliteral
Jim Laskeyef42a012006-11-02 20:12:39 +0000996 void EmitString(const std::string &String) const {
997 O << TAI->getAsciiDirective()
998 << "\"";
999 for (unsigned i = 0, N = String.size(); i < N; ++i) {
1000 unsigned char C = String[i];
1001
1002 if (!isascii(C) || iscntrl(C)) {
1003 switch(C) {
1004 case '\b': O << "\\b"; break;
1005 case '\f': O << "\\f"; break;
1006 case '\n': O << "\\n"; break;
1007 case '\r': O << "\\r"; break;
1008 case '\t': O << "\\t"; break;
1009 default:
1010 O << '\\';
1011 O << char('0' + ((C >> 6) & 7));
1012 O << char('0' + ((C >> 3) & 7));
1013 O << char('0' + ((C >> 0) & 7));
1014 break;
1015 }
1016 } else if (C == '\"') {
1017 O << "\\\"";
1018 } else if (C == '\'') {
1019 O << "\\\'";
1020 } else {
1021 O << C;
1022 }
1023 }
1024 O << "\\0\"";
1025 }
Jim Laskey65195462006-10-30 13:35:07 +00001026
1027 /// PrintLabelName - Print label name in form used by Dwarf writer.
1028 ///
1029 void PrintLabelName(DWLabel Label) const {
1030 PrintLabelName(Label.Tag, Label.Number);
1031 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001032 void PrintLabelName(const char *Tag, unsigned Number) const {
1033 O << TAI->getPrivateGlobalPrefix()
1034 << "debug_"
1035 << Tag;
1036 if (Number) O << Number;
1037 }
Jim Laskey65195462006-10-30 13:35:07 +00001038
1039 /// EmitLabel - Emit location label for internal use by Dwarf.
1040 ///
1041 void EmitLabel(DWLabel Label) const {
1042 EmitLabel(Label.Tag, Label.Number);
1043 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001044 void EmitLabel(const char *Tag, unsigned Number) const {
1045 PrintLabelName(Tag, Number);
1046 O << ":\n";
1047 }
Jim Laskey65195462006-10-30 13:35:07 +00001048
1049 /// EmitReference - Emit a reference to a label.
1050 ///
1051 void EmitReference(DWLabel Label) const {
1052 EmitReference(Label.Tag, Label.Number);
1053 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001054 void EmitReference(const char *Tag, unsigned Number) const {
1055 if (TAI->getAddressSize() == 4)
1056 O << TAI->getData32bitsDirective();
1057 else
1058 O << TAI->getData64bitsDirective();
1059
1060 PrintLabelName(Tag, Number);
1061 }
1062 void EmitReference(const std::string &Name) const {
1063 if (TAI->getAddressSize() == 4)
1064 O << TAI->getData32bitsDirective();
1065 else
1066 O << TAI->getData64bitsDirective();
1067
1068 O << Name;
1069 }
Jim Laskey65195462006-10-30 13:35:07 +00001070
1071 /// EmitDifference - Emit the difference between two labels. Some
1072 /// assemblers do not behave with absolute expressions with data directives,
1073 /// so there is an option (needsSet) to use an intermediary set expression.
1074 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo) const {
1075 EmitDifference(LabelHi.Tag, LabelHi.Number, LabelLo.Tag, LabelLo.Number);
1076 }
1077 void EmitDifference(const char *TagHi, unsigned NumberHi,
Jim Laskeyef42a012006-11-02 20:12:39 +00001078 const char *TagLo, unsigned NumberLo) const {
1079 if (TAI->needsSet()) {
1080 static unsigned SetCounter = 0;
1081
1082 O << "\t.set\t";
1083 PrintLabelName("set", SetCounter);
1084 O << ",";
1085 PrintLabelName(TagHi, NumberHi);
1086 O << "-";
1087 PrintLabelName(TagLo, NumberLo);
1088 O << "\n";
1089
1090 if (TAI->getAddressSize() == sizeof(int32_t))
1091 O << TAI->getData32bitsDirective();
1092 else
1093 O << TAI->getData64bitsDirective();
1094
1095 PrintLabelName("set", SetCounter);
1096
1097 ++SetCounter;
1098 } else {
1099 if (TAI->getAddressSize() == sizeof(int32_t))
1100 O << TAI->getData32bitsDirective();
1101 else
1102 O << TAI->getData64bitsDirective();
1103
1104 PrintLabelName(TagHi, NumberHi);
1105 O << "-";
1106 PrintLabelName(TagLo, NumberLo);
1107 }
1108 }
Jim Laskey65195462006-10-30 13:35:07 +00001109
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001110 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Jim Laskey65195462006-10-30 13:35:07 +00001111 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001112 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1113 // Profile the node so that we can make it unique.
1114 FoldingSetNodeID ID;
1115 Abbrev.Profile(ID);
1116
1117 // Check the set for priors.
1118 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
1119
1120 // If it's newly added.
1121 if (InSet == &Abbrev) {
1122 // Add to abbreviation list.
1123 Abbreviations.push_back(&Abbrev);
1124 // Assign the vector position + 1 as its number.
1125 Abbrev.setNumber(Abbreviations.size());
1126 } else {
1127 // Assign existing abbreviation number.
1128 Abbrev.setNumber(InSet->getNumber());
1129 }
1130 }
1131
Jim Laskey65195462006-10-30 13:35:07 +00001132 /// NewString - Add a string to the constant pool and returns a label.
1133 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001134 DWLabel NewString(const std::string &String) {
1135 unsigned StringID = StringPool.insert(String);
1136 return DWLabel("string", StringID);
1137 }
Jim Laskey65195462006-10-30 13:35:07 +00001138
Jim Laskeyef42a012006-11-02 20:12:39 +00001139 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1140 /// entry.
1141 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1142 DIEntry *Value;
1143
1144 if (Entry) {
1145 FoldingSetNodeID ID;
1146 ID.AddPointer(Entry);
1147 void *Where;
1148 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
1149
1150 if (Value) {
1151 Value->IncUsage();
1152 return Value;
1153 }
1154
1155 Value = new DIEntry(Entry);
1156 ValuesSet.InsertNode(Value, Where);
1157 } else {
1158 Value = new DIEntry(Entry);
1159 }
1160
1161 Values.push_back(Value);
1162 return Value;
1163 }
1164
1165 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1166 ///
1167 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1168 Value->Entry = Entry;
1169 // Add to values set if not already there. If it is, we merely have a
1170 // duplicate in the values list (no harm.)
1171 ValuesSet.GetOrInsertNode(Value);
1172 }
1173
1174 /// AddUInt - Add an unsigned integer attribute data and value.
1175 ///
1176 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1177 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1178
1179 FoldingSetNodeID ID;
1180 ID.AddInteger(Integer);
1181 void *Where;
1182 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1183 if (!Value) {
1184 Value = new DIEInteger(Integer);
1185 ValuesSet.InsertNode(Value, Where);
1186 Values.push_back(Value);
1187 } else {
1188 Value->IncUsage();
1189 }
1190
1191 Die->AddValue(Attribute, Form, Value);
1192 }
1193
1194 /// AddSInt - Add an signed integer attribute data and value.
1195 ///
1196 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1197 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1198
1199 FoldingSetNodeID ID;
1200 ID.AddInteger((uint64_t)Integer);
1201 void *Where;
1202 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1203 if (!Value) {
1204 Value = new DIEInteger(Integer);
1205 ValuesSet.InsertNode(Value, Where);
1206 Values.push_back(Value);
1207 } else {
1208 Value->IncUsage();
1209 }
1210
1211 Die->AddValue(Attribute, Form, Value);
1212 }
1213
1214 /// AddString - Add a std::string attribute data and value.
1215 ///
1216 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1217 const std::string &String) {
1218 FoldingSetNodeID ID;
1219 ID.AddString(String);
1220 void *Where;
1221 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1222 if (!Value) {
1223 Value = new DIEString(String);
1224 ValuesSet.InsertNode(Value, Where);
1225 Values.push_back(Value);
1226 } else {
1227 Value->IncUsage();
1228 }
1229
1230 Die->AddValue(Attribute, Form, Value);
1231 }
1232
1233 /// AddLabel - Add a Dwarf label attribute data and value.
1234 ///
1235 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1236 const DWLabel &Label) {
1237 FoldingSetNodeID ID;
1238 Label.Profile(ID);
1239 void *Where;
1240 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1241 if (!Value) {
1242 Value = new DIEDwarfLabel(Label);
1243 ValuesSet.InsertNode(Value, Where);
1244 Values.push_back(Value);
1245 } else {
1246 Value->IncUsage();
1247 }
1248
1249 Die->AddValue(Attribute, Form, Value);
1250 }
1251
1252 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1253 ///
1254 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1255 const std::string &Label) {
1256 FoldingSetNodeID ID;
1257 ID.AddString(Label);
1258 void *Where;
1259 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1260 if (!Value) {
1261 Value = new DIEObjectLabel(Label);
1262 ValuesSet.InsertNode(Value, Where);
1263 Values.push_back(Value);
1264 } else {
1265 Value->IncUsage();
1266 }
1267
1268 Die->AddValue(Attribute, Form, Value);
1269 }
1270
1271 /// AddDelta - Add a label delta attribute data and value.
1272 ///
1273 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1274 const DWLabel &Hi, const DWLabel &Lo) {
1275 FoldingSetNodeID ID;
1276 Hi.Profile(ID);
1277 Lo.Profile(ID);
1278 void *Where;
1279 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1280 if (!Value) {
1281 Value = new DIEDelta(Hi, Lo);
1282 ValuesSet.InsertNode(Value, Where);
1283 Values.push_back(Value);
1284 } else {
1285 Value->IncUsage();
1286 }
1287
1288 Die->AddValue(Attribute, Form, Value);
1289 }
1290
1291 /// AddDIEntry - Add a DIE attribute data and value.
1292 ///
1293 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1294 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1295 }
1296
1297 /// AddBlock - Add block data.
1298 ///
1299 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1300 Block->ComputeSize(*this);
1301 FoldingSetNodeID ID;
1302 Block->Profile(ID);
1303 void *Where;
1304 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1305 if (!Value) {
1306 Value = Block;
1307 ValuesSet.InsertNode(Value, Where);
1308 Values.push_back(Value);
1309 } else {
1310 Value->IncUsage();
1311 delete Block;
1312 }
1313
1314 Die->AddValue(Attribute, Block->BestForm(), Value);
1315 }
1316
Jim Laskey65195462006-10-30 13:35:07 +00001317private:
1318
1319 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001320 /// entry.
1321 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1322 if (File && Line) {
1323 CompileUnit *FileUnit = FindCompileUnit(File);
1324 unsigned FileID = FileUnit->getID();
1325 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1326 AddUInt(Die, DW_AT_decl_line, 0, Line);
1327 }
1328 }
Jim Laskey65195462006-10-30 13:35:07 +00001329
1330 /// AddAddress - Add an address attribute to a die based on the location
1331 /// provided.
1332 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001333 const MachineLocation &Location) {
1334 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1335 DIEBlock *Block = new DIEBlock();
1336
1337 if (Location.isRegister()) {
1338 if (Reg < 32) {
1339 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1340 } else {
1341 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1342 AddUInt(Block, 0, DW_FORM_udata, Reg);
1343 }
1344 } else {
1345 if (Reg < 32) {
1346 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1347 } else {
1348 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1349 AddUInt(Block, 0, DW_FORM_udata, Reg);
1350 }
1351 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1352 }
1353
1354 AddBlock(Die, Attribute, 0, Block);
1355 }
1356
1357 /// AddBasicType - Add a new basic type attribute to the specified entity.
1358 ///
1359 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1360 const std::string &Name,
1361 unsigned Encoding, unsigned Size) {
1362 DIE *Die = ConstructBasicType(Unit, Name, Encoding, Size);
1363 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1364 }
1365
1366 /// ConstructBasicType - Construct a new basic type.
1367 ///
1368 DIE *ConstructBasicType(CompileUnit *Unit,
1369 const std::string &Name,
1370 unsigned Encoding, unsigned Size) {
1371 DIE Buffer(DW_TAG_base_type);
1372 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1373 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1374 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1375 return Unit->AddDie(Buffer);
1376 }
1377
1378 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1379 ///
1380 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1381 DIE *Die = ConstructPointerType(Unit, Name);
1382 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1383 }
1384
1385 /// ConstructPointerType - Construct a new pointer type.
1386 ///
1387 DIE *ConstructPointerType(CompileUnit *Unit, const std::string &Name) {
1388 DIE Buffer(DW_TAG_pointer_type);
1389 AddUInt(&Buffer, DW_AT_byte_size, 0, TAI->getAddressSize());
1390 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1391 return Unit->AddDie(Buffer);
1392 }
1393
1394 /// AddType - Add a new type attribute to the specified entity.
1395 ///
1396 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1397 if (!TyDesc) {
1398 AddBasicType(Entity, Unit, "", DW_ATE_signed, 4);
1399 } else {
1400 // Check for pre-existence.
1401 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
1402
1403 // If it exists then use the existing value.
1404 if (Slot) {
1405 Slot->IncUsage();
1406 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1407 return;
1408 }
1409
1410 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1411 // FIXME - Not sure why programs and variables are coming through here.
1412 // Short cut for handling subprogram types (not really a TyDesc.)
1413 AddPointerType(Entity, Unit, SubprogramTy->getName());
1414 } else if (GlobalVariableDesc *GlobalTy =
1415 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1416 // FIXME - Not sure why programs and variables are coming through here.
1417 // Short cut for handling global variable types (not really a TyDesc.)
1418 AddPointerType(Entity, Unit, GlobalTy->getName());
1419 } else {
1420 // Set up proxy.
1421 Slot = NewDIEntry();
1422
1423 // Construct type.
1424 DIE Buffer(DW_TAG_base_type);
1425 ConstructType(Buffer, TyDesc, Unit);
1426
1427 // Add debug information entry to entity and unit.
1428 DIE *Die = Unit->AddDie(Buffer);
1429 SetDIEntry(Slot, Die);
1430 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1431 }
1432 }
1433 }
1434
1435 /// ConstructType - Adds all the required attributes to the type.
1436 ///
1437 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1438 // Get core information.
1439 const std::string &Name = TyDesc->getName();
1440 uint64_t Size = TyDesc->getSize() >> 3;
1441
1442 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1443 // Fundamental types like int, float, bool
1444 Buffer.setTag(DW_TAG_base_type);
1445 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1446 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1447 // Pointers, tyepdefs et al.
1448 Buffer.setTag(DerivedTy->getTag());
1449 // Map to main type, void will not have a type.
1450 if (TypeDesc *FromTy = DerivedTy->getFromType())
1451 AddType(&Buffer, FromTy, Unit);
1452 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1453 // Fetch tag.
1454 unsigned Tag = CompTy->getTag();
1455
1456 // Set tag accordingly.
1457 if (Tag == DW_TAG_vector_type)
1458 Buffer.setTag(DW_TAG_array_type);
1459 else
1460 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00001461
Jim Laskeyef42a012006-11-02 20:12:39 +00001462 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1463
1464 switch (Tag) {
1465 case DW_TAG_vector_type:
1466 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1467 // Fall thru
1468 case DW_TAG_array_type: {
1469 // Add element type.
1470 if (TypeDesc *FromTy = CompTy->getFromType())
1471 AddType(&Buffer, FromTy, Unit);
1472
1473 // Don't emit size attribute.
1474 Size = 0;
1475
1476 // Construct an anonymous type for index type.
1477 DIE *IndexTy = ConstructBasicType(Unit, "", DW_ATE_signed, 4);
1478
1479 // Add subranges to array type.
1480 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1481 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1482 int64_t Lo = SRD->getLo();
1483 int64_t Hi = SRD->getHi();
1484 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1485
1486 // If a range is available.
1487 if (Lo != Hi) {
1488 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1489 // Only add low if non-zero.
1490 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1491 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1492 }
1493
1494 Buffer.AddChild(Subrange);
1495 }
1496 break;
1497 }
1498 case DW_TAG_structure_type:
1499 case DW_TAG_union_type: {
1500 // Add elements to structure type.
1501 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1502 DebugInfoDesc *Element = Elements[i];
1503
1504 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1505 // Add field or base class.
1506
1507 unsigned Tag = MemberDesc->getTag();
1508
1509 // Extract the basic information.
1510 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001511 uint64_t Size = MemberDesc->getSize();
1512 uint64_t Align = MemberDesc->getAlign();
1513 uint64_t Offset = MemberDesc->getOffset();
1514
1515 // Construct member debug information entry.
1516 DIE *Member = new DIE(Tag);
1517
1518 // Add name if not "".
1519 if (!Name.empty())
1520 AddString(Member, DW_AT_name, DW_FORM_string, Name);
1521 // Add location if available.
1522 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1523
1524 // Most of the time the field info is the same as the members.
1525 uint64_t FieldSize = Size;
1526 uint64_t FieldAlign = Align;
1527 uint64_t FieldOffset = Offset;
1528
1529 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1530 AddType(Member, FromTy, Unit);
1531 FieldSize = FromTy->getSize();
1532 FieldAlign = FromTy->getSize();
1533 }
1534
1535 // Unless we have a bit field.
1536 if (Tag == DW_TAG_member && FieldSize != Size) {
1537 // Construct the alignment mask.
1538 uint64_t AlignMask = ~(FieldAlign - 1);
1539 // Determine the high bit + 1 of the declared size.
1540 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1541 // Work backwards to determine the base offset of the field.
1542 FieldOffset = HiMark - FieldSize;
1543 // Now normalize offset to the field.
1544 Offset -= FieldOffset;
1545
1546 // Maybe we need to work from the other end.
1547 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1548
1549 // Add size and offset.
1550 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1551 AddUInt(Member, DW_AT_bit_size, 0, Size);
1552 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1553 }
1554
1555 // Add computation for offset.
1556 DIEBlock *Block = new DIEBlock();
1557 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1558 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1559 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1560
1561 // Add accessibility (public default unless is base class.
1562 if (MemberDesc->isProtected()) {
1563 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1564 } else if (MemberDesc->isPrivate()) {
1565 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1566 } else if (Tag == DW_TAG_inheritance) {
1567 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1568 }
1569
1570 Buffer.AddChild(Member);
1571 } else if (GlobalVariableDesc *StaticDesc =
1572 dyn_cast<GlobalVariableDesc>(Element)) {
1573 // Add static member.
1574
1575 // Construct member debug information entry.
1576 DIE *Static = new DIE(DW_TAG_variable);
1577
1578 // Add name and mangled name.
1579 const std::string &Name = StaticDesc->getDisplayName();
1580 const std::string &MangledName = StaticDesc->getName();
1581 AddString(Static, DW_AT_name, DW_FORM_string, Name);
1582 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1583 MangledName);
1584
1585 // Add location.
1586 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1587
1588 // Add type.
1589 if (TypeDesc *StaticTy = StaticDesc->getType())
1590 AddType(Static, StaticTy, Unit);
1591
1592 // Add flags.
1593 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
1594 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
1595
1596 Buffer.AddChild(Static);
1597 } else if (SubprogramDesc *MethodDesc =
1598 dyn_cast<SubprogramDesc>(Element)) {
1599 // Add member function.
1600
1601 // Construct member debug information entry.
1602 DIE *Method = new DIE(DW_TAG_subprogram);
1603
1604 // Add name and mangled name.
1605 const std::string &Name = MethodDesc->getDisplayName();
1606 const std::string &MangledName = MethodDesc->getName();
1607 bool IsCTor = false;
1608
1609 if (Name.empty()) {
1610 AddString(Method, DW_AT_name, DW_FORM_string, MangledName);
1611 IsCTor = TyDesc->getName() == MangledName;
1612 } else {
1613 AddString(Method, DW_AT_name, DW_FORM_string, Name);
1614 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
1615 MangledName);
1616 }
1617
1618 // Add location.
1619 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1620
1621 // Add type.
1622 if (CompositeTypeDesc *MethodTy =
1623 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1624 // Get argument information.
1625 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1626
1627 // If not a ctor.
1628 if (!IsCTor) {
1629 // Add return type.
1630 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1631 }
1632
1633 // Add arguments.
1634 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1635 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1636 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1637 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1638 Method->AddChild(Arg);
1639 }
1640 }
1641
1642 // Add flags.
1643 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
1644 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
1645
1646 Buffer.AddChild(Method);
1647 }
1648 }
1649 break;
1650 }
1651 case DW_TAG_enumeration_type: {
1652 // Add enumerators to enumeration type.
1653 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1654 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1655 const std::string &Name = ED->getName();
1656 int64_t Value = ED->getValue();
1657 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1658 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1659 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1660 Buffer.AddChild(Enumerator);
1661 }
1662
1663 break;
1664 }
1665 case DW_TAG_subroutine_type: {
1666 // Add prototype flag.
1667 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1668 // Add return type.
1669 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
1670
1671 // Add arguments.
1672 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1673 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1674 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1675 Buffer.AddChild(Arg);
1676 }
1677
1678 break;
1679 }
1680 default: break;
1681 }
1682 }
1683
1684 // Add size if non-zero (derived types don't have a size.)
1685 if (Size) AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1686 // Add name if not anonymous or intermediate type.
1687 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1688 // Add source line info if available.
1689 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
1690 }
1691
1692 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00001693 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001694 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1695 // Construct debug information entry.
1696 DIE *Die = new DIE(DW_TAG_compile_unit);
1697 AddDelta(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0),
1698 DWLabel("section_line", 0));
1699 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1700 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1701 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1702 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1703
1704 // Construct compile unit.
1705 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1706
1707 // Add Unit to compile unit map.
1708 DescToUnitMap[UnitDesc] = Unit;
1709
1710 return Unit;
1711 }
1712
Jim Laskey65195462006-10-30 13:35:07 +00001713 /// FindCompileUnit - Get the compile unit for the given descriptor.
1714 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001715 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
1716#if 1
1717 // FIXME - Using only one compile unit. Needs to me fixed at the FE.
1718 CompileUnit *Unit = CompileUnits[0];
1719#else
1720 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1721#endif
1722 assert(Unit && "Missing compile unit.");
1723 return Unit;
1724 }
1725
1726 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00001727 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001728 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1729 // Get the compile unit context.
1730 CompileUnitDesc *UnitDesc =
1731 static_cast<CompileUnitDesc *>(GVD->getContext());
1732 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1733
1734 // Check for pre-existence.
1735 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1736 if (Slot) return Slot;
1737
1738 // Get the global variable itself.
1739 GlobalVariable *GV = GVD->getGlobalVariable();
1740
1741 const std::string &Name = GVD->hasMangledName() ? GVD->getDisplayName()
1742 : GVD->getName();
1743 const std::string &MangledName = GVD->hasMangledName() ? GVD->getName()
1744 : "";
1745 // Create the global's variable DIE.
1746 DIE *VariableDie = new DIE(DW_TAG_variable);
1747 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
1748 if (!MangledName.empty()) {
1749 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1750 MangledName);
1751 }
1752 AddType(VariableDie, GVD->getType(), Unit);
1753 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1754
1755 // Add source line info if available.
1756 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1757
1758 // Work up linkage name.
1759 const std::string LinkageName = Asm->getGlobalLinkName(GV);
1760
1761 // Add address.
1762 DIEBlock *Block = new DIEBlock();
1763 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
1764 AddObjectLabel(Block, 0, DW_FORM_udata, LinkageName);
1765 AddBlock(VariableDie, DW_AT_location, 0, Block);
1766
1767 // Add to map.
1768 Slot = VariableDie;
1769
1770 // Add to context owner.
1771 Unit->getDie()->AddChild(VariableDie);
1772
1773 // Expose as global.
1774 // FIXME - need to check external flag.
1775 Unit->AddGlobal(Name, VariableDie);
1776
1777 return VariableDie;
1778 }
Jim Laskey65195462006-10-30 13:35:07 +00001779
1780 /// NewSubprogram - Add a new subprogram DIE.
1781 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001782 DIE *NewSubprogram(SubprogramDesc *SPD) {
1783 // Get the compile unit context.
1784 CompileUnitDesc *UnitDesc =
1785 static_cast<CompileUnitDesc *>(SPD->getContext());
1786 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1787
1788 // Check for pre-existence.
1789 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1790 if (Slot) return Slot;
1791
1792 // Gather the details (simplify add attribute code.)
1793 const std::string &Name = SPD->hasMangledName() ? SPD->getDisplayName()
1794 : SPD->getName();
1795 const std::string &MangledName = SPD->hasMangledName() ? SPD->getName()
1796 : "";
1797 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
1798
1799 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1800 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
1801 if (!MangledName.empty()) {
1802 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1803 MangledName);
1804 }
1805 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
1806 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, IsExternal);
1807 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
1808
1809 // Add source line info if available.
1810 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1811
1812 // Add to map.
1813 Slot = SubprogramDie;
1814
1815 // Add to context owner.
1816 Unit->getDie()->AddChild(SubprogramDie);
1817
1818 // Expose as global.
1819 Unit->AddGlobal(Name, SubprogramDie);
1820
1821 return SubprogramDie;
1822 }
Jim Laskey65195462006-10-30 13:35:07 +00001823
1824 /// NewScopeVariable - Create a new scope variable.
1825 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001826 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1827 // Get the descriptor.
1828 VariableDesc *VD = DV->getDesc();
1829
1830 // Translate tag to proper Dwarf tag. The result variable is dropped for
1831 // now.
1832 unsigned Tag;
1833 switch (VD->getTag()) {
1834 case DW_TAG_return_variable: return NULL;
1835 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1836 case DW_TAG_auto_variable: // fall thru
1837 default: Tag = DW_TAG_variable; break;
1838 }
1839
1840 // Define variable debug information entry.
1841 DIE *VariableDie = new DIE(Tag);
1842 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1843
1844 // Add source line info if available.
1845 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1846
1847 // Add variable type.
1848 AddType(VariableDie, VD->getType(), Unit);
1849
1850 // Add variable address.
1851 MachineLocation Location;
1852 RI->getLocation(*MF, DV->getFrameIndex(), Location);
1853 AddAddress(VariableDie, DW_AT_location, Location);
1854
1855 return VariableDie;
1856 }
Jim Laskey65195462006-10-30 13:35:07 +00001857
1858 /// ConstructScope - Construct the components of a scope.
1859 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001860 void ConstructScope(DebugScope *ParentScope,
1861 DIE *ParentDie, CompileUnit *Unit) {
1862 // Add variables to scope.
1863 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1864 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1865 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1866 if (VariableDie) ParentDie->AddChild(VariableDie);
1867 }
1868
1869 // Add nested scopes.
1870 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1871 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1872 // Define the Scope debug information entry.
1873 DebugScope *Scope = Scopes[j];
1874 // FIXME - Ignore inlined functions for the time being.
1875 if (!Scope->getParent()) continue;
1876
1877 unsigned StartID = Scope->getStartLabelID();
1878 unsigned EndID = Scope->getEndLabelID();
1879
1880 // Throw out scope if block is discarded.
1881 if (StartID && !DebugInfo->isLabelValid(StartID)) continue;
1882 if (EndID && !DebugInfo->isLabelValid(EndID)) continue;
1883
1884 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1885
1886 // Add the scope bounds.
1887 if (StartID) {
1888 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1889 DWLabel("loc", StartID));
1890 } else {
1891 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1892 DWLabel("func_begin", SubprogramCount));
1893 }
1894 if (EndID) {
1895 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1896 DWLabel("loc", EndID));
1897 } else {
1898 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1899 DWLabel("func_end", SubprogramCount));
1900 }
1901
1902 // Add the scope contents.
1903 ConstructScope(Scope, ScopeDie, Unit);
1904 ParentDie->AddChild(ScopeDie);
1905 }
1906 }
Jim Laskey65195462006-10-30 13:35:07 +00001907
1908 /// ConstructRootScope - Construct the scope for the subprogram.
1909 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001910 void ConstructRootScope(DebugScope *RootScope) {
1911 // Exit if there is no root scope.
1912 if (!RootScope) return;
1913
1914 // Get the subprogram debug information entry.
1915 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1916
1917 // Get the compile unit context.
1918 CompileUnitDesc *UnitDesc =
1919 static_cast<CompileUnitDesc *>(SPD->getContext());
1920 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1921
1922 // Get the subprogram die.
1923 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
1924 assert(SPDie && "Missing subprogram descriptor");
1925
1926 // Add the function bounds.
1927 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1928 DWLabel("func_begin", SubprogramCount));
1929 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1930 DWLabel("func_end", SubprogramCount));
1931 MachineLocation Location(RI->getFrameRegister(*MF));
1932 AddAddress(SPDie, DW_AT_frame_base, Location);
1933
1934 ConstructScope(RootScope, SPDie, Unit);
1935 }
Jim Laskey65195462006-10-30 13:35:07 +00001936
Jim Laskeyef42a012006-11-02 20:12:39 +00001937 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1938 /// tools to recognize the object file contains Dwarf information.
1939 void EmitInitial() {
1940 // Check to see if we already emitted intial headers.
1941 if (didInitial) return;
1942 didInitial = true;
1943
1944 // Dwarf sections base addresses.
1945 if (TAI->getDwarfRequiresFrameSection()) {
1946 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1947 EmitLabel("section_frame", 0);
1948 }
1949 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
1950 EmitLabel("section_info", 0);
1951 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
1952 EmitLabel("section_abbrev", 0);
1953 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
1954 EmitLabel("section_aranges", 0);
1955 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
1956 EmitLabel("section_macinfo", 0);
1957 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
1958 EmitLabel("section_line", 0);
1959 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
1960 EmitLabel("section_loc", 0);
1961 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
1962 EmitLabel("section_pubnames", 0);
1963 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
1964 EmitLabel("section_str", 0);
1965 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
1966 EmitLabel("section_ranges", 0);
1967
1968 Asm->SwitchToTextSection(TAI->getTextSection());
1969 EmitLabel("text_begin", 0);
1970 Asm->SwitchToDataSection(TAI->getDataSection());
1971 EmitLabel("data_begin", 0);
1972
1973 // Emit common frame information.
1974 EmitInitialDebugFrame();
1975 }
1976
Jim Laskey65195462006-10-30 13:35:07 +00001977 /// EmitDIE - Recusively Emits a debug information entry.
1978 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001979 void EmitDIE(DIE *Die) const {
1980 // Get the abbreviation for this DIE.
1981 unsigned AbbrevNumber = Die->getAbbrevNumber();
1982 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1983
1984 O << "\n";
1985
1986 // Emit the code (index) for the abbreviation.
1987 EmitULEB128Bytes(AbbrevNumber);
1988 EOL(std::string("Abbrev [" +
1989 utostr(AbbrevNumber) +
1990 "] 0x" + utohexstr(Die->getOffset()) +
1991 ":0x" + utohexstr(Die->getSize()) + " " +
1992 TagString(Abbrev->getTag())));
1993
1994 const std::vector<DIEValue *> &Values = Die->getValues();
1995 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
1996
1997 // Emit the DIE attribute values.
1998 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1999 unsigned Attr = AbbrevData[i].getAttribute();
2000 unsigned Form = AbbrevData[i].getForm();
2001 assert(Form && "Too many attributes for DIE (check abbreviation)");
2002
2003 switch (Attr) {
2004 case DW_AT_sibling: {
2005 EmitInt32(Die->SiblingOffset());
2006 break;
2007 }
2008 default: {
2009 // Emit an attribute using the defined form.
2010 Values[i]->EmitValue(*this, Form);
2011 break;
2012 }
2013 }
2014
2015 EOL(AttributeString(Attr));
2016 }
2017
2018 // Emit the DIE children if any.
2019 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2020 const std::vector<DIE *> &Children = Die->getChildren();
2021
2022 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2023 EmitDIE(Children[j]);
2024 }
2025
2026 EmitInt8(0); EOL("End Of Children Mark");
2027 }
2028 }
2029
Jim Laskey65195462006-10-30 13:35:07 +00002030 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2031 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002032 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2033 // Get the children.
2034 const std::vector<DIE *> &Children = Die->getChildren();
2035
2036 // If not last sibling and has children then add sibling offset attribute.
2037 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2038
2039 // Record the abbreviation.
2040 AssignAbbrevNumber(Die->getAbbrev());
2041
2042 // Get the abbreviation for this DIE.
2043 unsigned AbbrevNumber = Die->getAbbrevNumber();
2044 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2045
2046 // Set DIE offset
2047 Die->setOffset(Offset);
2048
2049 // Start the size with the size of abbreviation code.
2050 Offset += SizeULEB128(AbbrevNumber);
2051
2052 const std::vector<DIEValue *> &Values = Die->getValues();
2053 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2054
2055 // Size the DIE attribute values.
2056 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2057 // Size attribute value.
2058 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2059 }
2060
2061 // Size the DIE children if any.
2062 if (!Children.empty()) {
2063 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2064 "Children flag not set");
2065
2066 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2067 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2068 }
2069
2070 // End of children marker.
2071 Offset += sizeof(int8_t);
2072 }
2073
2074 Die->setSize(Offset - Die->getOffset());
2075 return Offset;
2076 }
Jim Laskey65195462006-10-30 13:35:07 +00002077
2078 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2079 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002080 void SizeAndOffsets() {
2081 // Process each compile unit.
2082 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2083 CompileUnit *Unit = CompileUnits[i];
2084 if (Unit->hasContent()) {
2085 // Compute size of compile unit header
2086 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2087 sizeof(int16_t) + // DWARF version number
2088 sizeof(int32_t) + // Offset Into Abbrev. Section
2089 sizeof(int8_t); // Pointer Size (in bytes)
2090 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
2091 }
2092 }
2093 }
2094
Jim Laskey65195462006-10-30 13:35:07 +00002095 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
2096 /// frame.
2097 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Jim Laskeyef42a012006-11-02 20:12:39 +00002098 std::vector<MachineMove *> &Moves) {
2099 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
2100 MachineMove *Move = Moves[i];
2101 unsigned LabelID = Move->getLabelID();
2102
2103 // Throw out move if the label is invalid.
2104 if (LabelID && !DebugInfo->isLabelValid(LabelID)) continue;
2105
2106 const MachineLocation &Dst = Move->getDestination();
2107 const MachineLocation &Src = Move->getSource();
2108
2109 // Advance row if new location.
2110 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
2111 EmitInt8(DW_CFA_advance_loc4);
2112 EOL("DW_CFA_advance_loc4");
2113 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
2114 EOL("");
2115
2116 BaseLabelID = LabelID;
2117 BaseLabel = "loc";
2118 }
2119
2120 int stackGrowth =
2121 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2122 TargetFrameInfo::StackGrowsUp ?
2123 TAI->getAddressSize() : -TAI->getAddressSize();
2124
2125 // If advancing cfa.
2126 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
2127 if (!Src.isRegister()) {
2128 if (Src.getRegister() == MachineLocation::VirtualFP) {
2129 EmitInt8(DW_CFA_def_cfa_offset);
2130 EOL("DW_CFA_def_cfa_offset");
2131 } else {
2132 EmitInt8(DW_CFA_def_cfa);
2133 EOL("DW_CFA_def_cfa");
Jim Laskeyef42a012006-11-02 20:12:39 +00002134 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
2135 EOL("Register");
2136 }
2137
2138 int Offset = Src.getOffset() / stackGrowth;
2139
2140 EmitULEB128Bytes(Offset);
2141 EOL("Offset");
2142 } else {
2143 assert(0 && "Machine move no supported yet.");
2144 }
2145 } else {
2146 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
2147 int Offset = Dst.getOffset() / stackGrowth;
2148
2149 if (Offset < 0) {
2150 EmitInt8(DW_CFA_offset_extended_sf);
2151 EOL("DW_CFA_offset_extended_sf");
2152 EmitULEB128Bytes(Reg);
2153 EOL("Reg");
2154 EmitSLEB128Bytes(Offset);
2155 EOL("Offset");
2156 } else if (Reg < 64) {
2157 EmitInt8(DW_CFA_offset + Reg);
2158 EOL("DW_CFA_offset + Reg");
2159 EmitULEB128Bytes(Offset);
2160 EOL("Offset");
2161 } else {
2162 EmitInt8(DW_CFA_offset_extended);
2163 EOL("DW_CFA_offset_extended");
2164 EmitULEB128Bytes(Reg);
2165 EOL("Reg");
2166 EmitULEB128Bytes(Offset);
2167 EOL("Offset");
2168 }
2169 }
2170 }
2171 }
Jim Laskey65195462006-10-30 13:35:07 +00002172
2173 /// EmitDebugInfo - Emit the debug info section.
2174 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002175 void EmitDebugInfo() const {
2176 // Start debug info section.
2177 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2178
2179 // Process each compile unit.
2180 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2181 CompileUnit *Unit = CompileUnits[i];
2182
2183 if (Unit->hasContent()) {
2184 DIE *Die = Unit->getDie();
2185 // Emit the compile units header.
2186 EmitLabel("info_begin", Unit->getID());
2187 // Emit size of content not including length itself
2188 unsigned ContentSize = Die->getSize() +
2189 sizeof(int16_t) + // DWARF version number
2190 sizeof(int32_t) + // Offset Into Abbrev. Section
2191 sizeof(int8_t); // Pointer Size (in bytes)
2192
2193 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2194 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2195 EmitDifference("abbrev_begin", 0, "section_abbrev", 0);
2196 EOL("Offset Into Abbrev. Section");
2197 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
2198
2199 EmitDIE(Die);
2200 EmitLabel("info_end", Unit->getID());
2201 }
2202
2203 O << "\n";
2204 }
2205 }
2206
Jim Laskey65195462006-10-30 13:35:07 +00002207 /// EmitAbbreviations - Emit the abbreviation section.
2208 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002209 void EmitAbbreviations() const {
2210 // Check to see if it is worth the effort.
2211 if (!Abbreviations.empty()) {
2212 // Start the debug abbrev section.
2213 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2214
2215 EmitLabel("abbrev_begin", 0);
2216
2217 // For each abbrevation.
2218 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2219 // Get abbreviation data
2220 const DIEAbbrev *Abbrev = Abbreviations[i];
2221
2222 // Emit the abbrevations code (base 1 index.)
2223 EmitULEB128Bytes(Abbrev->getNumber()); EOL("Abbreviation Code");
2224
2225 // Emit the abbreviations data.
2226 Abbrev->Emit(*this);
2227
2228 O << "\n";
2229 }
2230
2231 EmitLabel("abbrev_end", 0);
2232
2233 O << "\n";
2234 }
2235 }
2236
Jim Laskey65195462006-10-30 13:35:07 +00002237 /// EmitDebugLines - Emit source line information.
2238 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002239 void EmitDebugLines() const {
2240 // Minimum line delta, thus ranging from -10..(255-10).
2241 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2242 // Maximum line delta, thus ranging from -10..(255-10).
2243 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002244
Jim Laskeyef42a012006-11-02 20:12:39 +00002245 // Start the dwarf line section.
2246 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2247
2248 // Construct the section header.
2249
2250 EmitDifference("line_end", 0, "line_begin", 0);
2251 EOL("Length of Source Line Info");
2252 EmitLabel("line_begin", 0);
2253
2254 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2255
2256 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
2257 EOL("Prolog Length");
2258 EmitLabel("line_prolog_begin", 0);
2259
2260 EmitInt8(1); EOL("Minimum Instruction Length");
2261
2262 EmitInt8(1); EOL("Default is_stmt_start flag");
2263
2264 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
2265
2266 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
2267
2268 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
2269
2270 // Line number standard opcode encodings argument count
2271 EmitInt8(0); EOL("DW_LNS_copy arg count");
2272 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2273 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2274 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2275 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2276 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2277 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2278 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2279 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
2280
2281 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2282 const UniqueVector<SourceFileInfo>
2283 &SourceFiles = DebugInfo->getSourceFiles();
2284
2285 // Emit directories.
2286 for (unsigned DirectoryID = 1, NDID = Directories.size();
2287 DirectoryID <= NDID; ++DirectoryID) {
2288 EmitString(Directories[DirectoryID]); EOL("Directory");
2289 }
2290 EmitInt8(0); EOL("End of directories");
2291
2292 // Emit files.
2293 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2294 SourceID <= NSID; ++SourceID) {
2295 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2296 EmitString(SourceFile.getName()); EOL("Source");
2297 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2298 EmitULEB128Bytes(0); EOL("Mod date");
2299 EmitULEB128Bytes(0); EOL("File size");
2300 }
2301 EmitInt8(0); EOL("End of files");
2302
2303 EmitLabel("line_prolog_end", 0);
2304
2305 // A sequence for each text section.
2306 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2307 // Isolate current sections line info.
2308 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
2309
2310 if (DwarfVerbose) {
2311 O << "\t"
2312 << TAI->getCommentString() << " "
2313 << "Section "
2314 << SectionMap[j + 1].c_str() << "\n";
2315 }
2316
2317 // Dwarf assumes we start with first line of first source file.
2318 unsigned Source = 1;
2319 unsigned Line = 1;
2320
2321 // Construct rows of the address, source, line, column matrix.
2322 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2323 const SourceLineInfo &LineInfo = LineInfos[i];
2324 unsigned LabelID = LineInfo.getLabelID();
2325
2326 // Source line labels are validated at the MachineDebugInfo level.
2327
2328 if (DwarfVerbose) {
2329 unsigned SourceID = LineInfo.getSourceID();
2330 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2331 unsigned DirectoryID = SourceFile.getDirectoryID();
2332 O << "\t"
2333 << TAI->getCommentString() << " "
2334 << Directories[DirectoryID]
2335 << SourceFile.getName() << ":"
2336 << LineInfo.getLine() << "\n";
2337 }
2338
2339 // Define the line address.
2340 EmitInt8(0); EOL("Extended Op");
2341 EmitInt8(4 + 1); EOL("Op size");
2342 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2343 EmitReference("loc", LabelID); EOL("Location label");
2344
2345 // If change of source, then switch to the new source.
2346 if (Source != LineInfo.getSourceID()) {
2347 Source = LineInfo.getSourceID();
2348 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2349 EmitULEB128Bytes(Source); EOL("New Source");
2350 }
2351
2352 // If change of line.
2353 if (Line != LineInfo.getLine()) {
2354 // Determine offset.
2355 int Offset = LineInfo.getLine() - Line;
2356 int Delta = Offset - MinLineDelta;
2357
2358 // Update line.
2359 Line = LineInfo.getLine();
2360
2361 // If delta is small enough and in range...
2362 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2363 // ... then use fast opcode.
2364 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2365 } else {
2366 // ... otherwise use long hand.
2367 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2368 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2369 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2370 }
2371 } else {
2372 // Copy the previous row (different address or source)
2373 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2374 }
2375 }
2376
2377 // Define last address of section.
2378 EmitInt8(0); EOL("Extended Op");
2379 EmitInt8(4 + 1); EOL("Op size");
2380 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2381 EmitReference("section_end", j + 1); EOL("Section end label");
2382
2383 // Mark end of matrix.
2384 EmitInt8(0); EOL("DW_LNE_end_sequence");
2385 EmitULEB128Bytes(1); O << "\n";
2386 EmitInt8(1); O << "\n";
2387 }
2388
2389 EmitLabel("line_end", 0);
2390
2391 O << "\n";
2392 }
2393
Jim Laskey65195462006-10-30 13:35:07 +00002394 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
2395 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002396 void EmitInitialDebugFrame() {
2397 if (!TAI->getDwarfRequiresFrameSection())
2398 return;
2399
2400 int stackGrowth =
2401 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2402 TargetFrameInfo::StackGrowsUp ?
2403 TAI->getAddressSize() : -TAI->getAddressSize();
2404
2405 // Start the dwarf frame section.
2406 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2407
2408 EmitLabel("frame_common", 0);
2409 EmitDifference("frame_common_end", 0,
2410 "frame_common_begin", 0);
2411 EOL("Length of Common Information Entry");
2412
2413 EmitLabel("frame_common_begin", 0);
2414 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2415 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2416 EmitString(""); EOL("CIE Augmentation");
2417 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
2418 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
2419 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
Jim Laskey65195462006-10-30 13:35:07 +00002420
Jim Laskeyef42a012006-11-02 20:12:39 +00002421 std::vector<MachineMove *> Moves;
2422 RI->getInitialFrameState(Moves);
2423 EmitFrameMoves(NULL, 0, Moves);
2424 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2425
2426 EmitAlign(2);
2427 EmitLabel("frame_common_end", 0);
2428
2429 O << "\n";
2430 }
2431
Jim Laskey65195462006-10-30 13:35:07 +00002432 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2433 /// section.
Jim Laskeyef42a012006-11-02 20:12:39 +00002434 void EmitFunctionDebugFrame() {
2435 // Start the dwarf frame section.
2436 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2437
2438 EmitDifference("frame_end", SubprogramCount,
2439 "frame_begin", SubprogramCount);
2440 EOL("Length of Frame Information Entry");
2441
2442 EmitLabel("frame_begin", SubprogramCount);
2443
2444 EmitDifference("frame_common", 0, "section_frame", 0);
2445 EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002446
Jim Laskeyef42a012006-11-02 20:12:39 +00002447 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2448 EmitDifference("func_end", SubprogramCount,
2449 "func_begin", SubprogramCount);
2450 EOL("FDE address range");
2451
2452 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2453
2454 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2455
2456 EmitAlign(2);
2457 EmitLabel("frame_end", SubprogramCount);
2458
2459 O << "\n";
2460 }
2461
2462 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002463 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002464 void EmitDebugPubNames() {
2465 // Start the dwarf pubnames section.
2466 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2467
2468 // Process each compile unit.
2469 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2470 CompileUnit *Unit = CompileUnits[i];
2471
2472 if (Unit->hasContent()) {
2473 EmitDifference("pubnames_end", Unit->getID(),
2474 "pubnames_begin", Unit->getID());
2475 EOL("Length of Public Names Info");
2476
2477 EmitLabel("pubnames_begin", Unit->getID());
2478
2479 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2480
2481 EmitDifference("info_begin", Unit->getID(), "section_info", 0);
2482 EOL("Offset of Compilation Unit Info");
2483
2484 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2485 EOL("Compilation Unit Length");
2486
2487 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2488
2489 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2490 GE = Globals.end();
2491 GI != GE; ++GI) {
2492 const std::string &Name = GI->first;
2493 DIE * Entity = GI->second;
2494
2495 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2496 EmitString(Name); EOL("External Name");
2497 }
2498
2499 EmitInt32(0); EOL("End Mark");
2500 EmitLabel("pubnames_end", Unit->getID());
2501
2502 O << "\n";
2503 }
2504 }
2505 }
2506
2507 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002508 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002509 void EmitDebugStr() {
2510 // Check to see if it is worth the effort.
2511 if (!StringPool.empty()) {
2512 // Start the dwarf str section.
2513 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2514
2515 // For each of strings in the string pool.
2516 for (unsigned StringID = 1, N = StringPool.size();
2517 StringID <= N; ++StringID) {
2518 // Emit a label for reference from debug information entries.
2519 EmitLabel("string", StringID);
2520 // Emit the string itself.
2521 const std::string &String = StringPool[StringID];
2522 EmitString(String); O << "\n";
2523 }
2524
2525 O << "\n";
2526 }
2527 }
2528
2529 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002530 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002531 void EmitDebugLoc() {
2532 // Start the dwarf loc section.
2533 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2534
2535 O << "\n";
2536 }
2537
2538 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002539 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002540 void EmitDebugARanges() {
2541 // Start the dwarf aranges section.
2542 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2543
2544 // FIXME - Mock up
2545 #if 0
2546 // Process each compile unit.
2547 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2548 CompileUnit *Unit = CompileUnits[i];
2549
2550 if (Unit->hasContent()) {
2551 // Don't include size of length
2552 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2553
2554 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2555
2556 EmitReference("info_begin", Unit->getID());
2557 EOL("Offset of Compilation Unit Info");
2558
2559 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
2560
2561 EmitInt8(0); EOL("Size of Segment Descriptor");
2562
2563 EmitInt16(0); EOL("Pad (1)");
2564 EmitInt16(0); EOL("Pad (2)");
2565
2566 // Range 1
2567 EmitReference("text_begin", 0); EOL("Address");
2568 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
2569
2570 EmitInt32(0); EOL("EOM (1)");
2571 EmitInt32(0); EOL("EOM (2)");
2572
2573 O << "\n";
2574 }
2575 }
2576 #endif
2577 }
2578
2579 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002580 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002581 void EmitDebugRanges() {
2582 // Start the dwarf ranges section.
2583 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2584
2585 O << "\n";
2586 }
2587
2588 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002589 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002590 void EmitDebugMacInfo() {
2591 // Start the dwarf macinfo section.
2592 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2593
2594 O << "\n";
2595 }
2596
Jim Laskey65195462006-10-30 13:35:07 +00002597 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2598 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002599 void ConstructCompileUnitDIEs() {
2600 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
2601
2602 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
2603 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
2604 CompileUnits.push_back(Unit);
2605 }
2606 }
2607
Jim Laskey65195462006-10-30 13:35:07 +00002608 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2609 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002610 void ConstructGlobalDIEs() {
2611 std::vector<GlobalVariableDesc *> GlobalVariables =
2612 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
2613
2614 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2615 GlobalVariableDesc *GVD = GlobalVariables[i];
2616 NewGlobalVariable(GVD);
2617 }
2618 }
Jim Laskey65195462006-10-30 13:35:07 +00002619
2620 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2621 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002622 void ConstructSubprogramDIEs() {
2623 std::vector<SubprogramDesc *> Subprograms =
2624 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
2625
2626 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2627 SubprogramDesc *SPD = Subprograms[i];
2628 NewSubprogram(SPD);
2629 }
2630 }
Jim Laskey65195462006-10-30 13:35:07 +00002631
2632 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
2633 ///
2634 bool ShouldEmitDwarf() const { return shouldEmit; }
2635
2636public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002637 //===--------------------------------------------------------------------===//
2638 // Main entry points.
2639 //
2640 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
2641 : O(OS)
2642 , Asm(A)
2643 , TAI(T)
2644 , TD(Asm->TM.getTargetData())
2645 , RI(Asm->TM.getRegisterInfo())
2646 , M(NULL)
2647 , MF(NULL)
2648 , DebugInfo(NULL)
2649 , didInitial(false)
2650 , shouldEmit(false)
2651 , SubprogramCount(0)
2652 , CompileUnits()
2653 , AbbreviationsSet(InitAbbreviationsSetSize)
2654 , Abbreviations()
2655 , ValuesSet(InitValuesSetSize)
2656 , Values()
2657 , StringPool()
2658 , DescToUnitMap()
2659 , SectionMap()
2660 , SectionSourceLines()
2661 {
2662 }
2663 virtual ~Dwarf() {
2664 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2665 delete CompileUnits[i];
2666 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2667 delete Values[j];
2668 }
2669
Jim Laskey65195462006-10-30 13:35:07 +00002670 // Accessors.
2671 //
2672 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
2673
2674 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2675 /// created it. Set by the target AsmPrinter.
Jim Laskeyef42a012006-11-02 20:12:39 +00002676 void SetDebugInfo(MachineDebugInfo *DI) {
2677 // Make sure initial declarations are made.
2678 if (!DebugInfo && DI->hasInfo()) {
2679 DebugInfo = DI;
2680 shouldEmit = true;
2681
2682 // Emit initial sections
2683 EmitInitial();
2684
2685 // Create all the compile unit DIEs.
2686 ConstructCompileUnitDIEs();
2687
2688 // Create DIEs for each of the externally visible global variables.
2689 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002690
Jim Laskeyef42a012006-11-02 20:12:39 +00002691 // Create DIEs for each of the externally visible subprograms.
2692 ConstructSubprogramDIEs();
2693
2694 // Prime section data.
2695 SectionMap.insert(std::string("\t") + TAI->getTextSection());
2696 }
2697 }
2698
Jim Laskey65195462006-10-30 13:35:07 +00002699 /// BeginModule - Emit all Dwarf sections that should come prior to the
2700 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002701 void BeginModule(Module *M) {
2702 this->M = M;
2703
2704 if (!ShouldEmitDwarf()) return;
2705 EOL("Dwarf Begin Module");
2706 }
2707
Jim Laskey65195462006-10-30 13:35:07 +00002708 /// EndModule - Emit all Dwarf sections that should come after the content.
2709 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002710 void EndModule() {
2711 if (!ShouldEmitDwarf()) return;
2712 EOL("Dwarf End Module");
2713
2714 // Standard sections final addresses.
2715 Asm->SwitchToTextSection(TAI->getTextSection());
2716 EmitLabel("text_end", 0);
2717 Asm->SwitchToDataSection(TAI->getDataSection());
2718 EmitLabel("data_end", 0);
2719
2720 // End text sections.
2721 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2722 Asm->SwitchToTextSection(SectionMap[i].c_str());
2723 EmitLabel("section_end", i);
2724 }
2725
2726 // Compute DIE offsets and sizes.
2727 SizeAndOffsets();
2728
2729 // Emit all the DIEs into a debug info section
2730 EmitDebugInfo();
2731
2732 // Corresponding abbreviations into a abbrev section.
2733 EmitAbbreviations();
2734
2735 // Emit source line correspondence into a debug line section.
2736 EmitDebugLines();
2737
2738 // Emit info into a debug pubnames section.
2739 EmitDebugPubNames();
2740
2741 // Emit info into a debug str section.
2742 EmitDebugStr();
2743
2744 // Emit info into a debug loc section.
2745 EmitDebugLoc();
2746
2747 // Emit info into a debug aranges section.
2748 EmitDebugARanges();
2749
2750 // Emit info into a debug ranges section.
2751 EmitDebugRanges();
2752
2753 // Emit info into a debug macinfo section.
2754 EmitDebugMacInfo();
2755 }
2756
Jim Laskey65195462006-10-30 13:35:07 +00002757 /// BeginFunction - Gather pre-function debug information. Assumes being
2758 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002759 void BeginFunction(MachineFunction *MF) {
2760 this->MF = MF;
2761
2762 if (!ShouldEmitDwarf()) return;
2763 EOL("Dwarf Begin Function");
2764
2765 // Begin accumulating function debug information.
2766 DebugInfo->BeginFunction(MF);
2767
2768 // Assumes in correct section after the entry point.
2769 EmitLabel("func_begin", ++SubprogramCount);
2770 }
2771
Jim Laskey65195462006-10-30 13:35:07 +00002772 /// EndFunction - Gather and emit post-function debug information.
2773 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002774 void EndFunction() {
2775 if (!ShouldEmitDwarf()) return;
2776 EOL("Dwarf End Function");
2777
2778 // Define end label for subprogram.
2779 EmitLabel("func_end", SubprogramCount);
2780
2781 // Get function line info.
2782 const std::vector<SourceLineInfo> &LineInfos = DebugInfo->getSourceLines();
2783
2784 if (!LineInfos.empty()) {
2785 // Get section line info.
2786 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2787 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2788 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2789 // Append the function info to section info.
2790 SectionLineInfos.insert(SectionLineInfos.end(),
2791 LineInfos.begin(), LineInfos.end());
2792 }
2793
2794 // Construct scopes for subprogram.
2795 ConstructRootScope(DebugInfo->getRootScope());
2796
2797 // Emit function frame information.
2798 EmitFunctionDebugFrame();
2799
2800 // Reset the line numbers for the next function.
2801 DebugInfo->ClearLineInfo();
2802
2803 // Clear function debug information.
2804 DebugInfo->EndFunction();
2805 }
Jim Laskey65195462006-10-30 13:35:07 +00002806};
2807
Jim Laskey0d086af2006-02-27 12:43:29 +00002808} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00002809
2810//===----------------------------------------------------------------------===//
2811
Jim Laskeyd18e2892006-01-20 20:34:06 +00002812/// Emit - Print the abbreviation using the specified Dwarf writer.
2813///
Jim Laskey65195462006-10-30 13:35:07 +00002814void DIEAbbrev::Emit(const Dwarf &DW) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002815 // Emit its Dwarf tag type.
2816 DW.EmitULEB128Bytes(Tag);
2817 DW.EOL(TagString(Tag));
2818
2819 // Emit whether it has children DIEs.
2820 DW.EmitULEB128Bytes(ChildrenFlag);
2821 DW.EOL(ChildrenString(ChildrenFlag));
2822
2823 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00002824 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002825 const DIEAbbrevData &AttrData = Data[i];
2826
2827 // Emit attribute type.
2828 DW.EmitULEB128Bytes(AttrData.getAttribute());
2829 DW.EOL(AttributeString(AttrData.getAttribute()));
2830
2831 // Emit form type.
2832 DW.EmitULEB128Bytes(AttrData.getForm());
2833 DW.EOL(FormEncodingString(AttrData.getForm()));
2834 }
2835
2836 // Mark end of abbreviation.
2837 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
2838 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
2839}
2840
2841#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00002842void DIEAbbrev::print(std::ostream &O) {
2843 O << "Abbreviation @"
2844 << std::hex << (intptr_t)this << std::dec
2845 << " "
2846 << TagString(Tag)
2847 << " "
2848 << ChildrenString(ChildrenFlag)
2849 << "\n";
2850
2851 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
2852 O << " "
2853 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002854 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00002855 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002856 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002857 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00002858}
2859void DIEAbbrev::dump() { print(std::cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002860#endif
2861
2862//===----------------------------------------------------------------------===//
2863
Jim Laskeyef42a012006-11-02 20:12:39 +00002864#ifndef NDEBUG
2865void DIEValue::dump() {
2866 print(std::cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002867}
Jim Laskeyef42a012006-11-02 20:12:39 +00002868#endif
2869
2870//===----------------------------------------------------------------------===//
2871
Jim Laskey063e7652006-01-17 17:31:53 +00002872/// EmitValue - Emit integer of appropriate size.
2873///
Jim Laskey65195462006-10-30 13:35:07 +00002874void DIEInteger::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +00002875 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00002876 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00002877 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002878 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002879 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002880 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002881 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002882 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002883 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +00002884 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +00002885 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
2886 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +00002887 default: assert(0 && "DIE Value form not supported yet"); break;
2888 }
2889}
2890
Jim Laskey063e7652006-01-17 17:31:53 +00002891//===----------------------------------------------------------------------===//
2892
2893/// EmitValue - Emit string value.
2894///
Jim Laskey65195462006-10-30 13:35:07 +00002895void DIEString::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002896 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00002897}
2898
Jim Laskey063e7652006-01-17 17:31:53 +00002899//===----------------------------------------------------------------------===//
2900
2901/// EmitValue - Emit label value.
2902///
Jim Laskey65195462006-10-30 13:35:07 +00002903void DIEDwarfLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002904 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +00002905}
2906
2907/// SizeOf - Determine size of label value in bytes.
2908///
Jim Laskey65195462006-10-30 13:35:07 +00002909unsigned DIEDwarfLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002910 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002911}
Jim Laskeyef42a012006-11-02 20:12:39 +00002912
Jim Laskey063e7652006-01-17 17:31:53 +00002913//===----------------------------------------------------------------------===//
2914
Jim Laskeyd18e2892006-01-20 20:34:06 +00002915/// EmitValue - Emit label value.
2916///
Jim Laskey65195462006-10-30 13:35:07 +00002917void DIEObjectLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002918 DW.EmitReference(Label);
2919}
2920
2921/// SizeOf - Determine size of label value in bytes.
2922///
Jim Laskey65195462006-10-30 13:35:07 +00002923unsigned DIEObjectLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002924 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00002925}
2926
2927//===----------------------------------------------------------------------===//
2928
Jim Laskey063e7652006-01-17 17:31:53 +00002929/// EmitValue - Emit delta value.
2930///
Jim Laskey65195462006-10-30 13:35:07 +00002931void DIEDelta::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002932 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +00002933}
2934
2935/// SizeOf - Determine size of delta value in bytes.
2936///
Jim Laskey65195462006-10-30 13:35:07 +00002937unsigned DIEDelta::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002938 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002939}
2940
2941//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00002942
Jim Laskeyb8509c52006-03-23 18:07:55 +00002943/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002944///
Jim Laskey65195462006-10-30 13:35:07 +00002945void DIEntry::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +00002946 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00002947}
Jim Laskeyd18e2892006-01-20 20:34:06 +00002948
2949//===----------------------------------------------------------------------===//
2950
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002951/// ComputeSize - calculate the size of the block.
2952///
Jim Laskey65195462006-10-30 13:35:07 +00002953unsigned DIEBlock::ComputeSize(Dwarf &DW) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002954 if (!Size) {
2955 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2956
2957 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2958 Size += Values[i]->SizeOf(DW, AbbrevData[i].getForm());
2959 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002960 }
2961 return Size;
2962}
2963
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002964/// EmitValue - Emit block data.
2965///
Jim Laskey65195462006-10-30 13:35:07 +00002966void DIEBlock::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002967 switch (Form) {
2968 case DW_FORM_block1: DW.EmitInt8(Size); break;
2969 case DW_FORM_block2: DW.EmitInt16(Size); break;
2970 case DW_FORM_block4: DW.EmitInt32(Size); break;
2971 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
2972 default: assert(0 && "Improper form for block"); break;
2973 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002974
2975 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2976
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002977 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2978 DW.EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002979 Values[i]->EmitValue(DW, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002980 }
2981}
2982
2983/// SizeOf - Determine size of block data in bytes.
2984///
Jim Laskey65195462006-10-30 13:35:07 +00002985unsigned DIEBlock::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002986 switch (Form) {
2987 case DW_FORM_block1: return Size + sizeof(int8_t);
2988 case DW_FORM_block2: return Size + sizeof(int16_t);
2989 case DW_FORM_block4: return Size + sizeof(int32_t);
Jim Laskeyef42a012006-11-02 20:12:39 +00002990 case DW_FORM_block: return Size + SizeULEB128(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002991 default: assert(0 && "Improper form for block"); break;
2992 }
2993 return 0;
2994}
2995
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002996//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00002997/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00002998
2999DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003000 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00003001 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00003002}
Jim Laskeyef42a012006-11-02 20:12:39 +00003003
Jim Laskeyb8509c52006-03-23 18:07:55 +00003004/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
3005///
3006void DIE::AddSiblingOffset() {
3007 DIEInteger *DI = new DIEInteger(0);
3008 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00003009 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00003010}
3011
Jim Laskeyef42a012006-11-02 20:12:39 +00003012/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003013///
Jim Laskeyef42a012006-11-02 20:12:39 +00003014void DIE::Profile(FoldingSetNodeID &ID) {
3015 Abbrev.Profile(ID);
3016
3017 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3018 ID.AddPointer(Children[i]);
3019
3020 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3021 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003022}
Jim Laskeyef42a012006-11-02 20:12:39 +00003023
3024#ifndef NDEBUG
3025void DIE::print(std::ostream &O, unsigned IncIndent) {
3026 static unsigned IndentCount = 0;
3027 IndentCount += IncIndent;
3028 const std::string Indent(IndentCount, ' ');
3029 bool isBlock = Abbrev.getTag() == 0;
3030
3031 if (!isBlock) {
3032 O << Indent
3033 << "Die: "
3034 << "0x" << std::hex << (intptr_t)this << std::dec
3035 << ", Offset: " << Offset
3036 << ", Size: " << Size
3037 << "\n";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003038
Jim Laskeyef42a012006-11-02 20:12:39 +00003039 O << Indent
3040 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00003041 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00003042 << ChildrenString(Abbrev.getChildrenFlag());
3043 } else {
3044 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00003045 }
3046 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003047
Jim Laskeyef42a012006-11-02 20:12:39 +00003048 const std::vector<DIEAbbrevData> &Data = Abbrev.getData();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003049
Jim Laskeyef42a012006-11-02 20:12:39 +00003050 IndentCount += 2;
3051 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3052 O << Indent;
3053 if (!isBlock) {
3054 O << AttributeString(Data[i].getAttribute());
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003055 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00003056 O << "Blk[" << i << "]";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003057 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003058 O << " "
3059 << FormEncodingString(Data[i].getForm())
3060 << " ";
3061 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00003062 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00003063 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003064 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00003065
Jim Laskeyef42a012006-11-02 20:12:39 +00003066 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3067 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00003068 }
Jim Laskey063e7652006-01-17 17:31:53 +00003069
Jim Laskeyef42a012006-11-02 20:12:39 +00003070 if (!isBlock) O << "\n";
3071 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00003072}
3073
Jim Laskeyef42a012006-11-02 20:12:39 +00003074void DIE::dump() {
3075 print(std::cerr);
Jim Laskey41886992006-04-07 16:34:46 +00003076}
Jim Laskeybd761842006-02-27 17:27:12 +00003077#endif
Jim Laskey65195462006-10-30 13:35:07 +00003078
3079//===----------------------------------------------------------------------===//
3080/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00003081///
Jim Laskey65195462006-10-30 13:35:07 +00003082
3083DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3084 const TargetAsmInfo *T) {
3085 DW = new Dwarf(OS, A, T);
3086}
3087
3088DwarfWriter::~DwarfWriter() {
3089 delete DW;
3090}
3091
3092/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
3093/// created it. Set by the target AsmPrinter.
3094void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
3095 DW->SetDebugInfo(DI);
3096}
3097
3098/// BeginModule - Emit all Dwarf sections that should come prior to the
3099/// content.
3100void DwarfWriter::BeginModule(Module *M) {
3101 DW->BeginModule(M);
3102}
3103
3104/// EndModule - Emit all Dwarf sections that should come after the content.
3105///
3106void DwarfWriter::EndModule() {
3107 DW->EndModule();
3108}
3109
3110/// BeginFunction - Gather pre-function debug information. Assumes being
3111/// emitted immediately after the function entry point.
3112void DwarfWriter::BeginFunction(MachineFunction *MF) {
3113 DW->BeginFunction(MF);
3114}
3115
3116/// EndFunction - Gather and emit post-function debug information.
3117///
3118void DwarfWriter::EndFunction() {
3119 DW->EndFunction();
3120}