blob: ab00a998530a91aab77a5e31588962e3c54c6164 [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 Laskeyb8509c52006-03-23 18:07:55 +000026#include "llvm/Target/MRegisterInfo.h"
Jim Laskey52060a02006-01-24 00:49:18 +000027#include "llvm/Target/TargetMachine.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000028
Jim Laskeyb2efb852006-01-04 22:28:25 +000029#include <iostream>
Jim Laskeya7cea6f2006-01-04 13:52:30 +000030
Jim Laskeyb2efb852006-01-04 22:28:25 +000031using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000032using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000033
34static cl::opt<bool>
35DwarfVerbose("dwarf-verbose", cl::Hidden,
Jim Laskey063e7652006-01-17 17:31:53 +000036 cl::desc("Add comments to Dwarf directives."));
37
Jim Laskey0d086af2006-02-27 12:43:29 +000038namespace llvm {
39
Jim Laskey063e7652006-01-17 17:31:53 +000040//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000041// Forward declarations.
42//
Jim Laskey0d086af2006-02-27 12:43:29 +000043class DIE;
Jim Laskey063e7652006-01-17 17:31:53 +000044
Jim Laskey0d086af2006-02-27 12:43:29 +000045//===----------------------------------------------------------------------===//
Jim Laskeyf01e5472006-03-03 15:06:57 +000046// CompileUnit - This dwarf writer support class manages information associate
47// with a source file.
Jim Laskeybd761842006-02-27 17:27:12 +000048class CompileUnit {
49private:
50 CompileUnitDesc *Desc; // Compile unit debug descriptor.
51 unsigned ID; // File ID for source.
Jim Laskeyb8509c52006-03-23 18:07:55 +000052 DIE *Die; // Compile unit debug information entry.
Jim Laskeybd761842006-02-27 17:27:12 +000053 std::map<std::string, DIE *> Globals; // A map of globally visible named
54 // entities for this unit.
Jim Laskey90c79d72006-03-23 23:02:34 +000055 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
56 // Tracks the mapping of unit level
57 // debug informaton descriptors to debug
58 // information entries.
Jim Laskeybd761842006-02-27 17:27:12 +000059
60public:
61 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
62 : Desc(CUD)
63 , ID(I)
64 , Die(D)
65 , Globals()
Jim Laskey90c79d72006-03-23 23:02:34 +000066 , DescToDieMap()
Jim Laskeybd761842006-02-27 17:27:12 +000067 {}
68
69 ~CompileUnit();
70
71 // Accessors.
72 CompileUnitDesc *getDesc() const { return Desc; }
73 unsigned getID() const { return ID; }
74 DIE* getDie() const { return Die; }
75 std::map<std::string, DIE *> &getGlobals() { return Globals; }
76
77 /// hasContent - Return true if this compile unit has something to write out.
78 ///
79 bool hasContent() const;
80
81 /// AddGlobal - Add a new global entity to the compile unit.
82 ///
83 void AddGlobal(const std::string &Name, DIE *Die);
84
Jim Laskey90c79d72006-03-23 23:02:34 +000085 /// getDieMapSlotFor - Returns the debug information entry map slot for the
86 /// specified debug descriptor.
87 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
88 return DescToDieMap[DD];
89 }
Jim Laskeybd761842006-02-27 17:27:12 +000090};
91
92//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000093// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
94// Dwarf abbreviation.
95class DIEAbbrevData {
96private:
97 unsigned Attribute; // Dwarf attribute code.
98 unsigned Form; // Dwarf form code.
99
100public:
101 DIEAbbrevData(unsigned A, unsigned F)
102 : Attribute(A)
103 , Form(F)
104 {}
105
Jim Laskeybd761842006-02-27 17:27:12 +0000106 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000107 unsigned getAttribute() const { return Attribute; }
108 unsigned getForm() const { return Form; }
109
110 /// operator== - Used by DIEAbbrev to locate entry.
111 ///
112 bool operator==(const DIEAbbrevData &DAD) const {
113 return Attribute == DAD.Attribute && Form == DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000114 }
Jim Laskey063e7652006-01-17 17:31:53 +0000115
Jim Laskey0d086af2006-02-27 12:43:29 +0000116 /// operator!= - Used by DIEAbbrev to locate entry.
117 ///
118 bool operator!=(const DIEAbbrevData &DAD) const {
119 return Attribute != DAD.Attribute || Form != DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000120 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000121
122 /// operator< - Used by DIEAbbrev to locate entry.
123 ///
124 bool operator<(const DIEAbbrevData &DAD) const {
125 return Attribute < DAD.Attribute ||
126 (Attribute == DAD.Attribute && Form < DAD.Form);
127 }
128};
Jim Laskey063e7652006-01-17 17:31:53 +0000129
Jim Laskey0d086af2006-02-27 12:43:29 +0000130//===----------------------------------------------------------------------===//
131// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
132// information object.
133class DIEAbbrev {
134private:
135 unsigned Tag; // Dwarf tag code.
136 unsigned ChildrenFlag; // Dwarf children flag.
137 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
Jim Laskey063e7652006-01-17 17:31:53 +0000138
Jim Laskey0d086af2006-02-27 12:43:29 +0000139public:
Jim Laskey063e7652006-01-17 17:31:53 +0000140
Jim Laskey0d086af2006-02-27 12:43:29 +0000141 DIEAbbrev(unsigned T, unsigned C)
142 : Tag(T)
143 , ChildrenFlag(C)
144 , Data()
145 {}
146 ~DIEAbbrev() {}
147
Jim Laskeybd761842006-02-27 17:27:12 +0000148 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000149 unsigned getTag() const { return Tag; }
150 unsigned getChildrenFlag() const { return ChildrenFlag; }
151 const std::vector<DIEAbbrevData> &getData() const { return Data; }
152 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskey063e7652006-01-17 17:31:53 +0000153
Jim Laskey0d086af2006-02-27 12:43:29 +0000154 /// operator== - Used by UniqueVector to locate entry.
155 ///
156 bool operator==(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000157
Jim Laskey0d086af2006-02-27 12:43:29 +0000158 /// operator< - Used by UniqueVector to locate entry.
159 ///
160 bool operator<(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000161
Jim Laskey0d086af2006-02-27 12:43:29 +0000162 /// AddAttribute - Adds another set of attribute information to the
163 /// abbreviation.
164 void AddAttribute(unsigned Attribute, unsigned Form) {
165 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000166 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000167
Jim Laskeyb8509c52006-03-23 18:07:55 +0000168 /// AddFirstAttribute - Adds a set of attribute information to the front
169 /// of the abbreviation.
170 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
171 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
172 }
173
Jim Laskey0d086af2006-02-27 12:43:29 +0000174 /// Emit - Print the abbreviation using the specified Dwarf writer.
175 ///
176 void Emit(const DwarfWriter &DW) const;
177
178#ifndef NDEBUG
179 void print(std::ostream &O);
180 void dump();
181#endif
182};
Jim Laskey063e7652006-01-17 17:31:53 +0000183
Jim Laskey0d086af2006-02-27 12:43:29 +0000184//===----------------------------------------------------------------------===//
185// DIEValue - A debug information entry value.
186//
187class DIEValue {
188public:
189 enum {
190 isInteger,
191 isString,
192 isLabel,
193 isAsIsLabel,
194 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000195 isEntry,
196 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000197 };
198
199 unsigned Type; // Type of the value
200
201 DIEValue(unsigned T) : Type(T) {}
202 virtual ~DIEValue() {}
203
204 // Implement isa/cast/dyncast.
205 static bool classof(const DIEValue *) { return true; }
206
207 /// EmitValue - Emit value via the Dwarf writer.
208 ///
209 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
210
211 /// SizeOf - Return the size of a value in bytes.
212 ///
213 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
214};
Jim Laskey063e7652006-01-17 17:31:53 +0000215
Jim Laskey0d086af2006-02-27 12:43:29 +0000216//===----------------------------------------------------------------------===//
217// DWInteger - An integer value DIE.
218//
219class DIEInteger : public DIEValue {
220private:
221 uint64_t Integer;
222
223public:
224 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000225
Jim Laskey0d086af2006-02-27 12:43:29 +0000226 // Implement isa/cast/dyncast.
227 static bool classof(const DIEInteger *) { return true; }
228 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
229
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000230 /// BestForm - Choose the best form for integer.
231 ///
232 unsigned BestForm(bool IsSigned);
233
Jim Laskey0d086af2006-02-27 12:43:29 +0000234 /// EmitValue - Emit integer of appropriate size.
235 ///
236 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
237
238 /// SizeOf - Determine size of integer value in bytes.
239 ///
240 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
241};
Jim Laskey063e7652006-01-17 17:31:53 +0000242
Jim Laskey0d086af2006-02-27 12:43:29 +0000243//===----------------------------------------------------------------------===//
244// DIEString - A string value DIE.
245//
246struct DIEString : public DIEValue {
247 const std::string String;
248
249 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000250
Jim Laskey0d086af2006-02-27 12:43:29 +0000251 // Implement isa/cast/dyncast.
252 static bool classof(const DIEString *) { return true; }
253 static bool classof(const DIEValue *S) { return S->Type == isString; }
254
255 /// EmitValue - Emit string value.
256 ///
257 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
258
259 /// SizeOf - Determine size of string value in bytes.
260 ///
261 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
262};
Jim Laskey063e7652006-01-17 17:31:53 +0000263
Jim Laskey0d086af2006-02-27 12:43:29 +0000264//===----------------------------------------------------------------------===//
265// DIEDwarfLabel - A Dwarf internal label expression DIE.
266//
267struct DIEDwarfLabel : public DIEValue {
268 const DWLabel Label;
269
270 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000271
Jim Laskey0d086af2006-02-27 12:43:29 +0000272 // Implement isa/cast/dyncast.
273 static bool classof(const DIEDwarfLabel *) { return true; }
274 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
275
276 /// EmitValue - Emit label value.
277 ///
278 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
279
280 /// SizeOf - Determine size of label value in bytes.
281 ///
282 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
283};
Jim Laskey063e7652006-01-17 17:31:53 +0000284
Jim Laskey063e7652006-01-17 17:31:53 +0000285
Jim Laskey0d086af2006-02-27 12:43:29 +0000286//===----------------------------------------------------------------------===//
287// DIEObjectLabel - A label to an object in code or data.
288//
289struct DIEObjectLabel : public DIEValue {
290 const std::string Label;
291
292 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000293
Jim Laskey0d086af2006-02-27 12:43:29 +0000294 // Implement isa/cast/dyncast.
295 static bool classof(const DIEObjectLabel *) { return true; }
296 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
297
298 /// EmitValue - Emit label value.
299 ///
300 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
301
302 /// SizeOf - Determine size of label value in bytes.
303 ///
304 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
305};
Jim Laskey063e7652006-01-17 17:31:53 +0000306
Jim Laskey0d086af2006-02-27 12:43:29 +0000307//===----------------------------------------------------------------------===//
308// DIEDelta - A simple label difference DIE.
309//
310struct DIEDelta : public DIEValue {
311 const DWLabel LabelHi;
312 const DWLabel LabelLo;
313
314 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
315 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000316
Jim Laskey0d086af2006-02-27 12:43:29 +0000317 // Implement isa/cast/dyncast.
318 static bool classof(const DIEDelta *) { return true; }
319 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
320
321 /// EmitValue - Emit delta value.
322 ///
323 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
324
325 /// SizeOf - Determine size of delta value in bytes.
326 ///
327 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
328};
Jim Laskey063e7652006-01-17 17:31:53 +0000329
Jim Laskey0d086af2006-02-27 12:43:29 +0000330//===----------------------------------------------------------------------===//
331// DIEntry - A pointer to a debug information entry.
332//
333struct DIEntry : public DIEValue {
334 DIE *Entry;
335
336 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
337
338 // Implement isa/cast/dyncast.
339 static bool classof(const DIEntry *) { return true; }
340 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
341
Jim Laskeyb8509c52006-03-23 18:07:55 +0000342 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000343 ///
344 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
345
Jim Laskeyb8509c52006-03-23 18:07:55 +0000346 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000347 ///
348 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
349};
350
351//===----------------------------------------------------------------------===//
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000352// DIEBlock - A block of values. Primarily used for location expressions.
353//
354struct DIEBlock : public DIEValue {
355 unsigned Size; // Size in bytes excluding size header.
356 std::vector<unsigned> Forms; // Data forms.
357 std::vector<DIEValue *> Values; // Block values.
358
359 DIEBlock()
360 : DIEValue(isBlock)
361 , Size(0)
362 , Forms()
363 , Values()
364 {}
365 ~DIEBlock();
366
367 // Implement isa/cast/dyncast.
368 static bool classof(const DIEBlock *) { return true; }
369 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
370
371 /// ComputeSize - calculate the size of the block.
372 ///
373 unsigned ComputeSize(DwarfWriter &DW);
374
375 /// BestForm - Choose the best form for data.
376 ///
377 unsigned BestForm();
378
379 /// EmitValue - Emit block data.
380 ///
381 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
382
383 /// SizeOf - Determine size of block data in bytes.
384 ///
385 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
386
387 /// AddUInt - Add an unsigned integer value.
388 ///
389 void AddUInt(unsigned Form, uint64_t Integer);
390
391 /// AddSInt - Add an signed integer value.
392 ///
393 void AddSInt(unsigned Form, int64_t Integer);
394
395 /// AddString - Add a std::string value.
396 ///
397 void AddString(unsigned Form, const std::string &String);
398
399 /// AddLabel - Add a Dwarf label value.
400 ///
401 void AddLabel(unsigned Form, const DWLabel &Label);
402
403 /// AddObjectLabel - Add a non-Dwarf label value.
404 ///
405 void AddObjectLabel(unsigned Form, const std::string &Label);
406
407 /// AddDelta - Add a label delta value.
408 ///
409 void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
410
411 /// AddDIEntry - Add a DIE value.
412 ///
413 void AddDIEntry(unsigned Form, DIE *Entry);
414
415};
416
417//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +0000418// DIE - A structured debug information entry. Has an abbreviation which
419// describes it's organization.
420class DIE {
421private:
422 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
423 unsigned AbbrevID; // Decribing abbreviation ID.
424 unsigned Offset; // Offset in debug info section.
425 unsigned Size; // Size of instance + children.
426 std::vector<DIE *> Children; // Children DIEs.
427 std::vector<DIEValue *> Values; // Attributes values.
428
429public:
430 DIE(unsigned Tag);
431 ~DIE();
432
Jim Laskeybd761842006-02-27 17:27:12 +0000433 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000434 unsigned getAbbrevID() const { return AbbrevID; }
435 unsigned getOffset() const { return Offset; }
436 unsigned getSize() const { return Size; }
437 const std::vector<DIE *> &getChildren() const { return Children; }
438 const std::vector<DIEValue *> &getValues() const { return Values; }
439 void setOffset(unsigned O) { Offset = O; }
440 void setSize(unsigned S) { Size = S; }
441
442 /// SiblingOffset - Return the offset of the debug information entry's
443 /// sibling.
444 unsigned SiblingOffset() const { return Offset + Size; }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000445
446 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
447 ///
448 void AddSiblingOffset();
Jim Laskey0d086af2006-02-27 12:43:29 +0000449
450 /// AddUInt - Add an unsigned integer attribute data and value.
451 ///
452 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
453
454 /// AddSInt - Add an signed integer attribute data and value.
455 ///
456 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
457
458 /// AddString - Add a std::string attribute data and value.
459 ///
460 void AddString(unsigned Attribute, unsigned Form,
461 const std::string &String);
462
463 /// AddLabel - Add a Dwarf label attribute data and value.
464 ///
465 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
466
467 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
468 ///
469 void AddObjectLabel(unsigned Attribute, unsigned Form,
470 const std::string &Label);
471
472 /// AddDelta - Add a label delta attribute data and value.
473 ///
474 void AddDelta(unsigned Attribute, unsigned Form,
475 const DWLabel &Hi, const DWLabel &Lo);
476
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000477 /// AddDIEntry - Add a DIE attribute data and value.
Jim Laskey0d086af2006-02-27 12:43:29 +0000478 ///
479 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
480
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000481 /// AddBlock - Add block data.
482 ///
483 void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
484
Jim Laskey0d086af2006-02-27 12:43:29 +0000485 /// Complete - Indicate that all attributes have been added and
486 /// ready to get an abbreviation ID.
487 ///
488 void Complete(DwarfWriter &DW);
489
490 /// AddChild - Add a child to the DIE.
491 void AddChild(DIE *Child);
492};
493
494} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +0000495
496//===----------------------------------------------------------------------===//
497
Jim Laskeybd761842006-02-27 17:27:12 +0000498CompileUnit::~CompileUnit() {
499 delete Die;
500}
501
502/// hasContent - Return true if this compile unit has something to write out.
503///
504bool CompileUnit::hasContent() const {
505 return !Die->getChildren().empty();
506}
507
508/// AddGlobal - Add a new global entity to the compile unit.
509///
510void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
511 Globals[Name] = Die;
512}
513
514//===----------------------------------------------------------------------===//
515
Jim Laskeyd18e2892006-01-20 20:34:06 +0000516/// operator== - Used by UniqueVector to locate entry.
517///
518bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
519 if (Tag != DA.Tag) return false;
520 if (ChildrenFlag != DA.ChildrenFlag) return false;
521 if (Data.size() != DA.Data.size()) return false;
522
Jim Laskey52060a02006-01-24 00:49:18 +0000523 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000524 if (Data[i] != DA.Data[i]) return false;
Jim Laskeyd18e2892006-01-20 20:34:06 +0000525 }
526
527 return true;
528}
529
530/// operator< - Used by UniqueVector to locate entry.
531///
532bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
533 if (Tag != DA.Tag) return Tag < DA.Tag;
534 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
535 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
536
Jim Laskey52060a02006-01-24 00:49:18 +0000537 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000538 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +0000539 }
540
541 return false;
542}
543
544/// Emit - Print the abbreviation using the specified Dwarf writer.
545///
546void DIEAbbrev::Emit(const DwarfWriter &DW) const {
547 // Emit its Dwarf tag type.
548 DW.EmitULEB128Bytes(Tag);
549 DW.EOL(TagString(Tag));
550
551 // Emit whether it has children DIEs.
552 DW.EmitULEB128Bytes(ChildrenFlag);
553 DW.EOL(ChildrenString(ChildrenFlag));
554
555 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +0000556 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000557 const DIEAbbrevData &AttrData = Data[i];
558
559 // Emit attribute type.
560 DW.EmitULEB128Bytes(AttrData.getAttribute());
561 DW.EOL(AttributeString(AttrData.getAttribute()));
562
563 // Emit form type.
564 DW.EmitULEB128Bytes(AttrData.getForm());
565 DW.EOL(FormEncodingString(AttrData.getForm()));
566 }
567
568 // Mark end of abbreviation.
569 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
570 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
571}
572
573#ifndef NDEBUG
574 void DIEAbbrev::print(std::ostream &O) {
575 O << "Abbreviation @"
Jeff Cohen05ebc8d2006-01-25 17:18:50 +0000576 << std::hex << (intptr_t)this << std::dec
Jim Laskeyd18e2892006-01-20 20:34:06 +0000577 << " "
578 << TagString(Tag)
579 << " "
580 << ChildrenString(ChildrenFlag)
581 << "\n";
582
Jim Laskey52060a02006-01-24 00:49:18 +0000583 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000584 O << " "
585 << AttributeString(Data[i].getAttribute())
586 << " "
587 << FormEncodingString(Data[i].getForm())
588 << "\n";
589 }
590 }
591 void DIEAbbrev::dump() { print(std::cerr); }
592#endif
593
594//===----------------------------------------------------------------------===//
595
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000596/// BestForm - Choose the best form for integer.
597///
598unsigned DIEInteger::BestForm(bool IsSigned) {
599 if (IsSigned) {
600 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
601 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
602 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
603 } else {
604 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
605 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
606 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
607 }
608 return DW_FORM_data8;
609}
610
Jim Laskey063e7652006-01-17 17:31:53 +0000611/// EmitValue - Emit integer of appropriate size.
612///
613void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
614 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +0000615 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000616 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000617 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000618 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000619 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000620 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000621 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000622 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000623 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +0000624 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
625 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +0000626 default: assert(0 && "DIE Value form not supported yet"); break;
627 }
628}
629
630/// SizeOf - Determine size of integer value in bytes.
631///
632unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
633 switch (Form) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000634 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000635 case DW_FORM_ref1: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000636 case DW_FORM_data1: return sizeof(int8_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000637 case DW_FORM_ref2: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000638 case DW_FORM_data2: return sizeof(int16_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000639 case DW_FORM_ref4: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000640 case DW_FORM_data4: return sizeof(int32_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000641 case DW_FORM_ref8: // Fall thru
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000642 case DW_FORM_data8: return sizeof(int64_t);
Jim Laskey40020172006-01-20 21:02:36 +0000643 case DW_FORM_udata: return DW.SizeULEB128(Integer);
644 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +0000645 default: assert(0 && "DIE Value form not supported yet"); break;
646 }
647 return 0;
648}
649
650//===----------------------------------------------------------------------===//
651
652/// EmitValue - Emit string value.
653///
654void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000655 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +0000656}
657
658/// SizeOf - Determine size of string value in bytes.
659///
660unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey40020172006-01-20 21:02:36 +0000661 return String.size() + sizeof(char); // sizeof('\0');
Jim Laskey063e7652006-01-17 17:31:53 +0000662}
663
664//===----------------------------------------------------------------------===//
665
666/// EmitValue - Emit label value.
667///
Jim Laskey52060a02006-01-24 00:49:18 +0000668void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000669 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +0000670}
671
672/// SizeOf - Determine size of label value in bytes.
673///
Jim Laskey52060a02006-01-24 00:49:18 +0000674unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +0000675 return DW.getAddressSize();
676}
677
678//===----------------------------------------------------------------------===//
679
Jim Laskeyd18e2892006-01-20 20:34:06 +0000680/// EmitValue - Emit label value.
681///
Jim Laskey52060a02006-01-24 00:49:18 +0000682void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000683 DW.EmitReference(Label);
684}
685
686/// SizeOf - Determine size of label value in bytes.
687///
Jim Laskey52060a02006-01-24 00:49:18 +0000688unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000689 return DW.getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +0000690}
691
692//===----------------------------------------------------------------------===//
693
Jim Laskey063e7652006-01-17 17:31:53 +0000694/// EmitValue - Emit delta value.
695///
696void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000697 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +0000698}
699
700/// SizeOf - Determine size of delta value in bytes.
701///
702unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
703 return DW.getAddressSize();
704}
705
706//===----------------------------------------------------------------------===//
Jim Laskeyb8509c52006-03-23 18:07:55 +0000707/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000708///
709void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +0000710 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +0000711}
712
Jim Laskeyb8509c52006-03-23 18:07:55 +0000713/// SizeOf - Determine size of debug information entry value in bytes.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000714///
715unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
716 return sizeof(int32_t);
717}
718
719//===----------------------------------------------------------------------===//
720
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000721DIEBlock::~DIEBlock() {
722 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
723 delete Values[i];
724 }
725}
726
727/// ComputeSize - calculate the size of the block.
728///
729unsigned DIEBlock::ComputeSize(DwarfWriter &DW) {
730 Size = 0;
731 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
732 Size += Values[i]->SizeOf(DW, Forms[i]);
733 }
734 return Size;
735}
736
737/// BestForm - Choose the best form for data.
738///
739unsigned DIEBlock::BestForm() {
740 if ((unsigned char)Size == Size) return DW_FORM_block1;
741 if ((unsigned short)Size == Size) return DW_FORM_block2;
742 if ((unsigned int)Size == Size) return DW_FORM_block4;
743 return DW_FORM_block;
744}
745
746/// EmitValue - Emit block data.
747///
748void DIEBlock::EmitValue(const DwarfWriter &DW, unsigned Form) const {
749 switch (Form) {
750 case DW_FORM_block1: DW.EmitInt8(Size); break;
751 case DW_FORM_block2: DW.EmitInt16(Size); break;
752 case DW_FORM_block4: DW.EmitInt32(Size); break;
753 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
754 default: assert(0 && "Improper form for block"); break;
755 }
756 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
757 DW.EOL("");
758 Values[i]->EmitValue(DW, Forms[i]);
759 }
760}
761
762/// SizeOf - Determine size of block data in bytes.
763///
764unsigned DIEBlock::SizeOf(const DwarfWriter &DW, unsigned Form) const {
765 switch (Form) {
766 case DW_FORM_block1: return Size + sizeof(int8_t);
767 case DW_FORM_block2: return Size + sizeof(int16_t);
768 case DW_FORM_block4: return Size + sizeof(int32_t);
769 case DW_FORM_block: return Size + DW.SizeULEB128(Size);
770 default: assert(0 && "Improper form for block"); break;
771 }
772 return 0;
773}
774
775/// AddUInt - Add an unsigned integer value.
776///
777void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
778 DIEInteger *DI = new DIEInteger(Integer);
779 Values.push_back(DI);
780 if (Form == 0) Form = DI->BestForm(false);
781 Forms.push_back(Form);
782}
783
784/// AddSInt - Add an signed integer value.
785///
786void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
787 DIEInteger *DI = new DIEInteger(Integer);
788 Values.push_back(DI);
789 if (Form == 0) Form = DI->BestForm(true);
790 Forms.push_back(Form);
791}
792
793/// AddString - Add a std::string value.
794///
795void DIEBlock::AddString(unsigned Form, const std::string &String) {
796 Values.push_back(new DIEString(String));
797 Forms.push_back(Form);
798}
799
800/// AddLabel - Add a Dwarf label value.
801///
802void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
803 Values.push_back(new DIEDwarfLabel(Label));
804 Forms.push_back(Form);
805}
806
807/// AddObjectLabel - Add a non-Dwarf label value.
808///
809void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
810 Values.push_back(new DIEObjectLabel(Label));
811 Forms.push_back(Form);
812}
813
814/// AddDelta - Add a label delta value.
815///
816void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
817 Values.push_back(new DIEDelta(Hi, Lo));
818 Forms.push_back(Form);
819}
820
821/// AddDIEntry - Add a DIE value.
822///
823void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
824 Values.push_back(new DIEntry(Entry));
825 Forms.push_back(Form);
826}
827
828//===----------------------------------------------------------------------===//
829
Jim Laskey0420f2a2006-02-22 19:02:11 +0000830DIE::DIE(unsigned Tag)
831: Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
Jim Laskeyd18e2892006-01-20 20:34:06 +0000832, AbbrevID(0)
833, Offset(0)
834, Size(0)
Jim Laskeyd18e2892006-01-20 20:34:06 +0000835, Children()
836, Values()
837{}
838
839DIE::~DIE() {
840 if (Abbrev) delete Abbrev;
841
Jim Laskey52060a02006-01-24 00:49:18 +0000842 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000843 delete Children[i];
844 }
845
Jim Laskey52060a02006-01-24 00:49:18 +0000846 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000847 delete Values[j];
848 }
Jim Laskeyd18e2892006-01-20 20:34:06 +0000849}
850
Jim Laskeyb8509c52006-03-23 18:07:55 +0000851/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
852///
853void DIE::AddSiblingOffset() {
854 DIEInteger *DI = new DIEInteger(0);
855 Values.insert(Values.begin(), DI);
856 Abbrev->AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
857}
858
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000859/// AddUInt - Add an unsigned integer attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000860///
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000861void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000862 DIEInteger *DI = new DIEInteger(Integer);
863 Values.push_back(DI);
864 if (!Form) Form = DI->BestForm(false);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000865 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000866}
867
868/// AddSInt - Add an signed integer attribute data and value.
869///
870void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000871 DIEInteger *DI = new DIEInteger(Integer);
872 Values.push_back(DI);
873 if (!Form) Form = DI->BestForm(true);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000874 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000875}
876
877/// AddString - Add a std::string attribute data and value.
878///
879void DIE::AddString(unsigned Attribute, unsigned Form,
880 const std::string &String) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000881 Values.push_back(new DIEString(String));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000882 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000883}
884
885/// AddLabel - Add a Dwarf label attribute data and value.
886///
887void DIE::AddLabel(unsigned Attribute, unsigned Form,
888 const DWLabel &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000889 Values.push_back(new DIEDwarfLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000890 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000891}
892
Jim Laskey52060a02006-01-24 00:49:18 +0000893/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000894///
Jim Laskey52060a02006-01-24 00:49:18 +0000895void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
896 const std::string &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000897 Values.push_back(new DIEObjectLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000898 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000899}
900
901/// AddDelta - Add a label delta attribute data and value.
902///
903void DIE::AddDelta(unsigned Attribute, unsigned Form,
904 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000905 Values.push_back(new DIEDelta(Hi, Lo));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000906 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000907}
908
909/// AddDIEntry - Add a DIE attribute data and value.
910///
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000911void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000912 Values.push_back(new DIEntry(Entry));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000913 Abbrev->AddAttribute(Attribute, Form);
914}
915
916/// AddBlock - Add block data.
917///
918void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
919 assert(Block->Size && "Block size has not been computed");
920 Values.push_back(Block);
921 if (!Form) Form = Block->BestForm();
922 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000923}
924
925/// Complete - Indicate that all attributes have been added and ready to get an
926/// abbreviation ID.
927void DIE::Complete(DwarfWriter &DW) {
928 AbbrevID = DW.NewAbbreviation(Abbrev);
929 delete Abbrev;
930 Abbrev = NULL;
931}
932
933/// AddChild - Add a child to the DIE.
934///
935void DIE::AddChild(DIE *Child) {
Jim Laskey0420f2a2006-02-22 19:02:11 +0000936 assert(Abbrev && "Adding children without an abbreviation");
937 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000938 Children.push_back(Child);
939}
940
941//===----------------------------------------------------------------------===//
942
Jim Laskey0420f2a2006-02-22 19:02:11 +0000943/// DWContext
Jim Laskeyd18e2892006-01-20 20:34:06 +0000944
945//===----------------------------------------------------------------------===//
Jim Laskey063e7652006-01-17 17:31:53 +0000946
947/// PrintHex - Print a value as a hexidecimal value.
948///
949void DwarfWriter::PrintHex(int Value) const {
950 O << "0x" << std::hex << Value << std::dec;
951}
952
953/// EOL - Print a newline character to asm stream. If a comment is present
954/// then it will be printed first. Comments should not contain '\n'.
955void DwarfWriter::EOL(const std::string &Comment) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000956 if (DwarfVerbose && !Comment.empty()) {
Jim Laskey063e7652006-01-17 17:31:53 +0000957 O << "\t"
958 << Asm->CommentString
959 << " "
960 << Comment;
961 }
962 O << "\n";
963}
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000964
Jim Laskeyb8509c52006-03-23 18:07:55 +0000965/// EmitAlign - Print a align directive.
966///
967void DwarfWriter::EmitAlign(unsigned Alignment) const {
968 O << Asm->AlignDirective << Alignment << "\n";
969}
970
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000971/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
Jim Laskey063e7652006-01-17 17:31:53 +0000972/// unsigned leb128 value.
973void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000974 if (hasLEB128) {
975 O << "\t.uleb128\t"
976 << Value;
977 } else {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000978 O << Asm->Data8bitsDirective;
Jim Laskey063e7652006-01-17 17:31:53 +0000979 PrintULEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000980 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000981}
982
983/// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
Jim Laskey063e7652006-01-17 17:31:53 +0000984/// signed leb128 value.
985void DwarfWriter::EmitSLEB128Bytes(int Value) const {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000986 if (hasLEB128) {
987 O << "\t.sleb128\t"
988 << Value;
989 } else {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000990 O << Asm->Data8bitsDirective;
Jim Laskey063e7652006-01-17 17:31:53 +0000991 PrintSLEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000992 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000993}
994
Jim Laskey063e7652006-01-17 17:31:53 +0000995/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +0000996/// representing an unsigned leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +0000997void DwarfWriter::PrintULEB128(unsigned Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000998 do {
999 unsigned Byte = Value & 0x7f;
1000 Value >>= 7;
1001 if (Value) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001002 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001003 if (Value) O << ", ";
1004 } while (Value);
1005}
1006
Jim Laskey063e7652006-01-17 17:31:53 +00001007/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
1008/// value.
1009unsigned DwarfWriter::SizeULEB128(unsigned Value) {
1010 unsigned Size = 0;
1011 do {
1012 Value >>= 7;
1013 Size += sizeof(int8_t);
1014 } while (Value);
1015 return Size;
1016}
1017
1018/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +00001019/// representing a signed leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +00001020void DwarfWriter::PrintSLEB128(int Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001021 int Sign = Value >> (8 * sizeof(Value) - 1);
1022 bool IsMore;
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001023
Jim Laskeyb2efb852006-01-04 22:28:25 +00001024 do {
1025 unsigned Byte = Value & 0x7f;
1026 Value >>= 7;
1027 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1028 if (IsMore) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001029 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001030 if (IsMore) O << ", ";
1031 } while (IsMore);
1032}
1033
Jim Laskey063e7652006-01-17 17:31:53 +00001034/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
1035/// value.
1036unsigned DwarfWriter::SizeSLEB128(int Value) {
1037 unsigned Size = 0;
1038 int Sign = Value >> (8 * sizeof(Value) - 1);
1039 bool IsMore;
1040
1041 do {
1042 unsigned Byte = Value & 0x7f;
1043 Value >>= 7;
1044 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1045 Size += sizeof(int8_t);
1046 } while (IsMore);
1047 return Size;
1048}
1049
Jim Laskeyda427fa2006-01-27 20:31:25 +00001050/// EmitInt8 - Emit a byte directive and value.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001051///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001052void DwarfWriter::EmitInt8(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001053 O << Asm->Data8bitsDirective;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001054 PrintHex(Value & 0xFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001055}
1056
Jim Laskeyda427fa2006-01-27 20:31:25 +00001057/// EmitInt16 - Emit a short directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001058///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001059void DwarfWriter::EmitInt16(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001060 O << Asm->Data16bitsDirective;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001061 PrintHex(Value & 0xFFFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001062}
1063
Jim Laskeyda427fa2006-01-27 20:31:25 +00001064/// EmitInt32 - Emit a long directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001065///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001066void DwarfWriter::EmitInt32(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001067 O << Asm->Data32bitsDirective;
1068 PrintHex(Value);
1069}
1070
Jim Laskeyda427fa2006-01-27 20:31:25 +00001071/// EmitInt64 - Emit a long long directive and value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001072///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001073void DwarfWriter::EmitInt64(uint64_t Value) const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001074 if (Asm->Data64bitsDirective) {
1075 O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
1076 } else {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001077 if (TD.isBigEndian()) {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001078 EmitInt32(unsigned(Value >> 32)); O << "\n";
1079 EmitInt32(unsigned(Value));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001080 } else {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001081 EmitInt32(unsigned(Value)); O << "\n";
1082 EmitInt32(unsigned(Value >> 32));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001083 }
1084 }
1085}
1086
Jim Laskey063e7652006-01-17 17:31:53 +00001087/// EmitString - Emit a string with quotes and a null terminator.
1088/// Special characters are emitted properly. (Eg. '\t')
1089void DwarfWriter::EmitString(const std::string &String) const {
1090 O << Asm->AsciiDirective
1091 << "\"";
Jim Laskey52060a02006-01-24 00:49:18 +00001092 for (unsigned i = 0, N = String.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001093 unsigned char C = String[i];
1094
1095 if (!isascii(C) || iscntrl(C)) {
1096 switch(C) {
1097 case '\b': O << "\\b"; break;
1098 case '\f': O << "\\f"; break;
1099 case '\n': O << "\\n"; break;
1100 case '\r': O << "\\r"; break;
1101 case '\t': O << "\\t"; break;
1102 default:
1103 O << '\\';
1104 O << char('0' + (C >> 6));
1105 O << char('0' + (C >> 3));
1106 O << char('0' + (C >> 0));
1107 break;
1108 }
1109 } else if (C == '\"') {
1110 O << "\\\"";
1111 } else if (C == '\'') {
1112 O << "\\\'";
1113 } else {
1114 O << C;
1115 }
1116 }
1117 O << "\\0\"";
1118}
1119
1120/// PrintLabelName - Print label name in form used by Dwarf writer.
1121///
1122void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001123 O << Asm->PrivateGlobalPrefix
1124 << "debug_"
Jim Laskey0420f2a2006-02-22 19:02:11 +00001125 << Tag;
1126 if (Number) O << Number;
Jim Laskeyb2efb852006-01-04 22:28:25 +00001127}
1128
Jim Laskey063e7652006-01-17 17:31:53 +00001129/// EmitLabel - Emit location label for internal use by Dwarf.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001130///
Jim Laskey063e7652006-01-17 17:31:53 +00001131void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
1132 PrintLabelName(Tag, Number);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001133 O << ":\n";
1134}
1135
Jim Laskeye719a7c2006-01-18 16:54:26 +00001136/// EmitReference - Emit a reference to a label.
Jim Laskey063e7652006-01-17 17:31:53 +00001137///
Jim Laskeye719a7c2006-01-18 16:54:26 +00001138void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001139 if (AddressSize == 4)
1140 O << Asm->Data32bitsDirective;
1141 else
1142 O << Asm->Data64bitsDirective;
1143
1144 PrintLabelName(Tag, Number);
1145}
Jim Laskey73683212006-01-21 00:59:54 +00001146void DwarfWriter::EmitReference(const std::string &Name) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001147 if (AddressSize == 4)
1148 O << Asm->Data32bitsDirective;
1149 else
1150 O << Asm->Data64bitsDirective;
1151
1152 O << Name;
1153}
Jim Laskey063e7652006-01-17 17:31:53 +00001154
1155/// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
1156/// assemblers do not accept absolute expressions with data directives, so there
1157/// is an option (needsSet) to use an intermediary 'set' expression.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001158void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
1159 const char *TagLo, unsigned NumberLo) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001160 if (needsSet) {
1161 static unsigned SetCounter = 0;
Jim Laskeyd18e2892006-01-20 20:34:06 +00001162
Jim Laskey063e7652006-01-17 17:31:53 +00001163 O << "\t.set\t";
1164 PrintLabelName("set", SetCounter);
1165 O << ",";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001166 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001167 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001168 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001169 O << "\n";
1170
1171 if (AddressSize == sizeof(int32_t))
1172 O << Asm->Data32bitsDirective;
1173 else
1174 O << Asm->Data64bitsDirective;
1175
1176 PrintLabelName("set", SetCounter);
1177
Jim Laskey52060a02006-01-24 00:49:18 +00001178 ++SetCounter;
Jim Laskey063e7652006-01-17 17:31:53 +00001179 } else {
1180 if (AddressSize == sizeof(int32_t))
1181 O << Asm->Data32bitsDirective;
1182 else
1183 O << Asm->Data64bitsDirective;
1184
Jim Laskeyd18e2892006-01-20 20:34:06 +00001185 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001186 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001187 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001188 }
1189}
1190
Jim Laskeyd18e2892006-01-20 20:34:06 +00001191/// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
Jim Laskey063e7652006-01-17 17:31:53 +00001192///
Jim Laskeyd18e2892006-01-20 20:34:06 +00001193unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
1194 return Abbreviations.insert(*Abbrev);
1195}
1196
1197/// NewString - Add a string to the constant pool and returns a label.
1198///
1199DWLabel DwarfWriter::NewString(const std::string &String) {
1200 unsigned StringID = StringPool.insert(String);
1201 return DWLabel("string", StringID);
1202}
1203
Jim Laskeyb8509c52006-03-23 18:07:55 +00001204/// AddSourceLine - Add location information to specified debug information
1205/// entry.
1206void DwarfWriter::AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1207 if (File && Line) {
1208 CompileUnit *FileUnit = FindCompileUnit(File);
1209 unsigned FileID = FileUnit->getID();
1210 Die->AddUInt(DW_AT_decl_file, 0, FileID);
1211 Die->AddUInt(DW_AT_decl_line, 0, Line);
1212 }
1213}
1214
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001215/// AddAddress - Add an address attribute to a die based on the location
1216/// provided.
1217void DwarfWriter::AddAddress(DIE *Die, unsigned Attribute,
Jim Laskey41886992006-04-07 16:34:46 +00001218 const MachineLocation &Location) {
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001219 DIEBlock *Block = new DIEBlock();
1220 if (Location.isRegister()) {
Jim Laskey41886992006-04-07 16:34:46 +00001221 Block->AddUInt(DW_FORM_data1,
1222 DW_OP_reg0 + RI->getDwarfRegNum(Location.getRegister()));
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001223 } else {
Jim Laskey41886992006-04-07 16:34:46 +00001224 Block->AddUInt(DW_FORM_data1,
1225 DW_OP_breg0 + RI->getDwarfRegNum(Location.getRegister()));
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001226 Block->AddUInt(DW_FORM_sdata, Location.getOffset());
1227 }
1228 Block->ComputeSize(*this);
1229 Die->AddBlock(Attribute, 0, Block);
1230}
1231
Jim Laskey90c79d72006-03-23 23:02:34 +00001232/// getDieMapSlotFor - Returns the debug information entry map slot for the
1233/// specified debug descriptor.
1234DIE *&DwarfWriter::getDieMapSlotFor(DebugInfoDesc *DD) {
1235 return DescToDieMap[DD];
Jim Laskey0420f2a2006-02-22 19:02:11 +00001236}
Jim Laskey90c79d72006-03-23 23:02:34 +00001237
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001238/// NewType - Create a new type DIE.
1239///
Jim Laskey90c79d72006-03-23 23:02:34 +00001240DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit) {
1241 if (!TyDesc) {
1242 // FIXME - Hack for missing types
1243 DIE *Die = new DIE(DW_TAG_base_type);
1244 Die->AddUInt(DW_AT_byte_size, 0, 4);
1245 Die->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1246 Unit->getDie()->AddChild(Die);
1247 return Die;
1248 }
Jim Laskey92ae7402006-03-01 18:20:30 +00001249
1250 // FIXME - Should handle other contexts that compile units.
Jim Laskey69906002006-02-24 16:46:40 +00001251
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001252 // Check for pre-existence.
Jim Laskey90c79d72006-03-23 23:02:34 +00001253 DIE *&Slot = Unit->getDieMapSlotFor(TyDesc);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001254 if (Slot) return Slot;
1255
1256 // Get core information.
1257 const std::string &Name = TyDesc->getName();
Jim Laskey288fe0f2006-03-01 18:13:05 +00001258 uint64_t Size = TyDesc->getSize() >> 3;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001259
Jim Laskey434b40b2006-02-23 22:37:30 +00001260 DIE *Ty = NULL;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001261
Jim Laskey434b40b2006-02-23 22:37:30 +00001262 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001263 // Fundamental types like int, float, bool
Jim Laskey434b40b2006-02-23 22:37:30 +00001264 Slot = Ty = new DIE(DW_TAG_base_type);
1265 unsigned Encoding = BasicTy->getEncoding();
1266 Ty->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
Jim Laskey69906002006-02-24 16:46:40 +00001267 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001268 // Create specific DIE.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001269 Slot = Ty = new DIE(DerivedTy->getTag());
Jim Laskey69906002006-02-24 16:46:40 +00001270
1271 // Map to main type, void will not have a type.
1272 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001273 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1274 NewType(Context, FromTy, Unit));
Jim Laskey69906002006-02-24 16:46:40 +00001275 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001276 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
Jim Laskeyf8913f12006-03-01 17:53:02 +00001277 // Create specific DIE.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001278 Slot = Ty = new DIE(CompTy->getTag());
Jim Laskeyf8913f12006-03-01 17:53:02 +00001279 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1280
1281 switch (CompTy->getTag()) {
Jim Laskey9c4447a2006-03-01 20:39:36 +00001282 case DW_TAG_array_type: {
Jim Laskeyf8913f12006-03-01 17:53:02 +00001283 // Add element type.
1284 if (TypeDesc *FromTy = CompTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001285 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1286 NewType(Context, FromTy, Unit));
Jim Laskeyf8913f12006-03-01 17:53:02 +00001287 }
1288 // Don't emit size attribute.
1289 Size = 0;
1290
1291 // Construct an anonymous type for index type.
1292 DIE *IndexTy = new DIE(DW_TAG_base_type);
1293 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1294 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1295 // Add to context.
Jim Laskey92ae7402006-03-01 18:20:30 +00001296 Context->AddChild(IndexTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001297
1298 // Add subranges to array type.
1299 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1300 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1301 int64_t Lo = SRD->getLo();
1302 int64_t Hi = SRD->getHi();
1303 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1304
1305 // If a range is available.
1306 if (Lo != Hi) {
1307 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1308 // Only add low if non-zero.
Jim Laskey6a3eb012006-03-01 23:52:37 +00001309 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1310 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001311 }
1312 Ty->AddChild(Subrange);
1313 }
1314
1315 break;
1316 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001317 case DW_TAG_structure_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +00001318 case DW_TAG_union_type: {
Jim Laskeyf01e5472006-03-03 15:06:57 +00001319 // FIXME - this is just the basics.
1320 // Add elements to structure type.
1321 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1322 DerivedTypeDesc *MemberDesc = cast<DerivedTypeDesc>(Elements[i]);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001323
1324 // Extract the basic information.
Jim Laskeyf01e5472006-03-03 15:06:57 +00001325 const std::string &Name = MemberDesc->getName();
Jim Laskeyf01e5472006-03-03 15:06:57 +00001326 TypeDesc *MemTy = MemberDesc->getFromType();
1327 uint64_t Size = MemberDesc->getSize();
Chris Lattner2695de42006-03-09 17:48:46 +00001328 uint64_t Align = MemberDesc->getAlign();
Jim Laskeyf01e5472006-03-03 15:06:57 +00001329 uint64_t Offset = MemberDesc->getOffset();
1330
Jim Laskeyb8509c52006-03-23 18:07:55 +00001331 // Construct member debug information entry.
Jim Laskeyf01e5472006-03-03 15:06:57 +00001332 DIE *Member = new DIE(DW_TAG_member);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001333
Jim Laskeyb8509c52006-03-23 18:07:55 +00001334 // Add name if not "".
Jim Laskeyf01e5472006-03-03 15:06:57 +00001335 if (!Name.empty()) Member->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001336 // Add location if available.
1337 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001338
Jim Laskey54689c22006-03-09 13:28:47 +00001339 // Most of the time the field info is the same as the members.
1340 uint64_t FieldSize = Size;
1341 uint64_t FieldAlign = Align;
1342 uint64_t FieldOffset = Offset;
Jim Laskey20c3ed82006-03-07 15:51:33 +00001343
Jim Laskeyf01e5472006-03-03 15:06:57 +00001344 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001345 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1346 NewType(Context, FromTy, Unit));
1347 FieldSize = FromTy->getSize();
1348 FieldAlign = FromTy->getSize();
Jim Laskey20c3ed82006-03-07 15:51:33 +00001349 }
1350
Jim Laskey54689c22006-03-09 13:28:47 +00001351 // Unless we have a bit field.
1352 if (FieldSize != Size) {
1353 // Construct the alignment mask.
1354 uint64_t AlignMask = ~(FieldAlign - 1);
1355 // Determine the high bit + 1 of the declared size.
1356 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1357 // Work backwards to determine the base offset of the field.
1358 FieldOffset = HiMark - FieldSize;
1359 // Now normalize offset to the field.
1360 Offset -= FieldOffset;
1361
Jim Laskey41886992006-04-07 16:34:46 +00001362 // Maybe we need to work from the other end.
Jim Laskey54689c22006-03-09 13:28:47 +00001363 if (TD.isLittleEndian()) Offset = FieldSize - (Offset + Size);
1364
1365 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1366 Member->AddUInt(DW_AT_bit_size, 0, Size);
1367 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
Jim Laskeyf01e5472006-03-03 15:06:57 +00001368 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001369
1370 // Add computation for offset.
1371 DIEBlock *Block = new DIEBlock();
1372 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
Jim Laskey54689c22006-03-09 13:28:47 +00001373 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001374 Block->ComputeSize(*this);
1375 Member->AddBlock(DW_AT_data_member_location, 0, Block);
1376
Jim Laskeyf01e5472006-03-03 15:06:57 +00001377 Ty->AddChild(Member);
1378 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001379 break;
1380 }
Jim Laskey9c4447a2006-03-01 20:39:36 +00001381 case DW_TAG_enumeration_type: {
Jim Laskey6a3eb012006-03-01 23:52:37 +00001382 // Add enumerators to enumeration type.
1383 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1384 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1385 const std::string &Name = ED->getName();
1386 int64_t Value = ED->getValue();
1387 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1388 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1389 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1390 Ty->AddChild(Enumerator);
1391 }
1392
Jim Laskeyf8913f12006-03-01 17:53:02 +00001393 break;
1394 }
1395 default: break;
1396 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001397 }
Jim Laskey434b40b2006-02-23 22:37:30 +00001398
1399 assert(Ty && "Type not supported yet");
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001400
Jim Laskey69906002006-02-24 16:46:40 +00001401 // Add size if non-zero (derived types don't have a size.)
Jim Laskey434b40b2006-02-23 22:37:30 +00001402 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
Jim Laskey69906002006-02-24 16:46:40 +00001403 // Add name if not anonymous or intermediate type.
Jim Laskey434b40b2006-02-23 22:37:30 +00001404 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001405 // Add source line info if available.
1406 AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
Jim Laskey434b40b2006-02-23 22:37:30 +00001407
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001408 // Add to context owner.
Jim Laskey92ae7402006-03-01 18:20:30 +00001409 Context->AddChild(Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001410
1411 return Slot;
1412}
1413
Jim Laskeyb8509c52006-03-23 18:07:55 +00001414/// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey063e7652006-01-17 17:31:53 +00001415///
Jim Laskeybd761842006-02-27 17:27:12 +00001416CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1417 unsigned ID) {
1418 // Construct debug information entry.
1419 DIE *Die = new DIE(DW_TAG_compile_unit);
1420 Die->AddLabel (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0));
1421 Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1422 Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
1423 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1424 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1425 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1426 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1427
Jim Laskeyb8509c52006-03-23 18:07:55 +00001428 // Add debug information entry to descriptor map.
Jim Laskey90c79d72006-03-23 23:02:34 +00001429 DIE *&Slot = getDieMapSlotFor(UnitDesc);
1430 Slot = Die;
Jim Laskeybd761842006-02-27 17:27:12 +00001431
1432 // Construct compile unit.
1433 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1434
1435 // Add Unit to compile unit map.
1436 DescToUnitMap[UnitDesc] = Unit;
1437
1438 return Unit;
1439}
Jim Laskey0420f2a2006-02-22 19:02:11 +00001440
Jim Laskeybd761842006-02-27 17:27:12 +00001441/// FindCompileUnit - Get the compile unit for the given descriptor.
1442///
1443CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1444 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1445 assert(Unit && "Missing compile unit.");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001446 return Unit;
Jim Laskey063e7652006-01-17 17:31:53 +00001447}
1448
Jim Laskey0420f2a2006-02-22 19:02:11 +00001449/// NewGlobalVariable - Add a new global variable DIE.
1450///
1451DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001452 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001453 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1454 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey90c79d72006-03-23 23:02:34 +00001455
1456 // Check for pre-existence.
1457 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1458 if (Slot) return Slot;
1459
Jim Laskey0420f2a2006-02-22 19:02:11 +00001460 // Get the global variable itself.
1461 GlobalVariable *GV = GVD->getGlobalVariable();
1462 // Generate the mangled name.
1463 std::string MangledName = Asm->Mang->getValueName(GV);
1464
1465 // Gather the details (simplify add attribute code.)
1466 const std::string &Name = GVD->getName();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001467
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001468 // Get the global's type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001469 DIE *Type = NewType(Unit->getDie(), GVD->getType(), Unit);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001470
1471 // Create the globale variable DIE.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001472 DIE *VariableDie = new DIE(DW_TAG_variable);
1473 VariableDie->AddString (DW_AT_name, DW_FORM_string, Name);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001474 VariableDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1475 VariableDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001476
1477 // Add source line info if available.
1478 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001479
Jim Laskeyb8509c52006-03-23 18:07:55 +00001480 // Add address.
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001481 DIEBlock *Block = new DIEBlock();
1482 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
1483 Block->AddObjectLabel(DW_FORM_udata, MangledName);
1484 Block->ComputeSize(*this);
1485 VariableDie->AddBlock(DW_AT_location, 0, Block);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001486
1487 // Add to map.
1488 Slot = VariableDie;
1489
1490 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001491 Unit->getDie()->AddChild(VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001492
1493 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001494 // FIXME - need to check external flag.
1495 Unit->AddGlobal(Name, VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001496
1497 return VariableDie;
1498}
1499
1500/// NewSubprogram - Add a new subprogram DIE.
1501///
1502DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001503 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001504 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1505 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001506
Jim Laskey90c79d72006-03-23 23:02:34 +00001507 // Check for pre-existence.
1508 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1509 if (Slot) return Slot;
1510
Jim Laskey0420f2a2006-02-22 19:02:11 +00001511 // Gather the details (simplify add attribute code.)
1512 const std::string &Name = SPD->getName();
Jim Laskey90c79d72006-03-23 23:02:34 +00001513 DIE *Type = NewType(Unit->getDie(), SPD->getType(), Unit);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001514 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001515
Jim Laskey8a8e9752006-02-27 20:37:42 +00001516 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001517 SubprogramDie->AddString (DW_AT_name, DW_FORM_string, Name);
Jim Laskey41886992006-04-07 16:34:46 +00001518 if (Type) {
1519 SubprogramDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1520 }
1521 SubprogramDie->AddUInt (DW_AT_external, DW_FORM_flag, IsExternal);
1522 SubprogramDie->AddUInt (DW_AT_prototyped, DW_FORM_flag, 1);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001523
Jim Laskeyb8509c52006-03-23 18:07:55 +00001524 // Add source line info if available.
1525 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1526
Jim Laskey0420f2a2006-02-22 19:02:11 +00001527 // Add to map.
1528 Slot = SubprogramDie;
1529
1530 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001531 Unit->getDie()->AddChild(SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001532
1533 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001534 Unit->AddGlobal(Name, SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001535
1536 return SubprogramDie;
1537}
1538
Jim Laskeyb8509c52006-03-23 18:07:55 +00001539/// NewScopeVariable - Create a new scope variable.
1540///
1541DIE *DwarfWriter::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1542 // Get the descriptor.
1543 VariableDesc *VD = DV->getDesc();
1544
1545 // Translate tag to proper Dwarf tag. The result variable is dropped for now.
1546 unsigned Tag;
1547 switch (VD->getTag()) {
1548 case DW_TAG_return_variable: return NULL;
1549 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1550 case DW_TAG_auto_variable: // fall thru
1551 default: Tag = DW_TAG_variable; break;
1552 }
1553
1554 // Define variable debug information entry.
1555 DIE *VariableDie = new DIE(Tag);
1556 VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
1557
1558 // Add source line info if available.
1559 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1560
1561 // Add variable type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001562 DIE *Type = NewType(Unit->getDie(), VD->getType(), Unit);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001563 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1564
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001565 // Add variable address.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001566 MachineLocation Location;
Jim Laskey41886992006-04-07 16:34:46 +00001567 RI->getLocation(*MF, DV->getFrameIndex(), Location);
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001568 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001569
1570 return VariableDie;
1571}
1572
1573/// ConstructScope - Construct the components of a scope.
1574///
1575void DwarfWriter::ConstructScope(DebugScope *ParentScope,
1576 DIE *ParentDie, CompileUnit *Unit) {
1577 // Add variables to scope.
1578 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1579 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1580 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1581 if (VariableDie) ParentDie->AddChild(VariableDie);
1582 }
1583
1584 // Add nested scopes.
1585 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1586 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1587 // Define the Scope debug information entry.
1588 DebugScope *Scope = Scopes[j];
1589 // FIXME - Ignore inlined functions for the time being.
1590 if (Scope->getParent()) continue;
1591
1592 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1593
1594 // Add the scope bounds.
1595 if (unsigned StartID = Scope->getStartLabelID()) {
1596 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1597 DWLabel("loc", StartID));
1598 } else {
1599 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1600 DWLabel("func_begin", SubprogramCount));
1601 }
1602 if (unsigned EndID = Scope->getEndLabelID()) {
1603 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1604 DWLabel("loc", EndID));
1605 } else {
1606 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1607 DWLabel("func_end", SubprogramCount));
1608 }
1609
1610 // Add the scope contents.
1611 ConstructScope(Scope, ScopeDie, Unit);
1612 ParentDie->AddChild(ScopeDie);
1613 }
1614}
1615
1616/// ConstructRootScope - Construct the scope for the subprogram.
1617///
1618void DwarfWriter::ConstructRootScope(DebugScope *RootScope) {
1619 // Exit if there is no root scope.
1620 if (!RootScope) return;
1621
1622 // Get the subprogram debug information entry.
1623 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Jim Laskey90c79d72006-03-23 23:02:34 +00001624
1625 // Get the compile unit context.
1626 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey41886992006-04-07 16:34:46 +00001627 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1628
Jim Laskey90c79d72006-03-23 23:02:34 +00001629 // Get the subprogram die.
1630 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001631 assert(SPDie && "Missing subprogram descriptor");
1632
1633 // Add the function bounds.
Jim Laskey6b92b8e2006-04-07 20:44:42 +00001634 SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1635 DWLabel("func_begin", SubprogramCount));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001636 SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1637 DWLabel("func_end", SubprogramCount));
Jim Laskey41886992006-04-07 16:34:46 +00001638 MachineLocation Location(RI->getFrameRegister(*MF));
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001639 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001640
Jim Laskeyb8509c52006-03-23 18:07:55 +00001641 ConstructScope(RootScope, SPDie, Unit);
1642}
1643
Jim Laskey063e7652006-01-17 17:31:53 +00001644/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1645/// tools to recognize the object file contains Dwarf information.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001646///
1647void DwarfWriter::EmitInitial() const {
Jim Laskey063e7652006-01-17 17:31:53 +00001648 // Dwarf sections base addresses.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001649 Asm->SwitchSection(DwarfFrameSection, 0);
1650 EmitLabel("section_frame", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001651 Asm->SwitchSection(DwarfInfoSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001652 EmitLabel("section_info", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001653 EmitLabel("info", 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001654 Asm->SwitchSection(DwarfAbbrevSection, 0);
1655 EmitLabel("section_abbrev", 0);
1656 EmitLabel("abbrev", 0);
1657 Asm->SwitchSection(DwarfARangesSection, 0);
1658 EmitLabel("section_aranges", 0);
1659 Asm->SwitchSection(DwarfMacInfoSection, 0);
1660 EmitLabel("section_macinfo", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001661 Asm->SwitchSection(DwarfLineSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001662 EmitLabel("section_line", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001663 EmitLabel("line", 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001664 Asm->SwitchSection(DwarfLocSection, 0);
1665 EmitLabel("section_loc", 0);
1666 Asm->SwitchSection(DwarfPubNamesSection, 0);
1667 EmitLabel("section_pubnames", 0);
1668 Asm->SwitchSection(DwarfStrSection, 0);
1669 EmitLabel("section_str", 0);
1670 Asm->SwitchSection(DwarfRangesSection, 0);
1671 EmitLabel("section_ranges", 0);
1672
Jim Laskey063e7652006-01-17 17:31:53 +00001673 Asm->SwitchSection(TextSection, 0);
1674 EmitLabel("text_begin", 0);
1675 Asm->SwitchSection(DataSection, 0);
1676 EmitLabel("data_begin", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001677}
1678
Jim Laskey063e7652006-01-17 17:31:53 +00001679/// EmitDIE - Recusively Emits a debug information entry.
1680///
1681void DwarfWriter::EmitDIE(DIE *Die) const {
1682 // Get the abbreviation for this DIE.
1683 unsigned AbbrevID = Die->getAbbrevID();
1684 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey0d086af2006-02-27 12:43:29 +00001685
1686 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001687
1688 // Emit the code (index) for the abbreviation.
1689 EmitULEB128Bytes(AbbrevID);
1690 EOL(std::string("Abbrev [" +
1691 utostr(AbbrevID) +
Jim Laskey0d086af2006-02-27 12:43:29 +00001692 "] 0x" + utohexstr(Die->getOffset()) +
1693 ":0x" + utohexstr(Die->getSize()) + " " +
1694 TagString(Abbrev.getTag())));
Jim Laskey063e7652006-01-17 17:31:53 +00001695
1696 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001697 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001698
1699 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001700 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001701 unsigned Attr = AbbrevData[i].getAttribute();
1702 unsigned Form = AbbrevData[i].getForm();
Jim Laskey063e7652006-01-17 17:31:53 +00001703 assert(Form && "Too many attributes for DIE (check abbreviation)");
1704
1705 switch (Attr) {
1706 case DW_AT_sibling: {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001707 EmitInt32(Die->SiblingOffset());
Jim Laskey063e7652006-01-17 17:31:53 +00001708 break;
1709 }
1710 default: {
1711 // Emit an attribute using the defined form.
1712 Values[i]->EmitValue(*this, Form);
1713 break;
1714 }
1715 }
1716
1717 EOL(AttributeString(Attr));
1718 }
1719
1720 // Emit the DIE children if any.
1721 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1722 const std::vector<DIE *> &Children = Die->getChildren();
1723
Jim Laskey52060a02006-01-24 00:49:18 +00001724 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskey063e7652006-01-17 17:31:53 +00001725 EmitDIE(Children[j]);
1726 }
1727
Jim Laskeyda427fa2006-01-27 20:31:25 +00001728 EmitInt8(0); EOL("End Of Children Mark");
Jim Laskey063e7652006-01-17 17:31:53 +00001729 }
1730}
1731
1732/// SizeAndOffsetDie - Compute the size and offset of a DIE.
1733///
Jim Laskeyb8509c52006-03-23 18:07:55 +00001734unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1735 // Get the children.
1736 const std::vector<DIE *> &Children = Die->getChildren();
1737
1738 // If not last sibling and has children then add sibling offset attribute.
1739 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1740
Jim Laskey0420f2a2006-02-22 19:02:11 +00001741 // Record the abbreviation.
1742 Die->Complete(*this);
1743
Jim Laskey063e7652006-01-17 17:31:53 +00001744 // Get the abbreviation for this DIE.
1745 unsigned AbbrevID = Die->getAbbrevID();
1746 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1747
1748 // Set DIE offset
1749 Die->setOffset(Offset);
1750
1751 // Start the size with the size of abbreviation code.
1752 Offset += SizeULEB128(AbbrevID);
1753
1754 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001755 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001756
1757 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001758 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001759 // Size attribute value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001760 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
Jim Laskey063e7652006-01-17 17:31:53 +00001761 }
1762
1763 // Emit the DIE children if any.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001764 if (!Children.empty()) {
1765 assert(Abbrev.getChildrenFlag() == DW_CHILDREN_yes &&
1766 "Children flag not set");
Jim Laskey063e7652006-01-17 17:31:53 +00001767
Jim Laskey52060a02006-01-24 00:49:18 +00001768 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001769 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
Jim Laskey063e7652006-01-17 17:31:53 +00001770 }
1771
1772 // End of children marker.
1773 Offset += sizeof(int8_t);
1774 }
1775
1776 Die->setSize(Offset - Die->getOffset());
1777 return Offset;
1778}
1779
1780/// SizeAndOffsets - Compute the size and offset of all the DIEs.
1781///
1782void DwarfWriter::SizeAndOffsets() {
Jim Laskey063e7652006-01-17 17:31:53 +00001783
1784 // Process each compile unit.
Jim Laskey52060a02006-01-24 00:49:18 +00001785 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00001786 CompileUnit *Unit = CompileUnits[i];
1787 if (Unit->hasContent()) {
1788 // Compute size of compile unit header
1789 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1790 sizeof(int16_t) + // DWARF version number
1791 sizeof(int32_t) + // Offset Into Abbrev. Section
1792 sizeof(int8_t); // Pointer Size (in bytes)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001793 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
Jim Laskeybd761842006-02-27 17:27:12 +00001794 }
Jim Laskey063e7652006-01-17 17:31:53 +00001795 }
1796}
1797
Jim Laskey41886992006-04-07 16:34:46 +00001798/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1799/// frame.
1800void DwarfWriter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
1801 std::vector<MachineMove *> &Moves) {
1802 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1803 MachineMove *Move = Moves[i];
1804 unsigned LabelID = Move->getLabelID();
1805 const MachineLocation &Dst = Move->getDestination();
1806 const MachineLocation &Src = Move->getSource();
1807
1808 // Advance row if new location.
1809 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
1810 EmitULEB128Bytes(DW_CFA_advance_loc4);
1811 EOL("DW_CFA_advance_loc4");
1812 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
1813 EOL("");
1814
1815 BaseLabelID = LabelID;
1816 BaseLabel = "loc";
1817 }
1818
1819 // If advancing cfa.
1820 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
1821 if (!Src.isRegister()) {
1822 if (Src.getRegister() == MachineLocation::VirtualFP) {
1823 EmitULEB128Bytes(DW_CFA_def_cfa_offset);
1824 EOL("DW_CFA_def_cfa_offset");
1825 } else {
1826 EmitULEB128Bytes(DW_CFA_def_cfa);
1827 EOL("DW_CFA_def_cfa");
1828
1829 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
1830 EOL("Register");
1831 }
1832
1833 EmitULEB128Bytes(Src.getOffset() / RI->getStackDirection());
1834 EOL("Offset");
1835 } else {
1836 }
1837 } else {
1838 }
1839 }
1840}
1841
Jim Laskey063e7652006-01-17 17:31:53 +00001842/// EmitDebugInfo - Emit the debug info section.
1843///
1844void DwarfWriter::EmitDebugInfo() const {
1845 // Start debug info section.
1846 Asm->SwitchSection(DwarfInfoSection, 0);
1847
Jim Laskeybd761842006-02-27 17:27:12 +00001848 // Process each compile unit.
1849 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1850 CompileUnit *Unit = CompileUnits[i];
1851
1852 if (Unit->hasContent()) {
1853 DIE *Die = Unit->getDie();
Jim Laskey0d086af2006-02-27 12:43:29 +00001854 // Emit the compile units header.
Jim Laskeybd761842006-02-27 17:27:12 +00001855 EmitLabel("info_begin", Unit->getID());
Jim Laskey0d086af2006-02-27 12:43:29 +00001856 // Emit size of content not including length itself
Jim Laskeybd761842006-02-27 17:27:12 +00001857 unsigned ContentSize = Die->getSize() +
Jim Laskey0d086af2006-02-27 12:43:29 +00001858 sizeof(int16_t) + // DWARF version number
1859 sizeof(int32_t) + // Offset Into Abbrev. Section
1860 sizeof(int8_t); // Pointer Size (in bytes)
1861
1862 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
1863 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1864 EmitReference("abbrev_begin", 0); EOL("Offset Into Abbrev. Section");
1865 EmitInt8(AddressSize); EOL("Address Size (in bytes)");
1866
Jim Laskeybd761842006-02-27 17:27:12 +00001867 EmitDIE(Die);
1868 EmitLabel("info_end", Unit->getID());
Jim Laskey063e7652006-01-17 17:31:53 +00001869 }
Jim Laskey0d086af2006-02-27 12:43:29 +00001870
1871 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001872 }
1873}
1874
1875/// EmitAbbreviations - Emit the abbreviation section.
1876///
1877void DwarfWriter::EmitAbbreviations() const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001878 // Check to see if it is worth the effort.
1879 if (!Abbreviations.empty()) {
1880 // Start the debug abbrev section.
1881 Asm->SwitchSection(DwarfAbbrevSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001882
Jim Laskeyd18e2892006-01-20 20:34:06 +00001883 EmitLabel("abbrev_begin", 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001884
Jim Laskeyd18e2892006-01-20 20:34:06 +00001885 // For each abbrevation.
1886 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001887 AbbrevID <= NAID; ++AbbrevID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001888 // Get abbreviation data
1889 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey063e7652006-01-17 17:31:53 +00001890
Jim Laskeyd18e2892006-01-20 20:34:06 +00001891 // Emit the abbrevations code (base 1 index.)
1892 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
Jim Laskey063e7652006-01-17 17:31:53 +00001893
Jim Laskeyd18e2892006-01-20 20:34:06 +00001894 // Emit the abbreviations data.
1895 Abbrev.Emit(*this);
Jim Laskey0d086af2006-02-27 12:43:29 +00001896
1897 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001898 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00001899
1900 EmitLabel("abbrev_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00001901
1902 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001903 }
1904}
1905
1906/// EmitDebugLines - Emit source line information.
1907///
1908void DwarfWriter::EmitDebugLines() const {
1909 // Minimum line delta, thus ranging from -10..(255-10).
1910 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1911 // Maximum line delta, thus ranging from -10..(255-10).
1912 const int MaxLineDelta = 255 + MinLineDelta;
1913
1914 // Start the dwarf line section.
1915 Asm->SwitchSection(DwarfLineSection, 0);
1916
1917 // Construct the section header.
1918
1919 EmitDifference("line_end", 0, "line_begin", 0);
1920 EOL("Length of Source Line Info");
1921 EmitLabel("line_begin", 0);
1922
Jim Laskeyda427fa2006-01-27 20:31:25 +00001923 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey063e7652006-01-17 17:31:53 +00001924
1925 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
1926 EOL("Prolog Length");
1927 EmitLabel("line_prolog_begin", 0);
1928
Jim Laskeyda427fa2006-01-27 20:31:25 +00001929 EmitInt8(1); EOL("Minimum Instruction Length");
Jim Laskey063e7652006-01-17 17:31:53 +00001930
Jim Laskeyda427fa2006-01-27 20:31:25 +00001931 EmitInt8(1); EOL("Default is_stmt_start flag");
Jim Laskey063e7652006-01-17 17:31:53 +00001932
Jim Laskeyda427fa2006-01-27 20:31:25 +00001933 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00001934
Jim Laskeyda427fa2006-01-27 20:31:25 +00001935 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00001936
Jim Laskeyda427fa2006-01-27 20:31:25 +00001937 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Jim Laskey063e7652006-01-17 17:31:53 +00001938
1939 // Line number standard opcode encodings argument count
Jim Laskeyda427fa2006-01-27 20:31:25 +00001940 EmitInt8(0); EOL("DW_LNS_copy arg count");
1941 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
1942 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
1943 EmitInt8(1); EOL("DW_LNS_set_file arg count");
1944 EmitInt8(1); EOL("DW_LNS_set_column arg count");
1945 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
1946 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
1947 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
1948 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskey063e7652006-01-17 17:31:53 +00001949
1950 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1951 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1952
1953 // Emit directories.
1954 for (unsigned DirectoryID = 1, NDID = Directories.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001955 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskey063e7652006-01-17 17:31:53 +00001956 EmitString(Directories[DirectoryID]); EOL("Directory");
1957 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00001958 EmitInt8(0); EOL("End of directories");
Jim Laskey063e7652006-01-17 17:31:53 +00001959
1960 // Emit files.
1961 for (unsigned SourceID = 1, NSID = SourceFiles.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001962 SourceID <= NSID; ++SourceID) {
Jim Laskey063e7652006-01-17 17:31:53 +00001963 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1964 EmitString(SourceFile.getName()); EOL("Source");
1965 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
1966 EmitULEB128Bytes(0); EOL("Mod date");
1967 EmitULEB128Bytes(0); EOL("File size");
1968 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00001969 EmitInt8(0); EOL("End of files");
Jim Laskey063e7652006-01-17 17:31:53 +00001970
1971 EmitLabel("line_prolog_end", 0);
1972
1973 // Emit line information
1974 const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
1975
1976 // Dwarf assumes we start with first line of first source file.
1977 unsigned Source = 1;
1978 unsigned Line = 1;
1979
1980 // Construct rows of the address, source, line, column matrix.
Jim Laskey52060a02006-01-24 00:49:18 +00001981 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001982 SourceLineInfo *LineInfo = LineInfos[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00001983
1984 if (DwarfVerbose) {
1985 unsigned SourceID = LineInfo->getSourceID();
1986 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1987 unsigned DirectoryID = SourceFile.getDirectoryID();
1988 O << "\t"
1989 << Asm->CommentString << " "
1990 << Directories[DirectoryID]
1991 << SourceFile.getName() << ":"
1992 << LineInfo->getLine() << "\n";
1993 }
Jim Laskey063e7652006-01-17 17:31:53 +00001994
1995 // Define the line address.
Jim Laskeyda427fa2006-01-27 20:31:25 +00001996 EmitInt8(0); EOL("Extended Op");
1997 EmitInt8(4 + 1); EOL("Op size");
1998 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskeyb8509c52006-03-23 18:07:55 +00001999 EmitReference("loc", LineInfo->getLabelID()); EOL("Location label");
Jim Laskey063e7652006-01-17 17:31:53 +00002000
2001 // If change of source, then switch to the new source.
2002 if (Source != LineInfo->getSourceID()) {
2003 Source = LineInfo->getSourceID();
Jim Laskeyda427fa2006-01-27 20:31:25 +00002004 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
Jim Laskeybd761842006-02-27 17:27:12 +00002005 EmitULEB128Bytes(Source); EOL("New Source");
Jim Laskey063e7652006-01-17 17:31:53 +00002006 }
2007
2008 // If change of line.
2009 if (Line != LineInfo->getLine()) {
2010 // Determine offset.
2011 int Offset = LineInfo->getLine() - Line;
2012 int Delta = Offset - MinLineDelta;
2013
2014 // Update line.
2015 Line = LineInfo->getLine();
2016
2017 // If delta is small enough and in range...
2018 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2019 // ... then use fast opcode.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002020 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
Jim Laskey063e7652006-01-17 17:31:53 +00002021 } else {
2022 // ... otherwise use long hand.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002023 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
Jim Laskey063e7652006-01-17 17:31:53 +00002024 EmitSLEB128Bytes(Offset); EOL("Line Offset");
Jim Laskeyda427fa2006-01-27 20:31:25 +00002025 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
Jim Laskey063e7652006-01-17 17:31:53 +00002026 }
2027 } else {
2028 // Copy the previous row (different address or source)
Jim Laskeyda427fa2006-01-27 20:31:25 +00002029 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
Jim Laskey063e7652006-01-17 17:31:53 +00002030 }
2031 }
2032
Jim Laskey0420f2a2006-02-22 19:02:11 +00002033 // Define last address.
2034 EmitInt8(0); EOL("Extended Op");
2035 EmitInt8(4 + 1); EOL("Op size");
2036 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2037 EmitReference("text_end", 0); EOL("Location label");
2038
Jim Laskey063e7652006-01-17 17:31:53 +00002039 // Mark end of matrix.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002040 EmitInt8(0); EOL("DW_LNE_end_sequence");
Jim Laskey063e7652006-01-17 17:31:53 +00002041 EmitULEB128Bytes(1); O << "\n";
Jim Laskeyda427fa2006-01-27 20:31:25 +00002042 EmitInt8(1); O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002043
2044 EmitLabel("line_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002045
2046 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002047}
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002048
Jim Laskey41886992006-04-07 16:34:46 +00002049/// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002050///
Jim Laskey41886992006-04-07 16:34:46 +00002051void DwarfWriter::EmitInitialDebugFrame() {
2052 // Start the dwarf frame section.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002053 Asm->SwitchSection(DwarfFrameSection, 0);
2054
2055 EmitDifference("frame_common_end", 0,
2056 "frame_common_begin", 0);
2057 EOL("Length of Common Information Entry");
2058
2059 EmitLabel("frame_common_begin", 0);
2060 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2061 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2062 EmitString(""); EOL("CIE Augmentation");
2063 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
Jim Laskey41886992006-04-07 16:34:46 +00002064 EmitSLEB128Bytes(RI->getStackDirection()); EOL("CIE Data Alignment Factor");
2065 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
2066
2067 std::vector<MachineMove *> Moves;
2068 RI->getInitialFrameState(Moves);
2069 EmitFrameMoves(NULL, 0, Moves);
2070 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2071
Jim Laskeyb8509c52006-03-23 18:07:55 +00002072 EmitAlign(2);
2073 EmitLabel("frame_common_end", 0);
2074
2075 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002076}
2077
Jim Laskey41886992006-04-07 16:34:46 +00002078/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2079/// section.
2080void DwarfWriter::EmitFunctionDebugFrame() {
2081 // Start the dwarf frame section.
2082 Asm->SwitchSection(DwarfFrameSection, 0);
2083
2084 EmitDifference("frame_end", SubprogramCount,
2085 "frame_begin", SubprogramCount);
2086 EOL("Length of Frame Information Entry");
2087
2088 EmitLabel("frame_begin", SubprogramCount);
2089
2090 EmitReference("section_frame", 0); EOL("FDE CIE offset");
2091
2092 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2093 EmitDifference("func_end", SubprogramCount,
2094 "func_begin", SubprogramCount);
2095 EOL("FDE address range");
2096
2097 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2098
2099 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2100
2101 EmitAlign(2);
2102 EmitLabel("frame_end", SubprogramCount);
2103
2104 O << "\n";
2105}
2106
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002107/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2108///
2109void DwarfWriter::EmitDebugPubNames() {
Jim Laskeybd761842006-02-27 17:27:12 +00002110 // Start the dwarf pubnames section.
2111 Asm->SwitchSection(DwarfPubNamesSection, 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002112
Jim Laskeybd761842006-02-27 17:27:12 +00002113 // Process each compile unit.
2114 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2115 CompileUnit *Unit = CompileUnits[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00002116
Jim Laskeybd761842006-02-27 17:27:12 +00002117 if (Unit->hasContent()) {
2118 EmitDifference("pubnames_end", Unit->getID(),
2119 "pubnames_begin", Unit->getID());
2120 EOL("Length of Public Names Info");
2121
2122 EmitLabel("pubnames_begin", Unit->getID());
2123
2124 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2125
2126 EmitReference("info_begin", Unit->getID());
2127 EOL("Offset of Compilation Unit Info");
Jim Laskeyd18e2892006-01-20 20:34:06 +00002128
Jim Laskeybd761842006-02-27 17:27:12 +00002129 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2130 EOL("Compilation Unit Length");
2131
2132 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2133
2134 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2135 GE = Globals.end();
2136 GI != GE; ++GI) {
2137 const std::string &Name = GI->first;
2138 DIE * Entity = GI->second;
2139
2140 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2141 EmitString(Name); EOL("External Name");
2142 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002143
Jim Laskeybd761842006-02-27 17:27:12 +00002144 EmitInt32(0); EOL("End Mark");
2145 EmitLabel("pubnames_end", Unit->getID());
2146
2147 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002148 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002149 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002150}
2151
2152/// EmitDebugStr - Emit visible names into a debug str section.
2153///
2154void DwarfWriter::EmitDebugStr() {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002155 // Check to see if it is worth the effort.
2156 if (!StringPool.empty()) {
2157 // Start the dwarf str section.
2158 Asm->SwitchSection(DwarfStrSection, 0);
2159
Jim Laskeyb8509c52006-03-23 18:07:55 +00002160 // For each of strings in the string pool.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002161 for (unsigned StringID = 1, N = StringPool.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002162 StringID <= N; ++StringID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002163 // Emit a label for reference from debug information entries.
2164 EmitLabel("string", StringID);
2165 // Emit the string itself.
2166 const std::string &String = StringPool[StringID];
2167 EmitString(String); O << "\n";
2168 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002169
2170 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002171 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002172}
2173
2174/// EmitDebugLoc - Emit visible names into a debug loc section.
2175///
2176void DwarfWriter::EmitDebugLoc() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002177 // Start the dwarf loc section.
2178 Asm->SwitchSection(DwarfLocSection, 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002179
2180 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002181}
2182
2183/// EmitDebugARanges - Emit visible names into a debug aranges section.
2184///
2185void DwarfWriter::EmitDebugARanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002186 // Start the dwarf aranges section.
2187 Asm->SwitchSection(DwarfARangesSection, 0);
2188
2189 // FIXME - Mock up
Jim Laskeybd761842006-02-27 17:27:12 +00002190#if 0
2191 // Process each compile unit.
2192 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2193 CompileUnit *Unit = CompileUnits[i];
2194
2195 if (Unit->hasContent()) {
2196 // Don't include size of length
2197 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2198
2199 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2200
2201 EmitReference("info_begin", Unit->getID());
2202 EOL("Offset of Compilation Unit Info");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002203
Jim Laskeybd761842006-02-27 17:27:12 +00002204 EmitInt8(AddressSize); EOL("Size of Address");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002205
Jim Laskeybd761842006-02-27 17:27:12 +00002206 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002207
Jim Laskeybd761842006-02-27 17:27:12 +00002208 EmitInt16(0); EOL("Pad (1)");
2209 EmitInt16(0); EOL("Pad (2)");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002210
Jim Laskeybd761842006-02-27 17:27:12 +00002211 // Range 1
2212 EmitReference("text_begin", 0); EOL("Address");
2213 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002214
Jim Laskeybd761842006-02-27 17:27:12 +00002215 EmitInt32(0); EOL("EOM (1)");
2216 EmitInt32(0); EOL("EOM (2)");
2217
2218 O << "\n";
2219 }
2220 }
2221#endif
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002222}
2223
2224/// EmitDebugRanges - Emit visible names into a debug ranges section.
2225///
2226void DwarfWriter::EmitDebugRanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002227 // Start the dwarf ranges section.
2228 Asm->SwitchSection(DwarfRangesSection, 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002229
2230 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002231}
2232
2233/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2234///
2235void DwarfWriter::EmitDebugMacInfo() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002236 // Start the dwarf macinfo section.
2237 Asm->SwitchSection(DwarfMacInfoSection, 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002238
2239 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002240}
Jim Laskey063e7652006-01-17 17:31:53 +00002241
Jim Laskey52060a02006-01-24 00:49:18 +00002242/// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2243/// header file.
2244void DwarfWriter::ConstructCompileUnitDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002245 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
Jim Laskey6e87c0e2006-01-26 21:22:49 +00002246
2247 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00002248 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
Jim Laskey52060a02006-01-24 00:49:18 +00002249 CompileUnits.push_back(Unit);
2250 }
2251}
2252
2253/// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2254/// variables.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002255void DwarfWriter::ConstructGlobalDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002256 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002257 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00002258
2259 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002260 GlobalVariableDesc *GVD = GlobalVariables[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002261 NewGlobalVariable(GVD);
Jim Laskey52060a02006-01-24 00:49:18 +00002262 }
2263}
2264
Jim Laskey0420f2a2006-02-22 19:02:11 +00002265/// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2266/// subprograms.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002267void DwarfWriter::ConstructSubprogramDIEs() {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002268 std::vector<SubprogramDesc *> Subprograms =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002269 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002270
2271 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2272 SubprogramDesc *SPD = Subprograms[i];
2273 NewSubprogram(SPD);
2274 }
2275}
Jim Laskey52060a02006-01-24 00:49:18 +00002276
Jim Laskey063e7652006-01-17 17:31:53 +00002277/// ShouldEmitDwarf - Determine if Dwarf declarations should be made.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002278///
2279bool DwarfWriter::ShouldEmitDwarf() {
2280 // Check if debug info is present.
2281 if (!DebugInfo || !DebugInfo->hasInfo()) return false;
2282
2283 // Make sure initial declarations are made.
2284 if (!didInitial) {
2285 EmitInitial();
Jim Laskeyb8509c52006-03-23 18:07:55 +00002286
Jim Laskey41886992006-04-07 16:34:46 +00002287 // Emit common frame information.
2288 EmitInitialDebugFrame();
2289
Jim Laskeyb8509c52006-03-23 18:07:55 +00002290 // Create all the compile unit DIEs.
2291 ConstructCompileUnitDIEs();
2292
2293 // Create DIEs for each of the externally visible global variables.
2294 ConstructGlobalDIEs();
2295
2296 // Create DIEs for each of the externally visible subprograms.
2297 ConstructSubprogramDIEs();
2298
Jim Laskeyb2efb852006-01-04 22:28:25 +00002299 didInitial = true;
2300 }
2301
2302 // Okay to emit.
2303 return true;
2304}
2305
Jim Laskey063e7652006-01-17 17:31:53 +00002306//===----------------------------------------------------------------------===//
Jim Laskeyd18e2892006-01-20 20:34:06 +00002307// Main entry points.
Jim Laskey063e7652006-01-17 17:31:53 +00002308//
Jim Laskey52060a02006-01-24 00:49:18 +00002309
2310DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
2311: O(OS)
2312, Asm(A)
Jim Laskey41886992006-04-07 16:34:46 +00002313, TD(Asm->TM.getTargetData())
2314, RI(Asm->TM.getRegisterInfo())
Jim Laskeyb8509c52006-03-23 18:07:55 +00002315, M(NULL)
2316, MF(NULL)
Jim Laskey52060a02006-01-24 00:49:18 +00002317, DebugInfo(NULL)
2318, didInitial(false)
Jim Laskeyb8509c52006-03-23 18:07:55 +00002319, SubprogramCount(0)
Jim Laskey52060a02006-01-24 00:49:18 +00002320, CompileUnits()
2321, Abbreviations()
Jim Laskey52060a02006-01-24 00:49:18 +00002322, StringPool()
Jim Laskeybd761842006-02-27 17:27:12 +00002323, DescToUnitMap()
Jim Laskey0420f2a2006-02-22 19:02:11 +00002324, DescToDieMap()
2325, TypeToDieMap()
Jim Laskey52060a02006-01-24 00:49:18 +00002326, AddressSize(sizeof(int32_t))
2327, hasLEB128(false)
2328, hasDotLoc(false)
2329, hasDotFile(false)
2330, needsSet(false)
2331, DwarfAbbrevSection(".debug_abbrev")
2332, DwarfInfoSection(".debug_info")
2333, DwarfLineSection(".debug_line")
2334, DwarfFrameSection(".debug_frame")
2335, DwarfPubNamesSection(".debug_pubnames")
2336, DwarfPubTypesSection(".debug_pubtypes")
2337, DwarfStrSection(".debug_str")
2338, DwarfLocSection(".debug_loc")
2339, DwarfARangesSection(".debug_aranges")
2340, DwarfRangesSection(".debug_ranges")
2341, DwarfMacInfoSection(".debug_macinfo")
2342, TextSection(".text")
2343, DataSection(".data")
2344{}
2345DwarfWriter::~DwarfWriter() {
2346 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2347 delete CompileUnits[i];
Jim Laskey063e7652006-01-17 17:31:53 +00002348 }
Jim Laskey52060a02006-01-24 00:49:18 +00002349}
Jim Laskey063e7652006-01-17 17:31:53 +00002350
Jim Laskey41886992006-04-07 16:34:46 +00002351/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2352/// created it. Set by the target AsmPrinter.
2353void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
2354 DebugInfo = DI;
2355}
2356
Jim Laskey063e7652006-01-17 17:31:53 +00002357/// BeginModule - Emit all Dwarf sections that should come prior to the content.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002358///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002359void DwarfWriter::BeginModule(Module *M) {
2360 this->M = M;
2361
Jim Laskeyb2efb852006-01-04 22:28:25 +00002362 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002363 EOL("Dwarf Begin Module");
Jim Laskeyb2efb852006-01-04 22:28:25 +00002364}
2365
Jim Laskey063e7652006-01-17 17:31:53 +00002366/// EndModule - Emit all Dwarf sections that should come after the content.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002367///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002368void DwarfWriter::EndModule() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002369 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002370 EOL("Dwarf End Module");
2371
2372 // Standard sections final addresses.
Jim Laskeye719a7c2006-01-18 16:54:26 +00002373 Asm->SwitchSection(TextSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002374 EmitLabel("text_end", 0);
Jim Laskeye719a7c2006-01-18 16:54:26 +00002375 Asm->SwitchSection(DataSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002376 EmitLabel("data_end", 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002377
Jim Laskey063e7652006-01-17 17:31:53 +00002378 // Compute DIE offsets and sizes.
2379 SizeAndOffsets();
2380
2381 // Emit all the DIEs into a debug info section
2382 EmitDebugInfo();
2383
2384 // Corresponding abbreviations into a abbrev section.
2385 EmitAbbreviations();
2386
2387 // Emit source line correspondence into a debug line section.
2388 EmitDebugLines();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002389
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002390 // Emit info into a debug pubnames section.
2391 EmitDebugPubNames();
2392
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002393 // Emit info into a debug str section.
2394 EmitDebugStr();
2395
2396 // Emit info into a debug loc section.
2397 EmitDebugLoc();
2398
2399 // Emit info into a debug aranges section.
2400 EmitDebugARanges();
2401
2402 // Emit info into a debug ranges section.
2403 EmitDebugRanges();
2404
2405 // Emit info into a debug macinfo section.
2406 EmitDebugMacInfo();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002407}
2408
Jim Laskey6b92b8e2006-04-07 20:44:42 +00002409/// BeginFunction - Gather pre-function debug information. Assumes being
2410/// emitted immediately after the function entry point.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002411void DwarfWriter::BeginFunction(MachineFunction *MF) {
2412 this->MF = MF;
2413
Jim Laskey41886992006-04-07 16:34:46 +00002414 // Begin accumulating function debug information.
2415 DebugInfo->BeginFunction(MF);
2416
Jim Laskeyb2efb852006-01-04 22:28:25 +00002417 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002418 EOL("Dwarf Begin Function");
Jim Laskeyb8509c52006-03-23 18:07:55 +00002419
Jim Laskey6b92b8e2006-04-07 20:44:42 +00002420 // Assumes in correct section after the entry point.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002421 EmitLabel("func_begin", ++SubprogramCount);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002422}
2423
Jim Laskeye719a7c2006-01-18 16:54:26 +00002424/// EndFunction - Gather and emit post-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002425///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002426void DwarfWriter::EndFunction() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002427 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002428 EOL("Dwarf End Function");
Jim Laskeyb8509c52006-03-23 18:07:55 +00002429
2430 // Define end label for subprogram.
2431 Asm->SwitchSection(TextSection, 0);
2432 EmitLabel("func_end", SubprogramCount);
2433
2434 // Construct scopes for subprogram.
2435 ConstructRootScope(DebugInfo->getRootScope());
Jim Laskey41886992006-04-07 16:34:46 +00002436
2437 // Emit function frame information.
2438 EmitFunctionDebugFrame();
2439
2440 // Clear function debug information.
2441 DebugInfo->EndFunction();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002442}