blob: 9896faacc16d89db030ee37dfeb3ca2fa9632eb0 [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 Laskey063e7652006-01-17 17:31:53 +000016#include "llvm/ADT/StringExtras.h"
Jim Laskey52060a02006-01-24 00:49:18 +000017#include "llvm/Module.h"
18#include "llvm/Type.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000019#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeyb2efb852006-01-04 22:28:25 +000020#include "llvm/CodeGen/MachineDebugInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000021#include "llvm/CodeGen/MachineLocation.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000022#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000023#include "llvm/Support/CommandLine.h"
Jim Laskey52060a02006-01-24 00:49:18 +000024#include "llvm/Support/Mangler.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000025#include "llvm/Target/MRegisterInfo.h"
Jim Laskey52060a02006-01-24 00:49:18 +000026#include "llvm/Target/TargetMachine.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000027
Jim Laskeyb2efb852006-01-04 22:28:25 +000028#include <iostream>
Jim Laskeya7cea6f2006-01-04 13:52:30 +000029
Jim Laskeyb2efb852006-01-04 22:28:25 +000030using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000031using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000032
33static cl::opt<bool>
34DwarfVerbose("dwarf-verbose", cl::Hidden,
Jim Laskey063e7652006-01-17 17:31:53 +000035 cl::desc("Add comments to Dwarf directives."));
36
Jim Laskey0d086af2006-02-27 12:43:29 +000037namespace llvm {
38
Jim Laskey063e7652006-01-17 17:31:53 +000039//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000040// Forward declarations.
41//
42class CompileUnit;
43class DIE;
Jim Laskey063e7652006-01-17 17:31:53 +000044
Jim Laskey0d086af2006-02-27 12:43:29 +000045//===----------------------------------------------------------------------===//
Jim Laskeyf01e5472006-03-03 15:06:57 +000046// CompileUnit - This dwarf writer support class manages information associate
47// with a source file.
Jim Laskeybd761842006-02-27 17:27:12 +000048class CompileUnit {
49private:
50 CompileUnitDesc *Desc; // Compile unit debug descriptor.
51 unsigned ID; // File ID for source.
Jim Laskeyb8509c52006-03-23 18:07:55 +000052 DIE *Die; // Compile unit debug information entry.
Jim Laskeybd761842006-02-27 17:27:12 +000053 std::map<std::string, DIE *> Globals; // A map of globally visible named
54 // entities for this unit.
55
56public:
57 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
58 : Desc(CUD)
59 , ID(I)
60 , Die(D)
61 , Globals()
62 {}
63
64 ~CompileUnit();
65
66 // Accessors.
67 CompileUnitDesc *getDesc() const { return Desc; }
68 unsigned getID() const { return ID; }
69 DIE* getDie() const { return Die; }
70 std::map<std::string, DIE *> &getGlobals() { return Globals; }
71
72 /// hasContent - Return true if this compile unit has something to write out.
73 ///
74 bool hasContent() const;
75
76 /// AddGlobal - Add a new global entity to the compile unit.
77 ///
78 void AddGlobal(const std::string &Name, DIE *Die);
79
80};
81
82//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000083// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
84// Dwarf abbreviation.
85class DIEAbbrevData {
86private:
87 unsigned Attribute; // Dwarf attribute code.
88 unsigned Form; // Dwarf form code.
89
90public:
91 DIEAbbrevData(unsigned A, unsigned F)
92 : Attribute(A)
93 , Form(F)
94 {}
95
Jim Laskeybd761842006-02-27 17:27:12 +000096 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +000097 unsigned getAttribute() const { return Attribute; }
98 unsigned getForm() const { return Form; }
99
100 /// operator== - Used by DIEAbbrev to locate entry.
101 ///
102 bool operator==(const DIEAbbrevData &DAD) const {
103 return Attribute == DAD.Attribute && Form == DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000104 }
Jim Laskey063e7652006-01-17 17:31:53 +0000105
Jim Laskey0d086af2006-02-27 12:43:29 +0000106 /// operator!= - Used by DIEAbbrev to locate entry.
107 ///
108 bool operator!=(const DIEAbbrevData &DAD) const {
109 return Attribute != DAD.Attribute || Form != DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000110 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000111
112 /// operator< - Used by DIEAbbrev to locate entry.
113 ///
114 bool operator<(const DIEAbbrevData &DAD) const {
115 return Attribute < DAD.Attribute ||
116 (Attribute == DAD.Attribute && Form < DAD.Form);
117 }
118};
Jim Laskey063e7652006-01-17 17:31:53 +0000119
Jim Laskey0d086af2006-02-27 12:43:29 +0000120//===----------------------------------------------------------------------===//
121// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
122// information object.
123class DIEAbbrev {
124private:
125 unsigned Tag; // Dwarf tag code.
126 unsigned ChildrenFlag; // Dwarf children flag.
127 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
Jim Laskey063e7652006-01-17 17:31:53 +0000128
Jim Laskey0d086af2006-02-27 12:43:29 +0000129public:
Jim Laskey063e7652006-01-17 17:31:53 +0000130
Jim Laskey0d086af2006-02-27 12:43:29 +0000131 DIEAbbrev(unsigned T, unsigned C)
132 : Tag(T)
133 , ChildrenFlag(C)
134 , Data()
135 {}
136 ~DIEAbbrev() {}
137
Jim Laskeybd761842006-02-27 17:27:12 +0000138 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000139 unsigned getTag() const { return Tag; }
140 unsigned getChildrenFlag() const { return ChildrenFlag; }
141 const std::vector<DIEAbbrevData> &getData() const { return Data; }
142 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskey063e7652006-01-17 17:31:53 +0000143
Jim Laskey0d086af2006-02-27 12:43:29 +0000144 /// operator== - Used by UniqueVector to locate entry.
145 ///
146 bool operator==(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000147
Jim Laskey0d086af2006-02-27 12:43:29 +0000148 /// operator< - Used by UniqueVector to locate entry.
149 ///
150 bool operator<(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000151
Jim Laskey0d086af2006-02-27 12:43:29 +0000152 /// AddAttribute - Adds another set of attribute information to the
153 /// abbreviation.
154 void AddAttribute(unsigned Attribute, unsigned Form) {
155 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000156 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000157
Jim Laskeyb8509c52006-03-23 18:07:55 +0000158 /// AddFirstAttribute - Adds a set of attribute information to the front
159 /// of the abbreviation.
160 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
161 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
162 }
163
Jim Laskey0d086af2006-02-27 12:43:29 +0000164 /// Emit - Print the abbreviation using the specified Dwarf writer.
165 ///
166 void Emit(const DwarfWriter &DW) const;
167
168#ifndef NDEBUG
169 void print(std::ostream &O);
170 void dump();
171#endif
172};
Jim Laskey063e7652006-01-17 17:31:53 +0000173
Jim Laskey0d086af2006-02-27 12:43:29 +0000174//===----------------------------------------------------------------------===//
175// DIEValue - A debug information entry value.
176//
177class DIEValue {
178public:
179 enum {
180 isInteger,
181 isString,
182 isLabel,
183 isAsIsLabel,
184 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000185 isEntry,
186 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000187 };
188
189 unsigned Type; // Type of the value
190
191 DIEValue(unsigned T) : Type(T) {}
192 virtual ~DIEValue() {}
193
194 // Implement isa/cast/dyncast.
195 static bool classof(const DIEValue *) { return true; }
196
197 /// EmitValue - Emit value via the Dwarf writer.
198 ///
199 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
200
201 /// SizeOf - Return the size of a value in bytes.
202 ///
203 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
204};
Jim Laskey063e7652006-01-17 17:31:53 +0000205
Jim Laskey0d086af2006-02-27 12:43:29 +0000206//===----------------------------------------------------------------------===//
207// DWInteger - An integer value DIE.
208//
209class DIEInteger : public DIEValue {
210private:
211 uint64_t Integer;
212
213public:
214 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000215
Jim Laskey0d086af2006-02-27 12:43:29 +0000216 // Implement isa/cast/dyncast.
217 static bool classof(const DIEInteger *) { return true; }
218 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
219
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000220 /// BestForm - Choose the best form for integer.
221 ///
222 unsigned BestForm(bool IsSigned);
223
Jim Laskey0d086af2006-02-27 12:43:29 +0000224 /// EmitValue - Emit integer of appropriate size.
225 ///
226 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
227
228 /// SizeOf - Determine size of integer value in bytes.
229 ///
230 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
231};
Jim Laskey063e7652006-01-17 17:31:53 +0000232
Jim Laskey0d086af2006-02-27 12:43:29 +0000233//===----------------------------------------------------------------------===//
234// DIEString - A string value DIE.
235//
236struct DIEString : public DIEValue {
237 const std::string String;
238
239 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000240
Jim Laskey0d086af2006-02-27 12:43:29 +0000241 // Implement isa/cast/dyncast.
242 static bool classof(const DIEString *) { return true; }
243 static bool classof(const DIEValue *S) { return S->Type == isString; }
244
245 /// EmitValue - Emit string value.
246 ///
247 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
248
249 /// SizeOf - Determine size of string value in bytes.
250 ///
251 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
252};
Jim Laskey063e7652006-01-17 17:31:53 +0000253
Jim Laskey0d086af2006-02-27 12:43:29 +0000254//===----------------------------------------------------------------------===//
255// DIEDwarfLabel - A Dwarf internal label expression DIE.
256//
257struct DIEDwarfLabel : public DIEValue {
258 const DWLabel Label;
259
260 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000261
Jim Laskey0d086af2006-02-27 12:43:29 +0000262 // Implement isa/cast/dyncast.
263 static bool classof(const DIEDwarfLabel *) { return true; }
264 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
265
266 /// EmitValue - Emit label value.
267 ///
268 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
269
270 /// SizeOf - Determine size of label value in bytes.
271 ///
272 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
273};
Jim Laskey063e7652006-01-17 17:31:53 +0000274
Jim Laskey063e7652006-01-17 17:31:53 +0000275
Jim Laskey0d086af2006-02-27 12:43:29 +0000276//===----------------------------------------------------------------------===//
277// DIEObjectLabel - A label to an object in code or data.
278//
279struct DIEObjectLabel : public DIEValue {
280 const std::string Label;
281
282 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000283
Jim Laskey0d086af2006-02-27 12:43:29 +0000284 // Implement isa/cast/dyncast.
285 static bool classof(const DIEObjectLabel *) { return true; }
286 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
287
288 /// EmitValue - Emit label value.
289 ///
290 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
291
292 /// SizeOf - Determine size of label value in bytes.
293 ///
294 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
295};
Jim Laskey063e7652006-01-17 17:31:53 +0000296
Jim Laskey0d086af2006-02-27 12:43:29 +0000297//===----------------------------------------------------------------------===//
298// DIEDelta - A simple label difference DIE.
299//
300struct DIEDelta : public DIEValue {
301 const DWLabel LabelHi;
302 const DWLabel LabelLo;
303
304 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
305 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000306
Jim Laskey0d086af2006-02-27 12:43:29 +0000307 // Implement isa/cast/dyncast.
308 static bool classof(const DIEDelta *) { return true; }
309 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
310
311 /// EmitValue - Emit delta value.
312 ///
313 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
314
315 /// SizeOf - Determine size of delta value in bytes.
316 ///
317 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
318};
Jim Laskey063e7652006-01-17 17:31:53 +0000319
Jim Laskey0d086af2006-02-27 12:43:29 +0000320//===----------------------------------------------------------------------===//
321// DIEntry - A pointer to a debug information entry.
322//
323struct DIEntry : public DIEValue {
324 DIE *Entry;
325
326 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
327
328 // Implement isa/cast/dyncast.
329 static bool classof(const DIEntry *) { return true; }
330 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
331
Jim Laskeyb8509c52006-03-23 18:07:55 +0000332 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000333 ///
334 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
335
Jim Laskeyb8509c52006-03-23 18:07:55 +0000336 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000337 ///
338 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
339};
340
341//===----------------------------------------------------------------------===//
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000342// DIEBlock - A block of values. Primarily used for location expressions.
343//
344struct DIEBlock : public DIEValue {
345 unsigned Size; // Size in bytes excluding size header.
346 std::vector<unsigned> Forms; // Data forms.
347 std::vector<DIEValue *> Values; // Block values.
348
349 DIEBlock()
350 : DIEValue(isBlock)
351 , Size(0)
352 , Forms()
353 , Values()
354 {}
355 ~DIEBlock();
356
357 // Implement isa/cast/dyncast.
358 static bool classof(const DIEBlock *) { return true; }
359 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
360
361 /// ComputeSize - calculate the size of the block.
362 ///
363 unsigned ComputeSize(DwarfWriter &DW);
364
365 /// BestForm - Choose the best form for data.
366 ///
367 unsigned BestForm();
368
369 /// EmitValue - Emit block data.
370 ///
371 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
372
373 /// SizeOf - Determine size of block data in bytes.
374 ///
375 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
376
377 /// AddUInt - Add an unsigned integer value.
378 ///
379 void AddUInt(unsigned Form, uint64_t Integer);
380
381 /// AddSInt - Add an signed integer value.
382 ///
383 void AddSInt(unsigned Form, int64_t Integer);
384
385 /// AddString - Add a std::string value.
386 ///
387 void AddString(unsigned Form, const std::string &String);
388
389 /// AddLabel - Add a Dwarf label value.
390 ///
391 void AddLabel(unsigned Form, const DWLabel &Label);
392
393 /// AddObjectLabel - Add a non-Dwarf label value.
394 ///
395 void AddObjectLabel(unsigned Form, const std::string &Label);
396
397 /// AddDelta - Add a label delta value.
398 ///
399 void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
400
401 /// AddDIEntry - Add a DIE value.
402 ///
403 void AddDIEntry(unsigned Form, DIE *Entry);
404
405};
406
407//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +0000408// DIE - A structured debug information entry. Has an abbreviation which
409// describes it's organization.
410class DIE {
411private:
412 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
413 unsigned AbbrevID; // Decribing abbreviation ID.
414 unsigned Offset; // Offset in debug info section.
415 unsigned Size; // Size of instance + children.
416 std::vector<DIE *> Children; // Children DIEs.
417 std::vector<DIEValue *> Values; // Attributes values.
418
419public:
420 DIE(unsigned Tag);
421 ~DIE();
422
Jim Laskeybd761842006-02-27 17:27:12 +0000423 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000424 unsigned getAbbrevID() const { return AbbrevID; }
425 unsigned getOffset() const { return Offset; }
426 unsigned getSize() const { return Size; }
427 const std::vector<DIE *> &getChildren() const { return Children; }
428 const std::vector<DIEValue *> &getValues() const { return Values; }
429 void setOffset(unsigned O) { Offset = O; }
430 void setSize(unsigned S) { Size = S; }
431
432 /// SiblingOffset - Return the offset of the debug information entry's
433 /// sibling.
434 unsigned SiblingOffset() const { return Offset + Size; }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000435
436 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
437 ///
438 void AddSiblingOffset();
Jim Laskey0d086af2006-02-27 12:43:29 +0000439
440 /// AddUInt - Add an unsigned integer attribute data and value.
441 ///
442 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
443
444 /// AddSInt - Add an signed integer attribute data and value.
445 ///
446 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
447
448 /// AddString - Add a std::string attribute data and value.
449 ///
450 void AddString(unsigned Attribute, unsigned Form,
451 const std::string &String);
452
453 /// AddLabel - Add a Dwarf label attribute data and value.
454 ///
455 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
456
457 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
458 ///
459 void AddObjectLabel(unsigned Attribute, unsigned Form,
460 const std::string &Label);
461
462 /// AddDelta - Add a label delta attribute data and value.
463 ///
464 void AddDelta(unsigned Attribute, unsigned Form,
465 const DWLabel &Hi, const DWLabel &Lo);
466
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000467 /// AddDIEntry - Add a DIE attribute data and value.
Jim Laskey0d086af2006-02-27 12:43:29 +0000468 ///
469 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
470
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000471 /// AddBlock - Add block data.
472 ///
473 void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
474
Jim Laskey0d086af2006-02-27 12:43:29 +0000475 /// Complete - Indicate that all attributes have been added and
476 /// ready to get an abbreviation ID.
477 ///
478 void Complete(DwarfWriter &DW);
479
480 /// AddChild - Add a child to the DIE.
481 void AddChild(DIE *Child);
482};
483
484} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +0000485
486//===----------------------------------------------------------------------===//
487
Jim Laskeybd761842006-02-27 17:27:12 +0000488CompileUnit::~CompileUnit() {
489 delete Die;
490}
491
492/// hasContent - Return true if this compile unit has something to write out.
493///
494bool CompileUnit::hasContent() const {
495 return !Die->getChildren().empty();
496}
497
498/// AddGlobal - Add a new global entity to the compile unit.
499///
500void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
501 Globals[Name] = Die;
502}
503
504//===----------------------------------------------------------------------===//
505
Jim Laskeyd18e2892006-01-20 20:34:06 +0000506/// operator== - Used by UniqueVector to locate entry.
507///
508bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
509 if (Tag != DA.Tag) return false;
510 if (ChildrenFlag != DA.ChildrenFlag) return false;
511 if (Data.size() != DA.Data.size()) return false;
512
Jim Laskey52060a02006-01-24 00:49:18 +0000513 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000514 if (Data[i] != DA.Data[i]) return false;
Jim Laskeyd18e2892006-01-20 20:34:06 +0000515 }
516
517 return true;
518}
519
520/// operator< - Used by UniqueVector to locate entry.
521///
522bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
523 if (Tag != DA.Tag) return Tag < DA.Tag;
524 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
525 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
526
Jim Laskey52060a02006-01-24 00:49:18 +0000527 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000528 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +0000529 }
530
531 return false;
532}
533
534/// Emit - Print the abbreviation using the specified Dwarf writer.
535///
536void DIEAbbrev::Emit(const DwarfWriter &DW) const {
537 // Emit its Dwarf tag type.
538 DW.EmitULEB128Bytes(Tag);
539 DW.EOL(TagString(Tag));
540
541 // Emit whether it has children DIEs.
542 DW.EmitULEB128Bytes(ChildrenFlag);
543 DW.EOL(ChildrenString(ChildrenFlag));
544
545 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +0000546 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000547 const DIEAbbrevData &AttrData = Data[i];
548
549 // Emit attribute type.
550 DW.EmitULEB128Bytes(AttrData.getAttribute());
551 DW.EOL(AttributeString(AttrData.getAttribute()));
552
553 // Emit form type.
554 DW.EmitULEB128Bytes(AttrData.getForm());
555 DW.EOL(FormEncodingString(AttrData.getForm()));
556 }
557
558 // Mark end of abbreviation.
559 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
560 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
561}
562
563#ifndef NDEBUG
564 void DIEAbbrev::print(std::ostream &O) {
565 O << "Abbreviation @"
Jeff Cohen05ebc8d2006-01-25 17:18:50 +0000566 << std::hex << (intptr_t)this << std::dec
Jim Laskeyd18e2892006-01-20 20:34:06 +0000567 << " "
568 << TagString(Tag)
569 << " "
570 << ChildrenString(ChildrenFlag)
571 << "\n";
572
Jim Laskey52060a02006-01-24 00:49:18 +0000573 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000574 O << " "
575 << AttributeString(Data[i].getAttribute())
576 << " "
577 << FormEncodingString(Data[i].getForm())
578 << "\n";
579 }
580 }
581 void DIEAbbrev::dump() { print(std::cerr); }
582#endif
583
584//===----------------------------------------------------------------------===//
585
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000586/// BestForm - Choose the best form for integer.
587///
588unsigned DIEInteger::BestForm(bool IsSigned) {
589 if (IsSigned) {
590 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
591 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
592 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
593 } else {
594 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
595 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
596 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
597 }
598 return DW_FORM_data8;
599}
600
Jim Laskey063e7652006-01-17 17:31:53 +0000601/// EmitValue - Emit integer of appropriate size.
602///
603void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
604 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +0000605 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000606 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000607 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000608 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000609 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000610 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000611 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000612 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000613 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +0000614 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
615 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +0000616 default: assert(0 && "DIE Value form not supported yet"); break;
617 }
618}
619
620/// SizeOf - Determine size of integer value in bytes.
621///
622unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
623 switch (Form) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000624 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000625 case DW_FORM_ref1: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000626 case DW_FORM_data1: return sizeof(int8_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000627 case DW_FORM_ref2: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000628 case DW_FORM_data2: return sizeof(int16_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000629 case DW_FORM_ref4: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000630 case DW_FORM_data4: return sizeof(int32_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000631 case DW_FORM_ref8: // Fall thru
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000632 case DW_FORM_data8: return sizeof(int64_t);
Jim Laskey40020172006-01-20 21:02:36 +0000633 case DW_FORM_udata: return DW.SizeULEB128(Integer);
634 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +0000635 default: assert(0 && "DIE Value form not supported yet"); break;
636 }
637 return 0;
638}
639
640//===----------------------------------------------------------------------===//
641
642/// EmitValue - Emit string value.
643///
644void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000645 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +0000646}
647
648/// SizeOf - Determine size of string value in bytes.
649///
650unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey40020172006-01-20 21:02:36 +0000651 return String.size() + sizeof(char); // sizeof('\0');
Jim Laskey063e7652006-01-17 17:31:53 +0000652}
653
654//===----------------------------------------------------------------------===//
655
656/// EmitValue - Emit label value.
657///
Jim Laskey52060a02006-01-24 00:49:18 +0000658void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000659 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +0000660}
661
662/// SizeOf - Determine size of label value in bytes.
663///
Jim Laskey52060a02006-01-24 00:49:18 +0000664unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +0000665 return DW.getAddressSize();
666}
667
668//===----------------------------------------------------------------------===//
669
Jim Laskeyd18e2892006-01-20 20:34:06 +0000670/// EmitValue - Emit label value.
671///
Jim Laskey52060a02006-01-24 00:49:18 +0000672void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000673 DW.EmitReference(Label);
674}
675
676/// SizeOf - Determine size of label value in bytes.
677///
Jim Laskey52060a02006-01-24 00:49:18 +0000678unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000679 return DW.getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +0000680}
681
682//===----------------------------------------------------------------------===//
683
Jim Laskey063e7652006-01-17 17:31:53 +0000684/// EmitValue - Emit delta value.
685///
686void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000687 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +0000688}
689
690/// SizeOf - Determine size of delta value in bytes.
691///
692unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
693 return DW.getAddressSize();
694}
695
696//===----------------------------------------------------------------------===//
Jim Laskeyb8509c52006-03-23 18:07:55 +0000697/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000698///
699void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +0000700 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +0000701}
702
Jim Laskeyb8509c52006-03-23 18:07:55 +0000703/// SizeOf - Determine size of debug information entry value in bytes.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000704///
705unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
706 return sizeof(int32_t);
707}
708
709//===----------------------------------------------------------------------===//
710
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000711DIEBlock::~DIEBlock() {
712 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
713 delete Values[i];
714 }
715}
716
717/// ComputeSize - calculate the size of the block.
718///
719unsigned DIEBlock::ComputeSize(DwarfWriter &DW) {
720 Size = 0;
721 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
722 Size += Values[i]->SizeOf(DW, Forms[i]);
723 }
724 return Size;
725}
726
727/// BestForm - Choose the best form for data.
728///
729unsigned DIEBlock::BestForm() {
730 if ((unsigned char)Size == Size) return DW_FORM_block1;
731 if ((unsigned short)Size == Size) return DW_FORM_block2;
732 if ((unsigned int)Size == Size) return DW_FORM_block4;
733 return DW_FORM_block;
734}
735
736/// EmitValue - Emit block data.
737///
738void DIEBlock::EmitValue(const DwarfWriter &DW, unsigned Form) const {
739 switch (Form) {
740 case DW_FORM_block1: DW.EmitInt8(Size); break;
741 case DW_FORM_block2: DW.EmitInt16(Size); break;
742 case DW_FORM_block4: DW.EmitInt32(Size); break;
743 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
744 default: assert(0 && "Improper form for block"); break;
745 }
746 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
747 DW.EOL("");
748 Values[i]->EmitValue(DW, Forms[i]);
749 }
750}
751
752/// SizeOf - Determine size of block data in bytes.
753///
754unsigned DIEBlock::SizeOf(const DwarfWriter &DW, unsigned Form) const {
755 switch (Form) {
756 case DW_FORM_block1: return Size + sizeof(int8_t);
757 case DW_FORM_block2: return Size + sizeof(int16_t);
758 case DW_FORM_block4: return Size + sizeof(int32_t);
759 case DW_FORM_block: return Size + DW.SizeULEB128(Size);
760 default: assert(0 && "Improper form for block"); break;
761 }
762 return 0;
763}
764
765/// AddUInt - Add an unsigned integer value.
766///
767void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
768 DIEInteger *DI = new DIEInteger(Integer);
769 Values.push_back(DI);
770 if (Form == 0) Form = DI->BestForm(false);
771 Forms.push_back(Form);
772}
773
774/// AddSInt - Add an signed integer value.
775///
776void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
777 DIEInteger *DI = new DIEInteger(Integer);
778 Values.push_back(DI);
779 if (Form == 0) Form = DI->BestForm(true);
780 Forms.push_back(Form);
781}
782
783/// AddString - Add a std::string value.
784///
785void DIEBlock::AddString(unsigned Form, const std::string &String) {
786 Values.push_back(new DIEString(String));
787 Forms.push_back(Form);
788}
789
790/// AddLabel - Add a Dwarf label value.
791///
792void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
793 Values.push_back(new DIEDwarfLabel(Label));
794 Forms.push_back(Form);
795}
796
797/// AddObjectLabel - Add a non-Dwarf label value.
798///
799void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
800 Values.push_back(new DIEObjectLabel(Label));
801 Forms.push_back(Form);
802}
803
804/// AddDelta - Add a label delta value.
805///
806void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
807 Values.push_back(new DIEDelta(Hi, Lo));
808 Forms.push_back(Form);
809}
810
811/// AddDIEntry - Add a DIE value.
812///
813void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
814 Values.push_back(new DIEntry(Entry));
815 Forms.push_back(Form);
816}
817
818//===----------------------------------------------------------------------===//
819
Jim Laskey0420f2a2006-02-22 19:02:11 +0000820DIE::DIE(unsigned Tag)
821: Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
Jim Laskeyd18e2892006-01-20 20:34:06 +0000822, AbbrevID(0)
823, Offset(0)
824, Size(0)
Jim Laskeyd18e2892006-01-20 20:34:06 +0000825, Children()
826, Values()
827{}
828
829DIE::~DIE() {
830 if (Abbrev) delete Abbrev;
831
Jim Laskey52060a02006-01-24 00:49:18 +0000832 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000833 delete Children[i];
834 }
835
Jim Laskey52060a02006-01-24 00:49:18 +0000836 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000837 delete Values[j];
838 }
Jim Laskeyd18e2892006-01-20 20:34:06 +0000839}
840
Jim Laskeyb8509c52006-03-23 18:07:55 +0000841/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
842///
843void DIE::AddSiblingOffset() {
844 DIEInteger *DI = new DIEInteger(0);
845 Values.insert(Values.begin(), DI);
846 Abbrev->AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
847}
848
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000849/// AddUInt - Add an unsigned integer attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000850///
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000851void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000852 DIEInteger *DI = new DIEInteger(Integer);
853 Values.push_back(DI);
854 if (!Form) Form = DI->BestForm(false);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000855 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000856}
857
858/// AddSInt - Add an signed integer attribute data and value.
859///
860void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000861 DIEInteger *DI = new DIEInteger(Integer);
862 Values.push_back(DI);
863 if (!Form) Form = DI->BestForm(true);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000864 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000865}
866
867/// AddString - Add a std::string attribute data and value.
868///
869void DIE::AddString(unsigned Attribute, unsigned Form,
870 const std::string &String) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000871 Values.push_back(new DIEString(String));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000872 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000873}
874
875/// AddLabel - Add a Dwarf label attribute data and value.
876///
877void DIE::AddLabel(unsigned Attribute, unsigned Form,
878 const DWLabel &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000879 Values.push_back(new DIEDwarfLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000880 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000881}
882
Jim Laskey52060a02006-01-24 00:49:18 +0000883/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000884///
Jim Laskey52060a02006-01-24 00:49:18 +0000885void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
886 const std::string &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000887 Values.push_back(new DIEObjectLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000888 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000889}
890
891/// AddDelta - Add a label delta attribute data and value.
892///
893void DIE::AddDelta(unsigned Attribute, unsigned Form,
894 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000895 Values.push_back(new DIEDelta(Hi, Lo));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000896 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000897}
898
899/// AddDIEntry - Add a DIE attribute data and value.
900///
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000901void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000902 Values.push_back(new DIEntry(Entry));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000903 Abbrev->AddAttribute(Attribute, Form);
904}
905
906/// AddBlock - Add block data.
907///
908void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
909 assert(Block->Size && "Block size has not been computed");
910 Values.push_back(Block);
911 if (!Form) Form = Block->BestForm();
912 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000913}
914
915/// Complete - Indicate that all attributes have been added and ready to get an
916/// abbreviation ID.
917void DIE::Complete(DwarfWriter &DW) {
918 AbbrevID = DW.NewAbbreviation(Abbrev);
919 delete Abbrev;
920 Abbrev = NULL;
921}
922
923/// AddChild - Add a child to the DIE.
924///
925void DIE::AddChild(DIE *Child) {
Jim Laskey0420f2a2006-02-22 19:02:11 +0000926 assert(Abbrev && "Adding children without an abbreviation");
927 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000928 Children.push_back(Child);
929}
930
931//===----------------------------------------------------------------------===//
932
Jim Laskey0420f2a2006-02-22 19:02:11 +0000933/// DWContext
Jim Laskeyd18e2892006-01-20 20:34:06 +0000934
935//===----------------------------------------------------------------------===//
Jim Laskey063e7652006-01-17 17:31:53 +0000936
937/// PrintHex - Print a value as a hexidecimal value.
938///
939void DwarfWriter::PrintHex(int Value) const {
940 O << "0x" << std::hex << Value << std::dec;
941}
942
943/// EOL - Print a newline character to asm stream. If a comment is present
944/// then it will be printed first. Comments should not contain '\n'.
945void DwarfWriter::EOL(const std::string &Comment) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000946 if (DwarfVerbose && !Comment.empty()) {
Jim Laskey063e7652006-01-17 17:31:53 +0000947 O << "\t"
948 << Asm->CommentString
949 << " "
950 << Comment;
951 }
952 O << "\n";
953}
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000954
Jim Laskeyb8509c52006-03-23 18:07:55 +0000955/// EmitAlign - Print a align directive.
956///
957void DwarfWriter::EmitAlign(unsigned Alignment) const {
958 O << Asm->AlignDirective << Alignment << "\n";
959}
960
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000961/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
Jim Laskey063e7652006-01-17 17:31:53 +0000962/// unsigned leb128 value.
963void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000964 if (hasLEB128) {
965 O << "\t.uleb128\t"
966 << Value;
967 } else {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000968 O << Asm->Data8bitsDirective;
Jim Laskey063e7652006-01-17 17:31:53 +0000969 PrintULEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000970 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000971}
972
973/// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
Jim Laskey063e7652006-01-17 17:31:53 +0000974/// signed leb128 value.
975void DwarfWriter::EmitSLEB128Bytes(int Value) const {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000976 if (hasLEB128) {
977 O << "\t.sleb128\t"
978 << Value;
979 } else {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000980 O << Asm->Data8bitsDirective;
Jim Laskey063e7652006-01-17 17:31:53 +0000981 PrintSLEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000982 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000983}
984
Jim Laskey063e7652006-01-17 17:31:53 +0000985/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +0000986/// representing an unsigned leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +0000987void DwarfWriter::PrintULEB128(unsigned Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000988 do {
989 unsigned Byte = Value & 0x7f;
990 Value >>= 7;
991 if (Value) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +0000992 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000993 if (Value) O << ", ";
994 } while (Value);
995}
996
Jim Laskey063e7652006-01-17 17:31:53 +0000997/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
998/// value.
999unsigned DwarfWriter::SizeULEB128(unsigned Value) {
1000 unsigned Size = 0;
1001 do {
1002 Value >>= 7;
1003 Size += sizeof(int8_t);
1004 } while (Value);
1005 return Size;
1006}
1007
1008/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +00001009/// representing a signed leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +00001010void DwarfWriter::PrintSLEB128(int Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001011 int Sign = Value >> (8 * sizeof(Value) - 1);
1012 bool IsMore;
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001013
Jim Laskeyb2efb852006-01-04 22:28:25 +00001014 do {
1015 unsigned Byte = Value & 0x7f;
1016 Value >>= 7;
1017 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1018 if (IsMore) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001019 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001020 if (IsMore) O << ", ";
1021 } while (IsMore);
1022}
1023
Jim Laskey063e7652006-01-17 17:31:53 +00001024/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
1025/// value.
1026unsigned DwarfWriter::SizeSLEB128(int Value) {
1027 unsigned Size = 0;
1028 int Sign = Value >> (8 * sizeof(Value) - 1);
1029 bool IsMore;
1030
1031 do {
1032 unsigned Byte = Value & 0x7f;
1033 Value >>= 7;
1034 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1035 Size += sizeof(int8_t);
1036 } while (IsMore);
1037 return Size;
1038}
1039
Jim Laskeyda427fa2006-01-27 20:31:25 +00001040/// EmitInt8 - Emit a byte directive and value.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001041///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001042void DwarfWriter::EmitInt8(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001043 O << Asm->Data8bitsDirective;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001044 PrintHex(Value & 0xFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001045}
1046
Jim Laskeyda427fa2006-01-27 20:31:25 +00001047/// EmitInt16 - Emit a short directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001048///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001049void DwarfWriter::EmitInt16(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001050 O << Asm->Data16bitsDirective;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001051 PrintHex(Value & 0xFFFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001052}
1053
Jim Laskeyda427fa2006-01-27 20:31:25 +00001054/// EmitInt32 - Emit a long directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001055///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001056void DwarfWriter::EmitInt32(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001057 O << Asm->Data32bitsDirective;
1058 PrintHex(Value);
1059}
1060
Jim Laskeyda427fa2006-01-27 20:31:25 +00001061/// EmitInt64 - Emit a long long directive and value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001062///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001063void DwarfWriter::EmitInt64(uint64_t Value) const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001064 if (Asm->Data64bitsDirective) {
1065 O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
1066 } else {
1067 const TargetData &TD = Asm->TM.getTargetData();
1068
1069 if (TD.isBigEndian()) {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001070 EmitInt32(unsigned(Value >> 32)); O << "\n";
1071 EmitInt32(unsigned(Value));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001072 } else {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001073 EmitInt32(unsigned(Value)); O << "\n";
1074 EmitInt32(unsigned(Value >> 32));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001075 }
1076 }
1077}
1078
Jim Laskey063e7652006-01-17 17:31:53 +00001079/// EmitString - Emit a string with quotes and a null terminator.
1080/// Special characters are emitted properly. (Eg. '\t')
1081void DwarfWriter::EmitString(const std::string &String) const {
1082 O << Asm->AsciiDirective
1083 << "\"";
Jim Laskey52060a02006-01-24 00:49:18 +00001084 for (unsigned i = 0, N = String.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001085 unsigned char C = String[i];
1086
1087 if (!isascii(C) || iscntrl(C)) {
1088 switch(C) {
1089 case '\b': O << "\\b"; break;
1090 case '\f': O << "\\f"; break;
1091 case '\n': O << "\\n"; break;
1092 case '\r': O << "\\r"; break;
1093 case '\t': O << "\\t"; break;
1094 default:
1095 O << '\\';
1096 O << char('0' + (C >> 6));
1097 O << char('0' + (C >> 3));
1098 O << char('0' + (C >> 0));
1099 break;
1100 }
1101 } else if (C == '\"') {
1102 O << "\\\"";
1103 } else if (C == '\'') {
1104 O << "\\\'";
1105 } else {
1106 O << C;
1107 }
1108 }
1109 O << "\\0\"";
1110}
1111
1112/// PrintLabelName - Print label name in form used by Dwarf writer.
1113///
1114void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001115 O << Asm->PrivateGlobalPrefix
1116 << "debug_"
Jim Laskey0420f2a2006-02-22 19:02:11 +00001117 << Tag;
1118 if (Number) O << Number;
Jim Laskeyb2efb852006-01-04 22:28:25 +00001119}
1120
Jim Laskey063e7652006-01-17 17:31:53 +00001121/// EmitLabel - Emit location label for internal use by Dwarf.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001122///
Jim Laskey063e7652006-01-17 17:31:53 +00001123void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
1124 PrintLabelName(Tag, Number);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001125 O << ":\n";
1126}
1127
Jim Laskeye719a7c2006-01-18 16:54:26 +00001128/// EmitReference - Emit a reference to a label.
Jim Laskey063e7652006-01-17 17:31:53 +00001129///
Jim Laskeye719a7c2006-01-18 16:54:26 +00001130void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001131 if (AddressSize == 4)
1132 O << Asm->Data32bitsDirective;
1133 else
1134 O << Asm->Data64bitsDirective;
1135
1136 PrintLabelName(Tag, Number);
1137}
Jim Laskey73683212006-01-21 00:59:54 +00001138void DwarfWriter::EmitReference(const std::string &Name) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001139 if (AddressSize == 4)
1140 O << Asm->Data32bitsDirective;
1141 else
1142 O << Asm->Data64bitsDirective;
1143
1144 O << Name;
1145}
Jim Laskey063e7652006-01-17 17:31:53 +00001146
1147/// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
1148/// assemblers do not accept absolute expressions with data directives, so there
1149/// is an option (needsSet) to use an intermediary 'set' expression.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001150void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
1151 const char *TagLo, unsigned NumberLo) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001152 if (needsSet) {
1153 static unsigned SetCounter = 0;
Jim Laskeyd18e2892006-01-20 20:34:06 +00001154
Jim Laskey063e7652006-01-17 17:31:53 +00001155 O << "\t.set\t";
1156 PrintLabelName("set", SetCounter);
1157 O << ",";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001158 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001159 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001160 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001161 O << "\n";
1162
1163 if (AddressSize == sizeof(int32_t))
1164 O << Asm->Data32bitsDirective;
1165 else
1166 O << Asm->Data64bitsDirective;
1167
1168 PrintLabelName("set", SetCounter);
1169
Jim Laskey52060a02006-01-24 00:49:18 +00001170 ++SetCounter;
Jim Laskey063e7652006-01-17 17:31:53 +00001171 } else {
1172 if (AddressSize == sizeof(int32_t))
1173 O << Asm->Data32bitsDirective;
1174 else
1175 O << Asm->Data64bitsDirective;
1176
Jim Laskeyd18e2892006-01-20 20:34:06 +00001177 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001178 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001179 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001180 }
1181}
1182
Jim Laskeyd18e2892006-01-20 20:34:06 +00001183/// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
Jim Laskey063e7652006-01-17 17:31:53 +00001184///
Jim Laskeyd18e2892006-01-20 20:34:06 +00001185unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
1186 return Abbreviations.insert(*Abbrev);
1187}
1188
1189/// NewString - Add a string to the constant pool and returns a label.
1190///
1191DWLabel DwarfWriter::NewString(const std::string &String) {
1192 unsigned StringID = StringPool.insert(String);
1193 return DWLabel("string", StringID);
1194}
1195
Jim Laskeyb8509c52006-03-23 18:07:55 +00001196/// AddSourceLine - Add location information to specified debug information
1197/// entry.
1198void DwarfWriter::AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1199 if (File && Line) {
1200 CompileUnit *FileUnit = FindCompileUnit(File);
1201 unsigned FileID = FileUnit->getID();
1202 Die->AddUInt(DW_AT_decl_file, 0, FileID);
1203 Die->AddUInt(DW_AT_decl_line, 0, Line);
1204 }
1205}
1206
1207
Jim Laskey0420f2a2006-02-22 19:02:11 +00001208/// NewBasicType - Creates a new basic type if necessary, then adds to the
1209/// owner.
1210/// FIXME - Should never be needed.
Jim Laskey92ae7402006-03-01 18:20:30 +00001211DIE *DwarfWriter::NewBasicType(DIE *Context, Type *Ty) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001212 DIE *&Slot = TypeToDieMap[Ty];
1213 if (Slot) return Slot;
1214
1215 const char *Name;
1216 unsigned Size;
1217 unsigned Encoding = 0;
1218
1219 switch (Ty->getTypeID()) {
1220 case Type::UByteTyID:
1221 Name = "unsigned char";
1222 Size = 1;
1223 Encoding = DW_ATE_unsigned_char;
1224 break;
1225 case Type::SByteTyID:
1226 Name = "char";
1227 Size = 1;
1228 Encoding = DW_ATE_signed_char;
1229 break;
1230 case Type::UShortTyID:
1231 Name = "unsigned short";
1232 Size = 2;
1233 Encoding = DW_ATE_unsigned;
1234 break;
1235 case Type::ShortTyID:
1236 Name = "short";
1237 Size = 2;
1238 Encoding = DW_ATE_signed;
1239 break;
1240 case Type::UIntTyID:
1241 Name = "unsigned int";
1242 Size = 4;
1243 Encoding = DW_ATE_unsigned;
1244 break;
1245 case Type::IntTyID:
1246 Name = "int";
1247 Size = 4;
1248 Encoding = DW_ATE_signed;
1249 break;
1250 case Type::ULongTyID:
1251 Name = "unsigned long long";
1252 Size = 7;
1253 Encoding = DW_ATE_unsigned;
1254 break;
1255 case Type::LongTyID:
1256 Name = "long long";
1257 Size = 7;
1258 Encoding = DW_ATE_signed;
1259 break;
1260 case Type::FloatTyID:
1261 Name = "float";
1262 Size = 4;
1263 Encoding = DW_ATE_float;
1264 break;
1265 case Type::DoubleTyID:
1266 Name = "double";
1267 Size = 8;
1268 Encoding = DW_ATE_float;
1269 break;
1270 default:
1271 // FIXME - handle more complex types.
1272 Name = "unknown";
1273 Size = 1;
1274 Encoding = DW_ATE_address;
1275 break;
1276 }
1277
1278 // construct the type DIE.
1279 Slot = new DIE(DW_TAG_base_type);
1280 Slot->AddString(DW_AT_name, DW_FORM_string, Name);
1281 Slot->AddUInt (DW_AT_byte_size, 0, Size);
1282 Slot->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
1283
Jim Laskey92ae7402006-03-01 18:20:30 +00001284 // Add to context.
1285 Context->AddChild(Slot);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001286
1287 return Slot;
1288}
1289
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001290/// NewType - Create a new type DIE.
1291///
Jim Laskey92ae7402006-03-01 18:20:30 +00001292DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc) {
Jim Laskey92ae7402006-03-01 18:20:30 +00001293 if (!TyDesc) return NewBasicType(Context, Type::IntTy);
1294
1295 // FIXME - Should handle other contexts that compile units.
Jim Laskey69906002006-02-24 16:46:40 +00001296
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001297 // Check for pre-existence.
1298 DIE *&Slot = DescToDieMap[TyDesc];
1299 if (Slot) return Slot;
1300
1301 // Get core information.
1302 const std::string &Name = TyDesc->getName();
Jim Laskey288fe0f2006-03-01 18:13:05 +00001303 uint64_t Size = TyDesc->getSize() >> 3;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001304
Jim Laskey434b40b2006-02-23 22:37:30 +00001305 DIE *Ty = NULL;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001306
Jim Laskey434b40b2006-02-23 22:37:30 +00001307 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001308 // Fundamental types like int, float, bool
Jim Laskey434b40b2006-02-23 22:37:30 +00001309 Slot = Ty = new DIE(DW_TAG_base_type);
1310 unsigned Encoding = BasicTy->getEncoding();
1311 Ty->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
Jim Laskey69906002006-02-24 16:46:40 +00001312 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001313 // Create specific DIE.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001314 Slot = Ty = new DIE(DerivedTy->getTag());
Jim Laskey69906002006-02-24 16:46:40 +00001315
1316 // Map to main type, void will not have a type.
1317 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
Jim Laskey92ae7402006-03-01 18:20:30 +00001318 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4, NewType(Context, FromTy));
Jim Laskey69906002006-02-24 16:46:40 +00001319 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001320 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
Jim Laskeyf8913f12006-03-01 17:53:02 +00001321 // Create specific DIE.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001322 Slot = Ty = new DIE(CompTy->getTag());
Jim Laskeyf8913f12006-03-01 17:53:02 +00001323 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1324
1325 switch (CompTy->getTag()) {
Jim Laskey9c4447a2006-03-01 20:39:36 +00001326 case DW_TAG_array_type: {
Jim Laskeyf8913f12006-03-01 17:53:02 +00001327 // Add element type.
1328 if (TypeDesc *FromTy = CompTy->getFromType()) {
Jim Laskey92ae7402006-03-01 18:20:30 +00001329 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4, NewType(Context, FromTy));
Jim Laskeyf8913f12006-03-01 17:53:02 +00001330 }
1331 // Don't emit size attribute.
1332 Size = 0;
1333
1334 // Construct an anonymous type for index type.
1335 DIE *IndexTy = new DIE(DW_TAG_base_type);
1336 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1337 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1338 // Add to context.
Jim Laskey92ae7402006-03-01 18:20:30 +00001339 Context->AddChild(IndexTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001340
1341 // Add subranges to array type.
1342 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1343 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1344 int64_t Lo = SRD->getLo();
1345 int64_t Hi = SRD->getHi();
1346 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1347
1348 // If a range is available.
1349 if (Lo != Hi) {
1350 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1351 // Only add low if non-zero.
Jim Laskey6a3eb012006-03-01 23:52:37 +00001352 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1353 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001354 }
1355 Ty->AddChild(Subrange);
1356 }
1357
1358 break;
1359 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001360 case DW_TAG_structure_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +00001361 case DW_TAG_union_type: {
Jim Laskeyf01e5472006-03-03 15:06:57 +00001362 // FIXME - this is just the basics.
1363 // Add elements to structure type.
1364 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1365 DerivedTypeDesc *MemberDesc = cast<DerivedTypeDesc>(Elements[i]);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001366
1367 // Extract the basic information.
Jim Laskeyf01e5472006-03-03 15:06:57 +00001368 const std::string &Name = MemberDesc->getName();
Jim Laskeyf01e5472006-03-03 15:06:57 +00001369 TypeDesc *MemTy = MemberDesc->getFromType();
1370 uint64_t Size = MemberDesc->getSize();
Chris Lattner2695de42006-03-09 17:48:46 +00001371 uint64_t Align = MemberDesc->getAlign();
Jim Laskeyf01e5472006-03-03 15:06:57 +00001372 uint64_t Offset = MemberDesc->getOffset();
1373
Jim Laskeyb8509c52006-03-23 18:07:55 +00001374 // Construct member debug information entry.
Jim Laskeyf01e5472006-03-03 15:06:57 +00001375 DIE *Member = new DIE(DW_TAG_member);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001376
Jim Laskeyb8509c52006-03-23 18:07:55 +00001377 // Add name if not "".
Jim Laskeyf01e5472006-03-03 15:06:57 +00001378 if (!Name.empty()) Member->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001379 // Add location if available.
1380 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001381
Jim Laskey54689c22006-03-09 13:28:47 +00001382 // Most of the time the field info is the same as the members.
1383 uint64_t FieldSize = Size;
1384 uint64_t FieldAlign = Align;
1385 uint64_t FieldOffset = Offset;
Jim Laskey20c3ed82006-03-07 15:51:33 +00001386
Jim Laskeyf01e5472006-03-03 15:06:57 +00001387 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1388 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1389 NewType(Context, FromTy));
Jim Laskey54689c22006-03-09 13:28:47 +00001390 FieldSize = FromTy->getSize();
1391 FieldAlign = FromTy->getSize();
Jim Laskey20c3ed82006-03-07 15:51:33 +00001392 }
1393
Jim Laskey54689c22006-03-09 13:28:47 +00001394 // Unless we have a bit field.
1395 if (FieldSize != Size) {
1396 // Construct the alignment mask.
1397 uint64_t AlignMask = ~(FieldAlign - 1);
1398 // Determine the high bit + 1 of the declared size.
1399 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1400 // Work backwards to determine the base offset of the field.
1401 FieldOffset = HiMark - FieldSize;
1402 // Now normalize offset to the field.
1403 Offset -= FieldOffset;
1404
1405 // Maybe we need to work from the other.
1406 const TargetData &TD = Asm->TM.getTargetData();
1407 if (TD.isLittleEndian()) Offset = FieldSize - (Offset + Size);
1408
1409 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1410 Member->AddUInt(DW_AT_bit_size, 0, Size);
1411 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
Jim Laskeyf01e5472006-03-03 15:06:57 +00001412 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001413
1414 // Add computation for offset.
1415 DIEBlock *Block = new DIEBlock();
1416 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
Jim Laskey54689c22006-03-09 13:28:47 +00001417 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001418 Block->ComputeSize(*this);
1419 Member->AddBlock(DW_AT_data_member_location, 0, Block);
1420
Jim Laskeyf01e5472006-03-03 15:06:57 +00001421 Ty->AddChild(Member);
1422 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001423 break;
1424 }
Jim Laskey9c4447a2006-03-01 20:39:36 +00001425 case DW_TAG_enumeration_type: {
Jim Laskey6a3eb012006-03-01 23:52:37 +00001426 // Add enumerators to enumeration type.
1427 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1428 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1429 const std::string &Name = ED->getName();
1430 int64_t Value = ED->getValue();
1431 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1432 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1433 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1434 Ty->AddChild(Enumerator);
1435 }
1436
Jim Laskeyf8913f12006-03-01 17:53:02 +00001437 break;
1438 }
1439 default: break;
1440 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001441 }
Jim Laskey434b40b2006-02-23 22:37:30 +00001442
1443 assert(Ty && "Type not supported yet");
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001444
Jim Laskey69906002006-02-24 16:46:40 +00001445 // Add size if non-zero (derived types don't have a size.)
Jim Laskey434b40b2006-02-23 22:37:30 +00001446 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
Jim Laskey69906002006-02-24 16:46:40 +00001447 // Add name if not anonymous or intermediate type.
Jim Laskey434b40b2006-02-23 22:37:30 +00001448 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001449 // Add source line info if available.
1450 AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
Jim Laskey434b40b2006-02-23 22:37:30 +00001451
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001452 // Add to context owner.
Jim Laskey92ae7402006-03-01 18:20:30 +00001453 Context->AddChild(Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001454
1455 return Slot;
1456}
1457
Jim Laskeyb8509c52006-03-23 18:07:55 +00001458/// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey063e7652006-01-17 17:31:53 +00001459///
Jim Laskeybd761842006-02-27 17:27:12 +00001460CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1461 unsigned ID) {
1462 // Construct debug information entry.
1463 DIE *Die = new DIE(DW_TAG_compile_unit);
1464 Die->AddLabel (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0));
1465 Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1466 Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
1467 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1468 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1469 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1470 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1471
Jim Laskeyb8509c52006-03-23 18:07:55 +00001472 // Add debug information entry to descriptor map.
Jim Laskeybd761842006-02-27 17:27:12 +00001473 DescToDieMap[UnitDesc] = Die;
1474
1475 // Construct compile unit.
1476 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1477
1478 // Add Unit to compile unit map.
1479 DescToUnitMap[UnitDesc] = Unit;
1480
1481 return Unit;
1482}
Jim Laskey0420f2a2006-02-22 19:02:11 +00001483
Jim Laskeybd761842006-02-27 17:27:12 +00001484/// FindCompileUnit - Get the compile unit for the given descriptor.
1485///
1486CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1487 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1488 assert(Unit && "Missing compile unit.");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001489 return Unit;
Jim Laskey063e7652006-01-17 17:31:53 +00001490}
1491
Jim Laskey0420f2a2006-02-22 19:02:11 +00001492/// NewGlobalVariable - Add a new global variable DIE.
1493///
1494DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
1495 // Check for pre-existence.
1496 DIE *&Slot = DescToDieMap[GVD];
1497 if (Slot) return Slot;
1498
1499 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001500 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1501 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001502 // Get the global variable itself.
1503 GlobalVariable *GV = GVD->getGlobalVariable();
1504 // Generate the mangled name.
1505 std::string MangledName = Asm->Mang->getValueName(GV);
1506
1507 // Gather the details (simplify add attribute code.)
1508 const std::string &Name = GVD->getName();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001509
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001510 // Get the global's type.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001511 DIE *Type = NewType(Unit->getDie(), GVD->getType());
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001512
1513 // Create the globale variable DIE.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001514 DIE *VariableDie = new DIE(DW_TAG_variable);
1515 VariableDie->AddString (DW_AT_name, DW_FORM_string, Name);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001516 VariableDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1517 VariableDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001518
1519 // Add source line info if available.
1520 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001521
Jim Laskeyb8509c52006-03-23 18:07:55 +00001522 // Add address.
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001523 DIEBlock *Block = new DIEBlock();
1524 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
1525 Block->AddObjectLabel(DW_FORM_udata, MangledName);
1526 Block->ComputeSize(*this);
1527 VariableDie->AddBlock(DW_AT_location, 0, Block);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001528
1529 // Add to map.
1530 Slot = VariableDie;
1531
1532 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001533 Unit->getDie()->AddChild(VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001534
1535 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001536 // FIXME - need to check external flag.
1537 Unit->AddGlobal(Name, VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001538
1539 return VariableDie;
1540}
1541
1542/// NewSubprogram - Add a new subprogram DIE.
1543///
1544DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
1545 // Check for pre-existence.
1546 DIE *&Slot = DescToDieMap[SPD];
1547 if (Slot) return Slot;
1548
1549 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001550 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1551 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001552
1553 // Gather the details (simplify add attribute code.)
1554 const std::string &Name = SPD->getName();
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001555 DIE *Type = NewBasicType(Unit->getDie(), Type::IntTy);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001556 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001557
Jim Laskey8a8e9752006-02-27 20:37:42 +00001558 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001559 SubprogramDie->AddString (DW_AT_name, DW_FORM_string, Name);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001560 SubprogramDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001561 SubprogramDie->AddUInt (DW_AT_external, DW_FORM_flag, IsExternal);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001562
Jim Laskeyb8509c52006-03-23 18:07:55 +00001563 // Add source line info if available.
1564 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1565
Jim Laskey0420f2a2006-02-22 19:02:11 +00001566 // Add to map.
1567 Slot = SubprogramDie;
1568
1569 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001570 Unit->getDie()->AddChild(SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001571
1572 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001573 Unit->AddGlobal(Name, SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001574
1575 return SubprogramDie;
1576}
1577
Jim Laskeyb8509c52006-03-23 18:07:55 +00001578
1579/// NewScopeVariable - Create a new scope variable.
1580///
1581DIE *DwarfWriter::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1582 // Get the descriptor.
1583 VariableDesc *VD = DV->getDesc();
1584
1585 // Translate tag to proper Dwarf tag. The result variable is dropped for now.
1586 unsigned Tag;
1587 switch (VD->getTag()) {
1588 case DW_TAG_return_variable: return NULL;
1589 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1590 case DW_TAG_auto_variable: // fall thru
1591 default: Tag = DW_TAG_variable; break;
1592 }
1593
1594 // Define variable debug information entry.
1595 DIE *VariableDie = new DIE(Tag);
1596 VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
1597
1598 // Add source line info if available.
1599 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1600
1601 // Add variable type.
1602 DIE *Type = NewType(Unit->getDie(), VD->getType());
1603 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1604
1605 // Get variable address.
1606 MachineLocation Location;
1607 Asm->TM.getRegisterInfo()->getLocation(*MF, DV->getFrameIndex(), Location);
1608
1609 // Add computation for variable.
1610 DIEBlock *Block = new DIEBlock();
1611 if (Location.isRegister()) {
1612 // FIXME - This is a real hack.
1613 Block->AddUInt(DW_FORM_data1, DW_OP_reg0 + Location.getRegister());
1614 } else {
1615 // FIXME - This is a real hack.
1616 Block->AddUInt(DW_FORM_data1, DW_OP_breg0 + Location.getRegister());
1617 Block->AddUInt(DW_FORM_sdata, Location.getOffset());
1618 }
1619 Block->ComputeSize(*this);
1620 VariableDie->AddBlock(DW_AT_location, 0, Block);
1621
1622 return VariableDie;
1623}
1624
1625/// ConstructScope - Construct the components of a scope.
1626///
1627void DwarfWriter::ConstructScope(DebugScope *ParentScope,
1628 DIE *ParentDie, CompileUnit *Unit) {
1629 // Add variables to scope.
1630 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1631 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1632 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1633 if (VariableDie) ParentDie->AddChild(VariableDie);
1634 }
1635
1636 // Add nested scopes.
1637 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1638 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1639 // Define the Scope debug information entry.
1640 DebugScope *Scope = Scopes[j];
1641 // FIXME - Ignore inlined functions for the time being.
1642 if (Scope->getParent()) continue;
1643
1644 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1645
1646 // Add the scope bounds.
1647 if (unsigned StartID = Scope->getStartLabelID()) {
1648 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1649 DWLabel("loc", StartID));
1650 } else {
1651 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1652 DWLabel("func_begin", SubprogramCount));
1653 }
1654 if (unsigned EndID = Scope->getEndLabelID()) {
1655 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1656 DWLabel("loc", EndID));
1657 } else {
1658 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1659 DWLabel("func_end", SubprogramCount));
1660 }
1661
1662 // Add the scope contents.
1663 ConstructScope(Scope, ScopeDie, Unit);
1664 ParentDie->AddChild(ScopeDie);
1665 }
1666}
1667
1668/// ConstructRootScope - Construct the scope for the subprogram.
1669///
1670void DwarfWriter::ConstructRootScope(DebugScope *RootScope) {
1671 // Exit if there is no root scope.
1672 if (!RootScope) return;
1673
1674 // Get the subprogram debug information entry.
1675 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1676 DIE *SPDie = DescToDieMap[SPD];
1677 assert(SPDie && "Missing subprogram descriptor");
1678
1679 // Add the function bounds.
1680 SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1681 DWLabel("func_begin", SubprogramCount));
1682 SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1683 DWLabel("func_end", SubprogramCount));
1684
1685 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1686 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1687
1688 ConstructScope(RootScope, SPDie, Unit);
1689}
1690
Jim Laskey063e7652006-01-17 17:31:53 +00001691/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1692/// tools to recognize the object file contains Dwarf information.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001693///
1694void DwarfWriter::EmitInitial() const {
Jim Laskey063e7652006-01-17 17:31:53 +00001695 // Dwarf sections base addresses.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001696 Asm->SwitchSection(DwarfFrameSection, 0);
1697 EmitLabel("section_frame", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001698 Asm->SwitchSection(DwarfInfoSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001699 EmitLabel("section_info", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001700 EmitLabel("info", 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001701 Asm->SwitchSection(DwarfAbbrevSection, 0);
1702 EmitLabel("section_abbrev", 0);
1703 EmitLabel("abbrev", 0);
1704 Asm->SwitchSection(DwarfARangesSection, 0);
1705 EmitLabel("section_aranges", 0);
1706 Asm->SwitchSection(DwarfMacInfoSection, 0);
1707 EmitLabel("section_macinfo", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001708 Asm->SwitchSection(DwarfLineSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001709 EmitLabel("section_line", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001710 EmitLabel("line", 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001711 Asm->SwitchSection(DwarfLocSection, 0);
1712 EmitLabel("section_loc", 0);
1713 Asm->SwitchSection(DwarfPubNamesSection, 0);
1714 EmitLabel("section_pubnames", 0);
1715 Asm->SwitchSection(DwarfStrSection, 0);
1716 EmitLabel("section_str", 0);
1717 Asm->SwitchSection(DwarfRangesSection, 0);
1718 EmitLabel("section_ranges", 0);
1719
Jim Laskey063e7652006-01-17 17:31:53 +00001720 Asm->SwitchSection(TextSection, 0);
1721 EmitLabel("text_begin", 0);
1722 Asm->SwitchSection(DataSection, 0);
1723 EmitLabel("data_begin", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001724}
1725
Jim Laskey063e7652006-01-17 17:31:53 +00001726/// EmitDIE - Recusively Emits a debug information entry.
1727///
1728void DwarfWriter::EmitDIE(DIE *Die) const {
1729 // Get the abbreviation for this DIE.
1730 unsigned AbbrevID = Die->getAbbrevID();
1731 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey0d086af2006-02-27 12:43:29 +00001732
1733 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001734
1735 // Emit the code (index) for the abbreviation.
1736 EmitULEB128Bytes(AbbrevID);
1737 EOL(std::string("Abbrev [" +
1738 utostr(AbbrevID) +
Jim Laskey0d086af2006-02-27 12:43:29 +00001739 "] 0x" + utohexstr(Die->getOffset()) +
1740 ":0x" + utohexstr(Die->getSize()) + " " +
1741 TagString(Abbrev.getTag())));
Jim Laskey063e7652006-01-17 17:31:53 +00001742
1743 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001744 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001745
1746 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001747 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001748 unsigned Attr = AbbrevData[i].getAttribute();
1749 unsigned Form = AbbrevData[i].getForm();
Jim Laskey063e7652006-01-17 17:31:53 +00001750 assert(Form && "Too many attributes for DIE (check abbreviation)");
1751
1752 switch (Attr) {
1753 case DW_AT_sibling: {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001754 EmitInt32(Die->SiblingOffset());
Jim Laskey063e7652006-01-17 17:31:53 +00001755 break;
1756 }
1757 default: {
1758 // Emit an attribute using the defined form.
1759 Values[i]->EmitValue(*this, Form);
1760 break;
1761 }
1762 }
1763
1764 EOL(AttributeString(Attr));
1765 }
1766
1767 // Emit the DIE children if any.
1768 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1769 const std::vector<DIE *> &Children = Die->getChildren();
1770
Jim Laskey52060a02006-01-24 00:49:18 +00001771 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskey063e7652006-01-17 17:31:53 +00001772 EmitDIE(Children[j]);
1773 }
1774
Jim Laskeyda427fa2006-01-27 20:31:25 +00001775 EmitInt8(0); EOL("End Of Children Mark");
Jim Laskey063e7652006-01-17 17:31:53 +00001776 }
1777}
1778
1779/// SizeAndOffsetDie - Compute the size and offset of a DIE.
1780///
Jim Laskeyb8509c52006-03-23 18:07:55 +00001781unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1782 // Get the children.
1783 const std::vector<DIE *> &Children = Die->getChildren();
1784
1785 // If not last sibling and has children then add sibling offset attribute.
1786 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1787
Jim Laskey0420f2a2006-02-22 19:02:11 +00001788 // Record the abbreviation.
1789 Die->Complete(*this);
1790
Jim Laskey063e7652006-01-17 17:31:53 +00001791 // Get the abbreviation for this DIE.
1792 unsigned AbbrevID = Die->getAbbrevID();
1793 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1794
1795 // Set DIE offset
1796 Die->setOffset(Offset);
1797
1798 // Start the size with the size of abbreviation code.
1799 Offset += SizeULEB128(AbbrevID);
1800
1801 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001802 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001803
1804 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001805 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001806 // Size attribute value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001807 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
Jim Laskey063e7652006-01-17 17:31:53 +00001808 }
1809
1810 // Emit the DIE children if any.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001811 if (!Children.empty()) {
1812 assert(Abbrev.getChildrenFlag() == DW_CHILDREN_yes &&
1813 "Children flag not set");
Jim Laskey063e7652006-01-17 17:31:53 +00001814
Jim Laskey52060a02006-01-24 00:49:18 +00001815 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001816 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
Jim Laskey063e7652006-01-17 17:31:53 +00001817 }
1818
1819 // End of children marker.
1820 Offset += sizeof(int8_t);
1821 }
1822
1823 Die->setSize(Offset - Die->getOffset());
1824 return Offset;
1825}
1826
1827/// SizeAndOffsets - Compute the size and offset of all the DIEs.
1828///
1829void DwarfWriter::SizeAndOffsets() {
Jim Laskey063e7652006-01-17 17:31:53 +00001830
1831 // Process each compile unit.
Jim Laskey52060a02006-01-24 00:49:18 +00001832 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00001833 CompileUnit *Unit = CompileUnits[i];
1834 if (Unit->hasContent()) {
1835 // Compute size of compile unit header
1836 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1837 sizeof(int16_t) + // DWARF version number
1838 sizeof(int32_t) + // Offset Into Abbrev. Section
1839 sizeof(int8_t); // Pointer Size (in bytes)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001840 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
Jim Laskeybd761842006-02-27 17:27:12 +00001841 }
Jim Laskey063e7652006-01-17 17:31:53 +00001842 }
1843}
1844
1845/// EmitDebugInfo - Emit the debug info section.
1846///
1847void DwarfWriter::EmitDebugInfo() const {
1848 // Start debug info section.
1849 Asm->SwitchSection(DwarfInfoSection, 0);
1850
Jim Laskeybd761842006-02-27 17:27:12 +00001851 // Process each compile unit.
1852 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1853 CompileUnit *Unit = CompileUnits[i];
1854
1855 if (Unit->hasContent()) {
1856 DIE *Die = Unit->getDie();
Jim Laskey0d086af2006-02-27 12:43:29 +00001857 // Emit the compile units header.
Jim Laskeybd761842006-02-27 17:27:12 +00001858 EmitLabel("info_begin", Unit->getID());
Jim Laskey0d086af2006-02-27 12:43:29 +00001859 // Emit size of content not including length itself
Jim Laskeybd761842006-02-27 17:27:12 +00001860 unsigned ContentSize = Die->getSize() +
Jim Laskey0d086af2006-02-27 12:43:29 +00001861 sizeof(int16_t) + // DWARF version number
1862 sizeof(int32_t) + // Offset Into Abbrev. Section
1863 sizeof(int8_t); // Pointer Size (in bytes)
1864
1865 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
1866 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1867 EmitReference("abbrev_begin", 0); EOL("Offset Into Abbrev. Section");
1868 EmitInt8(AddressSize); EOL("Address Size (in bytes)");
1869
Jim Laskeybd761842006-02-27 17:27:12 +00001870 EmitDIE(Die);
1871 EmitLabel("info_end", Unit->getID());
Jim Laskey063e7652006-01-17 17:31:53 +00001872 }
Jim Laskey0d086af2006-02-27 12:43:29 +00001873
1874 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001875 }
1876}
1877
1878/// EmitAbbreviations - Emit the abbreviation section.
1879///
1880void DwarfWriter::EmitAbbreviations() const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001881 // Check to see if it is worth the effort.
1882 if (!Abbreviations.empty()) {
1883 // Start the debug abbrev section.
1884 Asm->SwitchSection(DwarfAbbrevSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001885
Jim Laskeyd18e2892006-01-20 20:34:06 +00001886 EmitLabel("abbrev_begin", 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001887
Jim Laskeyd18e2892006-01-20 20:34:06 +00001888 // For each abbrevation.
1889 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001890 AbbrevID <= NAID; ++AbbrevID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001891 // Get abbreviation data
1892 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey063e7652006-01-17 17:31:53 +00001893
Jim Laskeyd18e2892006-01-20 20:34:06 +00001894 // Emit the abbrevations code (base 1 index.)
1895 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
Jim Laskey063e7652006-01-17 17:31:53 +00001896
Jim Laskeyd18e2892006-01-20 20:34:06 +00001897 // Emit the abbreviations data.
1898 Abbrev.Emit(*this);
Jim Laskey0d086af2006-02-27 12:43:29 +00001899
1900 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001901 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00001902
1903 EmitLabel("abbrev_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00001904
1905 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001906 }
1907}
1908
1909/// EmitDebugLines - Emit source line information.
1910///
1911void DwarfWriter::EmitDebugLines() const {
1912 // Minimum line delta, thus ranging from -10..(255-10).
1913 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1914 // Maximum line delta, thus ranging from -10..(255-10).
1915 const int MaxLineDelta = 255 + MinLineDelta;
1916
1917 // Start the dwarf line section.
1918 Asm->SwitchSection(DwarfLineSection, 0);
1919
1920 // Construct the section header.
1921
1922 EmitDifference("line_end", 0, "line_begin", 0);
1923 EOL("Length of Source Line Info");
1924 EmitLabel("line_begin", 0);
1925
Jim Laskeyda427fa2006-01-27 20:31:25 +00001926 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey063e7652006-01-17 17:31:53 +00001927
1928 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
1929 EOL("Prolog Length");
1930 EmitLabel("line_prolog_begin", 0);
1931
Jim Laskeyda427fa2006-01-27 20:31:25 +00001932 EmitInt8(1); EOL("Minimum Instruction Length");
Jim Laskey063e7652006-01-17 17:31:53 +00001933
Jim Laskeyda427fa2006-01-27 20:31:25 +00001934 EmitInt8(1); EOL("Default is_stmt_start flag");
Jim Laskey063e7652006-01-17 17:31:53 +00001935
Jim Laskeyda427fa2006-01-27 20:31:25 +00001936 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00001937
Jim Laskeyda427fa2006-01-27 20:31:25 +00001938 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00001939
Jim Laskeyda427fa2006-01-27 20:31:25 +00001940 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Jim Laskey063e7652006-01-17 17:31:53 +00001941
1942 // Line number standard opcode encodings argument count
Jim Laskeyda427fa2006-01-27 20:31:25 +00001943 EmitInt8(0); EOL("DW_LNS_copy arg count");
1944 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
1945 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
1946 EmitInt8(1); EOL("DW_LNS_set_file arg count");
1947 EmitInt8(1); EOL("DW_LNS_set_column arg count");
1948 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
1949 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
1950 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
1951 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskey063e7652006-01-17 17:31:53 +00001952
1953 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1954 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1955
1956 // Emit directories.
1957 for (unsigned DirectoryID = 1, NDID = Directories.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001958 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskey063e7652006-01-17 17:31:53 +00001959 EmitString(Directories[DirectoryID]); EOL("Directory");
1960 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00001961 EmitInt8(0); EOL("End of directories");
Jim Laskey063e7652006-01-17 17:31:53 +00001962
1963 // Emit files.
1964 for (unsigned SourceID = 1, NSID = SourceFiles.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001965 SourceID <= NSID; ++SourceID) {
Jim Laskey063e7652006-01-17 17:31:53 +00001966 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1967 EmitString(SourceFile.getName()); EOL("Source");
1968 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
1969 EmitULEB128Bytes(0); EOL("Mod date");
1970 EmitULEB128Bytes(0); EOL("File size");
1971 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00001972 EmitInt8(0); EOL("End of files");
Jim Laskey063e7652006-01-17 17:31:53 +00001973
1974 EmitLabel("line_prolog_end", 0);
1975
1976 // Emit line information
1977 const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
1978
1979 // Dwarf assumes we start with first line of first source file.
1980 unsigned Source = 1;
1981 unsigned Line = 1;
1982
1983 // Construct rows of the address, source, line, column matrix.
Jim Laskey52060a02006-01-24 00:49:18 +00001984 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001985 SourceLineInfo *LineInfo = LineInfos[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00001986
1987 if (DwarfVerbose) {
1988 unsigned SourceID = LineInfo->getSourceID();
1989 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1990 unsigned DirectoryID = SourceFile.getDirectoryID();
1991 O << "\t"
1992 << Asm->CommentString << " "
1993 << Directories[DirectoryID]
1994 << SourceFile.getName() << ":"
1995 << LineInfo->getLine() << "\n";
1996 }
Jim Laskey063e7652006-01-17 17:31:53 +00001997
1998 // Define the line address.
Jim Laskeyda427fa2006-01-27 20:31:25 +00001999 EmitInt8(0); EOL("Extended Op");
2000 EmitInt8(4 + 1); EOL("Op size");
2001 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskeyb8509c52006-03-23 18:07:55 +00002002 EmitReference("loc", LineInfo->getLabelID()); EOL("Location label");
Jim Laskey063e7652006-01-17 17:31:53 +00002003
2004 // If change of source, then switch to the new source.
2005 if (Source != LineInfo->getSourceID()) {
2006 Source = LineInfo->getSourceID();
Jim Laskeyda427fa2006-01-27 20:31:25 +00002007 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
Jim Laskeybd761842006-02-27 17:27:12 +00002008 EmitULEB128Bytes(Source); EOL("New Source");
Jim Laskey063e7652006-01-17 17:31:53 +00002009 }
2010
2011 // If change of line.
2012 if (Line != LineInfo->getLine()) {
2013 // Determine offset.
2014 int Offset = LineInfo->getLine() - Line;
2015 int Delta = Offset - MinLineDelta;
2016
2017 // Update line.
2018 Line = LineInfo->getLine();
2019
2020 // If delta is small enough and in range...
2021 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2022 // ... then use fast opcode.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002023 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
Jim Laskey063e7652006-01-17 17:31:53 +00002024 } else {
2025 // ... otherwise use long hand.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002026 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
Jim Laskey063e7652006-01-17 17:31:53 +00002027 EmitSLEB128Bytes(Offset); EOL("Line Offset");
Jim Laskeyda427fa2006-01-27 20:31:25 +00002028 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
Jim Laskey063e7652006-01-17 17:31:53 +00002029 }
2030 } else {
2031 // Copy the previous row (different address or source)
Jim Laskeyda427fa2006-01-27 20:31:25 +00002032 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
Jim Laskey063e7652006-01-17 17:31:53 +00002033 }
2034 }
2035
Jim Laskey0420f2a2006-02-22 19:02:11 +00002036 // Define last address.
2037 EmitInt8(0); EOL("Extended Op");
2038 EmitInt8(4 + 1); EOL("Op size");
2039 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2040 EmitReference("text_end", 0); EOL("Location label");
2041
Jim Laskey063e7652006-01-17 17:31:53 +00002042 // Mark end of matrix.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002043 EmitInt8(0); EOL("DW_LNE_end_sequence");
Jim Laskey063e7652006-01-17 17:31:53 +00002044 EmitULEB128Bytes(1); O << "\n";
Jim Laskeyda427fa2006-01-27 20:31:25 +00002045 EmitInt8(1); O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002046
2047 EmitLabel("line_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002048
2049 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002050}
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002051
2052/// EmitDebugFrame - Emit visible names into a debug frame section.
2053///
2054void DwarfWriter::EmitDebugFrame() {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002055 // Start the dwarf pubnames section.
2056 Asm->SwitchSection(DwarfFrameSection, 0);
2057
2058 EmitDifference("frame_common_end", 0,
2059 "frame_common_begin", 0);
2060 EOL("Length of Common Information Entry");
2061
2062 EmitLabel("frame_common_begin", 0);
2063 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2064 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2065 EmitString(""); EOL("CIE Augmentation");
2066 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
2067 // FIXME - needs to change based on stack direction.
2068 EmitSLEB128Bytes(-sizeof(int32_t)); EOL("CIE Data Alignment Factor");
2069 // FIXME - hard coded for PPC (LR).
2070 EmitInt8(0x41); EOL("CIE RA Column Hardcoded (PPC LR)");
2071 // FIXME - hard coded for PPC 0(SP).
2072 EmitULEB128Bytes(DW_CFA_def_cfa); EOL("DW_CFA_def_cfa");
2073 EmitULEB128Bytes(1); EOL("PPC Register SP");
2074 EmitULEB128Bytes(0); EOL("PPC offset 0 as in 0(SP)");
2075 EmitAlign(2);
2076 EmitLabel("frame_common_end", 0);
2077
2078 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002079}
2080
2081/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2082///
2083void DwarfWriter::EmitDebugPubNames() {
Jim Laskeybd761842006-02-27 17:27:12 +00002084 // Start the dwarf pubnames section.
2085 Asm->SwitchSection(DwarfPubNamesSection, 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002086
Jim Laskeybd761842006-02-27 17:27:12 +00002087 // Process each compile unit.
2088 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2089 CompileUnit *Unit = CompileUnits[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00002090
Jim Laskeybd761842006-02-27 17:27:12 +00002091 if (Unit->hasContent()) {
2092 EmitDifference("pubnames_end", Unit->getID(),
2093 "pubnames_begin", Unit->getID());
2094 EOL("Length of Public Names Info");
2095
2096 EmitLabel("pubnames_begin", Unit->getID());
2097
2098 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2099
2100 EmitReference("info_begin", Unit->getID());
2101 EOL("Offset of Compilation Unit Info");
Jim Laskeyd18e2892006-01-20 20:34:06 +00002102
Jim Laskeybd761842006-02-27 17:27:12 +00002103 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2104 EOL("Compilation Unit Length");
2105
2106 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2107
2108 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2109 GE = Globals.end();
2110 GI != GE; ++GI) {
2111 const std::string &Name = GI->first;
2112 DIE * Entity = GI->second;
2113
2114 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2115 EmitString(Name); EOL("External Name");
2116 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002117
Jim Laskeybd761842006-02-27 17:27:12 +00002118 EmitInt32(0); EOL("End Mark");
2119 EmitLabel("pubnames_end", Unit->getID());
2120
2121 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002122 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002123 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002124}
2125
2126/// EmitDebugStr - Emit visible names into a debug str section.
2127///
2128void DwarfWriter::EmitDebugStr() {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002129 // Check to see if it is worth the effort.
2130 if (!StringPool.empty()) {
2131 // Start the dwarf str section.
2132 Asm->SwitchSection(DwarfStrSection, 0);
2133
Jim Laskeyb8509c52006-03-23 18:07:55 +00002134 // For each of strings in the string pool.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002135 for (unsigned StringID = 1, N = StringPool.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002136 StringID <= N; ++StringID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002137 // Emit a label for reference from debug information entries.
2138 EmitLabel("string", StringID);
2139 // Emit the string itself.
2140 const std::string &String = StringPool[StringID];
2141 EmitString(String); O << "\n";
2142 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002143
2144 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002145 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002146}
2147
2148/// EmitDebugLoc - Emit visible names into a debug loc section.
2149///
2150void DwarfWriter::EmitDebugLoc() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002151 // Start the dwarf loc section.
2152 Asm->SwitchSection(DwarfLocSection, 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002153
2154 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002155}
2156
2157/// EmitDebugARanges - Emit visible names into a debug aranges section.
2158///
2159void DwarfWriter::EmitDebugARanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002160 // Start the dwarf aranges section.
2161 Asm->SwitchSection(DwarfARangesSection, 0);
2162
2163 // FIXME - Mock up
Jim Laskeybd761842006-02-27 17:27:12 +00002164#if 0
2165 // Process each compile unit.
2166 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2167 CompileUnit *Unit = CompileUnits[i];
2168
2169 if (Unit->hasContent()) {
2170 // Don't include size of length
2171 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2172
2173 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2174
2175 EmitReference("info_begin", Unit->getID());
2176 EOL("Offset of Compilation Unit Info");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002177
Jim Laskeybd761842006-02-27 17:27:12 +00002178 EmitInt8(AddressSize); EOL("Size of Address");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002179
Jim Laskeybd761842006-02-27 17:27:12 +00002180 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002181
Jim Laskeybd761842006-02-27 17:27:12 +00002182 EmitInt16(0); EOL("Pad (1)");
2183 EmitInt16(0); EOL("Pad (2)");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002184
Jim Laskeybd761842006-02-27 17:27:12 +00002185 // Range 1
2186 EmitReference("text_begin", 0); EOL("Address");
2187 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002188
Jim Laskeybd761842006-02-27 17:27:12 +00002189 EmitInt32(0); EOL("EOM (1)");
2190 EmitInt32(0); EOL("EOM (2)");
2191
2192 O << "\n";
2193 }
2194 }
2195#endif
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002196}
2197
2198/// EmitDebugRanges - Emit visible names into a debug ranges section.
2199///
2200void DwarfWriter::EmitDebugRanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002201 // Start the dwarf ranges section.
2202 Asm->SwitchSection(DwarfRangesSection, 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002203
2204 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002205}
2206
2207/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2208///
2209void DwarfWriter::EmitDebugMacInfo() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002210 // Start the dwarf macinfo section.
2211 Asm->SwitchSection(DwarfMacInfoSection, 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002212
2213 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002214}
Jim Laskey063e7652006-01-17 17:31:53 +00002215
Jim Laskey52060a02006-01-24 00:49:18 +00002216/// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2217/// header file.
2218void DwarfWriter::ConstructCompileUnitDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002219 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
Jim Laskey6e87c0e2006-01-26 21:22:49 +00002220
2221 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00002222 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
Jim Laskey52060a02006-01-24 00:49:18 +00002223 CompileUnits.push_back(Unit);
2224 }
2225}
2226
2227/// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2228/// variables.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002229void DwarfWriter::ConstructGlobalDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002230 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002231 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00002232
2233 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002234 GlobalVariableDesc *GVD = GlobalVariables[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002235 NewGlobalVariable(GVD);
Jim Laskey52060a02006-01-24 00:49:18 +00002236 }
2237}
2238
Jim Laskey0420f2a2006-02-22 19:02:11 +00002239/// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2240/// subprograms.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002241void DwarfWriter::ConstructSubprogramDIEs() {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002242 std::vector<SubprogramDesc *> Subprograms =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002243 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002244
2245 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2246 SubprogramDesc *SPD = Subprograms[i];
2247 NewSubprogram(SPD);
2248 }
2249}
Jim Laskey52060a02006-01-24 00:49:18 +00002250
Jim Laskey063e7652006-01-17 17:31:53 +00002251/// ShouldEmitDwarf - Determine if Dwarf declarations should be made.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002252///
2253bool DwarfWriter::ShouldEmitDwarf() {
2254 // Check if debug info is present.
2255 if (!DebugInfo || !DebugInfo->hasInfo()) return false;
2256
2257 // Make sure initial declarations are made.
2258 if (!didInitial) {
2259 EmitInitial();
Jim Laskeyb8509c52006-03-23 18:07:55 +00002260
2261 // Create all the compile unit DIEs.
2262 ConstructCompileUnitDIEs();
2263
2264 // Create DIEs for each of the externally visible global variables.
2265 ConstructGlobalDIEs();
2266
2267 // Create DIEs for each of the externally visible subprograms.
2268 ConstructSubprogramDIEs();
2269
Jim Laskeyb2efb852006-01-04 22:28:25 +00002270 didInitial = true;
2271 }
2272
2273 // Okay to emit.
2274 return true;
2275}
2276
Jim Laskey063e7652006-01-17 17:31:53 +00002277//===----------------------------------------------------------------------===//
Jim Laskeyd18e2892006-01-20 20:34:06 +00002278// Main entry points.
Jim Laskey063e7652006-01-17 17:31:53 +00002279//
Jim Laskey52060a02006-01-24 00:49:18 +00002280
2281DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
2282: O(OS)
2283, Asm(A)
Jim Laskeyb8509c52006-03-23 18:07:55 +00002284, M(NULL)
2285, MF(NULL)
Jim Laskey52060a02006-01-24 00:49:18 +00002286, DebugInfo(NULL)
2287, didInitial(false)
Jim Laskeyb8509c52006-03-23 18:07:55 +00002288, SubprogramCount(0)
Jim Laskey52060a02006-01-24 00:49:18 +00002289, CompileUnits()
2290, Abbreviations()
Jim Laskey52060a02006-01-24 00:49:18 +00002291, StringPool()
Jim Laskeybd761842006-02-27 17:27:12 +00002292, DescToUnitMap()
Jim Laskey0420f2a2006-02-22 19:02:11 +00002293, DescToDieMap()
2294, TypeToDieMap()
Jim Laskey52060a02006-01-24 00:49:18 +00002295, AddressSize(sizeof(int32_t))
2296, hasLEB128(false)
2297, hasDotLoc(false)
2298, hasDotFile(false)
2299, needsSet(false)
2300, DwarfAbbrevSection(".debug_abbrev")
2301, DwarfInfoSection(".debug_info")
2302, DwarfLineSection(".debug_line")
2303, DwarfFrameSection(".debug_frame")
2304, DwarfPubNamesSection(".debug_pubnames")
2305, DwarfPubTypesSection(".debug_pubtypes")
2306, DwarfStrSection(".debug_str")
2307, DwarfLocSection(".debug_loc")
2308, DwarfARangesSection(".debug_aranges")
2309, DwarfRangesSection(".debug_ranges")
2310, DwarfMacInfoSection(".debug_macinfo")
2311, TextSection(".text")
2312, DataSection(".data")
2313{}
2314DwarfWriter::~DwarfWriter() {
2315 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2316 delete CompileUnits[i];
Jim Laskey063e7652006-01-17 17:31:53 +00002317 }
Jim Laskey52060a02006-01-24 00:49:18 +00002318}
Jim Laskey063e7652006-01-17 17:31:53 +00002319
2320/// BeginModule - Emit all Dwarf sections that should come prior to the content.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002321///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002322void DwarfWriter::BeginModule(Module *M) {
2323 this->M = M;
2324
Jim Laskeyb2efb852006-01-04 22:28:25 +00002325 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002326 EOL("Dwarf Begin Module");
Jim Laskeyb2efb852006-01-04 22:28:25 +00002327}
2328
Jim Laskey063e7652006-01-17 17:31:53 +00002329/// EndModule - Emit all Dwarf sections that should come after the content.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002330///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002331void DwarfWriter::EndModule() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002332 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002333 EOL("Dwarf End Module");
2334
2335 // Standard sections final addresses.
Jim Laskeye719a7c2006-01-18 16:54:26 +00002336 Asm->SwitchSection(TextSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002337 EmitLabel("text_end", 0);
Jim Laskeye719a7c2006-01-18 16:54:26 +00002338 Asm->SwitchSection(DataSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002339 EmitLabel("data_end", 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002340
Jim Laskey063e7652006-01-17 17:31:53 +00002341 // Compute DIE offsets and sizes.
2342 SizeAndOffsets();
2343
2344 // Emit all the DIEs into a debug info section
2345 EmitDebugInfo();
2346
2347 // Corresponding abbreviations into a abbrev section.
2348 EmitAbbreviations();
2349
2350 // Emit source line correspondence into a debug line section.
2351 EmitDebugLines();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002352
2353 // Emit info into a debug frame section.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002354 EmitDebugFrame();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002355
2356 // Emit info into a debug pubnames section.
2357 EmitDebugPubNames();
2358
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002359 // Emit info into a debug str section.
2360 EmitDebugStr();
2361
2362 // Emit info into a debug loc section.
2363 EmitDebugLoc();
2364
2365 // Emit info into a debug aranges section.
2366 EmitDebugARanges();
2367
2368 // Emit info into a debug ranges section.
2369 EmitDebugRanges();
2370
2371 // Emit info into a debug macinfo section.
2372 EmitDebugMacInfo();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002373}
2374
Jim Laskeye719a7c2006-01-18 16:54:26 +00002375/// BeginFunction - Gather pre-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002376///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002377void DwarfWriter::BeginFunction(MachineFunction *MF) {
2378 this->MF = MF;
2379
Jim Laskeyb2efb852006-01-04 22:28:25 +00002380 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002381 EOL("Dwarf Begin Function");
Jim Laskeyb8509c52006-03-23 18:07:55 +00002382
2383 // Define begin label for subprogram.
2384 Asm->SwitchSection(TextSection, 0);
2385 EmitLabel("func_begin", ++SubprogramCount);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002386}
2387
Jim Laskeyb8509c52006-03-23 18:07:55 +00002388
Jim Laskeye719a7c2006-01-18 16:54:26 +00002389/// EndFunction - Gather and emit post-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002390///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002391void DwarfWriter::EndFunction() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002392 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002393 EOL("Dwarf End Function");
Jim Laskeyb8509c52006-03-23 18:07:55 +00002394
2395 // Define end label for subprogram.
2396 Asm->SwitchSection(TextSection, 0);
2397 EmitLabel("func_end", SubprogramCount);
2398
2399 // Construct scopes for subprogram.
2400 ConstructRootScope(DebugInfo->getRootScope());
2401 DebugInfo->ClearScopes();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002402}