blob: 9c4726ebc7823dd6ad33fd90a5b515ab028083ca [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
577 void DIEAbbrev::print(std::ostream &O) {
578 O << "Abbreviation @"
Jeff Cohen05ebc8d2006-01-25 17:18:50 +0000579 << std::hex << (intptr_t)this << std::dec
Jim Laskeyd18e2892006-01-20 20:34:06 +0000580 << " "
581 << TagString(Tag)
582 << " "
583 << ChildrenString(ChildrenFlag)
584 << "\n";
585
Jim Laskey52060a02006-01-24 00:49:18 +0000586 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000587 O << " "
588 << AttributeString(Data[i].getAttribute())
589 << " "
590 << FormEncodingString(Data[i].getForm())
591 << "\n";
592 }
593 }
594 void DIEAbbrev::dump() { print(std::cerr); }
595#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 Laskey563321a2006-09-06 18:34:40 +00001163 if (TAI->getNeedsSet()) {
Jim Laskey063e7652006-01-17 17:31:53 +00001164 static unsigned SetCounter = 0;
Jim Laskeyd18e2892006-01-20 20:34:06 +00001165
Jim Laskey063e7652006-01-17 17:31:53 +00001166 O << "\t.set\t";
1167 PrintLabelName("set", SetCounter);
1168 O << ",";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001169 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001170 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001171 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001172 O << "\n";
1173
Jim Laskey563321a2006-09-06 18:34:40 +00001174 if (TAI->getAddressSize() == sizeof(int32_t))
1175 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001176 else
Jim Laskey563321a2006-09-06 18:34:40 +00001177 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001178
1179 PrintLabelName("set", SetCounter);
1180
Jim Laskey52060a02006-01-24 00:49:18 +00001181 ++SetCounter;
Jim Laskey063e7652006-01-17 17:31:53 +00001182 } else {
Jim Laskey563321a2006-09-06 18:34:40 +00001183 if (TAI->getAddressSize() == sizeof(int32_t))
1184 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001185 else
Jim Laskey563321a2006-09-06 18:34:40 +00001186 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001187
Jim Laskeyd18e2892006-01-20 20:34:06 +00001188 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001189 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001190 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001191 }
1192}
1193
Jim Laskeyd18e2892006-01-20 20:34:06 +00001194/// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
Jim Laskey063e7652006-01-17 17:31:53 +00001195///
Jim Laskeyd18e2892006-01-20 20:34:06 +00001196unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
1197 return Abbreviations.insert(*Abbrev);
1198}
1199
1200/// NewString - Add a string to the constant pool and returns a label.
1201///
1202DWLabel DwarfWriter::NewString(const std::string &String) {
1203 unsigned StringID = StringPool.insert(String);
1204 return DWLabel("string", StringID);
1205}
1206
Jim Laskeyb8509c52006-03-23 18:07:55 +00001207/// AddSourceLine - Add location information to specified debug information
1208/// entry.
Jim Laskeyd16f2a72006-06-19 19:49:42 +00001209void DwarfWriter::AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line){
Jim Laskeyb8509c52006-03-23 18:07:55 +00001210 if (File && Line) {
1211 CompileUnit *FileUnit = FindCompileUnit(File);
1212 unsigned FileID = FileUnit->getID();
1213 Die->AddUInt(DW_AT_decl_file, 0, FileID);
1214 Die->AddUInt(DW_AT_decl_line, 0, Line);
1215 }
1216}
1217
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001218/// AddAddress - Add an address attribute to a die based on the location
1219/// provided.
1220void DwarfWriter::AddAddress(DIE *Die, unsigned Attribute,
Jim Laskey41886992006-04-07 16:34:46 +00001221 const MachineLocation &Location) {
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001222 DIEBlock *Block = new DIEBlock();
Jim Laskey3d3d4042006-08-25 19:39:52 +00001223 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1224
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001225 if (Location.isRegister()) {
Jim Laskey3d3d4042006-08-25 19:39:52 +00001226 if (Reg < 32) {
1227 Block->AddUInt(DW_FORM_data1, DW_OP_reg0 + Reg);
1228 } else {
1229 Block->AddUInt(DW_FORM_data1, DW_OP_regx);
1230 Block->AddUInt(DW_FORM_udata, Reg);
1231 }
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001232 } else {
Jim Laskey3d3d4042006-08-25 19:39:52 +00001233 if (Reg < 32) {
1234 Block->AddUInt(DW_FORM_data1, DW_OP_breg0 + Reg);
1235 } else {
1236 Block->AddUInt(DW_FORM_data1, DW_OP_bregx);
1237 Block->AddUInt(DW_FORM_udata, Reg);
1238 }
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001239 Block->AddUInt(DW_FORM_sdata, Location.getOffset());
1240 }
1241 Block->ComputeSize(*this);
1242 Die->AddBlock(Attribute, 0, Block);
1243}
1244
Jim Laskey90c79d72006-03-23 23:02:34 +00001245/// getDieMapSlotFor - Returns the debug information entry map slot for the
1246/// specified debug descriptor.
1247DIE *&DwarfWriter::getDieMapSlotFor(DebugInfoDesc *DD) {
1248 return DescToDieMap[DD];
Jim Laskey0420f2a2006-02-22 19:02:11 +00001249}
Jim Laskey760383e2006-08-21 21:20:18 +00001250
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001251/// NewType - Create a new type DIE.
1252///
Jim Laskey90c79d72006-03-23 23:02:34 +00001253DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit) {
1254 if (!TyDesc) {
1255 // FIXME - Hack for missing types
1256 DIE *Die = new DIE(DW_TAG_base_type);
1257 Die->AddUInt(DW_AT_byte_size, 0, 4);
1258 Die->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1259 Unit->getDie()->AddChild(Die);
1260 return Die;
1261 }
Jim Laskey92ae7402006-03-01 18:20:30 +00001262
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001263 // Check for pre-existence.
Jim Laskey90c79d72006-03-23 23:02:34 +00001264 DIE *&Slot = Unit->getDieMapSlotFor(TyDesc);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001265 if (Slot) return Slot;
1266
1267 // Get core information.
1268 const std::string &Name = TyDesc->getName();
Jim Laskey288fe0f2006-03-01 18:13:05 +00001269 uint64_t Size = TyDesc->getSize() >> 3;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001270
Jim Laskey434b40b2006-02-23 22:37:30 +00001271 DIE *Ty = NULL;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001272
Jim Laskey434b40b2006-02-23 22:37:30 +00001273 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001274 // Fundamental types like int, float, bool
Jim Laskey434b40b2006-02-23 22:37:30 +00001275 Slot = Ty = new DIE(DW_TAG_base_type);
1276 unsigned Encoding = BasicTy->getEncoding();
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001277 Ty->AddUInt(DW_AT_encoding, DW_FORM_data1, Encoding);
Jim Laskey69906002006-02-24 16:46:40 +00001278 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001279 // Create specific DIE.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001280 Slot = Ty = new DIE(DerivedTy->getTag());
Jim Laskey69906002006-02-24 16:46:40 +00001281
1282 // Map to main type, void will not have a type.
1283 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001284 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1285 NewType(Context, FromTy, Unit));
Jim Laskey69906002006-02-24 16:46:40 +00001286 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001287 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
Jim Laskey7089f452006-06-16 13:14:03 +00001288 // Fetch tag
1289 unsigned Tag = CompTy->getTag();
1290
Jim Laskeyf8913f12006-03-01 17:53:02 +00001291 // Create specific DIE.
Jim Laskey7089f452006-06-16 13:14:03 +00001292 Slot = Ty = Tag == DW_TAG_vector_type ? new DIE(DW_TAG_array_type) :
1293 new DIE(Tag);
1294
Jim Laskeyf8913f12006-03-01 17:53:02 +00001295 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1296
Jim Laskey7089f452006-06-16 13:14:03 +00001297 switch (Tag) {
1298 case DW_TAG_vector_type: Ty->AddUInt(DW_AT_GNU_vector, DW_FORM_flag, 1);
1299 // Fall thru
Jim Laskey9c4447a2006-03-01 20:39:36 +00001300 case DW_TAG_array_type: {
Jim Laskeyf8913f12006-03-01 17:53:02 +00001301 // Add element type.
1302 if (TypeDesc *FromTy = CompTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001303 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1304 NewType(Context, FromTy, Unit));
Jim Laskeyf8913f12006-03-01 17:53:02 +00001305 }
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001306
Jim Laskeyf8913f12006-03-01 17:53:02 +00001307 // Don't emit size attribute.
1308 Size = 0;
1309
1310 // Construct an anonymous type for index type.
1311 DIE *IndexTy = new DIE(DW_TAG_base_type);
1312 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1313 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1314 // Add to context.
Jim Laskey92ae7402006-03-01 18:20:30 +00001315 Context->AddChild(IndexTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001316
1317 // Add subranges to array type.
1318 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1319 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1320 int64_t Lo = SRD->getLo();
1321 int64_t Hi = SRD->getHi();
1322 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1323
1324 // If a range is available.
1325 if (Lo != Hi) {
1326 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1327 // Only add low if non-zero.
Jim Laskey6a3eb012006-03-01 23:52:37 +00001328 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1329 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001330 }
1331 Ty->AddChild(Subrange);
1332 }
1333
1334 break;
1335 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001336 case DW_TAG_structure_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +00001337 case DW_TAG_union_type: {
Jim Laskeyf01e5472006-03-03 15:06:57 +00001338 // Add elements to structure type.
1339 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskey760383e2006-08-21 21:20:18 +00001340 DebugInfoDesc *Element = Elements[i];
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001341
Jim Laskey760383e2006-08-21 21:20:18 +00001342 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)) {
1343 // Add field or base class.
Jim Laskey54689c22006-03-09 13:28:47 +00001344
Jim Laskey760383e2006-08-21 21:20:18 +00001345 unsigned Tag = MemberDesc->getTag();
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001346
Jim Laskey760383e2006-08-21 21:20:18 +00001347 // Extract the basic information.
1348 const std::string &Name = MemberDesc->getName();
1349 TypeDesc *MemTy = MemberDesc->getFromType();
1350 uint64_t Size = MemberDesc->getSize();
1351 uint64_t Align = MemberDesc->getAlign();
1352 uint64_t Offset = MemberDesc->getOffset();
1353
1354 // Construct member debug information entry.
1355 DIE *Member = new DIE(Tag);
1356
1357 // Add name if not "".
1358 if (!Name.empty())Member->AddString(DW_AT_name, DW_FORM_string, Name);
1359 // Add location if available.
1360 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1361
1362 // Most of the time the field info is the same as the members.
1363 uint64_t FieldSize = Size;
1364 uint64_t FieldAlign = Align;
1365 uint64_t FieldOffset = Offset;
1366
1367 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1368 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1369 NewType(Context, FromTy, Unit));
1370 FieldSize = FromTy->getSize();
1371 FieldAlign = FromTy->getSize();
1372 }
1373
1374 // Unless we have a bit field.
1375 if (Tag == DW_TAG_member && FieldSize != Size) {
1376 // Construct the alignment mask.
1377 uint64_t AlignMask = ~(FieldAlign - 1);
1378 // Determine the high bit + 1 of the declared size.
1379 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1380 // Work backwards to determine the base offset of the field.
1381 FieldOffset = HiMark - FieldSize;
1382 // Now normalize offset to the field.
1383 Offset -= FieldOffset;
1384
1385 // Maybe we need to work from the other end.
1386 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1387
1388 // Add size and offset.
1389 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1390 Member->AddUInt(DW_AT_bit_size, 0, Size);
1391 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
1392 }
1393
1394 // Add computation for offset.
1395 DIEBlock *Block = new DIEBlock();
1396 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
1397 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
1398 Block->ComputeSize(*this);
1399 Member->AddBlock(DW_AT_data_member_location, 0, Block);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001400
Jim Laskey760383e2006-08-21 21:20:18 +00001401 // Add accessibility (public default unless is base class.
1402 if (MemberDesc->isProtected()) {
1403 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_protected);
1404 } else if (MemberDesc->isPrivate()) {
1405 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_private);
1406 } else if (Tag == DW_TAG_inheritance) {
1407 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_public);
1408 }
1409
1410 Ty->AddChild(Member);
1411 } else if (GlobalVariableDesc *StaticDesc =
1412 dyn_cast<GlobalVariableDesc>(Element)) {
1413 // Add static member.
1414
1415 // Construct member debug information entry.
1416 DIE *Static = new DIE(DW_TAG_variable);
1417
1418 // Add name and mangled name.
1419 const std::string &Name = StaticDesc->getDisplayName();
1420 const std::string &MangledName = StaticDesc->getName();
1421 Static->AddString(DW_AT_name, DW_FORM_string, Name);
1422 Static->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1423 MangledName);
1424
1425 // Add location.
1426 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1427
1428 // Add type.
1429 if (TypeDesc *StaticTy = StaticDesc->getType()) {
1430 Static->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1431 NewType(Context, StaticTy, Unit));
1432 }
1433
1434 // Add flags.
1435 Static->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1436 Static->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1437
1438 Ty->AddChild(Static);
1439 } else if (SubprogramDesc *MethodDesc =
1440 dyn_cast<SubprogramDesc>(Element)) {
1441 // Add member function.
1442
1443 // Construct member debug information entry.
1444 DIE *Method = new DIE(DW_TAG_subprogram);
1445
1446 // Add name and mangled name.
1447 const std::string &Name = MethodDesc->getDisplayName();
1448 const std::string &MangledName = MethodDesc->getName();
1449 bool IsCTor = false;
1450
1451 if (Name.empty()) {
1452 Method->AddString(DW_AT_name, DW_FORM_string, MangledName);
1453 IsCTor = TyDesc->getName() == MangledName;
1454 } else {
1455 Method->AddString(DW_AT_name, DW_FORM_string, Name);
1456 Method->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1457 MangledName);
1458 }
1459
1460 // Add location.
1461 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1462
1463 // Add type.
1464 if (CompositeTypeDesc *MethodTy =
1465 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1466 // Get argument information.
1467 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1468
1469 // If not a ctor.
1470 if (!IsCTor) {
1471 // Add return type.
1472 Method->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1473 NewType(Context, dyn_cast<TypeDesc>(Args[0]),
1474 Unit));
1475 }
1476
1477 // Add arguments.
1478 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1479 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1480 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1481 NewType(Context, cast<TypeDesc>(Args[i]), Unit));
1482 Arg->AddUInt(DW_AT_artificial, DW_FORM_flag, 1);
1483 Method->AddChild(Arg);
1484 }
1485 }
1486
1487 // Add flags.
1488 Method->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1489 Method->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1490
1491 Ty->AddChild(Method);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001492 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001493 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001494 break;
1495 }
Jim Laskey9c4447a2006-03-01 20:39:36 +00001496 case DW_TAG_enumeration_type: {
Jim Laskey6a3eb012006-03-01 23:52:37 +00001497 // Add enumerators to enumeration type.
1498 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1499 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1500 const std::string &Name = ED->getName();
1501 int64_t Value = ED->getValue();
1502 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1503 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1504 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1505 Ty->AddChild(Enumerator);
1506 }
1507
Jim Laskeyf8913f12006-03-01 17:53:02 +00001508 break;
1509 }
Jim Laskey650f6092006-06-20 19:41:06 +00001510 case DW_TAG_subroutine_type: {
1511 // Add prototype flag.
1512 Ty->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
1513 // Add return type.
1514 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
Jim Laskeyd04c1592006-07-13 15:27:42 +00001515 NewType(Context, dyn_cast<TypeDesc>(Elements[0]), Unit));
Jim Laskey650f6092006-06-20 19:41:06 +00001516
1517 // Add arguments.
1518 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1519 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1520 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1521 NewType(Context, cast<TypeDesc>(Elements[i]), Unit));
1522 Ty->AddChild(Arg);
1523 }
1524
1525 break;
1526 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001527 default: break;
1528 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001529 }
Jim Laskey434b40b2006-02-23 22:37:30 +00001530
1531 assert(Ty && "Type not supported yet");
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001532
Jim Laskey69906002006-02-24 16:46:40 +00001533 // Add size if non-zero (derived types don't have a size.)
Jim Laskey434b40b2006-02-23 22:37:30 +00001534 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
Jim Laskey69906002006-02-24 16:46:40 +00001535 // Add name if not anonymous or intermediate type.
Jim Laskey434b40b2006-02-23 22:37:30 +00001536 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001537 // Add source line info if available.
1538 AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
Jim Laskey434b40b2006-02-23 22:37:30 +00001539
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001540 // Add to context owner.
Jim Laskey92ae7402006-03-01 18:20:30 +00001541 Context->AddChild(Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001542
1543 return Slot;
1544}
1545
Jim Laskeyb8509c52006-03-23 18:07:55 +00001546/// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey063e7652006-01-17 17:31:53 +00001547///
Jim Laskeybd761842006-02-27 17:27:12 +00001548CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1549 unsigned ID) {
1550 // Construct debug information entry.
1551 DIE *Die = new DIE(DW_TAG_compile_unit);
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001552 Die->AddDelta (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0),
1553 DWLabel("section_line", 0));
Jim Laskey89d67fa2006-06-23 12:51:53 +00001554// Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1555// Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
Jim Laskeybd761842006-02-27 17:27:12 +00001556 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1557 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1558 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1559 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1560
Jim Laskeyb8509c52006-03-23 18:07:55 +00001561 // Add debug information entry to descriptor map.
Jim Laskey90c79d72006-03-23 23:02:34 +00001562 DIE *&Slot = getDieMapSlotFor(UnitDesc);
1563 Slot = Die;
Jim Laskeybd761842006-02-27 17:27:12 +00001564
1565 // Construct compile unit.
1566 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1567
1568 // Add Unit to compile unit map.
1569 DescToUnitMap[UnitDesc] = Unit;
1570
1571 return Unit;
1572}
Jim Laskey0420f2a2006-02-22 19:02:11 +00001573
Jim Laskeybd761842006-02-27 17:27:12 +00001574/// FindCompileUnit - Get the compile unit for the given descriptor.
1575///
1576CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1577 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1578 assert(Unit && "Missing compile unit.");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001579 return Unit;
Jim Laskey063e7652006-01-17 17:31:53 +00001580}
1581
Jim Laskey0420f2a2006-02-22 19:02:11 +00001582/// NewGlobalVariable - Add a new global variable DIE.
1583///
1584DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001585 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001586 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1587 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey90c79d72006-03-23 23:02:34 +00001588
1589 // Check for pre-existence.
1590 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1591 if (Slot) return Slot;
1592
Jim Laskey0420f2a2006-02-22 19:02:11 +00001593 // Get the global variable itself.
1594 GlobalVariable *GV = GVD->getGlobalVariable();
1595 // Generate the mangled name.
1596 std::string MangledName = Asm->Mang->getValueName(GV);
1597
1598 // Gather the details (simplify add attribute code.)
1599 const std::string &Name = GVD->getName();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001600
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001601 // Get the global's type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001602 DIE *Type = NewType(Unit->getDie(), GVD->getType(), Unit);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001603
1604 // Create the globale variable DIE.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001605 DIE *VariableDie = new DIE(DW_TAG_variable);
1606 VariableDie->AddString (DW_AT_name, DW_FORM_string, Name);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001607 VariableDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1608 VariableDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001609
1610 // Add source line info if available.
1611 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001612
Jim Laskeyb8509c52006-03-23 18:07:55 +00001613 // Add address.
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001614 DIEBlock *Block = new DIEBlock();
1615 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
1616 Block->AddObjectLabel(DW_FORM_udata, MangledName);
1617 Block->ComputeSize(*this);
1618 VariableDie->AddBlock(DW_AT_location, 0, Block);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001619
1620 // Add to map.
1621 Slot = VariableDie;
1622
1623 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001624 Unit->getDie()->AddChild(VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001625
1626 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001627 // FIXME - need to check external flag.
1628 Unit->AddGlobal(Name, VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001629
1630 return VariableDie;
1631}
1632
1633/// NewSubprogram - Add a new subprogram DIE.
1634///
1635DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001636 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001637 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1638 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001639
Jim Laskey90c79d72006-03-23 23:02:34 +00001640 // Check for pre-existence.
1641 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1642 if (Slot) return Slot;
1643
Jim Laskey0420f2a2006-02-22 19:02:11 +00001644 // Gather the details (simplify add attribute code.)
1645 const std::string &Name = SPD->getName();
Jim Laskey90c79d72006-03-23 23:02:34 +00001646 DIE *Type = NewType(Unit->getDie(), SPD->getType(), Unit);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001647 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001648
Jim Laskey8a8e9752006-02-27 20:37:42 +00001649 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001650 SubprogramDie->AddString (DW_AT_name, DW_FORM_string, Name);
Jim Laskey41886992006-04-07 16:34:46 +00001651 if (Type) {
1652 SubprogramDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1653 }
1654 SubprogramDie->AddUInt (DW_AT_external, DW_FORM_flag, IsExternal);
1655 SubprogramDie->AddUInt (DW_AT_prototyped, DW_FORM_flag, 1);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001656
Jim Laskeyb8509c52006-03-23 18:07:55 +00001657 // Add source line info if available.
1658 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1659
Jim Laskey0420f2a2006-02-22 19:02:11 +00001660 // Add to map.
1661 Slot = SubprogramDie;
1662
1663 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001664 Unit->getDie()->AddChild(SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001665
1666 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001667 Unit->AddGlobal(Name, SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001668
1669 return SubprogramDie;
1670}
1671
Jim Laskeyb8509c52006-03-23 18:07:55 +00001672/// NewScopeVariable - Create a new scope variable.
1673///
1674DIE *DwarfWriter::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1675 // Get the descriptor.
1676 VariableDesc *VD = DV->getDesc();
1677
1678 // Translate tag to proper Dwarf tag. The result variable is dropped for now.
1679 unsigned Tag;
1680 switch (VD->getTag()) {
1681 case DW_TAG_return_variable: return NULL;
1682 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1683 case DW_TAG_auto_variable: // fall thru
1684 default: Tag = DW_TAG_variable; break;
1685 }
1686
1687 // Define variable debug information entry.
1688 DIE *VariableDie = new DIE(Tag);
1689 VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
1690
1691 // Add source line info if available.
1692 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1693
1694 // Add variable type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001695 DIE *Type = NewType(Unit->getDie(), VD->getType(), Unit);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001696 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1697
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001698 // Add variable address.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001699 MachineLocation Location;
Jim Laskey41886992006-04-07 16:34:46 +00001700 RI->getLocation(*MF, DV->getFrameIndex(), Location);
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001701 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001702
1703 return VariableDie;
1704}
1705
1706/// ConstructScope - Construct the components of a scope.
1707///
1708void DwarfWriter::ConstructScope(DebugScope *ParentScope,
1709 DIE *ParentDie, CompileUnit *Unit) {
1710 // Add variables to scope.
1711 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1712 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1713 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1714 if (VariableDie) ParentDie->AddChild(VariableDie);
1715 }
1716
1717 // Add nested scopes.
1718 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1719 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1720 // Define the Scope debug information entry.
1721 DebugScope *Scope = Scopes[j];
1722 // FIXME - Ignore inlined functions for the time being.
Jim Laskeyd16f2a72006-06-19 19:49:42 +00001723 if (!Scope->getParent()) continue;
Jim Laskeyb8509c52006-03-23 18:07:55 +00001724
1725 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1726
1727 // Add the scope bounds.
1728 if (unsigned StartID = Scope->getStartLabelID()) {
1729 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1730 DWLabel("loc", StartID));
1731 } else {
1732 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1733 DWLabel("func_begin", SubprogramCount));
1734 }
1735 if (unsigned EndID = Scope->getEndLabelID()) {
1736 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1737 DWLabel("loc", EndID));
1738 } else {
1739 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1740 DWLabel("func_end", SubprogramCount));
1741 }
1742
1743 // Add the scope contents.
1744 ConstructScope(Scope, ScopeDie, Unit);
1745 ParentDie->AddChild(ScopeDie);
1746 }
1747}
1748
1749/// ConstructRootScope - Construct the scope for the subprogram.
1750///
1751void DwarfWriter::ConstructRootScope(DebugScope *RootScope) {
1752 // Exit if there is no root scope.
1753 if (!RootScope) return;
1754
1755 // Get the subprogram debug information entry.
1756 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Jim Laskey90c79d72006-03-23 23:02:34 +00001757
1758 // Get the compile unit context.
1759 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey41886992006-04-07 16:34:46 +00001760 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1761
Jim Laskey90c79d72006-03-23 23:02:34 +00001762 // Get the subprogram die.
1763 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001764 assert(SPDie && "Missing subprogram descriptor");
1765
1766 // Add the function bounds.
Jim Laskey6b92b8e2006-04-07 20:44:42 +00001767 SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1768 DWLabel("func_begin", SubprogramCount));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001769 SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1770 DWLabel("func_end", SubprogramCount));
Jim Laskey41886992006-04-07 16:34:46 +00001771 MachineLocation Location(RI->getFrameRegister(*MF));
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001772 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001773
Jim Laskeyb8509c52006-03-23 18:07:55 +00001774 ConstructScope(RootScope, SPDie, Unit);
1775}
1776
Jim Laskey063e7652006-01-17 17:31:53 +00001777/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1778/// tools to recognize the object file contains Dwarf information.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001779///
Jim Laskey014f98c2006-06-14 11:35:03 +00001780void DwarfWriter::EmitInitial() {
1781 // Check to see if we already emitted intial headers.
1782 if (didInitial) return;
1783 didInitial = true;
1784
Jim Laskey063e7652006-01-17 17:31:53 +00001785 // Dwarf sections base addresses.
Jim Laskey563321a2006-09-06 18:34:40 +00001786 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001787 EmitLabel("section_frame", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001788 Asm->SwitchToDataSection(TAI->getDwarfInfoSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001789 EmitLabel("section_info", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001790 EmitLabel("info", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001791 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001792 EmitLabel("section_abbrev", 0);
1793 EmitLabel("abbrev", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001794 Asm->SwitchToDataSection(TAI->getDwarfARangesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001795 EmitLabel("section_aranges", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001796 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001797 EmitLabel("section_macinfo", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001798 Asm->SwitchToDataSection(TAI->getDwarfLineSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001799 EmitLabel("section_line", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001800 EmitLabel("line", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001801 Asm->SwitchToDataSection(TAI->getDwarfLocSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001802 EmitLabel("section_loc", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001803 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001804 EmitLabel("section_pubnames", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001805 Asm->SwitchToDataSection(TAI->getDwarfStrSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001806 EmitLabel("section_str", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001807 Asm->SwitchToDataSection(TAI->getDwarfRangesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001808 EmitLabel("section_ranges", 0);
1809
Jim Laskey563321a2006-09-06 18:34:40 +00001810 Asm->SwitchToTextSection(TAI->getTextSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001811 EmitLabel("text_begin", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00001812 Asm->SwitchToDataSection(TAI->getDataSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001813 EmitLabel("data_begin", 0);
Jim Laskey89d67fa2006-06-23 12:51:53 +00001814
Jim Laskey014f98c2006-06-14 11:35:03 +00001815 // Emit common frame information.
1816 EmitInitialDebugFrame();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001817}
1818
Jim Laskey063e7652006-01-17 17:31:53 +00001819/// EmitDIE - Recusively Emits a debug information entry.
1820///
1821void DwarfWriter::EmitDIE(DIE *Die) const {
1822 // Get the abbreviation for this DIE.
1823 unsigned AbbrevID = Die->getAbbrevID();
1824 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey0d086af2006-02-27 12:43:29 +00001825
1826 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001827
1828 // Emit the code (index) for the abbreviation.
1829 EmitULEB128Bytes(AbbrevID);
1830 EOL(std::string("Abbrev [" +
1831 utostr(AbbrevID) +
Jim Laskey0d086af2006-02-27 12:43:29 +00001832 "] 0x" + utohexstr(Die->getOffset()) +
1833 ":0x" + utohexstr(Die->getSize()) + " " +
1834 TagString(Abbrev.getTag())));
Jim Laskey063e7652006-01-17 17:31:53 +00001835
1836 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001837 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001838
1839 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001840 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001841 unsigned Attr = AbbrevData[i].getAttribute();
1842 unsigned Form = AbbrevData[i].getForm();
Jim Laskey063e7652006-01-17 17:31:53 +00001843 assert(Form && "Too many attributes for DIE (check abbreviation)");
1844
1845 switch (Attr) {
1846 case DW_AT_sibling: {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001847 EmitInt32(Die->SiblingOffset());
Jim Laskey063e7652006-01-17 17:31:53 +00001848 break;
1849 }
1850 default: {
1851 // Emit an attribute using the defined form.
1852 Values[i]->EmitValue(*this, Form);
1853 break;
1854 }
1855 }
1856
1857 EOL(AttributeString(Attr));
1858 }
1859
1860 // Emit the DIE children if any.
1861 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1862 const std::vector<DIE *> &Children = Die->getChildren();
1863
Jim Laskey52060a02006-01-24 00:49:18 +00001864 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskey063e7652006-01-17 17:31:53 +00001865 EmitDIE(Children[j]);
1866 }
1867
Jim Laskeyda427fa2006-01-27 20:31:25 +00001868 EmitInt8(0); EOL("End Of Children Mark");
Jim Laskey063e7652006-01-17 17:31:53 +00001869 }
1870}
1871
1872/// SizeAndOffsetDie - Compute the size and offset of a DIE.
1873///
Jim Laskeyb8509c52006-03-23 18:07:55 +00001874unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1875 // Get the children.
1876 const std::vector<DIE *> &Children = Die->getChildren();
1877
1878 // If not last sibling and has children then add sibling offset attribute.
1879 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1880
Jim Laskey0420f2a2006-02-22 19:02:11 +00001881 // Record the abbreviation.
1882 Die->Complete(*this);
1883
Jim Laskey063e7652006-01-17 17:31:53 +00001884 // Get the abbreviation for this DIE.
1885 unsigned AbbrevID = Die->getAbbrevID();
1886 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1887
1888 // Set DIE offset
1889 Die->setOffset(Offset);
1890
1891 // Start the size with the size of abbreviation code.
1892 Offset += SizeULEB128(AbbrevID);
1893
1894 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001895 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001896
1897 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001898 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001899 // Size attribute value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001900 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
Jim Laskey063e7652006-01-17 17:31:53 +00001901 }
1902
1903 // Emit the DIE children if any.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001904 if (!Children.empty()) {
1905 assert(Abbrev.getChildrenFlag() == DW_CHILDREN_yes &&
1906 "Children flag not set");
Jim Laskey063e7652006-01-17 17:31:53 +00001907
Jim Laskey52060a02006-01-24 00:49:18 +00001908 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001909 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
Jim Laskey063e7652006-01-17 17:31:53 +00001910 }
1911
1912 // End of children marker.
1913 Offset += sizeof(int8_t);
1914 }
1915
1916 Die->setSize(Offset - Die->getOffset());
1917 return Offset;
1918}
1919
1920/// SizeAndOffsets - Compute the size and offset of all the DIEs.
1921///
1922void DwarfWriter::SizeAndOffsets() {
Jim Laskey063e7652006-01-17 17:31:53 +00001923
1924 // Process each compile unit.
Jim Laskey52060a02006-01-24 00:49:18 +00001925 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00001926 CompileUnit *Unit = CompileUnits[i];
1927 if (Unit->hasContent()) {
1928 // Compute size of compile unit header
1929 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1930 sizeof(int16_t) + // DWARF version number
1931 sizeof(int32_t) + // Offset Into Abbrev. Section
1932 sizeof(int8_t); // Pointer Size (in bytes)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001933 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
Jim Laskeybd761842006-02-27 17:27:12 +00001934 }
Jim Laskey063e7652006-01-17 17:31:53 +00001935 }
1936}
1937
Jim Laskey41886992006-04-07 16:34:46 +00001938/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1939/// frame.
1940void DwarfWriter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
1941 std::vector<MachineMove *> &Moves) {
1942 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1943 MachineMove *Move = Moves[i];
1944 unsigned LabelID = Move->getLabelID();
1945 const MachineLocation &Dst = Move->getDestination();
1946 const MachineLocation &Src = Move->getSource();
1947
1948 // Advance row if new location.
1949 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
Jim Laskeyce50a162006-08-29 16:24:26 +00001950 EmitInt8(DW_CFA_advance_loc4);
Jim Laskey41886992006-04-07 16:34:46 +00001951 EOL("DW_CFA_advance_loc4");
1952 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
1953 EOL("");
1954
1955 BaseLabelID = LabelID;
1956 BaseLabel = "loc";
1957 }
1958
Jim Laskeyce50a162006-08-29 16:24:26 +00001959 int stackGrowth =
1960 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1961 TargetFrameInfo::StackGrowsUp ?
Jim Laskey563321a2006-09-06 18:34:40 +00001962 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskeyce50a162006-08-29 16:24:26 +00001963
Jim Laskey41886992006-04-07 16:34:46 +00001964 // If advancing cfa.
1965 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
1966 if (!Src.isRegister()) {
1967 if (Src.getRegister() == MachineLocation::VirtualFP) {
Jim Laskeyce50a162006-08-29 16:24:26 +00001968 EmitInt8(DW_CFA_def_cfa_offset);
Jim Laskey41886992006-04-07 16:34:46 +00001969 EOL("DW_CFA_def_cfa_offset");
1970 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00001971 EmitInt8(DW_CFA_def_cfa);
Jim Laskey41886992006-04-07 16:34:46 +00001972 EOL("DW_CFA_def_cfa");
1973
1974 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
1975 EOL("Register");
1976 }
Jim Laskey1069fbd2006-04-10 23:09:19 +00001977
Jim Laskeyce50a162006-08-29 16:24:26 +00001978 int Offset = Src.getOffset() / stackGrowth;
Jim Laskey1069fbd2006-04-10 23:09:19 +00001979
Jim Laskeyce50a162006-08-29 16:24:26 +00001980 EmitULEB128Bytes(Offset);
Jim Laskey41886992006-04-07 16:34:46 +00001981 EOL("Offset");
1982 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00001983 assert(0 && "Machine move no supported yet.");
Jim Laskey41886992006-04-07 16:34:46 +00001984 }
1985 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00001986 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
1987 int Offset = Dst.getOffset() / stackGrowth;
1988
1989 if (Offset < 0) {
1990 EmitInt8(DW_CFA_offset_extended_sf);
1991 EOL("DW_CFA_offset_extended_sf");
1992 EmitULEB128Bytes(Reg);
1993 EOL("Reg");
1994 EmitSLEB128Bytes(Offset);
1995 EOL("Offset");
1996 } else if (Reg < 64) {
1997 EmitInt8(DW_CFA_offset + Reg);
1998 EOL("DW_CFA_offset + Reg");
1999 EmitULEB128Bytes(Offset);
2000 EOL("Offset");
2001 } else {
2002 EmitInt8(DW_CFA_offset_extended);
2003 EOL("DW_CFA_offset_extended");
2004 EmitULEB128Bytes(Reg);
2005 EOL("Reg");
2006 EmitULEB128Bytes(Offset);
2007 EOL("Offset");
2008 }
Jim Laskey41886992006-04-07 16:34:46 +00002009 }
2010 }
2011}
2012
Jim Laskey063e7652006-01-17 17:31:53 +00002013/// EmitDebugInfo - Emit the debug info section.
2014///
2015void DwarfWriter::EmitDebugInfo() const {
2016 // Start debug info section.
Jim Laskey563321a2006-09-06 18:34:40 +00002017 Asm->SwitchToDataSection(TAI->getDwarfInfoSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002018
Jim Laskeybd761842006-02-27 17:27:12 +00002019 // Process each compile unit.
2020 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2021 CompileUnit *Unit = CompileUnits[i];
2022
2023 if (Unit->hasContent()) {
2024 DIE *Die = Unit->getDie();
Jim Laskey0d086af2006-02-27 12:43:29 +00002025 // Emit the compile units header.
Jim Laskeybd761842006-02-27 17:27:12 +00002026 EmitLabel("info_begin", Unit->getID());
Jim Laskey0d086af2006-02-27 12:43:29 +00002027 // Emit size of content not including length itself
Jim Laskeybd761842006-02-27 17:27:12 +00002028 unsigned ContentSize = Die->getSize() +
Jim Laskey0d086af2006-02-27 12:43:29 +00002029 sizeof(int16_t) + // DWARF version number
2030 sizeof(int32_t) + // Offset Into Abbrev. Section
2031 sizeof(int8_t); // Pointer Size (in bytes)
2032
2033 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2034 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskeyf8a01a92006-06-15 20:51:43 +00002035 EmitDifference("abbrev_begin", 0, "section_abbrev", 0);
2036 EOL("Offset Into Abbrev. Section");
Jim Laskey563321a2006-09-06 18:34:40 +00002037 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
Jim Laskey0d086af2006-02-27 12:43:29 +00002038
Jim Laskeybd761842006-02-27 17:27:12 +00002039 EmitDIE(Die);
2040 EmitLabel("info_end", Unit->getID());
Jim Laskey063e7652006-01-17 17:31:53 +00002041 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002042
2043 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002044 }
2045}
2046
2047/// EmitAbbreviations - Emit the abbreviation section.
2048///
2049void DwarfWriter::EmitAbbreviations() const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002050 // Check to see if it is worth the effort.
2051 if (!Abbreviations.empty()) {
2052 // Start the debug abbrev section.
Jim Laskey563321a2006-09-06 18:34:40 +00002053 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002054
Jim Laskeyd18e2892006-01-20 20:34:06 +00002055 EmitLabel("abbrev_begin", 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002056
Jim Laskeyd18e2892006-01-20 20:34:06 +00002057 // For each abbrevation.
2058 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002059 AbbrevID <= NAID; ++AbbrevID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002060 // Get abbreviation data
2061 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey063e7652006-01-17 17:31:53 +00002062
Jim Laskeyd18e2892006-01-20 20:34:06 +00002063 // Emit the abbrevations code (base 1 index.)
2064 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
Jim Laskey063e7652006-01-17 17:31:53 +00002065
Jim Laskeyd18e2892006-01-20 20:34:06 +00002066 // Emit the abbreviations data.
2067 Abbrev.Emit(*this);
Jim Laskey0d086af2006-02-27 12:43:29 +00002068
2069 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002070 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002071
2072 EmitLabel("abbrev_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002073
2074 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002075 }
2076}
2077
2078/// EmitDebugLines - Emit source line information.
2079///
2080void DwarfWriter::EmitDebugLines() const {
2081 // Minimum line delta, thus ranging from -10..(255-10).
2082 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2083 // Maximum line delta, thus ranging from -10..(255-10).
2084 const int MaxLineDelta = 255 + MinLineDelta;
2085
2086 // Start the dwarf line section.
Jim Laskey563321a2006-09-06 18:34:40 +00002087 Asm->SwitchToDataSection(TAI->getDwarfLineSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002088
2089 // Construct the section header.
2090
2091 EmitDifference("line_end", 0, "line_begin", 0);
2092 EOL("Length of Source Line Info");
2093 EmitLabel("line_begin", 0);
2094
Jim Laskeyda427fa2006-01-27 20:31:25 +00002095 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey063e7652006-01-17 17:31:53 +00002096
2097 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
2098 EOL("Prolog Length");
2099 EmitLabel("line_prolog_begin", 0);
2100
Jim Laskeyda427fa2006-01-27 20:31:25 +00002101 EmitInt8(1); EOL("Minimum Instruction Length");
Jim Laskey063e7652006-01-17 17:31:53 +00002102
Jim Laskeyda427fa2006-01-27 20:31:25 +00002103 EmitInt8(1); EOL("Default is_stmt_start flag");
Jim Laskey063e7652006-01-17 17:31:53 +00002104
Jim Laskeyda427fa2006-01-27 20:31:25 +00002105 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00002106
Jim Laskeyda427fa2006-01-27 20:31:25 +00002107 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00002108
Jim Laskeyda427fa2006-01-27 20:31:25 +00002109 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Jim Laskey063e7652006-01-17 17:31:53 +00002110
2111 // Line number standard opcode encodings argument count
Jim Laskeyda427fa2006-01-27 20:31:25 +00002112 EmitInt8(0); EOL("DW_LNS_copy arg count");
2113 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2114 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2115 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2116 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2117 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2118 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2119 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2120 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskey063e7652006-01-17 17:31:53 +00002121
2122 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2123 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
2124
2125 // Emit directories.
2126 for (unsigned DirectoryID = 1, NDID = Directories.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002127 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskey063e7652006-01-17 17:31:53 +00002128 EmitString(Directories[DirectoryID]); EOL("Directory");
2129 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00002130 EmitInt8(0); EOL("End of directories");
Jim Laskey063e7652006-01-17 17:31:53 +00002131
2132 // Emit files.
2133 for (unsigned SourceID = 1, NSID = SourceFiles.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002134 SourceID <= NSID; ++SourceID) {
Jim Laskey063e7652006-01-17 17:31:53 +00002135 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2136 EmitString(SourceFile.getName()); EOL("Source");
2137 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2138 EmitULEB128Bytes(0); EOL("Mod date");
2139 EmitULEB128Bytes(0); EOL("File size");
2140 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00002141 EmitInt8(0); EOL("End of files");
Jim Laskey063e7652006-01-17 17:31:53 +00002142
2143 EmitLabel("line_prolog_end", 0);
2144
Jim Laskey89d67fa2006-06-23 12:51:53 +00002145 // A sequence for each text section.
2146 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2147 // Isolate current sections line info.
2148 const std::vector<SourceLineInfo *> &LineInfos = SectionSourceLines[j];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002149
2150 if (DwarfVerbose) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002151 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00002152 << TAI->getCommentString() << " "
Jim Laskey89d67fa2006-06-23 12:51:53 +00002153 << "Section "
2154 << SectionMap[j + 1].c_str() << "\n";
Jim Laskey0420f2a2006-02-22 19:02:11 +00002155 }
Jim Laskey063e7652006-01-17 17:31:53 +00002156
Jim Laskey89d67fa2006-06-23 12:51:53 +00002157 // Dwarf assumes we start with first line of first source file.
2158 unsigned Source = 1;
2159 unsigned Line = 1;
2160
2161 // Construct rows of the address, source, line, column matrix.
2162 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2163 SourceLineInfo *LineInfo = LineInfos[i];
2164
2165 if (DwarfVerbose) {
2166 unsigned SourceID = LineInfo->getSourceID();
2167 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2168 unsigned DirectoryID = SourceFile.getDirectoryID();
2169 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00002170 << TAI->getCommentString() << " "
Jim Laskey89d67fa2006-06-23 12:51:53 +00002171 << Directories[DirectoryID]
2172 << SourceFile.getName() << ":"
2173 << LineInfo->getLine() << "\n";
2174 }
2175
2176 // Define the line address.
2177 EmitInt8(0); EOL("Extended Op");
2178 EmitInt8(4 + 1); EOL("Op size");
2179 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2180 EmitReference("loc", LineInfo->getLabelID()); EOL("Location label");
2181
2182 // If change of source, then switch to the new source.
2183 if (Source != LineInfo->getSourceID()) {
2184 Source = LineInfo->getSourceID();
2185 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2186 EmitULEB128Bytes(Source); EOL("New Source");
2187 }
2188
2189 // If change of line.
2190 if (Line != LineInfo->getLine()) {
2191 // Determine offset.
2192 int Offset = LineInfo->getLine() - Line;
2193 int Delta = Offset - MinLineDelta;
2194
2195 // Update line.
2196 Line = LineInfo->getLine();
2197
2198 // If delta is small enough and in range...
2199 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2200 // ... then use fast opcode.
2201 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2202 } else {
2203 // ... otherwise use long hand.
2204 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2205 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2206 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2207 }
2208 } else {
2209 // Copy the previous row (different address or source)
2210 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2211 }
2212 }
2213
2214 // Define last address of section.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002215 EmitInt8(0); EOL("Extended Op");
2216 EmitInt8(4 + 1); EOL("Op size");
2217 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskey89d67fa2006-06-23 12:51:53 +00002218 EmitReference("section_end", j + 1); EOL("Section end label");
2219
2220 // Mark end of matrix.
2221 EmitInt8(0); EOL("DW_LNE_end_sequence");
2222 EmitULEB128Bytes(1); O << "\n";
2223 EmitInt8(1); O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002224 }
Jim Laskey063e7652006-01-17 17:31:53 +00002225
2226 EmitLabel("line_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002227
2228 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002229}
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002230
Jim Laskey41886992006-04-07 16:34:46 +00002231/// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002232///
Jim Laskey41886992006-04-07 16:34:46 +00002233void DwarfWriter::EmitInitialDebugFrame() {
Jim Laskey1069fbd2006-04-10 23:09:19 +00002234 int stackGrowth =
2235 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2236 TargetFrameInfo::StackGrowsUp ?
Jim Laskey563321a2006-09-06 18:34:40 +00002237 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskey1069fbd2006-04-10 23:09:19 +00002238
Jim Laskey41886992006-04-07 16:34:46 +00002239 // Start the dwarf frame section.
Jim Laskey563321a2006-09-06 18:34:40 +00002240 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002241
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002242 EmitLabel("frame_common", 0);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002243 EmitDifference("frame_common_end", 0,
2244 "frame_common_begin", 0);
2245 EOL("Length of Common Information Entry");
2246
2247 EmitLabel("frame_common_begin", 0);
2248 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2249 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2250 EmitString(""); EOL("CIE Augmentation");
2251 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
Jim Laskey1069fbd2006-04-10 23:09:19 +00002252 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
Jim Laskey41886992006-04-07 16:34:46 +00002253 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
2254
2255 std::vector<MachineMove *> Moves;
2256 RI->getInitialFrameState(Moves);
2257 EmitFrameMoves(NULL, 0, Moves);
2258 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2259
Jim Laskeyb8509c52006-03-23 18:07:55 +00002260 EmitAlign(2);
2261 EmitLabel("frame_common_end", 0);
2262
2263 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002264}
2265
Jim Laskey41886992006-04-07 16:34:46 +00002266/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2267/// section.
2268void DwarfWriter::EmitFunctionDebugFrame() {
2269 // Start the dwarf frame section.
Jim Laskey563321a2006-09-06 18:34:40 +00002270 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskey41886992006-04-07 16:34:46 +00002271
2272 EmitDifference("frame_end", SubprogramCount,
2273 "frame_begin", SubprogramCount);
2274 EOL("Length of Frame Information Entry");
2275
2276 EmitLabel("frame_begin", SubprogramCount);
2277
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002278 EmitDifference("frame_common", 0, "section_frame", 0);
2279 EOL("FDE CIE offset");
Jim Laskey41886992006-04-07 16:34:46 +00002280
2281 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2282 EmitDifference("func_end", SubprogramCount,
2283 "func_begin", SubprogramCount);
2284 EOL("FDE address range");
2285
2286 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2287
2288 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2289
2290 EmitAlign(2);
2291 EmitLabel("frame_end", SubprogramCount);
2292
2293 O << "\n";
2294}
2295
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002296/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2297///
2298void DwarfWriter::EmitDebugPubNames() {
Jim Laskeybd761842006-02-27 17:27:12 +00002299 // Start the dwarf pubnames section.
Jim Laskey563321a2006-09-06 18:34:40 +00002300 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection(), 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002301
Jim Laskeybd761842006-02-27 17:27:12 +00002302 // Process each compile unit.
2303 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2304 CompileUnit *Unit = CompileUnits[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00002305
Jim Laskeybd761842006-02-27 17:27:12 +00002306 if (Unit->hasContent()) {
2307 EmitDifference("pubnames_end", Unit->getID(),
2308 "pubnames_begin", Unit->getID());
2309 EOL("Length of Public Names Info");
2310
2311 EmitLabel("pubnames_begin", Unit->getID());
2312
2313 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2314
Jim Laskey067ef412006-06-19 15:48:00 +00002315 EmitDifference("info_begin", Unit->getID(), "section_info", 0);
Jim Laskeybd761842006-02-27 17:27:12 +00002316 EOL("Offset of Compilation Unit Info");
Jim Laskeyd18e2892006-01-20 20:34:06 +00002317
Jim Laskeybd761842006-02-27 17:27:12 +00002318 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2319 EOL("Compilation Unit Length");
2320
2321 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2322
2323 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2324 GE = Globals.end();
2325 GI != GE; ++GI) {
2326 const std::string &Name = GI->first;
2327 DIE * Entity = GI->second;
2328
2329 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2330 EmitString(Name); EOL("External Name");
2331 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002332
Jim Laskeybd761842006-02-27 17:27:12 +00002333 EmitInt32(0); EOL("End Mark");
2334 EmitLabel("pubnames_end", Unit->getID());
2335
2336 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002337 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002338 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002339}
2340
2341/// EmitDebugStr - Emit visible names into a debug str section.
2342///
2343void DwarfWriter::EmitDebugStr() {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002344 // Check to see if it is worth the effort.
2345 if (!StringPool.empty()) {
2346 // Start the dwarf str section.
Jim Laskey563321a2006-09-06 18:34:40 +00002347 Asm->SwitchToDataSection(TAI->getDwarfStrSection(), 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002348
Jim Laskeyb8509c52006-03-23 18:07:55 +00002349 // For each of strings in the string pool.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002350 for (unsigned StringID = 1, N = StringPool.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002351 StringID <= N; ++StringID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002352 // Emit a label for reference from debug information entries.
2353 EmitLabel("string", StringID);
2354 // Emit the string itself.
2355 const std::string &String = StringPool[StringID];
2356 EmitString(String); O << "\n";
2357 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002358
2359 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002360 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002361}
2362
2363/// EmitDebugLoc - Emit visible names into a debug loc section.
2364///
2365void DwarfWriter::EmitDebugLoc() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002366 // Start the dwarf loc section.
Jim Laskey563321a2006-09-06 18:34:40 +00002367 Asm->SwitchToDataSection(TAI->getDwarfLocSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002368
2369 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002370}
2371
2372/// EmitDebugARanges - Emit visible names into a debug aranges section.
2373///
2374void DwarfWriter::EmitDebugARanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002375 // Start the dwarf aranges section.
Jim Laskey563321a2006-09-06 18:34:40 +00002376 Asm->SwitchToDataSection(TAI->getDwarfARangesSection(), 0);
Jim Laskeye719a7c2006-01-18 16:54:26 +00002377
2378 // FIXME - Mock up
Jim Laskeybd761842006-02-27 17:27:12 +00002379#if 0
2380 // Process each compile unit.
2381 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2382 CompileUnit *Unit = CompileUnits[i];
2383
2384 if (Unit->hasContent()) {
2385 // Don't include size of length
2386 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2387
2388 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2389
2390 EmitReference("info_begin", Unit->getID());
2391 EOL("Offset of Compilation Unit Info");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002392
Jim Laskey563321a2006-09-06 18:34:40 +00002393 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002394
Jim Laskeybd761842006-02-27 17:27:12 +00002395 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002396
Jim Laskeybd761842006-02-27 17:27:12 +00002397 EmitInt16(0); EOL("Pad (1)");
2398 EmitInt16(0); EOL("Pad (2)");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002399
Jim Laskeybd761842006-02-27 17:27:12 +00002400 // Range 1
2401 EmitReference("text_begin", 0); EOL("Address");
2402 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002403
Jim Laskeybd761842006-02-27 17:27:12 +00002404 EmitInt32(0); EOL("EOM (1)");
2405 EmitInt32(0); EOL("EOM (2)");
2406
2407 O << "\n";
2408 }
2409 }
2410#endif
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002411}
2412
2413/// EmitDebugRanges - Emit visible names into a debug ranges section.
2414///
2415void DwarfWriter::EmitDebugRanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002416 // Start the dwarf ranges section.
Jim Laskey563321a2006-09-06 18:34:40 +00002417 Asm->SwitchToDataSection(TAI->getDwarfRangesSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002418
2419 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002420}
2421
2422/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2423///
2424void DwarfWriter::EmitDebugMacInfo() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002425 // Start the dwarf macinfo section.
Jim Laskey563321a2006-09-06 18:34:40 +00002426 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002427
2428 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002429}
Jim Laskey063e7652006-01-17 17:31:53 +00002430
Jim Laskey52060a02006-01-24 00:49:18 +00002431/// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2432/// header file.
2433void DwarfWriter::ConstructCompileUnitDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002434 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
Jim Laskey6e87c0e2006-01-26 21:22:49 +00002435
2436 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00002437 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
Jim Laskey52060a02006-01-24 00:49:18 +00002438 CompileUnits.push_back(Unit);
2439 }
2440}
2441
2442/// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2443/// variables.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002444void DwarfWriter::ConstructGlobalDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002445 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002446 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00002447
2448 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002449 GlobalVariableDesc *GVD = GlobalVariables[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002450 NewGlobalVariable(GVD);
Jim Laskey52060a02006-01-24 00:49:18 +00002451 }
2452}
2453
Jim Laskey0420f2a2006-02-22 19:02:11 +00002454/// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2455/// subprograms.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002456void DwarfWriter::ConstructSubprogramDIEs() {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002457 std::vector<SubprogramDesc *> Subprograms =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002458 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002459
2460 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2461 SubprogramDesc *SPD = Subprograms[i];
2462 NewSubprogram(SPD);
2463 }
2464}
Jim Laskey52060a02006-01-24 00:49:18 +00002465
Jim Laskey063e7652006-01-17 17:31:53 +00002466//===----------------------------------------------------------------------===//
Jim Laskeyd18e2892006-01-20 20:34:06 +00002467// Main entry points.
Jim Laskey063e7652006-01-17 17:31:53 +00002468//
Jim Laskey52060a02006-01-24 00:49:18 +00002469
Jim Laskey563321a2006-09-06 18:34:40 +00002470DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A, TargetAsmInfo *T)
Jim Laskey52060a02006-01-24 00:49:18 +00002471: O(OS)
2472, Asm(A)
Jim Laskey563321a2006-09-06 18:34:40 +00002473, TAI(T)
Jim Laskey41886992006-04-07 16:34:46 +00002474, TD(Asm->TM.getTargetData())
2475, RI(Asm->TM.getRegisterInfo())
Jim Laskeyb8509c52006-03-23 18:07:55 +00002476, M(NULL)
2477, MF(NULL)
Jim Laskey52060a02006-01-24 00:49:18 +00002478, DebugInfo(NULL)
2479, didInitial(false)
Jim Laskey014f98c2006-06-14 11:35:03 +00002480, shouldEmit(false)
Jim Laskeyb8509c52006-03-23 18:07:55 +00002481, SubprogramCount(0)
Jim Laskey52060a02006-01-24 00:49:18 +00002482, CompileUnits()
2483, Abbreviations()
Jim Laskey52060a02006-01-24 00:49:18 +00002484, StringPool()
Jim Laskeybd761842006-02-27 17:27:12 +00002485, DescToUnitMap()
Jim Laskey0420f2a2006-02-22 19:02:11 +00002486, DescToDieMap()
Jim Laskey89d67fa2006-06-23 12:51:53 +00002487, SectionMap()
2488, SectionSourceLines()
Jim Laskey52060a02006-01-24 00:49:18 +00002489{}
2490DwarfWriter::~DwarfWriter() {
2491 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2492 delete CompileUnits[i];
Jim Laskey063e7652006-01-17 17:31:53 +00002493 }
Jim Laskey52060a02006-01-24 00:49:18 +00002494}
Jim Laskey063e7652006-01-17 17:31:53 +00002495
Jim Laskey41886992006-04-07 16:34:46 +00002496/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2497/// created it. Set by the target AsmPrinter.
2498void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
Jim Laskey014f98c2006-06-14 11:35:03 +00002499 // Make sure initial declarations are made.
2500 if (!DebugInfo && DI->hasInfo()) {
2501 DebugInfo = DI;
2502 shouldEmit = true;
2503
2504 // Emit initial sections
2505 EmitInitial();
2506
2507 // Create all the compile unit DIEs.
2508 ConstructCompileUnitDIEs();
2509
2510 // Create DIEs for each of the externally visible global variables.
2511 ConstructGlobalDIEs();
2512
2513 // Create DIEs for each of the externally visible subprograms.
2514 ConstructSubprogramDIEs();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002515
2516 // Prime section data.
Jim Laskey563321a2006-09-06 18:34:40 +00002517 SectionMap.insert(std::string("\t") + TAI->getTextSection());
Jim Laskey014f98c2006-06-14 11:35:03 +00002518 }
Jim Laskey41886992006-04-07 16:34:46 +00002519}
2520
Jim Laskey063e7652006-01-17 17:31:53 +00002521/// BeginModule - Emit all Dwarf sections that should come prior to the content.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002522///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002523void DwarfWriter::BeginModule(Module *M) {
2524 this->M = M;
2525
Jim Laskeyb2efb852006-01-04 22:28:25 +00002526 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002527 EOL("Dwarf Begin Module");
Jim Laskeyb2efb852006-01-04 22:28:25 +00002528}
2529
Jim Laskey063e7652006-01-17 17:31:53 +00002530/// EndModule - Emit all Dwarf sections that should come after the content.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002531///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002532void DwarfWriter::EndModule() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002533 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002534 EOL("Dwarf End Module");
2535
2536 // Standard sections final addresses.
Jim Laskey563321a2006-09-06 18:34:40 +00002537 Asm->SwitchToTextSection(TAI->getTextSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002538 EmitLabel("text_end", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002539 Asm->SwitchToDataSection(TAI->getDataSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002540 EmitLabel("data_end", 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002541
Jim Laskey89d67fa2006-06-23 12:51:53 +00002542 // End text sections.
2543 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2544 Asm->SwitchToTextSection(SectionMap[i].c_str(), 0);
2545 EmitLabel("section_end", i);
2546 }
2547
Jim Laskey063e7652006-01-17 17:31:53 +00002548 // Compute DIE offsets and sizes.
2549 SizeAndOffsets();
2550
2551 // Emit all the DIEs into a debug info section
2552 EmitDebugInfo();
2553
2554 // Corresponding abbreviations into a abbrev section.
2555 EmitAbbreviations();
2556
2557 // Emit source line correspondence into a debug line section.
2558 EmitDebugLines();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002559
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002560 // Emit info into a debug pubnames section.
2561 EmitDebugPubNames();
2562
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002563 // Emit info into a debug str section.
2564 EmitDebugStr();
2565
2566 // Emit info into a debug loc section.
2567 EmitDebugLoc();
2568
2569 // Emit info into a debug aranges section.
2570 EmitDebugARanges();
2571
2572 // Emit info into a debug ranges section.
2573 EmitDebugRanges();
2574
2575 // Emit info into a debug macinfo section.
2576 EmitDebugMacInfo();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002577}
2578
Jim Laskey6b92b8e2006-04-07 20:44:42 +00002579/// BeginFunction - Gather pre-function debug information. Assumes being
2580/// emitted immediately after the function entry point.
Jim Laskey89d67fa2006-06-23 12:51:53 +00002581void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002582 this->MF = MF;
2583
Jim Laskey89d67fa2006-06-23 12:51:53 +00002584 if (!ShouldEmitDwarf()) return;
2585 EOL("Dwarf Begin Function");
2586
2587 // Begin accumulating function debug information.
2588 DebugInfo->BeginFunction(MF);
Jim Laskey41886992006-04-07 16:34:46 +00002589
Jim Laskey89d67fa2006-06-23 12:51:53 +00002590 // Assumes in correct section after the entry point.
2591 EmitLabel("func_begin", ++SubprogramCount);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002592}
2593
Jim Laskeye719a7c2006-01-18 16:54:26 +00002594/// EndFunction - Gather and emit post-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002595///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002596void DwarfWriter::EndFunction() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002597 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002598 EOL("Dwarf End Function");
Jim Laskeyb8509c52006-03-23 18:07:55 +00002599
Jim Laskey89d67fa2006-06-23 12:51:53 +00002600 // Define end label for subprogram.
2601 EmitLabel("func_end", SubprogramCount);
Jim Laskey014f98c2006-06-14 11:35:03 +00002602
Jim Laskey89d67fa2006-06-23 12:51:53 +00002603 // Get function line info.
2604 std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
2605
2606 if (!LineInfos.empty()) {
2607 // Get section line info.
2608 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2609 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2610 std::vector<SourceLineInfo *> &SectionLineInfos =SectionSourceLines[ID-1];
2611 // Append the function info to section info.
2612 SectionLineInfos.insert(SectionLineInfos.end(),
2613 LineInfos.begin(), LineInfos.end());
Jim Laskey014f98c2006-06-14 11:35:03 +00002614 }
Jim Laskey41886992006-04-07 16:34:46 +00002615
Jim Laskey89d67fa2006-06-23 12:51:53 +00002616 // Construct scopes for subprogram.
2617 ConstructRootScope(DebugInfo->getRootScope());
2618
2619 // Emit function frame information.
2620 EmitFunctionDebugFrame();
2621
2622 // Reset the line numbers for the next function.
2623 LineInfos.clear();
2624
Jim Laskey41886992006-04-07 16:34:46 +00002625 // Clear function debug information.
2626 DebugInfo->EndFunction();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002627}