blob: 32079f22006969cd83327787a3e530d8d3183a56 [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 Laskey41886992006-04-07 16:34:46 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000022#include "llvm/CodeGen/MachineLocation.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000023#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000024#include "llvm/Support/CommandLine.h"
Jim Laskey52060a02006-01-24 00:49:18 +000025#include "llvm/Support/Mangler.h"
Jim Laskey563321a2006-09-06 18:34:40 +000026#include "llvm/Target/TargetAsmInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000027#include "llvm/Target/MRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000028#include "llvm/Target/TargetData.h"
Jim Laskey52060a02006-01-24 00:49:18 +000029#include "llvm/Target/TargetMachine.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000030#include "llvm/Target/TargetFrameInfo.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000031
Jim Laskeyb2efb852006-01-04 22:28:25 +000032#include <iostream>
Jim Laskeya7cea6f2006-01-04 13:52:30 +000033
Jim Laskeyb2efb852006-01-04 22:28:25 +000034using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000035using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000036
37static cl::opt<bool>
38DwarfVerbose("dwarf-verbose", cl::Hidden,
Jim Laskeyce50a162006-08-29 16:24:26 +000039 cl::desc("Add comments to Dwarf directives."));
Jim Laskey063e7652006-01-17 17:31:53 +000040
Jim Laskey0d086af2006-02-27 12:43:29 +000041namespace llvm {
42
Jim Laskey063e7652006-01-17 17:31:53 +000043//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000044// Forward declarations.
45//
Jim Laskey0d086af2006-02-27 12:43:29 +000046class DIE;
Jim Laskey063e7652006-01-17 17:31:53 +000047
Jim Laskey0d086af2006-02-27 12:43:29 +000048//===----------------------------------------------------------------------===//
Jim Laskeyf01e5472006-03-03 15:06:57 +000049// CompileUnit - This dwarf writer support class manages information associate
50// with a source file.
Jim Laskeybd761842006-02-27 17:27:12 +000051class CompileUnit {
52private:
53 CompileUnitDesc *Desc; // Compile unit debug descriptor.
54 unsigned ID; // File ID for source.
Jim Laskeyb8509c52006-03-23 18:07:55 +000055 DIE *Die; // Compile unit debug information entry.
Jim Laskeybd761842006-02-27 17:27:12 +000056 std::map<std::string, DIE *> Globals; // A map of globally visible named
57 // entities for this unit.
Jim Laskey90c79d72006-03-23 23:02:34 +000058 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
59 // Tracks the mapping of unit level
60 // debug informaton descriptors to debug
61 // information entries.
Jim Laskeybd761842006-02-27 17:27:12 +000062
63public:
64 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
65 : Desc(CUD)
66 , ID(I)
67 , Die(D)
68 , Globals()
Jim Laskey90c79d72006-03-23 23:02:34 +000069 , DescToDieMap()
Jim Laskeybd761842006-02-27 17:27:12 +000070 {}
71
72 ~CompileUnit();
73
74 // Accessors.
75 CompileUnitDesc *getDesc() const { return Desc; }
76 unsigned getID() const { return ID; }
77 DIE* getDie() const { return Die; }
78 std::map<std::string, DIE *> &getGlobals() { return Globals; }
79
80 /// hasContent - Return true if this compile unit has something to write out.
81 ///
82 bool hasContent() const;
83
84 /// AddGlobal - Add a new global entity to the compile unit.
85 ///
86 void AddGlobal(const std::string &Name, DIE *Die);
87
Jim Laskey90c79d72006-03-23 23:02:34 +000088 /// getDieMapSlotFor - Returns the debug information entry map slot for the
89 /// specified debug descriptor.
90 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
91 return DescToDieMap[DD];
92 }
Jim Laskeybd761842006-02-27 17:27:12 +000093};
94
95//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000096// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
97// Dwarf abbreviation.
98class DIEAbbrevData {
99private:
100 unsigned Attribute; // Dwarf attribute code.
101 unsigned Form; // Dwarf form code.
102
103public:
104 DIEAbbrevData(unsigned A, unsigned F)
105 : Attribute(A)
106 , Form(F)
107 {}
108
Jim Laskeybd761842006-02-27 17:27:12 +0000109 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000110 unsigned getAttribute() const { return Attribute; }
111 unsigned getForm() const { return Form; }
112
113 /// operator== - Used by DIEAbbrev to locate entry.
114 ///
115 bool operator==(const DIEAbbrevData &DAD) const {
116 return Attribute == DAD.Attribute && Form == DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000117 }
Jim Laskey063e7652006-01-17 17:31:53 +0000118
Jim Laskey0d086af2006-02-27 12:43:29 +0000119 /// operator!= - Used by DIEAbbrev to locate entry.
120 ///
121 bool operator!=(const DIEAbbrevData &DAD) const {
122 return Attribute != DAD.Attribute || Form != DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000123 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000124
125 /// operator< - Used by DIEAbbrev to locate entry.
126 ///
127 bool operator<(const DIEAbbrevData &DAD) const {
128 return Attribute < DAD.Attribute ||
129 (Attribute == DAD.Attribute && Form < DAD.Form);
130 }
131};
Jim Laskey063e7652006-01-17 17:31:53 +0000132
Jim Laskey0d086af2006-02-27 12:43:29 +0000133//===----------------------------------------------------------------------===//
134// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
135// information object.
136class DIEAbbrev {
137private:
138 unsigned Tag; // Dwarf tag code.
139 unsigned ChildrenFlag; // Dwarf children flag.
140 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
Jim Laskey063e7652006-01-17 17:31:53 +0000141
Jim Laskey0d086af2006-02-27 12:43:29 +0000142public:
Jim Laskey063e7652006-01-17 17:31:53 +0000143
Jim Laskey0d086af2006-02-27 12:43:29 +0000144 DIEAbbrev(unsigned T, unsigned C)
145 : Tag(T)
146 , ChildrenFlag(C)
147 , Data()
148 {}
149 ~DIEAbbrev() {}
150
Jim Laskeybd761842006-02-27 17:27:12 +0000151 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000152 unsigned getTag() const { return Tag; }
153 unsigned getChildrenFlag() const { return ChildrenFlag; }
154 const std::vector<DIEAbbrevData> &getData() const { return Data; }
155 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskey063e7652006-01-17 17:31:53 +0000156
Jim Laskey0d086af2006-02-27 12:43:29 +0000157 /// operator== - Used by UniqueVector to locate entry.
158 ///
159 bool operator==(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000160
Jim Laskey0d086af2006-02-27 12:43:29 +0000161 /// operator< - Used by UniqueVector to locate entry.
162 ///
163 bool operator<(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000164
Jim Laskey0d086af2006-02-27 12:43:29 +0000165 /// AddAttribute - Adds another set of attribute information to the
166 /// abbreviation.
167 void AddAttribute(unsigned Attribute, unsigned Form) {
168 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000169 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000170
Jim Laskeyb8509c52006-03-23 18:07:55 +0000171 /// AddFirstAttribute - Adds a set of attribute information to the front
172 /// of the abbreviation.
173 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
174 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
175 }
176
Jim Laskey0d086af2006-02-27 12:43:29 +0000177 /// Emit - Print the abbreviation using the specified Dwarf writer.
178 ///
179 void Emit(const DwarfWriter &DW) const;
180
181#ifndef NDEBUG
182 void print(std::ostream &O);
183 void dump();
184#endif
185};
Jim Laskey063e7652006-01-17 17:31:53 +0000186
Jim Laskey0d086af2006-02-27 12:43:29 +0000187//===----------------------------------------------------------------------===//
188// DIEValue - A debug information entry value.
189//
190class DIEValue {
191public:
192 enum {
193 isInteger,
194 isString,
195 isLabel,
196 isAsIsLabel,
197 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000198 isEntry,
199 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000200 };
201
202 unsigned Type; // Type of the value
203
204 DIEValue(unsigned T) : Type(T) {}
205 virtual ~DIEValue() {}
206
207 // Implement isa/cast/dyncast.
208 static bool classof(const DIEValue *) { return true; }
209
210 /// EmitValue - Emit value via the Dwarf writer.
211 ///
212 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
213
214 /// SizeOf - Return the size of a value in bytes.
215 ///
216 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
217};
Jim Laskey063e7652006-01-17 17:31:53 +0000218
Jim Laskey0d086af2006-02-27 12:43:29 +0000219//===----------------------------------------------------------------------===//
220// DWInteger - An integer value DIE.
221//
222class DIEInteger : public DIEValue {
223private:
224 uint64_t Integer;
225
226public:
227 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000228
Jim Laskey0d086af2006-02-27 12:43:29 +0000229 // Implement isa/cast/dyncast.
230 static bool classof(const DIEInteger *) { return true; }
231 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
232
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000233 /// BestForm - Choose the best form for integer.
234 ///
235 unsigned BestForm(bool IsSigned);
236
Jim Laskey0d086af2006-02-27 12:43:29 +0000237 /// EmitValue - Emit integer of appropriate size.
238 ///
239 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
240
241 /// SizeOf - Determine size of integer 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// DIEString - A string value DIE.
248//
249struct DIEString : public DIEValue {
250 const std::string String;
251
252 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
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 DIEString *) { return true; }
256 static bool classof(const DIEValue *S) { return S->Type == isString; }
257
258 /// EmitValue - Emit string value.
259 ///
260 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
261
262 /// SizeOf - Determine size of string value in bytes.
263 ///
264 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
265};
Jim Laskey063e7652006-01-17 17:31:53 +0000266
Jim Laskey0d086af2006-02-27 12:43:29 +0000267//===----------------------------------------------------------------------===//
268// DIEDwarfLabel - A Dwarf internal label expression DIE.
269//
270struct DIEDwarfLabel : public DIEValue {
271 const DWLabel Label;
272
273 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000274
Jim Laskey0d086af2006-02-27 12:43:29 +0000275 // Implement isa/cast/dyncast.
276 static bool classof(const DIEDwarfLabel *) { return true; }
277 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
278
279 /// EmitValue - Emit label value.
280 ///
281 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
282
283 /// SizeOf - Determine size of label value in bytes.
284 ///
285 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
286};
Jim Laskey063e7652006-01-17 17:31:53 +0000287
Jim Laskey063e7652006-01-17 17:31:53 +0000288
Jim Laskey0d086af2006-02-27 12:43:29 +0000289//===----------------------------------------------------------------------===//
290// DIEObjectLabel - A label to an object in code or data.
291//
292struct DIEObjectLabel : public DIEValue {
293 const std::string Label;
294
295 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000296
Jim Laskey0d086af2006-02-27 12:43:29 +0000297 // Implement isa/cast/dyncast.
298 static bool classof(const DIEObjectLabel *) { return true; }
299 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
300
301 /// EmitValue - Emit label value.
302 ///
303 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
304
305 /// SizeOf - Determine size of label value in bytes.
306 ///
307 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
308};
Jim Laskey063e7652006-01-17 17:31:53 +0000309
Jim Laskey0d086af2006-02-27 12:43:29 +0000310//===----------------------------------------------------------------------===//
311// DIEDelta - A simple label difference DIE.
312//
313struct DIEDelta : public DIEValue {
314 const DWLabel LabelHi;
315 const DWLabel LabelLo;
316
317 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
318 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000319
Jim Laskey0d086af2006-02-27 12:43:29 +0000320 // Implement isa/cast/dyncast.
321 static bool classof(const DIEDelta *) { return true; }
322 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
323
324 /// EmitValue - Emit delta value.
325 ///
326 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
327
328 /// SizeOf - Determine size of delta value in bytes.
329 ///
330 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
331};
Jim Laskey063e7652006-01-17 17:31:53 +0000332
Jim Laskey0d086af2006-02-27 12:43:29 +0000333//===----------------------------------------------------------------------===//
334// DIEntry - A pointer to a debug information entry.
335//
336struct DIEntry : public DIEValue {
337 DIE *Entry;
338
339 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
340
341 // Implement isa/cast/dyncast.
342 static bool classof(const DIEntry *) { return true; }
343 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
344
Jim Laskeyb8509c52006-03-23 18:07:55 +0000345 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000346 ///
347 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
348
Jim Laskeyb8509c52006-03-23 18:07:55 +0000349 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000350 ///
351 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
352};
353
354//===----------------------------------------------------------------------===//
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000355// DIEBlock - A block of values. Primarily used for location expressions.
356//
357struct DIEBlock : public DIEValue {
358 unsigned Size; // Size in bytes excluding size header.
359 std::vector<unsigned> Forms; // Data forms.
360 std::vector<DIEValue *> Values; // Block values.
361
362 DIEBlock()
363 : DIEValue(isBlock)
364 , Size(0)
365 , Forms()
366 , Values()
367 {}
368 ~DIEBlock();
369
370 // Implement isa/cast/dyncast.
371 static bool classof(const DIEBlock *) { return true; }
372 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
373
374 /// ComputeSize - calculate the size of the block.
375 ///
376 unsigned ComputeSize(DwarfWriter &DW);
377
378 /// BestForm - Choose the best form for data.
379 ///
380 unsigned BestForm();
381
382 /// EmitValue - Emit block data.
383 ///
384 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
385
386 /// SizeOf - Determine size of block data in bytes.
387 ///
388 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
389
390 /// AddUInt - Add an unsigned integer value.
391 ///
392 void AddUInt(unsigned Form, uint64_t Integer);
393
394 /// AddSInt - Add an signed integer value.
395 ///
396 void AddSInt(unsigned Form, int64_t Integer);
397
398 /// AddString - Add a std::string value.
399 ///
400 void AddString(unsigned Form, const std::string &String);
401
402 /// AddLabel - Add a Dwarf label value.
403 ///
404 void AddLabel(unsigned Form, const DWLabel &Label);
405
406 /// AddObjectLabel - Add a non-Dwarf label value.
407 ///
408 void AddObjectLabel(unsigned Form, const std::string &Label);
409
410 /// AddDelta - Add a label delta value.
411 ///
412 void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
413
414 /// AddDIEntry - Add a DIE value.
415 ///
416 void AddDIEntry(unsigned Form, DIE *Entry);
417
418};
419
420//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +0000421// DIE - A structured debug information entry. Has an abbreviation which
422// describes it's organization.
423class DIE {
424private:
425 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
426 unsigned AbbrevID; // Decribing abbreviation ID.
427 unsigned Offset; // Offset in debug info section.
428 unsigned Size; // Size of instance + children.
429 std::vector<DIE *> Children; // Children DIEs.
430 std::vector<DIEValue *> Values; // Attributes values.
431
432public:
433 DIE(unsigned Tag);
434 ~DIE();
435
Jim Laskeybd761842006-02-27 17:27:12 +0000436 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000437 unsigned getAbbrevID() const { return AbbrevID; }
438 unsigned getOffset() const { return Offset; }
439 unsigned getSize() const { return Size; }
440 const std::vector<DIE *> &getChildren() const { return Children; }
441 const std::vector<DIEValue *> &getValues() const { return Values; }
442 void setOffset(unsigned O) { Offset = O; }
443 void setSize(unsigned S) { Size = S; }
444
445 /// SiblingOffset - Return the offset of the debug information entry's
446 /// sibling.
447 unsigned SiblingOffset() const { return Offset + Size; }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000448
449 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
450 ///
451 void AddSiblingOffset();
Jim Laskey0d086af2006-02-27 12:43:29 +0000452
453 /// AddUInt - Add an unsigned integer attribute data and value.
454 ///
455 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
456
457 /// AddSInt - Add an signed integer attribute data and value.
458 ///
459 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
460
461 /// AddString - Add a std::string attribute data and value.
462 ///
463 void AddString(unsigned Attribute, unsigned Form,
464 const std::string &String);
465
466 /// AddLabel - Add a Dwarf label attribute data and value.
467 ///
468 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
469
470 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
471 ///
472 void AddObjectLabel(unsigned Attribute, unsigned Form,
473 const std::string &Label);
474
475 /// AddDelta - Add a label delta attribute data and value.
476 ///
477 void AddDelta(unsigned Attribute, unsigned Form,
478 const DWLabel &Hi, const DWLabel &Lo);
479
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000480 /// AddDIEntry - Add a DIE attribute data and value.
Jim Laskey0d086af2006-02-27 12:43:29 +0000481 ///
482 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
483
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000484 /// AddBlock - Add block data.
485 ///
486 void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
487
Jim Laskey0d086af2006-02-27 12:43:29 +0000488 /// Complete - Indicate that all attributes have been added and
489 /// ready to get an abbreviation ID.
490 ///
491 void Complete(DwarfWriter &DW);
492
493 /// AddChild - Add a child to the DIE.
494 void AddChild(DIE *Child);
495};
496
497} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +0000498
499//===----------------------------------------------------------------------===//
500
Jim Laskeybd761842006-02-27 17:27:12 +0000501CompileUnit::~CompileUnit() {
502 delete Die;
503}
504
505/// hasContent - Return true if this compile unit has something to write out.
506///
507bool CompileUnit::hasContent() const {
508 return !Die->getChildren().empty();
509}
510
511/// AddGlobal - Add a new global entity to the compile unit.
512///
513void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
514 Globals[Name] = Die;
515}
516
517//===----------------------------------------------------------------------===//
518
Jim Laskeyd18e2892006-01-20 20:34:06 +0000519/// operator== - Used by UniqueVector to locate entry.
520///
521bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
522 if (Tag != DA.Tag) return false;
523 if (ChildrenFlag != DA.ChildrenFlag) return false;
524 if (Data.size() != DA.Data.size()) return false;
525
Jim Laskey52060a02006-01-24 00:49:18 +0000526 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000527 if (Data[i] != DA.Data[i]) return false;
Jim Laskeyd18e2892006-01-20 20:34:06 +0000528 }
529
530 return true;
531}
532
533/// operator< - Used by UniqueVector to locate entry.
534///
535bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
536 if (Tag != DA.Tag) return Tag < DA.Tag;
537 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
538 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
539
Jim Laskey52060a02006-01-24 00:49:18 +0000540 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000541 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +0000542 }
543
544 return false;
545}
546
547/// Emit - Print the abbreviation using the specified Dwarf writer.
548///
549void DIEAbbrev::Emit(const DwarfWriter &DW) const {
550 // Emit its Dwarf tag type.
551 DW.EmitULEB128Bytes(Tag);
552 DW.EOL(TagString(Tag));
553
554 // Emit whether it has children DIEs.
555 DW.EmitULEB128Bytes(ChildrenFlag);
556 DW.EOL(ChildrenString(ChildrenFlag));
557
558 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +0000559 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000560 const DIEAbbrevData &AttrData = Data[i];
561
562 // Emit attribute type.
563 DW.EmitULEB128Bytes(AttrData.getAttribute());
564 DW.EOL(AttributeString(AttrData.getAttribute()));
565
566 // Emit form type.
567 DW.EmitULEB128Bytes(AttrData.getForm());
568 DW.EOL(FormEncodingString(AttrData.getForm()));
569 }
570
571 // Mark end of abbreviation.
572 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
573 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
574}
575
576#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +0000577void DIEAbbrev::print(std::ostream &O) {
578 O << "Abbreviation @"
579 << std::hex << (intptr_t)this << std::dec
580 << " "
581 << TagString(Tag)
582 << " "
583 << ChildrenString(ChildrenFlag)
584 << "\n";
585
586 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
587 O << " "
588 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +0000589 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +0000590 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +0000591 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +0000592 }
Jim Laskeya0f3d172006-09-07 22:06:40 +0000593}
594void DIEAbbrev::dump() { print(std::cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +0000595#endif
596
597//===----------------------------------------------------------------------===//
598
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000599/// BestForm - Choose the best form for integer.
600///
601unsigned DIEInteger::BestForm(bool IsSigned) {
602 if (IsSigned) {
603 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
604 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
605 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
606 } else {
607 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
608 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
609 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
610 }
611 return DW_FORM_data8;
612}
613
Jim Laskey063e7652006-01-17 17:31:53 +0000614/// EmitValue - Emit integer of appropriate size.
615///
616void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
617 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +0000618 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000619 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000620 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000621 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000622 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000623 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000624 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000625 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000626 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +0000627 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
628 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +0000629 default: assert(0 && "DIE Value form not supported yet"); break;
630 }
631}
632
633/// SizeOf - Determine size of integer value in bytes.
634///
635unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
636 switch (Form) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000637 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000638 case DW_FORM_ref1: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000639 case DW_FORM_data1: return sizeof(int8_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000640 case DW_FORM_ref2: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000641 case DW_FORM_data2: return sizeof(int16_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000642 case DW_FORM_ref4: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000643 case DW_FORM_data4: return sizeof(int32_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000644 case DW_FORM_ref8: // Fall thru
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000645 case DW_FORM_data8: return sizeof(int64_t);
Jim Laskey40020172006-01-20 21:02:36 +0000646 case DW_FORM_udata: return DW.SizeULEB128(Integer);
647 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +0000648 default: assert(0 && "DIE Value form not supported yet"); break;
649 }
650 return 0;
651}
652
653//===----------------------------------------------------------------------===//
654
655/// EmitValue - Emit string value.
656///
657void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000658 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +0000659}
660
661/// SizeOf - Determine size of string value in bytes.
662///
663unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey40020172006-01-20 21:02:36 +0000664 return String.size() + sizeof(char); // sizeof('\0');
Jim Laskey063e7652006-01-17 17:31:53 +0000665}
666
667//===----------------------------------------------------------------------===//
668
669/// EmitValue - Emit label value.
670///
Jim Laskey52060a02006-01-24 00:49:18 +0000671void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000672 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +0000673}
674
675/// SizeOf - Determine size of label value in bytes.
676///
Jim Laskey52060a02006-01-24 00:49:18 +0000677unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000678 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +0000679}
680
681//===----------------------------------------------------------------------===//
682
Jim Laskeyd18e2892006-01-20 20:34:06 +0000683/// EmitValue - Emit label value.
684///
Jim Laskey52060a02006-01-24 00:49:18 +0000685void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000686 DW.EmitReference(Label);
687}
688
689/// SizeOf - Determine size of label value in bytes.
690///
Jim Laskey52060a02006-01-24 00:49:18 +0000691unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000692 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +0000693}
694
695//===----------------------------------------------------------------------===//
696
Jim Laskey063e7652006-01-17 17:31:53 +0000697/// EmitValue - Emit delta value.
698///
699void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000700 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +0000701}
702
703/// SizeOf - Determine size of delta value in bytes.
704///
705unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000706 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +0000707}
708
709//===----------------------------------------------------------------------===//
Jim Laskeyb8509c52006-03-23 18:07:55 +0000710/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000711///
712void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +0000713 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +0000714}
715
Jim Laskeyb8509c52006-03-23 18:07:55 +0000716/// SizeOf - Determine size of debug information entry value in bytes.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000717///
718unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
719 return sizeof(int32_t);
720}
721
722//===----------------------------------------------------------------------===//
723
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000724DIEBlock::~DIEBlock() {
725 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
726 delete Values[i];
727 }
728}
729
730/// ComputeSize - calculate the size of the block.
731///
732unsigned DIEBlock::ComputeSize(DwarfWriter &DW) {
733 Size = 0;
734 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
735 Size += Values[i]->SizeOf(DW, Forms[i]);
736 }
737 return Size;
738}
739
740/// BestForm - Choose the best form for data.
741///
742unsigned DIEBlock::BestForm() {
743 if ((unsigned char)Size == Size) return DW_FORM_block1;
744 if ((unsigned short)Size == Size) return DW_FORM_block2;
745 if ((unsigned int)Size == Size) return DW_FORM_block4;
746 return DW_FORM_block;
747}
748
749/// EmitValue - Emit block data.
750///
751void DIEBlock::EmitValue(const DwarfWriter &DW, unsigned Form) const {
752 switch (Form) {
753 case DW_FORM_block1: DW.EmitInt8(Size); break;
754 case DW_FORM_block2: DW.EmitInt16(Size); break;
755 case DW_FORM_block4: DW.EmitInt32(Size); break;
756 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
757 default: assert(0 && "Improper form for block"); break;
758 }
759 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
760 DW.EOL("");
761 Values[i]->EmitValue(DW, Forms[i]);
762 }
763}
764
765/// SizeOf - Determine size of block data in bytes.
766///
767unsigned DIEBlock::SizeOf(const DwarfWriter &DW, unsigned Form) const {
768 switch (Form) {
769 case DW_FORM_block1: return Size + sizeof(int8_t);
770 case DW_FORM_block2: return Size + sizeof(int16_t);
771 case DW_FORM_block4: return Size + sizeof(int32_t);
772 case DW_FORM_block: return Size + DW.SizeULEB128(Size);
773 default: assert(0 && "Improper form for block"); break;
774 }
775 return 0;
776}
777
778/// AddUInt - Add an unsigned integer value.
779///
780void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
781 DIEInteger *DI = new DIEInteger(Integer);
782 Values.push_back(DI);
783 if (Form == 0) Form = DI->BestForm(false);
784 Forms.push_back(Form);
785}
786
787/// AddSInt - Add an signed integer value.
788///
789void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
790 DIEInteger *DI = new DIEInteger(Integer);
791 Values.push_back(DI);
792 if (Form == 0) Form = DI->BestForm(true);
793 Forms.push_back(Form);
794}
795
796/// AddString - Add a std::string value.
797///
798void DIEBlock::AddString(unsigned Form, const std::string &String) {
799 Values.push_back(new DIEString(String));
800 Forms.push_back(Form);
801}
802
803/// AddLabel - Add a Dwarf label value.
804///
805void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
806 Values.push_back(new DIEDwarfLabel(Label));
807 Forms.push_back(Form);
808}
809
810/// AddObjectLabel - Add a non-Dwarf label value.
811///
812void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
813 Values.push_back(new DIEObjectLabel(Label));
814 Forms.push_back(Form);
815}
816
817/// AddDelta - Add a label delta value.
818///
819void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
820 Values.push_back(new DIEDelta(Hi, Lo));
821 Forms.push_back(Form);
822}
823
824/// AddDIEntry - Add a DIE value.
825///
826void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
827 Values.push_back(new DIEntry(Entry));
828 Forms.push_back(Form);
829}
830
831//===----------------------------------------------------------------------===//
832
Jim Laskey0420f2a2006-02-22 19:02:11 +0000833DIE::DIE(unsigned Tag)
834: Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
Jim Laskeyd18e2892006-01-20 20:34:06 +0000835, AbbrevID(0)
836, Offset(0)
837, Size(0)
Jim Laskeyd18e2892006-01-20 20:34:06 +0000838, Children()
839, Values()
840{}
841
842DIE::~DIE() {
843 if (Abbrev) delete Abbrev;
844
Jim Laskey52060a02006-01-24 00:49:18 +0000845 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000846 delete Children[i];
847 }
848
Jim Laskey52060a02006-01-24 00:49:18 +0000849 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000850 delete Values[j];
851 }
Jim Laskeyd18e2892006-01-20 20:34:06 +0000852}
853
Jim Laskeyb8509c52006-03-23 18:07:55 +0000854/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
855///
856void DIE::AddSiblingOffset() {
857 DIEInteger *DI = new DIEInteger(0);
858 Values.insert(Values.begin(), DI);
859 Abbrev->AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
860}
861
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000862/// AddUInt - Add an unsigned integer attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000863///
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000864void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000865 DIEInteger *DI = new DIEInteger(Integer);
866 Values.push_back(DI);
867 if (!Form) Form = DI->BestForm(false);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000868 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000869}
870
871/// AddSInt - Add an signed integer attribute data and value.
872///
873void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000874 DIEInteger *DI = new DIEInteger(Integer);
875 Values.push_back(DI);
876 if (!Form) Form = DI->BestForm(true);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000877 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000878}
879
880/// AddString - Add a std::string attribute data and value.
881///
882void DIE::AddString(unsigned Attribute, unsigned Form,
883 const std::string &String) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000884 Values.push_back(new DIEString(String));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000885 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000886}
887
888/// AddLabel - Add a Dwarf label attribute data and value.
889///
890void DIE::AddLabel(unsigned Attribute, unsigned Form,
891 const DWLabel &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000892 Values.push_back(new DIEDwarfLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000893 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000894}
895
Jim Laskey52060a02006-01-24 00:49:18 +0000896/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000897///
Jim Laskey52060a02006-01-24 00:49:18 +0000898void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
899 const std::string &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000900 Values.push_back(new DIEObjectLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000901 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000902}
903
904/// AddDelta - Add a label delta attribute data and value.
905///
906void DIE::AddDelta(unsigned Attribute, unsigned Form,
907 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000908 Values.push_back(new DIEDelta(Hi, Lo));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000909 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000910}
911
912/// AddDIEntry - Add a DIE attribute data and value.
913///
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000914void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000915 Values.push_back(new DIEntry(Entry));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000916 Abbrev->AddAttribute(Attribute, Form);
917}
918
919/// AddBlock - Add block data.
920///
921void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
922 assert(Block->Size && "Block size has not been computed");
923 Values.push_back(Block);
924 if (!Form) Form = Block->BestForm();
925 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000926}
927
928/// Complete - Indicate that all attributes have been added and ready to get an
929/// abbreviation ID.
930void DIE::Complete(DwarfWriter &DW) {
931 AbbrevID = DW.NewAbbreviation(Abbrev);
932 delete Abbrev;
933 Abbrev = NULL;
934}
935
936/// AddChild - Add a child to the DIE.
937///
938void DIE::AddChild(DIE *Child) {
Jim Laskey0420f2a2006-02-22 19:02:11 +0000939 assert(Abbrev && "Adding children without an abbreviation");
940 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000941 Children.push_back(Child);
942}
943
944//===----------------------------------------------------------------------===//
945
Jim Laskey3ebe71d2006-09-01 12:55:05 +0000946/// DwarfWriter
Jim Laskeyd18e2892006-01-20 20:34:06 +0000947
948//===----------------------------------------------------------------------===//
Jim Laskey063e7652006-01-17 17:31:53 +0000949
950/// PrintHex - Print a value as a hexidecimal value.
951///
952void DwarfWriter::PrintHex(int Value) const {
953 O << "0x" << std::hex << Value << std::dec;
954}
955
956/// EOL - Print a newline character to asm stream. If a comment is present
957/// then it will be printed first. Comments should not contain '\n'.
958void DwarfWriter::EOL(const std::string &Comment) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000959 if (DwarfVerbose && !Comment.empty()) {
Jim Laskey063e7652006-01-17 17:31:53 +0000960 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +0000961 << TAI->getCommentString()
Jim Laskey063e7652006-01-17 17:31:53 +0000962 << " "
963 << Comment;
964 }
965 O << "\n";
966}
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000967
Jim Laskeyb8509c52006-03-23 18:07:55 +0000968/// EmitAlign - Print a align directive.
969///
970void DwarfWriter::EmitAlign(unsigned Alignment) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000971 O << TAI->getAlignDirective() << Alignment << "\n";
Jim Laskeyb8509c52006-03-23 18:07:55 +0000972}
973
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000974/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
Jim Laskey063e7652006-01-17 17:31:53 +0000975/// unsigned leb128 value.
976void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000977 if (TAI->hasLEB128()) {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000978 O << "\t.uleb128\t"
979 << Value;
980 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000981 O << TAI->getData8bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +0000982 PrintULEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000983 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000984}
985
986/// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
Jim Laskey063e7652006-01-17 17:31:53 +0000987/// signed leb128 value.
988void DwarfWriter::EmitSLEB128Bytes(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000989 if (TAI->hasLEB128()) {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000990 O << "\t.sleb128\t"
991 << Value;
992 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000993 O << TAI->getData8bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +0000994 PrintSLEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000995 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000996}
997
Jim Laskey063e7652006-01-17 17:31:53 +0000998/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +0000999/// representing an unsigned leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +00001000void DwarfWriter::PrintULEB128(unsigned Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001001 do {
1002 unsigned Byte = Value & 0x7f;
1003 Value >>= 7;
1004 if (Value) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001005 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001006 if (Value) O << ", ";
1007 } while (Value);
1008}
1009
Jim Laskey063e7652006-01-17 17:31:53 +00001010/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
1011/// value.
1012unsigned DwarfWriter::SizeULEB128(unsigned Value) {
1013 unsigned Size = 0;
1014 do {
1015 Value >>= 7;
1016 Size += sizeof(int8_t);
1017 } while (Value);
1018 return Size;
1019}
1020
1021/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +00001022/// representing a signed leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +00001023void DwarfWriter::PrintSLEB128(int Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001024 int Sign = Value >> (8 * sizeof(Value) - 1);
1025 bool IsMore;
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001026
Jim Laskeyb2efb852006-01-04 22:28:25 +00001027 do {
1028 unsigned Byte = Value & 0x7f;
1029 Value >>= 7;
1030 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1031 if (IsMore) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001032 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001033 if (IsMore) O << ", ";
1034 } while (IsMore);
1035}
1036
Jim Laskey063e7652006-01-17 17:31:53 +00001037/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
1038/// value.
1039unsigned DwarfWriter::SizeSLEB128(int Value) {
1040 unsigned Size = 0;
1041 int Sign = Value >> (8 * sizeof(Value) - 1);
1042 bool IsMore;
1043
1044 do {
1045 unsigned Byte = Value & 0x7f;
1046 Value >>= 7;
1047 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1048 Size += sizeof(int8_t);
1049 } while (IsMore);
1050 return Size;
1051}
1052
Jim Laskeyda427fa2006-01-27 20:31:25 +00001053/// EmitInt8 - Emit a byte directive and value.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001054///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001055void DwarfWriter::EmitInt8(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001056 O << TAI->getData8bitsDirective();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001057 PrintHex(Value & 0xFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001058}
1059
Jim Laskeyda427fa2006-01-27 20:31:25 +00001060/// EmitInt16 - Emit a short directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001061///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001062void DwarfWriter::EmitInt16(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001063 O << TAI->getData16bitsDirective();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001064 PrintHex(Value & 0xFFFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001065}
1066
Jim Laskeyda427fa2006-01-27 20:31:25 +00001067/// EmitInt32 - Emit a long directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001068///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001069void DwarfWriter::EmitInt32(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001070 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001071 PrintHex(Value);
1072}
1073
Jim Laskeyda427fa2006-01-27 20:31:25 +00001074/// EmitInt64 - Emit a long long directive and value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001075///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001076void DwarfWriter::EmitInt64(uint64_t Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001077 if (TAI->getData64bitsDirective()) {
1078 O << TAI->getData64bitsDirective() << "0x" << std::hex << Value << std::dec;
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001079 } else {
Owen Andersona69571c2006-05-03 01:29:57 +00001080 if (TD->isBigEndian()) {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001081 EmitInt32(unsigned(Value >> 32)); O << "\n";
1082 EmitInt32(unsigned(Value));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001083 } else {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001084 EmitInt32(unsigned(Value)); O << "\n";
1085 EmitInt32(unsigned(Value >> 32));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001086 }
1087 }
1088}
1089
Jim Laskey063e7652006-01-17 17:31:53 +00001090/// EmitString - Emit a string with quotes and a null terminator.
1091/// Special characters are emitted properly. (Eg. '\t')
1092void DwarfWriter::EmitString(const std::string &String) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001093 O << TAI->getAsciiDirective()
Jim Laskey063e7652006-01-17 17:31:53 +00001094 << "\"";
Jim Laskey52060a02006-01-24 00:49:18 +00001095 for (unsigned i = 0, N = String.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001096 unsigned char C = String[i];
1097
1098 if (!isascii(C) || iscntrl(C)) {
1099 switch(C) {
1100 case '\b': O << "\\b"; break;
1101 case '\f': O << "\\f"; break;
1102 case '\n': O << "\\n"; break;
1103 case '\r': O << "\\r"; break;
1104 case '\t': O << "\\t"; break;
1105 default:
1106 O << '\\';
Jim Laskey3ebe71d2006-09-01 12:55:05 +00001107 O << char('0' + ((C >> 6) & 7));
1108 O << char('0' + ((C >> 3) & 7));
1109 O << char('0' + ((C >> 0) & 7));
Jim Laskey063e7652006-01-17 17:31:53 +00001110 break;
1111 }
1112 } else if (C == '\"') {
1113 O << "\\\"";
1114 } else if (C == '\'') {
1115 O << "\\\'";
1116 } else {
1117 O << C;
1118 }
1119 }
1120 O << "\\0\"";
1121}
1122
1123/// PrintLabelName - Print label name in form used by Dwarf writer.
1124///
1125void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001126 O << TAI->getPrivateGlobalPrefix()
Jim Laskeyb2efb852006-01-04 22:28:25 +00001127 << "debug_"
Jim Laskey0420f2a2006-02-22 19:02:11 +00001128 << Tag;
1129 if (Number) O << Number;
Jim Laskeyb2efb852006-01-04 22:28:25 +00001130}
1131
Jim Laskey063e7652006-01-17 17:31:53 +00001132/// EmitLabel - Emit location label for internal use by Dwarf.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001133///
Jim Laskey063e7652006-01-17 17:31:53 +00001134void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
1135 PrintLabelName(Tag, Number);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001136 O << ":\n";
1137}
1138
Jim Laskeye719a7c2006-01-18 16:54:26 +00001139/// EmitReference - Emit a reference to a label.
Jim Laskey063e7652006-01-17 17:31:53 +00001140///
Jim Laskeye719a7c2006-01-18 16:54:26 +00001141void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001142 if (TAI->getAddressSize() == 4)
1143 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001144 else
Jim Laskey563321a2006-09-06 18:34:40 +00001145 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001146
1147 PrintLabelName(Tag, Number);
1148}
Jim Laskey73683212006-01-21 00:59:54 +00001149void DwarfWriter::EmitReference(const std::string &Name) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001150 if (TAI->getAddressSize() == 4)
1151 O << TAI->getData32bitsDirective();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001152 else
Jim Laskey563321a2006-09-06 18:34:40 +00001153 O << TAI->getData64bitsDirective();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001154
1155 O << Name;
1156}
Jim Laskey063e7652006-01-17 17:31:53 +00001157
1158/// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
1159/// assemblers do not accept absolute expressions with data directives, so there
1160/// is an option (needsSet) to use an intermediary 'set' expression.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001161void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
1162 const char *TagLo, unsigned NumberLo) const {
Jim Laskeya0f3d172006-09-07 22:06:40 +00001163 if (TAI->needsSet()) {
Jim Laskey063e7652006-01-17 17:31:53 +00001164 static unsigned SetCounter = 0;
Jim Laskeyd18e2892006-01-20 20:34:06 +00001165
Jim Laskey063e7652006-01-17 17:31:53 +00001166 O << "\t.set\t";
1167 PrintLabelName("set", SetCounter);
1168 O << ",";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001169 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001170 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001171 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001172 O << "\n";
1173
Jim Laskey563321a2006-09-06 18:34:40 +00001174 if (TAI->getAddressSize() == sizeof(int32_t))
1175 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001176 else
Jim Laskey563321a2006-09-06 18:34:40 +00001177 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001178
1179 PrintLabelName("set", SetCounter);
1180
Jim Laskey52060a02006-01-24 00:49:18 +00001181 ++SetCounter;
Jim Laskey063e7652006-01-17 17:31:53 +00001182 } else {
Jim Laskey563321a2006-09-06 18:34:40 +00001183 if (TAI->getAddressSize() == sizeof(int32_t))
1184 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001185 else
Jim Laskey563321a2006-09-06 18:34:40 +00001186 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001187
Jim Laskeyd18e2892006-01-20 20:34:06 +00001188 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001189 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001190 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001191 }
1192}
1193
Jim Laskeyd18e2892006-01-20 20:34:06 +00001194/// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
Jim Laskey063e7652006-01-17 17:31:53 +00001195///
Jim Laskeyd18e2892006-01-20 20:34:06 +00001196unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
1197 return Abbreviations.insert(*Abbrev);
1198}
1199
1200/// NewString - Add a string to the constant pool and returns a label.
1201///
1202DWLabel DwarfWriter::NewString(const std::string &String) {
1203 unsigned StringID = StringPool.insert(String);
1204 return DWLabel("string", StringID);
1205}
1206
Jim Laskeyb8509c52006-03-23 18:07:55 +00001207/// AddSourceLine - Add location information to specified debug information
1208/// entry.
Jim Laskeyd16f2a72006-06-19 19:49:42 +00001209void DwarfWriter::AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line){
Jim Laskeyb8509c52006-03-23 18:07:55 +00001210 if (File && Line) {
1211 CompileUnit *FileUnit = FindCompileUnit(File);
1212 unsigned FileID = FileUnit->getID();
1213 Die->AddUInt(DW_AT_decl_file, 0, FileID);
1214 Die->AddUInt(DW_AT_decl_line, 0, Line);
1215 }
1216}
1217
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001218/// AddAddress - Add an address attribute to a die based on the location
1219/// provided.
1220void DwarfWriter::AddAddress(DIE *Die, unsigned Attribute,
Jim Laskey41886992006-04-07 16:34:46 +00001221 const MachineLocation &Location) {
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001222 DIEBlock *Block = new DIEBlock();
Jim Laskey3d3d4042006-08-25 19:39:52 +00001223 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1224
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001225 if (Location.isRegister()) {
Jim Laskey3d3d4042006-08-25 19:39:52 +00001226 if (Reg < 32) {
1227 Block->AddUInt(DW_FORM_data1, DW_OP_reg0 + Reg);
1228 } else {
1229 Block->AddUInt(DW_FORM_data1, DW_OP_regx);
1230 Block->AddUInt(DW_FORM_udata, Reg);
1231 }
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001232 } else {
Jim Laskey3d3d4042006-08-25 19:39:52 +00001233 if (Reg < 32) {
1234 Block->AddUInt(DW_FORM_data1, DW_OP_breg0 + Reg);
1235 } else {
1236 Block->AddUInt(DW_FORM_data1, DW_OP_bregx);
1237 Block->AddUInt(DW_FORM_udata, Reg);
1238 }
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001239 Block->AddUInt(DW_FORM_sdata, Location.getOffset());
1240 }
1241 Block->ComputeSize(*this);
1242 Die->AddBlock(Attribute, 0, Block);
1243}
1244
Jim Laskey90c79d72006-03-23 23:02:34 +00001245/// getDieMapSlotFor - Returns the debug information entry map slot for the
1246/// specified debug descriptor.
1247DIE *&DwarfWriter::getDieMapSlotFor(DebugInfoDesc *DD) {
1248 return DescToDieMap[DD];
Jim Laskey0420f2a2006-02-22 19:02:11 +00001249}
Jim Laskey760383e2006-08-21 21:20:18 +00001250
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001251/// NewType - Create a new type DIE.
1252///
Jim Laskey90c79d72006-03-23 23:02:34 +00001253DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit) {
1254 if (!TyDesc) {
1255 // FIXME - Hack for missing types
1256 DIE *Die = new DIE(DW_TAG_base_type);
1257 Die->AddUInt(DW_AT_byte_size, 0, 4);
1258 Die->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1259 Unit->getDie()->AddChild(Die);
1260 return Die;
1261 }
Jim Laskey92ae7402006-03-01 18:20:30 +00001262
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001263 // Check for pre-existence.
Jim Laskey90c79d72006-03-23 23:02:34 +00001264 DIE *&Slot = Unit->getDieMapSlotFor(TyDesc);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001265 if (Slot) return Slot;
1266
1267 // Get core information.
1268 const std::string &Name = TyDesc->getName();
Jim Laskey288fe0f2006-03-01 18:13:05 +00001269 uint64_t Size = TyDesc->getSize() >> 3;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001270
Jim Laskey434b40b2006-02-23 22:37:30 +00001271 DIE *Ty = NULL;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001272
Jim Laskey434b40b2006-02-23 22:37:30 +00001273 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001274 // Fundamental types like int, float, bool
Jim Laskey434b40b2006-02-23 22:37:30 +00001275 Slot = Ty = new DIE(DW_TAG_base_type);
1276 unsigned Encoding = BasicTy->getEncoding();
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001277 Ty->AddUInt(DW_AT_encoding, DW_FORM_data1, Encoding);
Jim Laskey69906002006-02-24 16:46:40 +00001278 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001279 // Create specific DIE.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001280 Slot = Ty = new DIE(DerivedTy->getTag());
Jim Laskey69906002006-02-24 16:46:40 +00001281
1282 // Map to main type, void will not have a type.
1283 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001284 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1285 NewType(Context, FromTy, Unit));
Jim Laskey69906002006-02-24 16:46:40 +00001286 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001287 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
Jim Laskey7089f452006-06-16 13:14:03 +00001288 // Fetch tag
1289 unsigned Tag = CompTy->getTag();
1290
Jim Laskeyf8913f12006-03-01 17:53:02 +00001291 // Create specific DIE.
Jim Laskey7089f452006-06-16 13:14:03 +00001292 Slot = Ty = Tag == DW_TAG_vector_type ? new DIE(DW_TAG_array_type) :
1293 new DIE(Tag);
1294
Jim Laskeyf8913f12006-03-01 17:53:02 +00001295 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1296
Jim Laskey7089f452006-06-16 13:14:03 +00001297 switch (Tag) {
1298 case DW_TAG_vector_type: Ty->AddUInt(DW_AT_GNU_vector, DW_FORM_flag, 1);
1299 // Fall thru
Jim Laskey9c4447a2006-03-01 20:39:36 +00001300 case DW_TAG_array_type: {
Jim Laskeyf8913f12006-03-01 17:53:02 +00001301 // Add element type.
1302 if (TypeDesc *FromTy = CompTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001303 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1304 NewType(Context, FromTy, Unit));
Jim Laskeyf8913f12006-03-01 17:53:02 +00001305 }
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001306
Jim Laskeyf8913f12006-03-01 17:53:02 +00001307 // Don't emit size attribute.
1308 Size = 0;
1309
1310 // Construct an anonymous type for index type.
1311 DIE *IndexTy = new DIE(DW_TAG_base_type);
1312 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1313 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1314 // Add to context.
Jim Laskey92ae7402006-03-01 18:20:30 +00001315 Context->AddChild(IndexTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001316
1317 // Add subranges to array type.
1318 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1319 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1320 int64_t Lo = SRD->getLo();
1321 int64_t Hi = SRD->getHi();
1322 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1323
1324 // If a range is available.
1325 if (Lo != Hi) {
1326 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1327 // Only add low if non-zero.
Jim Laskey6a3eb012006-03-01 23:52:37 +00001328 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1329 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001330 }
1331 Ty->AddChild(Subrange);
1332 }
1333
1334 break;
1335 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001336 case DW_TAG_structure_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +00001337 case DW_TAG_union_type: {
Jim Laskeyf01e5472006-03-03 15:06:57 +00001338 // Add elements to structure type.
1339 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskey760383e2006-08-21 21:20:18 +00001340 DebugInfoDesc *Element = Elements[i];
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001341
Jim Laskey760383e2006-08-21 21:20:18 +00001342 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)) {
1343 // Add field or base class.
Jim Laskey54689c22006-03-09 13:28:47 +00001344
Jim Laskey760383e2006-08-21 21:20:18 +00001345 unsigned Tag = MemberDesc->getTag();
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001346
Jim Laskey760383e2006-08-21 21:20:18 +00001347 // Extract the basic information.
1348 const std::string &Name = MemberDesc->getName();
1349 TypeDesc *MemTy = MemberDesc->getFromType();
1350 uint64_t Size = MemberDesc->getSize();
1351 uint64_t Align = MemberDesc->getAlign();
1352 uint64_t Offset = MemberDesc->getOffset();
1353
1354 // Construct member debug information entry.
1355 DIE *Member = new DIE(Tag);
1356
1357 // Add name if not "".
1358 if (!Name.empty())Member->AddString(DW_AT_name, DW_FORM_string, Name);
1359 // Add location if available.
1360 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1361
1362 // Most of the time the field info is the same as the members.
1363 uint64_t FieldSize = Size;
1364 uint64_t FieldAlign = Align;
1365 uint64_t FieldOffset = Offset;
1366
1367 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1368 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1369 NewType(Context, FromTy, Unit));
1370 FieldSize = FromTy->getSize();
1371 FieldAlign = FromTy->getSize();
1372 }
1373
1374 // Unless we have a bit field.
1375 if (Tag == DW_TAG_member && FieldSize != Size) {
1376 // Construct the alignment mask.
1377 uint64_t AlignMask = ~(FieldAlign - 1);
1378 // Determine the high bit + 1 of the declared size.
1379 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1380 // Work backwards to determine the base offset of the field.
1381 FieldOffset = HiMark - FieldSize;
1382 // Now normalize offset to the field.
1383 Offset -= FieldOffset;
1384
1385 // Maybe we need to work from the other end.
1386 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1387
1388 // Add size and offset.
1389 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1390 Member->AddUInt(DW_AT_bit_size, 0, Size);
1391 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
1392 }
1393
1394 // Add computation for offset.
1395 DIEBlock *Block = new DIEBlock();
1396 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
1397 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
1398 Block->ComputeSize(*this);
1399 Member->AddBlock(DW_AT_data_member_location, 0, Block);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001400
Jim Laskey760383e2006-08-21 21:20:18 +00001401 // Add accessibility (public default unless is base class.
1402 if (MemberDesc->isProtected()) {
1403 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_protected);
1404 } else if (MemberDesc->isPrivate()) {
1405 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_private);
1406 } else if (Tag == DW_TAG_inheritance) {
1407 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_public);
1408 }
1409
1410 Ty->AddChild(Member);
1411 } else if (GlobalVariableDesc *StaticDesc =
1412 dyn_cast<GlobalVariableDesc>(Element)) {
1413 // Add static member.
1414
1415 // Construct member debug information entry.
1416 DIE *Static = new DIE(DW_TAG_variable);
1417
1418 // Add name and mangled name.
1419 const std::string &Name = StaticDesc->getDisplayName();
1420 const std::string &MangledName = StaticDesc->getName();
1421 Static->AddString(DW_AT_name, DW_FORM_string, Name);
1422 Static->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1423 MangledName);
1424
1425 // Add location.
1426 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1427
1428 // Add type.
1429 if (TypeDesc *StaticTy = StaticDesc->getType()) {
1430 Static->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1431 NewType(Context, StaticTy, Unit));
1432 }
1433
1434 // Add flags.
1435 Static->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1436 Static->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1437
1438 Ty->AddChild(Static);
1439 } else if (SubprogramDesc *MethodDesc =
1440 dyn_cast<SubprogramDesc>(Element)) {
1441 // Add member function.
1442
1443 // Construct member debug information entry.
1444 DIE *Method = new DIE(DW_TAG_subprogram);
1445
1446 // Add name and mangled name.
1447 const std::string &Name = MethodDesc->getDisplayName();
1448 const std::string &MangledName = MethodDesc->getName();
1449 bool IsCTor = false;
1450
1451 if (Name.empty()) {
1452 Method->AddString(DW_AT_name, DW_FORM_string, MangledName);
1453 IsCTor = TyDesc->getName() == MangledName;
1454 } else {
1455 Method->AddString(DW_AT_name, DW_FORM_string, Name);
1456 Method->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1457 MangledName);
1458 }
1459
1460 // Add location.
1461 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1462
1463 // Add type.
1464 if (CompositeTypeDesc *MethodTy =
1465 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1466 // Get argument information.
1467 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1468
1469 // If not a ctor.
1470 if (!IsCTor) {
1471 // Add return type.
1472 Method->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1473 NewType(Context, dyn_cast<TypeDesc>(Args[0]),
1474 Unit));
1475 }
1476
1477 // Add arguments.
1478 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1479 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1480 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1481 NewType(Context, cast<TypeDesc>(Args[i]), Unit));
1482 Arg->AddUInt(DW_AT_artificial, DW_FORM_flag, 1);
1483 Method->AddChild(Arg);
1484 }
1485 }
1486
1487 // Add flags.
1488 Method->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1489 Method->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1490
1491 Ty->AddChild(Method);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001492 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001493 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001494 break;
1495 }
Jim Laskey9c4447a2006-03-01 20:39:36 +00001496 case DW_TAG_enumeration_type: {
Jim Laskey6a3eb012006-03-01 23:52:37 +00001497 // Add enumerators to enumeration type.
1498 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1499 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1500 const std::string &Name = ED->getName();
1501 int64_t Value = ED->getValue();
1502 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1503 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1504 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1505 Ty->AddChild(Enumerator);
1506 }
1507
Jim Laskeyf8913f12006-03-01 17:53:02 +00001508 break;
1509 }
Jim Laskey650f6092006-06-20 19:41:06 +00001510 case DW_TAG_subroutine_type: {
1511 // Add prototype flag.
1512 Ty->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
1513 // Add return type.
1514 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
Jim Laskeyd04c1592006-07-13 15:27:42 +00001515 NewType(Context, dyn_cast<TypeDesc>(Elements[0]), Unit));
Jim Laskey650f6092006-06-20 19:41:06 +00001516
1517 // Add arguments.
1518 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1519 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1520 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1521 NewType(Context, cast<TypeDesc>(Elements[i]), Unit));
1522 Ty->AddChild(Arg);
1523 }
1524
1525 break;
1526 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001527 default: break;
1528 }
Jim Laskey0f77fe52006-10-04 01:43:13 +00001529 } else if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1530 Slot = Ty = new DIE(DW_TAG_pointer_type);
1531 Ty->AddUInt(DW_AT_byte_size, 0, TAI->getAddressSize());
1532 Ty->AddString(DW_AT_name, DW_FORM_string, SubprogramTy->getName());
1533 Context->AddChild(Ty);
1534 return Slot;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001535 }
Jim Laskey434b40b2006-02-23 22:37:30 +00001536
1537 assert(Ty && "Type not supported yet");
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001538
Jim Laskey69906002006-02-24 16:46:40 +00001539 // Add size if non-zero (derived types don't have a size.)
Jim Laskey434b40b2006-02-23 22:37:30 +00001540 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
Jim Laskey69906002006-02-24 16:46:40 +00001541 // Add name if not anonymous or intermediate type.
Jim Laskey434b40b2006-02-23 22:37:30 +00001542 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001543 // Add source line info if available.
1544 AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
Jim Laskey434b40b2006-02-23 22:37:30 +00001545
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001546 // Add to context owner.
Jim Laskey92ae7402006-03-01 18:20:30 +00001547 Context->AddChild(Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001548
1549 return Slot;
1550}
1551
Jim Laskeyb8509c52006-03-23 18:07:55 +00001552/// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey063e7652006-01-17 17:31:53 +00001553///
Jim Laskeybd761842006-02-27 17:27:12 +00001554CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1555 unsigned ID) {
1556 // Construct debug information entry.
1557 DIE *Die = new DIE(DW_TAG_compile_unit);
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001558 Die->AddDelta (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0),
1559 DWLabel("section_line", 0));
Jim Laskey89d67fa2006-06-23 12:51:53 +00001560// Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1561// Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
Jim Laskeybd761842006-02-27 17:27:12 +00001562 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1563 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1564 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1565 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1566
Jim Laskeyb8509c52006-03-23 18:07:55 +00001567 // Add debug information entry to descriptor map.
Jim Laskey90c79d72006-03-23 23:02:34 +00001568 DIE *&Slot = getDieMapSlotFor(UnitDesc);
1569 Slot = Die;
Jim Laskeybd761842006-02-27 17:27:12 +00001570
1571 // Construct compile unit.
1572 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1573
1574 // Add Unit to compile unit map.
1575 DescToUnitMap[UnitDesc] = Unit;
1576
1577 return Unit;
1578}
Jim Laskey0420f2a2006-02-22 19:02:11 +00001579
Jim Laskeybd761842006-02-27 17:27:12 +00001580/// FindCompileUnit - Get the compile unit for the given descriptor.
1581///
1582CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1583 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1584 assert(Unit && "Missing compile unit.");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001585 return Unit;
Jim Laskey063e7652006-01-17 17:31:53 +00001586}
1587
Jim Laskey0420f2a2006-02-22 19:02:11 +00001588/// NewGlobalVariable - Add a new global variable DIE.
1589///
1590DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001591 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001592 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1593 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey90c79d72006-03-23 23:02:34 +00001594
1595 // Check for pre-existence.
1596 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1597 if (Slot) return Slot;
1598
Jim Laskey0420f2a2006-02-22 19:02:11 +00001599 // Get the global variable itself.
1600 GlobalVariable *GV = GVD->getGlobalVariable();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001601
Jim Laskeye85fb672006-09-18 14:47:26 +00001602 const std::string &Name = GVD->hasMangledName() ? GVD->getDisplayName()
1603 : GVD->getName();
1604 const std::string &MangledName = GVD->hasMangledName() ? GVD->getName()
1605 : "";
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001606 // Get the global's type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001607 DIE *Type = NewType(Unit->getDie(), GVD->getType(), Unit);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001608
1609 // Create the globale variable DIE.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001610 DIE *VariableDie = new DIE(DW_TAG_variable);
Jim Laskeye85fb672006-09-18 14:47:26 +00001611 VariableDie->AddString(DW_AT_name, DW_FORM_string, Name);
1612 if (!MangledName.empty()) {
1613 VariableDie->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1614 MangledName);
1615 }
1616 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1617 VariableDie->AddUInt(DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001618
1619 // Add source line info if available.
1620 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001621
Jim Laskeyb8509c52006-03-23 18:07:55 +00001622 // Add address.
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001623 DIEBlock *Block = new DIEBlock();
1624 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
1625 Block->AddObjectLabel(DW_FORM_udata, MangledName);
1626 Block->ComputeSize(*this);
1627 VariableDie->AddBlock(DW_AT_location, 0, Block);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001628
1629 // Add to map.
1630 Slot = VariableDie;
1631
1632 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001633 Unit->getDie()->AddChild(VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001634
1635 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001636 // FIXME - need to check external flag.
1637 Unit->AddGlobal(Name, VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001638
1639 return VariableDie;
1640}
1641
1642/// NewSubprogram - Add a new subprogram DIE.
1643///
1644DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001645 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001646 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1647 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001648
Jim Laskey90c79d72006-03-23 23:02:34 +00001649 // Check for pre-existence.
1650 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1651 if (Slot) return Slot;
1652
Jim Laskey0420f2a2006-02-22 19:02:11 +00001653 // Gather the details (simplify add attribute code.)
Jim Laskeye85fb672006-09-18 14:47:26 +00001654 const std::string &Name = SPD->hasMangledName() ? SPD->getDisplayName()
1655 : SPD->getName();
1656 const std::string &MangledName = SPD->hasMangledName() ? SPD->getName()
1657 : "";
Jim Laskey90c79d72006-03-23 23:02:34 +00001658 DIE *Type = NewType(Unit->getDie(), SPD->getType(), Unit);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001659 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001660
Jim Laskey8a8e9752006-02-27 20:37:42 +00001661 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
Jim Laskeye85fb672006-09-18 14:47:26 +00001662 SubprogramDie->AddString(DW_AT_name, DW_FORM_string, Name);
1663 if (!MangledName.empty()) {
1664 SubprogramDie->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1665 MangledName);
Jim Laskey41886992006-04-07 16:34:46 +00001666 }
Jim Laskeye85fb672006-09-18 14:47:26 +00001667 if (Type) {
1668 SubprogramDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1669 }
1670 SubprogramDie->AddUInt(DW_AT_external, DW_FORM_flag, IsExternal);
1671 SubprogramDie->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001672
Jim Laskeyb8509c52006-03-23 18:07:55 +00001673 // Add source line info if available.
1674 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1675
Jim Laskey0420f2a2006-02-22 19:02:11 +00001676 // Add to map.
1677 Slot = SubprogramDie;
1678
1679 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001680 Unit->getDie()->AddChild(SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001681
1682 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001683 Unit->AddGlobal(Name, SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001684
1685 return SubprogramDie;
1686}
1687
Jim Laskeyb8509c52006-03-23 18:07:55 +00001688/// NewScopeVariable - Create a new scope variable.
1689///
1690DIE *DwarfWriter::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1691 // Get the descriptor.
1692 VariableDesc *VD = DV->getDesc();
1693
1694 // Translate tag to proper Dwarf tag. The result variable is dropped for now.
1695 unsigned Tag;
1696 switch (VD->getTag()) {
1697 case DW_TAG_return_variable: return NULL;
1698 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1699 case DW_TAG_auto_variable: // fall thru
1700 default: Tag = DW_TAG_variable; break;
1701 }
1702
1703 // Define variable debug information entry.
1704 DIE *VariableDie = new DIE(Tag);
1705 VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
1706
1707 // Add source line info if available.
1708 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1709
1710 // Add variable type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001711 DIE *Type = NewType(Unit->getDie(), VD->getType(), Unit);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001712 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1713
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001714 // Add variable address.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001715 MachineLocation Location;
Jim Laskey41886992006-04-07 16:34:46 +00001716 RI->getLocation(*MF, DV->getFrameIndex(), Location);
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001717 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001718
1719 return VariableDie;
1720}
1721
1722/// ConstructScope - Construct the components of a scope.
1723///
1724void DwarfWriter::ConstructScope(DebugScope *ParentScope,
1725 DIE *ParentDie, CompileUnit *Unit) {
1726 // Add variables to scope.
1727 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1728 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1729 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1730 if (VariableDie) ParentDie->AddChild(VariableDie);
1731 }
1732
1733 // Add nested scopes.
1734 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1735 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1736 // Define the Scope debug information entry.
1737 DebugScope *Scope = Scopes[j];
1738 // FIXME - Ignore inlined functions for the time being.
Jim Laskeyd16f2a72006-06-19 19:49:42 +00001739 if (!Scope->getParent()) continue;
Jim Laskeyb8509c52006-03-23 18:07:55 +00001740
1741 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1742
1743 // Add the scope bounds.
1744 if (unsigned StartID = Scope->getStartLabelID()) {
1745 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1746 DWLabel("loc", StartID));
1747 } else {
1748 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1749 DWLabel("func_begin", SubprogramCount));
1750 }
1751 if (unsigned EndID = Scope->getEndLabelID()) {
1752 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1753 DWLabel("loc", EndID));
1754 } else {
1755 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1756 DWLabel("func_end", SubprogramCount));
1757 }
1758
1759 // Add the scope contents.
1760 ConstructScope(Scope, ScopeDie, Unit);
1761 ParentDie->AddChild(ScopeDie);
1762 }
1763}
1764
1765/// ConstructRootScope - Construct the scope for the subprogram.
1766///
1767void DwarfWriter::ConstructRootScope(DebugScope *RootScope) {
1768 // Exit if there is no root scope.
1769 if (!RootScope) return;
1770
1771 // Get the subprogram debug information entry.
1772 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Jim Laskey90c79d72006-03-23 23:02:34 +00001773
1774 // Get the compile unit context.
1775 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey41886992006-04-07 16:34:46 +00001776 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1777
Jim Laskey90c79d72006-03-23 23:02:34 +00001778 // Get the subprogram die.
1779 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001780 assert(SPDie && "Missing subprogram descriptor");
1781
1782 // Add the function bounds.
Jim Laskey6b92b8e2006-04-07 20:44:42 +00001783 SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1784 DWLabel("func_begin", SubprogramCount));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001785 SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1786 DWLabel("func_end", SubprogramCount));
Jim Laskey41886992006-04-07 16:34:46 +00001787 MachineLocation Location(RI->getFrameRegister(*MF));
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001788 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001789
Jim Laskeyb8509c52006-03-23 18:07:55 +00001790 ConstructScope(RootScope, SPDie, Unit);
1791}
1792
Jim Laskey063e7652006-01-17 17:31:53 +00001793/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1794/// tools to recognize the object file contains Dwarf information.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001795///
Jim Laskey014f98c2006-06-14 11:35:03 +00001796void DwarfWriter::EmitInitial() {
1797 // Check to see if we already emitted intial headers.
1798 if (didInitial) return;
1799 didInitial = true;
1800
Jim Laskey063e7652006-01-17 17:31:53 +00001801 // Dwarf sections base addresses.
Jim Laskey563321a2006-09-06 18:34:40 +00001802 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001803 EmitLabel("section_frame", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001804 Asm->SwitchToDataSection(TAI->getDwarfInfoSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001805 EmitLabel("section_info", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001806 EmitLabel("info", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001807 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001808 EmitLabel("section_abbrev", 0);
1809 EmitLabel("abbrev", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001810 Asm->SwitchToDataSection(TAI->getDwarfARangesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001811 EmitLabel("section_aranges", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001812 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001813 EmitLabel("section_macinfo", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001814 Asm->SwitchToDataSection(TAI->getDwarfLineSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001815 EmitLabel("section_line", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001816 EmitLabel("line", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001817 Asm->SwitchToDataSection(TAI->getDwarfLocSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001818 EmitLabel("section_loc", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001819 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001820 EmitLabel("section_pubnames", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001821 Asm->SwitchToDataSection(TAI->getDwarfStrSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001822 EmitLabel("section_str", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001823 Asm->SwitchToDataSection(TAI->getDwarfRangesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001824 EmitLabel("section_ranges", 0);
1825
Jim Laskey563321a2006-09-06 18:34:40 +00001826 Asm->SwitchToTextSection(TAI->getTextSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001827 EmitLabel("text_begin", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001828 Asm->SwitchToDataSection(TAI->getDataSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001829 EmitLabel("data_begin", 0);
Jim Laskey89d67fa2006-06-23 12:51:53 +00001830
Jim Laskey014f98c2006-06-14 11:35:03 +00001831 // Emit common frame information.
1832 EmitInitialDebugFrame();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001833}
1834
Jim Laskey063e7652006-01-17 17:31:53 +00001835/// EmitDIE - Recusively Emits a debug information entry.
1836///
1837void DwarfWriter::EmitDIE(DIE *Die) const {
1838 // Get the abbreviation for this DIE.
1839 unsigned AbbrevID = Die->getAbbrevID();
1840 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey0d086af2006-02-27 12:43:29 +00001841
1842 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001843
1844 // Emit the code (index) for the abbreviation.
1845 EmitULEB128Bytes(AbbrevID);
1846 EOL(std::string("Abbrev [" +
1847 utostr(AbbrevID) +
Jim Laskey0d086af2006-02-27 12:43:29 +00001848 "] 0x" + utohexstr(Die->getOffset()) +
1849 ":0x" + utohexstr(Die->getSize()) + " " +
1850 TagString(Abbrev.getTag())));
Jim Laskey063e7652006-01-17 17:31:53 +00001851
1852 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001853 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001854
1855 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001856 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001857 unsigned Attr = AbbrevData[i].getAttribute();
1858 unsigned Form = AbbrevData[i].getForm();
Jim Laskey063e7652006-01-17 17:31:53 +00001859 assert(Form && "Too many attributes for DIE (check abbreviation)");
1860
1861 switch (Attr) {
1862 case DW_AT_sibling: {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001863 EmitInt32(Die->SiblingOffset());
Jim Laskey063e7652006-01-17 17:31:53 +00001864 break;
1865 }
1866 default: {
1867 // Emit an attribute using the defined form.
1868 Values[i]->EmitValue(*this, Form);
1869 break;
1870 }
1871 }
1872
1873 EOL(AttributeString(Attr));
1874 }
1875
1876 // Emit the DIE children if any.
1877 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1878 const std::vector<DIE *> &Children = Die->getChildren();
1879
Jim Laskey52060a02006-01-24 00:49:18 +00001880 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskey063e7652006-01-17 17:31:53 +00001881 EmitDIE(Children[j]);
1882 }
1883
Jim Laskeyda427fa2006-01-27 20:31:25 +00001884 EmitInt8(0); EOL("End Of Children Mark");
Jim Laskey063e7652006-01-17 17:31:53 +00001885 }
1886}
1887
1888/// SizeAndOffsetDie - Compute the size and offset of a DIE.
1889///
Jim Laskeyb8509c52006-03-23 18:07:55 +00001890unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1891 // Get the children.
1892 const std::vector<DIE *> &Children = Die->getChildren();
1893
1894 // If not last sibling and has children then add sibling offset attribute.
1895 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1896
Jim Laskey0420f2a2006-02-22 19:02:11 +00001897 // Record the abbreviation.
1898 Die->Complete(*this);
1899
Jim Laskey063e7652006-01-17 17:31:53 +00001900 // Get the abbreviation for this DIE.
1901 unsigned AbbrevID = Die->getAbbrevID();
1902 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1903
1904 // Set DIE offset
1905 Die->setOffset(Offset);
1906
1907 // Start the size with the size of abbreviation code.
1908 Offset += SizeULEB128(AbbrevID);
1909
1910 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001911 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001912
1913 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001914 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001915 // Size attribute value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001916 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
Jim Laskey063e7652006-01-17 17:31:53 +00001917 }
1918
1919 // Emit the DIE children if any.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001920 if (!Children.empty()) {
1921 assert(Abbrev.getChildrenFlag() == DW_CHILDREN_yes &&
1922 "Children flag not set");
Jim Laskey063e7652006-01-17 17:31:53 +00001923
Jim Laskey52060a02006-01-24 00:49:18 +00001924 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001925 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
Jim Laskey063e7652006-01-17 17:31:53 +00001926 }
1927
1928 // End of children marker.
1929 Offset += sizeof(int8_t);
1930 }
1931
1932 Die->setSize(Offset - Die->getOffset());
1933 return Offset;
1934}
1935
1936/// SizeAndOffsets - Compute the size and offset of all the DIEs.
1937///
1938void DwarfWriter::SizeAndOffsets() {
Jim Laskey063e7652006-01-17 17:31:53 +00001939
1940 // Process each compile unit.
Jim Laskey52060a02006-01-24 00:49:18 +00001941 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00001942 CompileUnit *Unit = CompileUnits[i];
1943 if (Unit->hasContent()) {
1944 // Compute size of compile unit header
1945 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1946 sizeof(int16_t) + // DWARF version number
1947 sizeof(int32_t) + // Offset Into Abbrev. Section
1948 sizeof(int8_t); // Pointer Size (in bytes)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001949 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
Jim Laskeybd761842006-02-27 17:27:12 +00001950 }
Jim Laskey063e7652006-01-17 17:31:53 +00001951 }
1952}
1953
Jim Laskey41886992006-04-07 16:34:46 +00001954/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1955/// frame.
1956void DwarfWriter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
1957 std::vector<MachineMove *> &Moves) {
1958 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1959 MachineMove *Move = Moves[i];
1960 unsigned LabelID = Move->getLabelID();
1961 const MachineLocation &Dst = Move->getDestination();
1962 const MachineLocation &Src = Move->getSource();
1963
1964 // Advance row if new location.
1965 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
Jim Laskeyce50a162006-08-29 16:24:26 +00001966 EmitInt8(DW_CFA_advance_loc4);
Jim Laskey41886992006-04-07 16:34:46 +00001967 EOL("DW_CFA_advance_loc4");
1968 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
1969 EOL("");
1970
1971 BaseLabelID = LabelID;
1972 BaseLabel = "loc";
1973 }
1974
Jim Laskeyce50a162006-08-29 16:24:26 +00001975 int stackGrowth =
1976 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1977 TargetFrameInfo::StackGrowsUp ?
Jim Laskey563321a2006-09-06 18:34:40 +00001978 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskeyce50a162006-08-29 16:24:26 +00001979
Jim Laskey41886992006-04-07 16:34:46 +00001980 // If advancing cfa.
1981 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
1982 if (!Src.isRegister()) {
1983 if (Src.getRegister() == MachineLocation::VirtualFP) {
Jim Laskeyce50a162006-08-29 16:24:26 +00001984 EmitInt8(DW_CFA_def_cfa_offset);
Jim Laskey41886992006-04-07 16:34:46 +00001985 EOL("DW_CFA_def_cfa_offset");
1986 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00001987 EmitInt8(DW_CFA_def_cfa);
Jim Laskey41886992006-04-07 16:34:46 +00001988 EOL("DW_CFA_def_cfa");
1989
1990 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
1991 EOL("Register");
1992 }
Jim Laskey1069fbd2006-04-10 23:09:19 +00001993
Jim Laskeyce50a162006-08-29 16:24:26 +00001994 int Offset = Src.getOffset() / stackGrowth;
Jim Laskey1069fbd2006-04-10 23:09:19 +00001995
Jim Laskeyce50a162006-08-29 16:24:26 +00001996 EmitULEB128Bytes(Offset);
Jim Laskey41886992006-04-07 16:34:46 +00001997 EOL("Offset");
1998 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00001999 assert(0 && "Machine move no supported yet.");
Jim Laskey41886992006-04-07 16:34:46 +00002000 }
2001 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00002002 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
2003 int Offset = Dst.getOffset() / stackGrowth;
2004
2005 if (Offset < 0) {
2006 EmitInt8(DW_CFA_offset_extended_sf);
2007 EOL("DW_CFA_offset_extended_sf");
2008 EmitULEB128Bytes(Reg);
2009 EOL("Reg");
2010 EmitSLEB128Bytes(Offset);
2011 EOL("Offset");
2012 } else if (Reg < 64) {
2013 EmitInt8(DW_CFA_offset + Reg);
2014 EOL("DW_CFA_offset + Reg");
2015 EmitULEB128Bytes(Offset);
2016 EOL("Offset");
2017 } else {
2018 EmitInt8(DW_CFA_offset_extended);
2019 EOL("DW_CFA_offset_extended");
2020 EmitULEB128Bytes(Reg);
2021 EOL("Reg");
2022 EmitULEB128Bytes(Offset);
2023 EOL("Offset");
2024 }
Jim Laskey41886992006-04-07 16:34:46 +00002025 }
2026 }
2027}
2028
Jim Laskey063e7652006-01-17 17:31:53 +00002029/// EmitDebugInfo - Emit the debug info section.
2030///
2031void DwarfWriter::EmitDebugInfo() const {
2032 // Start debug info section.
Jim Laskey563321a2006-09-06 18:34:40 +00002033 Asm->SwitchToDataSection(TAI->getDwarfInfoSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002034
Jim Laskeybd761842006-02-27 17:27:12 +00002035 // Process each compile unit.
2036 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2037 CompileUnit *Unit = CompileUnits[i];
2038
2039 if (Unit->hasContent()) {
2040 DIE *Die = Unit->getDie();
Jim Laskey0d086af2006-02-27 12:43:29 +00002041 // Emit the compile units header.
Jim Laskeybd761842006-02-27 17:27:12 +00002042 EmitLabel("info_begin", Unit->getID());
Jim Laskey0d086af2006-02-27 12:43:29 +00002043 // Emit size of content not including length itself
Jim Laskeybd761842006-02-27 17:27:12 +00002044 unsigned ContentSize = Die->getSize() +
Jim Laskey0d086af2006-02-27 12:43:29 +00002045 sizeof(int16_t) + // DWARF version number
2046 sizeof(int32_t) + // Offset Into Abbrev. Section
2047 sizeof(int8_t); // Pointer Size (in bytes)
2048
2049 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2050 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskeyf8a01a92006-06-15 20:51:43 +00002051 EmitDifference("abbrev_begin", 0, "section_abbrev", 0);
2052 EOL("Offset Into Abbrev. Section");
Jim Laskey563321a2006-09-06 18:34:40 +00002053 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
Jim Laskey0d086af2006-02-27 12:43:29 +00002054
Jim Laskeybd761842006-02-27 17:27:12 +00002055 EmitDIE(Die);
2056 EmitLabel("info_end", Unit->getID());
Jim Laskey063e7652006-01-17 17:31:53 +00002057 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002058
2059 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002060 }
2061}
2062
2063/// EmitAbbreviations - Emit the abbreviation section.
2064///
2065void DwarfWriter::EmitAbbreviations() const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002066 // Check to see if it is worth the effort.
2067 if (!Abbreviations.empty()) {
2068 // Start the debug abbrev section.
Jim Laskey563321a2006-09-06 18:34:40 +00002069 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002070
Jim Laskeyd18e2892006-01-20 20:34:06 +00002071 EmitLabel("abbrev_begin", 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002072
Jim Laskeyd18e2892006-01-20 20:34:06 +00002073 // For each abbrevation.
2074 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002075 AbbrevID <= NAID; ++AbbrevID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002076 // Get abbreviation data
2077 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey063e7652006-01-17 17:31:53 +00002078
Jim Laskeyd18e2892006-01-20 20:34:06 +00002079 // Emit the abbrevations code (base 1 index.)
2080 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
Jim Laskey063e7652006-01-17 17:31:53 +00002081
Jim Laskeyd18e2892006-01-20 20:34:06 +00002082 // Emit the abbreviations data.
2083 Abbrev.Emit(*this);
Jim Laskey0d086af2006-02-27 12:43:29 +00002084
2085 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002086 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002087
2088 EmitLabel("abbrev_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002089
2090 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002091 }
2092}
2093
2094/// EmitDebugLines - Emit source line information.
2095///
2096void DwarfWriter::EmitDebugLines() const {
2097 // Minimum line delta, thus ranging from -10..(255-10).
2098 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2099 // Maximum line delta, thus ranging from -10..(255-10).
2100 const int MaxLineDelta = 255 + MinLineDelta;
2101
2102 // Start the dwarf line section.
Jim Laskey563321a2006-09-06 18:34:40 +00002103 Asm->SwitchToDataSection(TAI->getDwarfLineSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002104
2105 // Construct the section header.
2106
2107 EmitDifference("line_end", 0, "line_begin", 0);
2108 EOL("Length of Source Line Info");
2109 EmitLabel("line_begin", 0);
2110
Jim Laskeyda427fa2006-01-27 20:31:25 +00002111 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey063e7652006-01-17 17:31:53 +00002112
2113 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
2114 EOL("Prolog Length");
2115 EmitLabel("line_prolog_begin", 0);
2116
Jim Laskeyda427fa2006-01-27 20:31:25 +00002117 EmitInt8(1); EOL("Minimum Instruction Length");
Jim Laskey063e7652006-01-17 17:31:53 +00002118
Jim Laskeyda427fa2006-01-27 20:31:25 +00002119 EmitInt8(1); EOL("Default is_stmt_start flag");
Jim Laskey063e7652006-01-17 17:31:53 +00002120
Jim Laskeyda427fa2006-01-27 20:31:25 +00002121 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00002122
Jim Laskeyda427fa2006-01-27 20:31:25 +00002123 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00002124
Jim Laskeyda427fa2006-01-27 20:31:25 +00002125 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Jim Laskey063e7652006-01-17 17:31:53 +00002126
2127 // Line number standard opcode encodings argument count
Jim Laskeyda427fa2006-01-27 20:31:25 +00002128 EmitInt8(0); EOL("DW_LNS_copy arg count");
2129 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2130 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2131 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2132 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2133 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2134 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2135 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2136 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskey063e7652006-01-17 17:31:53 +00002137
2138 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2139 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
2140
2141 // Emit directories.
2142 for (unsigned DirectoryID = 1, NDID = Directories.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002143 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskey063e7652006-01-17 17:31:53 +00002144 EmitString(Directories[DirectoryID]); EOL("Directory");
2145 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00002146 EmitInt8(0); EOL("End of directories");
Jim Laskey063e7652006-01-17 17:31:53 +00002147
2148 // Emit files.
2149 for (unsigned SourceID = 1, NSID = SourceFiles.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002150 SourceID <= NSID; ++SourceID) {
Jim Laskey063e7652006-01-17 17:31:53 +00002151 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2152 EmitString(SourceFile.getName()); EOL("Source");
2153 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2154 EmitULEB128Bytes(0); EOL("Mod date");
2155 EmitULEB128Bytes(0); EOL("File size");
2156 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00002157 EmitInt8(0); EOL("End of files");
Jim Laskey063e7652006-01-17 17:31:53 +00002158
2159 EmitLabel("line_prolog_end", 0);
2160
Jim Laskey89d67fa2006-06-23 12:51:53 +00002161 // A sequence for each text section.
2162 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2163 // Isolate current sections line info.
2164 const std::vector<SourceLineInfo *> &LineInfos = SectionSourceLines[j];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002165
2166 if (DwarfVerbose) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002167 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00002168 << TAI->getCommentString() << " "
Jim Laskey89d67fa2006-06-23 12:51:53 +00002169 << "Section "
2170 << SectionMap[j + 1].c_str() << "\n";
Jim Laskey0420f2a2006-02-22 19:02:11 +00002171 }
Jim Laskey063e7652006-01-17 17:31:53 +00002172
Jim Laskey89d67fa2006-06-23 12:51:53 +00002173 // Dwarf assumes we start with first line of first source file.
2174 unsigned Source = 1;
2175 unsigned Line = 1;
2176
2177 // Construct rows of the address, source, line, column matrix.
2178 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2179 SourceLineInfo *LineInfo = LineInfos[i];
2180
2181 if (DwarfVerbose) {
2182 unsigned SourceID = LineInfo->getSourceID();
2183 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2184 unsigned DirectoryID = SourceFile.getDirectoryID();
2185 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00002186 << TAI->getCommentString() << " "
Jim Laskey89d67fa2006-06-23 12:51:53 +00002187 << Directories[DirectoryID]
2188 << SourceFile.getName() << ":"
2189 << LineInfo->getLine() << "\n";
2190 }
2191
2192 // Define the line address.
2193 EmitInt8(0); EOL("Extended Op");
2194 EmitInt8(4 + 1); EOL("Op size");
2195 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2196 EmitReference("loc", LineInfo->getLabelID()); EOL("Location label");
2197
2198 // If change of source, then switch to the new source.
2199 if (Source != LineInfo->getSourceID()) {
2200 Source = LineInfo->getSourceID();
2201 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2202 EmitULEB128Bytes(Source); EOL("New Source");
2203 }
2204
2205 // If change of line.
2206 if (Line != LineInfo->getLine()) {
2207 // Determine offset.
2208 int Offset = LineInfo->getLine() - Line;
2209 int Delta = Offset - MinLineDelta;
2210
2211 // Update line.
2212 Line = LineInfo->getLine();
2213
2214 // If delta is small enough and in range...
2215 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2216 // ... then use fast opcode.
2217 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2218 } else {
2219 // ... otherwise use long hand.
2220 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2221 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2222 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2223 }
2224 } else {
2225 // Copy the previous row (different address or source)
2226 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2227 }
2228 }
2229
2230 // Define last address of section.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002231 EmitInt8(0); EOL("Extended Op");
2232 EmitInt8(4 + 1); EOL("Op size");
2233 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskey89d67fa2006-06-23 12:51:53 +00002234 EmitReference("section_end", j + 1); EOL("Section end label");
2235
2236 // Mark end of matrix.
2237 EmitInt8(0); EOL("DW_LNE_end_sequence");
2238 EmitULEB128Bytes(1); O << "\n";
2239 EmitInt8(1); O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002240 }
Jim Laskey063e7652006-01-17 17:31:53 +00002241
2242 EmitLabel("line_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002243
2244 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002245}
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002246
Jim Laskey41886992006-04-07 16:34:46 +00002247/// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002248///
Jim Laskey41886992006-04-07 16:34:46 +00002249void DwarfWriter::EmitInitialDebugFrame() {
Jim Laskey1069fbd2006-04-10 23:09:19 +00002250 int stackGrowth =
2251 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2252 TargetFrameInfo::StackGrowsUp ?
Jim Laskey563321a2006-09-06 18:34:40 +00002253 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskey1069fbd2006-04-10 23:09:19 +00002254
Jim Laskey41886992006-04-07 16:34:46 +00002255 // Start the dwarf frame section.
Jim Laskey563321a2006-09-06 18:34:40 +00002256 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002257
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002258 EmitLabel("frame_common", 0);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002259 EmitDifference("frame_common_end", 0,
2260 "frame_common_begin", 0);
2261 EOL("Length of Common Information Entry");
2262
2263 EmitLabel("frame_common_begin", 0);
2264 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2265 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2266 EmitString(""); EOL("CIE Augmentation");
2267 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
Jim Laskey1069fbd2006-04-10 23:09:19 +00002268 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
Jim Laskey41886992006-04-07 16:34:46 +00002269 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
2270
2271 std::vector<MachineMove *> Moves;
2272 RI->getInitialFrameState(Moves);
2273 EmitFrameMoves(NULL, 0, Moves);
2274 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2275
Jim Laskeyb8509c52006-03-23 18:07:55 +00002276 EmitAlign(2);
2277 EmitLabel("frame_common_end", 0);
2278
2279 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002280}
2281
Jim Laskey41886992006-04-07 16:34:46 +00002282/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2283/// section.
2284void DwarfWriter::EmitFunctionDebugFrame() {
2285 // Start the dwarf frame section.
Jim Laskey563321a2006-09-06 18:34:40 +00002286 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskey41886992006-04-07 16:34:46 +00002287
2288 EmitDifference("frame_end", SubprogramCount,
2289 "frame_begin", SubprogramCount);
2290 EOL("Length of Frame Information Entry");
2291
2292 EmitLabel("frame_begin", SubprogramCount);
2293
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002294 EmitDifference("frame_common", 0, "section_frame", 0);
2295 EOL("FDE CIE offset");
Jim Laskey41886992006-04-07 16:34:46 +00002296
2297 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2298 EmitDifference("func_end", SubprogramCount,
2299 "func_begin", SubprogramCount);
2300 EOL("FDE address range");
2301
2302 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2303
2304 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2305
2306 EmitAlign(2);
2307 EmitLabel("frame_end", SubprogramCount);
2308
2309 O << "\n";
2310}
2311
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002312/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2313///
2314void DwarfWriter::EmitDebugPubNames() {
Jim Laskeybd761842006-02-27 17:27:12 +00002315 // Start the dwarf pubnames section.
Jim Laskey563321a2006-09-06 18:34:40 +00002316 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection(), 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002317
Jim Laskeybd761842006-02-27 17:27:12 +00002318 // Process each compile unit.
2319 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2320 CompileUnit *Unit = CompileUnits[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00002321
Jim Laskeybd761842006-02-27 17:27:12 +00002322 if (Unit->hasContent()) {
2323 EmitDifference("pubnames_end", Unit->getID(),
2324 "pubnames_begin", Unit->getID());
2325 EOL("Length of Public Names Info");
2326
2327 EmitLabel("pubnames_begin", Unit->getID());
2328
2329 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2330
Jim Laskey067ef412006-06-19 15:48:00 +00002331 EmitDifference("info_begin", Unit->getID(), "section_info", 0);
Jim Laskeybd761842006-02-27 17:27:12 +00002332 EOL("Offset of Compilation Unit Info");
Jim Laskeyd18e2892006-01-20 20:34:06 +00002333
Jim Laskeybd761842006-02-27 17:27:12 +00002334 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2335 EOL("Compilation Unit Length");
2336
2337 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2338
2339 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2340 GE = Globals.end();
2341 GI != GE; ++GI) {
2342 const std::string &Name = GI->first;
2343 DIE * Entity = GI->second;
2344
2345 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2346 EmitString(Name); EOL("External Name");
2347 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002348
Jim Laskeybd761842006-02-27 17:27:12 +00002349 EmitInt32(0); EOL("End Mark");
2350 EmitLabel("pubnames_end", Unit->getID());
2351
2352 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002353 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002354 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002355}
2356
2357/// EmitDebugStr - Emit visible names into a debug str section.
2358///
2359void DwarfWriter::EmitDebugStr() {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002360 // Check to see if it is worth the effort.
2361 if (!StringPool.empty()) {
2362 // Start the dwarf str section.
Jim Laskey563321a2006-09-06 18:34:40 +00002363 Asm->SwitchToDataSection(TAI->getDwarfStrSection(), 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002364
Jim Laskeyb8509c52006-03-23 18:07:55 +00002365 // For each of strings in the string pool.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002366 for (unsigned StringID = 1, N = StringPool.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002367 StringID <= N; ++StringID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002368 // Emit a label for reference from debug information entries.
2369 EmitLabel("string", StringID);
2370 // Emit the string itself.
2371 const std::string &String = StringPool[StringID];
2372 EmitString(String); O << "\n";
2373 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002374
2375 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002376 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002377}
2378
2379/// EmitDebugLoc - Emit visible names into a debug loc section.
2380///
2381void DwarfWriter::EmitDebugLoc() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002382 // Start the dwarf loc section.
Jim Laskey563321a2006-09-06 18:34:40 +00002383 Asm->SwitchToDataSection(TAI->getDwarfLocSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002384
2385 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002386}
2387
2388/// EmitDebugARanges - Emit visible names into a debug aranges section.
2389///
2390void DwarfWriter::EmitDebugARanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002391 // Start the dwarf aranges section.
Jim Laskey563321a2006-09-06 18:34:40 +00002392 Asm->SwitchToDataSection(TAI->getDwarfARangesSection(), 0);
Jim Laskeye719a7c2006-01-18 16:54:26 +00002393
2394 // FIXME - Mock up
Jim Laskeybd761842006-02-27 17:27:12 +00002395#if 0
2396 // Process each compile unit.
2397 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2398 CompileUnit *Unit = CompileUnits[i];
2399
2400 if (Unit->hasContent()) {
2401 // Don't include size of length
2402 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2403
2404 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2405
2406 EmitReference("info_begin", Unit->getID());
2407 EOL("Offset of Compilation Unit Info");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002408
Jim Laskey563321a2006-09-06 18:34:40 +00002409 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002410
Jim Laskeybd761842006-02-27 17:27:12 +00002411 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002412
Jim Laskeybd761842006-02-27 17:27:12 +00002413 EmitInt16(0); EOL("Pad (1)");
2414 EmitInt16(0); EOL("Pad (2)");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002415
Jim Laskeybd761842006-02-27 17:27:12 +00002416 // Range 1
2417 EmitReference("text_begin", 0); EOL("Address");
2418 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002419
Jim Laskeybd761842006-02-27 17:27:12 +00002420 EmitInt32(0); EOL("EOM (1)");
2421 EmitInt32(0); EOL("EOM (2)");
2422
2423 O << "\n";
2424 }
2425 }
2426#endif
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002427}
2428
2429/// EmitDebugRanges - Emit visible names into a debug ranges section.
2430///
2431void DwarfWriter::EmitDebugRanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002432 // Start the dwarf ranges section.
Jim Laskey563321a2006-09-06 18:34:40 +00002433 Asm->SwitchToDataSection(TAI->getDwarfRangesSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002434
2435 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002436}
2437
2438/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2439///
2440void DwarfWriter::EmitDebugMacInfo() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002441 // Start the dwarf macinfo section.
Jim Laskey563321a2006-09-06 18:34:40 +00002442 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002443
2444 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002445}
Jim Laskey063e7652006-01-17 17:31:53 +00002446
Jim Laskey52060a02006-01-24 00:49:18 +00002447/// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2448/// header file.
2449void DwarfWriter::ConstructCompileUnitDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002450 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
Jim Laskey6e87c0e2006-01-26 21:22:49 +00002451
2452 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00002453 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
Jim Laskey52060a02006-01-24 00:49:18 +00002454 CompileUnits.push_back(Unit);
2455 }
2456}
2457
2458/// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2459/// variables.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002460void DwarfWriter::ConstructGlobalDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002461 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002462 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00002463
2464 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002465 GlobalVariableDesc *GVD = GlobalVariables[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002466 NewGlobalVariable(GVD);
Jim Laskey52060a02006-01-24 00:49:18 +00002467 }
2468}
2469
Jim Laskey0420f2a2006-02-22 19:02:11 +00002470/// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2471/// subprograms.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002472void DwarfWriter::ConstructSubprogramDIEs() {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002473 std::vector<SubprogramDesc *> Subprograms =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002474 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002475
2476 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2477 SubprogramDesc *SPD = Subprograms[i];
2478 NewSubprogram(SPD);
2479 }
2480}
Jim Laskey52060a02006-01-24 00:49:18 +00002481
Jim Laskey063e7652006-01-17 17:31:53 +00002482//===----------------------------------------------------------------------===//
Jim Laskeyd18e2892006-01-20 20:34:06 +00002483// Main entry points.
Jim Laskey063e7652006-01-17 17:31:53 +00002484//
Jim Laskey52060a02006-01-24 00:49:18 +00002485
Jim Laskeya0f3d172006-09-07 22:06:40 +00002486DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
2487 const TargetAsmInfo *T)
Jim Laskey52060a02006-01-24 00:49:18 +00002488: O(OS)
2489, Asm(A)
Jim Laskey563321a2006-09-06 18:34:40 +00002490, TAI(T)
Jim Laskey41886992006-04-07 16:34:46 +00002491, TD(Asm->TM.getTargetData())
2492, RI(Asm->TM.getRegisterInfo())
Jim Laskeyb8509c52006-03-23 18:07:55 +00002493, M(NULL)
2494, MF(NULL)
Jim Laskey52060a02006-01-24 00:49:18 +00002495, DebugInfo(NULL)
2496, didInitial(false)
Jim Laskey014f98c2006-06-14 11:35:03 +00002497, shouldEmit(false)
Jim Laskeyb8509c52006-03-23 18:07:55 +00002498, SubprogramCount(0)
Jim Laskey52060a02006-01-24 00:49:18 +00002499, CompileUnits()
2500, Abbreviations()
Jim Laskey52060a02006-01-24 00:49:18 +00002501, StringPool()
Jim Laskeybd761842006-02-27 17:27:12 +00002502, DescToUnitMap()
Jim Laskey0420f2a2006-02-22 19:02:11 +00002503, DescToDieMap()
Jim Laskey89d67fa2006-06-23 12:51:53 +00002504, SectionMap()
2505, SectionSourceLines()
Jim Laskey52060a02006-01-24 00:49:18 +00002506{}
2507DwarfWriter::~DwarfWriter() {
2508 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2509 delete CompileUnits[i];
Jim Laskey063e7652006-01-17 17:31:53 +00002510 }
Jim Laskey52060a02006-01-24 00:49:18 +00002511}
Jim Laskey063e7652006-01-17 17:31:53 +00002512
Jim Laskey41886992006-04-07 16:34:46 +00002513/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2514/// created it. Set by the target AsmPrinter.
2515void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
Jim Laskey014f98c2006-06-14 11:35:03 +00002516 // Make sure initial declarations are made.
2517 if (!DebugInfo && DI->hasInfo()) {
2518 DebugInfo = DI;
2519 shouldEmit = true;
2520
2521 // Emit initial sections
2522 EmitInitial();
2523
2524 // Create all the compile unit DIEs.
2525 ConstructCompileUnitDIEs();
2526
2527 // Create DIEs for each of the externally visible global variables.
2528 ConstructGlobalDIEs();
2529
2530 // Create DIEs for each of the externally visible subprograms.
2531 ConstructSubprogramDIEs();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002532
2533 // Prime section data.
Jim Laskey563321a2006-09-06 18:34:40 +00002534 SectionMap.insert(std::string("\t") + TAI->getTextSection());
Jim Laskey014f98c2006-06-14 11:35:03 +00002535 }
Jim Laskey41886992006-04-07 16:34:46 +00002536}
2537
Jim Laskey063e7652006-01-17 17:31:53 +00002538/// BeginModule - Emit all Dwarf sections that should come prior to the content.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002539///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002540void DwarfWriter::BeginModule(Module *M) {
2541 this->M = M;
2542
Jim Laskeyb2efb852006-01-04 22:28:25 +00002543 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002544 EOL("Dwarf Begin Module");
Jim Laskeyb2efb852006-01-04 22:28:25 +00002545}
2546
Jim Laskey063e7652006-01-17 17:31:53 +00002547/// EndModule - Emit all Dwarf sections that should come after the content.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002548///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002549void DwarfWriter::EndModule() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002550 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002551 EOL("Dwarf End Module");
2552
2553 // Standard sections final addresses.
Jim Laskey563321a2006-09-06 18:34:40 +00002554 Asm->SwitchToTextSection(TAI->getTextSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002555 EmitLabel("text_end", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002556 Asm->SwitchToDataSection(TAI->getDataSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002557 EmitLabel("data_end", 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002558
Jim Laskey89d67fa2006-06-23 12:51:53 +00002559 // End text sections.
2560 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2561 Asm->SwitchToTextSection(SectionMap[i].c_str(), 0);
2562 EmitLabel("section_end", i);
2563 }
2564
Jim Laskey063e7652006-01-17 17:31:53 +00002565 // Compute DIE offsets and sizes.
2566 SizeAndOffsets();
2567
2568 // Emit all the DIEs into a debug info section
2569 EmitDebugInfo();
2570
2571 // Corresponding abbreviations into a abbrev section.
2572 EmitAbbreviations();
2573
2574 // Emit source line correspondence into a debug line section.
2575 EmitDebugLines();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002576
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002577 // Emit info into a debug pubnames section.
2578 EmitDebugPubNames();
2579
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002580 // Emit info into a debug str section.
2581 EmitDebugStr();
2582
2583 // Emit info into a debug loc section.
2584 EmitDebugLoc();
2585
2586 // Emit info into a debug aranges section.
2587 EmitDebugARanges();
2588
2589 // Emit info into a debug ranges section.
2590 EmitDebugRanges();
2591
2592 // Emit info into a debug macinfo section.
2593 EmitDebugMacInfo();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002594}
2595
Jim Laskey6b92b8e2006-04-07 20:44:42 +00002596/// BeginFunction - Gather pre-function debug information. Assumes being
2597/// emitted immediately after the function entry point.
Jim Laskey89d67fa2006-06-23 12:51:53 +00002598void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002599 this->MF = MF;
2600
Jim Laskey89d67fa2006-06-23 12:51:53 +00002601 if (!ShouldEmitDwarf()) return;
2602 EOL("Dwarf Begin Function");
2603
2604 // Begin accumulating function debug information.
2605 DebugInfo->BeginFunction(MF);
Jim Laskey41886992006-04-07 16:34:46 +00002606
Jim Laskey89d67fa2006-06-23 12:51:53 +00002607 // Assumes in correct section after the entry point.
2608 EmitLabel("func_begin", ++SubprogramCount);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002609}
2610
Jim Laskeye719a7c2006-01-18 16:54:26 +00002611/// EndFunction - Gather and emit post-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002612///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002613void DwarfWriter::EndFunction() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002614 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002615 EOL("Dwarf End Function");
Jim Laskeyb8509c52006-03-23 18:07:55 +00002616
Jim Laskey89d67fa2006-06-23 12:51:53 +00002617 // Define end label for subprogram.
2618 EmitLabel("func_end", SubprogramCount);
Jim Laskey014f98c2006-06-14 11:35:03 +00002619
Jim Laskey89d67fa2006-06-23 12:51:53 +00002620 // Get function line info.
2621 std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
2622
2623 if (!LineInfos.empty()) {
2624 // Get section line info.
2625 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2626 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2627 std::vector<SourceLineInfo *> &SectionLineInfos =SectionSourceLines[ID-1];
2628 // Append the function info to section info.
2629 SectionLineInfos.insert(SectionLineInfos.end(),
2630 LineInfos.begin(), LineInfos.end());
Jim Laskey014f98c2006-06-14 11:35:03 +00002631 }
Jim Laskey41886992006-04-07 16:34:46 +00002632
Jim Laskey89d67fa2006-06-23 12:51:53 +00002633 // Construct scopes for subprogram.
2634 ConstructRootScope(DebugInfo->getRootScope());
2635
2636 // Emit function frame information.
2637 EmitFunctionDebugFrame();
2638
2639 // Reset the line numbers for the next function.
2640 LineInfos.clear();
2641
Jim Laskey41886992006-04-07 16:34:46 +00002642 // Clear function debug information.
2643 DebugInfo->EndFunction();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002644}