blob: aa393f3678486e494b6fa18fc277042caf89a90c [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 Laskey0c0feb92006-10-04 10:40:15 +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
Jim Laskey0c0feb92006-10-04 10:40:15 +00001267 // Type DIE result.
1268 DIE *Ty = NULL;
1269
1270 // Short cut for handling subprogram types (not really a TyDesc.)
1271 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1272 Slot = Ty = new DIE(DW_TAG_pointer_type);
1273 Ty->AddUInt(DW_AT_byte_size, 0, TAI->getAddressSize());
1274 Ty->AddString(DW_AT_name, DW_FORM_string, SubprogramTy->getName());
1275 Context->AddChild(Ty);
1276 return Slot;
1277 }
1278
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001279 // Get core information.
1280 const std::string &Name = TyDesc->getName();
Jim Laskey288fe0f2006-03-01 18:13:05 +00001281 uint64_t Size = TyDesc->getSize() >> 3;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001282
Jim Laskey434b40b2006-02-23 22:37:30 +00001283 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001284 // Fundamental types like int, float, bool
Jim Laskey434b40b2006-02-23 22:37:30 +00001285 Slot = Ty = new DIE(DW_TAG_base_type);
1286 unsigned Encoding = BasicTy->getEncoding();
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001287 Ty->AddUInt(DW_AT_encoding, DW_FORM_data1, Encoding);
Jim Laskey69906002006-02-24 16:46:40 +00001288 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001289 // Create specific DIE.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001290 Slot = Ty = new DIE(DerivedTy->getTag());
Jim Laskey69906002006-02-24 16:46:40 +00001291
1292 // Map to main type, void will not have a type.
1293 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001294 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1295 NewType(Context, FromTy, Unit));
Jim Laskey69906002006-02-24 16:46:40 +00001296 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001297 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
Jim Laskey7089f452006-06-16 13:14:03 +00001298 // Fetch tag
1299 unsigned Tag = CompTy->getTag();
1300
Jim Laskeyf8913f12006-03-01 17:53:02 +00001301 // Create specific DIE.
Jim Laskey7089f452006-06-16 13:14:03 +00001302 Slot = Ty = Tag == DW_TAG_vector_type ? new DIE(DW_TAG_array_type) :
1303 new DIE(Tag);
1304
Jim Laskeyf8913f12006-03-01 17:53:02 +00001305 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1306
Jim Laskey7089f452006-06-16 13:14:03 +00001307 switch (Tag) {
1308 case DW_TAG_vector_type: Ty->AddUInt(DW_AT_GNU_vector, DW_FORM_flag, 1);
1309 // Fall thru
Jim Laskey9c4447a2006-03-01 20:39:36 +00001310 case DW_TAG_array_type: {
Jim Laskeyf8913f12006-03-01 17:53:02 +00001311 // Add element type.
1312 if (TypeDesc *FromTy = CompTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001313 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1314 NewType(Context, FromTy, Unit));
Jim Laskeyf8913f12006-03-01 17:53:02 +00001315 }
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001316
Jim Laskeyf8913f12006-03-01 17:53:02 +00001317 // Don't emit size attribute.
1318 Size = 0;
1319
1320 // Construct an anonymous type for index type.
1321 DIE *IndexTy = new DIE(DW_TAG_base_type);
1322 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1323 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1324 // Add to context.
Jim Laskey92ae7402006-03-01 18:20:30 +00001325 Context->AddChild(IndexTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001326
1327 // Add subranges to array type.
1328 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1329 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1330 int64_t Lo = SRD->getLo();
1331 int64_t Hi = SRD->getHi();
1332 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1333
1334 // If a range is available.
1335 if (Lo != Hi) {
1336 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1337 // Only add low if non-zero.
Jim Laskey6a3eb012006-03-01 23:52:37 +00001338 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1339 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001340 }
1341 Ty->AddChild(Subrange);
1342 }
1343
1344 break;
1345 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001346 case DW_TAG_structure_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +00001347 case DW_TAG_union_type: {
Jim Laskeyf01e5472006-03-03 15:06:57 +00001348 // Add elements to structure type.
1349 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskey760383e2006-08-21 21:20:18 +00001350 DebugInfoDesc *Element = Elements[i];
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001351
Jim Laskey760383e2006-08-21 21:20:18 +00001352 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)) {
1353 // Add field or base class.
Jim Laskey54689c22006-03-09 13:28:47 +00001354
Jim Laskey760383e2006-08-21 21:20:18 +00001355 unsigned Tag = MemberDesc->getTag();
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001356
Jim Laskey760383e2006-08-21 21:20:18 +00001357 // Extract the basic information.
1358 const std::string &Name = MemberDesc->getName();
1359 TypeDesc *MemTy = MemberDesc->getFromType();
1360 uint64_t Size = MemberDesc->getSize();
1361 uint64_t Align = MemberDesc->getAlign();
1362 uint64_t Offset = MemberDesc->getOffset();
1363
1364 // Construct member debug information entry.
1365 DIE *Member = new DIE(Tag);
1366
1367 // Add name if not "".
1368 if (!Name.empty())Member->AddString(DW_AT_name, DW_FORM_string, Name);
1369 // Add location if available.
1370 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1371
1372 // Most of the time the field info is the same as the members.
1373 uint64_t FieldSize = Size;
1374 uint64_t FieldAlign = Align;
1375 uint64_t FieldOffset = Offset;
1376
1377 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1378 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1379 NewType(Context, FromTy, Unit));
1380 FieldSize = FromTy->getSize();
1381 FieldAlign = FromTy->getSize();
1382 }
1383
1384 // Unless we have a bit field.
1385 if (Tag == DW_TAG_member && FieldSize != Size) {
1386 // Construct the alignment mask.
1387 uint64_t AlignMask = ~(FieldAlign - 1);
1388 // Determine the high bit + 1 of the declared size.
1389 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1390 // Work backwards to determine the base offset of the field.
1391 FieldOffset = HiMark - FieldSize;
1392 // Now normalize offset to the field.
1393 Offset -= FieldOffset;
1394
1395 // Maybe we need to work from the other end.
1396 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1397
1398 // Add size and offset.
1399 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1400 Member->AddUInt(DW_AT_bit_size, 0, Size);
1401 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
1402 }
1403
1404 // Add computation for offset.
1405 DIEBlock *Block = new DIEBlock();
1406 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
1407 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
1408 Block->ComputeSize(*this);
1409 Member->AddBlock(DW_AT_data_member_location, 0, Block);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001410
Jim Laskey760383e2006-08-21 21:20:18 +00001411 // Add accessibility (public default unless is base class.
1412 if (MemberDesc->isProtected()) {
1413 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_protected);
1414 } else if (MemberDesc->isPrivate()) {
1415 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_private);
1416 } else if (Tag == DW_TAG_inheritance) {
1417 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_public);
1418 }
1419
1420 Ty->AddChild(Member);
1421 } else if (GlobalVariableDesc *StaticDesc =
1422 dyn_cast<GlobalVariableDesc>(Element)) {
1423 // Add static member.
1424
1425 // Construct member debug information entry.
1426 DIE *Static = new DIE(DW_TAG_variable);
1427
1428 // Add name and mangled name.
1429 const std::string &Name = StaticDesc->getDisplayName();
1430 const std::string &MangledName = StaticDesc->getName();
1431 Static->AddString(DW_AT_name, DW_FORM_string, Name);
1432 Static->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1433 MangledName);
1434
1435 // Add location.
1436 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1437
1438 // Add type.
1439 if (TypeDesc *StaticTy = StaticDesc->getType()) {
1440 Static->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1441 NewType(Context, StaticTy, Unit));
1442 }
1443
1444 // Add flags.
1445 Static->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1446 Static->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1447
1448 Ty->AddChild(Static);
1449 } else if (SubprogramDesc *MethodDesc =
1450 dyn_cast<SubprogramDesc>(Element)) {
1451 // Add member function.
1452
1453 // Construct member debug information entry.
1454 DIE *Method = new DIE(DW_TAG_subprogram);
1455
1456 // Add name and mangled name.
1457 const std::string &Name = MethodDesc->getDisplayName();
1458 const std::string &MangledName = MethodDesc->getName();
1459 bool IsCTor = false;
1460
1461 if (Name.empty()) {
1462 Method->AddString(DW_AT_name, DW_FORM_string, MangledName);
1463 IsCTor = TyDesc->getName() == MangledName;
1464 } else {
1465 Method->AddString(DW_AT_name, DW_FORM_string, Name);
1466 Method->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1467 MangledName);
1468 }
1469
1470 // Add location.
1471 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1472
1473 // Add type.
1474 if (CompositeTypeDesc *MethodTy =
1475 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1476 // Get argument information.
1477 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1478
1479 // If not a ctor.
1480 if (!IsCTor) {
1481 // Add return type.
1482 Method->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1483 NewType(Context, dyn_cast<TypeDesc>(Args[0]),
1484 Unit));
1485 }
1486
1487 // Add arguments.
1488 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1489 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1490 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1491 NewType(Context, cast<TypeDesc>(Args[i]), Unit));
1492 Arg->AddUInt(DW_AT_artificial, DW_FORM_flag, 1);
1493 Method->AddChild(Arg);
1494 }
1495 }
1496
1497 // Add flags.
1498 Method->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1499 Method->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1500
1501 Ty->AddChild(Method);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001502 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001503 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001504 break;
1505 }
Jim Laskey9c4447a2006-03-01 20:39:36 +00001506 case DW_TAG_enumeration_type: {
Jim Laskey6a3eb012006-03-01 23:52:37 +00001507 // Add enumerators to enumeration type.
1508 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1509 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1510 const std::string &Name = ED->getName();
1511 int64_t Value = ED->getValue();
1512 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1513 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1514 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1515 Ty->AddChild(Enumerator);
1516 }
1517
Jim Laskeyf8913f12006-03-01 17:53:02 +00001518 break;
1519 }
Jim Laskey650f6092006-06-20 19:41:06 +00001520 case DW_TAG_subroutine_type: {
1521 // Add prototype flag.
1522 Ty->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
1523 // Add return type.
1524 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
Jim Laskeyd04c1592006-07-13 15:27:42 +00001525 NewType(Context, dyn_cast<TypeDesc>(Elements[0]), Unit));
Jim Laskey650f6092006-06-20 19:41:06 +00001526
1527 // Add arguments.
1528 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1529 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1530 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1531 NewType(Context, cast<TypeDesc>(Elements[i]), Unit));
1532 Ty->AddChild(Arg);
1533 }
1534
1535 break;
1536 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001537 default: break;
1538 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001539 }
Jim Laskey0c0feb92006-10-04 10:40:15 +00001540
Jim Laskey434b40b2006-02-23 22:37:30 +00001541 assert(Ty && "Type not supported yet");
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001542
Jim Laskey69906002006-02-24 16:46:40 +00001543 // Add size if non-zero (derived types don't have a size.)
Jim Laskey434b40b2006-02-23 22:37:30 +00001544 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
Jim Laskey69906002006-02-24 16:46:40 +00001545 // Add name if not anonymous or intermediate type.
Jim Laskey434b40b2006-02-23 22:37:30 +00001546 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001547 // Add source line info if available.
1548 AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
Jim Laskey434b40b2006-02-23 22:37:30 +00001549
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001550 // Add to context owner.
Jim Laskey92ae7402006-03-01 18:20:30 +00001551 Context->AddChild(Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001552
1553 return Slot;
1554}
1555
Jim Laskeyb8509c52006-03-23 18:07:55 +00001556/// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey063e7652006-01-17 17:31:53 +00001557///
Jim Laskeybd761842006-02-27 17:27:12 +00001558CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1559 unsigned ID) {
1560 // Construct debug information entry.
1561 DIE *Die = new DIE(DW_TAG_compile_unit);
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001562 Die->AddDelta (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0),
1563 DWLabel("section_line", 0));
Jim Laskey89d67fa2006-06-23 12:51:53 +00001564// Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1565// Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
Jim Laskeybd761842006-02-27 17:27:12 +00001566 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1567 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1568 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1569 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1570
Jim Laskeyb8509c52006-03-23 18:07:55 +00001571 // Add debug information entry to descriptor map.
Jim Laskey90c79d72006-03-23 23:02:34 +00001572 DIE *&Slot = getDieMapSlotFor(UnitDesc);
1573 Slot = Die;
Jim Laskeybd761842006-02-27 17:27:12 +00001574
1575 // Construct compile unit.
1576 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1577
1578 // Add Unit to compile unit map.
1579 DescToUnitMap[UnitDesc] = Unit;
1580
1581 return Unit;
1582}
Jim Laskey0420f2a2006-02-22 19:02:11 +00001583
Jim Laskeybd761842006-02-27 17:27:12 +00001584/// FindCompileUnit - Get the compile unit for the given descriptor.
1585///
1586CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1587 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1588 assert(Unit && "Missing compile unit.");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001589 return Unit;
Jim Laskey063e7652006-01-17 17:31:53 +00001590}
1591
Jim Laskey0420f2a2006-02-22 19:02:11 +00001592/// NewGlobalVariable - Add a new global variable DIE.
1593///
1594DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001595 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001596 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1597 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey90c79d72006-03-23 23:02:34 +00001598
1599 // Check for pre-existence.
1600 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1601 if (Slot) return Slot;
1602
Jim Laskey0420f2a2006-02-22 19:02:11 +00001603 // Get the global variable itself.
1604 GlobalVariable *GV = GVD->getGlobalVariable();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001605
Jim Laskeye85fb672006-09-18 14:47:26 +00001606 const std::string &Name = GVD->hasMangledName() ? GVD->getDisplayName()
1607 : GVD->getName();
1608 const std::string &MangledName = GVD->hasMangledName() ? GVD->getName()
1609 : "";
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001610 // Get the global's type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001611 DIE *Type = NewType(Unit->getDie(), GVD->getType(), Unit);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001612
1613 // Create the globale variable DIE.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001614 DIE *VariableDie = new DIE(DW_TAG_variable);
Jim Laskeye85fb672006-09-18 14:47:26 +00001615 VariableDie->AddString(DW_AT_name, DW_FORM_string, Name);
1616 if (!MangledName.empty()) {
1617 VariableDie->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1618 MangledName);
1619 }
1620 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1621 VariableDie->AddUInt(DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001622
1623 // Add source line info if available.
1624 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001625
Jim Laskeyb8509c52006-03-23 18:07:55 +00001626 // Add address.
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001627 DIEBlock *Block = new DIEBlock();
1628 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
1629 Block->AddObjectLabel(DW_FORM_udata, MangledName);
1630 Block->ComputeSize(*this);
1631 VariableDie->AddBlock(DW_AT_location, 0, Block);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001632
1633 // Add to map.
1634 Slot = VariableDie;
1635
1636 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001637 Unit->getDie()->AddChild(VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001638
1639 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001640 // FIXME - need to check external flag.
1641 Unit->AddGlobal(Name, VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001642
1643 return VariableDie;
1644}
1645
1646/// NewSubprogram - Add a new subprogram DIE.
1647///
1648DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001649 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001650 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1651 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001652
Jim Laskey90c79d72006-03-23 23:02:34 +00001653 // Check for pre-existence.
1654 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1655 if (Slot) return Slot;
1656
Jim Laskey0420f2a2006-02-22 19:02:11 +00001657 // Gather the details (simplify add attribute code.)
Jim Laskeye85fb672006-09-18 14:47:26 +00001658 const std::string &Name = SPD->hasMangledName() ? SPD->getDisplayName()
1659 : SPD->getName();
1660 const std::string &MangledName = SPD->hasMangledName() ? SPD->getName()
1661 : "";
Jim Laskey90c79d72006-03-23 23:02:34 +00001662 DIE *Type = NewType(Unit->getDie(), SPD->getType(), Unit);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001663 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001664
Jim Laskey8a8e9752006-02-27 20:37:42 +00001665 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
Jim Laskeye85fb672006-09-18 14:47:26 +00001666 SubprogramDie->AddString(DW_AT_name, DW_FORM_string, Name);
1667 if (!MangledName.empty()) {
1668 SubprogramDie->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1669 MangledName);
Jim Laskey41886992006-04-07 16:34:46 +00001670 }
Jim Laskeye85fb672006-09-18 14:47:26 +00001671 if (Type) {
1672 SubprogramDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1673 }
1674 SubprogramDie->AddUInt(DW_AT_external, DW_FORM_flag, IsExternal);
1675 SubprogramDie->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001676
Jim Laskeyb8509c52006-03-23 18:07:55 +00001677 // Add source line info if available.
1678 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1679
Jim Laskey0420f2a2006-02-22 19:02:11 +00001680 // Add to map.
1681 Slot = SubprogramDie;
1682
1683 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001684 Unit->getDie()->AddChild(SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001685
1686 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001687 Unit->AddGlobal(Name, SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001688
1689 return SubprogramDie;
1690}
1691
Jim Laskeyb8509c52006-03-23 18:07:55 +00001692/// NewScopeVariable - Create a new scope variable.
1693///
1694DIE *DwarfWriter::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1695 // Get the descriptor.
1696 VariableDesc *VD = DV->getDesc();
1697
1698 // Translate tag to proper Dwarf tag. The result variable is dropped for now.
1699 unsigned Tag;
1700 switch (VD->getTag()) {
1701 case DW_TAG_return_variable: return NULL;
1702 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1703 case DW_TAG_auto_variable: // fall thru
1704 default: Tag = DW_TAG_variable; break;
1705 }
1706
1707 // Define variable debug information entry.
1708 DIE *VariableDie = new DIE(Tag);
1709 VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
1710
1711 // Add source line info if available.
1712 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1713
1714 // Add variable type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001715 DIE *Type = NewType(Unit->getDie(), VD->getType(), Unit);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001716 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1717
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001718 // Add variable address.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001719 MachineLocation Location;
Jim Laskey41886992006-04-07 16:34:46 +00001720 RI->getLocation(*MF, DV->getFrameIndex(), Location);
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001721 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001722
1723 return VariableDie;
1724}
1725
1726/// ConstructScope - Construct the components of a scope.
1727///
1728void DwarfWriter::ConstructScope(DebugScope *ParentScope,
1729 DIE *ParentDie, CompileUnit *Unit) {
1730 // Add variables to scope.
1731 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1732 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1733 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1734 if (VariableDie) ParentDie->AddChild(VariableDie);
1735 }
1736
1737 // Add nested scopes.
1738 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1739 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1740 // Define the Scope debug information entry.
1741 DebugScope *Scope = Scopes[j];
1742 // FIXME - Ignore inlined functions for the time being.
Jim Laskeyd16f2a72006-06-19 19:49:42 +00001743 if (!Scope->getParent()) continue;
Jim Laskeyb8509c52006-03-23 18:07:55 +00001744
1745 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1746
1747 // Add the scope bounds.
1748 if (unsigned StartID = Scope->getStartLabelID()) {
1749 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1750 DWLabel("loc", StartID));
1751 } else {
1752 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1753 DWLabel("func_begin", SubprogramCount));
1754 }
1755 if (unsigned EndID = Scope->getEndLabelID()) {
1756 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1757 DWLabel("loc", EndID));
1758 } else {
1759 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1760 DWLabel("func_end", SubprogramCount));
1761 }
1762
1763 // Add the scope contents.
1764 ConstructScope(Scope, ScopeDie, Unit);
1765 ParentDie->AddChild(ScopeDie);
1766 }
1767}
1768
1769/// ConstructRootScope - Construct the scope for the subprogram.
1770///
1771void DwarfWriter::ConstructRootScope(DebugScope *RootScope) {
1772 // Exit if there is no root scope.
1773 if (!RootScope) return;
1774
1775 // Get the subprogram debug information entry.
1776 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Jim Laskey90c79d72006-03-23 23:02:34 +00001777
1778 // Get the compile unit context.
1779 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey41886992006-04-07 16:34:46 +00001780 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1781
Jim Laskey90c79d72006-03-23 23:02:34 +00001782 // Get the subprogram die.
1783 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001784 assert(SPDie && "Missing subprogram descriptor");
1785
1786 // Add the function bounds.
Jim Laskey6b92b8e2006-04-07 20:44:42 +00001787 SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1788 DWLabel("func_begin", SubprogramCount));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001789 SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1790 DWLabel("func_end", SubprogramCount));
Jim Laskey41886992006-04-07 16:34:46 +00001791 MachineLocation Location(RI->getFrameRegister(*MF));
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001792 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001793
Jim Laskeyb8509c52006-03-23 18:07:55 +00001794 ConstructScope(RootScope, SPDie, Unit);
1795}
1796
Jim Laskey063e7652006-01-17 17:31:53 +00001797/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1798/// tools to recognize the object file contains Dwarf information.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001799///
Jim Laskey014f98c2006-06-14 11:35:03 +00001800void DwarfWriter::EmitInitial() {
1801 // Check to see if we already emitted intial headers.
1802 if (didInitial) return;
1803 didInitial = true;
1804
Jim Laskey063e7652006-01-17 17:31:53 +00001805 // Dwarf sections base addresses.
Jim Laskey563321a2006-09-06 18:34:40 +00001806 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001807 EmitLabel("section_frame", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001808 Asm->SwitchToDataSection(TAI->getDwarfInfoSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001809 EmitLabel("section_info", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001810 EmitLabel("info", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001811 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001812 EmitLabel("section_abbrev", 0);
1813 EmitLabel("abbrev", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001814 Asm->SwitchToDataSection(TAI->getDwarfARangesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001815 EmitLabel("section_aranges", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001816 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001817 EmitLabel("section_macinfo", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001818 Asm->SwitchToDataSection(TAI->getDwarfLineSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001819 EmitLabel("section_line", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001820 EmitLabel("line", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001821 Asm->SwitchToDataSection(TAI->getDwarfLocSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001822 EmitLabel("section_loc", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001823 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001824 EmitLabel("section_pubnames", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001825 Asm->SwitchToDataSection(TAI->getDwarfStrSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001826 EmitLabel("section_str", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001827 Asm->SwitchToDataSection(TAI->getDwarfRangesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001828 EmitLabel("section_ranges", 0);
1829
Jim Laskey563321a2006-09-06 18:34:40 +00001830 Asm->SwitchToTextSection(TAI->getTextSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001831 EmitLabel("text_begin", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001832 Asm->SwitchToDataSection(TAI->getDataSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001833 EmitLabel("data_begin", 0);
Jim Laskey89d67fa2006-06-23 12:51:53 +00001834
Jim Laskey014f98c2006-06-14 11:35:03 +00001835 // Emit common frame information.
1836 EmitInitialDebugFrame();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001837}
1838
Jim Laskey063e7652006-01-17 17:31:53 +00001839/// EmitDIE - Recusively Emits a debug information entry.
1840///
1841void DwarfWriter::EmitDIE(DIE *Die) const {
1842 // Get the abbreviation for this DIE.
1843 unsigned AbbrevID = Die->getAbbrevID();
1844 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey0d086af2006-02-27 12:43:29 +00001845
1846 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001847
1848 // Emit the code (index) for the abbreviation.
1849 EmitULEB128Bytes(AbbrevID);
1850 EOL(std::string("Abbrev [" +
1851 utostr(AbbrevID) +
Jim Laskey0d086af2006-02-27 12:43:29 +00001852 "] 0x" + utohexstr(Die->getOffset()) +
1853 ":0x" + utohexstr(Die->getSize()) + " " +
1854 TagString(Abbrev.getTag())));
Jim Laskey063e7652006-01-17 17:31:53 +00001855
1856 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001857 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001858
1859 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001860 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001861 unsigned Attr = AbbrevData[i].getAttribute();
1862 unsigned Form = AbbrevData[i].getForm();
Jim Laskey063e7652006-01-17 17:31:53 +00001863 assert(Form && "Too many attributes for DIE (check abbreviation)");
1864
1865 switch (Attr) {
1866 case DW_AT_sibling: {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001867 EmitInt32(Die->SiblingOffset());
Jim Laskey063e7652006-01-17 17:31:53 +00001868 break;
1869 }
1870 default: {
1871 // Emit an attribute using the defined form.
1872 Values[i]->EmitValue(*this, Form);
1873 break;
1874 }
1875 }
1876
1877 EOL(AttributeString(Attr));
1878 }
1879
1880 // Emit the DIE children if any.
1881 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1882 const std::vector<DIE *> &Children = Die->getChildren();
1883
Jim Laskey52060a02006-01-24 00:49:18 +00001884 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskey063e7652006-01-17 17:31:53 +00001885 EmitDIE(Children[j]);
1886 }
1887
Jim Laskeyda427fa2006-01-27 20:31:25 +00001888 EmitInt8(0); EOL("End Of Children Mark");
Jim Laskey063e7652006-01-17 17:31:53 +00001889 }
1890}
1891
1892/// SizeAndOffsetDie - Compute the size and offset of a DIE.
1893///
Jim Laskeyb8509c52006-03-23 18:07:55 +00001894unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1895 // Get the children.
1896 const std::vector<DIE *> &Children = Die->getChildren();
1897
1898 // If not last sibling and has children then add sibling offset attribute.
1899 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1900
Jim Laskey0420f2a2006-02-22 19:02:11 +00001901 // Record the abbreviation.
1902 Die->Complete(*this);
1903
Jim Laskey063e7652006-01-17 17:31:53 +00001904 // Get the abbreviation for this DIE.
1905 unsigned AbbrevID = Die->getAbbrevID();
1906 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1907
1908 // Set DIE offset
1909 Die->setOffset(Offset);
1910
1911 // Start the size with the size of abbreviation code.
1912 Offset += SizeULEB128(AbbrevID);
1913
1914 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001915 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001916
1917 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001918 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001919 // Size attribute value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001920 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
Jim Laskey063e7652006-01-17 17:31:53 +00001921 }
1922
1923 // Emit the DIE children if any.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001924 if (!Children.empty()) {
1925 assert(Abbrev.getChildrenFlag() == DW_CHILDREN_yes &&
1926 "Children flag not set");
Jim Laskey063e7652006-01-17 17:31:53 +00001927
Jim Laskey52060a02006-01-24 00:49:18 +00001928 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001929 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
Jim Laskey063e7652006-01-17 17:31:53 +00001930 }
1931
1932 // End of children marker.
1933 Offset += sizeof(int8_t);
1934 }
1935
1936 Die->setSize(Offset - Die->getOffset());
1937 return Offset;
1938}
1939
1940/// SizeAndOffsets - Compute the size and offset of all the DIEs.
1941///
1942void DwarfWriter::SizeAndOffsets() {
Jim Laskey063e7652006-01-17 17:31:53 +00001943
1944 // Process each compile unit.
Jim Laskey52060a02006-01-24 00:49:18 +00001945 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00001946 CompileUnit *Unit = CompileUnits[i];
1947 if (Unit->hasContent()) {
1948 // Compute size of compile unit header
1949 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1950 sizeof(int16_t) + // DWARF version number
1951 sizeof(int32_t) + // Offset Into Abbrev. Section
1952 sizeof(int8_t); // Pointer Size (in bytes)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001953 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
Jim Laskeybd761842006-02-27 17:27:12 +00001954 }
Jim Laskey063e7652006-01-17 17:31:53 +00001955 }
1956}
1957
Jim Laskey41886992006-04-07 16:34:46 +00001958/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1959/// frame.
1960void DwarfWriter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
1961 std::vector<MachineMove *> &Moves) {
1962 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1963 MachineMove *Move = Moves[i];
1964 unsigned LabelID = Move->getLabelID();
1965 const MachineLocation &Dst = Move->getDestination();
1966 const MachineLocation &Src = Move->getSource();
1967
1968 // Advance row if new location.
1969 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
Jim Laskeyce50a162006-08-29 16:24:26 +00001970 EmitInt8(DW_CFA_advance_loc4);
Jim Laskey41886992006-04-07 16:34:46 +00001971 EOL("DW_CFA_advance_loc4");
1972 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
1973 EOL("");
1974
1975 BaseLabelID = LabelID;
1976 BaseLabel = "loc";
1977 }
1978
Jim Laskeyce50a162006-08-29 16:24:26 +00001979 int stackGrowth =
1980 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1981 TargetFrameInfo::StackGrowsUp ?
Jim Laskey563321a2006-09-06 18:34:40 +00001982 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskeyce50a162006-08-29 16:24:26 +00001983
Jim Laskey41886992006-04-07 16:34:46 +00001984 // If advancing cfa.
1985 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
1986 if (!Src.isRegister()) {
1987 if (Src.getRegister() == MachineLocation::VirtualFP) {
Jim Laskeyce50a162006-08-29 16:24:26 +00001988 EmitInt8(DW_CFA_def_cfa_offset);
Jim Laskey41886992006-04-07 16:34:46 +00001989 EOL("DW_CFA_def_cfa_offset");
1990 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00001991 EmitInt8(DW_CFA_def_cfa);
Jim Laskey41886992006-04-07 16:34:46 +00001992 EOL("DW_CFA_def_cfa");
1993
1994 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
1995 EOL("Register");
1996 }
Jim Laskey1069fbd2006-04-10 23:09:19 +00001997
Jim Laskeyce50a162006-08-29 16:24:26 +00001998 int Offset = Src.getOffset() / stackGrowth;
Jim Laskey1069fbd2006-04-10 23:09:19 +00001999
Jim Laskeyce50a162006-08-29 16:24:26 +00002000 EmitULEB128Bytes(Offset);
Jim Laskey41886992006-04-07 16:34:46 +00002001 EOL("Offset");
2002 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00002003 assert(0 && "Machine move no supported yet.");
Jim Laskey41886992006-04-07 16:34:46 +00002004 }
2005 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00002006 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
2007 int Offset = Dst.getOffset() / stackGrowth;
2008
2009 if (Offset < 0) {
2010 EmitInt8(DW_CFA_offset_extended_sf);
2011 EOL("DW_CFA_offset_extended_sf");
2012 EmitULEB128Bytes(Reg);
2013 EOL("Reg");
2014 EmitSLEB128Bytes(Offset);
2015 EOL("Offset");
2016 } else if (Reg < 64) {
2017 EmitInt8(DW_CFA_offset + Reg);
2018 EOL("DW_CFA_offset + Reg");
2019 EmitULEB128Bytes(Offset);
2020 EOL("Offset");
2021 } else {
2022 EmitInt8(DW_CFA_offset_extended);
2023 EOL("DW_CFA_offset_extended");
2024 EmitULEB128Bytes(Reg);
2025 EOL("Reg");
2026 EmitULEB128Bytes(Offset);
2027 EOL("Offset");
2028 }
Jim Laskey41886992006-04-07 16:34:46 +00002029 }
2030 }
2031}
2032
Jim Laskey063e7652006-01-17 17:31:53 +00002033/// EmitDebugInfo - Emit the debug info section.
2034///
2035void DwarfWriter::EmitDebugInfo() const {
2036 // Start debug info section.
Jim Laskey563321a2006-09-06 18:34:40 +00002037 Asm->SwitchToDataSection(TAI->getDwarfInfoSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002038
Jim Laskeybd761842006-02-27 17:27:12 +00002039 // Process each compile unit.
2040 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2041 CompileUnit *Unit = CompileUnits[i];
2042
2043 if (Unit->hasContent()) {
2044 DIE *Die = Unit->getDie();
Jim Laskey0d086af2006-02-27 12:43:29 +00002045 // Emit the compile units header.
Jim Laskeybd761842006-02-27 17:27:12 +00002046 EmitLabel("info_begin", Unit->getID());
Jim Laskey0d086af2006-02-27 12:43:29 +00002047 // Emit size of content not including length itself
Jim Laskeybd761842006-02-27 17:27:12 +00002048 unsigned ContentSize = Die->getSize() +
Jim Laskey0d086af2006-02-27 12:43:29 +00002049 sizeof(int16_t) + // DWARF version number
2050 sizeof(int32_t) + // Offset Into Abbrev. Section
2051 sizeof(int8_t); // Pointer Size (in bytes)
2052
2053 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2054 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskeyf8a01a92006-06-15 20:51:43 +00002055 EmitDifference("abbrev_begin", 0, "section_abbrev", 0);
2056 EOL("Offset Into Abbrev. Section");
Jim Laskey563321a2006-09-06 18:34:40 +00002057 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
Jim Laskey0d086af2006-02-27 12:43:29 +00002058
Jim Laskeybd761842006-02-27 17:27:12 +00002059 EmitDIE(Die);
2060 EmitLabel("info_end", Unit->getID());
Jim Laskey063e7652006-01-17 17:31:53 +00002061 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002062
2063 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002064 }
2065}
2066
2067/// EmitAbbreviations - Emit the abbreviation section.
2068///
2069void DwarfWriter::EmitAbbreviations() const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002070 // Check to see if it is worth the effort.
2071 if (!Abbreviations.empty()) {
2072 // Start the debug abbrev section.
Jim Laskey563321a2006-09-06 18:34:40 +00002073 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002074
Jim Laskeyd18e2892006-01-20 20:34:06 +00002075 EmitLabel("abbrev_begin", 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002076
Jim Laskeyd18e2892006-01-20 20:34:06 +00002077 // For each abbrevation.
2078 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002079 AbbrevID <= NAID; ++AbbrevID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002080 // Get abbreviation data
2081 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey063e7652006-01-17 17:31:53 +00002082
Jim Laskeyd18e2892006-01-20 20:34:06 +00002083 // Emit the abbrevations code (base 1 index.)
2084 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
Jim Laskey063e7652006-01-17 17:31:53 +00002085
Jim Laskeyd18e2892006-01-20 20:34:06 +00002086 // Emit the abbreviations data.
2087 Abbrev.Emit(*this);
Jim Laskey0d086af2006-02-27 12:43:29 +00002088
2089 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002090 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002091
2092 EmitLabel("abbrev_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002093
2094 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002095 }
2096}
2097
2098/// EmitDebugLines - Emit source line information.
2099///
2100void DwarfWriter::EmitDebugLines() const {
2101 // Minimum line delta, thus ranging from -10..(255-10).
2102 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2103 // Maximum line delta, thus ranging from -10..(255-10).
2104 const int MaxLineDelta = 255 + MinLineDelta;
2105
2106 // Start the dwarf line section.
Jim Laskey563321a2006-09-06 18:34:40 +00002107 Asm->SwitchToDataSection(TAI->getDwarfLineSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002108
2109 // Construct the section header.
2110
2111 EmitDifference("line_end", 0, "line_begin", 0);
2112 EOL("Length of Source Line Info");
2113 EmitLabel("line_begin", 0);
2114
Jim Laskeyda427fa2006-01-27 20:31:25 +00002115 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey063e7652006-01-17 17:31:53 +00002116
2117 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
2118 EOL("Prolog Length");
2119 EmitLabel("line_prolog_begin", 0);
2120
Jim Laskeyda427fa2006-01-27 20:31:25 +00002121 EmitInt8(1); EOL("Minimum Instruction Length");
Jim Laskey063e7652006-01-17 17:31:53 +00002122
Jim Laskeyda427fa2006-01-27 20:31:25 +00002123 EmitInt8(1); EOL("Default is_stmt_start flag");
Jim Laskey063e7652006-01-17 17:31:53 +00002124
Jim Laskeyda427fa2006-01-27 20:31:25 +00002125 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00002126
Jim Laskeyda427fa2006-01-27 20:31:25 +00002127 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00002128
Jim Laskeyda427fa2006-01-27 20:31:25 +00002129 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Jim Laskey063e7652006-01-17 17:31:53 +00002130
2131 // Line number standard opcode encodings argument count
Jim Laskeyda427fa2006-01-27 20:31:25 +00002132 EmitInt8(0); EOL("DW_LNS_copy arg count");
2133 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2134 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2135 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2136 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2137 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2138 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2139 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2140 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskey063e7652006-01-17 17:31:53 +00002141
2142 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2143 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
2144
2145 // Emit directories.
2146 for (unsigned DirectoryID = 1, NDID = Directories.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002147 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskey063e7652006-01-17 17:31:53 +00002148 EmitString(Directories[DirectoryID]); EOL("Directory");
2149 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00002150 EmitInt8(0); EOL("End of directories");
Jim Laskey063e7652006-01-17 17:31:53 +00002151
2152 // Emit files.
2153 for (unsigned SourceID = 1, NSID = SourceFiles.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002154 SourceID <= NSID; ++SourceID) {
Jim Laskey063e7652006-01-17 17:31:53 +00002155 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2156 EmitString(SourceFile.getName()); EOL("Source");
2157 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2158 EmitULEB128Bytes(0); EOL("Mod date");
2159 EmitULEB128Bytes(0); EOL("File size");
2160 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00002161 EmitInt8(0); EOL("End of files");
Jim Laskey063e7652006-01-17 17:31:53 +00002162
2163 EmitLabel("line_prolog_end", 0);
2164
Jim Laskey89d67fa2006-06-23 12:51:53 +00002165 // A sequence for each text section.
2166 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2167 // Isolate current sections line info.
2168 const std::vector<SourceLineInfo *> &LineInfos = SectionSourceLines[j];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002169
2170 if (DwarfVerbose) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002171 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00002172 << TAI->getCommentString() << " "
Jim Laskey89d67fa2006-06-23 12:51:53 +00002173 << "Section "
2174 << SectionMap[j + 1].c_str() << "\n";
Jim Laskey0420f2a2006-02-22 19:02:11 +00002175 }
Jim Laskey063e7652006-01-17 17:31:53 +00002176
Jim Laskey89d67fa2006-06-23 12:51:53 +00002177 // Dwarf assumes we start with first line of first source file.
2178 unsigned Source = 1;
2179 unsigned Line = 1;
2180
2181 // Construct rows of the address, source, line, column matrix.
2182 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2183 SourceLineInfo *LineInfo = LineInfos[i];
2184
2185 if (DwarfVerbose) {
2186 unsigned SourceID = LineInfo->getSourceID();
2187 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2188 unsigned DirectoryID = SourceFile.getDirectoryID();
2189 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00002190 << TAI->getCommentString() << " "
Jim Laskey89d67fa2006-06-23 12:51:53 +00002191 << Directories[DirectoryID]
2192 << SourceFile.getName() << ":"
2193 << LineInfo->getLine() << "\n";
2194 }
2195
2196 // Define the line address.
2197 EmitInt8(0); EOL("Extended Op");
2198 EmitInt8(4 + 1); EOL("Op size");
2199 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2200 EmitReference("loc", LineInfo->getLabelID()); EOL("Location label");
2201
2202 // If change of source, then switch to the new source.
2203 if (Source != LineInfo->getSourceID()) {
2204 Source = LineInfo->getSourceID();
2205 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2206 EmitULEB128Bytes(Source); EOL("New Source");
2207 }
2208
2209 // If change of line.
2210 if (Line != LineInfo->getLine()) {
2211 // Determine offset.
2212 int Offset = LineInfo->getLine() - Line;
2213 int Delta = Offset - MinLineDelta;
2214
2215 // Update line.
2216 Line = LineInfo->getLine();
2217
2218 // If delta is small enough and in range...
2219 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2220 // ... then use fast opcode.
2221 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2222 } else {
2223 // ... otherwise use long hand.
2224 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2225 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2226 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2227 }
2228 } else {
2229 // Copy the previous row (different address or source)
2230 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2231 }
2232 }
2233
2234 // Define last address of section.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002235 EmitInt8(0); EOL("Extended Op");
2236 EmitInt8(4 + 1); EOL("Op size");
2237 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskey89d67fa2006-06-23 12:51:53 +00002238 EmitReference("section_end", j + 1); EOL("Section end label");
2239
2240 // Mark end of matrix.
2241 EmitInt8(0); EOL("DW_LNE_end_sequence");
2242 EmitULEB128Bytes(1); O << "\n";
2243 EmitInt8(1); O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002244 }
Jim Laskey063e7652006-01-17 17:31:53 +00002245
2246 EmitLabel("line_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002247
2248 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002249}
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002250
Jim Laskey41886992006-04-07 16:34:46 +00002251/// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002252///
Jim Laskey41886992006-04-07 16:34:46 +00002253void DwarfWriter::EmitInitialDebugFrame() {
Jim Laskey1069fbd2006-04-10 23:09:19 +00002254 int stackGrowth =
2255 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2256 TargetFrameInfo::StackGrowsUp ?
Jim Laskey563321a2006-09-06 18:34:40 +00002257 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskey1069fbd2006-04-10 23:09:19 +00002258
Jim Laskey41886992006-04-07 16:34:46 +00002259 // Start the dwarf frame section.
Jim Laskey563321a2006-09-06 18:34:40 +00002260 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002261
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002262 EmitLabel("frame_common", 0);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002263 EmitDifference("frame_common_end", 0,
2264 "frame_common_begin", 0);
2265 EOL("Length of Common Information Entry");
2266
2267 EmitLabel("frame_common_begin", 0);
2268 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2269 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2270 EmitString(""); EOL("CIE Augmentation");
2271 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
Jim Laskey1069fbd2006-04-10 23:09:19 +00002272 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
Jim Laskey41886992006-04-07 16:34:46 +00002273 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
2274
2275 std::vector<MachineMove *> Moves;
2276 RI->getInitialFrameState(Moves);
2277 EmitFrameMoves(NULL, 0, Moves);
2278 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2279
Jim Laskeyb8509c52006-03-23 18:07:55 +00002280 EmitAlign(2);
2281 EmitLabel("frame_common_end", 0);
2282
2283 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002284}
2285
Jim Laskey41886992006-04-07 16:34:46 +00002286/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2287/// section.
2288void DwarfWriter::EmitFunctionDebugFrame() {
2289 // Start the dwarf frame section.
Jim Laskey563321a2006-09-06 18:34:40 +00002290 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskey41886992006-04-07 16:34:46 +00002291
2292 EmitDifference("frame_end", SubprogramCount,
2293 "frame_begin", SubprogramCount);
2294 EOL("Length of Frame Information Entry");
2295
2296 EmitLabel("frame_begin", SubprogramCount);
2297
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002298 EmitDifference("frame_common", 0, "section_frame", 0);
2299 EOL("FDE CIE offset");
Jim Laskey41886992006-04-07 16:34:46 +00002300
2301 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2302 EmitDifference("func_end", SubprogramCount,
2303 "func_begin", SubprogramCount);
2304 EOL("FDE address range");
2305
2306 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2307
2308 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2309
2310 EmitAlign(2);
2311 EmitLabel("frame_end", SubprogramCount);
2312
2313 O << "\n";
2314}
2315
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002316/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2317///
2318void DwarfWriter::EmitDebugPubNames() {
Jim Laskeybd761842006-02-27 17:27:12 +00002319 // Start the dwarf pubnames section.
Jim Laskey563321a2006-09-06 18:34:40 +00002320 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection(), 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002321
Jim Laskeybd761842006-02-27 17:27:12 +00002322 // Process each compile unit.
2323 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2324 CompileUnit *Unit = CompileUnits[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00002325
Jim Laskeybd761842006-02-27 17:27:12 +00002326 if (Unit->hasContent()) {
2327 EmitDifference("pubnames_end", Unit->getID(),
2328 "pubnames_begin", Unit->getID());
2329 EOL("Length of Public Names Info");
2330
2331 EmitLabel("pubnames_begin", Unit->getID());
2332
2333 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2334
Jim Laskey067ef412006-06-19 15:48:00 +00002335 EmitDifference("info_begin", Unit->getID(), "section_info", 0);
Jim Laskeybd761842006-02-27 17:27:12 +00002336 EOL("Offset of Compilation Unit Info");
Jim Laskeyd18e2892006-01-20 20:34:06 +00002337
Jim Laskeybd761842006-02-27 17:27:12 +00002338 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2339 EOL("Compilation Unit Length");
2340
2341 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2342
2343 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2344 GE = Globals.end();
2345 GI != GE; ++GI) {
2346 const std::string &Name = GI->first;
2347 DIE * Entity = GI->second;
2348
2349 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2350 EmitString(Name); EOL("External Name");
2351 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002352
Jim Laskeybd761842006-02-27 17:27:12 +00002353 EmitInt32(0); EOL("End Mark");
2354 EmitLabel("pubnames_end", Unit->getID());
2355
2356 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002357 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002358 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002359}
2360
2361/// EmitDebugStr - Emit visible names into a debug str section.
2362///
2363void DwarfWriter::EmitDebugStr() {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002364 // Check to see if it is worth the effort.
2365 if (!StringPool.empty()) {
2366 // Start the dwarf str section.
Jim Laskey563321a2006-09-06 18:34:40 +00002367 Asm->SwitchToDataSection(TAI->getDwarfStrSection(), 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002368
Jim Laskeyb8509c52006-03-23 18:07:55 +00002369 // For each of strings in the string pool.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002370 for (unsigned StringID = 1, N = StringPool.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002371 StringID <= N; ++StringID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002372 // Emit a label for reference from debug information entries.
2373 EmitLabel("string", StringID);
2374 // Emit the string itself.
2375 const std::string &String = StringPool[StringID];
2376 EmitString(String); O << "\n";
2377 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002378
2379 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002380 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002381}
2382
2383/// EmitDebugLoc - Emit visible names into a debug loc section.
2384///
2385void DwarfWriter::EmitDebugLoc() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002386 // Start the dwarf loc section.
Jim Laskey563321a2006-09-06 18:34:40 +00002387 Asm->SwitchToDataSection(TAI->getDwarfLocSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002388
2389 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002390}
2391
2392/// EmitDebugARanges - Emit visible names into a debug aranges section.
2393///
2394void DwarfWriter::EmitDebugARanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002395 // Start the dwarf aranges section.
Jim Laskey563321a2006-09-06 18:34:40 +00002396 Asm->SwitchToDataSection(TAI->getDwarfARangesSection(), 0);
Jim Laskeye719a7c2006-01-18 16:54:26 +00002397
2398 // FIXME - Mock up
Jim Laskeybd761842006-02-27 17:27:12 +00002399#if 0
2400 // Process each compile unit.
2401 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2402 CompileUnit *Unit = CompileUnits[i];
2403
2404 if (Unit->hasContent()) {
2405 // Don't include size of length
2406 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2407
2408 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2409
2410 EmitReference("info_begin", Unit->getID());
2411 EOL("Offset of Compilation Unit Info");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002412
Jim Laskey563321a2006-09-06 18:34:40 +00002413 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002414
Jim Laskeybd761842006-02-27 17:27:12 +00002415 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002416
Jim Laskeybd761842006-02-27 17:27:12 +00002417 EmitInt16(0); EOL("Pad (1)");
2418 EmitInt16(0); EOL("Pad (2)");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002419
Jim Laskeybd761842006-02-27 17:27:12 +00002420 // Range 1
2421 EmitReference("text_begin", 0); EOL("Address");
2422 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002423
Jim Laskeybd761842006-02-27 17:27:12 +00002424 EmitInt32(0); EOL("EOM (1)");
2425 EmitInt32(0); EOL("EOM (2)");
2426
2427 O << "\n";
2428 }
2429 }
2430#endif
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002431}
2432
2433/// EmitDebugRanges - Emit visible names into a debug ranges section.
2434///
2435void DwarfWriter::EmitDebugRanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002436 // Start the dwarf ranges section.
Jim Laskey563321a2006-09-06 18:34:40 +00002437 Asm->SwitchToDataSection(TAI->getDwarfRangesSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002438
2439 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002440}
2441
2442/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2443///
2444void DwarfWriter::EmitDebugMacInfo() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002445 // Start the dwarf macinfo section.
Jim Laskey563321a2006-09-06 18:34:40 +00002446 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002447
2448 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002449}
Jim Laskey063e7652006-01-17 17:31:53 +00002450
Jim Laskey52060a02006-01-24 00:49:18 +00002451/// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2452/// header file.
2453void DwarfWriter::ConstructCompileUnitDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002454 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
Jim Laskey6e87c0e2006-01-26 21:22:49 +00002455
2456 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00002457 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
Jim Laskey52060a02006-01-24 00:49:18 +00002458 CompileUnits.push_back(Unit);
2459 }
2460}
2461
2462/// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2463/// variables.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002464void DwarfWriter::ConstructGlobalDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002465 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002466 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00002467
2468 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002469 GlobalVariableDesc *GVD = GlobalVariables[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002470 NewGlobalVariable(GVD);
Jim Laskey52060a02006-01-24 00:49:18 +00002471 }
2472}
2473
Jim Laskey0420f2a2006-02-22 19:02:11 +00002474/// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2475/// subprograms.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002476void DwarfWriter::ConstructSubprogramDIEs() {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002477 std::vector<SubprogramDesc *> Subprograms =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002478 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002479
2480 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2481 SubprogramDesc *SPD = Subprograms[i];
2482 NewSubprogram(SPD);
2483 }
2484}
Jim Laskey52060a02006-01-24 00:49:18 +00002485
Jim Laskey063e7652006-01-17 17:31:53 +00002486//===----------------------------------------------------------------------===//
Jim Laskeyd18e2892006-01-20 20:34:06 +00002487// Main entry points.
Jim Laskey063e7652006-01-17 17:31:53 +00002488//
Jim Laskey52060a02006-01-24 00:49:18 +00002489
Jim Laskeya0f3d172006-09-07 22:06:40 +00002490DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
2491 const TargetAsmInfo *T)
Jim Laskey52060a02006-01-24 00:49:18 +00002492: O(OS)
2493, Asm(A)
Jim Laskey563321a2006-09-06 18:34:40 +00002494, TAI(T)
Jim Laskey41886992006-04-07 16:34:46 +00002495, TD(Asm->TM.getTargetData())
2496, RI(Asm->TM.getRegisterInfo())
Jim Laskeyb8509c52006-03-23 18:07:55 +00002497, M(NULL)
2498, MF(NULL)
Jim Laskey52060a02006-01-24 00:49:18 +00002499, DebugInfo(NULL)
2500, didInitial(false)
Jim Laskey014f98c2006-06-14 11:35:03 +00002501, shouldEmit(false)
Jim Laskeyb8509c52006-03-23 18:07:55 +00002502, SubprogramCount(0)
Jim Laskey52060a02006-01-24 00:49:18 +00002503, CompileUnits()
2504, Abbreviations()
Jim Laskey52060a02006-01-24 00:49:18 +00002505, StringPool()
Jim Laskeybd761842006-02-27 17:27:12 +00002506, DescToUnitMap()
Jim Laskey0420f2a2006-02-22 19:02:11 +00002507, DescToDieMap()
Jim Laskey89d67fa2006-06-23 12:51:53 +00002508, SectionMap()
2509, SectionSourceLines()
Jim Laskey52060a02006-01-24 00:49:18 +00002510{}
2511DwarfWriter::~DwarfWriter() {
2512 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2513 delete CompileUnits[i];
Jim Laskey063e7652006-01-17 17:31:53 +00002514 }
Jim Laskey52060a02006-01-24 00:49:18 +00002515}
Jim Laskey063e7652006-01-17 17:31:53 +00002516
Jim Laskey41886992006-04-07 16:34:46 +00002517/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2518/// created it. Set by the target AsmPrinter.
2519void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
Jim Laskey014f98c2006-06-14 11:35:03 +00002520 // Make sure initial declarations are made.
2521 if (!DebugInfo && DI->hasInfo()) {
2522 DebugInfo = DI;
2523 shouldEmit = true;
2524
2525 // Emit initial sections
2526 EmitInitial();
2527
2528 // Create all the compile unit DIEs.
2529 ConstructCompileUnitDIEs();
2530
2531 // Create DIEs for each of the externally visible global variables.
2532 ConstructGlobalDIEs();
2533
2534 // Create DIEs for each of the externally visible subprograms.
2535 ConstructSubprogramDIEs();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002536
2537 // Prime section data.
Jim Laskey563321a2006-09-06 18:34:40 +00002538 SectionMap.insert(std::string("\t") + TAI->getTextSection());
Jim Laskey014f98c2006-06-14 11:35:03 +00002539 }
Jim Laskey41886992006-04-07 16:34:46 +00002540}
2541
Jim Laskey063e7652006-01-17 17:31:53 +00002542/// BeginModule - Emit all Dwarf sections that should come prior to the content.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002543///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002544void DwarfWriter::BeginModule(Module *M) {
2545 this->M = M;
2546
Jim Laskeyb2efb852006-01-04 22:28:25 +00002547 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002548 EOL("Dwarf Begin Module");
Jim Laskeyb2efb852006-01-04 22:28:25 +00002549}
2550
Jim Laskey063e7652006-01-17 17:31:53 +00002551/// EndModule - Emit all Dwarf sections that should come after the content.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002552///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002553void DwarfWriter::EndModule() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002554 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002555 EOL("Dwarf End Module");
2556
2557 // Standard sections final addresses.
Jim Laskey563321a2006-09-06 18:34:40 +00002558 Asm->SwitchToTextSection(TAI->getTextSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002559 EmitLabel("text_end", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002560 Asm->SwitchToDataSection(TAI->getDataSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002561 EmitLabel("data_end", 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002562
Jim Laskey89d67fa2006-06-23 12:51:53 +00002563 // End text sections.
2564 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2565 Asm->SwitchToTextSection(SectionMap[i].c_str(), 0);
2566 EmitLabel("section_end", i);
2567 }
2568
Jim Laskey063e7652006-01-17 17:31:53 +00002569 // Compute DIE offsets and sizes.
2570 SizeAndOffsets();
2571
2572 // Emit all the DIEs into a debug info section
2573 EmitDebugInfo();
2574
2575 // Corresponding abbreviations into a abbrev section.
2576 EmitAbbreviations();
2577
2578 // Emit source line correspondence into a debug line section.
2579 EmitDebugLines();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002580
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002581 // Emit info into a debug pubnames section.
2582 EmitDebugPubNames();
2583
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002584 // Emit info into a debug str section.
2585 EmitDebugStr();
2586
2587 // Emit info into a debug loc section.
2588 EmitDebugLoc();
2589
2590 // Emit info into a debug aranges section.
2591 EmitDebugARanges();
2592
2593 // Emit info into a debug ranges section.
2594 EmitDebugRanges();
2595
2596 // Emit info into a debug macinfo section.
2597 EmitDebugMacInfo();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002598}
2599
Jim Laskey6b92b8e2006-04-07 20:44:42 +00002600/// BeginFunction - Gather pre-function debug information. Assumes being
2601/// emitted immediately after the function entry point.
Jim Laskey89d67fa2006-06-23 12:51:53 +00002602void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002603 this->MF = MF;
2604
Jim Laskey89d67fa2006-06-23 12:51:53 +00002605 if (!ShouldEmitDwarf()) return;
2606 EOL("Dwarf Begin Function");
2607
2608 // Begin accumulating function debug information.
2609 DebugInfo->BeginFunction(MF);
Jim Laskey41886992006-04-07 16:34:46 +00002610
Jim Laskey89d67fa2006-06-23 12:51:53 +00002611 // Assumes in correct section after the entry point.
2612 EmitLabel("func_begin", ++SubprogramCount);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002613}
2614
Jim Laskeye719a7c2006-01-18 16:54:26 +00002615/// EndFunction - Gather and emit post-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002616///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002617void DwarfWriter::EndFunction() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002618 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002619 EOL("Dwarf End Function");
Jim Laskeyb8509c52006-03-23 18:07:55 +00002620
Jim Laskey89d67fa2006-06-23 12:51:53 +00002621 // Define end label for subprogram.
2622 EmitLabel("func_end", SubprogramCount);
Jim Laskey014f98c2006-06-14 11:35:03 +00002623
Jim Laskey89d67fa2006-06-23 12:51:53 +00002624 // Get function line info.
2625 std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
2626
2627 if (!LineInfos.empty()) {
2628 // Get section line info.
2629 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2630 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2631 std::vector<SourceLineInfo *> &SectionLineInfos =SectionSourceLines[ID-1];
2632 // Append the function info to section info.
2633 SectionLineInfos.insert(SectionLineInfos.end(),
2634 LineInfos.begin(), LineInfos.end());
Jim Laskey014f98c2006-06-14 11:35:03 +00002635 }
Jim Laskey41886992006-04-07 16:34:46 +00002636
Jim Laskey89d67fa2006-06-23 12:51:53 +00002637 // Construct scopes for subprogram.
2638 ConstructRootScope(DebugInfo->getRootScope());
2639
2640 // Emit function frame information.
2641 EmitFunctionDebugFrame();
2642
2643 // Reset the line numbers for the next function.
2644 LineInfos.clear();
2645
Jim Laskey41886992006-04-07 16:34:46 +00002646 // Clear function debug information.
2647 DebugInfo->EndFunction();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002648}