blob: 99beaf35510681dde081043e36311e647465238d [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 Laskeyb3e789a2006-01-26 20:21:46 +000021#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000022#include "llvm/Support/CommandLine.h"
Jim Laskey52060a02006-01-24 00:49:18 +000023#include "llvm/Support/Mangler.h"
24#include "llvm/Target/TargetMachine.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000025
Jim Laskeyb2efb852006-01-04 22:28:25 +000026#include <iostream>
Jim Laskeya7cea6f2006-01-04 13:52:30 +000027
Jim Laskeyb2efb852006-01-04 22:28:25 +000028using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000029using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000030
31static cl::opt<bool>
32DwarfVerbose("dwarf-verbose", cl::Hidden,
Jim Laskey063e7652006-01-17 17:31:53 +000033 cl::desc("Add comments to Dwarf directives."));
34
Jim Laskey0d086af2006-02-27 12:43:29 +000035namespace llvm {
36
Jim Laskey063e7652006-01-17 17:31:53 +000037//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000038// Forward declarations.
39//
40class CompileUnit;
41class DIE;
Jim Laskey063e7652006-01-17 17:31:53 +000042
Jim Laskey0d086af2006-02-27 12:43:29 +000043//===----------------------------------------------------------------------===//
Jim Laskeyf01e5472006-03-03 15:06:57 +000044// CompileUnit - This dwarf writer support class manages information associate
45// with a source file.
Jim Laskeybd761842006-02-27 17:27:12 +000046class CompileUnit {
47private:
48 CompileUnitDesc *Desc; // Compile unit debug descriptor.
49 unsigned ID; // File ID for source.
50 DIE *Die; // Compile unit die.
51 std::map<std::string, DIE *> Globals; // A map of globally visible named
52 // entities for this unit.
53
54public:
55 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
56 : Desc(CUD)
57 , ID(I)
58 , Die(D)
59 , Globals()
60 {}
61
62 ~CompileUnit();
63
64 // Accessors.
65 CompileUnitDesc *getDesc() const { return Desc; }
66 unsigned getID() const { return ID; }
67 DIE* getDie() const { return Die; }
68 std::map<std::string, DIE *> &getGlobals() { return Globals; }
69
70 /// hasContent - Return true if this compile unit has something to write out.
71 ///
72 bool hasContent() const;
73
74 /// AddGlobal - Add a new global entity to the compile unit.
75 ///
76 void AddGlobal(const std::string &Name, DIE *Die);
77
78};
79
80//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000081// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
82// Dwarf abbreviation.
83class DIEAbbrevData {
84private:
85 unsigned Attribute; // Dwarf attribute code.
86 unsigned Form; // Dwarf form code.
87
88public:
89 DIEAbbrevData(unsigned A, unsigned F)
90 : Attribute(A)
91 , Form(F)
92 {}
93
Jim Laskeybd761842006-02-27 17:27:12 +000094 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +000095 unsigned getAttribute() const { return Attribute; }
96 unsigned getForm() const { return Form; }
97
98 /// operator== - Used by DIEAbbrev to locate entry.
99 ///
100 bool operator==(const DIEAbbrevData &DAD) const {
101 return Attribute == DAD.Attribute && Form == DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000102 }
Jim Laskey063e7652006-01-17 17:31:53 +0000103
Jim Laskey0d086af2006-02-27 12:43:29 +0000104 /// operator!= - Used by DIEAbbrev to locate entry.
105 ///
106 bool operator!=(const DIEAbbrevData &DAD) const {
107 return Attribute != DAD.Attribute || Form != DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000108 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000109
110 /// operator< - Used by DIEAbbrev to locate entry.
111 ///
112 bool operator<(const DIEAbbrevData &DAD) const {
113 return Attribute < DAD.Attribute ||
114 (Attribute == DAD.Attribute && Form < DAD.Form);
115 }
116};
Jim Laskey063e7652006-01-17 17:31:53 +0000117
Jim Laskey0d086af2006-02-27 12:43:29 +0000118//===----------------------------------------------------------------------===//
119// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
120// information object.
121class DIEAbbrev {
122private:
123 unsigned Tag; // Dwarf tag code.
124 unsigned ChildrenFlag; // Dwarf children flag.
125 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
Jim Laskey063e7652006-01-17 17:31:53 +0000126
Jim Laskey0d086af2006-02-27 12:43:29 +0000127public:
Jim Laskey063e7652006-01-17 17:31:53 +0000128
Jim Laskey0d086af2006-02-27 12:43:29 +0000129 DIEAbbrev(unsigned T, unsigned C)
130 : Tag(T)
131 , ChildrenFlag(C)
132 , Data()
133 {}
134 ~DIEAbbrev() {}
135
Jim Laskeybd761842006-02-27 17:27:12 +0000136 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000137 unsigned getTag() const { return Tag; }
138 unsigned getChildrenFlag() const { return ChildrenFlag; }
139 const std::vector<DIEAbbrevData> &getData() const { return Data; }
140 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskey063e7652006-01-17 17:31:53 +0000141
Jim Laskey0d086af2006-02-27 12:43:29 +0000142 /// operator== - Used by UniqueVector to locate entry.
143 ///
144 bool operator==(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000145
Jim Laskey0d086af2006-02-27 12:43:29 +0000146 /// operator< - Used by UniqueVector to locate entry.
147 ///
148 bool operator<(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000149
Jim Laskey0d086af2006-02-27 12:43:29 +0000150 /// AddAttribute - Adds another set of attribute information to the
151 /// abbreviation.
152 void AddAttribute(unsigned Attribute, unsigned Form) {
153 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000154 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000155
156 /// Emit - Print the abbreviation using the specified Dwarf writer.
157 ///
158 void Emit(const DwarfWriter &DW) const;
159
160#ifndef NDEBUG
161 void print(std::ostream &O);
162 void dump();
163#endif
164};
Jim Laskey063e7652006-01-17 17:31:53 +0000165
Jim Laskey0d086af2006-02-27 12:43:29 +0000166//===----------------------------------------------------------------------===//
167// DIEValue - A debug information entry value.
168//
169class DIEValue {
170public:
171 enum {
172 isInteger,
173 isString,
174 isLabel,
175 isAsIsLabel,
176 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000177 isEntry,
178 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000179 };
180
181 unsigned Type; // Type of the value
182
183 DIEValue(unsigned T) : Type(T) {}
184 virtual ~DIEValue() {}
185
186 // Implement isa/cast/dyncast.
187 static bool classof(const DIEValue *) { return true; }
188
189 /// EmitValue - Emit value via the Dwarf writer.
190 ///
191 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
192
193 /// SizeOf - Return the size of a value in bytes.
194 ///
195 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
196};
Jim Laskey063e7652006-01-17 17:31:53 +0000197
Jim Laskey0d086af2006-02-27 12:43:29 +0000198//===----------------------------------------------------------------------===//
199// DWInteger - An integer value DIE.
200//
201class DIEInteger : public DIEValue {
202private:
203 uint64_t Integer;
204
205public:
206 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000207
Jim Laskey0d086af2006-02-27 12:43:29 +0000208 // Implement isa/cast/dyncast.
209 static bool classof(const DIEInteger *) { return true; }
210 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
211
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000212 /// BestForm - Choose the best form for integer.
213 ///
214 unsigned BestForm(bool IsSigned);
215
Jim Laskey0d086af2006-02-27 12:43:29 +0000216 /// EmitValue - Emit integer of appropriate size.
217 ///
218 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
219
220 /// SizeOf - Determine size of integer value in bytes.
221 ///
222 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
223};
Jim Laskey063e7652006-01-17 17:31:53 +0000224
Jim Laskey0d086af2006-02-27 12:43:29 +0000225//===----------------------------------------------------------------------===//
226// DIEString - A string value DIE.
227//
228struct DIEString : public DIEValue {
229 const std::string String;
230
231 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000232
Jim Laskey0d086af2006-02-27 12:43:29 +0000233 // Implement isa/cast/dyncast.
234 static bool classof(const DIEString *) { return true; }
235 static bool classof(const DIEValue *S) { return S->Type == isString; }
236
237 /// EmitValue - Emit string value.
238 ///
239 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
240
241 /// SizeOf - Determine size of string value in bytes.
242 ///
243 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
244};
Jim Laskey063e7652006-01-17 17:31:53 +0000245
Jim Laskey0d086af2006-02-27 12:43:29 +0000246//===----------------------------------------------------------------------===//
247// DIEDwarfLabel - A Dwarf internal label expression DIE.
248//
249struct DIEDwarfLabel : public DIEValue {
250 const DWLabel Label;
251
252 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000253
Jim Laskey0d086af2006-02-27 12:43:29 +0000254 // Implement isa/cast/dyncast.
255 static bool classof(const DIEDwarfLabel *) { return true; }
256 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
257
258 /// EmitValue - Emit label value.
259 ///
260 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
261
262 /// SizeOf - Determine size of label value in bytes.
263 ///
264 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
265};
Jim Laskey063e7652006-01-17 17:31:53 +0000266
Jim Laskey063e7652006-01-17 17:31:53 +0000267
Jim Laskey0d086af2006-02-27 12:43:29 +0000268//===----------------------------------------------------------------------===//
269// DIEObjectLabel - A label to an object in code or data.
270//
271struct DIEObjectLabel : public DIEValue {
272 const std::string Label;
273
274 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000275
Jim Laskey0d086af2006-02-27 12:43:29 +0000276 // Implement isa/cast/dyncast.
277 static bool classof(const DIEObjectLabel *) { return true; }
278 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
279
280 /// EmitValue - Emit label value.
281 ///
282 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
283
284 /// SizeOf - Determine size of label value in bytes.
285 ///
286 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
287};
Jim Laskey063e7652006-01-17 17:31:53 +0000288
Jim Laskey0d086af2006-02-27 12:43:29 +0000289//===----------------------------------------------------------------------===//
290// DIEDelta - A simple label difference DIE.
291//
292struct DIEDelta : public DIEValue {
293 const DWLabel LabelHi;
294 const DWLabel LabelLo;
295
296 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
297 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000298
Jim Laskey0d086af2006-02-27 12:43:29 +0000299 // Implement isa/cast/dyncast.
300 static bool classof(const DIEDelta *) { return true; }
301 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
302
303 /// EmitValue - Emit delta value.
304 ///
305 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
306
307 /// SizeOf - Determine size of delta value in bytes.
308 ///
309 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
310};
Jim Laskey063e7652006-01-17 17:31:53 +0000311
Jim Laskey0d086af2006-02-27 12:43:29 +0000312//===----------------------------------------------------------------------===//
313// DIEntry - A pointer to a debug information entry.
314//
315struct DIEntry : public DIEValue {
316 DIE *Entry;
317
318 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
319
320 // Implement isa/cast/dyncast.
321 static bool classof(const DIEntry *) { return true; }
322 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
323
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000324 /// EmitValue - Emit die entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000325 ///
326 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
327
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000328 /// SizeOf - Determine size of die entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000329 ///
330 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
331};
332
333//===----------------------------------------------------------------------===//
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000334// DIEBlock - A block of values. Primarily used for location expressions.
335//
336struct DIEBlock : public DIEValue {
337 unsigned Size; // Size in bytes excluding size header.
338 std::vector<unsigned> Forms; // Data forms.
339 std::vector<DIEValue *> Values; // Block values.
340
341 DIEBlock()
342 : DIEValue(isBlock)
343 , Size(0)
344 , Forms()
345 , Values()
346 {}
347 ~DIEBlock();
348
349 // Implement isa/cast/dyncast.
350 static bool classof(const DIEBlock *) { return true; }
351 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
352
353 /// ComputeSize - calculate the size of the block.
354 ///
355 unsigned ComputeSize(DwarfWriter &DW);
356
357 /// BestForm - Choose the best form for data.
358 ///
359 unsigned BestForm();
360
361 /// EmitValue - Emit block data.
362 ///
363 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
364
365 /// SizeOf - Determine size of block data in bytes.
366 ///
367 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
368
369 /// AddUInt - Add an unsigned integer value.
370 ///
371 void AddUInt(unsigned Form, uint64_t Integer);
372
373 /// AddSInt - Add an signed integer value.
374 ///
375 void AddSInt(unsigned Form, int64_t Integer);
376
377 /// AddString - Add a std::string value.
378 ///
379 void AddString(unsigned Form, const std::string &String);
380
381 /// AddLabel - Add a Dwarf label value.
382 ///
383 void AddLabel(unsigned Form, const DWLabel &Label);
384
385 /// AddObjectLabel - Add a non-Dwarf label value.
386 ///
387 void AddObjectLabel(unsigned Form, const std::string &Label);
388
389 /// AddDelta - Add a label delta value.
390 ///
391 void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
392
393 /// AddDIEntry - Add a DIE value.
394 ///
395 void AddDIEntry(unsigned Form, DIE *Entry);
396
397};
398
399//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +0000400// DIE - A structured debug information entry. Has an abbreviation which
401// describes it's organization.
402class DIE {
403private:
404 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
405 unsigned AbbrevID; // Decribing abbreviation ID.
406 unsigned Offset; // Offset in debug info section.
407 unsigned Size; // Size of instance + children.
408 std::vector<DIE *> Children; // Children DIEs.
409 std::vector<DIEValue *> Values; // Attributes values.
410
411public:
412 DIE(unsigned Tag);
413 ~DIE();
414
Jim Laskeybd761842006-02-27 17:27:12 +0000415 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000416 unsigned getAbbrevID() const { return AbbrevID; }
417 unsigned getOffset() const { return Offset; }
418 unsigned getSize() const { return Size; }
419 const std::vector<DIE *> &getChildren() const { return Children; }
420 const std::vector<DIEValue *> &getValues() const { return Values; }
421 void setOffset(unsigned O) { Offset = O; }
422 void setSize(unsigned S) { Size = S; }
423
424 /// SiblingOffset - Return the offset of the debug information entry's
425 /// sibling.
426 unsigned SiblingOffset() const { return Offset + Size; }
427
428 /// AddUInt - Add an unsigned integer attribute data and value.
429 ///
430 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
431
432 /// AddSInt - Add an signed integer attribute data and value.
433 ///
434 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
435
436 /// AddString - Add a std::string attribute data and value.
437 ///
438 void AddString(unsigned Attribute, unsigned Form,
439 const std::string &String);
440
441 /// AddLabel - Add a Dwarf label attribute data and value.
442 ///
443 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
444
445 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
446 ///
447 void AddObjectLabel(unsigned Attribute, unsigned Form,
448 const std::string &Label);
449
450 /// AddDelta - Add a label delta attribute data and value.
451 ///
452 void AddDelta(unsigned Attribute, unsigned Form,
453 const DWLabel &Hi, const DWLabel &Lo);
454
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000455 /// AddDIEntry - Add a DIE attribute data and value.
Jim Laskey0d086af2006-02-27 12:43:29 +0000456 ///
457 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
458
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000459 /// AddBlock - Add block data.
460 ///
461 void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
462
Jim Laskey0d086af2006-02-27 12:43:29 +0000463 /// Complete - Indicate that all attributes have been added and
464 /// ready to get an abbreviation ID.
465 ///
466 void Complete(DwarfWriter &DW);
467
468 /// AddChild - Add a child to the DIE.
469 void AddChild(DIE *Child);
470};
471
472} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +0000473
474//===----------------------------------------------------------------------===//
475
Jim Laskeybd761842006-02-27 17:27:12 +0000476CompileUnit::~CompileUnit() {
477 delete Die;
478}
479
480/// hasContent - Return true if this compile unit has something to write out.
481///
482bool CompileUnit::hasContent() const {
483 return !Die->getChildren().empty();
484}
485
486/// AddGlobal - Add a new global entity to the compile unit.
487///
488void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
489 Globals[Name] = Die;
490}
491
492//===----------------------------------------------------------------------===//
493
Jim Laskeyd18e2892006-01-20 20:34:06 +0000494/// operator== - Used by UniqueVector to locate entry.
495///
496bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
497 if (Tag != DA.Tag) return false;
498 if (ChildrenFlag != DA.ChildrenFlag) return false;
499 if (Data.size() != DA.Data.size()) return false;
500
Jim Laskey52060a02006-01-24 00:49:18 +0000501 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000502 if (Data[i] != DA.Data[i]) return false;
Jim Laskeyd18e2892006-01-20 20:34:06 +0000503 }
504
505 return true;
506}
507
508/// operator< - Used by UniqueVector to locate entry.
509///
510bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
511 if (Tag != DA.Tag) return Tag < DA.Tag;
512 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
513 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
514
Jim Laskey52060a02006-01-24 00:49:18 +0000515 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000516 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +0000517 }
518
519 return false;
520}
521
522/// Emit - Print the abbreviation using the specified Dwarf writer.
523///
524void DIEAbbrev::Emit(const DwarfWriter &DW) const {
525 // Emit its Dwarf tag type.
526 DW.EmitULEB128Bytes(Tag);
527 DW.EOL(TagString(Tag));
528
529 // Emit whether it has children DIEs.
530 DW.EmitULEB128Bytes(ChildrenFlag);
531 DW.EOL(ChildrenString(ChildrenFlag));
532
533 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +0000534 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000535 const DIEAbbrevData &AttrData = Data[i];
536
537 // Emit attribute type.
538 DW.EmitULEB128Bytes(AttrData.getAttribute());
539 DW.EOL(AttributeString(AttrData.getAttribute()));
540
541 // Emit form type.
542 DW.EmitULEB128Bytes(AttrData.getForm());
543 DW.EOL(FormEncodingString(AttrData.getForm()));
544 }
545
546 // Mark end of abbreviation.
547 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
548 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
549}
550
551#ifndef NDEBUG
552 void DIEAbbrev::print(std::ostream &O) {
553 O << "Abbreviation @"
Jeff Cohen05ebc8d2006-01-25 17:18:50 +0000554 << std::hex << (intptr_t)this << std::dec
Jim Laskeyd18e2892006-01-20 20:34:06 +0000555 << " "
556 << TagString(Tag)
557 << " "
558 << ChildrenString(ChildrenFlag)
559 << "\n";
560
Jim Laskey52060a02006-01-24 00:49:18 +0000561 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000562 O << " "
563 << AttributeString(Data[i].getAttribute())
564 << " "
565 << FormEncodingString(Data[i].getForm())
566 << "\n";
567 }
568 }
569 void DIEAbbrev::dump() { print(std::cerr); }
570#endif
571
572//===----------------------------------------------------------------------===//
573
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000574/// BestForm - Choose the best form for integer.
575///
576unsigned DIEInteger::BestForm(bool IsSigned) {
577 if (IsSigned) {
578 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
579 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
580 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
581 } else {
582 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
583 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
584 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
585 }
586 return DW_FORM_data8;
587}
588
Jim Laskey063e7652006-01-17 17:31:53 +0000589/// EmitValue - Emit integer of appropriate size.
590///
591void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
592 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +0000593 case DW_FORM_flag: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000594 case DW_FORM_data1: DW.EmitInt8(Integer); break;
595 case DW_FORM_data2: DW.EmitInt16(Integer); break;
596 case DW_FORM_data4: DW.EmitInt32(Integer); break;
597 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +0000598 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
599 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +0000600 default: assert(0 && "DIE Value form not supported yet"); break;
601 }
602}
603
604/// SizeOf - Determine size of integer value in bytes.
605///
606unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
607 switch (Form) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000608 case DW_FORM_flag: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000609 case DW_FORM_data1: return sizeof(int8_t);
610 case DW_FORM_data2: return sizeof(int16_t);
611 case DW_FORM_data4: return sizeof(int32_t);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000612 case DW_FORM_data8: return sizeof(int64_t);
Jim Laskey40020172006-01-20 21:02:36 +0000613 case DW_FORM_udata: return DW.SizeULEB128(Integer);
614 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +0000615 default: assert(0 && "DIE Value form not supported yet"); break;
616 }
617 return 0;
618}
619
620//===----------------------------------------------------------------------===//
621
622/// EmitValue - Emit string value.
623///
624void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000625 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +0000626}
627
628/// SizeOf - Determine size of string value in bytes.
629///
630unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey40020172006-01-20 21:02:36 +0000631 return String.size() + sizeof(char); // sizeof('\0');
Jim Laskey063e7652006-01-17 17:31:53 +0000632}
633
634//===----------------------------------------------------------------------===//
635
636/// EmitValue - Emit label value.
637///
Jim Laskey52060a02006-01-24 00:49:18 +0000638void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000639 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +0000640}
641
642/// SizeOf - Determine size of label value in bytes.
643///
Jim Laskey52060a02006-01-24 00:49:18 +0000644unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +0000645 return DW.getAddressSize();
646}
647
648//===----------------------------------------------------------------------===//
649
Jim Laskeyd18e2892006-01-20 20:34:06 +0000650/// EmitValue - Emit label value.
651///
Jim Laskey52060a02006-01-24 00:49:18 +0000652void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000653 DW.EmitReference(Label);
654}
655
656/// SizeOf - Determine size of label value in bytes.
657///
Jim Laskey52060a02006-01-24 00:49:18 +0000658unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000659 return DW.getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +0000660}
661
662//===----------------------------------------------------------------------===//
663
Jim Laskey063e7652006-01-17 17:31:53 +0000664/// EmitValue - Emit delta value.
665///
666void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000667 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +0000668}
669
670/// SizeOf - Determine size of delta value in bytes.
671///
672unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
673 return DW.getAddressSize();
674}
675
676//===----------------------------------------------------------------------===//
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000677/// EmitValue - Emit die entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000678///
679void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +0000680 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +0000681}
682
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000683/// SizeOf - Determine size of die value in bytes.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000684///
685unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
686 return sizeof(int32_t);
687}
688
689//===----------------------------------------------------------------------===//
690
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000691DIEBlock::~DIEBlock() {
692 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
693 delete Values[i];
694 }
695}
696
697/// ComputeSize - calculate the size of the block.
698///
699unsigned DIEBlock::ComputeSize(DwarfWriter &DW) {
700 Size = 0;
701 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
702 Size += Values[i]->SizeOf(DW, Forms[i]);
703 }
704 return Size;
705}
706
707/// BestForm - Choose the best form for data.
708///
709unsigned DIEBlock::BestForm() {
710 if ((unsigned char)Size == Size) return DW_FORM_block1;
711 if ((unsigned short)Size == Size) return DW_FORM_block2;
712 if ((unsigned int)Size == Size) return DW_FORM_block4;
713 return DW_FORM_block;
714}
715
716/// EmitValue - Emit block data.
717///
718void DIEBlock::EmitValue(const DwarfWriter &DW, unsigned Form) const {
719 switch (Form) {
720 case DW_FORM_block1: DW.EmitInt8(Size); break;
721 case DW_FORM_block2: DW.EmitInt16(Size); break;
722 case DW_FORM_block4: DW.EmitInt32(Size); break;
723 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
724 default: assert(0 && "Improper form for block"); break;
725 }
726 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
727 DW.EOL("");
728 Values[i]->EmitValue(DW, Forms[i]);
729 }
730}
731
732/// SizeOf - Determine size of block data in bytes.
733///
734unsigned DIEBlock::SizeOf(const DwarfWriter &DW, unsigned Form) const {
735 switch (Form) {
736 case DW_FORM_block1: return Size + sizeof(int8_t);
737 case DW_FORM_block2: return Size + sizeof(int16_t);
738 case DW_FORM_block4: return Size + sizeof(int32_t);
739 case DW_FORM_block: return Size + DW.SizeULEB128(Size);
740 default: assert(0 && "Improper form for block"); break;
741 }
742 return 0;
743}
744
745/// AddUInt - Add an unsigned integer value.
746///
747void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
748 DIEInteger *DI = new DIEInteger(Integer);
749 Values.push_back(DI);
750 if (Form == 0) Form = DI->BestForm(false);
751 Forms.push_back(Form);
752}
753
754/// AddSInt - Add an signed integer value.
755///
756void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
757 DIEInteger *DI = new DIEInteger(Integer);
758 Values.push_back(DI);
759 if (Form == 0) Form = DI->BestForm(true);
760 Forms.push_back(Form);
761}
762
763/// AddString - Add a std::string value.
764///
765void DIEBlock::AddString(unsigned Form, const std::string &String) {
766 Values.push_back(new DIEString(String));
767 Forms.push_back(Form);
768}
769
770/// AddLabel - Add a Dwarf label value.
771///
772void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
773 Values.push_back(new DIEDwarfLabel(Label));
774 Forms.push_back(Form);
775}
776
777/// AddObjectLabel - Add a non-Dwarf label value.
778///
779void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
780 Values.push_back(new DIEObjectLabel(Label));
781 Forms.push_back(Form);
782}
783
784/// AddDelta - Add a label delta value.
785///
786void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
787 Values.push_back(new DIEDelta(Hi, Lo));
788 Forms.push_back(Form);
789}
790
791/// AddDIEntry - Add a DIE value.
792///
793void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
794 Values.push_back(new DIEntry(Entry));
795 Forms.push_back(Form);
796}
797
798//===----------------------------------------------------------------------===//
799
Jim Laskey0420f2a2006-02-22 19:02:11 +0000800DIE::DIE(unsigned Tag)
801: Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
Jim Laskeyd18e2892006-01-20 20:34:06 +0000802, AbbrevID(0)
803, Offset(0)
804, Size(0)
Jim Laskeyd18e2892006-01-20 20:34:06 +0000805, Children()
806, Values()
807{}
808
809DIE::~DIE() {
810 if (Abbrev) delete Abbrev;
811
Jim Laskey52060a02006-01-24 00:49:18 +0000812 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000813 delete Children[i];
814 }
815
Jim Laskey52060a02006-01-24 00:49:18 +0000816 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000817 delete Values[j];
818 }
Jim Laskeyd18e2892006-01-20 20:34:06 +0000819}
820
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000821/// AddUInt - Add an unsigned integer attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000822///
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000823void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000824 DIEInteger *DI = new DIEInteger(Integer);
825 Values.push_back(DI);
826 if (!Form) Form = DI->BestForm(false);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000827 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000828}
829
830/// AddSInt - Add an signed integer attribute data and value.
831///
832void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000833 DIEInteger *DI = new DIEInteger(Integer);
834 Values.push_back(DI);
835 if (!Form) Form = DI->BestForm(true);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000836 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000837}
838
839/// AddString - Add a std::string attribute data and value.
840///
841void DIE::AddString(unsigned Attribute, unsigned Form,
842 const std::string &String) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000843 Values.push_back(new DIEString(String));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000844 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000845}
846
847/// AddLabel - Add a Dwarf label attribute data and value.
848///
849void DIE::AddLabel(unsigned Attribute, unsigned Form,
850 const DWLabel &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000851 Values.push_back(new DIEDwarfLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000852 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000853}
854
Jim Laskey52060a02006-01-24 00:49:18 +0000855/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000856///
Jim Laskey52060a02006-01-24 00:49:18 +0000857void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
858 const std::string &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000859 Values.push_back(new DIEObjectLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000860 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000861}
862
863/// AddDelta - Add a label delta attribute data and value.
864///
865void DIE::AddDelta(unsigned Attribute, unsigned Form,
866 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000867 Values.push_back(new DIEDelta(Hi, Lo));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000868 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000869}
870
871/// AddDIEntry - Add a DIE attribute data and value.
872///
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000873void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000874 Values.push_back(new DIEntry(Entry));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000875 Abbrev->AddAttribute(Attribute, Form);
876}
877
878/// AddBlock - Add block data.
879///
880void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
881 assert(Block->Size && "Block size has not been computed");
882 Values.push_back(Block);
883 if (!Form) Form = Block->BestForm();
884 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000885}
886
887/// Complete - Indicate that all attributes have been added and ready to get an
888/// abbreviation ID.
889void DIE::Complete(DwarfWriter &DW) {
890 AbbrevID = DW.NewAbbreviation(Abbrev);
891 delete Abbrev;
892 Abbrev = NULL;
893}
894
895/// AddChild - Add a child to the DIE.
896///
897void DIE::AddChild(DIE *Child) {
Jim Laskey0420f2a2006-02-22 19:02:11 +0000898 assert(Abbrev && "Adding children without an abbreviation");
899 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000900 Children.push_back(Child);
901}
902
903//===----------------------------------------------------------------------===//
904
Jim Laskey0420f2a2006-02-22 19:02:11 +0000905/// DWContext
Jim Laskeyd18e2892006-01-20 20:34:06 +0000906
907//===----------------------------------------------------------------------===//
Jim Laskey063e7652006-01-17 17:31:53 +0000908
909/// PrintHex - Print a value as a hexidecimal value.
910///
911void DwarfWriter::PrintHex(int Value) const {
912 O << "0x" << std::hex << Value << std::dec;
913}
914
915/// EOL - Print a newline character to asm stream. If a comment is present
916/// then it will be printed first. Comments should not contain '\n'.
917void DwarfWriter::EOL(const std::string &Comment) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000918 if (DwarfVerbose && !Comment.empty()) {
Jim Laskey063e7652006-01-17 17:31:53 +0000919 O << "\t"
920 << Asm->CommentString
921 << " "
922 << Comment;
923 }
924 O << "\n";
925}
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000926
927/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
Jim Laskey063e7652006-01-17 17:31:53 +0000928/// unsigned leb128 value.
929void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000930 if (hasLEB128) {
931 O << "\t.uleb128\t"
932 << Value;
933 } else {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000934 O << Asm->Data8bitsDirective;
Jim Laskey063e7652006-01-17 17:31:53 +0000935 PrintULEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000936 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000937}
938
939/// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
Jim Laskey063e7652006-01-17 17:31:53 +0000940/// signed leb128 value.
941void DwarfWriter::EmitSLEB128Bytes(int Value) const {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000942 if (hasLEB128) {
943 O << "\t.sleb128\t"
944 << Value;
945 } else {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000946 O << Asm->Data8bitsDirective;
Jim Laskey063e7652006-01-17 17:31:53 +0000947 PrintSLEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000948 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000949}
950
Jim Laskey063e7652006-01-17 17:31:53 +0000951/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +0000952/// representing an unsigned leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +0000953void DwarfWriter::PrintULEB128(unsigned Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000954 do {
955 unsigned Byte = Value & 0x7f;
956 Value >>= 7;
957 if (Value) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +0000958 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000959 if (Value) O << ", ";
960 } while (Value);
961}
962
Jim Laskey063e7652006-01-17 17:31:53 +0000963/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
964/// value.
965unsigned DwarfWriter::SizeULEB128(unsigned Value) {
966 unsigned Size = 0;
967 do {
968 Value >>= 7;
969 Size += sizeof(int8_t);
970 } while (Value);
971 return Size;
972}
973
974/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +0000975/// representing a signed leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +0000976void DwarfWriter::PrintSLEB128(int Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000977 int Sign = Value >> (8 * sizeof(Value) - 1);
978 bool IsMore;
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000979
Jim Laskeyb2efb852006-01-04 22:28:25 +0000980 do {
981 unsigned Byte = Value & 0x7f;
982 Value >>= 7;
983 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
984 if (IsMore) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +0000985 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000986 if (IsMore) O << ", ";
987 } while (IsMore);
988}
989
Jim Laskey063e7652006-01-17 17:31:53 +0000990/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
991/// value.
992unsigned DwarfWriter::SizeSLEB128(int Value) {
993 unsigned Size = 0;
994 int Sign = Value >> (8 * sizeof(Value) - 1);
995 bool IsMore;
996
997 do {
998 unsigned Byte = Value & 0x7f;
999 Value >>= 7;
1000 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1001 Size += sizeof(int8_t);
1002 } while (IsMore);
1003 return Size;
1004}
1005
Jim Laskeyda427fa2006-01-27 20:31:25 +00001006/// EmitInt8 - Emit a byte directive and value.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001007///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001008void DwarfWriter::EmitInt8(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001009 O << Asm->Data8bitsDirective;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001010 PrintHex(Value & 0xFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001011}
1012
Jim Laskeyda427fa2006-01-27 20:31:25 +00001013/// EmitInt16 - Emit a short directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001014///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001015void DwarfWriter::EmitInt16(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001016 O << Asm->Data16bitsDirective;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001017 PrintHex(Value & 0xFFFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001018}
1019
Jim Laskeyda427fa2006-01-27 20:31:25 +00001020/// EmitInt32 - Emit a long directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001021///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001022void DwarfWriter::EmitInt32(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001023 O << Asm->Data32bitsDirective;
1024 PrintHex(Value);
1025}
1026
Jim Laskeyda427fa2006-01-27 20:31:25 +00001027/// EmitInt64 - Emit a long long directive and value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001028///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001029void DwarfWriter::EmitInt64(uint64_t Value) const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001030 if (Asm->Data64bitsDirective) {
1031 O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
1032 } else {
1033 const TargetData &TD = Asm->TM.getTargetData();
1034
1035 if (TD.isBigEndian()) {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001036 EmitInt32(unsigned(Value >> 32)); O << "\n";
1037 EmitInt32(unsigned(Value));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001038 } else {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001039 EmitInt32(unsigned(Value)); O << "\n";
1040 EmitInt32(unsigned(Value >> 32));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001041 }
1042 }
1043}
1044
Jim Laskey063e7652006-01-17 17:31:53 +00001045/// EmitString - Emit a string with quotes and a null terminator.
1046/// Special characters are emitted properly. (Eg. '\t')
1047void DwarfWriter::EmitString(const std::string &String) const {
1048 O << Asm->AsciiDirective
1049 << "\"";
Jim Laskey52060a02006-01-24 00:49:18 +00001050 for (unsigned i = 0, N = String.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001051 unsigned char C = String[i];
1052
1053 if (!isascii(C) || iscntrl(C)) {
1054 switch(C) {
1055 case '\b': O << "\\b"; break;
1056 case '\f': O << "\\f"; break;
1057 case '\n': O << "\\n"; break;
1058 case '\r': O << "\\r"; break;
1059 case '\t': O << "\\t"; break;
1060 default:
1061 O << '\\';
1062 O << char('0' + (C >> 6));
1063 O << char('0' + (C >> 3));
1064 O << char('0' + (C >> 0));
1065 break;
1066 }
1067 } else if (C == '\"') {
1068 O << "\\\"";
1069 } else if (C == '\'') {
1070 O << "\\\'";
1071 } else {
1072 O << C;
1073 }
1074 }
1075 O << "\\0\"";
1076}
1077
1078/// PrintLabelName - Print label name in form used by Dwarf writer.
1079///
1080void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001081 O << Asm->PrivateGlobalPrefix
1082 << "debug_"
Jim Laskey0420f2a2006-02-22 19:02:11 +00001083 << Tag;
1084 if (Number) O << Number;
Jim Laskeyb2efb852006-01-04 22:28:25 +00001085}
1086
Jim Laskey063e7652006-01-17 17:31:53 +00001087/// EmitLabel - Emit location label for internal use by Dwarf.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001088///
Jim Laskey063e7652006-01-17 17:31:53 +00001089void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
1090 PrintLabelName(Tag, Number);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001091 O << ":\n";
1092}
1093
Jim Laskeye719a7c2006-01-18 16:54:26 +00001094/// EmitReference - Emit a reference to a label.
Jim Laskey063e7652006-01-17 17:31:53 +00001095///
Jim Laskeye719a7c2006-01-18 16:54:26 +00001096void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001097 if (AddressSize == 4)
1098 O << Asm->Data32bitsDirective;
1099 else
1100 O << Asm->Data64bitsDirective;
1101
1102 PrintLabelName(Tag, Number);
1103}
Jim Laskey73683212006-01-21 00:59:54 +00001104void DwarfWriter::EmitReference(const std::string &Name) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001105 if (AddressSize == 4)
1106 O << Asm->Data32bitsDirective;
1107 else
1108 O << Asm->Data64bitsDirective;
1109
1110 O << Name;
1111}
Jim Laskey063e7652006-01-17 17:31:53 +00001112
1113/// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
1114/// assemblers do not accept absolute expressions with data directives, so there
1115/// is an option (needsSet) to use an intermediary 'set' expression.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001116void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
1117 const char *TagLo, unsigned NumberLo) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001118 if (needsSet) {
1119 static unsigned SetCounter = 0;
Jim Laskeyd18e2892006-01-20 20:34:06 +00001120
Jim Laskey063e7652006-01-17 17:31:53 +00001121 O << "\t.set\t";
1122 PrintLabelName("set", SetCounter);
1123 O << ",";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001124 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001125 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001126 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001127 O << "\n";
1128
1129 if (AddressSize == sizeof(int32_t))
1130 O << Asm->Data32bitsDirective;
1131 else
1132 O << Asm->Data64bitsDirective;
1133
1134 PrintLabelName("set", SetCounter);
1135
Jim Laskey52060a02006-01-24 00:49:18 +00001136 ++SetCounter;
Jim Laskey063e7652006-01-17 17:31:53 +00001137 } else {
1138 if (AddressSize == sizeof(int32_t))
1139 O << Asm->Data32bitsDirective;
1140 else
1141 O << Asm->Data64bitsDirective;
1142
Jim Laskeyd18e2892006-01-20 20:34:06 +00001143 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001144 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001145 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001146 }
1147}
1148
Jim Laskeyd18e2892006-01-20 20:34:06 +00001149/// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
Jim Laskey063e7652006-01-17 17:31:53 +00001150///
Jim Laskeyd18e2892006-01-20 20:34:06 +00001151unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
1152 return Abbreviations.insert(*Abbrev);
1153}
1154
1155/// NewString - Add a string to the constant pool and returns a label.
1156///
1157DWLabel DwarfWriter::NewString(const std::string &String) {
1158 unsigned StringID = StringPool.insert(String);
1159 return DWLabel("string", StringID);
1160}
1161
Jim Laskey0420f2a2006-02-22 19:02:11 +00001162/// NewBasicType - Creates a new basic type if necessary, then adds to the
1163/// owner.
1164/// FIXME - Should never be needed.
Jim Laskey92ae7402006-03-01 18:20:30 +00001165DIE *DwarfWriter::NewBasicType(DIE *Context, Type *Ty) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001166 DIE *&Slot = TypeToDieMap[Ty];
1167 if (Slot) return Slot;
1168
1169 const char *Name;
1170 unsigned Size;
1171 unsigned Encoding = 0;
1172
1173 switch (Ty->getTypeID()) {
1174 case Type::UByteTyID:
1175 Name = "unsigned char";
1176 Size = 1;
1177 Encoding = DW_ATE_unsigned_char;
1178 break;
1179 case Type::SByteTyID:
1180 Name = "char";
1181 Size = 1;
1182 Encoding = DW_ATE_signed_char;
1183 break;
1184 case Type::UShortTyID:
1185 Name = "unsigned short";
1186 Size = 2;
1187 Encoding = DW_ATE_unsigned;
1188 break;
1189 case Type::ShortTyID:
1190 Name = "short";
1191 Size = 2;
1192 Encoding = DW_ATE_signed;
1193 break;
1194 case Type::UIntTyID:
1195 Name = "unsigned int";
1196 Size = 4;
1197 Encoding = DW_ATE_unsigned;
1198 break;
1199 case Type::IntTyID:
1200 Name = "int";
1201 Size = 4;
1202 Encoding = DW_ATE_signed;
1203 break;
1204 case Type::ULongTyID:
1205 Name = "unsigned long long";
1206 Size = 7;
1207 Encoding = DW_ATE_unsigned;
1208 break;
1209 case Type::LongTyID:
1210 Name = "long long";
1211 Size = 7;
1212 Encoding = DW_ATE_signed;
1213 break;
1214 case Type::FloatTyID:
1215 Name = "float";
1216 Size = 4;
1217 Encoding = DW_ATE_float;
1218 break;
1219 case Type::DoubleTyID:
1220 Name = "double";
1221 Size = 8;
1222 Encoding = DW_ATE_float;
1223 break;
1224 default:
1225 // FIXME - handle more complex types.
1226 Name = "unknown";
1227 Size = 1;
1228 Encoding = DW_ATE_address;
1229 break;
1230 }
1231
1232 // construct the type DIE.
1233 Slot = new DIE(DW_TAG_base_type);
1234 Slot->AddString(DW_AT_name, DW_FORM_string, Name);
1235 Slot->AddUInt (DW_AT_byte_size, 0, Size);
1236 Slot->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
1237
Jim Laskey92ae7402006-03-01 18:20:30 +00001238 // Add to context.
1239 Context->AddChild(Slot);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001240
1241 return Slot;
1242}
1243
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001244/// NewType - Create a new type DIE.
1245///
Jim Laskey92ae7402006-03-01 18:20:30 +00001246DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc) {
Jim Laskey69906002006-02-24 16:46:40 +00001247 // FIXME - hack to get around NULL types short term.
Jim Laskey92ae7402006-03-01 18:20:30 +00001248 if (!TyDesc) return NewBasicType(Context, Type::IntTy);
1249
1250 // FIXME - Should handle other contexts that compile units.
Jim Laskey69906002006-02-24 16:46:40 +00001251
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001252 // Check for pre-existence.
1253 DIE *&Slot = DescToDieMap[TyDesc];
1254 if (Slot) return Slot;
1255
1256 // Get core information.
1257 const std::string &Name = TyDesc->getName();
Jim Laskey288fe0f2006-03-01 18:13:05 +00001258 uint64_t Size = TyDesc->getSize() >> 3;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001259
Jim Laskey434b40b2006-02-23 22:37:30 +00001260 DIE *Ty = NULL;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001261
Jim Laskey434b40b2006-02-23 22:37:30 +00001262 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001263 // Fundamental types like int, float, bool
Jim Laskey434b40b2006-02-23 22:37:30 +00001264 Slot = Ty = new DIE(DW_TAG_base_type);
1265 unsigned Encoding = BasicTy->getEncoding();
1266 Ty->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
Jim Laskey69906002006-02-24 16:46:40 +00001267 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001268 // Create specific DIE.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001269 Slot = Ty = new DIE(DerivedTy->getTag());
Jim Laskey69906002006-02-24 16:46:40 +00001270
1271 // Map to main type, void will not have a type.
1272 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
Jim Laskey92ae7402006-03-01 18:20:30 +00001273 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4, NewType(Context, FromTy));
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 Laskey92ae7402006-03-01 18:20:30 +00001284 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4, NewType(Context, FromTy));
Jim Laskeyf8913f12006-03-01 17:53:02 +00001285 }
1286 // Don't emit size attribute.
1287 Size = 0;
1288
1289 // Construct an anonymous type for index type.
1290 DIE *IndexTy = new DIE(DW_TAG_base_type);
1291 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1292 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1293 // Add to context.
Jim Laskey92ae7402006-03-01 18:20:30 +00001294 Context->AddChild(IndexTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001295
1296 // Add subranges to array type.
1297 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1298 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1299 int64_t Lo = SRD->getLo();
1300 int64_t Hi = SRD->getHi();
1301 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1302
1303 // If a range is available.
1304 if (Lo != Hi) {
1305 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1306 // Only add low if non-zero.
Jim Laskey6a3eb012006-03-01 23:52:37 +00001307 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1308 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001309 }
1310 Ty->AddChild(Subrange);
1311 }
1312
1313 break;
1314 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001315 case DW_TAG_structure_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +00001316 case DW_TAG_union_type: {
Jim Laskeyf01e5472006-03-03 15:06:57 +00001317 // FIXME - this is just the basics.
1318 // Add elements to structure type.
1319 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1320 DerivedTypeDesc *MemberDesc = cast<DerivedTypeDesc>(Elements[i]);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001321
1322 // Extract the basic information.
Jim Laskeyf01e5472006-03-03 15:06:57 +00001323 const std::string &Name = MemberDesc->getName();
1324 unsigned Line = MemberDesc->getLine();
1325 TypeDesc *MemTy = MemberDesc->getFromType();
1326 uint64_t Size = MemberDesc->getSize();
1327 uint64_t Offset = MemberDesc->getOffset();
1328
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001329 // Construct member die.
Jim Laskeyf01e5472006-03-03 15:06:57 +00001330 DIE *Member = new DIE(DW_TAG_member);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001331
1332 // Add details.
Jim Laskeyf01e5472006-03-03 15:06:57 +00001333 if (!Name.empty()) Member->AddString(DW_AT_name, DW_FORM_string, Name);
1334 if (CompileUnitDesc *File = MemberDesc->getFile()) {
1335 CompileUnit *FileUnit = FindCompileUnit(File);
1336 unsigned FileID = FileUnit->getID();
Jim Laskey20c3ed82006-03-07 15:51:33 +00001337 int Line = MemberDesc->getLine();
Jim Laskeyf01e5472006-03-03 15:06:57 +00001338 Member->AddUInt(DW_AT_decl_file, 0, FileID);
1339 Member->AddUInt(DW_AT_decl_line, 0, Line);
1340 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001341
Jim Laskey20c3ed82006-03-07 15:51:33 +00001342 // FIXME - Bitfields not quite right but getting there.
1343 uint64_t ByteSize = Size;
1344 uint64_t ByteOffset = Offset;
1345
Jim Laskeyf01e5472006-03-03 15:06:57 +00001346 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1347 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1348 NewType(Context, FromTy));
Jim Laskey20c3ed82006-03-07 15:51:33 +00001349 ByteSize = FromTy->getSize();
1350 }
1351
1352 if (ByteSize != Size) {
1353 ByteOffset -= Offset % ByteSize;
1354 Member->AddUInt(DW_AT_byte_size, 0, ByteSize >> 3);
1355 Member->AddUInt(DW_AT_bit_size, 0, Size % ByteSize);
1356 Member->AddUInt(DW_AT_bit_offset, 0, Offset - ByteOffset);
Jim Laskeyf01e5472006-03-03 15:06:57 +00001357 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001358
1359 // Add computation for offset.
1360 DIEBlock *Block = new DIEBlock();
1361 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
Jim Laskey20c3ed82006-03-07 15:51:33 +00001362 Block->AddUInt(DW_FORM_udata, ByteOffset >> 3);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001363 Block->ComputeSize(*this);
1364 Member->AddBlock(DW_AT_data_member_location, 0, Block);
1365
Jim Laskeyf01e5472006-03-03 15:06:57 +00001366 Ty->AddChild(Member);
1367 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001368 break;
1369 }
Jim Laskey9c4447a2006-03-01 20:39:36 +00001370 case DW_TAG_enumeration_type: {
Jim Laskey6a3eb012006-03-01 23:52:37 +00001371 // Add enumerators to enumeration type.
1372 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1373 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1374 const std::string &Name = ED->getName();
1375 int64_t Value = ED->getValue();
1376 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1377 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1378 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1379 Ty->AddChild(Enumerator);
1380 }
1381
Jim Laskeyf8913f12006-03-01 17:53:02 +00001382 break;
1383 }
1384 default: break;
1385 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001386 }
Jim Laskey434b40b2006-02-23 22:37:30 +00001387
1388 assert(Ty && "Type not supported yet");
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001389
Jim Laskey69906002006-02-24 16:46:40 +00001390 // Add size if non-zero (derived types don't have a size.)
Jim Laskey434b40b2006-02-23 22:37:30 +00001391 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
Jim Laskey69906002006-02-24 16:46:40 +00001392 // Add name if not anonymous or intermediate type.
Jim Laskey434b40b2006-02-23 22:37:30 +00001393 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskey69906002006-02-24 16:46:40 +00001394 // Add source line info if present.
1395 if (CompileUnitDesc *File = TyDesc->getFile()) {
Jim Laskeybd761842006-02-27 17:27:12 +00001396 CompileUnit *FileUnit = FindCompileUnit(File);
1397 unsigned FileID = FileUnit->getID();
Jim Laskey69906002006-02-24 16:46:40 +00001398 int Line = TyDesc->getLine();
1399 Ty->AddUInt(DW_AT_decl_file, 0, FileID);
1400 Ty->AddUInt(DW_AT_decl_line, 0, Line);
1401 }
Jim Laskey434b40b2006-02-23 22:37:30 +00001402
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001403 // Add to context owner.
Jim Laskey92ae7402006-03-01 18:20:30 +00001404 Context->AddChild(Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001405
1406 return Slot;
1407}
1408
Jim Laskeybd761842006-02-27 17:27:12 +00001409/// NewCompileUnit - Create new compile unit and it's die.
Jim Laskey063e7652006-01-17 17:31:53 +00001410///
Jim Laskeybd761842006-02-27 17:27:12 +00001411CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1412 unsigned ID) {
1413 // Construct debug information entry.
1414 DIE *Die = new DIE(DW_TAG_compile_unit);
1415 Die->AddLabel (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0));
1416 Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1417 Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
1418 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1419 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1420 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1421 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1422
1423 // Add die to descriptor map.
1424 DescToDieMap[UnitDesc] = Die;
1425
1426 // Construct compile unit.
1427 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1428
1429 // Add Unit to compile unit map.
1430 DescToUnitMap[UnitDesc] = Unit;
1431
1432 return Unit;
1433}
Jim Laskey0420f2a2006-02-22 19:02:11 +00001434
Jim Laskeybd761842006-02-27 17:27:12 +00001435/// FindCompileUnit - Get the compile unit for the given descriptor.
1436///
1437CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1438 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1439 assert(Unit && "Missing compile unit.");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001440 return Unit;
Jim Laskey063e7652006-01-17 17:31:53 +00001441}
1442
Jim Laskey0420f2a2006-02-22 19:02:11 +00001443/// NewGlobalVariable - Add a new global variable DIE.
1444///
1445DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
1446 // Check for pre-existence.
1447 DIE *&Slot = DescToDieMap[GVD];
1448 if (Slot) return Slot;
1449
1450 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001451 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1452 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001453 // Get the global variable itself.
1454 GlobalVariable *GV = GVD->getGlobalVariable();
1455 // Generate the mangled name.
1456 std::string MangledName = Asm->Mang->getValueName(GV);
1457
1458 // Gather the details (simplify add attribute code.)
1459 const std::string &Name = GVD->getName();
Jim Laskeybd761842006-02-27 17:27:12 +00001460 unsigned FileID = Unit->getID();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001461 unsigned Line = GVD->getLine();
1462
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001463 // Get the global's type.
Jim Laskey92ae7402006-03-01 18:20:30 +00001464 DIE *Type = NewType(Unit->getDie(), GVD->getTypeDesc());
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001465
1466 // Create the globale variable DIE.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001467 DIE *VariableDie = new DIE(DW_TAG_variable);
1468 VariableDie->AddString (DW_AT_name, DW_FORM_string, Name);
1469 VariableDie->AddUInt (DW_AT_decl_file, 0, FileID);
1470 VariableDie->AddUInt (DW_AT_decl_line, 0, Line);
1471 VariableDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1472 VariableDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001473
1474 DIEBlock *Block = new DIEBlock();
1475 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
1476 Block->AddObjectLabel(DW_FORM_udata, MangledName);
1477 Block->ComputeSize(*this);
1478 VariableDie->AddBlock(DW_AT_location, 0, Block);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001479
1480 // Add to map.
1481 Slot = VariableDie;
1482
1483 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001484 Unit->getDie()->AddChild(VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001485
1486 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001487 // FIXME - need to check external flag.
1488 Unit->AddGlobal(Name, VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001489
1490 return VariableDie;
1491}
1492
1493/// NewSubprogram - Add a new subprogram DIE.
1494///
1495DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
1496 // Check for pre-existence.
1497 DIE *&Slot = DescToDieMap[SPD];
1498 if (Slot) return Slot;
1499
1500 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001501 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1502 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001503
1504 // Gather the details (simplify add attribute code.)
1505 const std::string &Name = SPD->getName();
Jim Laskeybd761842006-02-27 17:27:12 +00001506 unsigned FileID = Unit->getID();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001507 // FIXME - faking the line for the time being.
1508 unsigned Line = 1;
1509
1510 // FIXME - faking the type for the time being.
Jim Laskey92ae7402006-03-01 18:20:30 +00001511 DIE *Type = NewBasicType(Unit->getDie(), Type::IntTy);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001512
Jim Laskey8a8e9752006-02-27 20:37:42 +00001513 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001514 SubprogramDie->AddString (DW_AT_name, DW_FORM_string, Name);
1515 SubprogramDie->AddUInt (DW_AT_decl_file, 0, FileID);
1516 SubprogramDie->AddUInt (DW_AT_decl_line, 0, Line);
1517 SubprogramDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1518 SubprogramDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
1519
1520 // Add to map.
1521 Slot = SubprogramDie;
1522
1523 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001524 Unit->getDie()->AddChild(SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001525
1526 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001527 Unit->AddGlobal(Name, SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001528
1529 return SubprogramDie;
1530}
1531
Jim Laskey063e7652006-01-17 17:31:53 +00001532/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1533/// tools to recognize the object file contains Dwarf information.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001534///
1535void DwarfWriter::EmitInitial() const {
Jim Laskey063e7652006-01-17 17:31:53 +00001536 // Dwarf sections base addresses.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001537 Asm->SwitchSection(DwarfFrameSection, 0);
1538 EmitLabel("section_frame", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001539 Asm->SwitchSection(DwarfInfoSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001540 EmitLabel("section_info", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001541 EmitLabel("info", 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001542 Asm->SwitchSection(DwarfAbbrevSection, 0);
1543 EmitLabel("section_abbrev", 0);
1544 EmitLabel("abbrev", 0);
1545 Asm->SwitchSection(DwarfARangesSection, 0);
1546 EmitLabel("section_aranges", 0);
1547 Asm->SwitchSection(DwarfMacInfoSection, 0);
1548 EmitLabel("section_macinfo", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001549 Asm->SwitchSection(DwarfLineSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001550 EmitLabel("section_line", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001551 EmitLabel("line", 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001552 Asm->SwitchSection(DwarfLocSection, 0);
1553 EmitLabel("section_loc", 0);
1554 Asm->SwitchSection(DwarfPubNamesSection, 0);
1555 EmitLabel("section_pubnames", 0);
1556 Asm->SwitchSection(DwarfStrSection, 0);
1557 EmitLabel("section_str", 0);
1558 Asm->SwitchSection(DwarfRangesSection, 0);
1559 EmitLabel("section_ranges", 0);
1560
Jim Laskey063e7652006-01-17 17:31:53 +00001561 Asm->SwitchSection(TextSection, 0);
1562 EmitLabel("text_begin", 0);
1563 Asm->SwitchSection(DataSection, 0);
1564 EmitLabel("data_begin", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001565}
1566
Jim Laskey063e7652006-01-17 17:31:53 +00001567/// EmitDIE - Recusively Emits a debug information entry.
1568///
1569void DwarfWriter::EmitDIE(DIE *Die) const {
1570 // Get the abbreviation for this DIE.
1571 unsigned AbbrevID = Die->getAbbrevID();
1572 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey0d086af2006-02-27 12:43:29 +00001573
1574 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001575
1576 // Emit the code (index) for the abbreviation.
1577 EmitULEB128Bytes(AbbrevID);
1578 EOL(std::string("Abbrev [" +
1579 utostr(AbbrevID) +
Jim Laskey0d086af2006-02-27 12:43:29 +00001580 "] 0x" + utohexstr(Die->getOffset()) +
1581 ":0x" + utohexstr(Die->getSize()) + " " +
1582 TagString(Abbrev.getTag())));
Jim Laskey063e7652006-01-17 17:31:53 +00001583
1584 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001585 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001586
1587 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001588 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001589 unsigned Attr = AbbrevData[i].getAttribute();
1590 unsigned Form = AbbrevData[i].getForm();
Jim Laskey063e7652006-01-17 17:31:53 +00001591 assert(Form && "Too many attributes for DIE (check abbreviation)");
1592
1593 switch (Attr) {
1594 case DW_AT_sibling: {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001595 EmitInt32(Die->SiblingOffset());
Jim Laskey063e7652006-01-17 17:31:53 +00001596 break;
1597 }
1598 default: {
1599 // Emit an attribute using the defined form.
1600 Values[i]->EmitValue(*this, Form);
1601 break;
1602 }
1603 }
1604
1605 EOL(AttributeString(Attr));
1606 }
1607
1608 // Emit the DIE children if any.
1609 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1610 const std::vector<DIE *> &Children = Die->getChildren();
1611
Jim Laskey52060a02006-01-24 00:49:18 +00001612 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskey063e7652006-01-17 17:31:53 +00001613 // FIXME - handle sibling offsets.
1614 // FIXME - handle all DIE types.
1615 EmitDIE(Children[j]);
1616 }
1617
Jim Laskeyda427fa2006-01-27 20:31:25 +00001618 EmitInt8(0); EOL("End Of Children Mark");
Jim Laskey063e7652006-01-17 17:31:53 +00001619 }
1620}
1621
1622/// SizeAndOffsetDie - Compute the size and offset of a DIE.
1623///
Jim Laskey0420f2a2006-02-22 19:02:11 +00001624unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset) {
1625 // Record the abbreviation.
1626 Die->Complete(*this);
1627
Jim Laskey063e7652006-01-17 17:31:53 +00001628 // Get the abbreviation for this DIE.
1629 unsigned AbbrevID = Die->getAbbrevID();
1630 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1631
1632 // Set DIE offset
1633 Die->setOffset(Offset);
1634
1635 // Start the size with the size of abbreviation code.
1636 Offset += SizeULEB128(AbbrevID);
1637
1638 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001639 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001640
1641 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001642 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001643 // Size attribute value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001644 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
Jim Laskey063e7652006-01-17 17:31:53 +00001645 }
1646
1647 // Emit the DIE children if any.
1648 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1649 const std::vector<DIE *> &Children = Die->getChildren();
1650
Jim Laskey52060a02006-01-24 00:49:18 +00001651 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskey063e7652006-01-17 17:31:53 +00001652 // FIXME - handle sibling offsets.
1653 // FIXME - handle all DIE types.
1654 Offset = SizeAndOffsetDie(Children[j], Offset);
1655 }
1656
1657 // End of children marker.
1658 Offset += sizeof(int8_t);
1659 }
1660
1661 Die->setSize(Offset - Die->getOffset());
1662 return Offset;
1663}
1664
1665/// SizeAndOffsets - Compute the size and offset of all the DIEs.
1666///
1667void DwarfWriter::SizeAndOffsets() {
Jim Laskey063e7652006-01-17 17:31:53 +00001668
1669 // Process each compile unit.
Jim Laskey52060a02006-01-24 00:49:18 +00001670 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00001671 CompileUnit *Unit = CompileUnits[i];
1672 if (Unit->hasContent()) {
1673 // Compute size of compile unit header
1674 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1675 sizeof(int16_t) + // DWARF version number
1676 sizeof(int32_t) + // Offset Into Abbrev. Section
1677 sizeof(int8_t); // Pointer Size (in bytes)
1678
1679 SizeAndOffsetDie(Unit->getDie(), Offset);
1680 }
Jim Laskey063e7652006-01-17 17:31:53 +00001681 }
1682}
1683
1684/// EmitDebugInfo - Emit the debug info section.
1685///
1686void DwarfWriter::EmitDebugInfo() const {
1687 // Start debug info section.
1688 Asm->SwitchSection(DwarfInfoSection, 0);
1689
Jim Laskeybd761842006-02-27 17:27:12 +00001690 // Process each compile unit.
1691 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1692 CompileUnit *Unit = CompileUnits[i];
1693
1694 if (Unit->hasContent()) {
1695 DIE *Die = Unit->getDie();
Jim Laskey0d086af2006-02-27 12:43:29 +00001696 // Emit the compile units header.
Jim Laskeybd761842006-02-27 17:27:12 +00001697 EmitLabel("info_begin", Unit->getID());
Jim Laskey0d086af2006-02-27 12:43:29 +00001698 // Emit size of content not including length itself
Jim Laskeybd761842006-02-27 17:27:12 +00001699 unsigned ContentSize = Die->getSize() +
Jim Laskey0d086af2006-02-27 12:43:29 +00001700 sizeof(int16_t) + // DWARF version number
1701 sizeof(int32_t) + // Offset Into Abbrev. Section
1702 sizeof(int8_t); // Pointer Size (in bytes)
1703
1704 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
1705 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1706 EmitReference("abbrev_begin", 0); EOL("Offset Into Abbrev. Section");
1707 EmitInt8(AddressSize); EOL("Address Size (in bytes)");
1708
Jim Laskeybd761842006-02-27 17:27:12 +00001709 EmitDIE(Die);
1710 EmitLabel("info_end", Unit->getID());
Jim Laskey063e7652006-01-17 17:31:53 +00001711 }
Jim Laskey0d086af2006-02-27 12:43:29 +00001712
1713 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001714 }
1715}
1716
1717/// EmitAbbreviations - Emit the abbreviation section.
1718///
1719void DwarfWriter::EmitAbbreviations() const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001720 // Check to see if it is worth the effort.
1721 if (!Abbreviations.empty()) {
1722 // Start the debug abbrev section.
1723 Asm->SwitchSection(DwarfAbbrevSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001724
Jim Laskeyd18e2892006-01-20 20:34:06 +00001725 EmitLabel("abbrev_begin", 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001726
Jim Laskeyd18e2892006-01-20 20:34:06 +00001727 // For each abbrevation.
1728 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001729 AbbrevID <= NAID; ++AbbrevID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001730 // Get abbreviation data
1731 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey063e7652006-01-17 17:31:53 +00001732
Jim Laskeyd18e2892006-01-20 20:34:06 +00001733 // Emit the abbrevations code (base 1 index.)
1734 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
Jim Laskey063e7652006-01-17 17:31:53 +00001735
Jim Laskeyd18e2892006-01-20 20:34:06 +00001736 // Emit the abbreviations data.
1737 Abbrev.Emit(*this);
Jim Laskey0d086af2006-02-27 12:43:29 +00001738
1739 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001740 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00001741
1742 EmitLabel("abbrev_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00001743
1744 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001745 }
1746}
1747
1748/// EmitDebugLines - Emit source line information.
1749///
1750void DwarfWriter::EmitDebugLines() const {
1751 // Minimum line delta, thus ranging from -10..(255-10).
1752 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1753 // Maximum line delta, thus ranging from -10..(255-10).
1754 const int MaxLineDelta = 255 + MinLineDelta;
1755
1756 // Start the dwarf line section.
1757 Asm->SwitchSection(DwarfLineSection, 0);
1758
1759 // Construct the section header.
1760
1761 EmitDifference("line_end", 0, "line_begin", 0);
1762 EOL("Length of Source Line Info");
1763 EmitLabel("line_begin", 0);
1764
Jim Laskeyda427fa2006-01-27 20:31:25 +00001765 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey063e7652006-01-17 17:31:53 +00001766
1767 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
1768 EOL("Prolog Length");
1769 EmitLabel("line_prolog_begin", 0);
1770
Jim Laskeyda427fa2006-01-27 20:31:25 +00001771 EmitInt8(1); EOL("Minimum Instruction Length");
Jim Laskey063e7652006-01-17 17:31:53 +00001772
Jim Laskeyda427fa2006-01-27 20:31:25 +00001773 EmitInt8(1); EOL("Default is_stmt_start flag");
Jim Laskey063e7652006-01-17 17:31:53 +00001774
Jim Laskeyda427fa2006-01-27 20:31:25 +00001775 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00001776
Jim Laskeyda427fa2006-01-27 20:31:25 +00001777 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00001778
Jim Laskeyda427fa2006-01-27 20:31:25 +00001779 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Jim Laskey063e7652006-01-17 17:31:53 +00001780
1781 // Line number standard opcode encodings argument count
Jim Laskeyda427fa2006-01-27 20:31:25 +00001782 EmitInt8(0); EOL("DW_LNS_copy arg count");
1783 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
1784 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
1785 EmitInt8(1); EOL("DW_LNS_set_file arg count");
1786 EmitInt8(1); EOL("DW_LNS_set_column arg count");
1787 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
1788 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
1789 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
1790 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskey063e7652006-01-17 17:31:53 +00001791
1792 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1793 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1794
1795 // Emit directories.
1796 for (unsigned DirectoryID = 1, NDID = Directories.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001797 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskey063e7652006-01-17 17:31:53 +00001798 EmitString(Directories[DirectoryID]); EOL("Directory");
1799 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00001800 EmitInt8(0); EOL("End of directories");
Jim Laskey063e7652006-01-17 17:31:53 +00001801
1802 // Emit files.
1803 for (unsigned SourceID = 1, NSID = SourceFiles.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001804 SourceID <= NSID; ++SourceID) {
Jim Laskey063e7652006-01-17 17:31:53 +00001805 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1806 EmitString(SourceFile.getName()); EOL("Source");
1807 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
1808 EmitULEB128Bytes(0); EOL("Mod date");
1809 EmitULEB128Bytes(0); EOL("File size");
1810 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00001811 EmitInt8(0); EOL("End of files");
Jim Laskey063e7652006-01-17 17:31:53 +00001812
1813 EmitLabel("line_prolog_end", 0);
1814
1815 // Emit line information
1816 const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
1817
1818 // Dwarf assumes we start with first line of first source file.
1819 unsigned Source = 1;
1820 unsigned Line = 1;
1821
1822 // Construct rows of the address, source, line, column matrix.
Jim Laskey52060a02006-01-24 00:49:18 +00001823 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001824 SourceLineInfo *LineInfo = LineInfos[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00001825
1826 if (DwarfVerbose) {
1827 unsigned SourceID = LineInfo->getSourceID();
1828 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1829 unsigned DirectoryID = SourceFile.getDirectoryID();
1830 O << "\t"
1831 << Asm->CommentString << " "
1832 << Directories[DirectoryID]
1833 << SourceFile.getName() << ":"
1834 << LineInfo->getLine() << "\n";
1835 }
Jim Laskey063e7652006-01-17 17:31:53 +00001836
1837 // Define the line address.
Jim Laskeyda427fa2006-01-27 20:31:25 +00001838 EmitInt8(0); EOL("Extended Op");
1839 EmitInt8(4 + 1); EOL("Op size");
1840 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskeye719a7c2006-01-18 16:54:26 +00001841 EmitReference("loc", i + 1); EOL("Location label");
Jim Laskey063e7652006-01-17 17:31:53 +00001842
1843 // If change of source, then switch to the new source.
1844 if (Source != LineInfo->getSourceID()) {
1845 Source = LineInfo->getSourceID();
Jim Laskeyda427fa2006-01-27 20:31:25 +00001846 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
Jim Laskeybd761842006-02-27 17:27:12 +00001847 EmitULEB128Bytes(Source); EOL("New Source");
Jim Laskey063e7652006-01-17 17:31:53 +00001848 }
1849
1850 // If change of line.
1851 if (Line != LineInfo->getLine()) {
1852 // Determine offset.
1853 int Offset = LineInfo->getLine() - Line;
1854 int Delta = Offset - MinLineDelta;
1855
1856 // Update line.
1857 Line = LineInfo->getLine();
1858
1859 // If delta is small enough and in range...
1860 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
1861 // ... then use fast opcode.
Jim Laskeyda427fa2006-01-27 20:31:25 +00001862 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
Jim Laskey063e7652006-01-17 17:31:53 +00001863 } else {
1864 // ... otherwise use long hand.
Jim Laskeyda427fa2006-01-27 20:31:25 +00001865 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
Jim Laskey063e7652006-01-17 17:31:53 +00001866 EmitSLEB128Bytes(Offset); EOL("Line Offset");
Jim Laskeyda427fa2006-01-27 20:31:25 +00001867 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
Jim Laskey063e7652006-01-17 17:31:53 +00001868 }
1869 } else {
1870 // Copy the previous row (different address or source)
Jim Laskeyda427fa2006-01-27 20:31:25 +00001871 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
Jim Laskey063e7652006-01-17 17:31:53 +00001872 }
1873 }
1874
Jim Laskey0420f2a2006-02-22 19:02:11 +00001875 // Define last address.
1876 EmitInt8(0); EOL("Extended Op");
1877 EmitInt8(4 + 1); EOL("Op size");
1878 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1879 EmitReference("text_end", 0); EOL("Location label");
1880
Jim Laskey063e7652006-01-17 17:31:53 +00001881 // Mark end of matrix.
Jim Laskeyda427fa2006-01-27 20:31:25 +00001882 EmitInt8(0); EOL("DW_LNE_end_sequence");
Jim Laskey063e7652006-01-17 17:31:53 +00001883 EmitULEB128Bytes(1); O << "\n";
Jim Laskeyda427fa2006-01-27 20:31:25 +00001884 EmitInt8(1); O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001885
1886 EmitLabel("line_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00001887
1888 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001889}
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001890
1891/// EmitDebugFrame - Emit visible names into a debug frame section.
1892///
1893void DwarfWriter::EmitDebugFrame() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00001894 // FIXME - Should be per frame
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001895}
1896
1897/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
1898///
1899void DwarfWriter::EmitDebugPubNames() {
Jim Laskeybd761842006-02-27 17:27:12 +00001900 // Start the dwarf pubnames section.
1901 Asm->SwitchSection(DwarfPubNamesSection, 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001902
Jim Laskeybd761842006-02-27 17:27:12 +00001903 // Process each compile unit.
1904 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1905 CompileUnit *Unit = CompileUnits[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00001906
Jim Laskeybd761842006-02-27 17:27:12 +00001907 if (Unit->hasContent()) {
1908 EmitDifference("pubnames_end", Unit->getID(),
1909 "pubnames_begin", Unit->getID());
1910 EOL("Length of Public Names Info");
1911
1912 EmitLabel("pubnames_begin", Unit->getID());
1913
1914 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
1915
1916 EmitReference("info_begin", Unit->getID());
1917 EOL("Offset of Compilation Unit Info");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001918
Jim Laskeybd761842006-02-27 17:27:12 +00001919 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
1920 EOL("Compilation Unit Length");
1921
1922 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
1923
1924 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
1925 GE = Globals.end();
1926 GI != GE; ++GI) {
1927 const std::string &Name = GI->first;
1928 DIE * Entity = GI->second;
1929
1930 EmitInt32(Entity->getOffset()); EOL("DIE offset");
1931 EmitString(Name); EOL("External Name");
1932 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00001933
Jim Laskeybd761842006-02-27 17:27:12 +00001934 EmitInt32(0); EOL("End Mark");
1935 EmitLabel("pubnames_end", Unit->getID());
1936
1937 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001938 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00001939 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001940}
1941
1942/// EmitDebugStr - Emit visible names into a debug str section.
1943///
1944void DwarfWriter::EmitDebugStr() {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001945 // Check to see if it is worth the effort.
1946 if (!StringPool.empty()) {
1947 // Start the dwarf str section.
1948 Asm->SwitchSection(DwarfStrSection, 0);
1949
1950 // For each of strings in teh string pool.
1951 for (unsigned StringID = 1, N = StringPool.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001952 StringID <= N; ++StringID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001953 // Emit a label for reference from debug information entries.
1954 EmitLabel("string", StringID);
1955 // Emit the string itself.
1956 const std::string &String = StringPool[StringID];
1957 EmitString(String); O << "\n";
1958 }
Jim Laskey0d086af2006-02-27 12:43:29 +00001959
1960 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001961 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001962}
1963
1964/// EmitDebugLoc - Emit visible names into a debug loc section.
1965///
1966void DwarfWriter::EmitDebugLoc() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00001967 // Start the dwarf loc section.
1968 Asm->SwitchSection(DwarfLocSection, 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00001969
1970 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001971}
1972
1973/// EmitDebugARanges - Emit visible names into a debug aranges section.
1974///
1975void DwarfWriter::EmitDebugARanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00001976 // Start the dwarf aranges section.
1977 Asm->SwitchSection(DwarfARangesSection, 0);
1978
1979 // FIXME - Mock up
Jim Laskeybd761842006-02-27 17:27:12 +00001980#if 0
1981 // Process each compile unit.
1982 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1983 CompileUnit *Unit = CompileUnits[i];
1984
1985 if (Unit->hasContent()) {
1986 // Don't include size of length
1987 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
1988
1989 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
1990
1991 EmitReference("info_begin", Unit->getID());
1992 EOL("Offset of Compilation Unit Info");
Jim Laskeye719a7c2006-01-18 16:54:26 +00001993
Jim Laskeybd761842006-02-27 17:27:12 +00001994 EmitInt8(AddressSize); EOL("Size of Address");
Jim Laskeye719a7c2006-01-18 16:54:26 +00001995
Jim Laskeybd761842006-02-27 17:27:12 +00001996 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeye719a7c2006-01-18 16:54:26 +00001997
Jim Laskeybd761842006-02-27 17:27:12 +00001998 EmitInt16(0); EOL("Pad (1)");
1999 EmitInt16(0); EOL("Pad (2)");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002000
Jim Laskeybd761842006-02-27 17:27:12 +00002001 // Range 1
2002 EmitReference("text_begin", 0); EOL("Address");
2003 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002004
Jim Laskeybd761842006-02-27 17:27:12 +00002005 EmitInt32(0); EOL("EOM (1)");
2006 EmitInt32(0); EOL("EOM (2)");
2007
2008 O << "\n";
2009 }
2010 }
2011#endif
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002012}
2013
2014/// EmitDebugRanges - Emit visible names into a debug ranges section.
2015///
2016void DwarfWriter::EmitDebugRanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002017 // Start the dwarf ranges section.
2018 Asm->SwitchSection(DwarfRangesSection, 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002019
2020 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002021}
2022
2023/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2024///
2025void DwarfWriter::EmitDebugMacInfo() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002026 // Start the dwarf macinfo section.
2027 Asm->SwitchSection(DwarfMacInfoSection, 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002028
2029 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002030}
Jim Laskey063e7652006-01-17 17:31:53 +00002031
Jim Laskey52060a02006-01-24 00:49:18 +00002032/// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2033/// header file.
2034void DwarfWriter::ConstructCompileUnitDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002035 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
Jim Laskey6e87c0e2006-01-26 21:22:49 +00002036
2037 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00002038 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
Jim Laskey52060a02006-01-24 00:49:18 +00002039 CompileUnits.push_back(Unit);
2040 }
2041}
2042
2043/// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2044/// variables.
2045void DwarfWriter::ConstructGlobalDIEs(Module &M) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002046 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskey0420f2a2006-02-22 19:02:11 +00002047 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00002048
2049 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002050 GlobalVariableDesc *GVD = GlobalVariables[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002051 NewGlobalVariable(GVD);
Jim Laskey52060a02006-01-24 00:49:18 +00002052 }
2053}
2054
Jim Laskey0420f2a2006-02-22 19:02:11 +00002055/// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2056/// subprograms.
2057void DwarfWriter::ConstructSubprogramDIEs(Module &M) {
2058 std::vector<SubprogramDesc *> Subprograms =
2059 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(M);
2060
2061 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2062 SubprogramDesc *SPD = Subprograms[i];
2063 NewSubprogram(SPD);
2064 }
2065}
Jim Laskey52060a02006-01-24 00:49:18 +00002066
Jim Laskey063e7652006-01-17 17:31:53 +00002067/// ShouldEmitDwarf - Determine if Dwarf declarations should be made.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002068///
2069bool DwarfWriter::ShouldEmitDwarf() {
2070 // Check if debug info is present.
2071 if (!DebugInfo || !DebugInfo->hasInfo()) return false;
2072
2073 // Make sure initial declarations are made.
2074 if (!didInitial) {
2075 EmitInitial();
2076 didInitial = true;
2077 }
2078
2079 // Okay to emit.
2080 return true;
2081}
2082
Jim Laskey063e7652006-01-17 17:31:53 +00002083//===----------------------------------------------------------------------===//
Jim Laskeyd18e2892006-01-20 20:34:06 +00002084// Main entry points.
Jim Laskey063e7652006-01-17 17:31:53 +00002085//
Jim Laskey52060a02006-01-24 00:49:18 +00002086
2087DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
2088: O(OS)
2089, Asm(A)
2090, DebugInfo(NULL)
2091, didInitial(false)
2092, CompileUnits()
2093, Abbreviations()
Jim Laskey52060a02006-01-24 00:49:18 +00002094, StringPool()
Jim Laskeybd761842006-02-27 17:27:12 +00002095, DescToUnitMap()
Jim Laskey0420f2a2006-02-22 19:02:11 +00002096, DescToDieMap()
2097, TypeToDieMap()
Jim Laskey52060a02006-01-24 00:49:18 +00002098, AddressSize(sizeof(int32_t))
2099, hasLEB128(false)
2100, hasDotLoc(false)
2101, hasDotFile(false)
2102, needsSet(false)
2103, DwarfAbbrevSection(".debug_abbrev")
2104, DwarfInfoSection(".debug_info")
2105, DwarfLineSection(".debug_line")
2106, DwarfFrameSection(".debug_frame")
2107, DwarfPubNamesSection(".debug_pubnames")
2108, DwarfPubTypesSection(".debug_pubtypes")
2109, DwarfStrSection(".debug_str")
2110, DwarfLocSection(".debug_loc")
2111, DwarfARangesSection(".debug_aranges")
2112, DwarfRangesSection(".debug_ranges")
2113, DwarfMacInfoSection(".debug_macinfo")
2114, TextSection(".text")
2115, DataSection(".data")
2116{}
2117DwarfWriter::~DwarfWriter() {
2118 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2119 delete CompileUnits[i];
Jim Laskey063e7652006-01-17 17:31:53 +00002120 }
Jim Laskey52060a02006-01-24 00:49:18 +00002121}
Jim Laskey063e7652006-01-17 17:31:53 +00002122
2123/// BeginModule - Emit all Dwarf sections that should come prior to the content.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002124///
Jim Laskey52060a02006-01-24 00:49:18 +00002125void DwarfWriter::BeginModule(Module &M) {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002126 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002127 EOL("Dwarf Begin Module");
Jim Laskeyb2efb852006-01-04 22:28:25 +00002128}
2129
Jim Laskey063e7652006-01-17 17:31:53 +00002130/// EndModule - Emit all Dwarf sections that should come after the content.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002131///
Jim Laskey52060a02006-01-24 00:49:18 +00002132void DwarfWriter::EndModule(Module &M) {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002133 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002134 EOL("Dwarf End Module");
2135
2136 // Standard sections final addresses.
Jim Laskeye719a7c2006-01-18 16:54:26 +00002137 Asm->SwitchSection(TextSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002138 EmitLabel("text_end", 0);
Jim Laskeye719a7c2006-01-18 16:54:26 +00002139 Asm->SwitchSection(DataSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002140 EmitLabel("data_end", 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002141
Jim Laskey52060a02006-01-24 00:49:18 +00002142 // Create all the compile unit DIEs.
2143 ConstructCompileUnitDIEs();
2144
2145 // Create DIEs for each of the externally visible global variables.
2146 ConstructGlobalDIEs(M);
Jim Laskey063e7652006-01-17 17:31:53 +00002147
Jim Laskey0420f2a2006-02-22 19:02:11 +00002148 // Create DIEs for each of the externally visible subprograms.
2149 ConstructSubprogramDIEs(M);
2150
Jim Laskey063e7652006-01-17 17:31:53 +00002151 // Compute DIE offsets and sizes.
2152 SizeAndOffsets();
2153
2154 // Emit all the DIEs into a debug info section
2155 EmitDebugInfo();
2156
2157 // Corresponding abbreviations into a abbrev section.
2158 EmitAbbreviations();
2159
2160 // Emit source line correspondence into a debug line section.
2161 EmitDebugLines();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002162
2163 // Emit info into a debug frame section.
Jim Laskey0d086af2006-02-27 12:43:29 +00002164 // EmitDebugFrame();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002165
2166 // Emit info into a debug pubnames section.
2167 EmitDebugPubNames();
2168
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002169 // Emit info into a debug str section.
2170 EmitDebugStr();
2171
2172 // Emit info into a debug loc section.
2173 EmitDebugLoc();
2174
2175 // Emit info into a debug aranges section.
2176 EmitDebugARanges();
2177
2178 // Emit info into a debug ranges section.
2179 EmitDebugRanges();
2180
2181 // Emit info into a debug macinfo section.
2182 EmitDebugMacInfo();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002183}
2184
Jim Laskeye719a7c2006-01-18 16:54:26 +00002185/// BeginFunction - Gather pre-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002186///
Jim Laskey52060a02006-01-24 00:49:18 +00002187void DwarfWriter::BeginFunction(MachineFunction &MF) {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002188 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002189 EOL("Dwarf Begin Function");
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002190}
2191
Jim Laskeye719a7c2006-01-18 16:54:26 +00002192/// EndFunction - Gather and emit post-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002193///
Jim Laskey52060a02006-01-24 00:49:18 +00002194void DwarfWriter::EndFunction(MachineFunction &MF) {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002195 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002196 EOL("Dwarf End Function");
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002197}