blob: 15386048058e1358fdc472345e9a4651f51967cf [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"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000034#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000035#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000036using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000037using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000038
Jim Laskey0d086af2006-02-27 12:43:29 +000039namespace llvm {
Jim Laskey65195462006-10-30 13:35:07 +000040
41//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000042
43/// Configuration values for initial hash set sizes (log2).
44///
45static const unsigned InitDiesSetSize = 9; // 512
46static const unsigned InitAbbreviationsSetSize = 9; // 512
47static const unsigned InitValuesSetSize = 9; // 512
48
49//===----------------------------------------------------------------------===//
50/// Forward declarations.
51///
52class DIE;
53class DIEValue;
54
55//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000056/// DWLabel - Labels are used to track locations in the assembler file.
57/// Labels appear in the form <prefix>debug_<Tag><Number>, where the tag is a
58/// category of label (Ex. location) and number is a value unique in that
59/// category.
Jim Laskey65195462006-10-30 13:35:07 +000060class DWLabel {
61public:
Jim Laskeyef42a012006-11-02 20:12:39 +000062 /// Tag - Label category tag. Should always be a staticly declared C string.
63 ///
64 const char *Tag;
65
66 /// Number - Value to make label unique.
67 ///
68 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +000069
70 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Jim Laskeybd761842006-02-27 17:27:12 +000071
Jim Laskeyef42a012006-11-02 20:12:39 +000072 void Profile(FoldingSetNodeID &ID) const {
73 ID.AddString(std::string(Tag));
74 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +000075 }
Jim Laskeyef42a012006-11-02 20:12:39 +000076
77#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +000078 void print(std::ostream *O) const {
79 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +000080 }
Jim Laskeyef42a012006-11-02 20:12:39 +000081 void print(std::ostream &O) const {
82 O << ".debug_" << Tag;
83 if (Number) O << Number;
84 }
85#endif
Jim Laskeybd761842006-02-27 17:27:12 +000086};
87
88//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000089/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
90/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +000091class DIEAbbrevData {
92private:
Jim Laskeyef42a012006-11-02 20:12:39 +000093 /// Attribute - Dwarf attribute code.
94 ///
95 unsigned Attribute;
96
97 /// Form - Dwarf form code.
98 ///
99 unsigned Form;
Jim Laskey0d086af2006-02-27 12:43:29 +0000100
101public:
102 DIEAbbrevData(unsigned A, unsigned F)
103 : Attribute(A)
104 , Form(F)
105 {}
106
Jim Laskeybd761842006-02-27 17:27:12 +0000107 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000108 unsigned getAttribute() const { return Attribute; }
109 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000110
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000111 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000112 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000113 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000114 ID.AddInteger(Attribute);
115 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000116 }
117};
Jim Laskey063e7652006-01-17 17:31:53 +0000118
Jim Laskey0d086af2006-02-27 12:43:29 +0000119//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000120/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
121/// information object.
122class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000123private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000124 /// Tag - Dwarf tag code.
125 ///
126 unsigned Tag;
127
128 /// Unique number for node.
129 ///
130 unsigned Number;
131
132 /// ChildrenFlag - Dwarf children flag.
133 ///
134 unsigned ChildrenFlag;
135
136 /// Data - Raw data bytes for abbreviation.
137 ///
138 std::vector<DIEAbbrevData> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000139
Jim Laskey0d086af2006-02-27 12:43:29 +0000140public:
Jim Laskey063e7652006-01-17 17:31:53 +0000141
Jim Laskey0d086af2006-02-27 12:43:29 +0000142 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000143 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000144 , ChildrenFlag(C)
145 , Data()
146 {}
147 ~DIEAbbrev() {}
148
Jim Laskeybd761842006-02-27 17:27:12 +0000149 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000150 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000151 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000152 unsigned getChildrenFlag() const { return ChildrenFlag; }
153 const std::vector<DIEAbbrevData> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000154 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000155 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000156 void setNumber(unsigned N) { Number = N; }
157
Jim Laskey0d086af2006-02-27 12:43:29 +0000158 /// AddAttribute - Adds another set of attribute information to the
159 /// abbreviation.
160 void AddAttribute(unsigned Attribute, unsigned Form) {
161 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000162 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000163
Jim Laskeyb8509c52006-03-23 18:07:55 +0000164 /// AddFirstAttribute - Adds a set of attribute information to the front
165 /// of the abbreviation.
166 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
167 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
168 }
169
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000170 /// Profile - Used to gather unique data for the abbreviation folding set.
171 ///
172 void Profile(FoldingSetNodeID &ID) {
173 ID.AddInteger(Tag);
174 ID.AddInteger(ChildrenFlag);
175
176 // For each attribute description.
177 for (unsigned i = 0, N = Data.size(); i < N; ++i)
178 Data[i].Profile(ID);
179 }
180
Jim Laskey0d086af2006-02-27 12:43:29 +0000181 /// Emit - Print the abbreviation using the specified Dwarf writer.
182 ///
Jim Laskey65195462006-10-30 13:35:07 +0000183 void Emit(const Dwarf &DW) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000184
185#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000186 void print(std::ostream *O) {
187 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000188 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000189 void print(std::ostream &O);
190 void dump();
191#endif
192};
Jim Laskey063e7652006-01-17 17:31:53 +0000193
Jim Laskey0d086af2006-02-27 12:43:29 +0000194//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000195/// DIE - A structured debug information entry. Has an abbreviation which
196/// describes it's organization.
197class DIE : public FoldingSetNode {
198protected:
199 /// Abbrev - Buffer for constructing abbreviation.
200 ///
201 DIEAbbrev Abbrev;
202
203 /// Offset - Offset in debug info section.
204 ///
205 unsigned Offset;
206
207 /// Size - Size of instance + children.
208 ///
209 unsigned Size;
210
211 /// Children DIEs.
212 ///
213 std::vector<DIE *> Children;
214
215 /// Attributes values.
216 ///
217 std::vector<DIEValue *> Values;
218
219public:
220 DIE(unsigned Tag)
221 : Abbrev(Tag, DW_CHILDREN_no)
222 , Offset(0)
223 , Size(0)
224 , Children()
225 , Values()
226 {}
227 virtual ~DIE();
228
229 // Accessors.
230 DIEAbbrev &getAbbrev() { return Abbrev; }
231 unsigned getAbbrevNumber() const {
232 return Abbrev.getNumber();
233 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000234 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000235 unsigned getOffset() const { return Offset; }
236 unsigned getSize() const { return Size; }
237 const std::vector<DIE *> &getChildren() const { return Children; }
238 const std::vector<DIEValue *> &getValues() const { return Values; }
239 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
240 void setOffset(unsigned O) { Offset = O; }
241 void setSize(unsigned S) { Size = S; }
242
243 /// AddValue - Add a value and attributes to a DIE.
244 ///
245 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
246 Abbrev.AddAttribute(Attribute, Form);
247 Values.push_back(Value);
248 }
249
250 /// SiblingOffset - Return the offset of the debug information entry's
251 /// sibling.
252 unsigned SiblingOffset() const { return Offset + Size; }
253
254 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
255 ///
256 void AddSiblingOffset();
257
258 /// AddChild - Add a child to the DIE.
259 ///
260 void AddChild(DIE *Child) {
261 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
262 Children.push_back(Child);
263 }
264
265 /// Detach - Detaches objects connected to it after copying.
266 ///
267 void Detach() {
268 Children.clear();
269 }
270
271 /// Profile - Used to gather unique data for the value folding set.
272 ///
273 void Profile(FoldingSetNodeID &ID) ;
274
275#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000276 void print(std::ostream *O, unsigned IncIndent = 0) {
277 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000278 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000279 void print(std::ostream &O, unsigned IncIndent = 0);
280 void dump();
281#endif
282};
283
284//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000285/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000286///
287class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000288public:
289 enum {
290 isInteger,
291 isString,
292 isLabel,
293 isAsIsLabel,
294 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000295 isEntry,
296 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000297 };
298
Jim Laskeyef42a012006-11-02 20:12:39 +0000299 /// Type - Type of data stored in the value.
300 ///
301 unsigned Type;
Jim Laskey0d086af2006-02-27 12:43:29 +0000302
Jim Laskeyef42a012006-11-02 20:12:39 +0000303 DIEValue(unsigned T)
304 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000305 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000306 virtual ~DIEValue() {}
307
Jim Laskeyf6733882006-11-02 21:48:18 +0000308 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000309 unsigned getType() const { return Type; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000310
Jim Laskey0d086af2006-02-27 12:43:29 +0000311 // Implement isa/cast/dyncast.
312 static bool classof(const DIEValue *) { return true; }
313
314 /// EmitValue - Emit value via the Dwarf writer.
315 ///
Jim Laskey65195462006-10-30 13:35:07 +0000316 virtual void EmitValue(const Dwarf &DW, unsigned Form) const = 0;
Jim Laskey0d086af2006-02-27 12:43:29 +0000317
318 /// SizeOf - Return the size of a value in bytes.
319 ///
Jim Laskey65195462006-10-30 13:35:07 +0000320 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const = 0;
Jim Laskeyef42a012006-11-02 20:12:39 +0000321
322 /// Profile - Used to gather unique data for the value folding set.
323 ///
324 virtual void Profile(FoldingSetNodeID &ID) = 0;
325
326#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000327 void print(std::ostream *O) {
328 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000329 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000330 virtual void print(std::ostream &O) = 0;
331 void dump();
332#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000333};
Jim Laskey063e7652006-01-17 17:31:53 +0000334
Jim Laskey0d086af2006-02-27 12:43:29 +0000335//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000336/// DWInteger - An integer value DIE.
337///
Jim Laskey0d086af2006-02-27 12:43:29 +0000338class DIEInteger : public DIEValue {
339private:
340 uint64_t Integer;
341
342public:
343 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000344
Jim Laskey0d086af2006-02-27 12:43:29 +0000345 // Implement isa/cast/dyncast.
346 static bool classof(const DIEInteger *) { return true; }
347 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
348
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000349 /// BestForm - Choose the best form for integer.
350 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000351 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
352 if (IsSigned) {
353 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
354 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
355 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
356 } else {
357 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
358 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
359 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
360 }
361 return DW_FORM_data8;
362 }
363
Jim Laskey0d086af2006-02-27 12:43:29 +0000364 /// EmitValue - Emit integer of appropriate size.
365 ///
Jim Laskey65195462006-10-30 13:35:07 +0000366 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000367
368 /// SizeOf - Determine size of integer value in bytes.
369 ///
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000370 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000371
372 /// Profile - Used to gather unique data for the value folding set.
373 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000374 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000375 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000376 ID.AddInteger(Integer);
377 }
Jim Laskey5496f012006-11-09 14:52:14 +0000378 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000379
380#ifndef NDEBUG
381 virtual void print(std::ostream &O) {
382 O << "Int: " << (int64_t)Integer
383 << " 0x" << std::hex << Integer << std::dec;
384 }
385#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000386};
Jim Laskey063e7652006-01-17 17:31:53 +0000387
Jim Laskey0d086af2006-02-27 12:43:29 +0000388//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000389/// DIEString - A string value DIE.
390///
Jim Laskeyef42a012006-11-02 20:12:39 +0000391class DIEString : public DIEValue {
392public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000393 const std::string String;
394
395 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000396
Jim Laskey0d086af2006-02-27 12:43:29 +0000397 // Implement isa/cast/dyncast.
398 static bool classof(const DIEString *) { return true; }
399 static bool classof(const DIEValue *S) { return S->Type == isString; }
400
401 /// EmitValue - Emit string value.
402 ///
Jim Laskey65195462006-10-30 13:35:07 +0000403 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000404
405 /// SizeOf - Determine size of string value in bytes.
406 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000407 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
408 return String.size() + sizeof(char); // sizeof('\0');
409 }
410
411 /// Profile - Used to gather unique data for the value folding set.
412 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000413 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000414 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000415 ID.AddString(String);
416 }
Jim Laskey5496f012006-11-09 14:52:14 +0000417 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000418
419#ifndef NDEBUG
420 virtual void print(std::ostream &O) {
421 O << "Str: \"" << String << "\"";
422 }
423#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000424};
Jim Laskey063e7652006-01-17 17:31:53 +0000425
Jim Laskey0d086af2006-02-27 12:43:29 +0000426//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000427/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000428//
Jim Laskeyef42a012006-11-02 20:12:39 +0000429class DIEDwarfLabel : public DIEValue {
430public:
431
Jim Laskey0d086af2006-02-27 12:43:29 +0000432 const DWLabel Label;
433
434 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000435
Jim Laskey0d086af2006-02-27 12:43:29 +0000436 // Implement isa/cast/dyncast.
437 static bool classof(const DIEDwarfLabel *) { return true; }
438 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
439
440 /// EmitValue - Emit label value.
441 ///
Jim Laskey65195462006-10-30 13:35:07 +0000442 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000443
444 /// SizeOf - Determine size of label value in bytes.
445 ///
Jim Laskey65195462006-10-30 13:35:07 +0000446 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000447
448 /// Profile - Used to gather unique data for the value folding set.
449 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000450 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000451 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000452 Label.Profile(ID);
453 }
Jim Laskey5496f012006-11-09 14:52:14 +0000454 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000455
456#ifndef NDEBUG
457 virtual void print(std::ostream &O) {
458 O << "Lbl: ";
459 Label.print(O);
460 }
461#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000462};
Jim Laskey063e7652006-01-17 17:31:53 +0000463
Jim Laskey063e7652006-01-17 17:31:53 +0000464
Jim Laskey0d086af2006-02-27 12:43:29 +0000465//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000466/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000467//
Jim Laskeyef42a012006-11-02 20:12:39 +0000468class DIEObjectLabel : public DIEValue {
469public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000470 const std::string Label;
471
472 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000473
Jim Laskey0d086af2006-02-27 12:43:29 +0000474 // Implement isa/cast/dyncast.
475 static bool classof(const DIEObjectLabel *) { return true; }
476 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
477
478 /// EmitValue - Emit label value.
479 ///
Jim Laskey65195462006-10-30 13:35:07 +0000480 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000481
482 /// SizeOf - Determine size of label value in bytes.
483 ///
Jim Laskey65195462006-10-30 13:35:07 +0000484 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000485
486 /// Profile - Used to gather unique data for the value folding set.
487 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000488 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000489 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000490 ID.AddString(Label);
491 }
Jim Laskey5496f012006-11-09 14:52:14 +0000492 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000493
494#ifndef NDEBUG
495 virtual void print(std::ostream &O) {
496 O << "Obj: " << Label;
497 }
498#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000499};
Jim Laskey063e7652006-01-17 17:31:53 +0000500
Jim Laskey0d086af2006-02-27 12:43:29 +0000501//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000502/// DIEDelta - A simple label difference DIE.
503///
Jim Laskeyef42a012006-11-02 20:12:39 +0000504class DIEDelta : public DIEValue {
505public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000506 const DWLabel LabelHi;
507 const DWLabel LabelLo;
508
509 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
510 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000511
Jim Laskey0d086af2006-02-27 12:43:29 +0000512 // Implement isa/cast/dyncast.
513 static bool classof(const DIEDelta *) { return true; }
514 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
515
516 /// EmitValue - Emit delta value.
517 ///
Jim Laskey65195462006-10-30 13:35:07 +0000518 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000519
520 /// SizeOf - Determine size of delta value in bytes.
521 ///
Jim Laskey65195462006-10-30 13:35:07 +0000522 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000523
524 /// Profile - Used to gather unique data for the value folding set.
525 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000526 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
527 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000528 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000529 LabelHi.Profile(ID);
530 LabelLo.Profile(ID);
531 }
Jim Laskey5496f012006-11-09 14:52:14 +0000532 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000533
534#ifndef NDEBUG
535 virtual void print(std::ostream &O) {
536 O << "Del: ";
537 LabelHi.print(O);
538 O << "-";
539 LabelLo.print(O);
540 }
541#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000542};
Jim Laskey063e7652006-01-17 17:31:53 +0000543
Jim Laskey0d086af2006-02-27 12:43:29 +0000544//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000545/// DIEntry - A pointer to another debug information entry. An instance of this
546/// class can also be used as a proxy for a debug information entry not yet
547/// defined (ie. types.)
548class DIEntry : public DIEValue {
549public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000550 DIE *Entry;
551
552 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000553
Jim Laskey0d086af2006-02-27 12:43:29 +0000554 // Implement isa/cast/dyncast.
555 static bool classof(const DIEntry *) { return true; }
556 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
557
Jim Laskeyb8509c52006-03-23 18:07:55 +0000558 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000559 ///
Jim Laskey65195462006-10-30 13:35:07 +0000560 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000561
Jim Laskeyb8509c52006-03-23 18:07:55 +0000562 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000563 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000564 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const {
565 return sizeof(int32_t);
566 }
567
568 /// Profile - Used to gather unique data for the value folding set.
569 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000570 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
571 ID.AddInteger(isEntry);
572 ID.AddPointer(Entry);
573 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000574 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000575 ID.AddInteger(isEntry);
576
Jim Laskeyef42a012006-11-02 20:12:39 +0000577 if (Entry) {
578 ID.AddPointer(Entry);
579 } else {
580 ID.AddPointer(this);
581 }
582 }
583
584#ifndef NDEBUG
585 virtual void print(std::ostream &O) {
586 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
587 }
588#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000589};
590
591//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000592/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000593//
Jim Laskeyef42a012006-11-02 20:12:39 +0000594class DIEBlock : public DIEValue, public DIE {
595public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000596 unsigned Size; // Size in bytes excluding size header.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000597
598 DIEBlock()
599 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000600 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000601 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000602 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000603 ~DIEBlock() {
604 }
605
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000606 // Implement isa/cast/dyncast.
607 static bool classof(const DIEBlock *) { return true; }
608 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
609
610 /// ComputeSize - calculate the size of the block.
611 ///
Jim Laskey65195462006-10-30 13:35:07 +0000612 unsigned ComputeSize(Dwarf &DW);
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000613
614 /// BestForm - Choose the best form for data.
615 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000616 unsigned BestForm() const {
617 if ((unsigned char)Size == Size) return DW_FORM_block1;
618 if ((unsigned short)Size == Size) return DW_FORM_block2;
619 if ((unsigned int)Size == Size) return DW_FORM_block4;
620 return DW_FORM_block;
621 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000622
623 /// EmitValue - Emit block data.
624 ///
Jim Laskey65195462006-10-30 13:35:07 +0000625 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000626
627 /// SizeOf - Determine size of block data in bytes.
628 ///
Jim Laskey65195462006-10-30 13:35:07 +0000629 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000630
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000631
Jim Laskeyef42a012006-11-02 20:12:39 +0000632 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000633 ///
Reid Spencer97821312006-11-02 23:56:21 +0000634 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000635 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000636 DIE::Profile(ID);
637 }
638
639#ifndef NDEBUG
640 virtual void print(std::ostream &O) {
641 O << "Blk: ";
642 DIE::print(O, 5);
643 }
644#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000645};
646
647//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000648/// CompileUnit - This dwarf writer support class manages information associate
649/// with a source file.
650class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000651private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000652 /// Desc - Compile unit debug descriptor.
653 ///
654 CompileUnitDesc *Desc;
655
656 /// ID - File identifier for source.
657 ///
658 unsigned ID;
659
660 /// Die - Compile unit debug information entry.
661 ///
662 DIE *Die;
663
664 /// DescToDieMap - Tracks the mapping of unit level debug informaton
665 /// descriptors to debug information entries.
666 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
667
668 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
669 /// descriptors to debug information entries using a DIEntry proxy.
670 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
671
672 /// Globals - A map of globally visible named entities for this unit.
673 ///
674 std::map<std::string, DIE *> Globals;
675
676 /// DiesSet - Used to uniquely define dies within the compile unit.
677 ///
678 FoldingSet<DIE> DiesSet;
679
680 /// Dies - List of all dies in the compile unit.
681 ///
682 std::vector<DIE *> Dies;
Jim Laskey0d086af2006-02-27 12:43:29 +0000683
684public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000685 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
686 : Desc(CUD)
687 , ID(I)
688 , Die(D)
689 , DescToDieMap()
690 , DescToDIEntryMap()
691 , Globals()
692 , DiesSet(InitDiesSetSize)
693 , Dies()
694 {}
695
696 ~CompileUnit() {
697 delete Die;
698
699 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
700 delete Dies[i];
701 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000702
Jim Laskeybd761842006-02-27 17:27:12 +0000703 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000704 CompileUnitDesc *getDesc() const { return Desc; }
705 unsigned getID() const { return ID; }
706 DIE* getDie() const { return Die; }
707 std::map<std::string, DIE *> &getGlobals() { return Globals; }
708
709 /// hasContent - Return true if this compile unit has something to write out.
710 ///
711 bool hasContent() const {
712 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000713 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000714
715 /// AddGlobal - Add a new global entity to the compile unit.
716 ///
717 void AddGlobal(const std::string &Name, DIE *Die) {
718 Globals[Name] = Die;
719 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000720
Jim Laskeyef42a012006-11-02 20:12:39 +0000721 /// getDieMapSlotFor - Returns the debug information entry map slot for the
722 /// specified debug descriptor.
723 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
724 return DescToDieMap[DD];
725 }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000726
Jim Laskeyef42a012006-11-02 20:12:39 +0000727 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
728 /// specified debug descriptor.
729 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DD) {
730 return DescToDIEntryMap[DD];
731 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000732
Jim Laskeyef42a012006-11-02 20:12:39 +0000733 /// AddDie - Adds or interns the DIE to the compile unit.
734 ///
735 DIE *AddDie(DIE &Buffer) {
736 FoldingSetNodeID ID;
737 Buffer.Profile(ID);
738 void *Where;
739 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
740
741 if (!Die) {
742 Die = new DIE(Buffer);
743 DiesSet.InsertNode(Die, Where);
744 this->Die->AddChild(Die);
745 Buffer.Detach();
746 }
747
748 return Die;
749 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000750};
751
Jim Laskey65195462006-10-30 13:35:07 +0000752//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000753/// Dwarf - Emits Dwarf debug and exception handling directives.
754///
Jim Laskey65195462006-10-30 13:35:07 +0000755class Dwarf {
756
757private:
758
759 //===--------------------------------------------------------------------===//
760 // Core attributes used by the Dwarf writer.
761 //
762
763 //
764 /// O - Stream to .s file.
765 ///
766 std::ostream &O;
767
768 /// Asm - Target of Dwarf emission.
769 ///
770 AsmPrinter *Asm;
771
772 /// TAI - Target Asm Printer.
773 const TargetAsmInfo *TAI;
774
775 /// TD - Target data.
776 const TargetData *TD;
777
778 /// RI - Register Information.
779 const MRegisterInfo *RI;
780
781 /// M - Current module.
782 ///
783 Module *M;
784
785 /// MF - Current machine function.
786 ///
787 MachineFunction *MF;
788
789 /// DebugInfo - Collected debug information.
790 ///
791 MachineDebugInfo *DebugInfo;
792
793 /// didInitial - Flag to indicate if initial emission has been done.
794 ///
795 bool didInitial;
796
797 /// shouldEmit - Flag to indicate if debug information should be emitted.
798 ///
799 bool shouldEmit;
800
801 /// SubprogramCount - The running count of functions being compiled.
802 ///
803 unsigned SubprogramCount;
804
805 //===--------------------------------------------------------------------===//
806 // Attributes used to construct specific Dwarf sections.
807 //
808
809 /// CompileUnits - All the compile units involved in this build. The index
810 /// of each entry in this vector corresponds to the sources in DebugInfo.
811 std::vector<CompileUnit *> CompileUnits;
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000812
Jim Laskeyef42a012006-11-02 20:12:39 +0000813 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +0000814 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000815 FoldingSet<DIEAbbrev> AbbreviationsSet;
816
817 /// Abbreviations - A list of all the unique abbreviations in use.
818 ///
819 std::vector<DIEAbbrev *> Abbreviations;
Jim Laskey65195462006-10-30 13:35:07 +0000820
Jim Laskeyef42a012006-11-02 20:12:39 +0000821 /// ValuesSet - Used to uniquely define values.
822 ///
823 FoldingSet<DIEValue> ValuesSet;
824
825 /// Values - A list of all the unique values in use.
826 ///
827 std::vector<DIEValue *> Values;
828
Jim Laskey65195462006-10-30 13:35:07 +0000829 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +0000830 ///
Jim Laskey65195462006-10-30 13:35:07 +0000831 UniqueVector<std::string> StringPool;
832
833 /// UnitMap - Map debug information descriptor to compile unit.
834 ///
835 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
836
Jim Laskey65195462006-10-30 13:35:07 +0000837 /// SectionMap - Provides a unique id per text section.
838 ///
839 UniqueVector<std::string> SectionMap;
840
841 /// SectionSourceLines - Tracks line numbers per text section.
842 ///
843 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
844
845
846public:
847
848 //===--------------------------------------------------------------------===//
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000849 // Accessors.
Jim Laskey65195462006-10-30 13:35:07 +0000850 //
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000851 AsmPrinter *getAsm() const { return Asm; }
Jim Laskey65195462006-10-30 13:35:07 +0000852
853 /// PrintLabelName - Print label name in form used by Dwarf writer.
854 ///
855 void PrintLabelName(DWLabel Label) const {
856 PrintLabelName(Label.Tag, Label.Number);
857 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000858 void PrintLabelName(const char *Tag, unsigned Number) const {
859 O << TAI->getPrivateGlobalPrefix()
860 << "debug_"
861 << Tag;
862 if (Number) O << Number;
863 }
Jim Laskey65195462006-10-30 13:35:07 +0000864
865 /// EmitLabel - Emit location label for internal use by Dwarf.
866 ///
867 void EmitLabel(DWLabel Label) const {
868 EmitLabel(Label.Tag, Label.Number);
869 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000870 void EmitLabel(const char *Tag, unsigned Number) const {
871 PrintLabelName(Tag, Number);
872 O << ":\n";
873 }
Jim Laskey65195462006-10-30 13:35:07 +0000874
875 /// EmitReference - Emit a reference to a label.
876 ///
877 void EmitReference(DWLabel Label) const {
878 EmitReference(Label.Tag, Label.Number);
879 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000880 void EmitReference(const char *Tag, unsigned Number) const {
Jim Laskey2b935d52007-01-26 14:19:17 +0000881 if (TAI->getAddressSize() == sizeof(int32_t))
Jim Laskeyef42a012006-11-02 20:12:39 +0000882 O << TAI->getData32bitsDirective();
883 else
884 O << TAI->getData64bitsDirective();
885
886 PrintLabelName(Tag, Number);
887 }
888 void EmitReference(const std::string &Name) const {
Jim Laskey2b935d52007-01-26 14:19:17 +0000889 if (TAI->getAddressSize() == sizeof(int32_t))
Jim Laskeyef42a012006-11-02 20:12:39 +0000890 O << TAI->getData32bitsDirective();
891 else
892 O << TAI->getData64bitsDirective();
893
894 O << Name;
895 }
Jim Laskey65195462006-10-30 13:35:07 +0000896
897 /// EmitDifference - Emit the difference between two labels. Some
898 /// assemblers do not behave with absolute expressions with data directives,
899 /// so there is an option (needsSet) to use an intermediary set expression.
Jim Laskey2b4e98c2006-12-06 17:43:18 +0000900 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
901 bool IsSmall = false) const {
902 EmitDifference(LabelHi.Tag, LabelHi.Number,
903 LabelLo.Tag, LabelLo.Number,
904 IsSmall);
Jim Laskey65195462006-10-30 13:35:07 +0000905 }
906 void EmitDifference(const char *TagHi, unsigned NumberHi,
Jim Laskey2b4e98c2006-12-06 17:43:18 +0000907 const char *TagLo, unsigned NumberLo,
908 bool IsSmall = false) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000909 if (TAI->needsSet()) {
910 static unsigned SetCounter = 0;
911
912 O << "\t.set\t";
913 PrintLabelName("set", SetCounter);
914 O << ",";
915 PrintLabelName(TagHi, NumberHi);
916 O << "-";
917 PrintLabelName(TagLo, NumberLo);
918 O << "\n";
919
Jim Laskey2b4e98c2006-12-06 17:43:18 +0000920 if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
Jim Laskeyef42a012006-11-02 20:12:39 +0000921 O << TAI->getData32bitsDirective();
922 else
923 O << TAI->getData64bitsDirective();
924
925 PrintLabelName("set", SetCounter);
926
927 ++SetCounter;
928 } else {
Jim Laskey2b4e98c2006-12-06 17:43:18 +0000929 if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
Jim Laskeyef42a012006-11-02 20:12:39 +0000930 O << TAI->getData32bitsDirective();
931 else
932 O << TAI->getData64bitsDirective();
933
934 PrintLabelName(TagHi, NumberHi);
935 O << "-";
936 PrintLabelName(TagLo, NumberLo);
937 }
938 }
Jim Laskey65195462006-10-30 13:35:07 +0000939
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000940 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Jim Laskey65195462006-10-30 13:35:07 +0000941 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000942 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
943 // Profile the node so that we can make it unique.
944 FoldingSetNodeID ID;
945 Abbrev.Profile(ID);
946
947 // Check the set for priors.
948 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
949
950 // If it's newly added.
951 if (InSet == &Abbrev) {
952 // Add to abbreviation list.
953 Abbreviations.push_back(&Abbrev);
954 // Assign the vector position + 1 as its number.
955 Abbrev.setNumber(Abbreviations.size());
956 } else {
957 // Assign existing abbreviation number.
958 Abbrev.setNumber(InSet->getNumber());
959 }
960 }
961
Jim Laskey65195462006-10-30 13:35:07 +0000962 /// NewString - Add a string to the constant pool and returns a label.
963 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000964 DWLabel NewString(const std::string &String) {
965 unsigned StringID = StringPool.insert(String);
966 return DWLabel("string", StringID);
967 }
Jim Laskey65195462006-10-30 13:35:07 +0000968
Jim Laskeyef42a012006-11-02 20:12:39 +0000969 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
970 /// entry.
971 DIEntry *NewDIEntry(DIE *Entry = NULL) {
972 DIEntry *Value;
973
974 if (Entry) {
975 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +0000976 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +0000977 void *Where;
978 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
979
Jim Laskeyf6733882006-11-02 21:48:18 +0000980 if (Value) return Value;
Jim Laskeyef42a012006-11-02 20:12:39 +0000981
982 Value = new DIEntry(Entry);
983 ValuesSet.InsertNode(Value, Where);
984 } else {
985 Value = new DIEntry(Entry);
986 }
987
988 Values.push_back(Value);
989 return Value;
990 }
991
992 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
993 ///
994 void SetDIEntry(DIEntry *Value, DIE *Entry) {
995 Value->Entry = Entry;
996 // Add to values set if not already there. If it is, we merely have a
997 // duplicate in the values list (no harm.)
998 ValuesSet.GetOrInsertNode(Value);
999 }
1000
1001 /// AddUInt - Add an unsigned integer attribute data and value.
1002 ///
1003 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1004 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1005
1006 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001007 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001008 void *Where;
1009 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1010 if (!Value) {
1011 Value = new DIEInteger(Integer);
1012 ValuesSet.InsertNode(Value, Where);
1013 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001014 }
1015
1016 Die->AddValue(Attribute, Form, Value);
1017 }
1018
1019 /// AddSInt - Add an signed integer attribute data and value.
1020 ///
1021 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1022 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1023
1024 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001025 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001026 void *Where;
1027 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1028 if (!Value) {
1029 Value = new DIEInteger(Integer);
1030 ValuesSet.InsertNode(Value, Where);
1031 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001032 }
1033
1034 Die->AddValue(Attribute, Form, Value);
1035 }
1036
1037 /// AddString - Add a std::string attribute data and value.
1038 ///
1039 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1040 const std::string &String) {
1041 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001042 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001043 void *Where;
1044 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1045 if (!Value) {
1046 Value = new DIEString(String);
1047 ValuesSet.InsertNode(Value, Where);
1048 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001049 }
1050
1051 Die->AddValue(Attribute, Form, Value);
1052 }
1053
1054 /// AddLabel - Add a Dwarf label attribute data and value.
1055 ///
1056 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1057 const DWLabel &Label) {
1058 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001059 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001060 void *Where;
1061 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1062 if (!Value) {
1063 Value = new DIEDwarfLabel(Label);
1064 ValuesSet.InsertNode(Value, Where);
1065 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001066 }
1067
1068 Die->AddValue(Attribute, Form, Value);
1069 }
1070
1071 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1072 ///
1073 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1074 const std::string &Label) {
1075 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001076 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001077 void *Where;
1078 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1079 if (!Value) {
1080 Value = new DIEObjectLabel(Label);
1081 ValuesSet.InsertNode(Value, Where);
1082 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001083 }
1084
1085 Die->AddValue(Attribute, Form, Value);
1086 }
1087
1088 /// AddDelta - Add a label delta attribute data and value.
1089 ///
1090 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1091 const DWLabel &Hi, const DWLabel &Lo) {
1092 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001093 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001094 void *Where;
1095 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1096 if (!Value) {
1097 Value = new DIEDelta(Hi, Lo);
1098 ValuesSet.InsertNode(Value, Where);
1099 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001100 }
1101
1102 Die->AddValue(Attribute, Form, Value);
1103 }
1104
1105 /// AddDIEntry - Add a DIE attribute data and value.
1106 ///
1107 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1108 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1109 }
1110
1111 /// AddBlock - Add block data.
1112 ///
1113 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1114 Block->ComputeSize(*this);
1115 FoldingSetNodeID ID;
1116 Block->Profile(ID);
1117 void *Where;
1118 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1119 if (!Value) {
1120 Value = Block;
1121 ValuesSet.InsertNode(Value, Where);
1122 Values.push_back(Value);
1123 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001124 delete Block;
1125 }
1126
1127 Die->AddValue(Attribute, Block->BestForm(), Value);
1128 }
1129
Jim Laskey65195462006-10-30 13:35:07 +00001130private:
1131
1132 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001133 /// entry.
1134 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1135 if (File && Line) {
1136 CompileUnit *FileUnit = FindCompileUnit(File);
1137 unsigned FileID = FileUnit->getID();
1138 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1139 AddUInt(Die, DW_AT_decl_line, 0, Line);
1140 }
1141 }
Jim Laskey65195462006-10-30 13:35:07 +00001142
1143 /// AddAddress - Add an address attribute to a die based on the location
1144 /// provided.
1145 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001146 const MachineLocation &Location) {
1147 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1148 DIEBlock *Block = new DIEBlock();
1149
1150 if (Location.isRegister()) {
1151 if (Reg < 32) {
1152 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1153 } else {
1154 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1155 AddUInt(Block, 0, DW_FORM_udata, Reg);
1156 }
1157 } else {
1158 if (Reg < 32) {
1159 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1160 } else {
1161 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1162 AddUInt(Block, 0, DW_FORM_udata, Reg);
1163 }
1164 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1165 }
1166
1167 AddBlock(Die, Attribute, 0, Block);
1168 }
1169
1170 /// AddBasicType - Add a new basic type attribute to the specified entity.
1171 ///
1172 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1173 const std::string &Name,
1174 unsigned Encoding, unsigned Size) {
1175 DIE *Die = ConstructBasicType(Unit, Name, Encoding, Size);
1176 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1177 }
1178
1179 /// ConstructBasicType - Construct a new basic type.
1180 ///
1181 DIE *ConstructBasicType(CompileUnit *Unit,
1182 const std::string &Name,
1183 unsigned Encoding, unsigned Size) {
1184 DIE Buffer(DW_TAG_base_type);
1185 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1186 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1187 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1188 return Unit->AddDie(Buffer);
1189 }
1190
1191 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1192 ///
1193 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1194 DIE *Die = ConstructPointerType(Unit, Name);
1195 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1196 }
1197
1198 /// ConstructPointerType - Construct a new pointer type.
1199 ///
1200 DIE *ConstructPointerType(CompileUnit *Unit, const std::string &Name) {
1201 DIE Buffer(DW_TAG_pointer_type);
1202 AddUInt(&Buffer, DW_AT_byte_size, 0, TAI->getAddressSize());
1203 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1204 return Unit->AddDie(Buffer);
1205 }
1206
1207 /// AddType - Add a new type attribute to the specified entity.
1208 ///
1209 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1210 if (!TyDesc) {
Jim Laskey2b935d52007-01-26 14:19:17 +00001211 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001212 } else {
1213 // Check for pre-existence.
1214 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
1215
1216 // If it exists then use the existing value.
1217 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001218 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1219 return;
1220 }
1221
1222 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1223 // FIXME - Not sure why programs and variables are coming through here.
1224 // Short cut for handling subprogram types (not really a TyDesc.)
1225 AddPointerType(Entity, Unit, SubprogramTy->getName());
1226 } else if (GlobalVariableDesc *GlobalTy =
1227 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1228 // FIXME - Not sure why programs and variables are coming through here.
1229 // Short cut for handling global variable types (not really a TyDesc.)
1230 AddPointerType(Entity, Unit, GlobalTy->getName());
1231 } else {
1232 // Set up proxy.
1233 Slot = NewDIEntry();
1234
1235 // Construct type.
1236 DIE Buffer(DW_TAG_base_type);
1237 ConstructType(Buffer, TyDesc, Unit);
1238
1239 // Add debug information entry to entity and unit.
1240 DIE *Die = Unit->AddDie(Buffer);
1241 SetDIEntry(Slot, Die);
1242 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1243 }
1244 }
1245 }
1246
1247 /// ConstructType - Adds all the required attributes to the type.
1248 ///
1249 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1250 // Get core information.
1251 const std::string &Name = TyDesc->getName();
1252 uint64_t Size = TyDesc->getSize() >> 3;
1253
1254 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1255 // Fundamental types like int, float, bool
1256 Buffer.setTag(DW_TAG_base_type);
1257 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1258 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00001259 // Fetch tag.
1260 unsigned Tag = DerivedTy->getTag();
1261 // FIXME - Workaround for templates.
1262 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1263 // Pointers, typedefs et al.
1264 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00001265 // Map to main type, void will not have a type.
1266 if (TypeDesc *FromTy = DerivedTy->getFromType())
1267 AddType(&Buffer, FromTy, Unit);
1268 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1269 // Fetch tag.
1270 unsigned Tag = CompTy->getTag();
1271
1272 // Set tag accordingly.
1273 if (Tag == DW_TAG_vector_type)
1274 Buffer.setTag(DW_TAG_array_type);
1275 else
1276 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00001277
Jim Laskeyef42a012006-11-02 20:12:39 +00001278 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1279
1280 switch (Tag) {
1281 case DW_TAG_vector_type:
1282 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1283 // Fall thru
1284 case DW_TAG_array_type: {
1285 // Add element type.
1286 if (TypeDesc *FromTy = CompTy->getFromType())
1287 AddType(&Buffer, FromTy, Unit);
1288
1289 // Don't emit size attribute.
1290 Size = 0;
1291
1292 // Construct an anonymous type for index type.
Jim Laskey2b935d52007-01-26 14:19:17 +00001293 DIE *IndexTy = ConstructBasicType(Unit, "", DW_ATE_signed,
1294 sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001295
1296 // Add subranges to array type.
1297 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1298 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1299 int64_t Lo = SRD->getLo();
1300 int64_t Hi = SRD->getHi();
1301 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1302
1303 // If a range is available.
1304 if (Lo != Hi) {
1305 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1306 // Only add low if non-zero.
1307 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1308 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1309 }
1310
1311 Buffer.AddChild(Subrange);
1312 }
1313 break;
1314 }
1315 case DW_TAG_structure_type:
1316 case DW_TAG_union_type: {
1317 // Add elements to structure type.
1318 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1319 DebugInfoDesc *Element = Elements[i];
1320
1321 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1322 // Add field or base class.
1323
1324 unsigned Tag = MemberDesc->getTag();
1325
1326 // Extract the basic information.
1327 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001328 uint64_t Size = MemberDesc->getSize();
1329 uint64_t Align = MemberDesc->getAlign();
1330 uint64_t Offset = MemberDesc->getOffset();
1331
1332 // Construct member debug information entry.
1333 DIE *Member = new DIE(Tag);
1334
1335 // Add name if not "".
1336 if (!Name.empty())
1337 AddString(Member, DW_AT_name, DW_FORM_string, Name);
1338 // Add location if available.
1339 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1340
1341 // Most of the time the field info is the same as the members.
1342 uint64_t FieldSize = Size;
1343 uint64_t FieldAlign = Align;
1344 uint64_t FieldOffset = Offset;
1345
Jim Laskeyee5f9272006-12-22 20:03:42 +00001346 // Set the member type.
1347 TypeDesc *FromTy = MemberDesc->getFromType();
1348 AddType(Member, FromTy, Unit);
1349
1350 // Walk up typedefs until a real size is found.
1351 while (FromTy) {
1352 if (FromTy->getTag() != DW_TAG_typedef) {
1353 FieldSize = FromTy->getSize();
1354 FieldAlign = FromTy->getSize();
1355 break;
1356 }
1357
1358 FromTy = dyn_cast<DerivedTypeDesc>(FromTy)->getFromType();
Jim Laskeyef42a012006-11-02 20:12:39 +00001359 }
1360
1361 // Unless we have a bit field.
1362 if (Tag == DW_TAG_member && FieldSize != Size) {
1363 // Construct the alignment mask.
1364 uint64_t AlignMask = ~(FieldAlign - 1);
1365 // Determine the high bit + 1 of the declared size.
1366 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1367 // Work backwards to determine the base offset of the field.
1368 FieldOffset = HiMark - FieldSize;
1369 // Now normalize offset to the field.
1370 Offset -= FieldOffset;
1371
1372 // Maybe we need to work from the other end.
1373 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1374
1375 // Add size and offset.
1376 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1377 AddUInt(Member, DW_AT_bit_size, 0, Size);
1378 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1379 }
1380
1381 // Add computation for offset.
1382 DIEBlock *Block = new DIEBlock();
1383 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1384 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1385 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1386
1387 // Add accessibility (public default unless is base class.
1388 if (MemberDesc->isProtected()) {
1389 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1390 } else if (MemberDesc->isPrivate()) {
1391 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1392 } else if (Tag == DW_TAG_inheritance) {
1393 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1394 }
1395
1396 Buffer.AddChild(Member);
1397 } else if (GlobalVariableDesc *StaticDesc =
1398 dyn_cast<GlobalVariableDesc>(Element)) {
1399 // Add static member.
1400
1401 // Construct member debug information entry.
1402 DIE *Static = new DIE(DW_TAG_variable);
1403
1404 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001405 const std::string &Name = StaticDesc->getName();
1406 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001407 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001408 if (!LinkageName.empty()) {
1409 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1410 LinkageName);
1411 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001412
1413 // Add location.
1414 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1415
1416 // Add type.
1417 if (TypeDesc *StaticTy = StaticDesc->getType())
1418 AddType(Static, StaticTy, Unit);
1419
1420 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001421 if (!StaticDesc->isStatic())
1422 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001423 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
1424
1425 Buffer.AddChild(Static);
1426 } else if (SubprogramDesc *MethodDesc =
1427 dyn_cast<SubprogramDesc>(Element)) {
1428 // Add member function.
1429
1430 // Construct member debug information entry.
1431 DIE *Method = new DIE(DW_TAG_subprogram);
1432
1433 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001434 const std::string &Name = MethodDesc->getName();
1435 const std::string &LinkageName = MethodDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001436
Jim Laskey2172f962006-11-30 14:35:45 +00001437 AddString(Method, DW_AT_name, DW_FORM_string, Name);
1438 bool IsCTor = TyDesc->getName() == Name;
1439
1440 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001441 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001442 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001443 }
1444
1445 // Add location.
1446 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1447
1448 // Add type.
1449 if (CompositeTypeDesc *MethodTy =
1450 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1451 // Get argument information.
1452 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1453
1454 // If not a ctor.
1455 if (!IsCTor) {
1456 // Add return type.
1457 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1458 }
1459
1460 // Add arguments.
1461 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1462 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1463 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1464 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1465 Method->AddChild(Arg);
1466 }
1467 }
1468
1469 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001470 if (!MethodDesc->isStatic())
1471 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001472 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
1473
1474 Buffer.AddChild(Method);
1475 }
1476 }
1477 break;
1478 }
1479 case DW_TAG_enumeration_type: {
1480 // Add enumerators to enumeration type.
1481 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1482 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1483 const std::string &Name = ED->getName();
1484 int64_t Value = ED->getValue();
1485 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1486 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1487 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1488 Buffer.AddChild(Enumerator);
1489 }
1490
1491 break;
1492 }
1493 case DW_TAG_subroutine_type: {
1494 // Add prototype flag.
1495 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1496 // Add return type.
1497 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
1498
1499 // Add arguments.
1500 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1501 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1502 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1503 Buffer.AddChild(Arg);
1504 }
1505
1506 break;
1507 }
1508 default: break;
1509 }
1510 }
1511
1512 // Add size if non-zero (derived types don't have a size.)
1513 if (Size) AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1514 // Add name if not anonymous or intermediate type.
1515 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1516 // Add source line info if available.
1517 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
1518 }
1519
1520 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00001521 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001522 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1523 // Construct debug information entry.
1524 DIE *Die = new DIE(DW_TAG_compile_unit);
1525 AddDelta(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0),
1526 DWLabel("section_line", 0));
1527 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1528 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1529 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1530 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1531
1532 // Construct compile unit.
1533 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1534
1535 // Add Unit to compile unit map.
1536 DescToUnitMap[UnitDesc] = Unit;
1537
1538 return Unit;
1539 }
1540
Jim Laskey9d4209f2006-11-07 19:33:46 +00001541 /// GetBaseCompileUnit - Get the main compile unit.
1542 ///
1543 CompileUnit *GetBaseCompileUnit() const {
1544 CompileUnit *Unit = CompileUnits[0];
1545 assert(Unit && "Missing compile unit.");
1546 return Unit;
1547 }
1548
Jim Laskey65195462006-10-30 13:35:07 +00001549 /// FindCompileUnit - Get the compile unit for the given descriptor.
1550 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001551 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001552 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00001553 assert(Unit && "Missing compile unit.");
1554 return Unit;
1555 }
1556
1557 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00001558 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001559 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1560 // Get the compile unit context.
1561 CompileUnitDesc *UnitDesc =
1562 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001563 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001564
1565 // Check for pre-existence.
1566 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1567 if (Slot) return Slot;
1568
1569 // Get the global variable itself.
1570 GlobalVariable *GV = GVD->getGlobalVariable();
1571
Jim Laskey2172f962006-11-30 14:35:45 +00001572 const std::string &Name = GVD->getName();
1573 const std::string &FullName = GVD->getFullName();
1574 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001575 // Create the global's variable DIE.
1576 DIE *VariableDie = new DIE(DW_TAG_variable);
1577 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001578 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001579 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001580 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001581 }
Jim Laskey6488a072007-01-08 22:15:18 +00001582 AddType(VariableDie, GVD->getType(), Unit);
1583 if (!GVD->isStatic())
1584 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001585
1586 // Add source line info if available.
1587 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1588
Jim Laskeyef42a012006-11-02 20:12:39 +00001589 // Add address.
1590 DIEBlock *Block = new DIEBlock();
1591 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00001592 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
1593 AddBlock(VariableDie, DW_AT_location, 0, Block);
Jim Laskeyef42a012006-11-02 20:12:39 +00001594
1595 // Add to map.
1596 Slot = VariableDie;
1597
1598 // Add to context owner.
1599 Unit->getDie()->AddChild(VariableDie);
1600
1601 // Expose as global.
1602 // FIXME - need to check external flag.
Jim Laskey2172f962006-11-30 14:35:45 +00001603 Unit->AddGlobal(FullName, VariableDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001604
1605 return VariableDie;
1606 }
Jim Laskey65195462006-10-30 13:35:07 +00001607
1608 /// NewSubprogram - Add a new subprogram DIE.
1609 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001610 DIE *NewSubprogram(SubprogramDesc *SPD) {
1611 // Get the compile unit context.
1612 CompileUnitDesc *UnitDesc =
1613 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001614 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001615
1616 // Check for pre-existence.
1617 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1618 if (Slot) return Slot;
1619
1620 // Gather the details (simplify add attribute code.)
Jim Laskey2172f962006-11-30 14:35:45 +00001621 const std::string &Name = SPD->getName();
1622 const std::string &FullName = SPD->getFullName();
1623 const std::string &LinkageName = SPD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001624
1625 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1626 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001627 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001628 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001629 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001630 }
1631 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
Jim Laskey6488a072007-01-08 22:15:18 +00001632 if (!SPD->isStatic())
1633 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001634 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
1635
1636 // Add source line info if available.
1637 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1638
1639 // Add to map.
1640 Slot = SubprogramDie;
1641
1642 // Add to context owner.
1643 Unit->getDie()->AddChild(SubprogramDie);
1644
1645 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00001646 Unit->AddGlobal(FullName, SubprogramDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001647
1648 return SubprogramDie;
1649 }
Jim Laskey65195462006-10-30 13:35:07 +00001650
1651 /// NewScopeVariable - Create a new scope variable.
1652 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001653 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1654 // Get the descriptor.
1655 VariableDesc *VD = DV->getDesc();
1656
1657 // Translate tag to proper Dwarf tag. The result variable is dropped for
1658 // now.
1659 unsigned Tag;
1660 switch (VD->getTag()) {
1661 case DW_TAG_return_variable: return NULL;
1662 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1663 case DW_TAG_auto_variable: // fall thru
1664 default: Tag = DW_TAG_variable; break;
1665 }
1666
1667 // Define variable debug information entry.
1668 DIE *VariableDie = new DIE(Tag);
1669 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1670
1671 // Add source line info if available.
1672 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1673
1674 // Add variable type.
1675 AddType(VariableDie, VD->getType(), Unit);
1676
1677 // Add variable address.
1678 MachineLocation Location;
1679 RI->getLocation(*MF, DV->getFrameIndex(), Location);
1680 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001681
Jim Laskeyef42a012006-11-02 20:12:39 +00001682 return VariableDie;
1683 }
Jim Laskey65195462006-10-30 13:35:07 +00001684
1685 /// ConstructScope - Construct the components of a scope.
1686 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001687 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00001688 unsigned ParentStartID, unsigned ParentEndID,
1689 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001690 // Add variables to scope.
1691 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1692 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1693 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1694 if (VariableDie) ParentDie->AddChild(VariableDie);
1695 }
1696
1697 // Add nested scopes.
1698 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1699 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1700 // Define the Scope debug information entry.
1701 DebugScope *Scope = Scopes[j];
1702 // FIXME - Ignore inlined functions for the time being.
1703 if (!Scope->getParent()) continue;
1704
Jim Laskey9d4209f2006-11-07 19:33:46 +00001705 unsigned StartID = DebugInfo->MappedLabel(Scope->getStartLabelID());
1706 unsigned EndID = DebugInfo->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00001707
Jim Laskey9d4209f2006-11-07 19:33:46 +00001708 // Ignore empty scopes.
1709 if (StartID == EndID && StartID != 0) continue;
1710 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00001711
Jim Laskey36729dd2006-11-29 16:55:57 +00001712 if (StartID == ParentStartID && EndID == ParentEndID) {
1713 // Just add stuff to the parent scope.
1714 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001715 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00001716 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1717
1718 // Add the scope bounds.
1719 if (StartID) {
1720 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1721 DWLabel("loc", StartID));
1722 } else {
1723 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1724 DWLabel("func_begin", SubprogramCount));
1725 }
1726 if (EndID) {
1727 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1728 DWLabel("loc", EndID));
1729 } else {
1730 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1731 DWLabel("func_end", SubprogramCount));
1732 }
1733
1734 // Add the scope contents.
1735 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
1736 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001737 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001738 }
1739 }
Jim Laskey65195462006-10-30 13:35:07 +00001740
1741 /// ConstructRootScope - Construct the scope for the subprogram.
1742 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001743 void ConstructRootScope(DebugScope *RootScope) {
1744 // Exit if there is no root scope.
1745 if (!RootScope) return;
1746
1747 // Get the subprogram debug information entry.
1748 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1749
1750 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00001751 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001752
1753 // Get the subprogram die.
1754 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
1755 assert(SPDie && "Missing subprogram descriptor");
1756
1757 // Add the function bounds.
1758 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1759 DWLabel("func_begin", SubprogramCount));
1760 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1761 DWLabel("func_end", SubprogramCount));
1762 MachineLocation Location(RI->getFrameRegister(*MF));
1763 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001764
Jim Laskey36729dd2006-11-29 16:55:57 +00001765 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001766 }
Jim Laskey65195462006-10-30 13:35:07 +00001767
Jim Laskeyef42a012006-11-02 20:12:39 +00001768 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1769 /// tools to recognize the object file contains Dwarf information.
1770 void EmitInitial() {
1771 // Check to see if we already emitted intial headers.
1772 if (didInitial) return;
1773 didInitial = true;
1774
1775 // Dwarf sections base addresses.
1776 if (TAI->getDwarfRequiresFrameSection()) {
1777 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1778 EmitLabel("section_frame", 0);
1779 }
1780 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
1781 EmitLabel("section_info", 0);
1782 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
1783 EmitLabel("section_abbrev", 0);
1784 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
1785 EmitLabel("section_aranges", 0);
1786 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
1787 EmitLabel("section_macinfo", 0);
1788 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
1789 EmitLabel("section_line", 0);
1790 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
1791 EmitLabel("section_loc", 0);
1792 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
1793 EmitLabel("section_pubnames", 0);
1794 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
1795 EmitLabel("section_str", 0);
1796 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
1797 EmitLabel("section_ranges", 0);
1798
1799 Asm->SwitchToTextSection(TAI->getTextSection());
1800 EmitLabel("text_begin", 0);
1801 Asm->SwitchToDataSection(TAI->getDataSection());
1802 EmitLabel("data_begin", 0);
1803
1804 // Emit common frame information.
1805 EmitInitialDebugFrame();
1806 }
1807
Jim Laskey65195462006-10-30 13:35:07 +00001808 /// EmitDIE - Recusively Emits a debug information entry.
1809 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001810 void EmitDIE(DIE *Die) const {
1811 // Get the abbreviation for this DIE.
1812 unsigned AbbrevNumber = Die->getAbbrevNumber();
1813 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1814
Jim Laskey1a4a83c2007-01-25 15:45:58 +00001815 Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00001816
1817 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001818 Asm->EmitULEB128Bytes(AbbrevNumber);
1819 Asm->EOL(std::string("Abbrev [" +
1820 utostr(AbbrevNumber) +
1821 "] 0x" + utohexstr(Die->getOffset()) +
1822 ":0x" + utohexstr(Die->getSize()) + " " +
1823 TagString(Abbrev->getTag())));
Jim Laskeyef42a012006-11-02 20:12:39 +00001824
1825 const std::vector<DIEValue *> &Values = Die->getValues();
1826 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
1827
1828 // Emit the DIE attribute values.
1829 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1830 unsigned Attr = AbbrevData[i].getAttribute();
1831 unsigned Form = AbbrevData[i].getForm();
1832 assert(Form && "Too many attributes for DIE (check abbreviation)");
1833
1834 switch (Attr) {
1835 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001836 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00001837 break;
1838 }
1839 default: {
1840 // Emit an attribute using the defined form.
1841 Values[i]->EmitValue(*this, Form);
1842 break;
1843 }
1844 }
1845
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001846 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00001847 }
1848
1849 // Emit the DIE children if any.
1850 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
1851 const std::vector<DIE *> &Children = Die->getChildren();
1852
1853 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1854 EmitDIE(Children[j]);
1855 }
1856
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001857 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00001858 }
1859 }
1860
Jim Laskey65195462006-10-30 13:35:07 +00001861 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
1862 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001863 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1864 // Get the children.
1865 const std::vector<DIE *> &Children = Die->getChildren();
1866
1867 // If not last sibling and has children then add sibling offset attribute.
1868 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1869
1870 // Record the abbreviation.
1871 AssignAbbrevNumber(Die->getAbbrev());
1872
1873 // Get the abbreviation for this DIE.
1874 unsigned AbbrevNumber = Die->getAbbrevNumber();
1875 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
1876
1877 // Set DIE offset
1878 Die->setOffset(Offset);
1879
1880 // Start the size with the size of abbreviation code.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001881 Offset += Asm->SizeULEB128(AbbrevNumber);
Jim Laskeyef42a012006-11-02 20:12:39 +00001882
1883 const std::vector<DIEValue *> &Values = Die->getValues();
1884 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
1885
1886 // Size the DIE attribute values.
1887 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1888 // Size attribute value.
1889 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
1890 }
1891
1892 // Size the DIE children if any.
1893 if (!Children.empty()) {
1894 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
1895 "Children flag not set");
1896
1897 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1898 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
1899 }
1900
1901 // End of children marker.
1902 Offset += sizeof(int8_t);
1903 }
1904
1905 Die->setSize(Offset - Die->getOffset());
1906 return Offset;
1907 }
Jim Laskey65195462006-10-30 13:35:07 +00001908
1909 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
1910 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001911 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00001912 // Process base compile unit.
1913 CompileUnit *Unit = GetBaseCompileUnit();
1914 // Compute size of compile unit header
1915 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1916 sizeof(int16_t) + // DWARF version number
1917 sizeof(int32_t) + // Offset Into Abbrev. Section
1918 sizeof(int8_t); // Pointer Size (in bytes)
1919 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00001920 }
1921
Jim Laskey65195462006-10-30 13:35:07 +00001922 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1923 /// frame.
1924 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Jim Laskey5e73d5b2007-01-24 18:45:13 +00001925 std::vector<MachineMove> &Moves) {
1926 int stackGrowth =
1927 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1928 TargetFrameInfo::StackGrowsUp ?
1929 TAI->getAddressSize() : -TAI->getAddressSize();
1930
Jim Laskeyef42a012006-11-02 20:12:39 +00001931 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Jim Laskey5e73d5b2007-01-24 18:45:13 +00001932 MachineMove &Move = Moves[i];
1933 unsigned LabelID = Move.getLabelID();
Jim Laskeyef42a012006-11-02 20:12:39 +00001934
Jim Laskey5e73d5b2007-01-24 18:45:13 +00001935 if (LabelID) {
1936 LabelID = DebugInfo->MappedLabel(LabelID);
Jim Laskeyef42a012006-11-02 20:12:39 +00001937
Jim Laskey5e73d5b2007-01-24 18:45:13 +00001938 // Throw out move if the label is invalid.
1939 if (!LabelID) continue;
1940 }
1941
1942 const MachineLocation &Dst = Move.getDestination();
1943 const MachineLocation &Src = Move.getSource();
Jim Laskeyef42a012006-11-02 20:12:39 +00001944
1945 // Advance row if new location.
1946 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001947 Asm->EmitInt8(DW_CFA_advance_loc4);
1948 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00001949 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001950 Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00001951
1952 BaseLabelID = LabelID;
1953 BaseLabel = "loc";
1954 }
1955
Jim Laskeyef42a012006-11-02 20:12:39 +00001956 // If advancing cfa.
1957 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
1958 if (!Src.isRegister()) {
1959 if (Src.getRegister() == MachineLocation::VirtualFP) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001960 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1961 Asm->EOL("DW_CFA_def_cfa_offset");
Jim Laskeyef42a012006-11-02 20:12:39 +00001962 } else {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001963 Asm->EmitInt8(DW_CFA_def_cfa);
1964 Asm->EOL("DW_CFA_def_cfa");
1965 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
1966 Asm->EOL("Register");
Jim Laskeyef42a012006-11-02 20:12:39 +00001967 }
1968
1969 int Offset = Src.getOffset() / stackGrowth;
1970
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001971 Asm->EmitULEB128Bytes(Offset);
1972 Asm->EOL("Offset");
Jim Laskeyef42a012006-11-02 20:12:39 +00001973 } else {
1974 assert(0 && "Machine move no supported yet.");
1975 }
Jim Laskey5e73d5b2007-01-24 18:45:13 +00001976 } else if (Src.isRegister() &&
1977 Src.getRegister() == MachineLocation::VirtualFP) {
1978 if (Dst.isRegister()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001979 Asm->EmitInt8(DW_CFA_def_cfa_register);
1980 Asm->EOL("DW_CFA_def_cfa_register");
1981 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getRegister()));
1982 Asm->EOL("Register");
Jim Laskey5e73d5b2007-01-24 18:45:13 +00001983 } else {
1984 assert(0 && "Machine move no supported yet.");
1985 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001986 } else {
1987 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
1988 int Offset = Dst.getOffset() / stackGrowth;
1989
1990 if (Offset < 0) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001991 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1992 Asm->EOL("DW_CFA_offset_extended_sf");
1993 Asm->EmitULEB128Bytes(Reg);
1994 Asm->EOL("Reg");
1995 Asm->EmitSLEB128Bytes(Offset);
1996 Asm->EOL("Offset");
Jim Laskeyef42a012006-11-02 20:12:39 +00001997 } else if (Reg < 64) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001998 Asm->EmitInt8(DW_CFA_offset + Reg);
1999 Asm->EOL("DW_CFA_offset + Reg");
2000 Asm->EmitULEB128Bytes(Offset);
2001 Asm->EOL("Offset");
Jim Laskeyef42a012006-11-02 20:12:39 +00002002 } else {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002003 Asm->EmitInt8(DW_CFA_offset_extended);
2004 Asm->EOL("DW_CFA_offset_extended");
2005 Asm->EmitULEB128Bytes(Reg);
2006 Asm->EOL("Reg");
2007 Asm->EmitULEB128Bytes(Offset);
2008 Asm->EOL("Offset");
Jim Laskeyef42a012006-11-02 20:12:39 +00002009 }
2010 }
2011 }
2012 }
Jim Laskey65195462006-10-30 13:35:07 +00002013
2014 /// EmitDebugInfo - Emit the debug info section.
2015 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002016 void EmitDebugInfo() const {
2017 // Start debug info section.
2018 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2019
Jim Laskey5496f012006-11-09 14:52:14 +00002020 CompileUnit *Unit = GetBaseCompileUnit();
2021 DIE *Die = Unit->getDie();
2022 // Emit the compile units header.
2023 EmitLabel("info_begin", Unit->getID());
2024 // Emit size of content not including length itself
2025 unsigned ContentSize = Die->getSize() +
2026 sizeof(int16_t) + // DWARF version number
2027 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00002028 sizeof(int8_t) + // Pointer Size (in bytes)
2029 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Laskey5496f012006-11-09 14:52:14 +00002030
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002031 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2032 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002033 EmitDifference("abbrev_begin", 0, "section_abbrev", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002034 Asm->EOL("Offset Into Abbrev. Section");
2035 Asm->EmitInt8(TAI->getAddressSize()); Asm->EOL("Address Size (in bytes)");
Jim Laskey5496f012006-11-09 14:52:14 +00002036
2037 EmitDIE(Die);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002038 // FIXME - extra padding for gdb bug.
2039 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2040 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2041 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2042 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Jim Laskey5496f012006-11-09 14:52:14 +00002043 EmitLabel("info_end", Unit->getID());
2044
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002045 Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002046 }
2047
Jim Laskey65195462006-10-30 13:35:07 +00002048 /// EmitAbbreviations - Emit the abbreviation section.
2049 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002050 void EmitAbbreviations() const {
2051 // Check to see if it is worth the effort.
2052 if (!Abbreviations.empty()) {
2053 // Start the debug abbrev section.
2054 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2055
2056 EmitLabel("abbrev_begin", 0);
2057
2058 // For each abbrevation.
2059 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2060 // Get abbreviation data
2061 const DIEAbbrev *Abbrev = Abbreviations[i];
2062
2063 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002064 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2065 Asm->EOL("Abbreviation Code");
Jim Laskeyef42a012006-11-02 20:12:39 +00002066
2067 // Emit the abbreviations data.
2068 Abbrev->Emit(*this);
2069
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002070 Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002071 }
2072
2073 EmitLabel("abbrev_end", 0);
2074
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002075 Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002076 }
2077 }
2078
Jim Laskey65195462006-10-30 13:35:07 +00002079 /// EmitDebugLines - Emit source line information.
2080 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002081 void EmitDebugLines() const {
2082 // Minimum line delta, thus ranging from -10..(255-10).
2083 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2084 // Maximum line delta, thus ranging from -10..(255-10).
2085 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002086
Jim Laskeyef42a012006-11-02 20:12:39 +00002087 // Start the dwarf line section.
2088 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2089
2090 // Construct the section header.
2091
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002092 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002093 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002094 EmitLabel("line_begin", 0);
2095
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002096 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Jim Laskeyef42a012006-11-02 20:12:39 +00002097
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002098 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002099 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002100 EmitLabel("line_prolog_begin", 0);
2101
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002102 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002103
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002104 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002105
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002106 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002107
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002108 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002109
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002110 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Jim Laskeyef42a012006-11-02 20:12:39 +00002111
2112 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002113 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2114 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2115 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2116 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2117 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2118 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2119 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2120 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2121 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002122
2123 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2124 const UniqueVector<SourceFileInfo>
2125 &SourceFiles = DebugInfo->getSourceFiles();
2126
2127 // Emit directories.
2128 for (unsigned DirectoryID = 1, NDID = Directories.size();
2129 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002130 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002131 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002132 Asm->EmitInt8(0); Asm->EOL("End of directories");
Jim Laskeyef42a012006-11-02 20:12:39 +00002133
2134 // Emit files.
2135 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2136 SourceID <= NSID; ++SourceID) {
2137 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002138 Asm->EmitString(SourceFile.getName());
2139 Asm->EOL("Source");
2140 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2141 Asm->EOL("Directory #");
2142 Asm->EmitULEB128Bytes(0);
2143 Asm->EOL("Mod date");
2144 Asm->EmitULEB128Bytes(0);
2145 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002146 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002147 Asm->EmitInt8(0); Asm->EOL("End of files");
Jim Laskeyef42a012006-11-02 20:12:39 +00002148
2149 EmitLabel("line_prolog_end", 0);
2150
2151 // A sequence for each text section.
2152 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2153 // Isolate current sections line info.
2154 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
2155
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002156 Asm->EOL(std::string("Section ") + SectionMap[j + 1]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002157
2158 // Dwarf assumes we start with first line of first source file.
2159 unsigned Source = 1;
2160 unsigned Line = 1;
2161
2162 // Construct rows of the address, source, line, column matrix.
2163 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2164 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey9d4209f2006-11-07 19:33:46 +00002165 unsigned LabelID = DebugInfo->MappedLabel(LineInfo.getLabelID());
2166 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002167
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002168 unsigned SourceID = LineInfo.getSourceID();
2169 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2170 unsigned DirectoryID = SourceFile.getDirectoryID();
2171 Asm->EOL(Directories[DirectoryID]
2172 + SourceFile.getName()
2173 + ":"
2174 + utostr_32(LineInfo.getLine()));
Jim Laskeyef42a012006-11-02 20:12:39 +00002175
2176 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002177 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2178 Asm->EmitInt8(TAI->getAddressSize() + 1); Asm->EOL("Op size");
2179 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2180 EmitReference("loc", LabelID); Asm->EOL("Location label");
Jim Laskeyef42a012006-11-02 20:12:39 +00002181
2182 // If change of source, then switch to the new source.
2183 if (Source != LineInfo.getSourceID()) {
2184 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002185 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2186 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002187 }
2188
2189 // If change of line.
2190 if (Line != LineInfo.getLine()) {
2191 // Determine offset.
2192 int Offset = LineInfo.getLine() - Line;
2193 int Delta = Offset - MinLineDelta;
2194
2195 // Update line.
2196 Line = LineInfo.getLine();
2197
2198 // If delta is small enough and in range...
2199 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2200 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002201 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002202 } else {
2203 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002204 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2205 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2206 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002207 }
2208 } else {
2209 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002210 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002211 }
2212 }
2213
2214 // Define last address of section.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002215 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2216 Asm->EmitInt8(TAI->getAddressSize() + 1); Asm->EOL("Op size");
2217 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2218 EmitReference("section_end", j + 1); Asm->EOL("Section end label");
Jim Laskeyef42a012006-11-02 20:12:39 +00002219
2220 // Mark end of matrix.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002221 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002222 Asm->EmitULEB128Bytes(1); Asm->EOL("");
2223 Asm->EmitInt8(1); Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002224 }
2225
2226 EmitLabel("line_end", 0);
2227
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002228 Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002229 }
2230
Jim Laskey65195462006-10-30 13:35:07 +00002231 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
2232 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002233 void EmitInitialDebugFrame() {
2234 if (!TAI->getDwarfRequiresFrameSection())
2235 return;
2236
2237 int stackGrowth =
2238 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2239 TargetFrameInfo::StackGrowsUp ?
2240 TAI->getAddressSize() : -TAI->getAddressSize();
2241
2242 // Start the dwarf frame section.
2243 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2244
2245 EmitLabel("frame_common", 0);
2246 EmitDifference("frame_common_end", 0,
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002247 "frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002248 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002249
2250 EmitLabel("frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002251 Asm->EmitInt32((int)DW_CIE_ID);
2252 Asm->EOL("CIE Identifier Tag");
2253 Asm->EmitInt8(DW_CIE_VERSION);
2254 Asm->EOL("CIE Version");
2255 Asm->EmitString("");
2256 Asm->EOL("CIE Augmentation");
2257 Asm->EmitULEB128Bytes(1);
2258 Asm->EOL("CIE Code Alignment Factor");
2259 Asm->EmitSLEB128Bytes(stackGrowth);
2260 Asm->EOL("CIE Data Alignment Factor");
2261 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister()));
2262 Asm->EOL("CIE RA Column");
Jim Laskey65195462006-10-30 13:35:07 +00002263
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002264 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002265 RI->getInitialFrameState(Moves);
2266 EmitFrameMoves(NULL, 0, Moves);
Jim Laskeyef42a012006-11-02 20:12:39 +00002267
Jim Laskeyf9e56192007-01-24 13:12:32 +00002268 Asm->EmitAlignment(2);
Jim Laskeyef42a012006-11-02 20:12:39 +00002269 EmitLabel("frame_common_end", 0);
2270
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002271 Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002272 }
2273
Jim Laskey65195462006-10-30 13:35:07 +00002274 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2275 /// section.
Jim Laskeyef42a012006-11-02 20:12:39 +00002276 void EmitFunctionDebugFrame() {
Reid Spencer5a4951e2006-11-07 06:36:36 +00002277 if (!TAI->getDwarfRequiresFrameSection())
2278 return;
Jim Laskey9d4209f2006-11-07 19:33:46 +00002279
Jim Laskeyef42a012006-11-02 20:12:39 +00002280 // Start the dwarf frame section.
2281 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2282
2283 EmitDifference("frame_end", SubprogramCount,
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002284 "frame_begin", SubprogramCount, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002285 Asm->EOL("Length of Frame Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002286
2287 EmitLabel("frame_begin", SubprogramCount);
2288
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002289 EmitDifference("frame_common", 0, "section_frame", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002290 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002291
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002292 EmitReference("func_begin", SubprogramCount);
2293 Asm->EOL("FDE initial location");
Jim Laskeyef42a012006-11-02 20:12:39 +00002294 EmitDifference("func_end", SubprogramCount,
2295 "func_begin", SubprogramCount);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002296 Asm->EOL("FDE address range");
Jim Laskeyef42a012006-11-02 20:12:39 +00002297
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002298 std::vector<MachineMove> &Moves = DebugInfo->getFrameMoves();
Jim Laskeyef42a012006-11-02 20:12:39 +00002299
2300 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2301
Jim Laskeyf9e56192007-01-24 13:12:32 +00002302 Asm->EmitAlignment(2);
Jim Laskeyef42a012006-11-02 20:12:39 +00002303 EmitLabel("frame_end", SubprogramCount);
2304
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002305 Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002306 }
2307
2308 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002309 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002310 void EmitDebugPubNames() {
2311 // Start the dwarf pubnames section.
2312 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2313
Jim Laskey5496f012006-11-09 14:52:14 +00002314 CompileUnit *Unit = GetBaseCompileUnit();
2315
2316 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002317 "pubnames_begin", Unit->getID(), true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002318 Asm->EOL("Length of Public Names Info");
Jim Laskey5496f012006-11-09 14:52:14 +00002319
2320 EmitLabel("pubnames_begin", Unit->getID());
2321
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002322 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
Jim Laskey5496f012006-11-09 14:52:14 +00002323
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002324 EmitDifference("info_begin", Unit->getID(), "section_info", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002325 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002326
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002327 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002328 Asm->EOL("Compilation Unit Length");
Jim Laskey5496f012006-11-09 14:52:14 +00002329
2330 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2331
2332 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2333 GE = Globals.end();
2334 GI != GE; ++GI) {
2335 const std::string &Name = GI->first;
2336 DIE * Entity = GI->second;
Jim Laskeyef42a012006-11-02 20:12:39 +00002337
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002338 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2339 Asm->EmitString(Name); Asm->EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00002340 }
Jim Laskey5496f012006-11-09 14:52:14 +00002341
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002342 Asm->EmitInt32(0); Asm->EOL("End Mark");
Jim Laskey5496f012006-11-09 14:52:14 +00002343 EmitLabel("pubnames_end", Unit->getID());
2344
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002345 Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002346 }
2347
2348 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002349 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002350 void EmitDebugStr() {
2351 // Check to see if it is worth the effort.
2352 if (!StringPool.empty()) {
2353 // Start the dwarf str section.
2354 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2355
2356 // For each of strings in the string pool.
2357 for (unsigned StringID = 1, N = StringPool.size();
2358 StringID <= N; ++StringID) {
2359 // Emit a label for reference from debug information entries.
2360 EmitLabel("string", StringID);
2361 // Emit the string itself.
2362 const std::string &String = StringPool[StringID];
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002363 Asm->EmitString(String); Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002364 }
2365
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002366 Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002367 }
2368 }
2369
2370 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002371 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002372 void EmitDebugLoc() {
2373 // Start the dwarf loc section.
2374 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2375
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002376 Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002377 }
2378
2379 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002380 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002381 void EmitDebugARanges() {
2382 // Start the dwarf aranges section.
2383 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2384
2385 // FIXME - Mock up
2386 #if 0
Jim Laskey5496f012006-11-09 14:52:14 +00002387 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002388
Jim Laskey5496f012006-11-09 14:52:14 +00002389 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002390 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Jim Laskey5496f012006-11-09 14:52:14 +00002391
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002392 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Jim Laskey5496f012006-11-09 14:52:14 +00002393
2394 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002395 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002396
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002397 Asm->EmitInt8(TAI->getAddressSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002398
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002399 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002400
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002401 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2402 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002403
Jim Laskey5496f012006-11-09 14:52:14 +00002404 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002405 EmitReference("text_begin", 0); Asm->EOL("Address");
2406 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002407
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002408 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2409 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Jim Laskey5496f012006-11-09 14:52:14 +00002410
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002411 Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002412 #endif
2413 }
2414
2415 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002416 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002417 void EmitDebugRanges() {
2418 // Start the dwarf ranges section.
2419 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2420
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002421 Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002422 }
2423
2424 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002425 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002426 void EmitDebugMacInfo() {
2427 // Start the dwarf macinfo section.
2428 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2429
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002430 Asm->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002431 }
2432
Jim Laskey65195462006-10-30 13:35:07 +00002433 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2434 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002435 void ConstructCompileUnitDIEs() {
2436 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
2437
2438 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00002439 unsigned ID = DebugInfo->RecordSource(CUW[i]);
2440 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002441 CompileUnits.push_back(Unit);
2442 }
2443 }
2444
Jim Laskey65195462006-10-30 13:35:07 +00002445 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2446 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002447 void ConstructGlobalDIEs() {
2448 std::vector<GlobalVariableDesc *> GlobalVariables =
2449 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
2450
2451 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2452 GlobalVariableDesc *GVD = GlobalVariables[i];
2453 NewGlobalVariable(GVD);
2454 }
2455 }
Jim Laskey65195462006-10-30 13:35:07 +00002456
2457 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2458 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002459 void ConstructSubprogramDIEs() {
2460 std::vector<SubprogramDesc *> Subprograms =
2461 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
2462
2463 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2464 SubprogramDesc *SPD = Subprograms[i];
2465 NewSubprogram(SPD);
2466 }
2467 }
Jim Laskey65195462006-10-30 13:35:07 +00002468
2469 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
2470 ///
2471 bool ShouldEmitDwarf() const { return shouldEmit; }
2472
2473public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002474 //===--------------------------------------------------------------------===//
2475 // Main entry points.
2476 //
2477 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
2478 : O(OS)
2479 , Asm(A)
2480 , TAI(T)
2481 , TD(Asm->TM.getTargetData())
2482 , RI(Asm->TM.getRegisterInfo())
2483 , M(NULL)
2484 , MF(NULL)
2485 , DebugInfo(NULL)
2486 , didInitial(false)
2487 , shouldEmit(false)
2488 , SubprogramCount(0)
2489 , CompileUnits()
2490 , AbbreviationsSet(InitAbbreviationsSetSize)
2491 , Abbreviations()
2492 , ValuesSet(InitValuesSetSize)
2493 , Values()
2494 , StringPool()
2495 , DescToUnitMap()
2496 , SectionMap()
2497 , SectionSourceLines()
2498 {
2499 }
2500 virtual ~Dwarf() {
2501 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2502 delete CompileUnits[i];
2503 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2504 delete Values[j];
2505 }
2506
Jim Laskey65195462006-10-30 13:35:07 +00002507 // Accessors.
2508 //
2509 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
2510
2511 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2512 /// created it. Set by the target AsmPrinter.
Jim Laskeyef42a012006-11-02 20:12:39 +00002513 void SetDebugInfo(MachineDebugInfo *DI) {
2514 // Make sure initial declarations are made.
2515 if (!DebugInfo && DI->hasInfo()) {
2516 DebugInfo = DI;
2517 shouldEmit = true;
2518
2519 // Emit initial sections
2520 EmitInitial();
2521
2522 // Create all the compile unit DIEs.
2523 ConstructCompileUnitDIEs();
2524
2525 // Create DIEs for each of the externally visible global variables.
2526 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002527
Jim Laskeyef42a012006-11-02 20:12:39 +00002528 // Create DIEs for each of the externally visible subprograms.
2529 ConstructSubprogramDIEs();
2530
2531 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00002532 SectionMap.insert(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002533 }
2534 }
2535
Jim Laskey65195462006-10-30 13:35:07 +00002536 /// BeginModule - Emit all Dwarf sections that should come prior to the
2537 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002538 void BeginModule(Module *M) {
2539 this->M = M;
2540
2541 if (!ShouldEmitDwarf()) return;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002542 Asm->EOL("Dwarf Begin Module");
Jim Laskeyef42a012006-11-02 20:12:39 +00002543 }
2544
Jim Laskey65195462006-10-30 13:35:07 +00002545 /// EndModule - Emit all Dwarf sections that should come after the content.
2546 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002547 void EndModule() {
2548 if (!ShouldEmitDwarf()) return;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002549 Asm->EOL("Dwarf End Module");
Jim Laskeyef42a012006-11-02 20:12:39 +00002550
2551 // Standard sections final addresses.
2552 Asm->SwitchToTextSection(TAI->getTextSection());
2553 EmitLabel("text_end", 0);
2554 Asm->SwitchToDataSection(TAI->getDataSection());
2555 EmitLabel("data_end", 0);
2556
2557 // End text sections.
2558 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2559 Asm->SwitchToTextSection(SectionMap[i].c_str());
2560 EmitLabel("section_end", i);
2561 }
2562
2563 // Compute DIE offsets and sizes.
2564 SizeAndOffsets();
2565
2566 // Emit all the DIEs into a debug info section
2567 EmitDebugInfo();
2568
2569 // Corresponding abbreviations into a abbrev section.
2570 EmitAbbreviations();
2571
2572 // Emit source line correspondence into a debug line section.
2573 EmitDebugLines();
2574
2575 // Emit info into a debug pubnames section.
2576 EmitDebugPubNames();
2577
2578 // Emit info into a debug str section.
2579 EmitDebugStr();
2580
2581 // Emit info into a debug loc section.
2582 EmitDebugLoc();
2583
2584 // Emit info into a debug aranges section.
2585 EmitDebugARanges();
2586
2587 // Emit info into a debug ranges section.
2588 EmitDebugRanges();
2589
2590 // Emit info into a debug macinfo section.
2591 EmitDebugMacInfo();
2592 }
2593
Jim Laskey65195462006-10-30 13:35:07 +00002594 /// BeginFunction - Gather pre-function debug information. Assumes being
2595 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002596 void BeginFunction(MachineFunction *MF) {
2597 this->MF = MF;
2598
2599 if (!ShouldEmitDwarf()) return;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002600 Asm->EOL("Dwarf Begin Function");
Jim Laskeyef42a012006-11-02 20:12:39 +00002601
2602 // Begin accumulating function debug information.
2603 DebugInfo->BeginFunction(MF);
2604
2605 // Assumes in correct section after the entry point.
2606 EmitLabel("func_begin", ++SubprogramCount);
2607 }
2608
Jim Laskey65195462006-10-30 13:35:07 +00002609 /// EndFunction - Gather and emit post-function debug information.
2610 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002611 void EndFunction() {
2612 if (!ShouldEmitDwarf()) return;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002613 Asm->EOL("Dwarf End Function");
Jim Laskeyef42a012006-11-02 20:12:39 +00002614
2615 // Define end label for subprogram.
2616 EmitLabel("func_end", SubprogramCount);
2617
2618 // Get function line info.
2619 const std::vector<SourceLineInfo> &LineInfos = DebugInfo->getSourceLines();
2620
2621 if (!LineInfos.empty()) {
2622 // Get section line info.
2623 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2624 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2625 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2626 // Append the function info to section info.
2627 SectionLineInfos.insert(SectionLineInfos.end(),
2628 LineInfos.begin(), LineInfos.end());
2629 }
2630
2631 // Construct scopes for subprogram.
2632 ConstructRootScope(DebugInfo->getRootScope());
2633
2634 // Emit function frame information.
2635 EmitFunctionDebugFrame();
2636
2637 // Reset the line numbers for the next function.
2638 DebugInfo->ClearLineInfo();
2639
2640 // Clear function debug information.
2641 DebugInfo->EndFunction();
2642 }
Jim Laskey65195462006-10-30 13:35:07 +00002643};
2644
Jim Laskey0d086af2006-02-27 12:43:29 +00002645} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00002646
2647//===----------------------------------------------------------------------===//
2648
Jim Laskeyd18e2892006-01-20 20:34:06 +00002649/// Emit - Print the abbreviation using the specified Dwarf writer.
2650///
Jim Laskey65195462006-10-30 13:35:07 +00002651void DIEAbbrev::Emit(const Dwarf &DW) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002652 // Emit its Dwarf tag type.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002653 DW.getAsm()->EmitULEB128Bytes(Tag);
2654 DW.getAsm()->EOL(TagString(Tag));
Jim Laskeyd18e2892006-01-20 20:34:06 +00002655
2656 // Emit whether it has children DIEs.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002657 DW.getAsm()->EmitULEB128Bytes(ChildrenFlag);
2658 DW.getAsm()->EOL(ChildrenString(ChildrenFlag));
Jim Laskeyd18e2892006-01-20 20:34:06 +00002659
2660 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00002661 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002662 const DIEAbbrevData &AttrData = Data[i];
2663
2664 // Emit attribute type.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002665 DW.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
2666 DW.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00002667
2668 // Emit form type.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002669 DW.getAsm()->EmitULEB128Bytes(AttrData.getForm());
2670 DW.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00002671 }
2672
2673 // Mark end of abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002674 DW.getAsm()->EmitULEB128Bytes(0); DW.getAsm()->EOL("EOM(1)");
2675 DW.getAsm()->EmitULEB128Bytes(0); DW.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00002676}
2677
2678#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00002679void DIEAbbrev::print(std::ostream &O) {
2680 O << "Abbreviation @"
2681 << std::hex << (intptr_t)this << std::dec
2682 << " "
2683 << TagString(Tag)
2684 << " "
2685 << ChildrenString(ChildrenFlag)
2686 << "\n";
2687
2688 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
2689 O << " "
2690 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002691 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00002692 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00002693 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002694 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00002695}
Bill Wendlinge8156192006-12-07 01:30:32 +00002696void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002697#endif
2698
2699//===----------------------------------------------------------------------===//
2700
Jim Laskeyef42a012006-11-02 20:12:39 +00002701#ifndef NDEBUG
2702void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00002703 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002704}
Jim Laskeyef42a012006-11-02 20:12:39 +00002705#endif
2706
2707//===----------------------------------------------------------------------===//
2708
Jim Laskey063e7652006-01-17 17:31:53 +00002709/// EmitValue - Emit integer of appropriate size.
2710///
Jim Laskey65195462006-10-30 13:35:07 +00002711void DIEInteger::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +00002712 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00002713 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00002714 case DW_FORM_ref1: // Fall thru
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002715 case DW_FORM_data1: DW.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002716 case DW_FORM_ref2: // Fall thru
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002717 case DW_FORM_data2: DW.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002718 case DW_FORM_ref4: // Fall thru
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002719 case DW_FORM_data4: DW.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002720 case DW_FORM_ref8: // Fall thru
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002721 case DW_FORM_data8: DW.getAsm()->EmitInt64(Integer); break;
2722 case DW_FORM_udata: DW.getAsm()->EmitULEB128Bytes(Integer); break;
2723 case DW_FORM_sdata: DW.getAsm()->EmitSLEB128Bytes(Integer); break;
2724 default: assert(0 && "DIE Value form not supported yet"); break;
2725 }
2726}
2727
2728/// SizeOf - Determine size of integer value in bytes.
2729///
2730unsigned DIEInteger::SizeOf(const Dwarf &DW, unsigned Form) const {
2731 switch (Form) {
2732 case DW_FORM_flag: // Fall thru
2733 case DW_FORM_ref1: // Fall thru
2734 case DW_FORM_data1: return sizeof(int8_t);
2735 case DW_FORM_ref2: // Fall thru
2736 case DW_FORM_data2: return sizeof(int16_t);
2737 case DW_FORM_ref4: // Fall thru
2738 case DW_FORM_data4: return sizeof(int32_t);
2739 case DW_FORM_ref8: // Fall thru
2740 case DW_FORM_data8: return sizeof(int64_t);
2741 case DW_FORM_udata: return DW.getAsm()->SizeULEB128(Integer);
2742 case DW_FORM_sdata: return DW.getAsm()->SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00002743 default: assert(0 && "DIE Value form not supported yet"); break;
2744 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002745 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00002746}
2747
Jim Laskey063e7652006-01-17 17:31:53 +00002748//===----------------------------------------------------------------------===//
2749
2750/// EmitValue - Emit string value.
2751///
Jim Laskey65195462006-10-30 13:35:07 +00002752void DIEString::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002753 DW.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00002754}
2755
Jim Laskey063e7652006-01-17 17:31:53 +00002756//===----------------------------------------------------------------------===//
2757
2758/// EmitValue - Emit label value.
2759///
Jim Laskey65195462006-10-30 13:35:07 +00002760void DIEDwarfLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002761 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +00002762}
2763
2764/// SizeOf - Determine size of label value in bytes.
2765///
Jim Laskey65195462006-10-30 13:35:07 +00002766unsigned DIEDwarfLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002767 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002768}
Jim Laskeyef42a012006-11-02 20:12:39 +00002769
Jim Laskey063e7652006-01-17 17:31:53 +00002770//===----------------------------------------------------------------------===//
2771
Jim Laskeyd18e2892006-01-20 20:34:06 +00002772/// EmitValue - Emit label value.
2773///
Jim Laskey65195462006-10-30 13:35:07 +00002774void DIEObjectLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002775 DW.EmitReference(Label);
2776}
2777
2778/// SizeOf - Determine size of label value in bytes.
2779///
Jim Laskey65195462006-10-30 13:35:07 +00002780unsigned DIEObjectLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00002781 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00002782}
2783
2784//===----------------------------------------------------------------------===//
2785
Jim Laskey063e7652006-01-17 17:31:53 +00002786/// EmitValue - Emit delta value.
2787///
Jim Laskey65195462006-10-30 13:35:07 +00002788void DIEDelta::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002789 bool IsSmall = Form == DW_FORM_data4;
2790 DW.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00002791}
2792
2793/// SizeOf - Determine size of delta value in bytes.
2794///
Jim Laskey65195462006-10-30 13:35:07 +00002795unsigned DIEDelta::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002796 if (Form == DW_FORM_data4) return 4;
Jim Laskey563321a2006-09-06 18:34:40 +00002797 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00002798}
2799
2800//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00002801
Jim Laskeyb8509c52006-03-23 18:07:55 +00002802/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002803///
Jim Laskey65195462006-10-30 13:35:07 +00002804void DIEntry::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002805 DW.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00002806}
Jim Laskeyd18e2892006-01-20 20:34:06 +00002807
2808//===----------------------------------------------------------------------===//
2809
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002810/// ComputeSize - calculate the size of the block.
2811///
Jim Laskey65195462006-10-30 13:35:07 +00002812unsigned DIEBlock::ComputeSize(Dwarf &DW) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002813 if (!Size) {
2814 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2815
2816 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2817 Size += Values[i]->SizeOf(DW, AbbrevData[i].getForm());
2818 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002819 }
2820 return Size;
2821}
2822
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002823/// EmitValue - Emit block data.
2824///
Jim Laskey65195462006-10-30 13:35:07 +00002825void DIEBlock::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002826 switch (Form) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002827 case DW_FORM_block1: DW.getAsm()->EmitInt8(Size); break;
2828 case DW_FORM_block2: DW.getAsm()->EmitInt16(Size); break;
2829 case DW_FORM_block4: DW.getAsm()->EmitInt32(Size); break;
2830 case DW_FORM_block: DW.getAsm()->EmitULEB128Bytes(Size); break;
2831 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002832 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002833
2834 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
2835
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002836 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002837 DW.getAsm()->EOL("");
Jim Laskeyef42a012006-11-02 20:12:39 +00002838 Values[i]->EmitValue(DW, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002839 }
2840}
2841
2842/// SizeOf - Determine size of block data in bytes.
2843///
Jim Laskey65195462006-10-30 13:35:07 +00002844unsigned DIEBlock::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002845 switch (Form) {
2846 case DW_FORM_block1: return Size + sizeof(int8_t);
2847 case DW_FORM_block2: return Size + sizeof(int16_t);
2848 case DW_FORM_block4: return Size + sizeof(int32_t);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002849 case DW_FORM_block: return Size + DW.getAsm()->SizeULEB128(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002850 default: assert(0 && "Improper form for block"); break;
2851 }
2852 return 0;
2853}
2854
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002855//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00002856/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00002857
2858DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002859 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00002860 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00002861}
Jim Laskeyef42a012006-11-02 20:12:39 +00002862
Jim Laskeyb8509c52006-03-23 18:07:55 +00002863/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
2864///
2865void DIE::AddSiblingOffset() {
2866 DIEInteger *DI = new DIEInteger(0);
2867 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002868 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002869}
2870
Jim Laskeyef42a012006-11-02 20:12:39 +00002871/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002872///
Jim Laskeyef42a012006-11-02 20:12:39 +00002873void DIE::Profile(FoldingSetNodeID &ID) {
2874 Abbrev.Profile(ID);
2875
2876 for (unsigned i = 0, N = Children.size(); i < N; ++i)
2877 ID.AddPointer(Children[i]);
2878
2879 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2880 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00002881}
Jim Laskeyef42a012006-11-02 20:12:39 +00002882
2883#ifndef NDEBUG
2884void DIE::print(std::ostream &O, unsigned IncIndent) {
2885 static unsigned IndentCount = 0;
2886 IndentCount += IncIndent;
2887 const std::string Indent(IndentCount, ' ');
2888 bool isBlock = Abbrev.getTag() == 0;
2889
2890 if (!isBlock) {
2891 O << Indent
2892 << "Die: "
2893 << "0x" << std::hex << (intptr_t)this << std::dec
2894 << ", Offset: " << Offset
2895 << ", Size: " << Size
2896 << "\n";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00002897
Jim Laskeyef42a012006-11-02 20:12:39 +00002898 O << Indent
2899 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00002900 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00002901 << ChildrenString(Abbrev.getChildrenFlag());
2902 } else {
2903 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00002904 }
2905 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002906
Jim Laskeyef42a012006-11-02 20:12:39 +00002907 const std::vector<DIEAbbrevData> &Data = Abbrev.getData();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002908
Jim Laskeyef42a012006-11-02 20:12:39 +00002909 IndentCount += 2;
2910 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
2911 O << Indent;
2912 if (!isBlock) {
2913 O << AttributeString(Data[i].getAttribute());
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00002914 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00002915 O << "Blk[" << i << "]";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00002916 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002917 O << " "
2918 << FormEncodingString(Data[i].getForm())
2919 << " ";
2920 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00002921 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002922 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002923 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00002924
Jim Laskeyef42a012006-11-02 20:12:39 +00002925 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2926 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00002927 }
Jim Laskey063e7652006-01-17 17:31:53 +00002928
Jim Laskeyef42a012006-11-02 20:12:39 +00002929 if (!isBlock) O << "\n";
2930 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002931}
2932
Jim Laskeyef42a012006-11-02 20:12:39 +00002933void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00002934 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00002935}
Jim Laskeybd761842006-02-27 17:27:12 +00002936#endif
Jim Laskey65195462006-10-30 13:35:07 +00002937
2938//===----------------------------------------------------------------------===//
2939/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00002940///
Jim Laskey65195462006-10-30 13:35:07 +00002941
2942DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
2943 const TargetAsmInfo *T) {
2944 DW = new Dwarf(OS, A, T);
2945}
2946
2947DwarfWriter::~DwarfWriter() {
2948 delete DW;
2949}
2950
2951/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2952/// created it. Set by the target AsmPrinter.
2953void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
2954 DW->SetDebugInfo(DI);
2955}
2956
2957/// BeginModule - Emit all Dwarf sections that should come prior to the
2958/// content.
2959void DwarfWriter::BeginModule(Module *M) {
2960 DW->BeginModule(M);
2961}
2962
2963/// EndModule - Emit all Dwarf sections that should come after the content.
2964///
2965void DwarfWriter::EndModule() {
2966 DW->EndModule();
2967}
2968
2969/// BeginFunction - Gather pre-function debug information. Assumes being
2970/// emitted immediately after the function entry point.
2971void DwarfWriter::BeginFunction(MachineFunction *MF) {
2972 DW->BeginFunction(MF);
2973}
2974
2975/// EndFunction - Gather and emit post-function debug information.
2976///
2977void DwarfWriter::EndFunction() {
2978 DW->EndFunction();
2979}