blob: 0921cc5c6478971da21fe7a64a1ab920d9235967 [file] [log] [blame]
Jim Laskeye5032892005-12-21 19:48:16 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by James M. Laskey and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Jim Laskey3ea0e0e2006-01-27 18:32:41 +000013
Jim Laskeyb2efb852006-01-04 22:28:25 +000014#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskeye5032892005-12-21 19:48:16 +000015
Jim Laskey063e7652006-01-17 17:31:53 +000016#include "llvm/ADT/StringExtras.h"
Jim Laskey52060a02006-01-24 00:49:18 +000017#include "llvm/Module.h"
18#include "llvm/Type.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000019#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeyb2efb852006-01-04 22:28:25 +000020#include "llvm/CodeGen/MachineDebugInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000022#include "llvm/CodeGen/MachineLocation.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000023#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000024#include "llvm/Support/CommandLine.h"
Jim Laskey52060a02006-01-24 00:49:18 +000025#include "llvm/Support/Mangler.h"
Jim Laskey563321a2006-09-06 18:34:40 +000026#include "llvm/Target/TargetAsmInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000027#include "llvm/Target/MRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000028#include "llvm/Target/TargetData.h"
Jim Laskey52060a02006-01-24 00:49:18 +000029#include "llvm/Target/TargetMachine.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000030#include "llvm/Target/TargetFrameInfo.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000031
Jim Laskeyb2efb852006-01-04 22:28:25 +000032#include <iostream>
Jim Laskeya7cea6f2006-01-04 13:52:30 +000033
Jim Laskeyb2efb852006-01-04 22:28:25 +000034using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000035using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000036
37static cl::opt<bool>
38DwarfVerbose("dwarf-verbose", cl::Hidden,
Jim Laskeyce50a162006-08-29 16:24:26 +000039 cl::desc("Add comments to Dwarf directives."));
Jim Laskey063e7652006-01-17 17:31:53 +000040
Jim Laskey0d086af2006-02-27 12:43:29 +000041namespace llvm {
42
Jim Laskey063e7652006-01-17 17:31:53 +000043//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000044// Forward declarations.
45//
Jim Laskey0d086af2006-02-27 12:43:29 +000046class DIE;
Jim Laskey063e7652006-01-17 17:31:53 +000047
Jim Laskey0d086af2006-02-27 12:43:29 +000048//===----------------------------------------------------------------------===//
Jim Laskeyf01e5472006-03-03 15:06:57 +000049// CompileUnit - This dwarf writer support class manages information associate
50// with a source file.
Jim Laskeybd761842006-02-27 17:27:12 +000051class CompileUnit {
52private:
53 CompileUnitDesc *Desc; // Compile unit debug descriptor.
54 unsigned ID; // File ID for source.
Jim Laskeyb8509c52006-03-23 18:07:55 +000055 DIE *Die; // Compile unit debug information entry.
Jim Laskeybd761842006-02-27 17:27:12 +000056 std::map<std::string, DIE *> Globals; // A map of globally visible named
57 // entities for this unit.
Jim Laskey90c79d72006-03-23 23:02:34 +000058 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
59 // Tracks the mapping of unit level
60 // debug informaton descriptors to debug
61 // information entries.
Jim Laskeybd761842006-02-27 17:27:12 +000062
63public:
64 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
65 : Desc(CUD)
66 , ID(I)
67 , Die(D)
68 , Globals()
Jim Laskey90c79d72006-03-23 23:02:34 +000069 , DescToDieMap()
Jim Laskeybd761842006-02-27 17:27:12 +000070 {}
71
72 ~CompileUnit();
73
74 // Accessors.
75 CompileUnitDesc *getDesc() const { return Desc; }
76 unsigned getID() const { return ID; }
77 DIE* getDie() const { return Die; }
78 std::map<std::string, DIE *> &getGlobals() { return Globals; }
79
80 /// hasContent - Return true if this compile unit has something to write out.
81 ///
82 bool hasContent() const;
83
84 /// AddGlobal - Add a new global entity to the compile unit.
85 ///
86 void AddGlobal(const std::string &Name, DIE *Die);
87
Jim Laskey90c79d72006-03-23 23:02:34 +000088 /// getDieMapSlotFor - Returns the debug information entry map slot for the
89 /// specified debug descriptor.
90 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
91 return DescToDieMap[DD];
92 }
Jim Laskeybd761842006-02-27 17:27:12 +000093};
94
95//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000096// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
97// Dwarf abbreviation.
98class DIEAbbrevData {
99private:
100 unsigned Attribute; // Dwarf attribute code.
101 unsigned Form; // Dwarf form code.
102
103public:
104 DIEAbbrevData(unsigned A, unsigned F)
105 : Attribute(A)
106 , Form(F)
107 {}
108
Jim Laskeybd761842006-02-27 17:27:12 +0000109 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000110 unsigned getAttribute() const { return Attribute; }
111 unsigned getForm() const { return Form; }
112
113 /// operator== - Used by DIEAbbrev to locate entry.
114 ///
115 bool operator==(const DIEAbbrevData &DAD) const {
116 return Attribute == DAD.Attribute && Form == DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000117 }
Jim Laskey063e7652006-01-17 17:31:53 +0000118
Jim Laskey0d086af2006-02-27 12:43:29 +0000119 /// operator!= - Used by DIEAbbrev to locate entry.
120 ///
121 bool operator!=(const DIEAbbrevData &DAD) const {
122 return Attribute != DAD.Attribute || Form != DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000123 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000124
125 /// operator< - Used by DIEAbbrev to locate entry.
126 ///
127 bool operator<(const DIEAbbrevData &DAD) const {
128 return Attribute < DAD.Attribute ||
129 (Attribute == DAD.Attribute && Form < DAD.Form);
130 }
131};
Jim Laskey063e7652006-01-17 17:31:53 +0000132
Jim Laskey0d086af2006-02-27 12:43:29 +0000133//===----------------------------------------------------------------------===//
134// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
135// information object.
136class DIEAbbrev {
137private:
138 unsigned Tag; // Dwarf tag code.
139 unsigned ChildrenFlag; // Dwarf children flag.
140 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
Jim Laskey063e7652006-01-17 17:31:53 +0000141
Jim Laskey0d086af2006-02-27 12:43:29 +0000142public:
Jim Laskey063e7652006-01-17 17:31:53 +0000143
Jim Laskey0d086af2006-02-27 12:43:29 +0000144 DIEAbbrev(unsigned T, unsigned C)
145 : Tag(T)
146 , ChildrenFlag(C)
147 , Data()
148 {}
149 ~DIEAbbrev() {}
150
Jim Laskeybd761842006-02-27 17:27:12 +0000151 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000152 unsigned getTag() const { return Tag; }
153 unsigned getChildrenFlag() const { return ChildrenFlag; }
154 const std::vector<DIEAbbrevData> &getData() const { return Data; }
155 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskey063e7652006-01-17 17:31:53 +0000156
Jim Laskey0d086af2006-02-27 12:43:29 +0000157 /// operator== - Used by UniqueVector to locate entry.
158 ///
159 bool operator==(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000160
Jim Laskey0d086af2006-02-27 12:43:29 +0000161 /// operator< - Used by UniqueVector to locate entry.
162 ///
163 bool operator<(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000164
Jim Laskey0d086af2006-02-27 12:43:29 +0000165 /// AddAttribute - Adds another set of attribute information to the
166 /// abbreviation.
167 void AddAttribute(unsigned Attribute, unsigned Form) {
168 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000169 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000170
Jim Laskeyb8509c52006-03-23 18:07:55 +0000171 /// AddFirstAttribute - Adds a set of attribute information to the front
172 /// of the abbreviation.
173 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
174 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
175 }
176
Jim Laskey0d086af2006-02-27 12:43:29 +0000177 /// Emit - Print the abbreviation using the specified Dwarf writer.
178 ///
179 void Emit(const DwarfWriter &DW) const;
180
181#ifndef NDEBUG
182 void print(std::ostream &O);
183 void dump();
184#endif
185};
Jim Laskey063e7652006-01-17 17:31:53 +0000186
Jim Laskey0d086af2006-02-27 12:43:29 +0000187//===----------------------------------------------------------------------===//
188// DIEValue - A debug information entry value.
189//
190class DIEValue {
191public:
192 enum {
193 isInteger,
194 isString,
195 isLabel,
196 isAsIsLabel,
197 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000198 isEntry,
199 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000200 };
201
202 unsigned Type; // Type of the value
203
204 DIEValue(unsigned T) : Type(T) {}
205 virtual ~DIEValue() {}
206
207 // Implement isa/cast/dyncast.
208 static bool classof(const DIEValue *) { return true; }
209
210 /// EmitValue - Emit value via the Dwarf writer.
211 ///
212 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
213
214 /// SizeOf - Return the size of a value in bytes.
215 ///
216 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
217};
Jim Laskey063e7652006-01-17 17:31:53 +0000218
Jim Laskey0d086af2006-02-27 12:43:29 +0000219//===----------------------------------------------------------------------===//
220// DWInteger - An integer value DIE.
221//
222class DIEInteger : public DIEValue {
223private:
224 uint64_t Integer;
225
226public:
227 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000228
Jim Laskey0d086af2006-02-27 12:43:29 +0000229 // Implement isa/cast/dyncast.
230 static bool classof(const DIEInteger *) { return true; }
231 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
232
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000233 /// BestForm - Choose the best form for integer.
234 ///
235 unsigned BestForm(bool IsSigned);
236
Jim Laskey0d086af2006-02-27 12:43:29 +0000237 /// EmitValue - Emit integer of appropriate size.
238 ///
239 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
240
241 /// SizeOf - Determine size of integer value in bytes.
242 ///
243 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
244};
Jim Laskey063e7652006-01-17 17:31:53 +0000245
Jim Laskey0d086af2006-02-27 12:43:29 +0000246//===----------------------------------------------------------------------===//
247// DIEString - A string value DIE.
248//
249struct DIEString : public DIEValue {
250 const std::string String;
251
252 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000253
Jim Laskey0d086af2006-02-27 12:43:29 +0000254 // Implement isa/cast/dyncast.
255 static bool classof(const DIEString *) { return true; }
256 static bool classof(const DIEValue *S) { return S->Type == isString; }
257
258 /// EmitValue - Emit string value.
259 ///
260 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
261
262 /// SizeOf - Determine size of string value in bytes.
263 ///
264 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
265};
Jim Laskey063e7652006-01-17 17:31:53 +0000266
Jim Laskey0d086af2006-02-27 12:43:29 +0000267//===----------------------------------------------------------------------===//
268// DIEDwarfLabel - A Dwarf internal label expression DIE.
269//
270struct DIEDwarfLabel : public DIEValue {
271 const DWLabel Label;
272
273 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000274
Jim Laskey0d086af2006-02-27 12:43:29 +0000275 // Implement isa/cast/dyncast.
276 static bool classof(const DIEDwarfLabel *) { return true; }
277 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
278
279 /// EmitValue - Emit label value.
280 ///
281 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
282
283 /// SizeOf - Determine size of label value in bytes.
284 ///
285 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
286};
Jim Laskey063e7652006-01-17 17:31:53 +0000287
Jim Laskey063e7652006-01-17 17:31:53 +0000288
Jim Laskey0d086af2006-02-27 12:43:29 +0000289//===----------------------------------------------------------------------===//
290// DIEObjectLabel - A label to an object in code or data.
291//
292struct DIEObjectLabel : public DIEValue {
293 const std::string Label;
294
295 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000296
Jim Laskey0d086af2006-02-27 12:43:29 +0000297 // Implement isa/cast/dyncast.
298 static bool classof(const DIEObjectLabel *) { return true; }
299 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
300
301 /// EmitValue - Emit label value.
302 ///
303 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
304
305 /// SizeOf - Determine size of label value in bytes.
306 ///
307 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
308};
Jim Laskey063e7652006-01-17 17:31:53 +0000309
Jim Laskey0d086af2006-02-27 12:43:29 +0000310//===----------------------------------------------------------------------===//
311// DIEDelta - A simple label difference DIE.
312//
313struct DIEDelta : public DIEValue {
314 const DWLabel LabelHi;
315 const DWLabel LabelLo;
316
317 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
318 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000319
Jim Laskey0d086af2006-02-27 12:43:29 +0000320 // Implement isa/cast/dyncast.
321 static bool classof(const DIEDelta *) { return true; }
322 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
323
324 /// EmitValue - Emit delta value.
325 ///
326 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
327
328 /// SizeOf - Determine size of delta value in bytes.
329 ///
330 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
331};
Jim Laskey063e7652006-01-17 17:31:53 +0000332
Jim Laskey0d086af2006-02-27 12:43:29 +0000333//===----------------------------------------------------------------------===//
334// DIEntry - A pointer to a debug information entry.
335//
336struct DIEntry : public DIEValue {
337 DIE *Entry;
338
339 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
340
341 // Implement isa/cast/dyncast.
342 static bool classof(const DIEntry *) { return true; }
343 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
344
Jim Laskeyb8509c52006-03-23 18:07:55 +0000345 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000346 ///
347 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
348
Jim Laskeyb8509c52006-03-23 18:07:55 +0000349 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000350 ///
351 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
352};
353
354//===----------------------------------------------------------------------===//
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000355// DIEBlock - A block of values. Primarily used for location expressions.
356//
357struct DIEBlock : public DIEValue {
358 unsigned Size; // Size in bytes excluding size header.
359 std::vector<unsigned> Forms; // Data forms.
360 std::vector<DIEValue *> Values; // Block values.
361
362 DIEBlock()
363 : DIEValue(isBlock)
364 , Size(0)
365 , Forms()
366 , Values()
367 {}
368 ~DIEBlock();
369
370 // Implement isa/cast/dyncast.
371 static bool classof(const DIEBlock *) { return true; }
372 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
373
374 /// ComputeSize - calculate the size of the block.
375 ///
376 unsigned ComputeSize(DwarfWriter &DW);
377
378 /// BestForm - Choose the best form for data.
379 ///
380 unsigned BestForm();
381
382 /// EmitValue - Emit block data.
383 ///
384 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
385
386 /// SizeOf - Determine size of block data in bytes.
387 ///
388 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
389
390 /// AddUInt - Add an unsigned integer value.
391 ///
392 void AddUInt(unsigned Form, uint64_t Integer);
393
394 /// AddSInt - Add an signed integer value.
395 ///
396 void AddSInt(unsigned Form, int64_t Integer);
397
398 /// AddString - Add a std::string value.
399 ///
400 void AddString(unsigned Form, const std::string &String);
401
402 /// AddLabel - Add a Dwarf label value.
403 ///
404 void AddLabel(unsigned Form, const DWLabel &Label);
405
406 /// AddObjectLabel - Add a non-Dwarf label value.
407 ///
408 void AddObjectLabel(unsigned Form, const std::string &Label);
409
410 /// AddDelta - Add a label delta value.
411 ///
412 void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
413
414 /// AddDIEntry - Add a DIE value.
415 ///
416 void AddDIEntry(unsigned Form, DIE *Entry);
417
418};
419
420//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +0000421// DIE - A structured debug information entry. Has an abbreviation which
422// describes it's organization.
423class DIE {
424private:
425 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
426 unsigned AbbrevID; // Decribing abbreviation ID.
427 unsigned Offset; // Offset in debug info section.
428 unsigned Size; // Size of instance + children.
429 std::vector<DIE *> Children; // Children DIEs.
430 std::vector<DIEValue *> Values; // Attributes values.
431
432public:
433 DIE(unsigned Tag);
434 ~DIE();
435
Jim Laskeybd761842006-02-27 17:27:12 +0000436 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000437 unsigned getAbbrevID() const { return AbbrevID; }
438 unsigned getOffset() const { return Offset; }
439 unsigned getSize() const { return Size; }
440 const std::vector<DIE *> &getChildren() const { return Children; }
441 const std::vector<DIEValue *> &getValues() const { return Values; }
442 void setOffset(unsigned O) { Offset = O; }
443 void setSize(unsigned S) { Size = S; }
444
445 /// SiblingOffset - Return the offset of the debug information entry's
446 /// sibling.
447 unsigned SiblingOffset() const { return Offset + Size; }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000448
449 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
450 ///
451 void AddSiblingOffset();
Jim Laskey0d086af2006-02-27 12:43:29 +0000452
453 /// AddUInt - Add an unsigned integer attribute data and value.
454 ///
455 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
456
457 /// AddSInt - Add an signed integer attribute data and value.
458 ///
459 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
460
461 /// AddString - Add a std::string attribute data and value.
462 ///
463 void AddString(unsigned Attribute, unsigned Form,
464 const std::string &String);
465
466 /// AddLabel - Add a Dwarf label attribute data and value.
467 ///
468 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
469
470 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
471 ///
472 void AddObjectLabel(unsigned Attribute, unsigned Form,
473 const std::string &Label);
474
475 /// AddDelta - Add a label delta attribute data and value.
476 ///
477 void AddDelta(unsigned Attribute, unsigned Form,
478 const DWLabel &Hi, const DWLabel &Lo);
479
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000480 /// AddDIEntry - Add a DIE attribute data and value.
Jim Laskey0d086af2006-02-27 12:43:29 +0000481 ///
482 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
483
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000484 /// AddBlock - Add block data.
485 ///
486 void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
487
Jim Laskey0d086af2006-02-27 12:43:29 +0000488 /// Complete - Indicate that all attributes have been added and
489 /// ready to get an abbreviation ID.
490 ///
491 void Complete(DwarfWriter &DW);
492
493 /// AddChild - Add a child to the DIE.
494 void AddChild(DIE *Child);
495};
496
497} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +0000498
499//===----------------------------------------------------------------------===//
500
Jim Laskeybd761842006-02-27 17:27:12 +0000501CompileUnit::~CompileUnit() {
502 delete Die;
503}
504
505/// hasContent - Return true if this compile unit has something to write out.
506///
507bool CompileUnit::hasContent() const {
508 return !Die->getChildren().empty();
509}
510
511/// AddGlobal - Add a new global entity to the compile unit.
512///
513void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
514 Globals[Name] = Die;
515}
516
517//===----------------------------------------------------------------------===//
518
Jim Laskeyd18e2892006-01-20 20:34:06 +0000519/// operator== - Used by UniqueVector to locate entry.
520///
521bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
522 if (Tag != DA.Tag) return false;
523 if (ChildrenFlag != DA.ChildrenFlag) return false;
524 if (Data.size() != DA.Data.size()) return false;
525
Jim Laskey52060a02006-01-24 00:49:18 +0000526 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000527 if (Data[i] != DA.Data[i]) return false;
Jim Laskeyd18e2892006-01-20 20:34:06 +0000528 }
529
530 return true;
531}
532
533/// operator< - Used by UniqueVector to locate entry.
534///
535bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
536 if (Tag != DA.Tag) return Tag < DA.Tag;
537 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
538 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
539
Jim Laskey52060a02006-01-24 00:49:18 +0000540 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000541 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +0000542 }
543
544 return false;
545}
546
547/// Emit - Print the abbreviation using the specified Dwarf writer.
548///
549void DIEAbbrev::Emit(const DwarfWriter &DW) const {
550 // Emit its Dwarf tag type.
551 DW.EmitULEB128Bytes(Tag);
552 DW.EOL(TagString(Tag));
553
554 // Emit whether it has children DIEs.
555 DW.EmitULEB128Bytes(ChildrenFlag);
556 DW.EOL(ChildrenString(ChildrenFlag));
557
558 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +0000559 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000560 const DIEAbbrevData &AttrData = Data[i];
561
562 // Emit attribute type.
563 DW.EmitULEB128Bytes(AttrData.getAttribute());
564 DW.EOL(AttributeString(AttrData.getAttribute()));
565
566 // Emit form type.
567 DW.EmitULEB128Bytes(AttrData.getForm());
568 DW.EOL(FormEncodingString(AttrData.getForm()));
569 }
570
571 // Mark end of abbreviation.
572 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
573 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
574}
575
576#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +0000577void DIEAbbrev::print(std::ostream &O) {
578 O << "Abbreviation @"
579 << std::hex << (intptr_t)this << std::dec
580 << " "
581 << TagString(Tag)
582 << " "
583 << ChildrenString(ChildrenFlag)
584 << "\n";
585
586 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
587 O << " "
588 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +0000589 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +0000590 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +0000591 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +0000592 }
Jim Laskeya0f3d172006-09-07 22:06:40 +0000593}
594void DIEAbbrev::dump() { print(std::cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +0000595#endif
596
597//===----------------------------------------------------------------------===//
598
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000599/// BestForm - Choose the best form for integer.
600///
601unsigned DIEInteger::BestForm(bool IsSigned) {
602 if (IsSigned) {
603 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
604 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
605 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
606 } else {
607 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
608 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
609 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
610 }
611 return DW_FORM_data8;
612}
613
Jim Laskey063e7652006-01-17 17:31:53 +0000614/// EmitValue - Emit integer of appropriate size.
615///
616void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
617 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +0000618 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000619 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000620 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000621 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000622 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000623 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000624 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000625 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000626 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +0000627 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
628 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +0000629 default: assert(0 && "DIE Value form not supported yet"); break;
630 }
631}
632
633/// SizeOf - Determine size of integer value in bytes.
634///
635unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
636 switch (Form) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000637 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000638 case DW_FORM_ref1: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000639 case DW_FORM_data1: return sizeof(int8_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000640 case DW_FORM_ref2: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000641 case DW_FORM_data2: return sizeof(int16_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000642 case DW_FORM_ref4: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000643 case DW_FORM_data4: return sizeof(int32_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000644 case DW_FORM_ref8: // Fall thru
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000645 case DW_FORM_data8: return sizeof(int64_t);
Jim Laskey40020172006-01-20 21:02:36 +0000646 case DW_FORM_udata: return DW.SizeULEB128(Integer);
647 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +0000648 default: assert(0 && "DIE Value form not supported yet"); break;
649 }
650 return 0;
651}
652
653//===----------------------------------------------------------------------===//
654
655/// EmitValue - Emit string value.
656///
657void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000658 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +0000659}
660
661/// SizeOf - Determine size of string value in bytes.
662///
663unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey40020172006-01-20 21:02:36 +0000664 return String.size() + sizeof(char); // sizeof('\0');
Jim Laskey063e7652006-01-17 17:31:53 +0000665}
666
667//===----------------------------------------------------------------------===//
668
669/// EmitValue - Emit label value.
670///
Jim Laskey52060a02006-01-24 00:49:18 +0000671void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000672 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +0000673}
674
675/// SizeOf - Determine size of label value in bytes.
676///
Jim Laskey52060a02006-01-24 00:49:18 +0000677unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000678 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +0000679}
680
681//===----------------------------------------------------------------------===//
682
Jim Laskeyd18e2892006-01-20 20:34:06 +0000683/// EmitValue - Emit label value.
684///
Jim Laskey52060a02006-01-24 00:49:18 +0000685void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000686 DW.EmitReference(Label);
687}
688
689/// SizeOf - Determine size of label value in bytes.
690///
Jim Laskey52060a02006-01-24 00:49:18 +0000691unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000692 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +0000693}
694
695//===----------------------------------------------------------------------===//
696
Jim Laskey063e7652006-01-17 17:31:53 +0000697/// EmitValue - Emit delta value.
698///
699void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000700 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +0000701}
702
703/// SizeOf - Determine size of delta value in bytes.
704///
705unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000706 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +0000707}
708
709//===----------------------------------------------------------------------===//
Jim Laskeyb8509c52006-03-23 18:07:55 +0000710/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000711///
712void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +0000713 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +0000714}
715
Jim Laskeyb8509c52006-03-23 18:07:55 +0000716/// SizeOf - Determine size of debug information entry value in bytes.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000717///
718unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
719 return sizeof(int32_t);
720}
721
722//===----------------------------------------------------------------------===//
723
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000724DIEBlock::~DIEBlock() {
725 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
726 delete Values[i];
727 }
728}
729
730/// ComputeSize - calculate the size of the block.
731///
732unsigned DIEBlock::ComputeSize(DwarfWriter &DW) {
733 Size = 0;
734 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
735 Size += Values[i]->SizeOf(DW, Forms[i]);
736 }
737 return Size;
738}
739
740/// BestForm - Choose the best form for data.
741///
742unsigned DIEBlock::BestForm() {
743 if ((unsigned char)Size == Size) return DW_FORM_block1;
744 if ((unsigned short)Size == Size) return DW_FORM_block2;
745 if ((unsigned int)Size == Size) return DW_FORM_block4;
746 return DW_FORM_block;
747}
748
749/// EmitValue - Emit block data.
750///
751void DIEBlock::EmitValue(const DwarfWriter &DW, unsigned Form) const {
752 switch (Form) {
753 case DW_FORM_block1: DW.EmitInt8(Size); break;
754 case DW_FORM_block2: DW.EmitInt16(Size); break;
755 case DW_FORM_block4: DW.EmitInt32(Size); break;
756 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
757 default: assert(0 && "Improper form for block"); break;
758 }
759 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
760 DW.EOL("");
761 Values[i]->EmitValue(DW, Forms[i]);
762 }
763}
764
765/// SizeOf - Determine size of block data in bytes.
766///
767unsigned DIEBlock::SizeOf(const DwarfWriter &DW, unsigned Form) const {
768 switch (Form) {
769 case DW_FORM_block1: return Size + sizeof(int8_t);
770 case DW_FORM_block2: return Size + sizeof(int16_t);
771 case DW_FORM_block4: return Size + sizeof(int32_t);
772 case DW_FORM_block: return Size + DW.SizeULEB128(Size);
773 default: assert(0 && "Improper form for block"); break;
774 }
775 return 0;
776}
777
778/// AddUInt - Add an unsigned integer value.
779///
780void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
781 DIEInteger *DI = new DIEInteger(Integer);
782 Values.push_back(DI);
783 if (Form == 0) Form = DI->BestForm(false);
784 Forms.push_back(Form);
785}
786
787/// AddSInt - Add an signed integer value.
788///
789void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
790 DIEInteger *DI = new DIEInteger(Integer);
791 Values.push_back(DI);
792 if (Form == 0) Form = DI->BestForm(true);
793 Forms.push_back(Form);
794}
795
796/// AddString - Add a std::string value.
797///
798void DIEBlock::AddString(unsigned Form, const std::string &String) {
799 Values.push_back(new DIEString(String));
800 Forms.push_back(Form);
801}
802
803/// AddLabel - Add a Dwarf label value.
804///
805void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
806 Values.push_back(new DIEDwarfLabel(Label));
807 Forms.push_back(Form);
808}
809
810/// AddObjectLabel - Add a non-Dwarf label value.
811///
812void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
813 Values.push_back(new DIEObjectLabel(Label));
814 Forms.push_back(Form);
815}
816
817/// AddDelta - Add a label delta value.
818///
819void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
820 Values.push_back(new DIEDelta(Hi, Lo));
821 Forms.push_back(Form);
822}
823
824/// AddDIEntry - Add a DIE value.
825///
826void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
827 Values.push_back(new DIEntry(Entry));
828 Forms.push_back(Form);
829}
830
831//===----------------------------------------------------------------------===//
832
Jim Laskey0420f2a2006-02-22 19:02:11 +0000833DIE::DIE(unsigned Tag)
834: Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
Jim Laskeyd18e2892006-01-20 20:34:06 +0000835, AbbrevID(0)
836, Offset(0)
837, Size(0)
Jim Laskeyd18e2892006-01-20 20:34:06 +0000838, Children()
839, Values()
840{}
841
842DIE::~DIE() {
843 if (Abbrev) delete Abbrev;
844
Jim Laskey52060a02006-01-24 00:49:18 +0000845 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000846 delete Children[i];
847 }
848
Jim Laskey52060a02006-01-24 00:49:18 +0000849 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000850 delete Values[j];
851 }
Jim Laskeyd18e2892006-01-20 20:34:06 +0000852}
853
Jim Laskeyb8509c52006-03-23 18:07:55 +0000854/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
855///
856void DIE::AddSiblingOffset() {
857 DIEInteger *DI = new DIEInteger(0);
858 Values.insert(Values.begin(), DI);
859 Abbrev->AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
860}
861
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000862/// AddUInt - Add an unsigned integer attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000863///
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000864void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000865 DIEInteger *DI = new DIEInteger(Integer);
866 Values.push_back(DI);
867 if (!Form) Form = DI->BestForm(false);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000868 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000869}
870
871/// AddSInt - Add an signed integer attribute data and value.
872///
873void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000874 DIEInteger *DI = new DIEInteger(Integer);
875 Values.push_back(DI);
876 if (!Form) Form = DI->BestForm(true);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000877 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000878}
879
880/// AddString - Add a std::string attribute data and value.
881///
882void DIE::AddString(unsigned Attribute, unsigned Form,
883 const std::string &String) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000884 Values.push_back(new DIEString(String));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000885 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000886}
887
888/// AddLabel - Add a Dwarf label attribute data and value.
889///
890void DIE::AddLabel(unsigned Attribute, unsigned Form,
891 const DWLabel &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000892 Values.push_back(new DIEDwarfLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000893 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000894}
895
Jim Laskey52060a02006-01-24 00:49:18 +0000896/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000897///
Jim Laskey52060a02006-01-24 00:49:18 +0000898void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
899 const std::string &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000900 Values.push_back(new DIEObjectLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000901 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000902}
903
904/// AddDelta - Add a label delta attribute data and value.
905///
906void DIE::AddDelta(unsigned Attribute, unsigned Form,
907 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000908 Values.push_back(new DIEDelta(Hi, Lo));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000909 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000910}
911
912/// AddDIEntry - Add a DIE attribute data and value.
913///
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000914void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000915 Values.push_back(new DIEntry(Entry));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000916 Abbrev->AddAttribute(Attribute, Form);
917}
918
919/// AddBlock - Add block data.
920///
921void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
922 assert(Block->Size && "Block size has not been computed");
923 Values.push_back(Block);
924 if (!Form) Form = Block->BestForm();
925 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000926}
927
928/// Complete - Indicate that all attributes have been added and ready to get an
929/// abbreviation ID.
930void DIE::Complete(DwarfWriter &DW) {
931 AbbrevID = DW.NewAbbreviation(Abbrev);
932 delete Abbrev;
933 Abbrev = NULL;
934}
935
936/// AddChild - Add a child to the DIE.
937///
938void DIE::AddChild(DIE *Child) {
Jim Laskey0420f2a2006-02-22 19:02:11 +0000939 assert(Abbrev && "Adding children without an abbreviation");
940 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000941 Children.push_back(Child);
942}
943
944//===----------------------------------------------------------------------===//
945
Jim Laskey3ebe71d2006-09-01 12:55:05 +0000946/// DwarfWriter
Jim Laskeyd18e2892006-01-20 20:34:06 +0000947
948//===----------------------------------------------------------------------===//
Jim Laskey063e7652006-01-17 17:31:53 +0000949
950/// PrintHex - Print a value as a hexidecimal value.
951///
952void DwarfWriter::PrintHex(int Value) const {
953 O << "0x" << std::hex << Value << std::dec;
954}
955
956/// EOL - Print a newline character to asm stream. If a comment is present
957/// then it will be printed first. Comments should not contain '\n'.
958void DwarfWriter::EOL(const std::string &Comment) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000959 if (DwarfVerbose && !Comment.empty()) {
Jim Laskey063e7652006-01-17 17:31:53 +0000960 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +0000961 << TAI->getCommentString()
Jim Laskey063e7652006-01-17 17:31:53 +0000962 << " "
963 << Comment;
964 }
965 O << "\n";
966}
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000967
Jim Laskeyb8509c52006-03-23 18:07:55 +0000968/// EmitAlign - Print a align directive.
969///
970void DwarfWriter::EmitAlign(unsigned Alignment) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000971 O << TAI->getAlignDirective() << Alignment << "\n";
Jim Laskeyb8509c52006-03-23 18:07:55 +0000972}
973
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000974/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
Jim Laskey063e7652006-01-17 17:31:53 +0000975/// unsigned leb128 value.
976void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000977 if (TAI->hasLEB128()) {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000978 O << "\t.uleb128\t"
979 << Value;
980 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000981 O << TAI->getData8bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +0000982 PrintULEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000983 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000984}
985
986/// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
Jim Laskey063e7652006-01-17 17:31:53 +0000987/// signed leb128 value.
988void DwarfWriter::EmitSLEB128Bytes(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000989 if (TAI->hasLEB128()) {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000990 O << "\t.sleb128\t"
991 << Value;
992 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000993 O << TAI->getData8bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +0000994 PrintSLEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000995 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000996}
997
Jim Laskey063e7652006-01-17 17:31:53 +0000998/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +0000999/// representing an unsigned leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +00001000void DwarfWriter::PrintULEB128(unsigned Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001001 do {
1002 unsigned Byte = Value & 0x7f;
1003 Value >>= 7;
1004 if (Value) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001005 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001006 if (Value) O << ", ";
1007 } while (Value);
1008}
1009
Jim Laskey063e7652006-01-17 17:31:53 +00001010/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
1011/// value.
1012unsigned DwarfWriter::SizeULEB128(unsigned Value) {
1013 unsigned Size = 0;
1014 do {
1015 Value >>= 7;
1016 Size += sizeof(int8_t);
1017 } while (Value);
1018 return Size;
1019}
1020
1021/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +00001022/// representing a signed leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +00001023void DwarfWriter::PrintSLEB128(int Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001024 int Sign = Value >> (8 * sizeof(Value) - 1);
1025 bool IsMore;
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001026
Jim Laskeyb2efb852006-01-04 22:28:25 +00001027 do {
1028 unsigned Byte = Value & 0x7f;
1029 Value >>= 7;
1030 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1031 if (IsMore) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001032 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001033 if (IsMore) O << ", ";
1034 } while (IsMore);
1035}
1036
Jim Laskey063e7652006-01-17 17:31:53 +00001037/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
1038/// value.
1039unsigned DwarfWriter::SizeSLEB128(int Value) {
1040 unsigned Size = 0;
1041 int Sign = Value >> (8 * sizeof(Value) - 1);
1042 bool IsMore;
1043
1044 do {
1045 unsigned Byte = Value & 0x7f;
1046 Value >>= 7;
1047 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1048 Size += sizeof(int8_t);
1049 } while (IsMore);
1050 return Size;
1051}
1052
Jim Laskeyda427fa2006-01-27 20:31:25 +00001053/// EmitInt8 - Emit a byte directive and value.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001054///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001055void DwarfWriter::EmitInt8(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001056 O << TAI->getData8bitsDirective();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001057 PrintHex(Value & 0xFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001058}
1059
Jim Laskeyda427fa2006-01-27 20:31:25 +00001060/// EmitInt16 - Emit a short directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001061///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001062void DwarfWriter::EmitInt16(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001063 O << TAI->getData16bitsDirective();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001064 PrintHex(Value & 0xFFFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001065}
1066
Jim Laskeyda427fa2006-01-27 20:31:25 +00001067/// EmitInt32 - Emit a long directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001068///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001069void DwarfWriter::EmitInt32(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001070 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001071 PrintHex(Value);
1072}
1073
Jim Laskeyda427fa2006-01-27 20:31:25 +00001074/// EmitInt64 - Emit a long long directive and value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001075///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001076void DwarfWriter::EmitInt64(uint64_t Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001077 if (TAI->getData64bitsDirective()) {
1078 O << TAI->getData64bitsDirective() << "0x" << std::hex << Value << std::dec;
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001079 } else {
Owen Andersona69571c2006-05-03 01:29:57 +00001080 if (TD->isBigEndian()) {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001081 EmitInt32(unsigned(Value >> 32)); O << "\n";
1082 EmitInt32(unsigned(Value));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001083 } else {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001084 EmitInt32(unsigned(Value)); O << "\n";
1085 EmitInt32(unsigned(Value >> 32));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001086 }
1087 }
1088}
1089
Jim Laskey063e7652006-01-17 17:31:53 +00001090/// EmitString - Emit a string with quotes and a null terminator.
1091/// Special characters are emitted properly. (Eg. '\t')
1092void DwarfWriter::EmitString(const std::string &String) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001093 O << TAI->getAsciiDirective()
Jim Laskey063e7652006-01-17 17:31:53 +00001094 << "\"";
Jim Laskey52060a02006-01-24 00:49:18 +00001095 for (unsigned i = 0, N = String.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001096 unsigned char C = String[i];
1097
1098 if (!isascii(C) || iscntrl(C)) {
1099 switch(C) {
1100 case '\b': O << "\\b"; break;
1101 case '\f': O << "\\f"; break;
1102 case '\n': O << "\\n"; break;
1103 case '\r': O << "\\r"; break;
1104 case '\t': O << "\\t"; break;
1105 default:
1106 O << '\\';
Jim Laskey3ebe71d2006-09-01 12:55:05 +00001107 O << char('0' + ((C >> 6) & 7));
1108 O << char('0' + ((C >> 3) & 7));
1109 O << char('0' + ((C >> 0) & 7));
Jim Laskey063e7652006-01-17 17:31:53 +00001110 break;
1111 }
1112 } else if (C == '\"') {
1113 O << "\\\"";
1114 } else if (C == '\'') {
1115 O << "\\\'";
1116 } else {
1117 O << C;
1118 }
1119 }
1120 O << "\\0\"";
1121}
1122
1123/// PrintLabelName - Print label name in form used by Dwarf writer.
1124///
1125void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001126 O << TAI->getPrivateGlobalPrefix()
Jim Laskeyb2efb852006-01-04 22:28:25 +00001127 << "debug_"
Jim Laskey0420f2a2006-02-22 19:02:11 +00001128 << Tag;
1129 if (Number) O << Number;
Jim Laskeyb2efb852006-01-04 22:28:25 +00001130}
1131
Jim Laskey063e7652006-01-17 17:31:53 +00001132/// EmitLabel - Emit location label for internal use by Dwarf.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001133///
Jim Laskey063e7652006-01-17 17:31:53 +00001134void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
1135 PrintLabelName(Tag, Number);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001136 O << ":\n";
1137}
1138
Jim Laskeye719a7c2006-01-18 16:54:26 +00001139/// EmitReference - Emit a reference to a label.
Jim Laskey063e7652006-01-17 17:31:53 +00001140///
Jim Laskeye719a7c2006-01-18 16:54:26 +00001141void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001142 if (TAI->getAddressSize() == 4)
1143 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001144 else
Jim Laskey563321a2006-09-06 18:34:40 +00001145 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001146
1147 PrintLabelName(Tag, Number);
1148}
Jim Laskey73683212006-01-21 00:59:54 +00001149void DwarfWriter::EmitReference(const std::string &Name) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001150 if (TAI->getAddressSize() == 4)
1151 O << TAI->getData32bitsDirective();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001152 else
Jim Laskey563321a2006-09-06 18:34:40 +00001153 O << TAI->getData64bitsDirective();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001154
1155 O << Name;
1156}
Jim Laskey063e7652006-01-17 17:31:53 +00001157
1158/// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
1159/// assemblers do not accept absolute expressions with data directives, so there
1160/// is an option (needsSet) to use an intermediary 'set' expression.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001161void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
1162 const char *TagLo, unsigned NumberLo) const {
Jim Laskeya0f3d172006-09-07 22:06:40 +00001163 if (TAI->needsSet()) {
Jim Laskey063e7652006-01-17 17:31:53 +00001164 static unsigned SetCounter = 0;
Jim Laskeyd18e2892006-01-20 20:34:06 +00001165
Jim Laskey063e7652006-01-17 17:31:53 +00001166 O << "\t.set\t";
1167 PrintLabelName("set", SetCounter);
1168 O << ",";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001169 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001170 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001171 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001172 O << "\n";
1173
Jim Laskey563321a2006-09-06 18:34:40 +00001174 if (TAI->getAddressSize() == sizeof(int32_t))
1175 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001176 else
Jim Laskey563321a2006-09-06 18:34:40 +00001177 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001178
1179 PrintLabelName("set", SetCounter);
1180
Jim Laskey52060a02006-01-24 00:49:18 +00001181 ++SetCounter;
Jim Laskey063e7652006-01-17 17:31:53 +00001182 } else {
Jim Laskey563321a2006-09-06 18:34:40 +00001183 if (TAI->getAddressSize() == sizeof(int32_t))
1184 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001185 else
Jim Laskey563321a2006-09-06 18:34:40 +00001186 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001187
Jim Laskeyd18e2892006-01-20 20:34:06 +00001188 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001189 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001190 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001191 }
1192}
1193
Jim Laskeyd18e2892006-01-20 20:34:06 +00001194/// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
Jim Laskey063e7652006-01-17 17:31:53 +00001195///
Jim Laskeyd18e2892006-01-20 20:34:06 +00001196unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
1197 return Abbreviations.insert(*Abbrev);
1198}
1199
1200/// NewString - Add a string to the constant pool and returns a label.
1201///
1202DWLabel DwarfWriter::NewString(const std::string &String) {
1203 unsigned StringID = StringPool.insert(String);
1204 return DWLabel("string", StringID);
1205}
1206
Jim Laskeyb8509c52006-03-23 18:07:55 +00001207/// AddSourceLine - Add location information to specified debug information
1208/// entry.
Jim Laskeyd16f2a72006-06-19 19:49:42 +00001209void DwarfWriter::AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line){
Jim Laskeyb8509c52006-03-23 18:07:55 +00001210 if (File && Line) {
1211 CompileUnit *FileUnit = FindCompileUnit(File);
1212 unsigned FileID = FileUnit->getID();
1213 Die->AddUInt(DW_AT_decl_file, 0, FileID);
1214 Die->AddUInt(DW_AT_decl_line, 0, Line);
1215 }
1216}
1217
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001218/// AddAddress - Add an address attribute to a die based on the location
1219/// provided.
1220void DwarfWriter::AddAddress(DIE *Die, unsigned Attribute,
Jim Laskey41886992006-04-07 16:34:46 +00001221 const MachineLocation &Location) {
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001222 DIEBlock *Block = new DIEBlock();
Jim Laskey3d3d4042006-08-25 19:39:52 +00001223 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1224
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001225 if (Location.isRegister()) {
Jim Laskey3d3d4042006-08-25 19:39:52 +00001226 if (Reg < 32) {
1227 Block->AddUInt(DW_FORM_data1, DW_OP_reg0 + Reg);
1228 } else {
1229 Block->AddUInt(DW_FORM_data1, DW_OP_regx);
1230 Block->AddUInt(DW_FORM_udata, Reg);
1231 }
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001232 } else {
Jim Laskey3d3d4042006-08-25 19:39:52 +00001233 if (Reg < 32) {
1234 Block->AddUInt(DW_FORM_data1, DW_OP_breg0 + Reg);
1235 } else {
1236 Block->AddUInt(DW_FORM_data1, DW_OP_bregx);
1237 Block->AddUInt(DW_FORM_udata, Reg);
1238 }
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001239 Block->AddUInt(DW_FORM_sdata, Location.getOffset());
1240 }
1241 Block->ComputeSize(*this);
1242 Die->AddBlock(Attribute, 0, Block);
1243}
1244
Jim Laskey90c79d72006-03-23 23:02:34 +00001245/// getDieMapSlotFor - Returns the debug information entry map slot for the
1246/// specified debug descriptor.
1247DIE *&DwarfWriter::getDieMapSlotFor(DebugInfoDesc *DD) {
1248 return DescToDieMap[DD];
Jim Laskey0420f2a2006-02-22 19:02:11 +00001249}
Jim Laskey760383e2006-08-21 21:20:18 +00001250
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001251/// NewType - Create a new type DIE.
1252///
Jim Laskey90c79d72006-03-23 23:02:34 +00001253DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit) {
1254 if (!TyDesc) {
1255 // FIXME - Hack for missing types
1256 DIE *Die = new DIE(DW_TAG_base_type);
1257 Die->AddUInt(DW_AT_byte_size, 0, 4);
1258 Die->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1259 Unit->getDie()->AddChild(Die);
1260 return Die;
1261 }
Jim Laskey92ae7402006-03-01 18:20:30 +00001262
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001263 // Check for pre-existence.
Jim Laskey90c79d72006-03-23 23:02:34 +00001264 DIE *&Slot = Unit->getDieMapSlotFor(TyDesc);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001265 if (Slot) return Slot;
1266
1267 // Get core information.
1268 const std::string &Name = TyDesc->getName();
Jim Laskey288fe0f2006-03-01 18:13:05 +00001269 uint64_t Size = TyDesc->getSize() >> 3;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001270
Jim Laskey434b40b2006-02-23 22:37:30 +00001271 DIE *Ty = NULL;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001272
Jim Laskey434b40b2006-02-23 22:37:30 +00001273 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001274 // Fundamental types like int, float, bool
Jim Laskey434b40b2006-02-23 22:37:30 +00001275 Slot = Ty = new DIE(DW_TAG_base_type);
1276 unsigned Encoding = BasicTy->getEncoding();
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001277 Ty->AddUInt(DW_AT_encoding, DW_FORM_data1, Encoding);
Jim Laskey69906002006-02-24 16:46:40 +00001278 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001279 // Create specific DIE.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001280 Slot = Ty = new DIE(DerivedTy->getTag());
Jim Laskey69906002006-02-24 16:46:40 +00001281
1282 // Map to main type, void will not have a type.
1283 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001284 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1285 NewType(Context, FromTy, Unit));
Jim Laskey69906002006-02-24 16:46:40 +00001286 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001287 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
Jim Laskey7089f452006-06-16 13:14:03 +00001288 // Fetch tag
1289 unsigned Tag = CompTy->getTag();
1290
Jim Laskeyf8913f12006-03-01 17:53:02 +00001291 // Create specific DIE.
Jim Laskey7089f452006-06-16 13:14:03 +00001292 Slot = Ty = Tag == DW_TAG_vector_type ? new DIE(DW_TAG_array_type) :
1293 new DIE(Tag);
1294
Jim Laskeyf8913f12006-03-01 17:53:02 +00001295 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1296
Jim Laskey7089f452006-06-16 13:14:03 +00001297 switch (Tag) {
1298 case DW_TAG_vector_type: Ty->AddUInt(DW_AT_GNU_vector, DW_FORM_flag, 1);
1299 // Fall thru
Jim Laskey9c4447a2006-03-01 20:39:36 +00001300 case DW_TAG_array_type: {
Jim Laskeyf8913f12006-03-01 17:53:02 +00001301 // Add element type.
1302 if (TypeDesc *FromTy = CompTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001303 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1304 NewType(Context, FromTy, Unit));
Jim Laskeyf8913f12006-03-01 17:53:02 +00001305 }
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001306
Jim Laskeyf8913f12006-03-01 17:53:02 +00001307 // Don't emit size attribute.
1308 Size = 0;
1309
1310 // Construct an anonymous type for index type.
1311 DIE *IndexTy = new DIE(DW_TAG_base_type);
1312 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1313 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1314 // Add to context.
Jim Laskey92ae7402006-03-01 18:20:30 +00001315 Context->AddChild(IndexTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001316
1317 // Add subranges to array type.
1318 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1319 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1320 int64_t Lo = SRD->getLo();
1321 int64_t Hi = SRD->getHi();
1322 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1323
1324 // If a range is available.
1325 if (Lo != Hi) {
1326 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1327 // Only add low if non-zero.
Jim Laskey6a3eb012006-03-01 23:52:37 +00001328 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1329 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001330 }
1331 Ty->AddChild(Subrange);
1332 }
1333
1334 break;
1335 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001336 case DW_TAG_structure_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +00001337 case DW_TAG_union_type: {
Jim Laskeyf01e5472006-03-03 15:06:57 +00001338 // Add elements to structure type.
1339 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskey760383e2006-08-21 21:20:18 +00001340 DebugInfoDesc *Element = Elements[i];
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001341
Jim Laskey760383e2006-08-21 21:20:18 +00001342 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)) {
1343 // Add field or base class.
Jim Laskey54689c22006-03-09 13:28:47 +00001344
Jim Laskey760383e2006-08-21 21:20:18 +00001345 unsigned Tag = MemberDesc->getTag();
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001346
Jim Laskey760383e2006-08-21 21:20:18 +00001347 // Extract the basic information.
1348 const std::string &Name = MemberDesc->getName();
1349 TypeDesc *MemTy = MemberDesc->getFromType();
1350 uint64_t Size = MemberDesc->getSize();
1351 uint64_t Align = MemberDesc->getAlign();
1352 uint64_t Offset = MemberDesc->getOffset();
1353
1354 // Construct member debug information entry.
1355 DIE *Member = new DIE(Tag);
1356
1357 // Add name if not "".
1358 if (!Name.empty())Member->AddString(DW_AT_name, DW_FORM_string, Name);
1359 // Add location if available.
1360 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1361
1362 // Most of the time the field info is the same as the members.
1363 uint64_t FieldSize = Size;
1364 uint64_t FieldAlign = Align;
1365 uint64_t FieldOffset = Offset;
1366
1367 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1368 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1369 NewType(Context, FromTy, Unit));
1370 FieldSize = FromTy->getSize();
1371 FieldAlign = FromTy->getSize();
1372 }
1373
1374 // Unless we have a bit field.
1375 if (Tag == DW_TAG_member && FieldSize != Size) {
1376 // Construct the alignment mask.
1377 uint64_t AlignMask = ~(FieldAlign - 1);
1378 // Determine the high bit + 1 of the declared size.
1379 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1380 // Work backwards to determine the base offset of the field.
1381 FieldOffset = HiMark - FieldSize;
1382 // Now normalize offset to the field.
1383 Offset -= FieldOffset;
1384
1385 // Maybe we need to work from the other end.
1386 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1387
1388 // Add size and offset.
1389 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1390 Member->AddUInt(DW_AT_bit_size, 0, Size);
1391 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
1392 }
1393
1394 // Add computation for offset.
1395 DIEBlock *Block = new DIEBlock();
1396 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
1397 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
1398 Block->ComputeSize(*this);
1399 Member->AddBlock(DW_AT_data_member_location, 0, Block);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001400
Jim Laskey760383e2006-08-21 21:20:18 +00001401 // Add accessibility (public default unless is base class.
1402 if (MemberDesc->isProtected()) {
1403 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_protected);
1404 } else if (MemberDesc->isPrivate()) {
1405 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_private);
1406 } else if (Tag == DW_TAG_inheritance) {
1407 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_public);
1408 }
1409
1410 Ty->AddChild(Member);
1411 } else if (GlobalVariableDesc *StaticDesc =
1412 dyn_cast<GlobalVariableDesc>(Element)) {
1413 // Add static member.
1414
1415 // Construct member debug information entry.
1416 DIE *Static = new DIE(DW_TAG_variable);
1417
1418 // Add name and mangled name.
1419 const std::string &Name = StaticDesc->getDisplayName();
1420 const std::string &MangledName = StaticDesc->getName();
1421 Static->AddString(DW_AT_name, DW_FORM_string, Name);
1422 Static->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1423 MangledName);
1424
1425 // Add location.
1426 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1427
1428 // Add type.
1429 if (TypeDesc *StaticTy = StaticDesc->getType()) {
1430 Static->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1431 NewType(Context, StaticTy, Unit));
1432 }
1433
1434 // Add flags.
1435 Static->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1436 Static->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1437
1438 Ty->AddChild(Static);
1439 } else if (SubprogramDesc *MethodDesc =
1440 dyn_cast<SubprogramDesc>(Element)) {
1441 // Add member function.
1442
1443 // Construct member debug information entry.
1444 DIE *Method = new DIE(DW_TAG_subprogram);
1445
1446 // Add name and mangled name.
1447 const std::string &Name = MethodDesc->getDisplayName();
1448 const std::string &MangledName = MethodDesc->getName();
1449 bool IsCTor = false;
1450
1451 if (Name.empty()) {
1452 Method->AddString(DW_AT_name, DW_FORM_string, MangledName);
1453 IsCTor = TyDesc->getName() == MangledName;
1454 } else {
1455 Method->AddString(DW_AT_name, DW_FORM_string, Name);
1456 Method->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1457 MangledName);
1458 }
1459
1460 // Add location.
1461 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1462
1463 // Add type.
1464 if (CompositeTypeDesc *MethodTy =
1465 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1466 // Get argument information.
1467 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1468
1469 // If not a ctor.
1470 if (!IsCTor) {
1471 // Add return type.
1472 Method->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1473 NewType(Context, dyn_cast<TypeDesc>(Args[0]),
1474 Unit));
1475 }
1476
1477 // Add arguments.
1478 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1479 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1480 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1481 NewType(Context, cast<TypeDesc>(Args[i]), Unit));
1482 Arg->AddUInt(DW_AT_artificial, DW_FORM_flag, 1);
1483 Method->AddChild(Arg);
1484 }
1485 }
1486
1487 // Add flags.
1488 Method->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1489 Method->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1490
1491 Ty->AddChild(Method);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001492 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001493 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001494 break;
1495 }
Jim Laskey9c4447a2006-03-01 20:39:36 +00001496 case DW_TAG_enumeration_type: {
Jim Laskey6a3eb012006-03-01 23:52:37 +00001497 // Add enumerators to enumeration type.
1498 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1499 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1500 const std::string &Name = ED->getName();
1501 int64_t Value = ED->getValue();
1502 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1503 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1504 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1505 Ty->AddChild(Enumerator);
1506 }
1507
Jim Laskeyf8913f12006-03-01 17:53:02 +00001508 break;
1509 }
Jim Laskey650f6092006-06-20 19:41:06 +00001510 case DW_TAG_subroutine_type: {
1511 // Add prototype flag.
1512 Ty->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
1513 // Add return type.
1514 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
Jim Laskeyd04c1592006-07-13 15:27:42 +00001515 NewType(Context, dyn_cast<TypeDesc>(Elements[0]), Unit));
Jim Laskey650f6092006-06-20 19:41:06 +00001516
1517 // Add arguments.
1518 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1519 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1520 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1521 NewType(Context, cast<TypeDesc>(Elements[i]), Unit));
1522 Ty->AddChild(Arg);
1523 }
1524
1525 break;
1526 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001527 default: break;
1528 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001529 }
Jim Laskey434b40b2006-02-23 22:37:30 +00001530
1531 assert(Ty && "Type not supported yet");
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001532
Jim Laskey69906002006-02-24 16:46:40 +00001533 // Add size if non-zero (derived types don't have a size.)
Jim Laskey434b40b2006-02-23 22:37:30 +00001534 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
Jim Laskey69906002006-02-24 16:46:40 +00001535 // Add name if not anonymous or intermediate type.
Jim Laskey434b40b2006-02-23 22:37:30 +00001536 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001537 // Add source line info if available.
1538 AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
Jim Laskey434b40b2006-02-23 22:37:30 +00001539
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001540 // Add to context owner.
Jim Laskey92ae7402006-03-01 18:20:30 +00001541 Context->AddChild(Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001542
1543 return Slot;
1544}
1545
Jim Laskeyb8509c52006-03-23 18:07:55 +00001546/// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey063e7652006-01-17 17:31:53 +00001547///
Jim Laskeybd761842006-02-27 17:27:12 +00001548CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1549 unsigned ID) {
1550 // Construct debug information entry.
1551 DIE *Die = new DIE(DW_TAG_compile_unit);
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001552 Die->AddDelta (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0),
1553 DWLabel("section_line", 0));
Jim Laskey89d67fa2006-06-23 12:51:53 +00001554// Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1555// Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
Jim Laskeybd761842006-02-27 17:27:12 +00001556 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1557 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1558 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1559 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1560
Jim Laskeyb8509c52006-03-23 18:07:55 +00001561 // Add debug information entry to descriptor map.
Jim Laskey90c79d72006-03-23 23:02:34 +00001562 DIE *&Slot = getDieMapSlotFor(UnitDesc);
1563 Slot = Die;
Jim Laskeybd761842006-02-27 17:27:12 +00001564
1565 // Construct compile unit.
1566 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1567
1568 // Add Unit to compile unit map.
1569 DescToUnitMap[UnitDesc] = Unit;
1570
1571 return Unit;
1572}
Jim Laskey0420f2a2006-02-22 19:02:11 +00001573
Jim Laskeybd761842006-02-27 17:27:12 +00001574/// FindCompileUnit - Get the compile unit for the given descriptor.
1575///
1576CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1577 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1578 assert(Unit && "Missing compile unit.");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001579 return Unit;
Jim Laskey063e7652006-01-17 17:31:53 +00001580}
1581
Jim Laskey0420f2a2006-02-22 19:02:11 +00001582/// NewGlobalVariable - Add a new global variable DIE.
1583///
1584DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001585 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001586 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1587 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey90c79d72006-03-23 23:02:34 +00001588
1589 // Check for pre-existence.
1590 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1591 if (Slot) return Slot;
1592
Jim Laskey0420f2a2006-02-22 19:02:11 +00001593 // Get the global variable itself.
1594 GlobalVariable *GV = GVD->getGlobalVariable();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001595
Jim Laskeye85fb672006-09-18 14:47:26 +00001596 const std::string &Name = GVD->hasMangledName() ? GVD->getDisplayName()
1597 : GVD->getName();
1598 const std::string &MangledName = GVD->hasMangledName() ? GVD->getName()
1599 : "";
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001600 // Get the global's type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001601 DIE *Type = NewType(Unit->getDie(), GVD->getType(), Unit);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001602
1603 // Create the globale variable DIE.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001604 DIE *VariableDie = new DIE(DW_TAG_variable);
Jim Laskeye85fb672006-09-18 14:47:26 +00001605 VariableDie->AddString(DW_AT_name, DW_FORM_string, Name);
1606 if (!MangledName.empty()) {
1607 VariableDie->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1608 MangledName);
1609 }
1610 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1611 VariableDie->AddUInt(DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001612
1613 // Add source line info if available.
1614 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001615
Jim Laskeyb8509c52006-03-23 18:07:55 +00001616 // Add address.
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001617 DIEBlock *Block = new DIEBlock();
1618 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
1619 Block->AddObjectLabel(DW_FORM_udata, MangledName);
1620 Block->ComputeSize(*this);
1621 VariableDie->AddBlock(DW_AT_location, 0, Block);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001622
1623 // Add to map.
1624 Slot = VariableDie;
1625
1626 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001627 Unit->getDie()->AddChild(VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001628
1629 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001630 // FIXME - need to check external flag.
1631 Unit->AddGlobal(Name, VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001632
1633 return VariableDie;
1634}
1635
1636/// NewSubprogram - Add a new subprogram DIE.
1637///
1638DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001639 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001640 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1641 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001642
Jim Laskey90c79d72006-03-23 23:02:34 +00001643 // Check for pre-existence.
1644 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1645 if (Slot) return Slot;
1646
Jim Laskey0420f2a2006-02-22 19:02:11 +00001647 // Gather the details (simplify add attribute code.)
Jim Laskeye85fb672006-09-18 14:47:26 +00001648 const std::string &Name = SPD->hasMangledName() ? SPD->getDisplayName()
1649 : SPD->getName();
1650 const std::string &MangledName = SPD->hasMangledName() ? SPD->getName()
1651 : "";
Jim Laskey90c79d72006-03-23 23:02:34 +00001652 DIE *Type = NewType(Unit->getDie(), SPD->getType(), Unit);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001653 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001654
Jim Laskey8a8e9752006-02-27 20:37:42 +00001655 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
Jim Laskeye85fb672006-09-18 14:47:26 +00001656 SubprogramDie->AddString(DW_AT_name, DW_FORM_string, Name);
1657 if (!MangledName.empty()) {
1658 SubprogramDie->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1659 MangledName);
Jim Laskey41886992006-04-07 16:34:46 +00001660 }
Jim Laskeye85fb672006-09-18 14:47:26 +00001661 if (Type) {
1662 SubprogramDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1663 }
1664 SubprogramDie->AddUInt(DW_AT_external, DW_FORM_flag, IsExternal);
1665 SubprogramDie->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001666
Jim Laskeyb8509c52006-03-23 18:07:55 +00001667 // Add source line info if available.
1668 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1669
Jim Laskey0420f2a2006-02-22 19:02:11 +00001670 // Add to map.
1671 Slot = SubprogramDie;
1672
1673 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001674 Unit->getDie()->AddChild(SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001675
1676 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001677 Unit->AddGlobal(Name, SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001678
1679 return SubprogramDie;
1680}
1681
Jim Laskeyb8509c52006-03-23 18:07:55 +00001682/// NewScopeVariable - Create a new scope variable.
1683///
1684DIE *DwarfWriter::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1685 // Get the descriptor.
1686 VariableDesc *VD = DV->getDesc();
1687
1688 // Translate tag to proper Dwarf tag. The result variable is dropped for now.
1689 unsigned Tag;
1690 switch (VD->getTag()) {
1691 case DW_TAG_return_variable: return NULL;
1692 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1693 case DW_TAG_auto_variable: // fall thru
1694 default: Tag = DW_TAG_variable; break;
1695 }
1696
1697 // Define variable debug information entry.
1698 DIE *VariableDie = new DIE(Tag);
1699 VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
1700
1701 // Add source line info if available.
1702 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1703
1704 // Add variable type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001705 DIE *Type = NewType(Unit->getDie(), VD->getType(), Unit);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001706 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1707
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001708 // Add variable address.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001709 MachineLocation Location;
Jim Laskey41886992006-04-07 16:34:46 +00001710 RI->getLocation(*MF, DV->getFrameIndex(), Location);
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001711 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001712
1713 return VariableDie;
1714}
1715
1716/// ConstructScope - Construct the components of a scope.
1717///
1718void DwarfWriter::ConstructScope(DebugScope *ParentScope,
1719 DIE *ParentDie, CompileUnit *Unit) {
1720 // Add variables to scope.
1721 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1722 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1723 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1724 if (VariableDie) ParentDie->AddChild(VariableDie);
1725 }
1726
1727 // Add nested scopes.
1728 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1729 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1730 // Define the Scope debug information entry.
1731 DebugScope *Scope = Scopes[j];
1732 // FIXME - Ignore inlined functions for the time being.
Jim Laskeyd16f2a72006-06-19 19:49:42 +00001733 if (!Scope->getParent()) continue;
Jim Laskeyb8509c52006-03-23 18:07:55 +00001734
1735 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1736
1737 // Add the scope bounds.
1738 if (unsigned StartID = Scope->getStartLabelID()) {
1739 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1740 DWLabel("loc", StartID));
1741 } else {
1742 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1743 DWLabel("func_begin", SubprogramCount));
1744 }
1745 if (unsigned EndID = Scope->getEndLabelID()) {
1746 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1747 DWLabel("loc", EndID));
1748 } else {
1749 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1750 DWLabel("func_end", SubprogramCount));
1751 }
1752
1753 // Add the scope contents.
1754 ConstructScope(Scope, ScopeDie, Unit);
1755 ParentDie->AddChild(ScopeDie);
1756 }
1757}
1758
1759/// ConstructRootScope - Construct the scope for the subprogram.
1760///
1761void DwarfWriter::ConstructRootScope(DebugScope *RootScope) {
1762 // Exit if there is no root scope.
1763 if (!RootScope) return;
1764
1765 // Get the subprogram debug information entry.
1766 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Jim Laskey90c79d72006-03-23 23:02:34 +00001767
1768 // Get the compile unit context.
1769 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey41886992006-04-07 16:34:46 +00001770 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1771
Jim Laskey90c79d72006-03-23 23:02:34 +00001772 // Get the subprogram die.
1773 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001774 assert(SPDie && "Missing subprogram descriptor");
1775
1776 // Add the function bounds.
Jim Laskey6b92b8e2006-04-07 20:44:42 +00001777 SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1778 DWLabel("func_begin", SubprogramCount));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001779 SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1780 DWLabel("func_end", SubprogramCount));
Jim Laskey41886992006-04-07 16:34:46 +00001781 MachineLocation Location(RI->getFrameRegister(*MF));
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001782 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001783
Jim Laskeyb8509c52006-03-23 18:07:55 +00001784 ConstructScope(RootScope, SPDie, Unit);
1785}
1786
Jim Laskey063e7652006-01-17 17:31:53 +00001787/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1788/// tools to recognize the object file contains Dwarf information.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001789///
Jim Laskey014f98c2006-06-14 11:35:03 +00001790void DwarfWriter::EmitInitial() {
1791 // Check to see if we already emitted intial headers.
1792 if (didInitial) return;
1793 didInitial = true;
1794
Jim Laskey063e7652006-01-17 17:31:53 +00001795 // Dwarf sections base addresses.
Jim Laskey563321a2006-09-06 18:34:40 +00001796 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001797 EmitLabel("section_frame", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001798 Asm->SwitchToDataSection(TAI->getDwarfInfoSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001799 EmitLabel("section_info", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001800 EmitLabel("info", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001801 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001802 EmitLabel("section_abbrev", 0);
1803 EmitLabel("abbrev", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001804 Asm->SwitchToDataSection(TAI->getDwarfARangesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001805 EmitLabel("section_aranges", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001806 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001807 EmitLabel("section_macinfo", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001808 Asm->SwitchToDataSection(TAI->getDwarfLineSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001809 EmitLabel("section_line", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001810 EmitLabel("line", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001811 Asm->SwitchToDataSection(TAI->getDwarfLocSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001812 EmitLabel("section_loc", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001813 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001814 EmitLabel("section_pubnames", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001815 Asm->SwitchToDataSection(TAI->getDwarfStrSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001816 EmitLabel("section_str", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001817 Asm->SwitchToDataSection(TAI->getDwarfRangesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001818 EmitLabel("section_ranges", 0);
1819
Jim Laskey563321a2006-09-06 18:34:40 +00001820 Asm->SwitchToTextSection(TAI->getTextSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001821 EmitLabel("text_begin", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001822 Asm->SwitchToDataSection(TAI->getDataSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001823 EmitLabel("data_begin", 0);
Jim Laskey89d67fa2006-06-23 12:51:53 +00001824
Jim Laskey014f98c2006-06-14 11:35:03 +00001825 // Emit common frame information.
1826 EmitInitialDebugFrame();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001827}
1828
Jim Laskey063e7652006-01-17 17:31:53 +00001829/// EmitDIE - Recusively Emits a debug information entry.
1830///
1831void DwarfWriter::EmitDIE(DIE *Die) const {
1832 // Get the abbreviation for this DIE.
1833 unsigned AbbrevID = Die->getAbbrevID();
1834 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey0d086af2006-02-27 12:43:29 +00001835
1836 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001837
1838 // Emit the code (index) for the abbreviation.
1839 EmitULEB128Bytes(AbbrevID);
1840 EOL(std::string("Abbrev [" +
1841 utostr(AbbrevID) +
Jim Laskey0d086af2006-02-27 12:43:29 +00001842 "] 0x" + utohexstr(Die->getOffset()) +
1843 ":0x" + utohexstr(Die->getSize()) + " " +
1844 TagString(Abbrev.getTag())));
Jim Laskey063e7652006-01-17 17:31:53 +00001845
1846 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001847 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001848
1849 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001850 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001851 unsigned Attr = AbbrevData[i].getAttribute();
1852 unsigned Form = AbbrevData[i].getForm();
Jim Laskey063e7652006-01-17 17:31:53 +00001853 assert(Form && "Too many attributes for DIE (check abbreviation)");
1854
1855 switch (Attr) {
1856 case DW_AT_sibling: {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001857 EmitInt32(Die->SiblingOffset());
Jim Laskey063e7652006-01-17 17:31:53 +00001858 break;
1859 }
1860 default: {
1861 // Emit an attribute using the defined form.
1862 Values[i]->EmitValue(*this, Form);
1863 break;
1864 }
1865 }
1866
1867 EOL(AttributeString(Attr));
1868 }
1869
1870 // Emit the DIE children if any.
1871 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1872 const std::vector<DIE *> &Children = Die->getChildren();
1873
Jim Laskey52060a02006-01-24 00:49:18 +00001874 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskey063e7652006-01-17 17:31:53 +00001875 EmitDIE(Children[j]);
1876 }
1877
Jim Laskeyda427fa2006-01-27 20:31:25 +00001878 EmitInt8(0); EOL("End Of Children Mark");
Jim Laskey063e7652006-01-17 17:31:53 +00001879 }
1880}
1881
1882/// SizeAndOffsetDie - Compute the size and offset of a DIE.
1883///
Jim Laskeyb8509c52006-03-23 18:07:55 +00001884unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1885 // Get the children.
1886 const std::vector<DIE *> &Children = Die->getChildren();
1887
1888 // If not last sibling and has children then add sibling offset attribute.
1889 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1890
Jim Laskey0420f2a2006-02-22 19:02:11 +00001891 // Record the abbreviation.
1892 Die->Complete(*this);
1893
Jim Laskey063e7652006-01-17 17:31:53 +00001894 // Get the abbreviation for this DIE.
1895 unsigned AbbrevID = Die->getAbbrevID();
1896 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1897
1898 // Set DIE offset
1899 Die->setOffset(Offset);
1900
1901 // Start the size with the size of abbreviation code.
1902 Offset += SizeULEB128(AbbrevID);
1903
1904 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001905 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001906
1907 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001908 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001909 // Size attribute value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001910 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
Jim Laskey063e7652006-01-17 17:31:53 +00001911 }
1912
1913 // Emit the DIE children if any.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001914 if (!Children.empty()) {
1915 assert(Abbrev.getChildrenFlag() == DW_CHILDREN_yes &&
1916 "Children flag not set");
Jim Laskey063e7652006-01-17 17:31:53 +00001917
Jim Laskey52060a02006-01-24 00:49:18 +00001918 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001919 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
Jim Laskey063e7652006-01-17 17:31:53 +00001920 }
1921
1922 // End of children marker.
1923 Offset += sizeof(int8_t);
1924 }
1925
1926 Die->setSize(Offset - Die->getOffset());
1927 return Offset;
1928}
1929
1930/// SizeAndOffsets - Compute the size and offset of all the DIEs.
1931///
1932void DwarfWriter::SizeAndOffsets() {
Jim Laskey063e7652006-01-17 17:31:53 +00001933
1934 // Process each compile unit.
Jim Laskey52060a02006-01-24 00:49:18 +00001935 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00001936 CompileUnit *Unit = CompileUnits[i];
1937 if (Unit->hasContent()) {
1938 // Compute size of compile unit header
1939 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1940 sizeof(int16_t) + // DWARF version number
1941 sizeof(int32_t) + // Offset Into Abbrev. Section
1942 sizeof(int8_t); // Pointer Size (in bytes)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001943 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
Jim Laskeybd761842006-02-27 17:27:12 +00001944 }
Jim Laskey063e7652006-01-17 17:31:53 +00001945 }
1946}
1947
Jim Laskey41886992006-04-07 16:34:46 +00001948/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1949/// frame.
1950void DwarfWriter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
1951 std::vector<MachineMove *> &Moves) {
1952 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1953 MachineMove *Move = Moves[i];
1954 unsigned LabelID = Move->getLabelID();
1955 const MachineLocation &Dst = Move->getDestination();
1956 const MachineLocation &Src = Move->getSource();
1957
1958 // Advance row if new location.
1959 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
Jim Laskeyce50a162006-08-29 16:24:26 +00001960 EmitInt8(DW_CFA_advance_loc4);
Jim Laskey41886992006-04-07 16:34:46 +00001961 EOL("DW_CFA_advance_loc4");
1962 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
1963 EOL("");
1964
1965 BaseLabelID = LabelID;
1966 BaseLabel = "loc";
1967 }
1968
Jim Laskeyce50a162006-08-29 16:24:26 +00001969 int stackGrowth =
1970 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1971 TargetFrameInfo::StackGrowsUp ?
Jim Laskey563321a2006-09-06 18:34:40 +00001972 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskeyce50a162006-08-29 16:24:26 +00001973
Jim Laskey41886992006-04-07 16:34:46 +00001974 // If advancing cfa.
1975 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
1976 if (!Src.isRegister()) {
1977 if (Src.getRegister() == MachineLocation::VirtualFP) {
Jim Laskeyce50a162006-08-29 16:24:26 +00001978 EmitInt8(DW_CFA_def_cfa_offset);
Jim Laskey41886992006-04-07 16:34:46 +00001979 EOL("DW_CFA_def_cfa_offset");
1980 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00001981 EmitInt8(DW_CFA_def_cfa);
Jim Laskey41886992006-04-07 16:34:46 +00001982 EOL("DW_CFA_def_cfa");
1983
1984 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
1985 EOL("Register");
1986 }
Jim Laskey1069fbd2006-04-10 23:09:19 +00001987
Jim Laskeyce50a162006-08-29 16:24:26 +00001988 int Offset = Src.getOffset() / stackGrowth;
Jim Laskey1069fbd2006-04-10 23:09:19 +00001989
Jim Laskeyce50a162006-08-29 16:24:26 +00001990 EmitULEB128Bytes(Offset);
Jim Laskey41886992006-04-07 16:34:46 +00001991 EOL("Offset");
1992 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00001993 assert(0 && "Machine move no supported yet.");
Jim Laskey41886992006-04-07 16:34:46 +00001994 }
1995 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00001996 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
1997 int Offset = Dst.getOffset() / stackGrowth;
1998
1999 if (Offset < 0) {
2000 EmitInt8(DW_CFA_offset_extended_sf);
2001 EOL("DW_CFA_offset_extended_sf");
2002 EmitULEB128Bytes(Reg);
2003 EOL("Reg");
2004 EmitSLEB128Bytes(Offset);
2005 EOL("Offset");
2006 } else if (Reg < 64) {
2007 EmitInt8(DW_CFA_offset + Reg);
2008 EOL("DW_CFA_offset + Reg");
2009 EmitULEB128Bytes(Offset);
2010 EOL("Offset");
2011 } else {
2012 EmitInt8(DW_CFA_offset_extended);
2013 EOL("DW_CFA_offset_extended");
2014 EmitULEB128Bytes(Reg);
2015 EOL("Reg");
2016 EmitULEB128Bytes(Offset);
2017 EOL("Offset");
2018 }
Jim Laskey41886992006-04-07 16:34:46 +00002019 }
2020 }
2021}
2022
Jim Laskey063e7652006-01-17 17:31:53 +00002023/// EmitDebugInfo - Emit the debug info section.
2024///
2025void DwarfWriter::EmitDebugInfo() const {
2026 // Start debug info section.
Jim Laskey563321a2006-09-06 18:34:40 +00002027 Asm->SwitchToDataSection(TAI->getDwarfInfoSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002028
Jim Laskeybd761842006-02-27 17:27:12 +00002029 // Process each compile unit.
2030 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2031 CompileUnit *Unit = CompileUnits[i];
2032
2033 if (Unit->hasContent()) {
2034 DIE *Die = Unit->getDie();
Jim Laskey0d086af2006-02-27 12:43:29 +00002035 // Emit the compile units header.
Jim Laskeybd761842006-02-27 17:27:12 +00002036 EmitLabel("info_begin", Unit->getID());
Jim Laskey0d086af2006-02-27 12:43:29 +00002037 // Emit size of content not including length itself
Jim Laskeybd761842006-02-27 17:27:12 +00002038 unsigned ContentSize = Die->getSize() +
Jim Laskey0d086af2006-02-27 12:43:29 +00002039 sizeof(int16_t) + // DWARF version number
2040 sizeof(int32_t) + // Offset Into Abbrev. Section
2041 sizeof(int8_t); // Pointer Size (in bytes)
2042
2043 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2044 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskeyf8a01a92006-06-15 20:51:43 +00002045 EmitDifference("abbrev_begin", 0, "section_abbrev", 0);
2046 EOL("Offset Into Abbrev. Section");
Jim Laskey563321a2006-09-06 18:34:40 +00002047 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
Jim Laskey0d086af2006-02-27 12:43:29 +00002048
Jim Laskeybd761842006-02-27 17:27:12 +00002049 EmitDIE(Die);
2050 EmitLabel("info_end", Unit->getID());
Jim Laskey063e7652006-01-17 17:31:53 +00002051 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002052
2053 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002054 }
2055}
2056
2057/// EmitAbbreviations - Emit the abbreviation section.
2058///
2059void DwarfWriter::EmitAbbreviations() const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002060 // Check to see if it is worth the effort.
2061 if (!Abbreviations.empty()) {
2062 // Start the debug abbrev section.
Jim Laskey563321a2006-09-06 18:34:40 +00002063 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002064
Jim Laskeyd18e2892006-01-20 20:34:06 +00002065 EmitLabel("abbrev_begin", 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002066
Jim Laskeyd18e2892006-01-20 20:34:06 +00002067 // For each abbrevation.
2068 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002069 AbbrevID <= NAID; ++AbbrevID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002070 // Get abbreviation data
2071 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey063e7652006-01-17 17:31:53 +00002072
Jim Laskeyd18e2892006-01-20 20:34:06 +00002073 // Emit the abbrevations code (base 1 index.)
2074 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
Jim Laskey063e7652006-01-17 17:31:53 +00002075
Jim Laskeyd18e2892006-01-20 20:34:06 +00002076 // Emit the abbreviations data.
2077 Abbrev.Emit(*this);
Jim Laskey0d086af2006-02-27 12:43:29 +00002078
2079 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002080 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002081
2082 EmitLabel("abbrev_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002083
2084 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002085 }
2086}
2087
2088/// EmitDebugLines - Emit source line information.
2089///
2090void DwarfWriter::EmitDebugLines() const {
2091 // Minimum line delta, thus ranging from -10..(255-10).
2092 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2093 // Maximum line delta, thus ranging from -10..(255-10).
2094 const int MaxLineDelta = 255 + MinLineDelta;
2095
2096 // Start the dwarf line section.
Jim Laskey563321a2006-09-06 18:34:40 +00002097 Asm->SwitchToDataSection(TAI->getDwarfLineSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002098
2099 // Construct the section header.
2100
2101 EmitDifference("line_end", 0, "line_begin", 0);
2102 EOL("Length of Source Line Info");
2103 EmitLabel("line_begin", 0);
2104
Jim Laskeyda427fa2006-01-27 20:31:25 +00002105 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey063e7652006-01-17 17:31:53 +00002106
2107 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
2108 EOL("Prolog Length");
2109 EmitLabel("line_prolog_begin", 0);
2110
Jim Laskeyda427fa2006-01-27 20:31:25 +00002111 EmitInt8(1); EOL("Minimum Instruction Length");
Jim Laskey063e7652006-01-17 17:31:53 +00002112
Jim Laskeyda427fa2006-01-27 20:31:25 +00002113 EmitInt8(1); EOL("Default is_stmt_start flag");
Jim Laskey063e7652006-01-17 17:31:53 +00002114
Jim Laskeyda427fa2006-01-27 20:31:25 +00002115 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00002116
Jim Laskeyda427fa2006-01-27 20:31:25 +00002117 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00002118
Jim Laskeyda427fa2006-01-27 20:31:25 +00002119 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Jim Laskey063e7652006-01-17 17:31:53 +00002120
2121 // Line number standard opcode encodings argument count
Jim Laskeyda427fa2006-01-27 20:31:25 +00002122 EmitInt8(0); EOL("DW_LNS_copy arg count");
2123 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2124 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2125 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2126 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2127 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2128 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2129 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2130 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskey063e7652006-01-17 17:31:53 +00002131
2132 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2133 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
2134
2135 // Emit directories.
2136 for (unsigned DirectoryID = 1, NDID = Directories.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002137 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskey063e7652006-01-17 17:31:53 +00002138 EmitString(Directories[DirectoryID]); EOL("Directory");
2139 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00002140 EmitInt8(0); EOL("End of directories");
Jim Laskey063e7652006-01-17 17:31:53 +00002141
2142 // Emit files.
2143 for (unsigned SourceID = 1, NSID = SourceFiles.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002144 SourceID <= NSID; ++SourceID) {
Jim Laskey063e7652006-01-17 17:31:53 +00002145 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2146 EmitString(SourceFile.getName()); EOL("Source");
2147 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2148 EmitULEB128Bytes(0); EOL("Mod date");
2149 EmitULEB128Bytes(0); EOL("File size");
2150 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00002151 EmitInt8(0); EOL("End of files");
Jim Laskey063e7652006-01-17 17:31:53 +00002152
2153 EmitLabel("line_prolog_end", 0);
2154
Jim Laskey89d67fa2006-06-23 12:51:53 +00002155 // A sequence for each text section.
2156 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2157 // Isolate current sections line info.
2158 const std::vector<SourceLineInfo *> &LineInfos = SectionSourceLines[j];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002159
2160 if (DwarfVerbose) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002161 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00002162 << TAI->getCommentString() << " "
Jim Laskey89d67fa2006-06-23 12:51:53 +00002163 << "Section "
2164 << SectionMap[j + 1].c_str() << "\n";
Jim Laskey0420f2a2006-02-22 19:02:11 +00002165 }
Jim Laskey063e7652006-01-17 17:31:53 +00002166
Jim Laskey89d67fa2006-06-23 12:51:53 +00002167 // Dwarf assumes we start with first line of first source file.
2168 unsigned Source = 1;
2169 unsigned Line = 1;
2170
2171 // Construct rows of the address, source, line, column matrix.
2172 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2173 SourceLineInfo *LineInfo = LineInfos[i];
2174
2175 if (DwarfVerbose) {
2176 unsigned SourceID = LineInfo->getSourceID();
2177 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2178 unsigned DirectoryID = SourceFile.getDirectoryID();
2179 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00002180 << TAI->getCommentString() << " "
Jim Laskey89d67fa2006-06-23 12:51:53 +00002181 << Directories[DirectoryID]
2182 << SourceFile.getName() << ":"
2183 << LineInfo->getLine() << "\n";
2184 }
2185
2186 // Define the line address.
2187 EmitInt8(0); EOL("Extended Op");
2188 EmitInt8(4 + 1); EOL("Op size");
2189 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2190 EmitReference("loc", LineInfo->getLabelID()); EOL("Location label");
2191
2192 // If change of source, then switch to the new source.
2193 if (Source != LineInfo->getSourceID()) {
2194 Source = LineInfo->getSourceID();
2195 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2196 EmitULEB128Bytes(Source); EOL("New Source");
2197 }
2198
2199 // If change of line.
2200 if (Line != LineInfo->getLine()) {
2201 // Determine offset.
2202 int Offset = LineInfo->getLine() - Line;
2203 int Delta = Offset - MinLineDelta;
2204
2205 // Update line.
2206 Line = LineInfo->getLine();
2207
2208 // If delta is small enough and in range...
2209 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2210 // ... then use fast opcode.
2211 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2212 } else {
2213 // ... otherwise use long hand.
2214 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2215 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2216 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2217 }
2218 } else {
2219 // Copy the previous row (different address or source)
2220 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2221 }
2222 }
2223
2224 // Define last address of section.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002225 EmitInt8(0); EOL("Extended Op");
2226 EmitInt8(4 + 1); EOL("Op size");
2227 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskey89d67fa2006-06-23 12:51:53 +00002228 EmitReference("section_end", j + 1); EOL("Section end label");
2229
2230 // Mark end of matrix.
2231 EmitInt8(0); EOL("DW_LNE_end_sequence");
2232 EmitULEB128Bytes(1); O << "\n";
2233 EmitInt8(1); O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002234 }
Jim Laskey063e7652006-01-17 17:31:53 +00002235
2236 EmitLabel("line_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002237
2238 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002239}
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002240
Jim Laskey41886992006-04-07 16:34:46 +00002241/// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002242///
Jim Laskey41886992006-04-07 16:34:46 +00002243void DwarfWriter::EmitInitialDebugFrame() {
Jim Laskey1069fbd2006-04-10 23:09:19 +00002244 int stackGrowth =
2245 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2246 TargetFrameInfo::StackGrowsUp ?
Jim Laskey563321a2006-09-06 18:34:40 +00002247 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskey1069fbd2006-04-10 23:09:19 +00002248
Jim Laskey41886992006-04-07 16:34:46 +00002249 // Start the dwarf frame section.
Jim Laskey563321a2006-09-06 18:34:40 +00002250 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002251
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002252 EmitLabel("frame_common", 0);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002253 EmitDifference("frame_common_end", 0,
2254 "frame_common_begin", 0);
2255 EOL("Length of Common Information Entry");
2256
2257 EmitLabel("frame_common_begin", 0);
2258 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2259 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2260 EmitString(""); EOL("CIE Augmentation");
2261 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
Jim Laskey1069fbd2006-04-10 23:09:19 +00002262 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
Jim Laskey41886992006-04-07 16:34:46 +00002263 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
2264
2265 std::vector<MachineMove *> Moves;
2266 RI->getInitialFrameState(Moves);
2267 EmitFrameMoves(NULL, 0, Moves);
2268 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2269
Jim Laskeyb8509c52006-03-23 18:07:55 +00002270 EmitAlign(2);
2271 EmitLabel("frame_common_end", 0);
2272
2273 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002274}
2275
Jim Laskey41886992006-04-07 16:34:46 +00002276/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2277/// section.
2278void DwarfWriter::EmitFunctionDebugFrame() {
2279 // Start the dwarf frame section.
Jim Laskey563321a2006-09-06 18:34:40 +00002280 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskey41886992006-04-07 16:34:46 +00002281
2282 EmitDifference("frame_end", SubprogramCount,
2283 "frame_begin", SubprogramCount);
2284 EOL("Length of Frame Information Entry");
2285
2286 EmitLabel("frame_begin", SubprogramCount);
2287
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002288 EmitDifference("frame_common", 0, "section_frame", 0);
2289 EOL("FDE CIE offset");
Jim Laskey41886992006-04-07 16:34:46 +00002290
2291 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2292 EmitDifference("func_end", SubprogramCount,
2293 "func_begin", SubprogramCount);
2294 EOL("FDE address range");
2295
2296 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2297
2298 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2299
2300 EmitAlign(2);
2301 EmitLabel("frame_end", SubprogramCount);
2302
2303 O << "\n";
2304}
2305
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002306/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2307///
2308void DwarfWriter::EmitDebugPubNames() {
Jim Laskeybd761842006-02-27 17:27:12 +00002309 // Start the dwarf pubnames section.
Jim Laskey563321a2006-09-06 18:34:40 +00002310 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection(), 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002311
Jim Laskeybd761842006-02-27 17:27:12 +00002312 // Process each compile unit.
2313 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2314 CompileUnit *Unit = CompileUnits[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00002315
Jim Laskeybd761842006-02-27 17:27:12 +00002316 if (Unit->hasContent()) {
2317 EmitDifference("pubnames_end", Unit->getID(),
2318 "pubnames_begin", Unit->getID());
2319 EOL("Length of Public Names Info");
2320
2321 EmitLabel("pubnames_begin", Unit->getID());
2322
2323 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2324
Jim Laskey067ef412006-06-19 15:48:00 +00002325 EmitDifference("info_begin", Unit->getID(), "section_info", 0);
Jim Laskeybd761842006-02-27 17:27:12 +00002326 EOL("Offset of Compilation Unit Info");
Jim Laskeyd18e2892006-01-20 20:34:06 +00002327
Jim Laskeybd761842006-02-27 17:27:12 +00002328 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2329 EOL("Compilation Unit Length");
2330
2331 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2332
2333 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2334 GE = Globals.end();
2335 GI != GE; ++GI) {
2336 const std::string &Name = GI->first;
2337 DIE * Entity = GI->second;
2338
2339 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2340 EmitString(Name); EOL("External Name");
2341 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002342
Jim Laskeybd761842006-02-27 17:27:12 +00002343 EmitInt32(0); EOL("End Mark");
2344 EmitLabel("pubnames_end", Unit->getID());
2345
2346 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002347 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002348 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002349}
2350
2351/// EmitDebugStr - Emit visible names into a debug str section.
2352///
2353void DwarfWriter::EmitDebugStr() {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002354 // Check to see if it is worth the effort.
2355 if (!StringPool.empty()) {
2356 // Start the dwarf str section.
Jim Laskey563321a2006-09-06 18:34:40 +00002357 Asm->SwitchToDataSection(TAI->getDwarfStrSection(), 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002358
Jim Laskeyb8509c52006-03-23 18:07:55 +00002359 // For each of strings in the string pool.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002360 for (unsigned StringID = 1, N = StringPool.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002361 StringID <= N; ++StringID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002362 // Emit a label for reference from debug information entries.
2363 EmitLabel("string", StringID);
2364 // Emit the string itself.
2365 const std::string &String = StringPool[StringID];
2366 EmitString(String); O << "\n";
2367 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002368
2369 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002370 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002371}
2372
2373/// EmitDebugLoc - Emit visible names into a debug loc section.
2374///
2375void DwarfWriter::EmitDebugLoc() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002376 // Start the dwarf loc section.
Jim Laskey563321a2006-09-06 18:34:40 +00002377 Asm->SwitchToDataSection(TAI->getDwarfLocSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002378
2379 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002380}
2381
2382/// EmitDebugARanges - Emit visible names into a debug aranges section.
2383///
2384void DwarfWriter::EmitDebugARanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002385 // Start the dwarf aranges section.
Jim Laskey563321a2006-09-06 18:34:40 +00002386 Asm->SwitchToDataSection(TAI->getDwarfARangesSection(), 0);
Jim Laskeye719a7c2006-01-18 16:54:26 +00002387
2388 // FIXME - Mock up
Jim Laskeybd761842006-02-27 17:27:12 +00002389#if 0
2390 // Process each compile unit.
2391 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2392 CompileUnit *Unit = CompileUnits[i];
2393
2394 if (Unit->hasContent()) {
2395 // Don't include size of length
2396 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2397
2398 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2399
2400 EmitReference("info_begin", Unit->getID());
2401 EOL("Offset of Compilation Unit Info");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002402
Jim Laskey563321a2006-09-06 18:34:40 +00002403 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002404
Jim Laskeybd761842006-02-27 17:27:12 +00002405 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002406
Jim Laskeybd761842006-02-27 17:27:12 +00002407 EmitInt16(0); EOL("Pad (1)");
2408 EmitInt16(0); EOL("Pad (2)");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002409
Jim Laskeybd761842006-02-27 17:27:12 +00002410 // Range 1
2411 EmitReference("text_begin", 0); EOL("Address");
2412 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002413
Jim Laskeybd761842006-02-27 17:27:12 +00002414 EmitInt32(0); EOL("EOM (1)");
2415 EmitInt32(0); EOL("EOM (2)");
2416
2417 O << "\n";
2418 }
2419 }
2420#endif
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002421}
2422
2423/// EmitDebugRanges - Emit visible names into a debug ranges section.
2424///
2425void DwarfWriter::EmitDebugRanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002426 // Start the dwarf ranges section.
Jim Laskey563321a2006-09-06 18:34:40 +00002427 Asm->SwitchToDataSection(TAI->getDwarfRangesSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002428
2429 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002430}
2431
2432/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2433///
2434void DwarfWriter::EmitDebugMacInfo() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002435 // Start the dwarf macinfo section.
Jim Laskey563321a2006-09-06 18:34:40 +00002436 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002437
2438 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002439}
Jim Laskey063e7652006-01-17 17:31:53 +00002440
Jim Laskey52060a02006-01-24 00:49:18 +00002441/// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2442/// header file.
2443void DwarfWriter::ConstructCompileUnitDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002444 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
Jim Laskey6e87c0e2006-01-26 21:22:49 +00002445
2446 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00002447 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
Jim Laskey52060a02006-01-24 00:49:18 +00002448 CompileUnits.push_back(Unit);
2449 }
2450}
2451
2452/// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2453/// variables.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002454void DwarfWriter::ConstructGlobalDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002455 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002456 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00002457
2458 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002459 GlobalVariableDesc *GVD = GlobalVariables[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002460 NewGlobalVariable(GVD);
Jim Laskey52060a02006-01-24 00:49:18 +00002461 }
2462}
2463
Jim Laskey0420f2a2006-02-22 19:02:11 +00002464/// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2465/// subprograms.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002466void DwarfWriter::ConstructSubprogramDIEs() {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002467 std::vector<SubprogramDesc *> Subprograms =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002468 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002469
2470 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2471 SubprogramDesc *SPD = Subprograms[i];
2472 NewSubprogram(SPD);
2473 }
2474}
Jim Laskey52060a02006-01-24 00:49:18 +00002475
Jim Laskey063e7652006-01-17 17:31:53 +00002476//===----------------------------------------------------------------------===//
Jim Laskeyd18e2892006-01-20 20:34:06 +00002477// Main entry points.
Jim Laskey063e7652006-01-17 17:31:53 +00002478//
Jim Laskey52060a02006-01-24 00:49:18 +00002479
Jim Laskeya0f3d172006-09-07 22:06:40 +00002480DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
2481 const TargetAsmInfo *T)
Jim Laskey52060a02006-01-24 00:49:18 +00002482: O(OS)
2483, Asm(A)
Jim Laskey563321a2006-09-06 18:34:40 +00002484, TAI(T)
Jim Laskey41886992006-04-07 16:34:46 +00002485, TD(Asm->TM.getTargetData())
2486, RI(Asm->TM.getRegisterInfo())
Jim Laskeyb8509c52006-03-23 18:07:55 +00002487, M(NULL)
2488, MF(NULL)
Jim Laskey52060a02006-01-24 00:49:18 +00002489, DebugInfo(NULL)
2490, didInitial(false)
Jim Laskey014f98c2006-06-14 11:35:03 +00002491, shouldEmit(false)
Jim Laskeyb8509c52006-03-23 18:07:55 +00002492, SubprogramCount(0)
Jim Laskey52060a02006-01-24 00:49:18 +00002493, CompileUnits()
2494, Abbreviations()
Jim Laskey52060a02006-01-24 00:49:18 +00002495, StringPool()
Jim Laskeybd761842006-02-27 17:27:12 +00002496, DescToUnitMap()
Jim Laskey0420f2a2006-02-22 19:02:11 +00002497, DescToDieMap()
Jim Laskey89d67fa2006-06-23 12:51:53 +00002498, SectionMap()
2499, SectionSourceLines()
Jim Laskey52060a02006-01-24 00:49:18 +00002500{}
2501DwarfWriter::~DwarfWriter() {
2502 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2503 delete CompileUnits[i];
Jim Laskey063e7652006-01-17 17:31:53 +00002504 }
Jim Laskey52060a02006-01-24 00:49:18 +00002505}
Jim Laskey063e7652006-01-17 17:31:53 +00002506
Jim Laskey41886992006-04-07 16:34:46 +00002507/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2508/// created it. Set by the target AsmPrinter.
2509void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
Jim Laskey014f98c2006-06-14 11:35:03 +00002510 // Make sure initial declarations are made.
2511 if (!DebugInfo && DI->hasInfo()) {
2512 DebugInfo = DI;
2513 shouldEmit = true;
2514
2515 // Emit initial sections
2516 EmitInitial();
2517
2518 // Create all the compile unit DIEs.
2519 ConstructCompileUnitDIEs();
2520
2521 // Create DIEs for each of the externally visible global variables.
2522 ConstructGlobalDIEs();
2523
2524 // Create DIEs for each of the externally visible subprograms.
2525 ConstructSubprogramDIEs();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002526
2527 // Prime section data.
Jim Laskey563321a2006-09-06 18:34:40 +00002528 SectionMap.insert(std::string("\t") + TAI->getTextSection());
Jim Laskey014f98c2006-06-14 11:35:03 +00002529 }
Jim Laskey41886992006-04-07 16:34:46 +00002530}
2531
Jim Laskey063e7652006-01-17 17:31:53 +00002532/// BeginModule - Emit all Dwarf sections that should come prior to the content.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002533///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002534void DwarfWriter::BeginModule(Module *M) {
2535 this->M = M;
2536
Jim Laskeyb2efb852006-01-04 22:28:25 +00002537 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002538 EOL("Dwarf Begin Module");
Jim Laskeyb2efb852006-01-04 22:28:25 +00002539}
2540
Jim Laskey063e7652006-01-17 17:31:53 +00002541/// EndModule - Emit all Dwarf sections that should come after the content.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002542///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002543void DwarfWriter::EndModule() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002544 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002545 EOL("Dwarf End Module");
2546
2547 // Standard sections final addresses.
Jim Laskey563321a2006-09-06 18:34:40 +00002548 Asm->SwitchToTextSection(TAI->getTextSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002549 EmitLabel("text_end", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002550 Asm->SwitchToDataSection(TAI->getDataSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002551 EmitLabel("data_end", 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002552
Jim Laskey89d67fa2006-06-23 12:51:53 +00002553 // End text sections.
2554 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2555 Asm->SwitchToTextSection(SectionMap[i].c_str(), 0);
2556 EmitLabel("section_end", i);
2557 }
2558
Jim Laskey063e7652006-01-17 17:31:53 +00002559 // Compute DIE offsets and sizes.
2560 SizeAndOffsets();
2561
2562 // Emit all the DIEs into a debug info section
2563 EmitDebugInfo();
2564
2565 // Corresponding abbreviations into a abbrev section.
2566 EmitAbbreviations();
2567
2568 // Emit source line correspondence into a debug line section.
2569 EmitDebugLines();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002570
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002571 // Emit info into a debug pubnames section.
2572 EmitDebugPubNames();
2573
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002574 // Emit info into a debug str section.
2575 EmitDebugStr();
2576
2577 // Emit info into a debug loc section.
2578 EmitDebugLoc();
2579
2580 // Emit info into a debug aranges section.
2581 EmitDebugARanges();
2582
2583 // Emit info into a debug ranges section.
2584 EmitDebugRanges();
2585
2586 // Emit info into a debug macinfo section.
2587 EmitDebugMacInfo();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002588}
2589
Jim Laskey6b92b8e2006-04-07 20:44:42 +00002590/// BeginFunction - Gather pre-function debug information. Assumes being
2591/// emitted immediately after the function entry point.
Jim Laskey89d67fa2006-06-23 12:51:53 +00002592void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002593 this->MF = MF;
2594
Jim Laskey89d67fa2006-06-23 12:51:53 +00002595 if (!ShouldEmitDwarf()) return;
2596 EOL("Dwarf Begin Function");
2597
2598 // Begin accumulating function debug information.
2599 DebugInfo->BeginFunction(MF);
Jim Laskey41886992006-04-07 16:34:46 +00002600
Jim Laskey89d67fa2006-06-23 12:51:53 +00002601 // Assumes in correct section after the entry point.
2602 EmitLabel("func_begin", ++SubprogramCount);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002603}
2604
Jim Laskeye719a7c2006-01-18 16:54:26 +00002605/// EndFunction - Gather and emit post-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002606///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002607void DwarfWriter::EndFunction() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002608 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002609 EOL("Dwarf End Function");
Jim Laskeyb8509c52006-03-23 18:07:55 +00002610
Jim Laskey89d67fa2006-06-23 12:51:53 +00002611 // Define end label for subprogram.
2612 EmitLabel("func_end", SubprogramCount);
Jim Laskey014f98c2006-06-14 11:35:03 +00002613
Jim Laskey89d67fa2006-06-23 12:51:53 +00002614 // Get function line info.
2615 std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
2616
2617 if (!LineInfos.empty()) {
2618 // Get section line info.
2619 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2620 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2621 std::vector<SourceLineInfo *> &SectionLineInfos =SectionSourceLines[ID-1];
2622 // Append the function info to section info.
2623 SectionLineInfos.insert(SectionLineInfos.end(),
2624 LineInfos.begin(), LineInfos.end());
Jim Laskey014f98c2006-06-14 11:35:03 +00002625 }
Jim Laskey41886992006-04-07 16:34:46 +00002626
Jim Laskey89d67fa2006-06-23 12:51:53 +00002627 // Construct scopes for subprogram.
2628 ConstructRootScope(DebugInfo->getRootScope());
2629
2630 // Emit function frame information.
2631 EmitFunctionDebugFrame();
2632
2633 // Reset the line numbers for the next function.
2634 LineInfos.clear();
2635
Jim Laskey41886992006-04-07 16:34:46 +00002636 // Clear function debug information.
2637 DebugInfo->EndFunction();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002638}