blob: 43fbb3b902fe949a7f34e2ac249575fd539282e2 [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//
Jim Laskey0d086af2006-02-27 12:43:29 +000042class DIE;
Jim Laskey063e7652006-01-17 17:31:53 +000043
Jim Laskey0d086af2006-02-27 12:43:29 +000044//===----------------------------------------------------------------------===//
Jim Laskeyf01e5472006-03-03 15:06:57 +000045// CompileUnit - This dwarf writer support class manages information associate
46// with a source file.
Jim Laskeybd761842006-02-27 17:27:12 +000047class CompileUnit {
48private:
49 CompileUnitDesc *Desc; // Compile unit debug descriptor.
50 unsigned ID; // File ID for source.
Jim Laskeyb8509c52006-03-23 18:07:55 +000051 DIE *Die; // Compile unit debug information entry.
Jim Laskeybd761842006-02-27 17:27:12 +000052 std::map<std::string, DIE *> Globals; // A map of globally visible named
53 // entities for this unit.
Jim Laskey90c79d72006-03-23 23:02:34 +000054 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
55 // Tracks the mapping of unit level
56 // debug informaton descriptors to debug
57 // information entries.
Jim Laskeybd761842006-02-27 17:27:12 +000058
59public:
60 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
61 : Desc(CUD)
62 , ID(I)
63 , Die(D)
64 , Globals()
Jim Laskey90c79d72006-03-23 23:02:34 +000065 , DescToDieMap()
Jim Laskeybd761842006-02-27 17:27:12 +000066 {}
67
68 ~CompileUnit();
69
70 // Accessors.
71 CompileUnitDesc *getDesc() const { return Desc; }
72 unsigned getID() const { return ID; }
73 DIE* getDie() const { return Die; }
74 std::map<std::string, DIE *> &getGlobals() { return Globals; }
75
76 /// hasContent - Return true if this compile unit has something to write out.
77 ///
78 bool hasContent() const;
79
80 /// AddGlobal - Add a new global entity to the compile unit.
81 ///
82 void AddGlobal(const std::string &Name, DIE *Die);
83
Jim Laskey90c79d72006-03-23 23:02:34 +000084 /// getDieMapSlotFor - Returns the debug information entry map slot for the
85 /// specified debug descriptor.
86 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
87 return DescToDieMap[DD];
88 }
Jim Laskeybd761842006-02-27 17:27:12 +000089};
90
91//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000092// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
93// Dwarf abbreviation.
94class DIEAbbrevData {
95private:
96 unsigned Attribute; // Dwarf attribute code.
97 unsigned Form; // Dwarf form code.
98
99public:
100 DIEAbbrevData(unsigned A, unsigned F)
101 : Attribute(A)
102 , Form(F)
103 {}
104
Jim Laskeybd761842006-02-27 17:27:12 +0000105 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000106 unsigned getAttribute() const { return Attribute; }
107 unsigned getForm() const { return Form; }
108
109 /// operator== - Used by DIEAbbrev to locate entry.
110 ///
111 bool operator==(const DIEAbbrevData &DAD) const {
112 return Attribute == DAD.Attribute && Form == DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000113 }
Jim Laskey063e7652006-01-17 17:31:53 +0000114
Jim Laskey0d086af2006-02-27 12:43:29 +0000115 /// operator!= - Used by DIEAbbrev to locate entry.
116 ///
117 bool operator!=(const DIEAbbrevData &DAD) const {
118 return Attribute != DAD.Attribute || Form != DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000119 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000120
121 /// operator< - Used by DIEAbbrev to locate entry.
122 ///
123 bool operator<(const DIEAbbrevData &DAD) const {
124 return Attribute < DAD.Attribute ||
125 (Attribute == DAD.Attribute && Form < DAD.Form);
126 }
127};
Jim Laskey063e7652006-01-17 17:31:53 +0000128
Jim Laskey0d086af2006-02-27 12:43:29 +0000129//===----------------------------------------------------------------------===//
130// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
131// information object.
132class DIEAbbrev {
133private:
134 unsigned Tag; // Dwarf tag code.
135 unsigned ChildrenFlag; // Dwarf children flag.
136 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
Jim Laskey063e7652006-01-17 17:31:53 +0000137
Jim Laskey0d086af2006-02-27 12:43:29 +0000138public:
Jim Laskey063e7652006-01-17 17:31:53 +0000139
Jim Laskey0d086af2006-02-27 12:43:29 +0000140 DIEAbbrev(unsigned T, unsigned C)
141 : Tag(T)
142 , ChildrenFlag(C)
143 , Data()
144 {}
145 ~DIEAbbrev() {}
146
Jim Laskeybd761842006-02-27 17:27:12 +0000147 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000148 unsigned getTag() const { return Tag; }
149 unsigned getChildrenFlag() const { return ChildrenFlag; }
150 const std::vector<DIEAbbrevData> &getData() const { return Data; }
151 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskey063e7652006-01-17 17:31:53 +0000152
Jim Laskey0d086af2006-02-27 12:43:29 +0000153 /// operator== - Used by UniqueVector to locate entry.
154 ///
155 bool operator==(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000156
Jim Laskey0d086af2006-02-27 12:43:29 +0000157 /// operator< - Used by UniqueVector to locate entry.
158 ///
159 bool operator<(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000160
Jim Laskey0d086af2006-02-27 12:43:29 +0000161 /// AddAttribute - Adds another set of attribute information to the
162 /// abbreviation.
163 void AddAttribute(unsigned Attribute, unsigned Form) {
164 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000165 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000166
Jim Laskeyb8509c52006-03-23 18:07:55 +0000167 /// AddFirstAttribute - Adds a set of attribute information to the front
168 /// of the abbreviation.
169 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
170 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
171 }
172
Jim Laskey0d086af2006-02-27 12:43:29 +0000173 /// Emit - Print the abbreviation using the specified Dwarf writer.
174 ///
175 void Emit(const DwarfWriter &DW) const;
176
177#ifndef NDEBUG
178 void print(std::ostream &O);
179 void dump();
180#endif
181};
Jim Laskey063e7652006-01-17 17:31:53 +0000182
Jim Laskey0d086af2006-02-27 12:43:29 +0000183//===----------------------------------------------------------------------===//
184// DIEValue - A debug information entry value.
185//
186class DIEValue {
187public:
188 enum {
189 isInteger,
190 isString,
191 isLabel,
192 isAsIsLabel,
193 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000194 isEntry,
195 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000196 };
197
198 unsigned Type; // Type of the value
199
200 DIEValue(unsigned T) : Type(T) {}
201 virtual ~DIEValue() {}
202
203 // Implement isa/cast/dyncast.
204 static bool classof(const DIEValue *) { return true; }
205
206 /// EmitValue - Emit value via the Dwarf writer.
207 ///
208 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
209
210 /// SizeOf - Return the size of a value in bytes.
211 ///
212 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
213};
Jim Laskey063e7652006-01-17 17:31:53 +0000214
Jim Laskey0d086af2006-02-27 12:43:29 +0000215//===----------------------------------------------------------------------===//
216// DWInteger - An integer value DIE.
217//
218class DIEInteger : public DIEValue {
219private:
220 uint64_t Integer;
221
222public:
223 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000224
Jim Laskey0d086af2006-02-27 12:43:29 +0000225 // Implement isa/cast/dyncast.
226 static bool classof(const DIEInteger *) { return true; }
227 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
228
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000229 /// BestForm - Choose the best form for integer.
230 ///
231 unsigned BestForm(bool IsSigned);
232
Jim Laskey0d086af2006-02-27 12:43:29 +0000233 /// EmitValue - Emit integer of appropriate size.
234 ///
235 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
236
237 /// SizeOf - Determine size of integer value in bytes.
238 ///
239 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
240};
Jim Laskey063e7652006-01-17 17:31:53 +0000241
Jim Laskey0d086af2006-02-27 12:43:29 +0000242//===----------------------------------------------------------------------===//
243// DIEString - A string value DIE.
244//
245struct DIEString : public DIEValue {
246 const std::string String;
247
248 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000249
Jim Laskey0d086af2006-02-27 12:43:29 +0000250 // Implement isa/cast/dyncast.
251 static bool classof(const DIEString *) { return true; }
252 static bool classof(const DIEValue *S) { return S->Type == isString; }
253
254 /// EmitValue - Emit string value.
255 ///
256 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
257
258 /// SizeOf - Determine size of string value in bytes.
259 ///
260 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
261};
Jim Laskey063e7652006-01-17 17:31:53 +0000262
Jim Laskey0d086af2006-02-27 12:43:29 +0000263//===----------------------------------------------------------------------===//
264// DIEDwarfLabel - A Dwarf internal label expression DIE.
265//
266struct DIEDwarfLabel : public DIEValue {
267 const DWLabel Label;
268
269 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000270
Jim Laskey0d086af2006-02-27 12:43:29 +0000271 // Implement isa/cast/dyncast.
272 static bool classof(const DIEDwarfLabel *) { return true; }
273 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
274
275 /// EmitValue - Emit label value.
276 ///
277 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
278
279 /// SizeOf - Determine size of label value in bytes.
280 ///
281 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
282};
Jim Laskey063e7652006-01-17 17:31:53 +0000283
Jim Laskey063e7652006-01-17 17:31:53 +0000284
Jim Laskey0d086af2006-02-27 12:43:29 +0000285//===----------------------------------------------------------------------===//
286// DIEObjectLabel - A label to an object in code or data.
287//
288struct DIEObjectLabel : public DIEValue {
289 const std::string Label;
290
291 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000292
Jim Laskey0d086af2006-02-27 12:43:29 +0000293 // Implement isa/cast/dyncast.
294 static bool classof(const DIEObjectLabel *) { return true; }
295 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
296
297 /// EmitValue - Emit label value.
298 ///
299 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
300
301 /// SizeOf - Determine size of label value in bytes.
302 ///
303 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
304};
Jim Laskey063e7652006-01-17 17:31:53 +0000305
Jim Laskey0d086af2006-02-27 12:43:29 +0000306//===----------------------------------------------------------------------===//
307// DIEDelta - A simple label difference DIE.
308//
309struct DIEDelta : public DIEValue {
310 const DWLabel LabelHi;
311 const DWLabel LabelLo;
312
313 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
314 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000315
Jim Laskey0d086af2006-02-27 12:43:29 +0000316 // Implement isa/cast/dyncast.
317 static bool classof(const DIEDelta *) { return true; }
318 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
319
320 /// EmitValue - Emit delta value.
321 ///
322 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
323
324 /// SizeOf - Determine size of delta value in bytes.
325 ///
326 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
327};
Jim Laskey063e7652006-01-17 17:31:53 +0000328
Jim Laskey0d086af2006-02-27 12:43:29 +0000329//===----------------------------------------------------------------------===//
330// DIEntry - A pointer to a debug information entry.
331//
332struct DIEntry : public DIEValue {
333 DIE *Entry;
334
335 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
336
337 // Implement isa/cast/dyncast.
338 static bool classof(const DIEntry *) { return true; }
339 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
340
Jim Laskeyb8509c52006-03-23 18:07:55 +0000341 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000342 ///
343 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
344
Jim Laskeyb8509c52006-03-23 18:07:55 +0000345 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000346 ///
347 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
348};
349
350//===----------------------------------------------------------------------===//
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000351// DIEBlock - A block of values. Primarily used for location expressions.
352//
353struct DIEBlock : public DIEValue {
354 unsigned Size; // Size in bytes excluding size header.
355 std::vector<unsigned> Forms; // Data forms.
356 std::vector<DIEValue *> Values; // Block values.
357
358 DIEBlock()
359 : DIEValue(isBlock)
360 , Size(0)
361 , Forms()
362 , Values()
363 {}
364 ~DIEBlock();
365
366 // Implement isa/cast/dyncast.
367 static bool classof(const DIEBlock *) { return true; }
368 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
369
370 /// ComputeSize - calculate the size of the block.
371 ///
372 unsigned ComputeSize(DwarfWriter &DW);
373
374 /// BestForm - Choose the best form for data.
375 ///
376 unsigned BestForm();
377
378 /// EmitValue - Emit block data.
379 ///
380 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
381
382 /// SizeOf - Determine size of block data in bytes.
383 ///
384 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
385
386 /// AddUInt - Add an unsigned integer value.
387 ///
388 void AddUInt(unsigned Form, uint64_t Integer);
389
390 /// AddSInt - Add an signed integer value.
391 ///
392 void AddSInt(unsigned Form, int64_t Integer);
393
394 /// AddString - Add a std::string value.
395 ///
396 void AddString(unsigned Form, const std::string &String);
397
398 /// AddLabel - Add a Dwarf label value.
399 ///
400 void AddLabel(unsigned Form, const DWLabel &Label);
401
402 /// AddObjectLabel - Add a non-Dwarf label value.
403 ///
404 void AddObjectLabel(unsigned Form, const std::string &Label);
405
406 /// AddDelta - Add a label delta value.
407 ///
408 void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
409
410 /// AddDIEntry - Add a DIE value.
411 ///
412 void AddDIEntry(unsigned Form, DIE *Entry);
413
414};
415
416//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +0000417// DIE - A structured debug information entry. Has an abbreviation which
418// describes it's organization.
419class DIE {
420private:
421 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
422 unsigned AbbrevID; // Decribing abbreviation ID.
423 unsigned Offset; // Offset in debug info section.
424 unsigned Size; // Size of instance + children.
425 std::vector<DIE *> Children; // Children DIEs.
426 std::vector<DIEValue *> Values; // Attributes values.
427
428public:
429 DIE(unsigned Tag);
430 ~DIE();
431
Jim Laskeybd761842006-02-27 17:27:12 +0000432 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000433 unsigned getAbbrevID() const { return AbbrevID; }
434 unsigned getOffset() const { return Offset; }
435 unsigned getSize() const { return Size; }
436 const std::vector<DIE *> &getChildren() const { return Children; }
437 const std::vector<DIEValue *> &getValues() const { return Values; }
438 void setOffset(unsigned O) { Offset = O; }
439 void setSize(unsigned S) { Size = S; }
440
441 /// SiblingOffset - Return the offset of the debug information entry's
442 /// sibling.
443 unsigned SiblingOffset() const { return Offset + Size; }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000444
445 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
446 ///
447 void AddSiblingOffset();
Jim Laskey0d086af2006-02-27 12:43:29 +0000448
449 /// AddUInt - Add an unsigned integer attribute data and value.
450 ///
451 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
452
453 /// AddSInt - Add an signed integer attribute data and value.
454 ///
455 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
456
457 /// AddString - Add a std::string attribute data and value.
458 ///
459 void AddString(unsigned Attribute, unsigned Form,
460 const std::string &String);
461
462 /// AddLabel - Add a Dwarf label attribute data and value.
463 ///
464 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
465
466 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
467 ///
468 void AddObjectLabel(unsigned Attribute, unsigned Form,
469 const std::string &Label);
470
471 /// AddDelta - Add a label delta attribute data and value.
472 ///
473 void AddDelta(unsigned Attribute, unsigned Form,
474 const DWLabel &Hi, const DWLabel &Lo);
475
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000476 /// AddDIEntry - Add a DIE attribute data and value.
Jim Laskey0d086af2006-02-27 12:43:29 +0000477 ///
478 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
479
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000480 /// AddBlock - Add block data.
481 ///
482 void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
483
Jim Laskey0d086af2006-02-27 12:43:29 +0000484 /// Complete - Indicate that all attributes have been added and
485 /// ready to get an abbreviation ID.
486 ///
487 void Complete(DwarfWriter &DW);
488
489 /// AddChild - Add a child to the DIE.
490 void AddChild(DIE *Child);
491};
492
493} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +0000494
495//===----------------------------------------------------------------------===//
496
Jim Laskeybd761842006-02-27 17:27:12 +0000497CompileUnit::~CompileUnit() {
498 delete Die;
499}
500
501/// hasContent - Return true if this compile unit has something to write out.
502///
503bool CompileUnit::hasContent() const {
504 return !Die->getChildren().empty();
505}
506
507/// AddGlobal - Add a new global entity to the compile unit.
508///
509void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
510 Globals[Name] = Die;
511}
512
513//===----------------------------------------------------------------------===//
514
Jim Laskeyd18e2892006-01-20 20:34:06 +0000515/// operator== - Used by UniqueVector to locate entry.
516///
517bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
518 if (Tag != DA.Tag) return false;
519 if (ChildrenFlag != DA.ChildrenFlag) return false;
520 if (Data.size() != DA.Data.size()) return false;
521
Jim Laskey52060a02006-01-24 00:49:18 +0000522 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000523 if (Data[i] != DA.Data[i]) return false;
Jim Laskeyd18e2892006-01-20 20:34:06 +0000524 }
525
526 return true;
527}
528
529/// operator< - Used by UniqueVector to locate entry.
530///
531bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
532 if (Tag != DA.Tag) return Tag < DA.Tag;
533 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
534 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
535
Jim Laskey52060a02006-01-24 00:49:18 +0000536 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000537 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +0000538 }
539
540 return false;
541}
542
543/// Emit - Print the abbreviation using the specified Dwarf writer.
544///
545void DIEAbbrev::Emit(const DwarfWriter &DW) const {
546 // Emit its Dwarf tag type.
547 DW.EmitULEB128Bytes(Tag);
548 DW.EOL(TagString(Tag));
549
550 // Emit whether it has children DIEs.
551 DW.EmitULEB128Bytes(ChildrenFlag);
552 DW.EOL(ChildrenString(ChildrenFlag));
553
554 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +0000555 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000556 const DIEAbbrevData &AttrData = Data[i];
557
558 // Emit attribute type.
559 DW.EmitULEB128Bytes(AttrData.getAttribute());
560 DW.EOL(AttributeString(AttrData.getAttribute()));
561
562 // Emit form type.
563 DW.EmitULEB128Bytes(AttrData.getForm());
564 DW.EOL(FormEncodingString(AttrData.getForm()));
565 }
566
567 // Mark end of abbreviation.
568 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
569 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
570}
571
572#ifndef NDEBUG
573 void DIEAbbrev::print(std::ostream &O) {
574 O << "Abbreviation @"
Jeff Cohen05ebc8d2006-01-25 17:18:50 +0000575 << std::hex << (intptr_t)this << std::dec
Jim Laskeyd18e2892006-01-20 20:34:06 +0000576 << " "
577 << TagString(Tag)
578 << " "
579 << ChildrenString(ChildrenFlag)
580 << "\n";
581
Jim Laskey52060a02006-01-24 00:49:18 +0000582 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000583 O << " "
584 << AttributeString(Data[i].getAttribute())
585 << " "
586 << FormEncodingString(Data[i].getForm())
587 << "\n";
588 }
589 }
590 void DIEAbbrev::dump() { print(std::cerr); }
591#endif
592
593//===----------------------------------------------------------------------===//
594
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000595/// BestForm - Choose the best form for integer.
596///
597unsigned DIEInteger::BestForm(bool IsSigned) {
598 if (IsSigned) {
599 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
600 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
601 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
602 } else {
603 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
604 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
605 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
606 }
607 return DW_FORM_data8;
608}
609
Jim Laskey063e7652006-01-17 17:31:53 +0000610/// EmitValue - Emit integer of appropriate size.
611///
612void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
613 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +0000614 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000615 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000616 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000617 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000618 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000619 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000620 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000621 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000622 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +0000623 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
624 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +0000625 default: assert(0 && "DIE Value form not supported yet"); break;
626 }
627}
628
629/// SizeOf - Determine size of integer value in bytes.
630///
631unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
632 switch (Form) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000633 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000634 case DW_FORM_ref1: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000635 case DW_FORM_data1: return sizeof(int8_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000636 case DW_FORM_ref2: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000637 case DW_FORM_data2: return sizeof(int16_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000638 case DW_FORM_ref4: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000639 case DW_FORM_data4: return sizeof(int32_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000640 case DW_FORM_ref8: // Fall thru
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000641 case DW_FORM_data8: return sizeof(int64_t);
Jim Laskey40020172006-01-20 21:02:36 +0000642 case DW_FORM_udata: return DW.SizeULEB128(Integer);
643 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +0000644 default: assert(0 && "DIE Value form not supported yet"); break;
645 }
646 return 0;
647}
648
649//===----------------------------------------------------------------------===//
650
651/// EmitValue - Emit string value.
652///
653void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000654 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +0000655}
656
657/// SizeOf - Determine size of string value in bytes.
658///
659unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey40020172006-01-20 21:02:36 +0000660 return String.size() + sizeof(char); // sizeof('\0');
Jim Laskey063e7652006-01-17 17:31:53 +0000661}
662
663//===----------------------------------------------------------------------===//
664
665/// EmitValue - Emit label value.
666///
Jim Laskey52060a02006-01-24 00:49:18 +0000667void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000668 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +0000669}
670
671/// SizeOf - Determine size of label value in bytes.
672///
Jim Laskey52060a02006-01-24 00:49:18 +0000673unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +0000674 return DW.getAddressSize();
675}
676
677//===----------------------------------------------------------------------===//
678
Jim Laskeyd18e2892006-01-20 20:34:06 +0000679/// EmitValue - Emit label value.
680///
Jim Laskey52060a02006-01-24 00:49:18 +0000681void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000682 DW.EmitReference(Label);
683}
684
685/// SizeOf - Determine size of label value in bytes.
686///
Jim Laskey52060a02006-01-24 00:49:18 +0000687unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000688 return DW.getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +0000689}
690
691//===----------------------------------------------------------------------===//
692
Jim Laskey063e7652006-01-17 17:31:53 +0000693/// EmitValue - Emit delta value.
694///
695void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000696 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +0000697}
698
699/// SizeOf - Determine size of delta value in bytes.
700///
701unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
702 return DW.getAddressSize();
703}
704
705//===----------------------------------------------------------------------===//
Jim Laskeyb8509c52006-03-23 18:07:55 +0000706/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000707///
708void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +0000709 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +0000710}
711
Jim Laskeyb8509c52006-03-23 18:07:55 +0000712/// SizeOf - Determine size of debug information entry value in bytes.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000713///
714unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
715 return sizeof(int32_t);
716}
717
718//===----------------------------------------------------------------------===//
719
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000720DIEBlock::~DIEBlock() {
721 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
722 delete Values[i];
723 }
724}
725
726/// ComputeSize - calculate the size of the block.
727///
728unsigned DIEBlock::ComputeSize(DwarfWriter &DW) {
729 Size = 0;
730 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
731 Size += Values[i]->SizeOf(DW, Forms[i]);
732 }
733 return Size;
734}
735
736/// BestForm - Choose the best form for data.
737///
738unsigned DIEBlock::BestForm() {
739 if ((unsigned char)Size == Size) return DW_FORM_block1;
740 if ((unsigned short)Size == Size) return DW_FORM_block2;
741 if ((unsigned int)Size == Size) return DW_FORM_block4;
742 return DW_FORM_block;
743}
744
745/// EmitValue - Emit block data.
746///
747void DIEBlock::EmitValue(const DwarfWriter &DW, unsigned Form) const {
748 switch (Form) {
749 case DW_FORM_block1: DW.EmitInt8(Size); break;
750 case DW_FORM_block2: DW.EmitInt16(Size); break;
751 case DW_FORM_block4: DW.EmitInt32(Size); break;
752 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
753 default: assert(0 && "Improper form for block"); break;
754 }
755 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
756 DW.EOL("");
757 Values[i]->EmitValue(DW, Forms[i]);
758 }
759}
760
761/// SizeOf - Determine size of block data in bytes.
762///
763unsigned DIEBlock::SizeOf(const DwarfWriter &DW, unsigned Form) const {
764 switch (Form) {
765 case DW_FORM_block1: return Size + sizeof(int8_t);
766 case DW_FORM_block2: return Size + sizeof(int16_t);
767 case DW_FORM_block4: return Size + sizeof(int32_t);
768 case DW_FORM_block: return Size + DW.SizeULEB128(Size);
769 default: assert(0 && "Improper form for block"); break;
770 }
771 return 0;
772}
773
774/// AddUInt - Add an unsigned integer value.
775///
776void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
777 DIEInteger *DI = new DIEInteger(Integer);
778 Values.push_back(DI);
779 if (Form == 0) Form = DI->BestForm(false);
780 Forms.push_back(Form);
781}
782
783/// AddSInt - Add an signed integer value.
784///
785void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
786 DIEInteger *DI = new DIEInteger(Integer);
787 Values.push_back(DI);
788 if (Form == 0) Form = DI->BestForm(true);
789 Forms.push_back(Form);
790}
791
792/// AddString - Add a std::string value.
793///
794void DIEBlock::AddString(unsigned Form, const std::string &String) {
795 Values.push_back(new DIEString(String));
796 Forms.push_back(Form);
797}
798
799/// AddLabel - Add a Dwarf label value.
800///
801void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
802 Values.push_back(new DIEDwarfLabel(Label));
803 Forms.push_back(Form);
804}
805
806/// AddObjectLabel - Add a non-Dwarf label value.
807///
808void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
809 Values.push_back(new DIEObjectLabel(Label));
810 Forms.push_back(Form);
811}
812
813/// AddDelta - Add a label delta value.
814///
815void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
816 Values.push_back(new DIEDelta(Hi, Lo));
817 Forms.push_back(Form);
818}
819
820/// AddDIEntry - Add a DIE value.
821///
822void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
823 Values.push_back(new DIEntry(Entry));
824 Forms.push_back(Form);
825}
826
827//===----------------------------------------------------------------------===//
828
Jim Laskey0420f2a2006-02-22 19:02:11 +0000829DIE::DIE(unsigned Tag)
830: Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
Jim Laskeyd18e2892006-01-20 20:34:06 +0000831, AbbrevID(0)
832, Offset(0)
833, Size(0)
Jim Laskeyd18e2892006-01-20 20:34:06 +0000834, Children()
835, Values()
836{}
837
838DIE::~DIE() {
839 if (Abbrev) delete Abbrev;
840
Jim Laskey52060a02006-01-24 00:49:18 +0000841 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000842 delete Children[i];
843 }
844
Jim Laskey52060a02006-01-24 00:49:18 +0000845 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000846 delete Values[j];
847 }
Jim Laskeyd18e2892006-01-20 20:34:06 +0000848}
849
Jim Laskeyb8509c52006-03-23 18:07:55 +0000850/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
851///
852void DIE::AddSiblingOffset() {
853 DIEInteger *DI = new DIEInteger(0);
854 Values.insert(Values.begin(), DI);
855 Abbrev->AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
856}
857
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000858/// AddUInt - Add an unsigned integer attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000859///
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000860void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_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(false);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000864 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000865}
866
867/// AddSInt - Add an signed integer attribute data and value.
868///
869void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000870 DIEInteger *DI = new DIEInteger(Integer);
871 Values.push_back(DI);
872 if (!Form) Form = DI->BestForm(true);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000873 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000874}
875
876/// AddString - Add a std::string attribute data and value.
877///
878void DIE::AddString(unsigned Attribute, unsigned Form,
879 const std::string &String) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000880 Values.push_back(new DIEString(String));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000881 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000882}
883
884/// AddLabel - Add a Dwarf label attribute data and value.
885///
886void DIE::AddLabel(unsigned Attribute, unsigned Form,
887 const DWLabel &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000888 Values.push_back(new DIEDwarfLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000889 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000890}
891
Jim Laskey52060a02006-01-24 00:49:18 +0000892/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000893///
Jim Laskey52060a02006-01-24 00:49:18 +0000894void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
895 const std::string &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000896 Values.push_back(new DIEObjectLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000897 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000898}
899
900/// AddDelta - Add a label delta attribute data and value.
901///
902void DIE::AddDelta(unsigned Attribute, unsigned Form,
903 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000904 Values.push_back(new DIEDelta(Hi, Lo));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000905 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000906}
907
908/// AddDIEntry - Add a DIE attribute data and value.
909///
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000910void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000911 Values.push_back(new DIEntry(Entry));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000912 Abbrev->AddAttribute(Attribute, Form);
913}
914
915/// AddBlock - Add block data.
916///
917void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
918 assert(Block->Size && "Block size has not been computed");
919 Values.push_back(Block);
920 if (!Form) Form = Block->BestForm();
921 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000922}
923
924/// Complete - Indicate that all attributes have been added and ready to get an
925/// abbreviation ID.
926void DIE::Complete(DwarfWriter &DW) {
927 AbbrevID = DW.NewAbbreviation(Abbrev);
928 delete Abbrev;
929 Abbrev = NULL;
930}
931
932/// AddChild - Add a child to the DIE.
933///
934void DIE::AddChild(DIE *Child) {
Jim Laskey0420f2a2006-02-22 19:02:11 +0000935 assert(Abbrev && "Adding children without an abbreviation");
936 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000937 Children.push_back(Child);
938}
939
940//===----------------------------------------------------------------------===//
941
Jim Laskey0420f2a2006-02-22 19:02:11 +0000942/// DWContext
Jim Laskeyd18e2892006-01-20 20:34:06 +0000943
944//===----------------------------------------------------------------------===//
Jim Laskey063e7652006-01-17 17:31:53 +0000945
946/// PrintHex - Print a value as a hexidecimal value.
947///
948void DwarfWriter::PrintHex(int Value) const {
949 O << "0x" << std::hex << Value << std::dec;
950}
951
952/// EOL - Print a newline character to asm stream. If a comment is present
953/// then it will be printed first. Comments should not contain '\n'.
954void DwarfWriter::EOL(const std::string &Comment) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000955 if (DwarfVerbose && !Comment.empty()) {
Jim Laskey063e7652006-01-17 17:31:53 +0000956 O << "\t"
957 << Asm->CommentString
958 << " "
959 << Comment;
960 }
961 O << "\n";
962}
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000963
Jim Laskeyb8509c52006-03-23 18:07:55 +0000964/// EmitAlign - Print a align directive.
965///
966void DwarfWriter::EmitAlign(unsigned Alignment) const {
967 O << Asm->AlignDirective << Alignment << "\n";
968}
969
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000970/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
Jim Laskey063e7652006-01-17 17:31:53 +0000971/// unsigned leb128 value.
972void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000973 if (hasLEB128) {
974 O << "\t.uleb128\t"
975 << Value;
976 } else {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000977 O << Asm->Data8bitsDirective;
Jim Laskey063e7652006-01-17 17:31:53 +0000978 PrintULEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000979 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000980}
981
982/// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
Jim Laskey063e7652006-01-17 17:31:53 +0000983/// signed leb128 value.
984void DwarfWriter::EmitSLEB128Bytes(int Value) const {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000985 if (hasLEB128) {
986 O << "\t.sleb128\t"
987 << Value;
988 } else {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000989 O << Asm->Data8bitsDirective;
Jim Laskey063e7652006-01-17 17:31:53 +0000990 PrintSLEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000991 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000992}
993
Jim Laskey063e7652006-01-17 17:31:53 +0000994/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +0000995/// representing an unsigned leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +0000996void DwarfWriter::PrintULEB128(unsigned Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000997 do {
998 unsigned Byte = Value & 0x7f;
999 Value >>= 7;
1000 if (Value) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001001 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001002 if (Value) O << ", ";
1003 } while (Value);
1004}
1005
Jim Laskey063e7652006-01-17 17:31:53 +00001006/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
1007/// value.
1008unsigned DwarfWriter::SizeULEB128(unsigned Value) {
1009 unsigned Size = 0;
1010 do {
1011 Value >>= 7;
1012 Size += sizeof(int8_t);
1013 } while (Value);
1014 return Size;
1015}
1016
1017/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +00001018/// representing a signed leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +00001019void DwarfWriter::PrintSLEB128(int Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001020 int Sign = Value >> (8 * sizeof(Value) - 1);
1021 bool IsMore;
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001022
Jim Laskeyb2efb852006-01-04 22:28:25 +00001023 do {
1024 unsigned Byte = Value & 0x7f;
1025 Value >>= 7;
1026 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1027 if (IsMore) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001028 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001029 if (IsMore) O << ", ";
1030 } while (IsMore);
1031}
1032
Jim Laskey063e7652006-01-17 17:31:53 +00001033/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
1034/// value.
1035unsigned DwarfWriter::SizeSLEB128(int Value) {
1036 unsigned Size = 0;
1037 int Sign = Value >> (8 * sizeof(Value) - 1);
1038 bool IsMore;
1039
1040 do {
1041 unsigned Byte = Value & 0x7f;
1042 Value >>= 7;
1043 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1044 Size += sizeof(int8_t);
1045 } while (IsMore);
1046 return Size;
1047}
1048
Jim Laskeyda427fa2006-01-27 20:31:25 +00001049/// EmitInt8 - Emit a byte directive and value.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001050///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001051void DwarfWriter::EmitInt8(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001052 O << Asm->Data8bitsDirective;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001053 PrintHex(Value & 0xFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001054}
1055
Jim Laskeyda427fa2006-01-27 20:31:25 +00001056/// EmitInt16 - Emit a short directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001057///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001058void DwarfWriter::EmitInt16(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001059 O << Asm->Data16bitsDirective;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001060 PrintHex(Value & 0xFFFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001061}
1062
Jim Laskeyda427fa2006-01-27 20:31:25 +00001063/// EmitInt32 - Emit a long directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001064///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001065void DwarfWriter::EmitInt32(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001066 O << Asm->Data32bitsDirective;
1067 PrintHex(Value);
1068}
1069
Jim Laskeyda427fa2006-01-27 20:31:25 +00001070/// EmitInt64 - Emit a long long directive and value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001071///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001072void DwarfWriter::EmitInt64(uint64_t Value) const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001073 if (Asm->Data64bitsDirective) {
1074 O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
1075 } else {
1076 const TargetData &TD = Asm->TM.getTargetData();
1077
1078 if (TD.isBigEndian()) {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001079 EmitInt32(unsigned(Value >> 32)); O << "\n";
1080 EmitInt32(unsigned(Value));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001081 } else {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001082 EmitInt32(unsigned(Value)); O << "\n";
1083 EmitInt32(unsigned(Value >> 32));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001084 }
1085 }
1086}
1087
Jim Laskey063e7652006-01-17 17:31:53 +00001088/// EmitString - Emit a string with quotes and a null terminator.
1089/// Special characters are emitted properly. (Eg. '\t')
1090void DwarfWriter::EmitString(const std::string &String) const {
1091 O << Asm->AsciiDirective
1092 << "\"";
Jim Laskey52060a02006-01-24 00:49:18 +00001093 for (unsigned i = 0, N = String.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001094 unsigned char C = String[i];
1095
1096 if (!isascii(C) || iscntrl(C)) {
1097 switch(C) {
1098 case '\b': O << "\\b"; break;
1099 case '\f': O << "\\f"; break;
1100 case '\n': O << "\\n"; break;
1101 case '\r': O << "\\r"; break;
1102 case '\t': O << "\\t"; break;
1103 default:
1104 O << '\\';
1105 O << char('0' + (C >> 6));
1106 O << char('0' + (C >> 3));
1107 O << char('0' + (C >> 0));
1108 break;
1109 }
1110 } else if (C == '\"') {
1111 O << "\\\"";
1112 } else if (C == '\'') {
1113 O << "\\\'";
1114 } else {
1115 O << C;
1116 }
1117 }
1118 O << "\\0\"";
1119}
1120
1121/// PrintLabelName - Print label name in form used by Dwarf writer.
1122///
1123void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001124 O << Asm->PrivateGlobalPrefix
1125 << "debug_"
Jim Laskey0420f2a2006-02-22 19:02:11 +00001126 << Tag;
1127 if (Number) O << Number;
Jim Laskeyb2efb852006-01-04 22:28:25 +00001128}
1129
Jim Laskey063e7652006-01-17 17:31:53 +00001130/// EmitLabel - Emit location label for internal use by Dwarf.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001131///
Jim Laskey063e7652006-01-17 17:31:53 +00001132void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
1133 PrintLabelName(Tag, Number);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001134 O << ":\n";
1135}
1136
Jim Laskeye719a7c2006-01-18 16:54:26 +00001137/// EmitReference - Emit a reference to a label.
Jim Laskey063e7652006-01-17 17:31:53 +00001138///
Jim Laskeye719a7c2006-01-18 16:54:26 +00001139void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001140 if (AddressSize == 4)
1141 O << Asm->Data32bitsDirective;
1142 else
1143 O << Asm->Data64bitsDirective;
1144
1145 PrintLabelName(Tag, Number);
1146}
Jim Laskey73683212006-01-21 00:59:54 +00001147void DwarfWriter::EmitReference(const std::string &Name) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001148 if (AddressSize == 4)
1149 O << Asm->Data32bitsDirective;
1150 else
1151 O << Asm->Data64bitsDirective;
1152
1153 O << Name;
1154}
Jim Laskey063e7652006-01-17 17:31:53 +00001155
1156/// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
1157/// assemblers do not accept absolute expressions with data directives, so there
1158/// is an option (needsSet) to use an intermediary 'set' expression.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001159void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
1160 const char *TagLo, unsigned NumberLo) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001161 if (needsSet) {
1162 static unsigned SetCounter = 0;
Jim Laskeyd18e2892006-01-20 20:34:06 +00001163
Jim Laskey063e7652006-01-17 17:31:53 +00001164 O << "\t.set\t";
1165 PrintLabelName("set", SetCounter);
1166 O << ",";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001167 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001168 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001169 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001170 O << "\n";
1171
1172 if (AddressSize == sizeof(int32_t))
1173 O << Asm->Data32bitsDirective;
1174 else
1175 O << Asm->Data64bitsDirective;
1176
1177 PrintLabelName("set", SetCounter);
1178
Jim Laskey52060a02006-01-24 00:49:18 +00001179 ++SetCounter;
Jim Laskey063e7652006-01-17 17:31:53 +00001180 } else {
1181 if (AddressSize == sizeof(int32_t))
1182 O << Asm->Data32bitsDirective;
1183 else
1184 O << Asm->Data64bitsDirective;
1185
Jim Laskeyd18e2892006-01-20 20:34:06 +00001186 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001187 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001188 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001189 }
1190}
1191
Jim Laskeyd18e2892006-01-20 20:34:06 +00001192/// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
Jim Laskey063e7652006-01-17 17:31:53 +00001193///
Jim Laskeyd18e2892006-01-20 20:34:06 +00001194unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
1195 return Abbreviations.insert(*Abbrev);
1196}
1197
1198/// NewString - Add a string to the constant pool and returns a label.
1199///
1200DWLabel DwarfWriter::NewString(const std::string &String) {
1201 unsigned StringID = StringPool.insert(String);
1202 return DWLabel("string", StringID);
1203}
1204
Jim Laskeyb8509c52006-03-23 18:07:55 +00001205/// AddSourceLine - Add location information to specified debug information
1206/// entry.
1207void DwarfWriter::AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1208 if (File && Line) {
1209 CompileUnit *FileUnit = FindCompileUnit(File);
1210 unsigned FileID = FileUnit->getID();
1211 Die->AddUInt(DW_AT_decl_file, 0, FileID);
1212 Die->AddUInt(DW_AT_decl_line, 0, Line);
1213 }
1214}
1215
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001216/// AddAddress - Add an address attribute to a die based on the location
1217/// provided.
1218void DwarfWriter::AddAddress(DIE *Die, unsigned Attribute,
1219 MachineLocation &Location) {
1220 DIEBlock *Block = new DIEBlock();
1221 if (Location.isRegister()) {
1222 Block->AddUInt(DW_FORM_data1, DW_OP_reg0 + Location.getRegister());
1223 } else {
1224 Block->AddUInt(DW_FORM_data1, DW_OP_breg0 + Location.getRegister());
1225 Block->AddUInt(DW_FORM_sdata, Location.getOffset());
1226 }
1227 Block->ComputeSize(*this);
1228 Die->AddBlock(Attribute, 0, Block);
1229}
1230
Jim Laskey90c79d72006-03-23 23:02:34 +00001231/// getDieMapSlotFor - Returns the debug information entry map slot for the
1232/// specified debug descriptor.
1233DIE *&DwarfWriter::getDieMapSlotFor(DebugInfoDesc *DD) {
1234 return DescToDieMap[DD];
Jim Laskey0420f2a2006-02-22 19:02:11 +00001235}
Jim Laskey90c79d72006-03-23 23:02:34 +00001236
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001237/// NewType - Create a new type DIE.
1238///
Jim Laskey90c79d72006-03-23 23:02:34 +00001239DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit) {
1240 if (!TyDesc) {
1241 // FIXME - Hack for missing types
1242 DIE *Die = new DIE(DW_TAG_base_type);
1243 Die->AddUInt(DW_AT_byte_size, 0, 4);
1244 Die->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1245 Unit->getDie()->AddChild(Die);
1246 return Die;
1247 }
Jim Laskey92ae7402006-03-01 18:20:30 +00001248
1249 // FIXME - Should handle other contexts that compile units.
Jim Laskey69906002006-02-24 16:46:40 +00001250
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001251 // Check for pre-existence.
Jim Laskey90c79d72006-03-23 23:02:34 +00001252 DIE *&Slot = Unit->getDieMapSlotFor(TyDesc);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001253 if (Slot) return Slot;
1254
1255 // Get core information.
1256 const std::string &Name = TyDesc->getName();
Jim Laskey288fe0f2006-03-01 18:13:05 +00001257 uint64_t Size = TyDesc->getSize() >> 3;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001258
Jim Laskey434b40b2006-02-23 22:37:30 +00001259 DIE *Ty = NULL;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001260
Jim Laskey434b40b2006-02-23 22:37:30 +00001261 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001262 // Fundamental types like int, float, bool
Jim Laskey434b40b2006-02-23 22:37:30 +00001263 Slot = Ty = new DIE(DW_TAG_base_type);
1264 unsigned Encoding = BasicTy->getEncoding();
1265 Ty->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
Jim Laskey69906002006-02-24 16:46:40 +00001266 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001267 // Create specific DIE.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001268 Slot = Ty = new DIE(DerivedTy->getTag());
Jim Laskey69906002006-02-24 16:46:40 +00001269
1270 // Map to main type, void will not have a type.
1271 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001272 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1273 NewType(Context, FromTy, Unit));
Jim Laskey69906002006-02-24 16:46:40 +00001274 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001275 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
Jim Laskeyf8913f12006-03-01 17:53:02 +00001276 // Create specific DIE.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001277 Slot = Ty = new DIE(CompTy->getTag());
Jim Laskeyf8913f12006-03-01 17:53:02 +00001278 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1279
1280 switch (CompTy->getTag()) {
Jim Laskey9c4447a2006-03-01 20:39:36 +00001281 case DW_TAG_array_type: {
Jim Laskeyf8913f12006-03-01 17:53:02 +00001282 // Add element type.
1283 if (TypeDesc *FromTy = CompTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001284 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1285 NewType(Context, FromTy, Unit));
Jim Laskeyf8913f12006-03-01 17:53:02 +00001286 }
1287 // Don't emit size attribute.
1288 Size = 0;
1289
1290 // Construct an anonymous type for index type.
1291 DIE *IndexTy = new DIE(DW_TAG_base_type);
1292 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1293 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1294 // Add to context.
Jim Laskey92ae7402006-03-01 18:20:30 +00001295 Context->AddChild(IndexTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001296
1297 // Add subranges to array type.
1298 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1299 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1300 int64_t Lo = SRD->getLo();
1301 int64_t Hi = SRD->getHi();
1302 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1303
1304 // If a range is available.
1305 if (Lo != Hi) {
1306 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1307 // Only add low if non-zero.
Jim Laskey6a3eb012006-03-01 23:52:37 +00001308 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1309 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001310 }
1311 Ty->AddChild(Subrange);
1312 }
1313
1314 break;
1315 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001316 case DW_TAG_structure_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +00001317 case DW_TAG_union_type: {
Jim Laskeyf01e5472006-03-03 15:06:57 +00001318 // FIXME - this is just the basics.
1319 // Add elements to structure type.
1320 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1321 DerivedTypeDesc *MemberDesc = cast<DerivedTypeDesc>(Elements[i]);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001322
1323 // Extract the basic information.
Jim Laskeyf01e5472006-03-03 15:06:57 +00001324 const std::string &Name = MemberDesc->getName();
Jim Laskeyf01e5472006-03-03 15:06:57 +00001325 TypeDesc *MemTy = MemberDesc->getFromType();
1326 uint64_t Size = MemberDesc->getSize();
Chris Lattner2695de42006-03-09 17:48:46 +00001327 uint64_t Align = MemberDesc->getAlign();
Jim Laskeyf01e5472006-03-03 15:06:57 +00001328 uint64_t Offset = MemberDesc->getOffset();
1329
Jim Laskeyb8509c52006-03-23 18:07:55 +00001330 // Construct member debug information entry.
Jim Laskeyf01e5472006-03-03 15:06:57 +00001331 DIE *Member = new DIE(DW_TAG_member);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001332
Jim Laskeyb8509c52006-03-23 18:07:55 +00001333 // Add name if not "".
Jim Laskeyf01e5472006-03-03 15:06:57 +00001334 if (!Name.empty()) Member->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001335 // Add location if available.
1336 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001337
Jim Laskey54689c22006-03-09 13:28:47 +00001338 // Most of the time the field info is the same as the members.
1339 uint64_t FieldSize = Size;
1340 uint64_t FieldAlign = Align;
1341 uint64_t FieldOffset = Offset;
Jim Laskey20c3ed82006-03-07 15:51:33 +00001342
Jim Laskeyf01e5472006-03-03 15:06:57 +00001343 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001344 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1345 NewType(Context, FromTy, Unit));
1346 FieldSize = FromTy->getSize();
1347 FieldAlign = FromTy->getSize();
Jim Laskey20c3ed82006-03-07 15:51:33 +00001348 }
1349
Jim Laskey54689c22006-03-09 13:28:47 +00001350 // Unless we have a bit field.
1351 if (FieldSize != Size) {
1352 // Construct the alignment mask.
1353 uint64_t AlignMask = ~(FieldAlign - 1);
1354 // Determine the high bit + 1 of the declared size.
1355 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1356 // Work backwards to determine the base offset of the field.
1357 FieldOffset = HiMark - FieldSize;
1358 // Now normalize offset to the field.
1359 Offset -= FieldOffset;
1360
1361 // Maybe we need to work from the other.
1362 const TargetData &TD = Asm->TM.getTargetData();
1363 if (TD.isLittleEndian()) Offset = FieldSize - (Offset + Size);
1364
1365 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1366 Member->AddUInt(DW_AT_bit_size, 0, Size);
1367 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
Jim Laskeyf01e5472006-03-03 15:06:57 +00001368 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001369
1370 // Add computation for offset.
1371 DIEBlock *Block = new DIEBlock();
1372 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
Jim Laskey54689c22006-03-09 13:28:47 +00001373 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001374 Block->ComputeSize(*this);
1375 Member->AddBlock(DW_AT_data_member_location, 0, Block);
1376
Jim Laskeyf01e5472006-03-03 15:06:57 +00001377 Ty->AddChild(Member);
1378 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001379 break;
1380 }
Jim Laskey9c4447a2006-03-01 20:39:36 +00001381 case DW_TAG_enumeration_type: {
Jim Laskey6a3eb012006-03-01 23:52:37 +00001382 // Add enumerators to enumeration type.
1383 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1384 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1385 const std::string &Name = ED->getName();
1386 int64_t Value = ED->getValue();
1387 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1388 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1389 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1390 Ty->AddChild(Enumerator);
1391 }
1392
Jim Laskeyf8913f12006-03-01 17:53:02 +00001393 break;
1394 }
1395 default: break;
1396 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001397 }
Jim Laskey434b40b2006-02-23 22:37:30 +00001398
1399 assert(Ty && "Type not supported yet");
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001400
Jim Laskey69906002006-02-24 16:46:40 +00001401 // Add size if non-zero (derived types don't have a size.)
Jim Laskey434b40b2006-02-23 22:37:30 +00001402 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
Jim Laskey69906002006-02-24 16:46:40 +00001403 // Add name if not anonymous or intermediate type.
Jim Laskey434b40b2006-02-23 22:37:30 +00001404 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001405 // Add source line info if available.
1406 AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
Jim Laskey434b40b2006-02-23 22:37:30 +00001407
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001408 // Add to context owner.
Jim Laskey92ae7402006-03-01 18:20:30 +00001409 Context->AddChild(Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001410
1411 return Slot;
1412}
1413
Jim Laskeyb8509c52006-03-23 18:07:55 +00001414/// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey063e7652006-01-17 17:31:53 +00001415///
Jim Laskeybd761842006-02-27 17:27:12 +00001416CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1417 unsigned ID) {
1418 // Construct debug information entry.
1419 DIE *Die = new DIE(DW_TAG_compile_unit);
1420 Die->AddLabel (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0));
1421 Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1422 Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
1423 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1424 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1425 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1426 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1427
Jim Laskeyb8509c52006-03-23 18:07:55 +00001428 // Add debug information entry to descriptor map.
Jim Laskey90c79d72006-03-23 23:02:34 +00001429 DIE *&Slot = getDieMapSlotFor(UnitDesc);
1430 Slot = Die;
Jim Laskeybd761842006-02-27 17:27:12 +00001431
1432 // Construct compile unit.
1433 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1434
1435 // Add Unit to compile unit map.
1436 DescToUnitMap[UnitDesc] = Unit;
1437
1438 return Unit;
1439}
Jim Laskey0420f2a2006-02-22 19:02:11 +00001440
Jim Laskeybd761842006-02-27 17:27:12 +00001441/// FindCompileUnit - Get the compile unit for the given descriptor.
1442///
1443CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1444 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1445 assert(Unit && "Missing compile unit.");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001446 return Unit;
Jim Laskey063e7652006-01-17 17:31:53 +00001447}
1448
Jim Laskey0420f2a2006-02-22 19:02:11 +00001449/// NewGlobalVariable - Add a new global variable DIE.
1450///
1451DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001452 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001453 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1454 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey90c79d72006-03-23 23:02:34 +00001455
1456 // Check for pre-existence.
1457 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1458 if (Slot) return Slot;
1459
Jim Laskey0420f2a2006-02-22 19:02:11 +00001460 // Get the global variable itself.
1461 GlobalVariable *GV = GVD->getGlobalVariable();
1462 // Generate the mangled name.
1463 std::string MangledName = Asm->Mang->getValueName(GV);
1464
1465 // Gather the details (simplify add attribute code.)
1466 const std::string &Name = GVD->getName();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001467
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001468 // Get the global's type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001469 DIE *Type = NewType(Unit->getDie(), GVD->getType(), Unit);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001470
1471 // Create the globale variable DIE.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001472 DIE *VariableDie = new DIE(DW_TAG_variable);
1473 VariableDie->AddString (DW_AT_name, DW_FORM_string, Name);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001474 VariableDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1475 VariableDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001476
1477 // Add source line info if available.
1478 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001479
Jim Laskeyb8509c52006-03-23 18:07:55 +00001480 // Add address.
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001481 DIEBlock *Block = new DIEBlock();
1482 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
1483 Block->AddObjectLabel(DW_FORM_udata, MangledName);
1484 Block->ComputeSize(*this);
1485 VariableDie->AddBlock(DW_AT_location, 0, Block);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001486
1487 // Add to map.
1488 Slot = VariableDie;
1489
1490 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001491 Unit->getDie()->AddChild(VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001492
1493 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001494 // FIXME - need to check external flag.
1495 Unit->AddGlobal(Name, VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001496
1497 return VariableDie;
1498}
1499
1500/// NewSubprogram - Add a new subprogram DIE.
1501///
1502DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001503 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001504 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1505 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001506
Jim Laskey90c79d72006-03-23 23:02:34 +00001507 // Check for pre-existence.
1508 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1509 if (Slot) return Slot;
1510
Jim Laskey0420f2a2006-02-22 19:02:11 +00001511 // Gather the details (simplify add attribute code.)
1512 const std::string &Name = SPD->getName();
Jim Laskey90c79d72006-03-23 23:02:34 +00001513 DIE *Type = NewType(Unit->getDie(), SPD->getType(), Unit);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001514 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001515
Jim Laskey8a8e9752006-02-27 20:37:42 +00001516 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001517 SubprogramDie->AddString (DW_AT_name, DW_FORM_string, Name);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001518 SubprogramDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001519 SubprogramDie->AddUInt (DW_AT_external, DW_FORM_flag, IsExternal);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001520
Jim Laskeyb8509c52006-03-23 18:07:55 +00001521 // Add source line info if available.
1522 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1523
Jim Laskey0420f2a2006-02-22 19:02:11 +00001524 // Add to map.
1525 Slot = SubprogramDie;
1526
1527 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001528 Unit->getDie()->AddChild(SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001529
1530 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001531 Unit->AddGlobal(Name, SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001532
1533 return SubprogramDie;
1534}
1535
Jim Laskeyb8509c52006-03-23 18:07:55 +00001536/// NewScopeVariable - Create a new scope variable.
1537///
1538DIE *DwarfWriter::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1539 // Get the descriptor.
1540 VariableDesc *VD = DV->getDesc();
1541
1542 // Translate tag to proper Dwarf tag. The result variable is dropped for now.
1543 unsigned Tag;
1544 switch (VD->getTag()) {
1545 case DW_TAG_return_variable: return NULL;
1546 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1547 case DW_TAG_auto_variable: // fall thru
1548 default: Tag = DW_TAG_variable; break;
1549 }
1550
1551 // Define variable debug information entry.
1552 DIE *VariableDie = new DIE(Tag);
1553 VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
1554
1555 // Add source line info if available.
1556 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1557
1558 // Add variable type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001559 DIE *Type = NewType(Unit->getDie(), VD->getType(), Unit);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001560 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1561
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001562 // Add variable address.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001563 MachineLocation Location;
1564 Asm->TM.getRegisterInfo()->getLocation(*MF, DV->getFrameIndex(), Location);
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001565 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001566
1567 return VariableDie;
1568}
1569
1570/// ConstructScope - Construct the components of a scope.
1571///
1572void DwarfWriter::ConstructScope(DebugScope *ParentScope,
1573 DIE *ParentDie, CompileUnit *Unit) {
1574 // Add variables to scope.
1575 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1576 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1577 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1578 if (VariableDie) ParentDie->AddChild(VariableDie);
1579 }
1580
1581 // Add nested scopes.
1582 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1583 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1584 // Define the Scope debug information entry.
1585 DebugScope *Scope = Scopes[j];
1586 // FIXME - Ignore inlined functions for the time being.
1587 if (Scope->getParent()) continue;
1588
1589 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1590
1591 // Add the scope bounds.
1592 if (unsigned StartID = Scope->getStartLabelID()) {
1593 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1594 DWLabel("loc", StartID));
1595 } else {
1596 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1597 DWLabel("func_begin", SubprogramCount));
1598 }
1599 if (unsigned EndID = Scope->getEndLabelID()) {
1600 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1601 DWLabel("loc", EndID));
1602 } else {
1603 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1604 DWLabel("func_end", SubprogramCount));
1605 }
1606
1607 // Add the scope contents.
1608 ConstructScope(Scope, ScopeDie, Unit);
1609 ParentDie->AddChild(ScopeDie);
1610 }
1611}
1612
1613/// ConstructRootScope - Construct the scope for the subprogram.
1614///
1615void DwarfWriter::ConstructRootScope(DebugScope *RootScope) {
1616 // Exit if there is no root scope.
1617 if (!RootScope) return;
1618
1619 // Get the subprogram debug information entry.
1620 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Jim Laskey90c79d72006-03-23 23:02:34 +00001621
1622 // Get the compile unit context.
1623 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1624 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1625
1626 // Get the subprogram die.
1627 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001628 assert(SPDie && "Missing subprogram descriptor");
1629
1630 // Add the function bounds.
1631 SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1632 DWLabel("func_begin", SubprogramCount));
1633 SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1634 DWLabel("func_end", SubprogramCount));
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001635 MachineLocation Location(Asm->TM.getRegisterInfo()->getFrameRegister(*MF));
1636 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001637
Jim Laskeyb8509c52006-03-23 18:07:55 +00001638 ConstructScope(RootScope, SPDie, Unit);
1639}
1640
Jim Laskey063e7652006-01-17 17:31:53 +00001641/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1642/// tools to recognize the object file contains Dwarf information.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001643///
1644void DwarfWriter::EmitInitial() const {
Jim Laskey063e7652006-01-17 17:31:53 +00001645 // Dwarf sections base addresses.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001646 Asm->SwitchSection(DwarfFrameSection, 0);
1647 EmitLabel("section_frame", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001648 Asm->SwitchSection(DwarfInfoSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001649 EmitLabel("section_info", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001650 EmitLabel("info", 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001651 Asm->SwitchSection(DwarfAbbrevSection, 0);
1652 EmitLabel("section_abbrev", 0);
1653 EmitLabel("abbrev", 0);
1654 Asm->SwitchSection(DwarfARangesSection, 0);
1655 EmitLabel("section_aranges", 0);
1656 Asm->SwitchSection(DwarfMacInfoSection, 0);
1657 EmitLabel("section_macinfo", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001658 Asm->SwitchSection(DwarfLineSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001659 EmitLabel("section_line", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001660 EmitLabel("line", 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001661 Asm->SwitchSection(DwarfLocSection, 0);
1662 EmitLabel("section_loc", 0);
1663 Asm->SwitchSection(DwarfPubNamesSection, 0);
1664 EmitLabel("section_pubnames", 0);
1665 Asm->SwitchSection(DwarfStrSection, 0);
1666 EmitLabel("section_str", 0);
1667 Asm->SwitchSection(DwarfRangesSection, 0);
1668 EmitLabel("section_ranges", 0);
1669
Jim Laskey063e7652006-01-17 17:31:53 +00001670 Asm->SwitchSection(TextSection, 0);
1671 EmitLabel("text_begin", 0);
1672 Asm->SwitchSection(DataSection, 0);
1673 EmitLabel("data_begin", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001674}
1675
Jim Laskey063e7652006-01-17 17:31:53 +00001676/// EmitDIE - Recusively Emits a debug information entry.
1677///
1678void DwarfWriter::EmitDIE(DIE *Die) const {
1679 // Get the abbreviation for this DIE.
1680 unsigned AbbrevID = Die->getAbbrevID();
1681 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey0d086af2006-02-27 12:43:29 +00001682
1683 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001684
1685 // Emit the code (index) for the abbreviation.
1686 EmitULEB128Bytes(AbbrevID);
1687 EOL(std::string("Abbrev [" +
1688 utostr(AbbrevID) +
Jim Laskey0d086af2006-02-27 12:43:29 +00001689 "] 0x" + utohexstr(Die->getOffset()) +
1690 ":0x" + utohexstr(Die->getSize()) + " " +
1691 TagString(Abbrev.getTag())));
Jim Laskey063e7652006-01-17 17:31:53 +00001692
1693 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001694 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001695
1696 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001697 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001698 unsigned Attr = AbbrevData[i].getAttribute();
1699 unsigned Form = AbbrevData[i].getForm();
Jim Laskey063e7652006-01-17 17:31:53 +00001700 assert(Form && "Too many attributes for DIE (check abbreviation)");
1701
1702 switch (Attr) {
1703 case DW_AT_sibling: {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001704 EmitInt32(Die->SiblingOffset());
Jim Laskey063e7652006-01-17 17:31:53 +00001705 break;
1706 }
1707 default: {
1708 // Emit an attribute using the defined form.
1709 Values[i]->EmitValue(*this, Form);
1710 break;
1711 }
1712 }
1713
1714 EOL(AttributeString(Attr));
1715 }
1716
1717 // Emit the DIE children if any.
1718 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1719 const std::vector<DIE *> &Children = Die->getChildren();
1720
Jim Laskey52060a02006-01-24 00:49:18 +00001721 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskey063e7652006-01-17 17:31:53 +00001722 EmitDIE(Children[j]);
1723 }
1724
Jim Laskeyda427fa2006-01-27 20:31:25 +00001725 EmitInt8(0); EOL("End Of Children Mark");
Jim Laskey063e7652006-01-17 17:31:53 +00001726 }
1727}
1728
1729/// SizeAndOffsetDie - Compute the size and offset of a DIE.
1730///
Jim Laskeyb8509c52006-03-23 18:07:55 +00001731unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1732 // Get the children.
1733 const std::vector<DIE *> &Children = Die->getChildren();
1734
1735 // If not last sibling and has children then add sibling offset attribute.
1736 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1737
Jim Laskey0420f2a2006-02-22 19:02:11 +00001738 // Record the abbreviation.
1739 Die->Complete(*this);
1740
Jim Laskey063e7652006-01-17 17:31:53 +00001741 // Get the abbreviation for this DIE.
1742 unsigned AbbrevID = Die->getAbbrevID();
1743 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1744
1745 // Set DIE offset
1746 Die->setOffset(Offset);
1747
1748 // Start the size with the size of abbreviation code.
1749 Offset += SizeULEB128(AbbrevID);
1750
1751 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001752 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001753
1754 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001755 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001756 // Size attribute value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001757 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
Jim Laskey063e7652006-01-17 17:31:53 +00001758 }
1759
1760 // Emit the DIE children if any.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001761 if (!Children.empty()) {
1762 assert(Abbrev.getChildrenFlag() == DW_CHILDREN_yes &&
1763 "Children flag not set");
Jim Laskey063e7652006-01-17 17:31:53 +00001764
Jim Laskey52060a02006-01-24 00:49:18 +00001765 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001766 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
Jim Laskey063e7652006-01-17 17:31:53 +00001767 }
1768
1769 // End of children marker.
1770 Offset += sizeof(int8_t);
1771 }
1772
1773 Die->setSize(Offset - Die->getOffset());
1774 return Offset;
1775}
1776
1777/// SizeAndOffsets - Compute the size and offset of all the DIEs.
1778///
1779void DwarfWriter::SizeAndOffsets() {
Jim Laskey063e7652006-01-17 17:31:53 +00001780
1781 // Process each compile unit.
Jim Laskey52060a02006-01-24 00:49:18 +00001782 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00001783 CompileUnit *Unit = CompileUnits[i];
1784 if (Unit->hasContent()) {
1785 // Compute size of compile unit header
1786 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1787 sizeof(int16_t) + // DWARF version number
1788 sizeof(int32_t) + // Offset Into Abbrev. Section
1789 sizeof(int8_t); // Pointer Size (in bytes)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001790 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
Jim Laskeybd761842006-02-27 17:27:12 +00001791 }
Jim Laskey063e7652006-01-17 17:31:53 +00001792 }
1793}
1794
1795/// EmitDebugInfo - Emit the debug info section.
1796///
1797void DwarfWriter::EmitDebugInfo() const {
1798 // Start debug info section.
1799 Asm->SwitchSection(DwarfInfoSection, 0);
1800
Jim Laskeybd761842006-02-27 17:27:12 +00001801 // Process each compile unit.
1802 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1803 CompileUnit *Unit = CompileUnits[i];
1804
1805 if (Unit->hasContent()) {
1806 DIE *Die = Unit->getDie();
Jim Laskey0d086af2006-02-27 12:43:29 +00001807 // Emit the compile units header.
Jim Laskeybd761842006-02-27 17:27:12 +00001808 EmitLabel("info_begin", Unit->getID());
Jim Laskey0d086af2006-02-27 12:43:29 +00001809 // Emit size of content not including length itself
Jim Laskeybd761842006-02-27 17:27:12 +00001810 unsigned ContentSize = Die->getSize() +
Jim Laskey0d086af2006-02-27 12:43:29 +00001811 sizeof(int16_t) + // DWARF version number
1812 sizeof(int32_t) + // Offset Into Abbrev. Section
1813 sizeof(int8_t); // Pointer Size (in bytes)
1814
1815 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
1816 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1817 EmitReference("abbrev_begin", 0); EOL("Offset Into Abbrev. Section");
1818 EmitInt8(AddressSize); EOL("Address Size (in bytes)");
1819
Jim Laskeybd761842006-02-27 17:27:12 +00001820 EmitDIE(Die);
1821 EmitLabel("info_end", Unit->getID());
Jim Laskey063e7652006-01-17 17:31:53 +00001822 }
Jim Laskey0d086af2006-02-27 12:43:29 +00001823
1824 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001825 }
1826}
1827
1828/// EmitAbbreviations - Emit the abbreviation section.
1829///
1830void DwarfWriter::EmitAbbreviations() const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001831 // Check to see if it is worth the effort.
1832 if (!Abbreviations.empty()) {
1833 // Start the debug abbrev section.
1834 Asm->SwitchSection(DwarfAbbrevSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001835
Jim Laskeyd18e2892006-01-20 20:34:06 +00001836 EmitLabel("abbrev_begin", 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001837
Jim Laskeyd18e2892006-01-20 20:34:06 +00001838 // For each abbrevation.
1839 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001840 AbbrevID <= NAID; ++AbbrevID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001841 // Get abbreviation data
1842 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey063e7652006-01-17 17:31:53 +00001843
Jim Laskeyd18e2892006-01-20 20:34:06 +00001844 // Emit the abbrevations code (base 1 index.)
1845 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
Jim Laskey063e7652006-01-17 17:31:53 +00001846
Jim Laskeyd18e2892006-01-20 20:34:06 +00001847 // Emit the abbreviations data.
1848 Abbrev.Emit(*this);
Jim Laskey0d086af2006-02-27 12:43:29 +00001849
1850 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001851 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00001852
1853 EmitLabel("abbrev_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00001854
1855 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001856 }
1857}
1858
1859/// EmitDebugLines - Emit source line information.
1860///
1861void DwarfWriter::EmitDebugLines() const {
1862 // Minimum line delta, thus ranging from -10..(255-10).
1863 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1864 // Maximum line delta, thus ranging from -10..(255-10).
1865 const int MaxLineDelta = 255 + MinLineDelta;
1866
1867 // Start the dwarf line section.
1868 Asm->SwitchSection(DwarfLineSection, 0);
1869
1870 // Construct the section header.
1871
1872 EmitDifference("line_end", 0, "line_begin", 0);
1873 EOL("Length of Source Line Info");
1874 EmitLabel("line_begin", 0);
1875
Jim Laskeyda427fa2006-01-27 20:31:25 +00001876 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey063e7652006-01-17 17:31:53 +00001877
1878 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
1879 EOL("Prolog Length");
1880 EmitLabel("line_prolog_begin", 0);
1881
Jim Laskeyda427fa2006-01-27 20:31:25 +00001882 EmitInt8(1); EOL("Minimum Instruction Length");
Jim Laskey063e7652006-01-17 17:31:53 +00001883
Jim Laskeyda427fa2006-01-27 20:31:25 +00001884 EmitInt8(1); EOL("Default is_stmt_start flag");
Jim Laskey063e7652006-01-17 17:31:53 +00001885
Jim Laskeyda427fa2006-01-27 20:31:25 +00001886 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00001887
Jim Laskeyda427fa2006-01-27 20:31:25 +00001888 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00001889
Jim Laskeyda427fa2006-01-27 20:31:25 +00001890 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Jim Laskey063e7652006-01-17 17:31:53 +00001891
1892 // Line number standard opcode encodings argument count
Jim Laskeyda427fa2006-01-27 20:31:25 +00001893 EmitInt8(0); EOL("DW_LNS_copy arg count");
1894 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
1895 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
1896 EmitInt8(1); EOL("DW_LNS_set_file arg count");
1897 EmitInt8(1); EOL("DW_LNS_set_column arg count");
1898 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
1899 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
1900 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
1901 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskey063e7652006-01-17 17:31:53 +00001902
1903 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1904 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1905
1906 // Emit directories.
1907 for (unsigned DirectoryID = 1, NDID = Directories.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001908 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskey063e7652006-01-17 17:31:53 +00001909 EmitString(Directories[DirectoryID]); EOL("Directory");
1910 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00001911 EmitInt8(0); EOL("End of directories");
Jim Laskey063e7652006-01-17 17:31:53 +00001912
1913 // Emit files.
1914 for (unsigned SourceID = 1, NSID = SourceFiles.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001915 SourceID <= NSID; ++SourceID) {
Jim Laskey063e7652006-01-17 17:31:53 +00001916 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1917 EmitString(SourceFile.getName()); EOL("Source");
1918 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
1919 EmitULEB128Bytes(0); EOL("Mod date");
1920 EmitULEB128Bytes(0); EOL("File size");
1921 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00001922 EmitInt8(0); EOL("End of files");
Jim Laskey063e7652006-01-17 17:31:53 +00001923
1924 EmitLabel("line_prolog_end", 0);
1925
1926 // Emit line information
1927 const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
1928
1929 // Dwarf assumes we start with first line of first source file.
1930 unsigned Source = 1;
1931 unsigned Line = 1;
1932
1933 // Construct rows of the address, source, line, column matrix.
Jim Laskey52060a02006-01-24 00:49:18 +00001934 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001935 SourceLineInfo *LineInfo = LineInfos[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00001936
1937 if (DwarfVerbose) {
1938 unsigned SourceID = LineInfo->getSourceID();
1939 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1940 unsigned DirectoryID = SourceFile.getDirectoryID();
1941 O << "\t"
1942 << Asm->CommentString << " "
1943 << Directories[DirectoryID]
1944 << SourceFile.getName() << ":"
1945 << LineInfo->getLine() << "\n";
1946 }
Jim Laskey063e7652006-01-17 17:31:53 +00001947
1948 // Define the line address.
Jim Laskeyda427fa2006-01-27 20:31:25 +00001949 EmitInt8(0); EOL("Extended Op");
1950 EmitInt8(4 + 1); EOL("Op size");
1951 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskeyb8509c52006-03-23 18:07:55 +00001952 EmitReference("loc", LineInfo->getLabelID()); EOL("Location label");
Jim Laskey063e7652006-01-17 17:31:53 +00001953
1954 // If change of source, then switch to the new source.
1955 if (Source != LineInfo->getSourceID()) {
1956 Source = LineInfo->getSourceID();
Jim Laskeyda427fa2006-01-27 20:31:25 +00001957 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
Jim Laskeybd761842006-02-27 17:27:12 +00001958 EmitULEB128Bytes(Source); EOL("New Source");
Jim Laskey063e7652006-01-17 17:31:53 +00001959 }
1960
1961 // If change of line.
1962 if (Line != LineInfo->getLine()) {
1963 // Determine offset.
1964 int Offset = LineInfo->getLine() - Line;
1965 int Delta = Offset - MinLineDelta;
1966
1967 // Update line.
1968 Line = LineInfo->getLine();
1969
1970 // If delta is small enough and in range...
1971 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
1972 // ... then use fast opcode.
Jim Laskeyda427fa2006-01-27 20:31:25 +00001973 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
Jim Laskey063e7652006-01-17 17:31:53 +00001974 } else {
1975 // ... otherwise use long hand.
Jim Laskeyda427fa2006-01-27 20:31:25 +00001976 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
Jim Laskey063e7652006-01-17 17:31:53 +00001977 EmitSLEB128Bytes(Offset); EOL("Line Offset");
Jim Laskeyda427fa2006-01-27 20:31:25 +00001978 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
Jim Laskey063e7652006-01-17 17:31:53 +00001979 }
1980 } else {
1981 // Copy the previous row (different address or source)
Jim Laskeyda427fa2006-01-27 20:31:25 +00001982 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
Jim Laskey063e7652006-01-17 17:31:53 +00001983 }
1984 }
1985
Jim Laskey0420f2a2006-02-22 19:02:11 +00001986 // Define last address.
1987 EmitInt8(0); EOL("Extended Op");
1988 EmitInt8(4 + 1); EOL("Op size");
1989 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1990 EmitReference("text_end", 0); EOL("Location label");
1991
Jim Laskey063e7652006-01-17 17:31:53 +00001992 // Mark end of matrix.
Jim Laskeyda427fa2006-01-27 20:31:25 +00001993 EmitInt8(0); EOL("DW_LNE_end_sequence");
Jim Laskey063e7652006-01-17 17:31:53 +00001994 EmitULEB128Bytes(1); O << "\n";
Jim Laskeyda427fa2006-01-27 20:31:25 +00001995 EmitInt8(1); O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001996
1997 EmitLabel("line_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00001998
1999 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002000}
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002001
2002/// EmitDebugFrame - Emit visible names into a debug frame section.
2003///
2004void DwarfWriter::EmitDebugFrame() {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002005 // Start the dwarf pubnames section.
2006 Asm->SwitchSection(DwarfFrameSection, 0);
2007
2008 EmitDifference("frame_common_end", 0,
2009 "frame_common_begin", 0);
2010 EOL("Length of Common Information Entry");
2011
2012 EmitLabel("frame_common_begin", 0);
2013 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2014 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2015 EmitString(""); EOL("CIE Augmentation");
2016 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
2017 // FIXME - needs to change based on stack direction.
2018 EmitSLEB128Bytes(-sizeof(int32_t)); EOL("CIE Data Alignment Factor");
2019 // FIXME - hard coded for PPC (LR).
2020 EmitInt8(0x41); EOL("CIE RA Column Hardcoded (PPC LR)");
2021 // FIXME - hard coded for PPC 0(SP).
2022 EmitULEB128Bytes(DW_CFA_def_cfa); EOL("DW_CFA_def_cfa");
2023 EmitULEB128Bytes(1); EOL("PPC Register SP");
2024 EmitULEB128Bytes(0); EOL("PPC offset 0 as in 0(SP)");
2025 EmitAlign(2);
2026 EmitLabel("frame_common_end", 0);
2027
2028 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002029}
2030
2031/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2032///
2033void DwarfWriter::EmitDebugPubNames() {
Jim Laskeybd761842006-02-27 17:27:12 +00002034 // Start the dwarf pubnames section.
2035 Asm->SwitchSection(DwarfPubNamesSection, 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002036
Jim Laskeybd761842006-02-27 17:27:12 +00002037 // Process each compile unit.
2038 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2039 CompileUnit *Unit = CompileUnits[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00002040
Jim Laskeybd761842006-02-27 17:27:12 +00002041 if (Unit->hasContent()) {
2042 EmitDifference("pubnames_end", Unit->getID(),
2043 "pubnames_begin", Unit->getID());
2044 EOL("Length of Public Names Info");
2045
2046 EmitLabel("pubnames_begin", Unit->getID());
2047
2048 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2049
2050 EmitReference("info_begin", Unit->getID());
2051 EOL("Offset of Compilation Unit Info");
Jim Laskeyd18e2892006-01-20 20:34:06 +00002052
Jim Laskeybd761842006-02-27 17:27:12 +00002053 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2054 EOL("Compilation Unit Length");
2055
2056 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2057
2058 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2059 GE = Globals.end();
2060 GI != GE; ++GI) {
2061 const std::string &Name = GI->first;
2062 DIE * Entity = GI->second;
2063
2064 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2065 EmitString(Name); EOL("External Name");
2066 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002067
Jim Laskeybd761842006-02-27 17:27:12 +00002068 EmitInt32(0); EOL("End Mark");
2069 EmitLabel("pubnames_end", Unit->getID());
2070
2071 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002072 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002073 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002074}
2075
2076/// EmitDebugStr - Emit visible names into a debug str section.
2077///
2078void DwarfWriter::EmitDebugStr() {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002079 // Check to see if it is worth the effort.
2080 if (!StringPool.empty()) {
2081 // Start the dwarf str section.
2082 Asm->SwitchSection(DwarfStrSection, 0);
2083
Jim Laskeyb8509c52006-03-23 18:07:55 +00002084 // For each of strings in the string pool.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002085 for (unsigned StringID = 1, N = StringPool.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002086 StringID <= N; ++StringID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002087 // Emit a label for reference from debug information entries.
2088 EmitLabel("string", StringID);
2089 // Emit the string itself.
2090 const std::string &String = StringPool[StringID];
2091 EmitString(String); O << "\n";
2092 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002093
2094 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002095 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002096}
2097
2098/// EmitDebugLoc - Emit visible names into a debug loc section.
2099///
2100void DwarfWriter::EmitDebugLoc() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002101 // Start the dwarf loc section.
2102 Asm->SwitchSection(DwarfLocSection, 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002103
2104 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002105}
2106
2107/// EmitDebugARanges - Emit visible names into a debug aranges section.
2108///
2109void DwarfWriter::EmitDebugARanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002110 // Start the dwarf aranges section.
2111 Asm->SwitchSection(DwarfARangesSection, 0);
2112
2113 // FIXME - Mock up
Jim Laskeybd761842006-02-27 17:27:12 +00002114#if 0
2115 // Process each compile unit.
2116 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2117 CompileUnit *Unit = CompileUnits[i];
2118
2119 if (Unit->hasContent()) {
2120 // Don't include size of length
2121 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2122
2123 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2124
2125 EmitReference("info_begin", Unit->getID());
2126 EOL("Offset of Compilation Unit Info");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002127
Jim Laskeybd761842006-02-27 17:27:12 +00002128 EmitInt8(AddressSize); EOL("Size of Address");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002129
Jim Laskeybd761842006-02-27 17:27:12 +00002130 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002131
Jim Laskeybd761842006-02-27 17:27:12 +00002132 EmitInt16(0); EOL("Pad (1)");
2133 EmitInt16(0); EOL("Pad (2)");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002134
Jim Laskeybd761842006-02-27 17:27:12 +00002135 // Range 1
2136 EmitReference("text_begin", 0); EOL("Address");
2137 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002138
Jim Laskeybd761842006-02-27 17:27:12 +00002139 EmitInt32(0); EOL("EOM (1)");
2140 EmitInt32(0); EOL("EOM (2)");
2141
2142 O << "\n";
2143 }
2144 }
2145#endif
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002146}
2147
2148/// EmitDebugRanges - Emit visible names into a debug ranges section.
2149///
2150void DwarfWriter::EmitDebugRanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002151 // Start the dwarf ranges section.
2152 Asm->SwitchSection(DwarfRangesSection, 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002153
2154 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002155}
2156
2157/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2158///
2159void DwarfWriter::EmitDebugMacInfo() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002160 // Start the dwarf macinfo section.
2161 Asm->SwitchSection(DwarfMacInfoSection, 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002162
2163 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002164}
Jim Laskey063e7652006-01-17 17:31:53 +00002165
Jim Laskey52060a02006-01-24 00:49:18 +00002166/// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2167/// header file.
2168void DwarfWriter::ConstructCompileUnitDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002169 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
Jim Laskey6e87c0e2006-01-26 21:22:49 +00002170
2171 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00002172 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
Jim Laskey52060a02006-01-24 00:49:18 +00002173 CompileUnits.push_back(Unit);
2174 }
2175}
2176
2177/// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2178/// variables.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002179void DwarfWriter::ConstructGlobalDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002180 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002181 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00002182
2183 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002184 GlobalVariableDesc *GVD = GlobalVariables[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002185 NewGlobalVariable(GVD);
Jim Laskey52060a02006-01-24 00:49:18 +00002186 }
2187}
2188
Jim Laskey0420f2a2006-02-22 19:02:11 +00002189/// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2190/// subprograms.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002191void DwarfWriter::ConstructSubprogramDIEs() {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002192 std::vector<SubprogramDesc *> Subprograms =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002193 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002194
2195 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2196 SubprogramDesc *SPD = Subprograms[i];
2197 NewSubprogram(SPD);
2198 }
2199}
Jim Laskey52060a02006-01-24 00:49:18 +00002200
Jim Laskey063e7652006-01-17 17:31:53 +00002201/// ShouldEmitDwarf - Determine if Dwarf declarations should be made.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002202///
2203bool DwarfWriter::ShouldEmitDwarf() {
2204 // Check if debug info is present.
2205 if (!DebugInfo || !DebugInfo->hasInfo()) return false;
2206
2207 // Make sure initial declarations are made.
2208 if (!didInitial) {
2209 EmitInitial();
Jim Laskeyb8509c52006-03-23 18:07:55 +00002210
2211 // Create all the compile unit DIEs.
2212 ConstructCompileUnitDIEs();
2213
2214 // Create DIEs for each of the externally visible global variables.
2215 ConstructGlobalDIEs();
2216
2217 // Create DIEs for each of the externally visible subprograms.
2218 ConstructSubprogramDIEs();
2219
Jim Laskeyb2efb852006-01-04 22:28:25 +00002220 didInitial = true;
2221 }
2222
2223 // Okay to emit.
2224 return true;
2225}
2226
Jim Laskey063e7652006-01-17 17:31:53 +00002227//===----------------------------------------------------------------------===//
Jim Laskeyd18e2892006-01-20 20:34:06 +00002228// Main entry points.
Jim Laskey063e7652006-01-17 17:31:53 +00002229//
Jim Laskey52060a02006-01-24 00:49:18 +00002230
2231DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
2232: O(OS)
2233, Asm(A)
Jim Laskeyb8509c52006-03-23 18:07:55 +00002234, M(NULL)
2235, MF(NULL)
Jim Laskey52060a02006-01-24 00:49:18 +00002236, DebugInfo(NULL)
2237, didInitial(false)
Jim Laskeyb8509c52006-03-23 18:07:55 +00002238, SubprogramCount(0)
Jim Laskey52060a02006-01-24 00:49:18 +00002239, CompileUnits()
2240, Abbreviations()
Jim Laskey52060a02006-01-24 00:49:18 +00002241, StringPool()
Jim Laskeybd761842006-02-27 17:27:12 +00002242, DescToUnitMap()
Jim Laskey0420f2a2006-02-22 19:02:11 +00002243, DescToDieMap()
2244, TypeToDieMap()
Jim Laskey52060a02006-01-24 00:49:18 +00002245, AddressSize(sizeof(int32_t))
2246, hasLEB128(false)
2247, hasDotLoc(false)
2248, hasDotFile(false)
2249, needsSet(false)
2250, DwarfAbbrevSection(".debug_abbrev")
2251, DwarfInfoSection(".debug_info")
2252, DwarfLineSection(".debug_line")
2253, DwarfFrameSection(".debug_frame")
2254, DwarfPubNamesSection(".debug_pubnames")
2255, DwarfPubTypesSection(".debug_pubtypes")
2256, DwarfStrSection(".debug_str")
2257, DwarfLocSection(".debug_loc")
2258, DwarfARangesSection(".debug_aranges")
2259, DwarfRangesSection(".debug_ranges")
2260, DwarfMacInfoSection(".debug_macinfo")
2261, TextSection(".text")
2262, DataSection(".data")
2263{}
2264DwarfWriter::~DwarfWriter() {
2265 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2266 delete CompileUnits[i];
Jim Laskey063e7652006-01-17 17:31:53 +00002267 }
Jim Laskey52060a02006-01-24 00:49:18 +00002268}
Jim Laskey063e7652006-01-17 17:31:53 +00002269
2270/// BeginModule - Emit all Dwarf sections that should come prior to the content.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002271///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002272void DwarfWriter::BeginModule(Module *M) {
2273 this->M = M;
2274
Jim Laskeyb2efb852006-01-04 22:28:25 +00002275 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002276 EOL("Dwarf Begin Module");
Jim Laskeyb2efb852006-01-04 22:28:25 +00002277}
2278
Jim Laskey063e7652006-01-17 17:31:53 +00002279/// EndModule - Emit all Dwarf sections that should come after the content.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002280///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002281void DwarfWriter::EndModule() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002282 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002283 EOL("Dwarf End Module");
2284
2285 // Standard sections final addresses.
Jim Laskeye719a7c2006-01-18 16:54:26 +00002286 Asm->SwitchSection(TextSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002287 EmitLabel("text_end", 0);
Jim Laskeye719a7c2006-01-18 16:54:26 +00002288 Asm->SwitchSection(DataSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002289 EmitLabel("data_end", 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002290
Jim Laskey063e7652006-01-17 17:31:53 +00002291 // Compute DIE offsets and sizes.
2292 SizeAndOffsets();
2293
2294 // Emit all the DIEs into a debug info section
2295 EmitDebugInfo();
2296
2297 // Corresponding abbreviations into a abbrev section.
2298 EmitAbbreviations();
2299
2300 // Emit source line correspondence into a debug line section.
2301 EmitDebugLines();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002302
2303 // Emit info into a debug frame section.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002304 EmitDebugFrame();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002305
2306 // Emit info into a debug pubnames section.
2307 EmitDebugPubNames();
2308
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002309 // Emit info into a debug str section.
2310 EmitDebugStr();
2311
2312 // Emit info into a debug loc section.
2313 EmitDebugLoc();
2314
2315 // Emit info into a debug aranges section.
2316 EmitDebugARanges();
2317
2318 // Emit info into a debug ranges section.
2319 EmitDebugRanges();
2320
2321 // Emit info into a debug macinfo section.
2322 EmitDebugMacInfo();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002323}
2324
Jim Laskeye719a7c2006-01-18 16:54:26 +00002325/// BeginFunction - Gather pre-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002326///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002327void DwarfWriter::BeginFunction(MachineFunction *MF) {
2328 this->MF = MF;
2329
Jim Laskeyb2efb852006-01-04 22:28:25 +00002330 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002331 EOL("Dwarf Begin Function");
Jim Laskeyb8509c52006-03-23 18:07:55 +00002332
2333 // Define begin label for subprogram.
2334 Asm->SwitchSection(TextSection, 0);
2335 EmitLabel("func_begin", ++SubprogramCount);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002336}
2337
Jim Laskeyb8509c52006-03-23 18:07:55 +00002338
Jim Laskeye719a7c2006-01-18 16:54:26 +00002339/// EndFunction - Gather and emit post-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002340///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002341void DwarfWriter::EndFunction() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002342 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002343 EOL("Dwarf End Function");
Jim Laskeyb8509c52006-03-23 18:07:55 +00002344
2345 // Define end label for subprogram.
2346 Asm->SwitchSection(TextSection, 0);
2347 EmitLabel("func_end", SubprogramCount);
2348
2349 // Construct scopes for subprogram.
2350 ConstructRootScope(DebugInfo->getRootScope());
2351 DebugInfo->ClearScopes();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002352}