blob: c600edbdf831b943e58d59d86850768a262370e6 [file] [log] [blame]
Jim Laskeye5032892005-12-21 19:48:16 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by James M. Laskey and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Jim Laskey3ea0e0e2006-01-27 18:32:41 +000013
Jim Laskeyb2efb852006-01-04 22:28:25 +000014#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskeye5032892005-12-21 19:48:16 +000015
Jim Laskey063e7652006-01-17 17:31:53 +000016#include "llvm/ADT/StringExtras.h"
Jim Laskey52060a02006-01-24 00:49:18 +000017#include "llvm/Module.h"
18#include "llvm/Type.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000019#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeyb2efb852006-01-04 22:28:25 +000020#include "llvm/CodeGen/MachineDebugInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000022#include "llvm/CodeGen/MachineLocation.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000023#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000024#include "llvm/Support/CommandLine.h"
Jim Laskey52060a02006-01-24 00:49:18 +000025#include "llvm/Support/Mangler.h"
Jim Laskey563321a2006-09-06 18:34:40 +000026#include "llvm/Target/TargetAsmInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000027#include "llvm/Target/MRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000028#include "llvm/Target/TargetData.h"
Jim Laskey52060a02006-01-24 00:49:18 +000029#include "llvm/Target/TargetMachine.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000030#include "llvm/Target/TargetFrameInfo.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000031
Jim Laskeyb2efb852006-01-04 22:28:25 +000032#include <iostream>
Jim Laskeya7cea6f2006-01-04 13:52:30 +000033
Jim Laskeyb2efb852006-01-04 22:28:25 +000034using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000035using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000036
37static cl::opt<bool>
38DwarfVerbose("dwarf-verbose", cl::Hidden,
Jim Laskeyce50a162006-08-29 16:24:26 +000039 cl::desc("Add comments to Dwarf directives."));
Jim Laskey063e7652006-01-17 17:31:53 +000040
Jim Laskey0d086af2006-02-27 12:43:29 +000041namespace llvm {
42
Jim Laskey063e7652006-01-17 17:31:53 +000043//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000044// Forward declarations.
45//
Jim Laskey0d086af2006-02-27 12:43:29 +000046class DIE;
Jim Laskey063e7652006-01-17 17:31:53 +000047
Jim Laskey0d086af2006-02-27 12:43:29 +000048//===----------------------------------------------------------------------===//
Jim Laskeyf01e5472006-03-03 15:06:57 +000049// CompileUnit - This dwarf writer support class manages information associate
50// with a source file.
Jim Laskeybd761842006-02-27 17:27:12 +000051class CompileUnit {
52private:
53 CompileUnitDesc *Desc; // Compile unit debug descriptor.
54 unsigned ID; // File ID for source.
Jim Laskeyb8509c52006-03-23 18:07:55 +000055 DIE *Die; // Compile unit debug information entry.
Jim Laskeybd761842006-02-27 17:27:12 +000056 std::map<std::string, DIE *> Globals; // A map of globally visible named
57 // entities for this unit.
Jim Laskey90c79d72006-03-23 23:02:34 +000058 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
59 // Tracks the mapping of unit level
60 // debug informaton descriptors to debug
61 // information entries.
Jim Laskeybd761842006-02-27 17:27:12 +000062
63public:
64 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
65 : Desc(CUD)
66 , ID(I)
67 , Die(D)
68 , Globals()
Jim Laskey90c79d72006-03-23 23:02:34 +000069 , DescToDieMap()
Jim Laskeybd761842006-02-27 17:27:12 +000070 {}
71
72 ~CompileUnit();
73
74 // Accessors.
75 CompileUnitDesc *getDesc() const { return Desc; }
76 unsigned getID() const { return ID; }
77 DIE* getDie() const { return Die; }
78 std::map<std::string, DIE *> &getGlobals() { return Globals; }
79
80 /// hasContent - Return true if this compile unit has something to write out.
81 ///
82 bool hasContent() const;
83
84 /// AddGlobal - Add a new global entity to the compile unit.
85 ///
86 void AddGlobal(const std::string &Name, DIE *Die);
87
Jim Laskey90c79d72006-03-23 23:02:34 +000088 /// getDieMapSlotFor - Returns the debug information entry map slot for the
89 /// specified debug descriptor.
90 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
91 return DescToDieMap[DD];
92 }
Jim Laskeybd761842006-02-27 17:27:12 +000093};
94
95//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000096// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
97// Dwarf abbreviation.
98class DIEAbbrevData {
99private:
100 unsigned Attribute; // Dwarf attribute code.
101 unsigned Form; // Dwarf form code.
102
103public:
104 DIEAbbrevData(unsigned A, unsigned F)
105 : Attribute(A)
106 , Form(F)
107 {}
108
Jim Laskeybd761842006-02-27 17:27:12 +0000109 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000110 unsigned getAttribute() const { return Attribute; }
111 unsigned getForm() const { return Form; }
112
113 /// operator== - Used by DIEAbbrev to locate entry.
114 ///
115 bool operator==(const DIEAbbrevData &DAD) const {
116 return Attribute == DAD.Attribute && Form == DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000117 }
Jim Laskey063e7652006-01-17 17:31:53 +0000118
Jim Laskey0d086af2006-02-27 12:43:29 +0000119 /// operator!= - Used by DIEAbbrev to locate entry.
120 ///
121 bool operator!=(const DIEAbbrevData &DAD) const {
122 return Attribute != DAD.Attribute || Form != DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000123 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000124
125 /// operator< - Used by DIEAbbrev to locate entry.
126 ///
127 bool operator<(const DIEAbbrevData &DAD) const {
128 return Attribute < DAD.Attribute ||
129 (Attribute == DAD.Attribute && Form < DAD.Form);
130 }
131};
Jim Laskey063e7652006-01-17 17:31:53 +0000132
Jim Laskey0d086af2006-02-27 12:43:29 +0000133//===----------------------------------------------------------------------===//
134// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
135// information object.
136class DIEAbbrev {
137private:
138 unsigned Tag; // Dwarf tag code.
139 unsigned ChildrenFlag; // Dwarf children flag.
140 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
Jim Laskey063e7652006-01-17 17:31:53 +0000141
Jim Laskey0d086af2006-02-27 12:43:29 +0000142public:
Jim Laskey063e7652006-01-17 17:31:53 +0000143
Jim Laskey0d086af2006-02-27 12:43:29 +0000144 DIEAbbrev(unsigned T, unsigned C)
145 : Tag(T)
146 , ChildrenFlag(C)
147 , Data()
148 {}
149 ~DIEAbbrev() {}
150
Jim Laskeybd761842006-02-27 17:27:12 +0000151 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000152 unsigned getTag() const { return Tag; }
153 unsigned getChildrenFlag() const { return ChildrenFlag; }
154 const std::vector<DIEAbbrevData> &getData() const { return Data; }
155 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskey063e7652006-01-17 17:31:53 +0000156
Jim Laskey0d086af2006-02-27 12:43:29 +0000157 /// operator== - Used by UniqueVector to locate entry.
158 ///
159 bool operator==(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000160
Jim Laskey0d086af2006-02-27 12:43:29 +0000161 /// operator< - Used by UniqueVector to locate entry.
162 ///
163 bool operator<(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000164
Jim Laskey0d086af2006-02-27 12:43:29 +0000165 /// AddAttribute - Adds another set of attribute information to the
166 /// abbreviation.
167 void AddAttribute(unsigned Attribute, unsigned Form) {
168 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000169 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000170
Jim Laskeyb8509c52006-03-23 18:07:55 +0000171 /// AddFirstAttribute - Adds a set of attribute information to the front
172 /// of the abbreviation.
173 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
174 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
175 }
176
Jim Laskey0d086af2006-02-27 12:43:29 +0000177 /// Emit - Print the abbreviation using the specified Dwarf writer.
178 ///
179 void Emit(const DwarfWriter &DW) const;
180
181#ifndef NDEBUG
182 void print(std::ostream &O);
183 void dump();
184#endif
185};
Jim Laskey063e7652006-01-17 17:31:53 +0000186
Jim Laskey0d086af2006-02-27 12:43:29 +0000187//===----------------------------------------------------------------------===//
188// DIEValue - A debug information entry value.
189//
190class DIEValue {
191public:
192 enum {
193 isInteger,
194 isString,
195 isLabel,
196 isAsIsLabel,
197 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000198 isEntry,
199 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000200 };
201
202 unsigned Type; // Type of the value
203
204 DIEValue(unsigned T) : Type(T) {}
205 virtual ~DIEValue() {}
206
207 // Implement isa/cast/dyncast.
208 static bool classof(const DIEValue *) { return true; }
209
210 /// EmitValue - Emit value via the Dwarf writer.
211 ///
212 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
213
214 /// SizeOf - Return the size of a value in bytes.
215 ///
216 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
217};
Jim Laskey063e7652006-01-17 17:31:53 +0000218
Jim Laskey0d086af2006-02-27 12:43:29 +0000219//===----------------------------------------------------------------------===//
220// DWInteger - An integer value DIE.
221//
222class DIEInteger : public DIEValue {
223private:
224 uint64_t Integer;
225
226public:
227 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000228
Jim Laskey0d086af2006-02-27 12:43:29 +0000229 // Implement isa/cast/dyncast.
230 static bool classof(const DIEInteger *) { return true; }
231 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
232
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000233 /// BestForm - Choose the best form for integer.
234 ///
235 unsigned BestForm(bool IsSigned);
236
Jim Laskey0d086af2006-02-27 12:43:29 +0000237 /// EmitValue - Emit integer of appropriate size.
238 ///
239 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
240
241 /// SizeOf - Determine size of integer value in bytes.
242 ///
243 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
244};
Jim Laskey063e7652006-01-17 17:31:53 +0000245
Jim Laskey0d086af2006-02-27 12:43:29 +0000246//===----------------------------------------------------------------------===//
247// DIEString - A string value DIE.
248//
249struct DIEString : public DIEValue {
250 const std::string String;
251
252 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000253
Jim Laskey0d086af2006-02-27 12:43:29 +0000254 // Implement isa/cast/dyncast.
255 static bool classof(const DIEString *) { return true; }
256 static bool classof(const DIEValue *S) { return S->Type == isString; }
257
258 /// EmitValue - Emit string value.
259 ///
260 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
261
262 /// SizeOf - Determine size of string value in bytes.
263 ///
264 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
265};
Jim Laskey063e7652006-01-17 17:31:53 +0000266
Jim Laskey0d086af2006-02-27 12:43:29 +0000267//===----------------------------------------------------------------------===//
268// DIEDwarfLabel - A Dwarf internal label expression DIE.
269//
270struct DIEDwarfLabel : public DIEValue {
271 const DWLabel Label;
272
273 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000274
Jim Laskey0d086af2006-02-27 12:43:29 +0000275 // Implement isa/cast/dyncast.
276 static bool classof(const DIEDwarfLabel *) { return true; }
277 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
278
279 /// EmitValue - Emit label value.
280 ///
281 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
282
283 /// SizeOf - Determine size of label value in bytes.
284 ///
285 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
286};
Jim Laskey063e7652006-01-17 17:31:53 +0000287
Jim Laskey063e7652006-01-17 17:31:53 +0000288
Jim Laskey0d086af2006-02-27 12:43:29 +0000289//===----------------------------------------------------------------------===//
290// DIEObjectLabel - A label to an object in code or data.
291//
292struct DIEObjectLabel : public DIEValue {
293 const std::string Label;
294
295 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000296
Jim Laskey0d086af2006-02-27 12:43:29 +0000297 // Implement isa/cast/dyncast.
298 static bool classof(const DIEObjectLabel *) { return true; }
299 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
300
301 /// EmitValue - Emit label value.
302 ///
303 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
304
305 /// SizeOf - Determine size of label value in bytes.
306 ///
307 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
308};
Jim Laskey063e7652006-01-17 17:31:53 +0000309
Jim Laskey0d086af2006-02-27 12:43:29 +0000310//===----------------------------------------------------------------------===//
311// DIEDelta - A simple label difference DIE.
312//
313struct DIEDelta : public DIEValue {
314 const DWLabel LabelHi;
315 const DWLabel LabelLo;
316
317 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
318 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000319
Jim Laskey0d086af2006-02-27 12:43:29 +0000320 // Implement isa/cast/dyncast.
321 static bool classof(const DIEDelta *) { return true; }
322 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
323
324 /// EmitValue - Emit delta value.
325 ///
326 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
327
328 /// SizeOf - Determine size of delta value in bytes.
329 ///
330 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
331};
Jim Laskey063e7652006-01-17 17:31:53 +0000332
Jim Laskey0d086af2006-02-27 12:43:29 +0000333//===----------------------------------------------------------------------===//
334// DIEntry - A pointer to a debug information entry.
335//
336struct DIEntry : public DIEValue {
337 DIE *Entry;
338
339 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
340
341 // Implement isa/cast/dyncast.
342 static bool classof(const DIEntry *) { return true; }
343 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
344
Jim Laskeyb8509c52006-03-23 18:07:55 +0000345 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000346 ///
347 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
348
Jim Laskeyb8509c52006-03-23 18:07:55 +0000349 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000350 ///
351 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
352};
353
354//===----------------------------------------------------------------------===//
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000355// DIEBlock - A block of values. Primarily used for location expressions.
356//
357struct DIEBlock : public DIEValue {
358 unsigned Size; // Size in bytes excluding size header.
359 std::vector<unsigned> Forms; // Data forms.
360 std::vector<DIEValue *> Values; // Block values.
361
362 DIEBlock()
363 : DIEValue(isBlock)
364 , Size(0)
365 , Forms()
366 , Values()
367 {}
368 ~DIEBlock();
369
370 // Implement isa/cast/dyncast.
371 static bool classof(const DIEBlock *) { return true; }
372 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
373
374 /// ComputeSize - calculate the size of the block.
375 ///
376 unsigned ComputeSize(DwarfWriter &DW);
377
378 /// BestForm - Choose the best form for data.
379 ///
380 unsigned BestForm();
381
382 /// EmitValue - Emit block data.
383 ///
384 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
385
386 /// SizeOf - Determine size of block data in bytes.
387 ///
388 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
389
390 /// AddUInt - Add an unsigned integer value.
391 ///
392 void AddUInt(unsigned Form, uint64_t Integer);
393
394 /// AddSInt - Add an signed integer value.
395 ///
396 void AddSInt(unsigned Form, int64_t Integer);
397
398 /// AddString - Add a std::string value.
399 ///
400 void AddString(unsigned Form, const std::string &String);
401
402 /// AddLabel - Add a Dwarf label value.
403 ///
404 void AddLabel(unsigned Form, const DWLabel &Label);
405
406 /// AddObjectLabel - Add a non-Dwarf label value.
407 ///
408 void AddObjectLabel(unsigned Form, const std::string &Label);
409
410 /// AddDelta - Add a label delta value.
411 ///
412 void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
413
414 /// AddDIEntry - Add a DIE value.
415 ///
416 void AddDIEntry(unsigned Form, DIE *Entry);
417
418};
419
420//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +0000421// DIE - A structured debug information entry. Has an abbreviation which
422// describes it's organization.
423class DIE {
424private:
425 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
426 unsigned AbbrevID; // Decribing abbreviation ID.
427 unsigned Offset; // Offset in debug info section.
428 unsigned Size; // Size of instance + children.
429 std::vector<DIE *> Children; // Children DIEs.
430 std::vector<DIEValue *> Values; // Attributes values.
431
432public:
433 DIE(unsigned Tag);
434 ~DIE();
435
Jim Laskeybd761842006-02-27 17:27:12 +0000436 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000437 unsigned getAbbrevID() const { return AbbrevID; }
438 unsigned getOffset() const { return Offset; }
439 unsigned getSize() const { return Size; }
440 const std::vector<DIE *> &getChildren() const { return Children; }
441 const std::vector<DIEValue *> &getValues() const { return Values; }
442 void setOffset(unsigned O) { Offset = O; }
443 void setSize(unsigned S) { Size = S; }
444
445 /// SiblingOffset - Return the offset of the debug information entry's
446 /// sibling.
447 unsigned SiblingOffset() const { return Offset + Size; }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000448
449 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
450 ///
451 void AddSiblingOffset();
Jim Laskey0d086af2006-02-27 12:43:29 +0000452
453 /// AddUInt - Add an unsigned integer attribute data and value.
454 ///
455 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
456
457 /// AddSInt - Add an signed integer attribute data and value.
458 ///
459 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
460
461 /// AddString - Add a std::string attribute data and value.
462 ///
463 void AddString(unsigned Attribute, unsigned Form,
464 const std::string &String);
465
466 /// AddLabel - Add a Dwarf label attribute data and value.
467 ///
468 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
469
470 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
471 ///
472 void AddObjectLabel(unsigned Attribute, unsigned Form,
473 const std::string &Label);
474
475 /// AddDelta - Add a label delta attribute data and value.
476 ///
477 void AddDelta(unsigned Attribute, unsigned Form,
478 const DWLabel &Hi, const DWLabel &Lo);
479
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000480 /// AddDIEntry - Add a DIE attribute data and value.
Jim Laskey0d086af2006-02-27 12:43:29 +0000481 ///
482 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
483
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000484 /// AddBlock - Add block data.
485 ///
486 void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
487
Jim Laskey0d086af2006-02-27 12:43:29 +0000488 /// Complete - Indicate that all attributes have been added and
489 /// ready to get an abbreviation ID.
490 ///
491 void Complete(DwarfWriter &DW);
492
493 /// AddChild - Add a child to the DIE.
494 void AddChild(DIE *Child);
495};
496
497} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +0000498
499//===----------------------------------------------------------------------===//
500
Jim Laskeybd761842006-02-27 17:27:12 +0000501CompileUnit::~CompileUnit() {
502 delete Die;
503}
504
505/// hasContent - Return true if this compile unit has something to write out.
506///
507bool CompileUnit::hasContent() const {
508 return !Die->getChildren().empty();
509}
510
511/// AddGlobal - Add a new global entity to the compile unit.
512///
513void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
514 Globals[Name] = Die;
515}
516
517//===----------------------------------------------------------------------===//
518
Jim Laskeyd18e2892006-01-20 20:34:06 +0000519/// operator== - Used by UniqueVector to locate entry.
520///
521bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
522 if (Tag != DA.Tag) return false;
523 if (ChildrenFlag != DA.ChildrenFlag) return false;
524 if (Data.size() != DA.Data.size()) return false;
525
Jim Laskey52060a02006-01-24 00:49:18 +0000526 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000527 if (Data[i] != DA.Data[i]) return false;
Jim Laskeyd18e2892006-01-20 20:34:06 +0000528 }
529
530 return true;
531}
532
533/// operator< - Used by UniqueVector to locate entry.
534///
535bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
536 if (Tag != DA.Tag) return Tag < DA.Tag;
537 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
538 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
539
Jim Laskey52060a02006-01-24 00:49:18 +0000540 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000541 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +0000542 }
543
544 return false;
545}
546
547/// Emit - Print the abbreviation using the specified Dwarf writer.
548///
549void DIEAbbrev::Emit(const DwarfWriter &DW) const {
550 // Emit its Dwarf tag type.
551 DW.EmitULEB128Bytes(Tag);
552 DW.EOL(TagString(Tag));
553
554 // Emit whether it has children DIEs.
555 DW.EmitULEB128Bytes(ChildrenFlag);
556 DW.EOL(ChildrenString(ChildrenFlag));
557
558 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +0000559 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000560 const DIEAbbrevData &AttrData = Data[i];
561
562 // Emit attribute type.
563 DW.EmitULEB128Bytes(AttrData.getAttribute());
564 DW.EOL(AttributeString(AttrData.getAttribute()));
565
566 // Emit form type.
567 DW.EmitULEB128Bytes(AttrData.getForm());
568 DW.EOL(FormEncodingString(AttrData.getForm()));
569 }
570
571 // Mark end of abbreviation.
572 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
573 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
574}
575
576#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +0000577void DIEAbbrev::print(std::ostream &O) {
578 O << "Abbreviation @"
579 << std::hex << (intptr_t)this << std::dec
580 << " "
581 << TagString(Tag)
582 << " "
583 << ChildrenString(ChildrenFlag)
584 << "\n";
585
586 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
587 O << " "
588 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +0000589 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +0000590 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +0000591 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +0000592 }
Jim Laskeya0f3d172006-09-07 22:06:40 +0000593}
594void DIEAbbrev::dump() { print(std::cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +0000595#endif
596
597//===----------------------------------------------------------------------===//
598
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000599/// BestForm - Choose the best form for integer.
600///
601unsigned DIEInteger::BestForm(bool IsSigned) {
602 if (IsSigned) {
603 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
604 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
605 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
606 } else {
607 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
608 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
609 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
610 }
611 return DW_FORM_data8;
612}
613
Jim Laskey063e7652006-01-17 17:31:53 +0000614/// EmitValue - Emit integer of appropriate size.
615///
616void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
617 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +0000618 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000619 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000620 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000621 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000622 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000623 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000624 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000625 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000626 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +0000627 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
628 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +0000629 default: assert(0 && "DIE Value form not supported yet"); break;
630 }
631}
632
633/// SizeOf - Determine size of integer value in bytes.
634///
635unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
636 switch (Form) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000637 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000638 case DW_FORM_ref1: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000639 case DW_FORM_data1: return sizeof(int8_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000640 case DW_FORM_ref2: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000641 case DW_FORM_data2: return sizeof(int16_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000642 case DW_FORM_ref4: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000643 case DW_FORM_data4: return sizeof(int32_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000644 case DW_FORM_ref8: // Fall thru
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000645 case DW_FORM_data8: return sizeof(int64_t);
Jim Laskey40020172006-01-20 21:02:36 +0000646 case DW_FORM_udata: return DW.SizeULEB128(Integer);
647 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +0000648 default: assert(0 && "DIE Value form not supported yet"); break;
649 }
650 return 0;
651}
652
653//===----------------------------------------------------------------------===//
654
655/// EmitValue - Emit string value.
656///
657void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000658 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +0000659}
660
661/// SizeOf - Determine size of string value in bytes.
662///
663unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey40020172006-01-20 21:02:36 +0000664 return String.size() + sizeof(char); // sizeof('\0');
Jim Laskey063e7652006-01-17 17:31:53 +0000665}
666
667//===----------------------------------------------------------------------===//
668
669/// EmitValue - Emit label value.
670///
Jim Laskey52060a02006-01-24 00:49:18 +0000671void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000672 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +0000673}
674
675/// SizeOf - Determine size of label value in bytes.
676///
Jim Laskey52060a02006-01-24 00:49:18 +0000677unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000678 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +0000679}
680
681//===----------------------------------------------------------------------===//
682
Jim Laskeyd18e2892006-01-20 20:34:06 +0000683/// EmitValue - Emit label value.
684///
Jim Laskey52060a02006-01-24 00:49:18 +0000685void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000686 DW.EmitReference(Label);
687}
688
689/// SizeOf - Determine size of label value in bytes.
690///
Jim Laskey52060a02006-01-24 00:49:18 +0000691unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000692 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +0000693}
694
695//===----------------------------------------------------------------------===//
696
Jim Laskey063e7652006-01-17 17:31:53 +0000697/// EmitValue - Emit delta value.
698///
699void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000700 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +0000701}
702
703/// SizeOf - Determine size of delta value in bytes.
704///
705unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000706 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +0000707}
708
709//===----------------------------------------------------------------------===//
Jim Laskeyb8509c52006-03-23 18:07:55 +0000710/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000711///
712void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +0000713 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +0000714}
715
Jim Laskeyb8509c52006-03-23 18:07:55 +0000716/// SizeOf - Determine size of debug information entry value in bytes.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000717///
718unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
719 return sizeof(int32_t);
720}
721
722//===----------------------------------------------------------------------===//
723
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000724DIEBlock::~DIEBlock() {
725 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
726 delete Values[i];
727 }
728}
729
730/// ComputeSize - calculate the size of the block.
731///
732unsigned DIEBlock::ComputeSize(DwarfWriter &DW) {
733 Size = 0;
734 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
735 Size += Values[i]->SizeOf(DW, Forms[i]);
736 }
737 return Size;
738}
739
740/// BestForm - Choose the best form for data.
741///
742unsigned DIEBlock::BestForm() {
743 if ((unsigned char)Size == Size) return DW_FORM_block1;
744 if ((unsigned short)Size == Size) return DW_FORM_block2;
745 if ((unsigned int)Size == Size) return DW_FORM_block4;
746 return DW_FORM_block;
747}
748
749/// EmitValue - Emit block data.
750///
751void DIEBlock::EmitValue(const DwarfWriter &DW, unsigned Form) const {
752 switch (Form) {
753 case DW_FORM_block1: DW.EmitInt8(Size); break;
754 case DW_FORM_block2: DW.EmitInt16(Size); break;
755 case DW_FORM_block4: DW.EmitInt32(Size); break;
756 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
757 default: assert(0 && "Improper form for block"); break;
758 }
759 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
760 DW.EOL("");
761 Values[i]->EmitValue(DW, Forms[i]);
762 }
763}
764
765/// SizeOf - Determine size of block data in bytes.
766///
767unsigned DIEBlock::SizeOf(const DwarfWriter &DW, unsigned Form) const {
768 switch (Form) {
769 case DW_FORM_block1: return Size + sizeof(int8_t);
770 case DW_FORM_block2: return Size + sizeof(int16_t);
771 case DW_FORM_block4: return Size + sizeof(int32_t);
772 case DW_FORM_block: return Size + DW.SizeULEB128(Size);
773 default: assert(0 && "Improper form for block"); break;
774 }
775 return 0;
776}
777
778/// AddUInt - Add an unsigned integer value.
779///
780void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
781 DIEInteger *DI = new DIEInteger(Integer);
782 Values.push_back(DI);
783 if (Form == 0) Form = DI->BestForm(false);
784 Forms.push_back(Form);
785}
786
787/// AddSInt - Add an signed integer value.
788///
789void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
790 DIEInteger *DI = new DIEInteger(Integer);
791 Values.push_back(DI);
792 if (Form == 0) Form = DI->BestForm(true);
793 Forms.push_back(Form);
794}
795
796/// AddString - Add a std::string value.
797///
798void DIEBlock::AddString(unsigned Form, const std::string &String) {
799 Values.push_back(new DIEString(String));
800 Forms.push_back(Form);
801}
802
803/// AddLabel - Add a Dwarf label value.
804///
805void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
806 Values.push_back(new DIEDwarfLabel(Label));
807 Forms.push_back(Form);
808}
809
810/// AddObjectLabel - Add a non-Dwarf label value.
811///
812void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
813 Values.push_back(new DIEObjectLabel(Label));
814 Forms.push_back(Form);
815}
816
817/// AddDelta - Add a label delta value.
818///
819void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
820 Values.push_back(new DIEDelta(Hi, Lo));
821 Forms.push_back(Form);
822}
823
824/// AddDIEntry - Add a DIE value.
825///
826void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
827 Values.push_back(new DIEntry(Entry));
828 Forms.push_back(Form);
829}
830
831//===----------------------------------------------------------------------===//
832
Jim Laskey0420f2a2006-02-22 19:02:11 +0000833DIE::DIE(unsigned Tag)
834: Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
Jim Laskeyd18e2892006-01-20 20:34:06 +0000835, AbbrevID(0)
836, Offset(0)
837, Size(0)
Jim Laskeyd18e2892006-01-20 20:34:06 +0000838, Children()
839, Values()
840{}
841
842DIE::~DIE() {
843 if (Abbrev) delete Abbrev;
844
Jim Laskey52060a02006-01-24 00:49:18 +0000845 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000846 delete Children[i];
847 }
848
Jim Laskey52060a02006-01-24 00:49:18 +0000849 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000850 delete Values[j];
851 }
Jim Laskeyd18e2892006-01-20 20:34:06 +0000852}
853
Jim Laskeyb8509c52006-03-23 18:07:55 +0000854/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
855///
856void DIE::AddSiblingOffset() {
857 DIEInteger *DI = new DIEInteger(0);
858 Values.insert(Values.begin(), DI);
859 Abbrev->AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
860}
861
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000862/// AddUInt - Add an unsigned integer attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000863///
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000864void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000865 DIEInteger *DI = new DIEInteger(Integer);
866 Values.push_back(DI);
867 if (!Form) Form = DI->BestForm(false);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000868 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000869}
870
871/// AddSInt - Add an signed integer attribute data and value.
872///
873void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000874 DIEInteger *DI = new DIEInteger(Integer);
875 Values.push_back(DI);
876 if (!Form) Form = DI->BestForm(true);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000877 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000878}
879
880/// AddString - Add a std::string attribute data and value.
881///
882void DIE::AddString(unsigned Attribute, unsigned Form,
883 const std::string &String) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000884 Values.push_back(new DIEString(String));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000885 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000886}
887
888/// AddLabel - Add a Dwarf label attribute data and value.
889///
890void DIE::AddLabel(unsigned Attribute, unsigned Form,
891 const DWLabel &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000892 Values.push_back(new DIEDwarfLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000893 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000894}
895
Jim Laskey52060a02006-01-24 00:49:18 +0000896/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000897///
Jim Laskey52060a02006-01-24 00:49:18 +0000898void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
899 const std::string &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000900 Values.push_back(new DIEObjectLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000901 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000902}
903
904/// AddDelta - Add a label delta attribute data and value.
905///
906void DIE::AddDelta(unsigned Attribute, unsigned Form,
907 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000908 Values.push_back(new DIEDelta(Hi, Lo));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000909 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000910}
911
912/// AddDIEntry - Add a DIE attribute data and value.
913///
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000914void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000915 Values.push_back(new DIEntry(Entry));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000916 Abbrev->AddAttribute(Attribute, Form);
917}
918
919/// AddBlock - Add block data.
920///
921void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
922 assert(Block->Size && "Block size has not been computed");
923 Values.push_back(Block);
924 if (!Form) Form = Block->BestForm();
925 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000926}
927
928/// Complete - Indicate that all attributes have been added and ready to get an
929/// abbreviation ID.
930void DIE::Complete(DwarfWriter &DW) {
931 AbbrevID = DW.NewAbbreviation(Abbrev);
932 delete Abbrev;
933 Abbrev = NULL;
934}
935
936/// AddChild - Add a child to the DIE.
937///
938void DIE::AddChild(DIE *Child) {
Jim Laskey0420f2a2006-02-22 19:02:11 +0000939 assert(Abbrev && "Adding children without an abbreviation");
940 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000941 Children.push_back(Child);
942}
943
944//===----------------------------------------------------------------------===//
945
Jim Laskey3ebe71d2006-09-01 12:55:05 +0000946/// DwarfWriter
Jim Laskeyd18e2892006-01-20 20:34:06 +0000947
948//===----------------------------------------------------------------------===//
Jim Laskey063e7652006-01-17 17:31:53 +0000949
950/// PrintHex - Print a value as a hexidecimal value.
951///
952void DwarfWriter::PrintHex(int Value) const {
953 O << "0x" << std::hex << Value << std::dec;
954}
955
956/// EOL - Print a newline character to asm stream. If a comment is present
957/// then it will be printed first. Comments should not contain '\n'.
958void DwarfWriter::EOL(const std::string &Comment) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000959 if (DwarfVerbose && !Comment.empty()) {
Jim Laskey063e7652006-01-17 17:31:53 +0000960 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +0000961 << TAI->getCommentString()
Jim Laskey063e7652006-01-17 17:31:53 +0000962 << " "
963 << Comment;
964 }
965 O << "\n";
966}
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000967
Jim Laskeyb8509c52006-03-23 18:07:55 +0000968/// EmitAlign - Print a align directive.
969///
970void DwarfWriter::EmitAlign(unsigned Alignment) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000971 O << TAI->getAlignDirective() << Alignment << "\n";
Jim Laskeyb8509c52006-03-23 18:07:55 +0000972}
973
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000974/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
Jim Laskey063e7652006-01-17 17:31:53 +0000975/// unsigned leb128 value.
976void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000977 if (TAI->hasLEB128()) {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000978 O << "\t.uleb128\t"
979 << Value;
980 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000981 O << TAI->getData8bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +0000982 PrintULEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000983 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000984}
985
986/// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
Jim Laskey063e7652006-01-17 17:31:53 +0000987/// signed leb128 value.
988void DwarfWriter::EmitSLEB128Bytes(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000989 if (TAI->hasLEB128()) {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000990 O << "\t.sleb128\t"
991 << Value;
992 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000993 O << TAI->getData8bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +0000994 PrintSLEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000995 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000996}
997
Jim Laskey063e7652006-01-17 17:31:53 +0000998/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +0000999/// representing an unsigned leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +00001000void DwarfWriter::PrintULEB128(unsigned Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001001 do {
1002 unsigned Byte = Value & 0x7f;
1003 Value >>= 7;
1004 if (Value) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001005 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001006 if (Value) O << ", ";
1007 } while (Value);
1008}
1009
Jim Laskey063e7652006-01-17 17:31:53 +00001010/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
1011/// value.
1012unsigned DwarfWriter::SizeULEB128(unsigned Value) {
1013 unsigned Size = 0;
1014 do {
1015 Value >>= 7;
1016 Size += sizeof(int8_t);
1017 } while (Value);
1018 return Size;
1019}
1020
1021/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +00001022/// representing a signed leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +00001023void DwarfWriter::PrintSLEB128(int Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001024 int Sign = Value >> (8 * sizeof(Value) - 1);
1025 bool IsMore;
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001026
Jim Laskeyb2efb852006-01-04 22:28:25 +00001027 do {
1028 unsigned Byte = Value & 0x7f;
1029 Value >>= 7;
1030 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1031 if (IsMore) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001032 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001033 if (IsMore) O << ", ";
1034 } while (IsMore);
1035}
1036
Jim Laskey063e7652006-01-17 17:31:53 +00001037/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
1038/// value.
1039unsigned DwarfWriter::SizeSLEB128(int Value) {
1040 unsigned Size = 0;
1041 int Sign = Value >> (8 * sizeof(Value) - 1);
1042 bool IsMore;
1043
1044 do {
1045 unsigned Byte = Value & 0x7f;
1046 Value >>= 7;
1047 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1048 Size += sizeof(int8_t);
1049 } while (IsMore);
1050 return Size;
1051}
1052
Jim Laskeyda427fa2006-01-27 20:31:25 +00001053/// EmitInt8 - Emit a byte directive and value.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001054///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001055void DwarfWriter::EmitInt8(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001056 O << TAI->getData8bitsDirective();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001057 PrintHex(Value & 0xFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001058}
1059
Jim Laskeyda427fa2006-01-27 20:31:25 +00001060/// EmitInt16 - Emit a short directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001061///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001062void DwarfWriter::EmitInt16(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001063 O << TAI->getData16bitsDirective();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001064 PrintHex(Value & 0xFFFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001065}
1066
Jim Laskeyda427fa2006-01-27 20:31:25 +00001067/// EmitInt32 - Emit a long directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001068///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001069void DwarfWriter::EmitInt32(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001070 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001071 PrintHex(Value);
1072}
1073
Jim Laskeyda427fa2006-01-27 20:31:25 +00001074/// EmitInt64 - Emit a long long directive and value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001075///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001076void DwarfWriter::EmitInt64(uint64_t Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001077 if (TAI->getData64bitsDirective()) {
1078 O << TAI->getData64bitsDirective() << "0x" << std::hex << Value << std::dec;
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001079 } else {
Owen Andersona69571c2006-05-03 01:29:57 +00001080 if (TD->isBigEndian()) {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001081 EmitInt32(unsigned(Value >> 32)); O << "\n";
1082 EmitInt32(unsigned(Value));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001083 } else {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001084 EmitInt32(unsigned(Value)); O << "\n";
1085 EmitInt32(unsigned(Value >> 32));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001086 }
1087 }
1088}
1089
Jim Laskey063e7652006-01-17 17:31:53 +00001090/// EmitString - Emit a string with quotes and a null terminator.
1091/// Special characters are emitted properly. (Eg. '\t')
1092void DwarfWriter::EmitString(const std::string &String) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001093 O << TAI->getAsciiDirective()
Jim Laskey063e7652006-01-17 17:31:53 +00001094 << "\"";
Jim Laskey52060a02006-01-24 00:49:18 +00001095 for (unsigned i = 0, N = String.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001096 unsigned char C = String[i];
1097
1098 if (!isascii(C) || iscntrl(C)) {
1099 switch(C) {
1100 case '\b': O << "\\b"; break;
1101 case '\f': O << "\\f"; break;
1102 case '\n': O << "\\n"; break;
1103 case '\r': O << "\\r"; break;
1104 case '\t': O << "\\t"; break;
1105 default:
1106 O << '\\';
Jim Laskey3ebe71d2006-09-01 12:55:05 +00001107 O << char('0' + ((C >> 6) & 7));
1108 O << char('0' + ((C >> 3) & 7));
1109 O << char('0' + ((C >> 0) & 7));
Jim Laskey063e7652006-01-17 17:31:53 +00001110 break;
1111 }
1112 } else if (C == '\"') {
1113 O << "\\\"";
1114 } else if (C == '\'') {
1115 O << "\\\'";
1116 } else {
1117 O << C;
1118 }
1119 }
1120 O << "\\0\"";
1121}
1122
1123/// PrintLabelName - Print label name in form used by Dwarf writer.
1124///
1125void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001126 O << TAI->getPrivateGlobalPrefix()
Jim Laskeyb2efb852006-01-04 22:28:25 +00001127 << "debug_"
Jim Laskey0420f2a2006-02-22 19:02:11 +00001128 << Tag;
1129 if (Number) O << Number;
Jim Laskeyb2efb852006-01-04 22:28:25 +00001130}
1131
Jim Laskey063e7652006-01-17 17:31:53 +00001132/// EmitLabel - Emit location label for internal use by Dwarf.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001133///
Jim Laskey063e7652006-01-17 17:31:53 +00001134void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
1135 PrintLabelName(Tag, Number);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001136 O << ":\n";
1137}
1138
Jim Laskeye719a7c2006-01-18 16:54:26 +00001139/// EmitReference - Emit a reference to a label.
Jim Laskey063e7652006-01-17 17:31:53 +00001140///
Jim Laskeye719a7c2006-01-18 16:54:26 +00001141void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001142 if (TAI->getAddressSize() == 4)
1143 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001144 else
Jim Laskey563321a2006-09-06 18:34:40 +00001145 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001146
1147 PrintLabelName(Tag, Number);
1148}
Jim Laskey73683212006-01-21 00:59:54 +00001149void DwarfWriter::EmitReference(const std::string &Name) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001150 if (TAI->getAddressSize() == 4)
1151 O << TAI->getData32bitsDirective();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001152 else
Jim Laskey563321a2006-09-06 18:34:40 +00001153 O << TAI->getData64bitsDirective();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001154
1155 O << Name;
1156}
Jim Laskey063e7652006-01-17 17:31:53 +00001157
1158/// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
1159/// assemblers do not accept absolute expressions with data directives, so there
1160/// is an option (needsSet) to use an intermediary 'set' expression.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001161void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
1162 const char *TagLo, unsigned NumberLo) const {
Jim Laskeya0f3d172006-09-07 22:06:40 +00001163 if (TAI->needsSet()) {
Jim Laskey063e7652006-01-17 17:31:53 +00001164 static unsigned SetCounter = 0;
Jim Laskeyd18e2892006-01-20 20:34:06 +00001165
Jim Laskey063e7652006-01-17 17:31:53 +00001166 O << "\t.set\t";
1167 PrintLabelName("set", SetCounter);
1168 O << ",";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001169 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001170 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001171 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001172 O << "\n";
1173
Jim Laskey563321a2006-09-06 18:34:40 +00001174 if (TAI->getAddressSize() == sizeof(int32_t))
1175 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001176 else
Jim Laskey563321a2006-09-06 18:34:40 +00001177 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001178
1179 PrintLabelName("set", SetCounter);
1180
Jim Laskey52060a02006-01-24 00:49:18 +00001181 ++SetCounter;
Jim Laskey063e7652006-01-17 17:31:53 +00001182 } else {
Jim Laskey563321a2006-09-06 18:34:40 +00001183 if (TAI->getAddressSize() == sizeof(int32_t))
1184 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001185 else
Jim Laskey563321a2006-09-06 18:34:40 +00001186 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001187
Jim Laskeyd18e2892006-01-20 20:34:06 +00001188 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001189 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001190 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001191 }
1192}
1193
Jim Laskeyd18e2892006-01-20 20:34:06 +00001194/// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
Jim Laskey063e7652006-01-17 17:31:53 +00001195///
Jim Laskeyd18e2892006-01-20 20:34:06 +00001196unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
1197 return Abbreviations.insert(*Abbrev);
1198}
1199
1200/// NewString - Add a string to the constant pool and returns a label.
1201///
1202DWLabel DwarfWriter::NewString(const std::string &String) {
1203 unsigned StringID = StringPool.insert(String);
1204 return DWLabel("string", StringID);
1205}
1206
Jim Laskeyb8509c52006-03-23 18:07:55 +00001207/// AddSourceLine - Add location information to specified debug information
1208/// entry.
Jim Laskeyd16f2a72006-06-19 19:49:42 +00001209void DwarfWriter::AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line){
Jim Laskeyb8509c52006-03-23 18:07:55 +00001210 if (File && Line) {
1211 CompileUnit *FileUnit = FindCompileUnit(File);
1212 unsigned FileID = FileUnit->getID();
1213 Die->AddUInt(DW_AT_decl_file, 0, FileID);
1214 Die->AddUInt(DW_AT_decl_line, 0, Line);
1215 }
1216}
1217
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001218/// AddAddress - Add an address attribute to a die based on the location
1219/// provided.
1220void DwarfWriter::AddAddress(DIE *Die, unsigned Attribute,
Jim Laskey41886992006-04-07 16:34:46 +00001221 const MachineLocation &Location) {
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001222 DIEBlock *Block = new DIEBlock();
Jim Laskey3d3d4042006-08-25 19:39:52 +00001223 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1224
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001225 if (Location.isRegister()) {
Jim Laskey3d3d4042006-08-25 19:39:52 +00001226 if (Reg < 32) {
1227 Block->AddUInt(DW_FORM_data1, DW_OP_reg0 + Reg);
1228 } else {
1229 Block->AddUInt(DW_FORM_data1, DW_OP_regx);
1230 Block->AddUInt(DW_FORM_udata, Reg);
1231 }
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001232 } else {
Jim Laskey3d3d4042006-08-25 19:39:52 +00001233 if (Reg < 32) {
1234 Block->AddUInt(DW_FORM_data1, DW_OP_breg0 + Reg);
1235 } else {
1236 Block->AddUInt(DW_FORM_data1, DW_OP_bregx);
1237 Block->AddUInt(DW_FORM_udata, Reg);
1238 }
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001239 Block->AddUInt(DW_FORM_sdata, Location.getOffset());
1240 }
1241 Block->ComputeSize(*this);
1242 Die->AddBlock(Attribute, 0, Block);
1243}
1244
Jim Laskey90c79d72006-03-23 23:02:34 +00001245/// getDieMapSlotFor - Returns the debug information entry map slot for the
1246/// specified debug descriptor.
1247DIE *&DwarfWriter::getDieMapSlotFor(DebugInfoDesc *DD) {
1248 return DescToDieMap[DD];
Jim Laskey0420f2a2006-02-22 19:02:11 +00001249}
Jim Laskey760383e2006-08-21 21:20:18 +00001250
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001251/// NewType - Create a new type DIE.
1252///
Jim Laskey90c79d72006-03-23 23:02:34 +00001253DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit) {
1254 if (!TyDesc) {
1255 // FIXME - Hack for missing types
1256 DIE *Die = new DIE(DW_TAG_base_type);
1257 Die->AddUInt(DW_AT_byte_size, 0, 4);
1258 Die->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1259 Unit->getDie()->AddChild(Die);
1260 return Die;
1261 }
Jim Laskey0c0feb92006-10-04 10:40:15 +00001262
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001263 // Check for pre-existence.
Jim Laskey90c79d72006-03-23 23:02:34 +00001264 DIE *&Slot = Unit->getDieMapSlotFor(TyDesc);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001265 if (Slot) return Slot;
1266
Jim Laskey0c0feb92006-10-04 10:40:15 +00001267 // Type DIE result.
1268 DIE *Ty = NULL;
1269
Jim Laskey339ec4c2006-10-13 13:02:19 +00001270 // FIXME - Not sure why programs and variables are coming through here.
Jim Laskey0c0feb92006-10-04 10:40:15 +00001271 // Short cut for handling subprogram types (not really a TyDesc.)
1272 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1273 Slot = Ty = new DIE(DW_TAG_pointer_type);
1274 Ty->AddUInt(DW_AT_byte_size, 0, TAI->getAddressSize());
1275 Ty->AddString(DW_AT_name, DW_FORM_string, SubprogramTy->getName());
1276 Context->AddChild(Ty);
1277 return Slot;
1278 }
Jim Laskey339ec4c2006-10-13 13:02:19 +00001279 // Short cut for handling global variable types (not really a TyDesc.)
1280 if (GlobalVariableDesc *GlobalVariableTy =
1281 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1282 Slot = Ty = new DIE(DW_TAG_pointer_type);
1283 Ty->AddUInt(DW_AT_byte_size, 0, TAI->getAddressSize());
1284 Ty->AddString(DW_AT_name, DW_FORM_string, GlobalVariableTy->getName());
1285 Context->AddChild(Ty);
1286 return Slot;
1287 }
Jim Laskey0c0feb92006-10-04 10:40:15 +00001288
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001289 // Get core information.
1290 const std::string &Name = TyDesc->getName();
Jim Laskey288fe0f2006-03-01 18:13:05 +00001291 uint64_t Size = TyDesc->getSize() >> 3;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001292
Jim Laskey434b40b2006-02-23 22:37:30 +00001293 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001294 // Fundamental types like int, float, bool
Jim Laskey434b40b2006-02-23 22:37:30 +00001295 Slot = Ty = new DIE(DW_TAG_base_type);
1296 unsigned Encoding = BasicTy->getEncoding();
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001297 Ty->AddUInt(DW_AT_encoding, DW_FORM_data1, Encoding);
Jim Laskey69906002006-02-24 16:46:40 +00001298 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001299 // Create specific DIE.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001300 Slot = Ty = new DIE(DerivedTy->getTag());
Jim Laskey69906002006-02-24 16:46:40 +00001301
1302 // Map to main type, void will not have a type.
1303 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001304 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1305 NewType(Context, FromTy, Unit));
Jim Laskey69906002006-02-24 16:46:40 +00001306 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001307 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
Jim Laskey7089f452006-06-16 13:14:03 +00001308 // Fetch tag
1309 unsigned Tag = CompTy->getTag();
1310
Jim Laskeyf8913f12006-03-01 17:53:02 +00001311 // Create specific DIE.
Jim Laskey7089f452006-06-16 13:14:03 +00001312 Slot = Ty = Tag == DW_TAG_vector_type ? new DIE(DW_TAG_array_type) :
1313 new DIE(Tag);
1314
Jim Laskeyf8913f12006-03-01 17:53:02 +00001315 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1316
Jim Laskey7089f452006-06-16 13:14:03 +00001317 switch (Tag) {
1318 case DW_TAG_vector_type: Ty->AddUInt(DW_AT_GNU_vector, DW_FORM_flag, 1);
1319 // Fall thru
Jim Laskey9c4447a2006-03-01 20:39:36 +00001320 case DW_TAG_array_type: {
Jim Laskeyf8913f12006-03-01 17:53:02 +00001321 // Add element type.
1322 if (TypeDesc *FromTy = CompTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001323 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1324 NewType(Context, FromTy, Unit));
Jim Laskeyf8913f12006-03-01 17:53:02 +00001325 }
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001326
Jim Laskeyf8913f12006-03-01 17:53:02 +00001327 // Don't emit size attribute.
1328 Size = 0;
1329
1330 // Construct an anonymous type for index type.
1331 DIE *IndexTy = new DIE(DW_TAG_base_type);
1332 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1333 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1334 // Add to context.
Jim Laskey92ae7402006-03-01 18:20:30 +00001335 Context->AddChild(IndexTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001336
1337 // Add subranges to array type.
1338 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1339 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1340 int64_t Lo = SRD->getLo();
1341 int64_t Hi = SRD->getHi();
1342 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1343
1344 // If a range is available.
1345 if (Lo != Hi) {
1346 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1347 // Only add low if non-zero.
Jim Laskey6a3eb012006-03-01 23:52:37 +00001348 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1349 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001350 }
1351 Ty->AddChild(Subrange);
1352 }
1353
1354 break;
1355 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001356 case DW_TAG_structure_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +00001357 case DW_TAG_union_type: {
Jim Laskeyf01e5472006-03-03 15:06:57 +00001358 // Add elements to structure type.
1359 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskey760383e2006-08-21 21:20:18 +00001360 DebugInfoDesc *Element = Elements[i];
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001361
Jim Laskey760383e2006-08-21 21:20:18 +00001362 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)) {
1363 // Add field or base class.
Jim Laskey54689c22006-03-09 13:28:47 +00001364
Jim Laskey760383e2006-08-21 21:20:18 +00001365 unsigned Tag = MemberDesc->getTag();
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001366
Jim Laskey760383e2006-08-21 21:20:18 +00001367 // Extract the basic information.
1368 const std::string &Name = MemberDesc->getName();
1369 TypeDesc *MemTy = MemberDesc->getFromType();
1370 uint64_t Size = MemberDesc->getSize();
1371 uint64_t Align = MemberDesc->getAlign();
1372 uint64_t Offset = MemberDesc->getOffset();
1373
1374 // Construct member debug information entry.
1375 DIE *Member = new DIE(Tag);
1376
1377 // Add name if not "".
1378 if (!Name.empty())Member->AddString(DW_AT_name, DW_FORM_string, Name);
1379 // Add location if available.
1380 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1381
1382 // Most of the time the field info is the same as the members.
1383 uint64_t FieldSize = Size;
1384 uint64_t FieldAlign = Align;
1385 uint64_t FieldOffset = Offset;
1386
1387 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1388 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1389 NewType(Context, FromTy, Unit));
1390 FieldSize = FromTy->getSize();
1391 FieldAlign = FromTy->getSize();
1392 }
1393
1394 // Unless we have a bit field.
1395 if (Tag == DW_TAG_member && FieldSize != Size) {
1396 // Construct the alignment mask.
1397 uint64_t AlignMask = ~(FieldAlign - 1);
1398 // Determine the high bit + 1 of the declared size.
1399 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1400 // Work backwards to determine the base offset of the field.
1401 FieldOffset = HiMark - FieldSize;
1402 // Now normalize offset to the field.
1403 Offset -= FieldOffset;
1404
1405 // Maybe we need to work from the other end.
1406 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1407
1408 // Add size and offset.
1409 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1410 Member->AddUInt(DW_AT_bit_size, 0, Size);
1411 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
1412 }
1413
1414 // Add computation for offset.
1415 DIEBlock *Block = new DIEBlock();
1416 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
1417 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
1418 Block->ComputeSize(*this);
1419 Member->AddBlock(DW_AT_data_member_location, 0, Block);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001420
Jim Laskey760383e2006-08-21 21:20:18 +00001421 // Add accessibility (public default unless is base class.
1422 if (MemberDesc->isProtected()) {
1423 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_protected);
1424 } else if (MemberDesc->isPrivate()) {
1425 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_private);
1426 } else if (Tag == DW_TAG_inheritance) {
1427 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_public);
1428 }
1429
1430 Ty->AddChild(Member);
1431 } else if (GlobalVariableDesc *StaticDesc =
1432 dyn_cast<GlobalVariableDesc>(Element)) {
1433 // Add static member.
1434
1435 // Construct member debug information entry.
1436 DIE *Static = new DIE(DW_TAG_variable);
1437
1438 // Add name and mangled name.
1439 const std::string &Name = StaticDesc->getDisplayName();
1440 const std::string &MangledName = StaticDesc->getName();
1441 Static->AddString(DW_AT_name, DW_FORM_string, Name);
1442 Static->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1443 MangledName);
1444
1445 // Add location.
1446 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1447
1448 // Add type.
1449 if (TypeDesc *StaticTy = StaticDesc->getType()) {
1450 Static->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1451 NewType(Context, StaticTy, Unit));
1452 }
1453
1454 // Add flags.
1455 Static->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1456 Static->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1457
1458 Ty->AddChild(Static);
1459 } else if (SubprogramDesc *MethodDesc =
1460 dyn_cast<SubprogramDesc>(Element)) {
1461 // Add member function.
1462
1463 // Construct member debug information entry.
1464 DIE *Method = new DIE(DW_TAG_subprogram);
1465
1466 // Add name and mangled name.
1467 const std::string &Name = MethodDesc->getDisplayName();
1468 const std::string &MangledName = MethodDesc->getName();
1469 bool IsCTor = false;
1470
1471 if (Name.empty()) {
1472 Method->AddString(DW_AT_name, DW_FORM_string, MangledName);
1473 IsCTor = TyDesc->getName() == MangledName;
1474 } else {
1475 Method->AddString(DW_AT_name, DW_FORM_string, Name);
1476 Method->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1477 MangledName);
1478 }
1479
1480 // Add location.
1481 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1482
1483 // Add type.
1484 if (CompositeTypeDesc *MethodTy =
1485 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1486 // Get argument information.
1487 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1488
1489 // If not a ctor.
1490 if (!IsCTor) {
1491 // Add return type.
1492 Method->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1493 NewType(Context, dyn_cast<TypeDesc>(Args[0]),
1494 Unit));
1495 }
1496
1497 // Add arguments.
1498 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1499 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1500 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1501 NewType(Context, cast<TypeDesc>(Args[i]), Unit));
1502 Arg->AddUInt(DW_AT_artificial, DW_FORM_flag, 1);
1503 Method->AddChild(Arg);
1504 }
1505 }
1506
1507 // Add flags.
1508 Method->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1509 Method->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1510
1511 Ty->AddChild(Method);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001512 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001513 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001514 break;
1515 }
Jim Laskey9c4447a2006-03-01 20:39:36 +00001516 case DW_TAG_enumeration_type: {
Jim Laskey6a3eb012006-03-01 23:52:37 +00001517 // Add enumerators to enumeration type.
1518 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1519 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1520 const std::string &Name = ED->getName();
1521 int64_t Value = ED->getValue();
1522 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1523 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1524 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1525 Ty->AddChild(Enumerator);
1526 }
1527
Jim Laskeyf8913f12006-03-01 17:53:02 +00001528 break;
1529 }
Jim Laskey650f6092006-06-20 19:41:06 +00001530 case DW_TAG_subroutine_type: {
1531 // Add prototype flag.
1532 Ty->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
1533 // Add return type.
1534 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
Jim Laskeyd04c1592006-07-13 15:27:42 +00001535 NewType(Context, dyn_cast<TypeDesc>(Elements[0]), Unit));
Jim Laskey650f6092006-06-20 19:41:06 +00001536
1537 // Add arguments.
1538 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1539 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1540 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1541 NewType(Context, cast<TypeDesc>(Elements[i]), Unit));
1542 Ty->AddChild(Arg);
1543 }
1544
1545 break;
1546 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001547 default: break;
1548 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001549 }
Jim Laskey0c0feb92006-10-04 10:40:15 +00001550
Jim Laskey434b40b2006-02-23 22:37:30 +00001551 assert(Ty && "Type not supported yet");
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001552
Jim Laskey69906002006-02-24 16:46:40 +00001553 // Add size if non-zero (derived types don't have a size.)
Jim Laskey434b40b2006-02-23 22:37:30 +00001554 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
Jim Laskey69906002006-02-24 16:46:40 +00001555 // Add name if not anonymous or intermediate type.
Jim Laskey434b40b2006-02-23 22:37:30 +00001556 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001557 // Add source line info if available.
1558 AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
Jim Laskey434b40b2006-02-23 22:37:30 +00001559
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001560 // Add to context owner.
Jim Laskey92ae7402006-03-01 18:20:30 +00001561 Context->AddChild(Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001562
1563 return Slot;
1564}
1565
Jim Laskeyb8509c52006-03-23 18:07:55 +00001566/// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey063e7652006-01-17 17:31:53 +00001567///
Jim Laskeybd761842006-02-27 17:27:12 +00001568CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1569 unsigned ID) {
1570 // Construct debug information entry.
1571 DIE *Die = new DIE(DW_TAG_compile_unit);
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001572 Die->AddDelta (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0),
1573 DWLabel("section_line", 0));
Jim Laskey89d67fa2006-06-23 12:51:53 +00001574// Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1575// Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
Jim Laskeybd761842006-02-27 17:27:12 +00001576 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1577 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1578 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1579 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1580
Jim Laskeyb8509c52006-03-23 18:07:55 +00001581 // Add debug information entry to descriptor map.
Jim Laskey90c79d72006-03-23 23:02:34 +00001582 DIE *&Slot = getDieMapSlotFor(UnitDesc);
1583 Slot = Die;
Jim Laskeybd761842006-02-27 17:27:12 +00001584
1585 // Construct compile unit.
1586 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1587
1588 // Add Unit to compile unit map.
1589 DescToUnitMap[UnitDesc] = Unit;
1590
1591 return Unit;
1592}
Jim Laskey0420f2a2006-02-22 19:02:11 +00001593
Jim Laskeybd761842006-02-27 17:27:12 +00001594/// FindCompileUnit - Get the compile unit for the given descriptor.
1595///
1596CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1597 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1598 assert(Unit && "Missing compile unit.");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001599 return Unit;
Jim Laskey063e7652006-01-17 17:31:53 +00001600}
1601
Jim Laskey0420f2a2006-02-22 19:02:11 +00001602/// NewGlobalVariable - Add a new global variable DIE.
1603///
1604DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001605 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001606 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1607 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey90c79d72006-03-23 23:02:34 +00001608
1609 // Check for pre-existence.
1610 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1611 if (Slot) return Slot;
1612
Jim Laskey0420f2a2006-02-22 19:02:11 +00001613 // Get the global variable itself.
1614 GlobalVariable *GV = GVD->getGlobalVariable();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001615
Jim Laskeye85fb672006-09-18 14:47:26 +00001616 const std::string &Name = GVD->hasMangledName() ? GVD->getDisplayName()
1617 : GVD->getName();
1618 const std::string &MangledName = GVD->hasMangledName() ? GVD->getName()
1619 : "";
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001620 // Get the global's type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001621 DIE *Type = NewType(Unit->getDie(), GVD->getType(), Unit);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001622
1623 // Create the globale variable DIE.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001624 DIE *VariableDie = new DIE(DW_TAG_variable);
Jim Laskeye85fb672006-09-18 14:47:26 +00001625 VariableDie->AddString(DW_AT_name, DW_FORM_string, Name);
1626 if (!MangledName.empty()) {
1627 VariableDie->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1628 MangledName);
1629 }
1630 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1631 VariableDie->AddUInt(DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001632
1633 // Add source line info if available.
1634 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Jim Laskeyba8a2ee2006-10-16 19:38:41 +00001635
1636 // Work up linkage name.
Jim Laskey99e41ee2006-10-17 17:17:24 +00001637 const std::string LinkageName = Asm->getGlobalLinkName(GV);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001638
Jim Laskeyb8509c52006-03-23 18:07:55 +00001639 // Add address.
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001640 DIEBlock *Block = new DIEBlock();
1641 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
Jim Laskeyba8a2ee2006-10-16 19:38:41 +00001642 Block->AddObjectLabel(DW_FORM_udata, LinkageName);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001643 Block->ComputeSize(*this);
1644 VariableDie->AddBlock(DW_AT_location, 0, Block);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001645
1646 // Add to map.
1647 Slot = VariableDie;
1648
1649 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001650 Unit->getDie()->AddChild(VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001651
1652 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001653 // FIXME - need to check external flag.
1654 Unit->AddGlobal(Name, VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001655
1656 return VariableDie;
1657}
1658
1659/// NewSubprogram - Add a new subprogram DIE.
1660///
1661DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001662 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001663 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1664 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001665
Jim Laskey90c79d72006-03-23 23:02:34 +00001666 // Check for pre-existence.
1667 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1668 if (Slot) return Slot;
1669
Jim Laskey0420f2a2006-02-22 19:02:11 +00001670 // Gather the details (simplify add attribute code.)
Jim Laskeye85fb672006-09-18 14:47:26 +00001671 const std::string &Name = SPD->hasMangledName() ? SPD->getDisplayName()
1672 : SPD->getName();
1673 const std::string &MangledName = SPD->hasMangledName() ? SPD->getName()
1674 : "";
Jim Laskey90c79d72006-03-23 23:02:34 +00001675 DIE *Type = NewType(Unit->getDie(), SPD->getType(), Unit);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001676 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001677
Jim Laskey8a8e9752006-02-27 20:37:42 +00001678 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
Jim Laskeye85fb672006-09-18 14:47:26 +00001679 SubprogramDie->AddString(DW_AT_name, DW_FORM_string, Name);
1680 if (!MangledName.empty()) {
1681 SubprogramDie->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1682 MangledName);
Jim Laskey41886992006-04-07 16:34:46 +00001683 }
Jim Laskeye85fb672006-09-18 14:47:26 +00001684 if (Type) {
1685 SubprogramDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1686 }
1687 SubprogramDie->AddUInt(DW_AT_external, DW_FORM_flag, IsExternal);
1688 SubprogramDie->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001689
Jim Laskeyb8509c52006-03-23 18:07:55 +00001690 // Add source line info if available.
1691 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1692
Jim Laskey0420f2a2006-02-22 19:02:11 +00001693 // Add to map.
1694 Slot = SubprogramDie;
1695
1696 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001697 Unit->getDie()->AddChild(SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001698
1699 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001700 Unit->AddGlobal(Name, SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001701
1702 return SubprogramDie;
1703}
1704
Jim Laskeyb8509c52006-03-23 18:07:55 +00001705/// NewScopeVariable - Create a new scope variable.
1706///
1707DIE *DwarfWriter::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1708 // Get the descriptor.
1709 VariableDesc *VD = DV->getDesc();
1710
1711 // Translate tag to proper Dwarf tag. The result variable is dropped for now.
1712 unsigned Tag;
1713 switch (VD->getTag()) {
1714 case DW_TAG_return_variable: return NULL;
1715 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1716 case DW_TAG_auto_variable: // fall thru
1717 default: Tag = DW_TAG_variable; break;
1718 }
1719
1720 // Define variable debug information entry.
1721 DIE *VariableDie = new DIE(Tag);
1722 VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
1723
1724 // Add source line info if available.
1725 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1726
1727 // Add variable type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001728 DIE *Type = NewType(Unit->getDie(), VD->getType(), Unit);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001729 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1730
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001731 // Add variable address.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001732 MachineLocation Location;
Jim Laskey41886992006-04-07 16:34:46 +00001733 RI->getLocation(*MF, DV->getFrameIndex(), Location);
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001734 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001735
1736 return VariableDie;
1737}
1738
1739/// ConstructScope - Construct the components of a scope.
1740///
1741void DwarfWriter::ConstructScope(DebugScope *ParentScope,
1742 DIE *ParentDie, CompileUnit *Unit) {
1743 // Add variables to scope.
1744 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1745 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1746 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1747 if (VariableDie) ParentDie->AddChild(VariableDie);
1748 }
1749
1750 // Add nested scopes.
1751 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1752 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1753 // Define the Scope debug information entry.
1754 DebugScope *Scope = Scopes[j];
1755 // FIXME - Ignore inlined functions for the time being.
Jim Laskeyd16f2a72006-06-19 19:49:42 +00001756 if (!Scope->getParent()) continue;
Jim Laskeyb8509c52006-03-23 18:07:55 +00001757
Jim Laskey66ebf092006-10-23 14:56:37 +00001758 unsigned StartID = Scope->getStartLabelID();
1759 unsigned EndID = Scope->getEndLabelID();
1760
1761 // Throw out scope if block is discarded.
1762 if (StartID && !DebugInfo->isLabelValid(StartID)) continue;
1763 if (EndID && !DebugInfo->isLabelValid(EndID)) continue;
1764
Jim Laskeyb8509c52006-03-23 18:07:55 +00001765 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1766
1767 // Add the scope bounds.
Jim Laskey66ebf092006-10-23 14:56:37 +00001768 if (StartID) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001769 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1770 DWLabel("loc", StartID));
1771 } else {
1772 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1773 DWLabel("func_begin", SubprogramCount));
1774 }
Jim Laskey66ebf092006-10-23 14:56:37 +00001775 if (EndID) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001776 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1777 DWLabel("loc", EndID));
1778 } else {
1779 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1780 DWLabel("func_end", SubprogramCount));
1781 }
1782
1783 // Add the scope contents.
1784 ConstructScope(Scope, ScopeDie, Unit);
1785 ParentDie->AddChild(ScopeDie);
1786 }
1787}
1788
1789/// ConstructRootScope - Construct the scope for the subprogram.
1790///
1791void DwarfWriter::ConstructRootScope(DebugScope *RootScope) {
1792 // Exit if there is no root scope.
1793 if (!RootScope) return;
1794
1795 // Get the subprogram debug information entry.
1796 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Jim Laskey90c79d72006-03-23 23:02:34 +00001797
1798 // Get the compile unit context.
1799 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey41886992006-04-07 16:34:46 +00001800 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1801
Jim Laskey90c79d72006-03-23 23:02:34 +00001802 // Get the subprogram die.
1803 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001804 assert(SPDie && "Missing subprogram descriptor");
1805
1806 // Add the function bounds.
Jim Laskey6b92b8e2006-04-07 20:44:42 +00001807 SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1808 DWLabel("func_begin", SubprogramCount));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001809 SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1810 DWLabel("func_end", SubprogramCount));
Jim Laskey41886992006-04-07 16:34:46 +00001811 MachineLocation Location(RI->getFrameRegister(*MF));
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001812 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001813
Jim Laskeyb8509c52006-03-23 18:07:55 +00001814 ConstructScope(RootScope, SPDie, Unit);
1815}
1816
Jim Laskey063e7652006-01-17 17:31:53 +00001817/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1818/// tools to recognize the object file contains Dwarf information.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001819///
Jim Laskey014f98c2006-06-14 11:35:03 +00001820void DwarfWriter::EmitInitial() {
1821 // Check to see if we already emitted intial headers.
1822 if (didInitial) return;
1823 didInitial = true;
1824
Jim Laskey063e7652006-01-17 17:31:53 +00001825 // Dwarf sections base addresses.
Jim Laskey563321a2006-09-06 18:34:40 +00001826 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001827 EmitLabel("section_frame", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001828 Asm->SwitchToDataSection(TAI->getDwarfInfoSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001829 EmitLabel("section_info", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001830 EmitLabel("info", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001831 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001832 EmitLabel("section_abbrev", 0);
1833 EmitLabel("abbrev", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001834 Asm->SwitchToDataSection(TAI->getDwarfARangesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001835 EmitLabel("section_aranges", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001836 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001837 EmitLabel("section_macinfo", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001838 Asm->SwitchToDataSection(TAI->getDwarfLineSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001839 EmitLabel("section_line", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001840 EmitLabel("line", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001841 Asm->SwitchToDataSection(TAI->getDwarfLocSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001842 EmitLabel("section_loc", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001843 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001844 EmitLabel("section_pubnames", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001845 Asm->SwitchToDataSection(TAI->getDwarfStrSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001846 EmitLabel("section_str", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001847 Asm->SwitchToDataSection(TAI->getDwarfRangesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001848 EmitLabel("section_ranges", 0);
1849
Jim Laskey563321a2006-09-06 18:34:40 +00001850 Asm->SwitchToTextSection(TAI->getTextSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001851 EmitLabel("text_begin", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001852 Asm->SwitchToDataSection(TAI->getDataSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001853 EmitLabel("data_begin", 0);
Jim Laskey89d67fa2006-06-23 12:51:53 +00001854
Jim Laskey014f98c2006-06-14 11:35:03 +00001855 // Emit common frame information.
1856 EmitInitialDebugFrame();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001857}
1858
Jim Laskey063e7652006-01-17 17:31:53 +00001859/// EmitDIE - Recusively Emits a debug information entry.
1860///
1861void DwarfWriter::EmitDIE(DIE *Die) const {
1862 // Get the abbreviation for this DIE.
1863 unsigned AbbrevID = Die->getAbbrevID();
1864 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey0d086af2006-02-27 12:43:29 +00001865
1866 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001867
1868 // Emit the code (index) for the abbreviation.
1869 EmitULEB128Bytes(AbbrevID);
1870 EOL(std::string("Abbrev [" +
1871 utostr(AbbrevID) +
Jim Laskey0d086af2006-02-27 12:43:29 +00001872 "] 0x" + utohexstr(Die->getOffset()) +
1873 ":0x" + utohexstr(Die->getSize()) + " " +
1874 TagString(Abbrev.getTag())));
Jim Laskey063e7652006-01-17 17:31:53 +00001875
1876 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001877 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001878
1879 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001880 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001881 unsigned Attr = AbbrevData[i].getAttribute();
1882 unsigned Form = AbbrevData[i].getForm();
Jim Laskey063e7652006-01-17 17:31:53 +00001883 assert(Form && "Too many attributes for DIE (check abbreviation)");
1884
1885 switch (Attr) {
1886 case DW_AT_sibling: {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001887 EmitInt32(Die->SiblingOffset());
Jim Laskey063e7652006-01-17 17:31:53 +00001888 break;
1889 }
1890 default: {
1891 // Emit an attribute using the defined form.
1892 Values[i]->EmitValue(*this, Form);
1893 break;
1894 }
1895 }
1896
1897 EOL(AttributeString(Attr));
1898 }
1899
1900 // Emit the DIE children if any.
1901 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1902 const std::vector<DIE *> &Children = Die->getChildren();
1903
Jim Laskey52060a02006-01-24 00:49:18 +00001904 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskey063e7652006-01-17 17:31:53 +00001905 EmitDIE(Children[j]);
1906 }
1907
Jim Laskeyda427fa2006-01-27 20:31:25 +00001908 EmitInt8(0); EOL("End Of Children Mark");
Jim Laskey063e7652006-01-17 17:31:53 +00001909 }
1910}
1911
1912/// SizeAndOffsetDie - Compute the size and offset of a DIE.
1913///
Jim Laskeyb8509c52006-03-23 18:07:55 +00001914unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1915 // Get the children.
1916 const std::vector<DIE *> &Children = Die->getChildren();
1917
1918 // If not last sibling and has children then add sibling offset attribute.
1919 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1920
Jim Laskey0420f2a2006-02-22 19:02:11 +00001921 // Record the abbreviation.
1922 Die->Complete(*this);
1923
Jim Laskey063e7652006-01-17 17:31:53 +00001924 // Get the abbreviation for this DIE.
1925 unsigned AbbrevID = Die->getAbbrevID();
1926 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1927
1928 // Set DIE offset
1929 Die->setOffset(Offset);
1930
1931 // Start the size with the size of abbreviation code.
1932 Offset += SizeULEB128(AbbrevID);
1933
1934 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001935 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001936
1937 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001938 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001939 // Size attribute value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001940 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
Jim Laskey063e7652006-01-17 17:31:53 +00001941 }
1942
1943 // Emit the DIE children if any.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001944 if (!Children.empty()) {
1945 assert(Abbrev.getChildrenFlag() == DW_CHILDREN_yes &&
1946 "Children flag not set");
Jim Laskey063e7652006-01-17 17:31:53 +00001947
Jim Laskey52060a02006-01-24 00:49:18 +00001948 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001949 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
Jim Laskey063e7652006-01-17 17:31:53 +00001950 }
1951
1952 // End of children marker.
1953 Offset += sizeof(int8_t);
1954 }
1955
1956 Die->setSize(Offset - Die->getOffset());
1957 return Offset;
1958}
1959
1960/// SizeAndOffsets - Compute the size and offset of all the DIEs.
1961///
1962void DwarfWriter::SizeAndOffsets() {
Jim Laskey063e7652006-01-17 17:31:53 +00001963
1964 // Process each compile unit.
Jim Laskey52060a02006-01-24 00:49:18 +00001965 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00001966 CompileUnit *Unit = CompileUnits[i];
1967 if (Unit->hasContent()) {
1968 // Compute size of compile unit header
1969 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1970 sizeof(int16_t) + // DWARF version number
1971 sizeof(int32_t) + // Offset Into Abbrev. Section
1972 sizeof(int8_t); // Pointer Size (in bytes)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001973 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
Jim Laskeybd761842006-02-27 17:27:12 +00001974 }
Jim Laskey063e7652006-01-17 17:31:53 +00001975 }
1976}
1977
Jim Laskey41886992006-04-07 16:34:46 +00001978/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1979/// frame.
1980void DwarfWriter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
1981 std::vector<MachineMove *> &Moves) {
1982 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1983 MachineMove *Move = Moves[i];
1984 unsigned LabelID = Move->getLabelID();
Jim Laskey66ebf092006-10-23 14:56:37 +00001985
1986 // Throw out move if the label is invalid.
1987 if (LabelID && !DebugInfo->isLabelValid(LabelID)) continue;
1988
Jim Laskey41886992006-04-07 16:34:46 +00001989 const MachineLocation &Dst = Move->getDestination();
1990 const MachineLocation &Src = Move->getSource();
1991
1992 // Advance row if new location.
1993 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
Jim Laskeyce50a162006-08-29 16:24:26 +00001994 EmitInt8(DW_CFA_advance_loc4);
Jim Laskey41886992006-04-07 16:34:46 +00001995 EOL("DW_CFA_advance_loc4");
1996 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
1997 EOL("");
1998
1999 BaseLabelID = LabelID;
2000 BaseLabel = "loc";
2001 }
2002
Jim Laskeyce50a162006-08-29 16:24:26 +00002003 int stackGrowth =
2004 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2005 TargetFrameInfo::StackGrowsUp ?
Jim Laskey563321a2006-09-06 18:34:40 +00002006 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskeyce50a162006-08-29 16:24:26 +00002007
Jim Laskey41886992006-04-07 16:34:46 +00002008 // If advancing cfa.
2009 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
2010 if (!Src.isRegister()) {
2011 if (Src.getRegister() == MachineLocation::VirtualFP) {
Jim Laskeyce50a162006-08-29 16:24:26 +00002012 EmitInt8(DW_CFA_def_cfa_offset);
Jim Laskey41886992006-04-07 16:34:46 +00002013 EOL("DW_CFA_def_cfa_offset");
2014 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00002015 EmitInt8(DW_CFA_def_cfa);
Jim Laskey41886992006-04-07 16:34:46 +00002016 EOL("DW_CFA_def_cfa");
2017
2018 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
2019 EOL("Register");
2020 }
Jim Laskey1069fbd2006-04-10 23:09:19 +00002021
Jim Laskeyce50a162006-08-29 16:24:26 +00002022 int Offset = Src.getOffset() / stackGrowth;
Jim Laskey1069fbd2006-04-10 23:09:19 +00002023
Jim Laskeyce50a162006-08-29 16:24:26 +00002024 EmitULEB128Bytes(Offset);
Jim Laskey41886992006-04-07 16:34:46 +00002025 EOL("Offset");
2026 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00002027 assert(0 && "Machine move no supported yet.");
Jim Laskey41886992006-04-07 16:34:46 +00002028 }
2029 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00002030 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
2031 int Offset = Dst.getOffset() / stackGrowth;
2032
2033 if (Offset < 0) {
2034 EmitInt8(DW_CFA_offset_extended_sf);
2035 EOL("DW_CFA_offset_extended_sf");
2036 EmitULEB128Bytes(Reg);
2037 EOL("Reg");
2038 EmitSLEB128Bytes(Offset);
2039 EOL("Offset");
2040 } else if (Reg < 64) {
2041 EmitInt8(DW_CFA_offset + Reg);
2042 EOL("DW_CFA_offset + Reg");
2043 EmitULEB128Bytes(Offset);
2044 EOL("Offset");
2045 } else {
2046 EmitInt8(DW_CFA_offset_extended);
2047 EOL("DW_CFA_offset_extended");
2048 EmitULEB128Bytes(Reg);
2049 EOL("Reg");
2050 EmitULEB128Bytes(Offset);
2051 EOL("Offset");
2052 }
Jim Laskey41886992006-04-07 16:34:46 +00002053 }
2054 }
2055}
2056
Jim Laskey063e7652006-01-17 17:31:53 +00002057/// EmitDebugInfo - Emit the debug info section.
2058///
2059void DwarfWriter::EmitDebugInfo() const {
2060 // Start debug info section.
Jim Laskey563321a2006-09-06 18:34:40 +00002061 Asm->SwitchToDataSection(TAI->getDwarfInfoSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002062
Jim Laskeybd761842006-02-27 17:27:12 +00002063 // Process each compile unit.
2064 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2065 CompileUnit *Unit = CompileUnits[i];
2066
2067 if (Unit->hasContent()) {
2068 DIE *Die = Unit->getDie();
Jim Laskey0d086af2006-02-27 12:43:29 +00002069 // Emit the compile units header.
Jim Laskeybd761842006-02-27 17:27:12 +00002070 EmitLabel("info_begin", Unit->getID());
Jim Laskey0d086af2006-02-27 12:43:29 +00002071 // Emit size of content not including length itself
Jim Laskeybd761842006-02-27 17:27:12 +00002072 unsigned ContentSize = Die->getSize() +
Jim Laskey0d086af2006-02-27 12:43:29 +00002073 sizeof(int16_t) + // DWARF version number
2074 sizeof(int32_t) + // Offset Into Abbrev. Section
2075 sizeof(int8_t); // Pointer Size (in bytes)
2076
2077 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2078 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskeyf8a01a92006-06-15 20:51:43 +00002079 EmitDifference("abbrev_begin", 0, "section_abbrev", 0);
2080 EOL("Offset Into Abbrev. Section");
Jim Laskey563321a2006-09-06 18:34:40 +00002081 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
Jim Laskey0d086af2006-02-27 12:43:29 +00002082
Jim Laskeybd761842006-02-27 17:27:12 +00002083 EmitDIE(Die);
2084 EmitLabel("info_end", Unit->getID());
Jim Laskey063e7652006-01-17 17:31:53 +00002085 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002086
2087 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002088 }
2089}
2090
2091/// EmitAbbreviations - Emit the abbreviation section.
2092///
2093void DwarfWriter::EmitAbbreviations() const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002094 // Check to see if it is worth the effort.
2095 if (!Abbreviations.empty()) {
2096 // Start the debug abbrev section.
Jim Laskey563321a2006-09-06 18:34:40 +00002097 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002098
Jim Laskeyd18e2892006-01-20 20:34:06 +00002099 EmitLabel("abbrev_begin", 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002100
Jim Laskeyd18e2892006-01-20 20:34:06 +00002101 // For each abbrevation.
2102 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002103 AbbrevID <= NAID; ++AbbrevID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002104 // Get abbreviation data
2105 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey063e7652006-01-17 17:31:53 +00002106
Jim Laskeyd18e2892006-01-20 20:34:06 +00002107 // Emit the abbrevations code (base 1 index.)
2108 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
Jim Laskey063e7652006-01-17 17:31:53 +00002109
Jim Laskeyd18e2892006-01-20 20:34:06 +00002110 // Emit the abbreviations data.
2111 Abbrev.Emit(*this);
Jim Laskey0d086af2006-02-27 12:43:29 +00002112
2113 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002114 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002115
2116 EmitLabel("abbrev_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002117
2118 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002119 }
2120}
2121
2122/// EmitDebugLines - Emit source line information.
2123///
2124void DwarfWriter::EmitDebugLines() const {
2125 // Minimum line delta, thus ranging from -10..(255-10).
2126 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2127 // Maximum line delta, thus ranging from -10..(255-10).
2128 const int MaxLineDelta = 255 + MinLineDelta;
2129
2130 // Start the dwarf line section.
Jim Laskey563321a2006-09-06 18:34:40 +00002131 Asm->SwitchToDataSection(TAI->getDwarfLineSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002132
2133 // Construct the section header.
2134
2135 EmitDifference("line_end", 0, "line_begin", 0);
2136 EOL("Length of Source Line Info");
2137 EmitLabel("line_begin", 0);
2138
Jim Laskeyda427fa2006-01-27 20:31:25 +00002139 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey063e7652006-01-17 17:31:53 +00002140
2141 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
2142 EOL("Prolog Length");
2143 EmitLabel("line_prolog_begin", 0);
2144
Jim Laskeyda427fa2006-01-27 20:31:25 +00002145 EmitInt8(1); EOL("Minimum Instruction Length");
Jim Laskey063e7652006-01-17 17:31:53 +00002146
Jim Laskeyda427fa2006-01-27 20:31:25 +00002147 EmitInt8(1); EOL("Default is_stmt_start flag");
Jim Laskey063e7652006-01-17 17:31:53 +00002148
Jim Laskeyda427fa2006-01-27 20:31:25 +00002149 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00002150
Jim Laskeyda427fa2006-01-27 20:31:25 +00002151 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00002152
Jim Laskeyda427fa2006-01-27 20:31:25 +00002153 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Jim Laskey063e7652006-01-17 17:31:53 +00002154
2155 // Line number standard opcode encodings argument count
Jim Laskeyda427fa2006-01-27 20:31:25 +00002156 EmitInt8(0); EOL("DW_LNS_copy arg count");
2157 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2158 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2159 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2160 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2161 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2162 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2163 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2164 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskey063e7652006-01-17 17:31:53 +00002165
2166 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2167 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
2168
2169 // Emit directories.
2170 for (unsigned DirectoryID = 1, NDID = Directories.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002171 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskey063e7652006-01-17 17:31:53 +00002172 EmitString(Directories[DirectoryID]); EOL("Directory");
2173 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00002174 EmitInt8(0); EOL("End of directories");
Jim Laskey063e7652006-01-17 17:31:53 +00002175
2176 // Emit files.
2177 for (unsigned SourceID = 1, NSID = SourceFiles.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002178 SourceID <= NSID; ++SourceID) {
Jim Laskey063e7652006-01-17 17:31:53 +00002179 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2180 EmitString(SourceFile.getName()); EOL("Source");
2181 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2182 EmitULEB128Bytes(0); EOL("Mod date");
2183 EmitULEB128Bytes(0); EOL("File size");
2184 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00002185 EmitInt8(0); EOL("End of files");
Jim Laskey063e7652006-01-17 17:31:53 +00002186
2187 EmitLabel("line_prolog_end", 0);
2188
Jim Laskey89d67fa2006-06-23 12:51:53 +00002189 // A sequence for each text section.
2190 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2191 // Isolate current sections line info.
Chris Lattner8466b212006-10-17 22:06:46 +00002192 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002193
2194 if (DwarfVerbose) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002195 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00002196 << TAI->getCommentString() << " "
Jim Laskey89d67fa2006-06-23 12:51:53 +00002197 << "Section "
2198 << SectionMap[j + 1].c_str() << "\n";
Jim Laskey0420f2a2006-02-22 19:02:11 +00002199 }
Jim Laskey063e7652006-01-17 17:31:53 +00002200
Jim Laskey89d67fa2006-06-23 12:51:53 +00002201 // Dwarf assumes we start with first line of first source file.
2202 unsigned Source = 1;
2203 unsigned Line = 1;
2204
2205 // Construct rows of the address, source, line, column matrix.
2206 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Chris Lattner8466b212006-10-17 22:06:46 +00002207 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey66ebf092006-10-23 14:56:37 +00002208 unsigned LabelID = LineInfo.getLabelID();
2209
2210 // Throw out line info if label is invalid.
2211 if (!DebugInfo->isLabelValid(LabelID)) continue;
Jim Laskey89d67fa2006-06-23 12:51:53 +00002212
2213 if (DwarfVerbose) {
Chris Lattner8466b212006-10-17 22:06:46 +00002214 unsigned SourceID = LineInfo.getSourceID();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002215 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2216 unsigned DirectoryID = SourceFile.getDirectoryID();
2217 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00002218 << TAI->getCommentString() << " "
Jim Laskey89d67fa2006-06-23 12:51:53 +00002219 << Directories[DirectoryID]
2220 << SourceFile.getName() << ":"
Chris Lattner8466b212006-10-17 22:06:46 +00002221 << LineInfo.getLine() << "\n";
Jim Laskey89d67fa2006-06-23 12:51:53 +00002222 }
2223
2224 // Define the line address.
2225 EmitInt8(0); EOL("Extended Op");
2226 EmitInt8(4 + 1); EOL("Op size");
2227 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskey66ebf092006-10-23 14:56:37 +00002228 EmitReference("loc", LabelID); EOL("Location label");
Jim Laskey89d67fa2006-06-23 12:51:53 +00002229
2230 // If change of source, then switch to the new source.
Chris Lattner8466b212006-10-17 22:06:46 +00002231 if (Source != LineInfo.getSourceID()) {
2232 Source = LineInfo.getSourceID();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002233 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2234 EmitULEB128Bytes(Source); EOL("New Source");
2235 }
2236
2237 // If change of line.
Chris Lattner8466b212006-10-17 22:06:46 +00002238 if (Line != LineInfo.getLine()) {
Jim Laskey89d67fa2006-06-23 12:51:53 +00002239 // Determine offset.
Chris Lattner8466b212006-10-17 22:06:46 +00002240 int Offset = LineInfo.getLine() - Line;
Jim Laskey89d67fa2006-06-23 12:51:53 +00002241 int Delta = Offset - MinLineDelta;
2242
2243 // Update line.
Chris Lattner8466b212006-10-17 22:06:46 +00002244 Line = LineInfo.getLine();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002245
2246 // If delta is small enough and in range...
2247 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2248 // ... then use fast opcode.
2249 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2250 } else {
2251 // ... otherwise use long hand.
2252 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2253 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2254 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2255 }
2256 } else {
2257 // Copy the previous row (different address or source)
2258 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2259 }
2260 }
2261
2262 // Define last address of section.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002263 EmitInt8(0); EOL("Extended Op");
2264 EmitInt8(4 + 1); EOL("Op size");
2265 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskey89d67fa2006-06-23 12:51:53 +00002266 EmitReference("section_end", j + 1); EOL("Section end label");
2267
2268 // Mark end of matrix.
2269 EmitInt8(0); EOL("DW_LNE_end_sequence");
2270 EmitULEB128Bytes(1); O << "\n";
2271 EmitInt8(1); O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002272 }
Jim Laskey063e7652006-01-17 17:31:53 +00002273
2274 EmitLabel("line_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002275
2276 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002277}
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002278
Jim Laskey41886992006-04-07 16:34:46 +00002279/// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002280///
Jim Laskey41886992006-04-07 16:34:46 +00002281void DwarfWriter::EmitInitialDebugFrame() {
Jim Laskey1069fbd2006-04-10 23:09:19 +00002282 int stackGrowth =
2283 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2284 TargetFrameInfo::StackGrowsUp ?
Jim Laskey563321a2006-09-06 18:34:40 +00002285 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskey1069fbd2006-04-10 23:09:19 +00002286
Jim Laskey41886992006-04-07 16:34:46 +00002287 // Start the dwarf frame section.
Jim Laskey563321a2006-09-06 18:34:40 +00002288 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002289
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002290 EmitLabel("frame_common", 0);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002291 EmitDifference("frame_common_end", 0,
2292 "frame_common_begin", 0);
2293 EOL("Length of Common Information Entry");
2294
2295 EmitLabel("frame_common_begin", 0);
2296 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2297 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2298 EmitString(""); EOL("CIE Augmentation");
2299 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
Jim Laskey1069fbd2006-04-10 23:09:19 +00002300 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
Jim Laskey41886992006-04-07 16:34:46 +00002301 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
2302
2303 std::vector<MachineMove *> Moves;
2304 RI->getInitialFrameState(Moves);
2305 EmitFrameMoves(NULL, 0, Moves);
2306 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2307
Jim Laskeyb8509c52006-03-23 18:07:55 +00002308 EmitAlign(2);
2309 EmitLabel("frame_common_end", 0);
2310
2311 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002312}
2313
Jim Laskey41886992006-04-07 16:34:46 +00002314/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2315/// section.
2316void DwarfWriter::EmitFunctionDebugFrame() {
2317 // Start the dwarf frame section.
Jim Laskey563321a2006-09-06 18:34:40 +00002318 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskey41886992006-04-07 16:34:46 +00002319
2320 EmitDifference("frame_end", SubprogramCount,
2321 "frame_begin", SubprogramCount);
2322 EOL("Length of Frame Information Entry");
2323
2324 EmitLabel("frame_begin", SubprogramCount);
2325
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002326 EmitDifference("frame_common", 0, "section_frame", 0);
2327 EOL("FDE CIE offset");
Jim Laskey41886992006-04-07 16:34:46 +00002328
2329 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2330 EmitDifference("func_end", SubprogramCount,
2331 "func_begin", SubprogramCount);
2332 EOL("FDE address range");
2333
2334 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2335
2336 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2337
2338 EmitAlign(2);
2339 EmitLabel("frame_end", SubprogramCount);
2340
2341 O << "\n";
2342}
2343
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002344/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2345///
2346void DwarfWriter::EmitDebugPubNames() {
Jim Laskeybd761842006-02-27 17:27:12 +00002347 // Start the dwarf pubnames section.
Jim Laskey563321a2006-09-06 18:34:40 +00002348 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection(), 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002349
Jim Laskeybd761842006-02-27 17:27:12 +00002350 // Process each compile unit.
2351 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2352 CompileUnit *Unit = CompileUnits[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00002353
Jim Laskeybd761842006-02-27 17:27:12 +00002354 if (Unit->hasContent()) {
2355 EmitDifference("pubnames_end", Unit->getID(),
2356 "pubnames_begin", Unit->getID());
2357 EOL("Length of Public Names Info");
2358
2359 EmitLabel("pubnames_begin", Unit->getID());
2360
2361 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2362
Jim Laskey067ef412006-06-19 15:48:00 +00002363 EmitDifference("info_begin", Unit->getID(), "section_info", 0);
Jim Laskeybd761842006-02-27 17:27:12 +00002364 EOL("Offset of Compilation Unit Info");
Jim Laskeyd18e2892006-01-20 20:34:06 +00002365
Jim Laskeybd761842006-02-27 17:27:12 +00002366 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2367 EOL("Compilation Unit Length");
2368
2369 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2370
2371 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2372 GE = Globals.end();
2373 GI != GE; ++GI) {
2374 const std::string &Name = GI->first;
2375 DIE * Entity = GI->second;
2376
2377 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2378 EmitString(Name); EOL("External Name");
2379 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002380
Jim Laskeybd761842006-02-27 17:27:12 +00002381 EmitInt32(0); EOL("End Mark");
2382 EmitLabel("pubnames_end", Unit->getID());
2383
2384 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002385 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002386 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002387}
2388
2389/// EmitDebugStr - Emit visible names into a debug str section.
2390///
2391void DwarfWriter::EmitDebugStr() {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002392 // Check to see if it is worth the effort.
2393 if (!StringPool.empty()) {
2394 // Start the dwarf str section.
Jim Laskey563321a2006-09-06 18:34:40 +00002395 Asm->SwitchToDataSection(TAI->getDwarfStrSection(), 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002396
Jim Laskeyb8509c52006-03-23 18:07:55 +00002397 // For each of strings in the string pool.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002398 for (unsigned StringID = 1, N = StringPool.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002399 StringID <= N; ++StringID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002400 // Emit a label for reference from debug information entries.
2401 EmitLabel("string", StringID);
2402 // Emit the string itself.
2403 const std::string &String = StringPool[StringID];
2404 EmitString(String); O << "\n";
2405 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002406
2407 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002408 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002409}
2410
2411/// EmitDebugLoc - Emit visible names into a debug loc section.
2412///
2413void DwarfWriter::EmitDebugLoc() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002414 // Start the dwarf loc section.
Jim Laskey563321a2006-09-06 18:34:40 +00002415 Asm->SwitchToDataSection(TAI->getDwarfLocSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002416
2417 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002418}
2419
2420/// EmitDebugARanges - Emit visible names into a debug aranges section.
2421///
2422void DwarfWriter::EmitDebugARanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002423 // Start the dwarf aranges section.
Jim Laskey563321a2006-09-06 18:34:40 +00002424 Asm->SwitchToDataSection(TAI->getDwarfARangesSection(), 0);
Jim Laskeye719a7c2006-01-18 16:54:26 +00002425
2426 // FIXME - Mock up
Jim Laskeybd761842006-02-27 17:27:12 +00002427#if 0
2428 // Process each compile unit.
2429 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2430 CompileUnit *Unit = CompileUnits[i];
2431
2432 if (Unit->hasContent()) {
2433 // Don't include size of length
2434 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2435
2436 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2437
2438 EmitReference("info_begin", Unit->getID());
2439 EOL("Offset of Compilation Unit Info");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002440
Jim Laskey563321a2006-09-06 18:34:40 +00002441 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002442
Jim Laskeybd761842006-02-27 17:27:12 +00002443 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002444
Jim Laskeybd761842006-02-27 17:27:12 +00002445 EmitInt16(0); EOL("Pad (1)");
2446 EmitInt16(0); EOL("Pad (2)");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002447
Jim Laskeybd761842006-02-27 17:27:12 +00002448 // Range 1
2449 EmitReference("text_begin", 0); EOL("Address");
2450 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002451
Jim Laskeybd761842006-02-27 17:27:12 +00002452 EmitInt32(0); EOL("EOM (1)");
2453 EmitInt32(0); EOL("EOM (2)");
2454
2455 O << "\n";
2456 }
2457 }
2458#endif
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002459}
2460
2461/// EmitDebugRanges - Emit visible names into a debug ranges section.
2462///
2463void DwarfWriter::EmitDebugRanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002464 // Start the dwarf ranges section.
Jim Laskey563321a2006-09-06 18:34:40 +00002465 Asm->SwitchToDataSection(TAI->getDwarfRangesSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002466
2467 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002468}
2469
2470/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2471///
2472void DwarfWriter::EmitDebugMacInfo() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002473 // Start the dwarf macinfo section.
Jim Laskey563321a2006-09-06 18:34:40 +00002474 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002475
2476 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002477}
Jim Laskey063e7652006-01-17 17:31:53 +00002478
Jim Laskey52060a02006-01-24 00:49:18 +00002479/// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2480/// header file.
2481void DwarfWriter::ConstructCompileUnitDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002482 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
Jim Laskey6e87c0e2006-01-26 21:22:49 +00002483
2484 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00002485 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
Jim Laskey52060a02006-01-24 00:49:18 +00002486 CompileUnits.push_back(Unit);
2487 }
2488}
2489
2490/// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2491/// variables.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002492void DwarfWriter::ConstructGlobalDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002493 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002494 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00002495
2496 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002497 GlobalVariableDesc *GVD = GlobalVariables[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002498 NewGlobalVariable(GVD);
Jim Laskey52060a02006-01-24 00:49:18 +00002499 }
2500}
2501
Jim Laskey0420f2a2006-02-22 19:02:11 +00002502/// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2503/// subprograms.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002504void DwarfWriter::ConstructSubprogramDIEs() {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002505 std::vector<SubprogramDesc *> Subprograms =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002506 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002507
2508 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2509 SubprogramDesc *SPD = Subprograms[i];
2510 NewSubprogram(SPD);
2511 }
2512}
Jim Laskey52060a02006-01-24 00:49:18 +00002513
Jim Laskey063e7652006-01-17 17:31:53 +00002514//===----------------------------------------------------------------------===//
Jim Laskeyd18e2892006-01-20 20:34:06 +00002515// Main entry points.
Jim Laskey063e7652006-01-17 17:31:53 +00002516//
Jim Laskey52060a02006-01-24 00:49:18 +00002517
Jim Laskeya0f3d172006-09-07 22:06:40 +00002518DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
2519 const TargetAsmInfo *T)
Jim Laskey52060a02006-01-24 00:49:18 +00002520: O(OS)
2521, Asm(A)
Jim Laskey563321a2006-09-06 18:34:40 +00002522, TAI(T)
Jim Laskey41886992006-04-07 16:34:46 +00002523, TD(Asm->TM.getTargetData())
2524, RI(Asm->TM.getRegisterInfo())
Jim Laskeyb8509c52006-03-23 18:07:55 +00002525, M(NULL)
2526, MF(NULL)
Jim Laskey52060a02006-01-24 00:49:18 +00002527, DebugInfo(NULL)
2528, didInitial(false)
Jim Laskey014f98c2006-06-14 11:35:03 +00002529, shouldEmit(false)
Jim Laskeyb8509c52006-03-23 18:07:55 +00002530, SubprogramCount(0)
Jim Laskey52060a02006-01-24 00:49:18 +00002531, CompileUnits()
2532, Abbreviations()
Jim Laskey52060a02006-01-24 00:49:18 +00002533, StringPool()
Jim Laskeybd761842006-02-27 17:27:12 +00002534, DescToUnitMap()
Jim Laskey0420f2a2006-02-22 19:02:11 +00002535, DescToDieMap()
Jim Laskey89d67fa2006-06-23 12:51:53 +00002536, SectionMap()
2537, SectionSourceLines()
Jim Laskey52060a02006-01-24 00:49:18 +00002538{}
2539DwarfWriter::~DwarfWriter() {
2540 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2541 delete CompileUnits[i];
Jim Laskey063e7652006-01-17 17:31:53 +00002542 }
Jim Laskey52060a02006-01-24 00:49:18 +00002543}
Jim Laskey063e7652006-01-17 17:31:53 +00002544
Jim Laskey41886992006-04-07 16:34:46 +00002545/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2546/// created it. Set by the target AsmPrinter.
2547void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
Jim Laskey014f98c2006-06-14 11:35:03 +00002548 // Make sure initial declarations are made.
2549 if (!DebugInfo && DI->hasInfo()) {
2550 DebugInfo = DI;
2551 shouldEmit = true;
2552
2553 // Emit initial sections
2554 EmitInitial();
2555
2556 // Create all the compile unit DIEs.
2557 ConstructCompileUnitDIEs();
2558
2559 // Create DIEs for each of the externally visible global variables.
2560 ConstructGlobalDIEs();
2561
2562 // Create DIEs for each of the externally visible subprograms.
2563 ConstructSubprogramDIEs();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002564
2565 // Prime section data.
Jim Laskey563321a2006-09-06 18:34:40 +00002566 SectionMap.insert(std::string("\t") + TAI->getTextSection());
Jim Laskey014f98c2006-06-14 11:35:03 +00002567 }
Jim Laskey41886992006-04-07 16:34:46 +00002568}
2569
Jim Laskey063e7652006-01-17 17:31:53 +00002570/// BeginModule - Emit all Dwarf sections that should come prior to the content.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002571///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002572void DwarfWriter::BeginModule(Module *M) {
2573 this->M = M;
2574
Jim Laskeyb2efb852006-01-04 22:28:25 +00002575 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002576 EOL("Dwarf Begin Module");
Jim Laskeyb2efb852006-01-04 22:28:25 +00002577}
2578
Jim Laskey063e7652006-01-17 17:31:53 +00002579/// EndModule - Emit all Dwarf sections that should come after the content.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002580///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002581void DwarfWriter::EndModule() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002582 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002583 EOL("Dwarf End Module");
2584
2585 // Standard sections final addresses.
Jim Laskey563321a2006-09-06 18:34:40 +00002586 Asm->SwitchToTextSection(TAI->getTextSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002587 EmitLabel("text_end", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002588 Asm->SwitchToDataSection(TAI->getDataSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002589 EmitLabel("data_end", 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002590
Jim Laskey89d67fa2006-06-23 12:51:53 +00002591 // End text sections.
2592 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2593 Asm->SwitchToTextSection(SectionMap[i].c_str(), 0);
2594 EmitLabel("section_end", i);
2595 }
2596
Jim Laskey063e7652006-01-17 17:31:53 +00002597 // Compute DIE offsets and sizes.
2598 SizeAndOffsets();
2599
2600 // Emit all the DIEs into a debug info section
2601 EmitDebugInfo();
2602
2603 // Corresponding abbreviations into a abbrev section.
2604 EmitAbbreviations();
2605
2606 // Emit source line correspondence into a debug line section.
2607 EmitDebugLines();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002608
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002609 // Emit info into a debug pubnames section.
2610 EmitDebugPubNames();
2611
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002612 // Emit info into a debug str section.
2613 EmitDebugStr();
2614
2615 // Emit info into a debug loc section.
2616 EmitDebugLoc();
2617
2618 // Emit info into a debug aranges section.
2619 EmitDebugARanges();
2620
2621 // Emit info into a debug ranges section.
2622 EmitDebugRanges();
2623
2624 // Emit info into a debug macinfo section.
2625 EmitDebugMacInfo();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002626}
2627
Jim Laskey6b92b8e2006-04-07 20:44:42 +00002628/// BeginFunction - Gather pre-function debug information. Assumes being
2629/// emitted immediately after the function entry point.
Jim Laskey89d67fa2006-06-23 12:51:53 +00002630void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002631 this->MF = MF;
2632
Jim Laskey89d67fa2006-06-23 12:51:53 +00002633 if (!ShouldEmitDwarf()) return;
2634 EOL("Dwarf Begin Function");
2635
2636 // Begin accumulating function debug information.
2637 DebugInfo->BeginFunction(MF);
Jim Laskey41886992006-04-07 16:34:46 +00002638
Jim Laskey89d67fa2006-06-23 12:51:53 +00002639 // Assumes in correct section after the entry point.
2640 EmitLabel("func_begin", ++SubprogramCount);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002641}
2642
Jim Laskeye719a7c2006-01-18 16:54:26 +00002643/// EndFunction - Gather and emit post-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002644///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002645void DwarfWriter::EndFunction() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002646 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002647 EOL("Dwarf End Function");
Jim Laskeyb8509c52006-03-23 18:07:55 +00002648
Jim Laskey89d67fa2006-06-23 12:51:53 +00002649 // Define end label for subprogram.
2650 EmitLabel("func_end", SubprogramCount);
Jim Laskey014f98c2006-06-14 11:35:03 +00002651
Jim Laskey89d67fa2006-06-23 12:51:53 +00002652 // Get function line info.
Chris Lattner8466b212006-10-17 22:06:46 +00002653 const std::vector<SourceLineInfo> &LineInfos = DebugInfo->getSourceLines();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002654
2655 if (!LineInfos.empty()) {
2656 // Get section line info.
2657 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2658 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Chris Lattner8466b212006-10-17 22:06:46 +00002659 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskey89d67fa2006-06-23 12:51:53 +00002660 // Append the function info to section info.
2661 SectionLineInfos.insert(SectionLineInfos.end(),
2662 LineInfos.begin(), LineInfos.end());
Jim Laskey014f98c2006-06-14 11:35:03 +00002663 }
Jim Laskey41886992006-04-07 16:34:46 +00002664
Jim Laskey89d67fa2006-06-23 12:51:53 +00002665 // Construct scopes for subprogram.
2666 ConstructRootScope(DebugInfo->getRootScope());
2667
2668 // Emit function frame information.
2669 EmitFunctionDebugFrame();
2670
2671 // Reset the line numbers for the next function.
Chris Lattner8466b212006-10-17 22:06:46 +00002672 // FIXME: move this to release memory of the debuginfo object.
2673 DebugInfo->ClearLineInfo();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002674
Jim Laskey41886992006-04-07 16:34:46 +00002675 // Clear function debug information.
2676 DebugInfo->EndFunction();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002677}