blob: f77871e8ca9ec53864436f9e4b5d74820744414f [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"
Owen Anderson07000c62006-05-12 06:33:49 +000027#include "llvm/Target/TargetData.h"
Jim Laskey52060a02006-01-24 00:49:18 +000028#include "llvm/Target/TargetMachine.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000029#include "llvm/Target/TargetFrameInfo.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000030
Jim Laskeyb2efb852006-01-04 22:28:25 +000031#include <iostream>
Jim Laskeya7cea6f2006-01-04 13:52:30 +000032
Jim Laskeyb2efb852006-01-04 22:28:25 +000033using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000034using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000035
36static cl::opt<bool>
37DwarfVerbose("dwarf-verbose", cl::Hidden,
Jim Laskey063e7652006-01-17 17:31:53 +000038 cl::desc("Add comments to Dwarf directives."));
39
Jim Laskey0d086af2006-02-27 12:43:29 +000040namespace llvm {
41
Jim Laskey063e7652006-01-17 17:31:53 +000042//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000043// Forward declarations.
44//
Jim Laskey0d086af2006-02-27 12:43:29 +000045class DIE;
Jim Laskey063e7652006-01-17 17:31:53 +000046
Jim Laskey0d086af2006-02-27 12:43:29 +000047//===----------------------------------------------------------------------===//
Jim Laskeyf01e5472006-03-03 15:06:57 +000048// CompileUnit - This dwarf writer support class manages information associate
49// with a source file.
Jim Laskeybd761842006-02-27 17:27:12 +000050class CompileUnit {
51private:
52 CompileUnitDesc *Desc; // Compile unit debug descriptor.
53 unsigned ID; // File ID for source.
Jim Laskeyb8509c52006-03-23 18:07:55 +000054 DIE *Die; // Compile unit debug information entry.
Jim Laskeybd761842006-02-27 17:27:12 +000055 std::map<std::string, DIE *> Globals; // A map of globally visible named
56 // entities for this unit.
Jim Laskey90c79d72006-03-23 23:02:34 +000057 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
58 // Tracks the mapping of unit level
59 // debug informaton descriptors to debug
60 // information entries.
Jim Laskeybd761842006-02-27 17:27:12 +000061
62public:
63 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
64 : Desc(CUD)
65 , ID(I)
66 , Die(D)
67 , Globals()
Jim Laskey90c79d72006-03-23 23:02:34 +000068 , DescToDieMap()
Jim Laskeybd761842006-02-27 17:27:12 +000069 {}
70
71 ~CompileUnit();
72
73 // Accessors.
74 CompileUnitDesc *getDesc() const { return Desc; }
75 unsigned getID() const { return ID; }
76 DIE* getDie() const { return Die; }
77 std::map<std::string, DIE *> &getGlobals() { return Globals; }
78
79 /// hasContent - Return true if this compile unit has something to write out.
80 ///
81 bool hasContent() const;
82
83 /// AddGlobal - Add a new global entity to the compile unit.
84 ///
85 void AddGlobal(const std::string &Name, DIE *Die);
86
Jim Laskey90c79d72006-03-23 23:02:34 +000087 /// getDieMapSlotFor - Returns the debug information entry map slot for the
88 /// specified debug descriptor.
89 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
90 return DescToDieMap[DD];
91 }
Jim Laskeybd761842006-02-27 17:27:12 +000092};
93
94//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000095// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
96// Dwarf abbreviation.
97class DIEAbbrevData {
98private:
99 unsigned Attribute; // Dwarf attribute code.
100 unsigned Form; // Dwarf form code.
101
102public:
103 DIEAbbrevData(unsigned A, unsigned F)
104 : Attribute(A)
105 , Form(F)
106 {}
107
Jim Laskeybd761842006-02-27 17:27:12 +0000108 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000109 unsigned getAttribute() const { return Attribute; }
110 unsigned getForm() const { return Form; }
111
112 /// operator== - Used by DIEAbbrev to locate entry.
113 ///
114 bool operator==(const DIEAbbrevData &DAD) const {
115 return Attribute == DAD.Attribute && Form == DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000116 }
Jim Laskey063e7652006-01-17 17:31:53 +0000117
Jim Laskey0d086af2006-02-27 12:43:29 +0000118 /// operator!= - Used by DIEAbbrev to locate entry.
119 ///
120 bool operator!=(const DIEAbbrevData &DAD) const {
121 return Attribute != DAD.Attribute || Form != DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000122 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000123
124 /// operator< - Used by DIEAbbrev to locate entry.
125 ///
126 bool operator<(const DIEAbbrevData &DAD) const {
127 return Attribute < DAD.Attribute ||
128 (Attribute == DAD.Attribute && Form < DAD.Form);
129 }
130};
Jim Laskey063e7652006-01-17 17:31:53 +0000131
Jim Laskey0d086af2006-02-27 12:43:29 +0000132//===----------------------------------------------------------------------===//
133// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
134// information object.
135class DIEAbbrev {
136private:
137 unsigned Tag; // Dwarf tag code.
138 unsigned ChildrenFlag; // Dwarf children flag.
139 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
Jim Laskey063e7652006-01-17 17:31:53 +0000140
Jim Laskey0d086af2006-02-27 12:43:29 +0000141public:
Jim Laskey063e7652006-01-17 17:31:53 +0000142
Jim Laskey0d086af2006-02-27 12:43:29 +0000143 DIEAbbrev(unsigned T, unsigned C)
144 : Tag(T)
145 , ChildrenFlag(C)
146 , Data()
147 {}
148 ~DIEAbbrev() {}
149
Jim Laskeybd761842006-02-27 17:27:12 +0000150 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000151 unsigned getTag() const { return Tag; }
152 unsigned getChildrenFlag() const { return ChildrenFlag; }
153 const std::vector<DIEAbbrevData> &getData() const { return Data; }
154 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskey063e7652006-01-17 17:31:53 +0000155
Jim Laskey0d086af2006-02-27 12:43:29 +0000156 /// operator== - Used by UniqueVector to locate entry.
157 ///
158 bool operator==(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000159
Jim Laskey0d086af2006-02-27 12:43:29 +0000160 /// operator< - Used by UniqueVector to locate entry.
161 ///
162 bool operator<(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000163
Jim Laskey0d086af2006-02-27 12:43:29 +0000164 /// AddAttribute - Adds another set of attribute information to the
165 /// abbreviation.
166 void AddAttribute(unsigned Attribute, unsigned Form) {
167 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000168 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000169
Jim Laskeyb8509c52006-03-23 18:07:55 +0000170 /// AddFirstAttribute - Adds a set of attribute information to the front
171 /// of the abbreviation.
172 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
173 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
174 }
175
Jim Laskey0d086af2006-02-27 12:43:29 +0000176 /// Emit - Print the abbreviation using the specified Dwarf writer.
177 ///
178 void Emit(const DwarfWriter &DW) const;
179
180#ifndef NDEBUG
181 void print(std::ostream &O);
182 void dump();
183#endif
184};
Jim Laskey063e7652006-01-17 17:31:53 +0000185
Jim Laskey0d086af2006-02-27 12:43:29 +0000186//===----------------------------------------------------------------------===//
187// DIEValue - A debug information entry value.
188//
189class DIEValue {
190public:
191 enum {
192 isInteger,
193 isString,
194 isLabel,
195 isAsIsLabel,
196 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000197 isEntry,
198 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000199 };
200
201 unsigned Type; // Type of the value
202
203 DIEValue(unsigned T) : Type(T) {}
204 virtual ~DIEValue() {}
205
206 // Implement isa/cast/dyncast.
207 static bool classof(const DIEValue *) { return true; }
208
209 /// EmitValue - Emit value via the Dwarf writer.
210 ///
211 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
212
213 /// SizeOf - Return the size of a value in bytes.
214 ///
215 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
216};
Jim Laskey063e7652006-01-17 17:31:53 +0000217
Jim Laskey0d086af2006-02-27 12:43:29 +0000218//===----------------------------------------------------------------------===//
219// DWInteger - An integer value DIE.
220//
221class DIEInteger : public DIEValue {
222private:
223 uint64_t Integer;
224
225public:
226 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000227
Jim Laskey0d086af2006-02-27 12:43:29 +0000228 // Implement isa/cast/dyncast.
229 static bool classof(const DIEInteger *) { return true; }
230 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
231
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000232 /// BestForm - Choose the best form for integer.
233 ///
234 unsigned BestForm(bool IsSigned);
235
Jim Laskey0d086af2006-02-27 12:43:29 +0000236 /// EmitValue - Emit integer of appropriate size.
237 ///
238 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
239
240 /// SizeOf - Determine size of integer value in bytes.
241 ///
242 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
243};
Jim Laskey063e7652006-01-17 17:31:53 +0000244
Jim Laskey0d086af2006-02-27 12:43:29 +0000245//===----------------------------------------------------------------------===//
246// DIEString - A string value DIE.
247//
248struct DIEString : public DIEValue {
249 const std::string String;
250
251 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000252
Jim Laskey0d086af2006-02-27 12:43:29 +0000253 // Implement isa/cast/dyncast.
254 static bool classof(const DIEString *) { return true; }
255 static bool classof(const DIEValue *S) { return S->Type == isString; }
256
257 /// EmitValue - Emit string value.
258 ///
259 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
260
261 /// SizeOf - Determine size of string value in bytes.
262 ///
263 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
264};
Jim Laskey063e7652006-01-17 17:31:53 +0000265
Jim Laskey0d086af2006-02-27 12:43:29 +0000266//===----------------------------------------------------------------------===//
267// DIEDwarfLabel - A Dwarf internal label expression DIE.
268//
269struct DIEDwarfLabel : public DIEValue {
270 const DWLabel Label;
271
272 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000273
Jim Laskey0d086af2006-02-27 12:43:29 +0000274 // Implement isa/cast/dyncast.
275 static bool classof(const DIEDwarfLabel *) { return true; }
276 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
277
278 /// EmitValue - Emit label value.
279 ///
280 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
281
282 /// SizeOf - Determine size of label value in bytes.
283 ///
284 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
285};
Jim Laskey063e7652006-01-17 17:31:53 +0000286
Jim Laskey063e7652006-01-17 17:31:53 +0000287
Jim Laskey0d086af2006-02-27 12:43:29 +0000288//===----------------------------------------------------------------------===//
289// DIEObjectLabel - A label to an object in code or data.
290//
291struct DIEObjectLabel : public DIEValue {
292 const std::string Label;
293
294 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000295
Jim Laskey0d086af2006-02-27 12:43:29 +0000296 // Implement isa/cast/dyncast.
297 static bool classof(const DIEObjectLabel *) { return true; }
298 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
299
300 /// EmitValue - Emit label value.
301 ///
302 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
303
304 /// SizeOf - Determine size of label value in bytes.
305 ///
306 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
307};
Jim Laskey063e7652006-01-17 17:31:53 +0000308
Jim Laskey0d086af2006-02-27 12:43:29 +0000309//===----------------------------------------------------------------------===//
310// DIEDelta - A simple label difference DIE.
311//
312struct DIEDelta : public DIEValue {
313 const DWLabel LabelHi;
314 const DWLabel LabelLo;
315
316 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
317 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000318
Jim Laskey0d086af2006-02-27 12:43:29 +0000319 // Implement isa/cast/dyncast.
320 static bool classof(const DIEDelta *) { return true; }
321 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
322
323 /// EmitValue - Emit delta value.
324 ///
325 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
326
327 /// SizeOf - Determine size of delta value in bytes.
328 ///
329 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
330};
Jim Laskey063e7652006-01-17 17:31:53 +0000331
Jim Laskey0d086af2006-02-27 12:43:29 +0000332//===----------------------------------------------------------------------===//
333// DIEntry - A pointer to a debug information entry.
334//
335struct DIEntry : public DIEValue {
336 DIE *Entry;
337
338 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
339
340 // Implement isa/cast/dyncast.
341 static bool classof(const DIEntry *) { return true; }
342 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
343
Jim Laskeyb8509c52006-03-23 18:07:55 +0000344 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000345 ///
346 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
347
Jim Laskeyb8509c52006-03-23 18:07:55 +0000348 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000349 ///
350 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
351};
352
353//===----------------------------------------------------------------------===//
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000354// DIEBlock - A block of values. Primarily used for location expressions.
355//
356struct DIEBlock : public DIEValue {
357 unsigned Size; // Size in bytes excluding size header.
358 std::vector<unsigned> Forms; // Data forms.
359 std::vector<DIEValue *> Values; // Block values.
360
361 DIEBlock()
362 : DIEValue(isBlock)
363 , Size(0)
364 , Forms()
365 , Values()
366 {}
367 ~DIEBlock();
368
369 // Implement isa/cast/dyncast.
370 static bool classof(const DIEBlock *) { return true; }
371 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
372
373 /// ComputeSize - calculate the size of the block.
374 ///
375 unsigned ComputeSize(DwarfWriter &DW);
376
377 /// BestForm - Choose the best form for data.
378 ///
379 unsigned BestForm();
380
381 /// EmitValue - Emit block data.
382 ///
383 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
384
385 /// SizeOf - Determine size of block data in bytes.
386 ///
387 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
388
389 /// AddUInt - Add an unsigned integer value.
390 ///
391 void AddUInt(unsigned Form, uint64_t Integer);
392
393 /// AddSInt - Add an signed integer value.
394 ///
395 void AddSInt(unsigned Form, int64_t Integer);
396
397 /// AddString - Add a std::string value.
398 ///
399 void AddString(unsigned Form, const std::string &String);
400
401 /// AddLabel - Add a Dwarf label value.
402 ///
403 void AddLabel(unsigned Form, const DWLabel &Label);
404
405 /// AddObjectLabel - Add a non-Dwarf label value.
406 ///
407 void AddObjectLabel(unsigned Form, const std::string &Label);
408
409 /// AddDelta - Add a label delta value.
410 ///
411 void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
412
413 /// AddDIEntry - Add a DIE value.
414 ///
415 void AddDIEntry(unsigned Form, DIE *Entry);
416
417};
418
419//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +0000420// DIE - A structured debug information entry. Has an abbreviation which
421// describes it's organization.
422class DIE {
423private:
424 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
425 unsigned AbbrevID; // Decribing abbreviation ID.
426 unsigned Offset; // Offset in debug info section.
427 unsigned Size; // Size of instance + children.
428 std::vector<DIE *> Children; // Children DIEs.
429 std::vector<DIEValue *> Values; // Attributes values.
430
431public:
432 DIE(unsigned Tag);
433 ~DIE();
434
Jim Laskeybd761842006-02-27 17:27:12 +0000435 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000436 unsigned getAbbrevID() const { return AbbrevID; }
437 unsigned getOffset() const { return Offset; }
438 unsigned getSize() const { return Size; }
439 const std::vector<DIE *> &getChildren() const { return Children; }
440 const std::vector<DIEValue *> &getValues() const { return Values; }
441 void setOffset(unsigned O) { Offset = O; }
442 void setSize(unsigned S) { Size = S; }
443
444 /// SiblingOffset - Return the offset of the debug information entry's
445 /// sibling.
446 unsigned SiblingOffset() const { return Offset + Size; }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000447
448 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
449 ///
450 void AddSiblingOffset();
Jim Laskey0d086af2006-02-27 12:43:29 +0000451
452 /// AddUInt - Add an unsigned integer attribute data and value.
453 ///
454 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
455
456 /// AddSInt - Add an signed integer attribute data and value.
457 ///
458 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
459
460 /// AddString - Add a std::string attribute data and value.
461 ///
462 void AddString(unsigned Attribute, unsigned Form,
463 const std::string &String);
464
465 /// AddLabel - Add a Dwarf label attribute data and value.
466 ///
467 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
468
469 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
470 ///
471 void AddObjectLabel(unsigned Attribute, unsigned Form,
472 const std::string &Label);
473
474 /// AddDelta - Add a label delta attribute data and value.
475 ///
476 void AddDelta(unsigned Attribute, unsigned Form,
477 const DWLabel &Hi, const DWLabel &Lo);
478
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000479 /// AddDIEntry - Add a DIE attribute data and value.
Jim Laskey0d086af2006-02-27 12:43:29 +0000480 ///
481 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
482
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000483 /// AddBlock - Add block data.
484 ///
485 void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
486
Jim Laskey0d086af2006-02-27 12:43:29 +0000487 /// Complete - Indicate that all attributes have been added and
488 /// ready to get an abbreviation ID.
489 ///
490 void Complete(DwarfWriter &DW);
491
492 /// AddChild - Add a child to the DIE.
493 void AddChild(DIE *Child);
494};
495
496} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +0000497
498//===----------------------------------------------------------------------===//
499
Jim Laskeybd761842006-02-27 17:27:12 +0000500CompileUnit::~CompileUnit() {
501 delete Die;
502}
503
504/// hasContent - Return true if this compile unit has something to write out.
505///
506bool CompileUnit::hasContent() const {
507 return !Die->getChildren().empty();
508}
509
510/// AddGlobal - Add a new global entity to the compile unit.
511///
512void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
513 Globals[Name] = Die;
514}
515
516//===----------------------------------------------------------------------===//
517
Jim Laskeyd18e2892006-01-20 20:34:06 +0000518/// operator== - Used by UniqueVector to locate entry.
519///
520bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
521 if (Tag != DA.Tag) return false;
522 if (ChildrenFlag != DA.ChildrenFlag) return false;
523 if (Data.size() != DA.Data.size()) return false;
524
Jim Laskey52060a02006-01-24 00:49:18 +0000525 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000526 if (Data[i] != DA.Data[i]) return false;
Jim Laskeyd18e2892006-01-20 20:34:06 +0000527 }
528
529 return true;
530}
531
532/// operator< - Used by UniqueVector to locate entry.
533///
534bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
535 if (Tag != DA.Tag) return Tag < DA.Tag;
536 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
537 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
538
Jim Laskey52060a02006-01-24 00:49:18 +0000539 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000540 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +0000541 }
542
543 return false;
544}
545
546/// Emit - Print the abbreviation using the specified Dwarf writer.
547///
548void DIEAbbrev::Emit(const DwarfWriter &DW) const {
549 // Emit its Dwarf tag type.
550 DW.EmitULEB128Bytes(Tag);
551 DW.EOL(TagString(Tag));
552
553 // Emit whether it has children DIEs.
554 DW.EmitULEB128Bytes(ChildrenFlag);
555 DW.EOL(ChildrenString(ChildrenFlag));
556
557 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +0000558 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000559 const DIEAbbrevData &AttrData = Data[i];
560
561 // Emit attribute type.
562 DW.EmitULEB128Bytes(AttrData.getAttribute());
563 DW.EOL(AttributeString(AttrData.getAttribute()));
564
565 // Emit form type.
566 DW.EmitULEB128Bytes(AttrData.getForm());
567 DW.EOL(FormEncodingString(AttrData.getForm()));
568 }
569
570 // Mark end of abbreviation.
571 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
572 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
573}
574
575#ifndef NDEBUG
576 void DIEAbbrev::print(std::ostream &O) {
577 O << "Abbreviation @"
Jeff Cohen05ebc8d2006-01-25 17:18:50 +0000578 << std::hex << (intptr_t)this << std::dec
Jim Laskeyd18e2892006-01-20 20:34:06 +0000579 << " "
580 << TagString(Tag)
581 << " "
582 << ChildrenString(ChildrenFlag)
583 << "\n";
584
Jim Laskey52060a02006-01-24 00:49:18 +0000585 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000586 O << " "
587 << AttributeString(Data[i].getAttribute())
588 << " "
589 << FormEncodingString(Data[i].getForm())
590 << "\n";
591 }
592 }
593 void DIEAbbrev::dump() { print(std::cerr); }
594#endif
595
596//===----------------------------------------------------------------------===//
597
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000598/// BestForm - Choose the best form for integer.
599///
600unsigned DIEInteger::BestForm(bool IsSigned) {
601 if (IsSigned) {
602 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
603 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
604 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
605 } else {
606 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
607 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
608 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
609 }
610 return DW_FORM_data8;
611}
612
Jim Laskey063e7652006-01-17 17:31:53 +0000613/// EmitValue - Emit integer of appropriate size.
614///
615void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
616 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +0000617 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000618 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000619 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000620 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000621 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000622 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000623 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000624 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000625 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +0000626 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
627 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +0000628 default: assert(0 && "DIE Value form not supported yet"); break;
629 }
630}
631
632/// SizeOf - Determine size of integer value in bytes.
633///
634unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
635 switch (Form) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000636 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000637 case DW_FORM_ref1: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000638 case DW_FORM_data1: return sizeof(int8_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000639 case DW_FORM_ref2: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000640 case DW_FORM_data2: return sizeof(int16_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000641 case DW_FORM_ref4: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000642 case DW_FORM_data4: return sizeof(int32_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000643 case DW_FORM_ref8: // Fall thru
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000644 case DW_FORM_data8: return sizeof(int64_t);
Jim Laskey40020172006-01-20 21:02:36 +0000645 case DW_FORM_udata: return DW.SizeULEB128(Integer);
646 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +0000647 default: assert(0 && "DIE Value form not supported yet"); break;
648 }
649 return 0;
650}
651
652//===----------------------------------------------------------------------===//
653
654/// EmitValue - Emit string value.
655///
656void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000657 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +0000658}
659
660/// SizeOf - Determine size of string value in bytes.
661///
662unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey40020172006-01-20 21:02:36 +0000663 return String.size() + sizeof(char); // sizeof('\0');
Jim Laskey063e7652006-01-17 17:31:53 +0000664}
665
666//===----------------------------------------------------------------------===//
667
668/// EmitValue - Emit label value.
669///
Jim Laskey52060a02006-01-24 00:49:18 +0000670void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000671 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +0000672}
673
674/// SizeOf - Determine size of label value in bytes.
675///
Jim Laskey52060a02006-01-24 00:49:18 +0000676unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +0000677 return DW.getAddressSize();
678}
679
680//===----------------------------------------------------------------------===//
681
Jim Laskeyd18e2892006-01-20 20:34:06 +0000682/// EmitValue - Emit label value.
683///
Jim Laskey52060a02006-01-24 00:49:18 +0000684void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000685 DW.EmitReference(Label);
686}
687
688/// SizeOf - Determine size of label value in bytes.
689///
Jim Laskey52060a02006-01-24 00:49:18 +0000690unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000691 return DW.getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +0000692}
693
694//===----------------------------------------------------------------------===//
695
Jim Laskey063e7652006-01-17 17:31:53 +0000696/// EmitValue - Emit delta value.
697///
698void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000699 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +0000700}
701
702/// SizeOf - Determine size of delta value in bytes.
703///
704unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
705 return DW.getAddressSize();
706}
707
708//===----------------------------------------------------------------------===//
Jim Laskeyb8509c52006-03-23 18:07:55 +0000709/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000710///
711void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +0000712 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +0000713}
714
Jim Laskeyb8509c52006-03-23 18:07:55 +0000715/// SizeOf - Determine size of debug information entry value in bytes.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000716///
717unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
718 return sizeof(int32_t);
719}
720
721//===----------------------------------------------------------------------===//
722
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000723DIEBlock::~DIEBlock() {
724 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
725 delete Values[i];
726 }
727}
728
729/// ComputeSize - calculate the size of the block.
730///
731unsigned DIEBlock::ComputeSize(DwarfWriter &DW) {
732 Size = 0;
733 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
734 Size += Values[i]->SizeOf(DW, Forms[i]);
735 }
736 return Size;
737}
738
739/// BestForm - Choose the best form for data.
740///
741unsigned DIEBlock::BestForm() {
742 if ((unsigned char)Size == Size) return DW_FORM_block1;
743 if ((unsigned short)Size == Size) return DW_FORM_block2;
744 if ((unsigned int)Size == Size) return DW_FORM_block4;
745 return DW_FORM_block;
746}
747
748/// EmitValue - Emit block data.
749///
750void DIEBlock::EmitValue(const DwarfWriter &DW, unsigned Form) const {
751 switch (Form) {
752 case DW_FORM_block1: DW.EmitInt8(Size); break;
753 case DW_FORM_block2: DW.EmitInt16(Size); break;
754 case DW_FORM_block4: DW.EmitInt32(Size); break;
755 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
756 default: assert(0 && "Improper form for block"); break;
757 }
758 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
759 DW.EOL("");
760 Values[i]->EmitValue(DW, Forms[i]);
761 }
762}
763
764/// SizeOf - Determine size of block data in bytes.
765///
766unsigned DIEBlock::SizeOf(const DwarfWriter &DW, unsigned Form) const {
767 switch (Form) {
768 case DW_FORM_block1: return Size + sizeof(int8_t);
769 case DW_FORM_block2: return Size + sizeof(int16_t);
770 case DW_FORM_block4: return Size + sizeof(int32_t);
771 case DW_FORM_block: return Size + DW.SizeULEB128(Size);
772 default: assert(0 && "Improper form for block"); break;
773 }
774 return 0;
775}
776
777/// AddUInt - Add an unsigned integer value.
778///
779void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
780 DIEInteger *DI = new DIEInteger(Integer);
781 Values.push_back(DI);
782 if (Form == 0) Form = DI->BestForm(false);
783 Forms.push_back(Form);
784}
785
786/// AddSInt - Add an signed integer value.
787///
788void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
789 DIEInteger *DI = new DIEInteger(Integer);
790 Values.push_back(DI);
791 if (Form == 0) Form = DI->BestForm(true);
792 Forms.push_back(Form);
793}
794
795/// AddString - Add a std::string value.
796///
797void DIEBlock::AddString(unsigned Form, const std::string &String) {
798 Values.push_back(new DIEString(String));
799 Forms.push_back(Form);
800}
801
802/// AddLabel - Add a Dwarf label value.
803///
804void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
805 Values.push_back(new DIEDwarfLabel(Label));
806 Forms.push_back(Form);
807}
808
809/// AddObjectLabel - Add a non-Dwarf label value.
810///
811void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
812 Values.push_back(new DIEObjectLabel(Label));
813 Forms.push_back(Form);
814}
815
816/// AddDelta - Add a label delta value.
817///
818void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
819 Values.push_back(new DIEDelta(Hi, Lo));
820 Forms.push_back(Form);
821}
822
823/// AddDIEntry - Add a DIE value.
824///
825void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
826 Values.push_back(new DIEntry(Entry));
827 Forms.push_back(Form);
828}
829
830//===----------------------------------------------------------------------===//
831
Jim Laskey0420f2a2006-02-22 19:02:11 +0000832DIE::DIE(unsigned Tag)
833: Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
Jim Laskeyd18e2892006-01-20 20:34:06 +0000834, AbbrevID(0)
835, Offset(0)
836, Size(0)
Jim Laskeyd18e2892006-01-20 20:34:06 +0000837, Children()
838, Values()
839{}
840
841DIE::~DIE() {
842 if (Abbrev) delete Abbrev;
843
Jim Laskey52060a02006-01-24 00:49:18 +0000844 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000845 delete Children[i];
846 }
847
Jim Laskey52060a02006-01-24 00:49:18 +0000848 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000849 delete Values[j];
850 }
Jim Laskeyd18e2892006-01-20 20:34:06 +0000851}
852
Jim Laskeyb8509c52006-03-23 18:07:55 +0000853/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
854///
855void DIE::AddSiblingOffset() {
856 DIEInteger *DI = new DIEInteger(0);
857 Values.insert(Values.begin(), DI);
858 Abbrev->AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
859}
860
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000861/// AddUInt - Add an unsigned integer attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000862///
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000863void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000864 DIEInteger *DI = new DIEInteger(Integer);
865 Values.push_back(DI);
866 if (!Form) Form = DI->BestForm(false);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000867 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000868}
869
870/// AddSInt - Add an signed integer attribute data and value.
871///
872void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000873 DIEInteger *DI = new DIEInteger(Integer);
874 Values.push_back(DI);
875 if (!Form) Form = DI->BestForm(true);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000876 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000877}
878
879/// AddString - Add a std::string attribute data and value.
880///
881void DIE::AddString(unsigned Attribute, unsigned Form,
882 const std::string &String) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000883 Values.push_back(new DIEString(String));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000884 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000885}
886
887/// AddLabel - Add a Dwarf label attribute data and value.
888///
889void DIE::AddLabel(unsigned Attribute, unsigned Form,
890 const DWLabel &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000891 Values.push_back(new DIEDwarfLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000892 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000893}
894
Jim Laskey52060a02006-01-24 00:49:18 +0000895/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000896///
Jim Laskey52060a02006-01-24 00:49:18 +0000897void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
898 const std::string &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +0000899 Values.push_back(new DIEObjectLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000900 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000901}
902
903/// AddDelta - Add a label delta attribute data and value.
904///
905void DIE::AddDelta(unsigned Attribute, unsigned Form,
906 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000907 Values.push_back(new DIEDelta(Hi, Lo));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000908 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000909}
910
911/// AddDIEntry - Add a DIE attribute data and value.
912///
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000913void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000914 Values.push_back(new DIEntry(Entry));
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000915 Abbrev->AddAttribute(Attribute, Form);
916}
917
918/// AddBlock - Add block data.
919///
920void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
921 assert(Block->Size && "Block size has not been computed");
922 Values.push_back(Block);
923 if (!Form) Form = Block->BestForm();
924 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000925}
926
927/// Complete - Indicate that all attributes have been added and ready to get an
928/// abbreviation ID.
929void DIE::Complete(DwarfWriter &DW) {
930 AbbrevID = DW.NewAbbreviation(Abbrev);
931 delete Abbrev;
932 Abbrev = NULL;
933}
934
935/// AddChild - Add a child to the DIE.
936///
937void DIE::AddChild(DIE *Child) {
Jim Laskey0420f2a2006-02-22 19:02:11 +0000938 assert(Abbrev && "Adding children without an abbreviation");
939 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000940 Children.push_back(Child);
941}
942
943//===----------------------------------------------------------------------===//
944
Jim Laskey0420f2a2006-02-22 19:02:11 +0000945/// DWContext
Jim Laskeyd18e2892006-01-20 20:34:06 +0000946
947//===----------------------------------------------------------------------===//
Jim Laskey063e7652006-01-17 17:31:53 +0000948
949/// PrintHex - Print a value as a hexidecimal value.
950///
951void DwarfWriter::PrintHex(int Value) const {
952 O << "0x" << std::hex << Value << std::dec;
953}
954
955/// EOL - Print a newline character to asm stream. If a comment is present
956/// then it will be printed first. Comments should not contain '\n'.
957void DwarfWriter::EOL(const std::string &Comment) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000958 if (DwarfVerbose && !Comment.empty()) {
Jim Laskey063e7652006-01-17 17:31:53 +0000959 O << "\t"
960 << Asm->CommentString
961 << " "
962 << Comment;
963 }
964 O << "\n";
965}
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000966
Jim Laskeyb8509c52006-03-23 18:07:55 +0000967/// EmitAlign - Print a align directive.
968///
969void DwarfWriter::EmitAlign(unsigned Alignment) const {
970 O << Asm->AlignDirective << Alignment << "\n";
971}
972
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000973/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
Jim Laskey063e7652006-01-17 17:31:53 +0000974/// unsigned leb128 value.
975void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000976 if (hasLEB128) {
977 O << "\t.uleb128\t"
978 << Value;
979 } else {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000980 O << Asm->Data8bitsDirective;
Jim Laskey063e7652006-01-17 17:31:53 +0000981 PrintULEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000982 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000983}
984
985/// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
Jim Laskey063e7652006-01-17 17:31:53 +0000986/// signed leb128 value.
987void DwarfWriter::EmitSLEB128Bytes(int Value) const {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000988 if (hasLEB128) {
989 O << "\t.sleb128\t"
990 << Value;
991 } else {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000992 O << Asm->Data8bitsDirective;
Jim Laskey063e7652006-01-17 17:31:53 +0000993 PrintSLEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000994 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000995}
996
Jim Laskey063e7652006-01-17 17:31:53 +0000997/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +0000998/// representing an unsigned leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +0000999void DwarfWriter::PrintULEB128(unsigned Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001000 do {
1001 unsigned Byte = Value & 0x7f;
1002 Value >>= 7;
1003 if (Value) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001004 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001005 if (Value) O << ", ";
1006 } while (Value);
1007}
1008
Jim Laskey063e7652006-01-17 17:31:53 +00001009/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
1010/// value.
1011unsigned DwarfWriter::SizeULEB128(unsigned Value) {
1012 unsigned Size = 0;
1013 do {
1014 Value >>= 7;
1015 Size += sizeof(int8_t);
1016 } while (Value);
1017 return Size;
1018}
1019
1020/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +00001021/// representing a signed leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +00001022void DwarfWriter::PrintSLEB128(int Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001023 int Sign = Value >> (8 * sizeof(Value) - 1);
1024 bool IsMore;
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001025
Jim Laskeyb2efb852006-01-04 22:28:25 +00001026 do {
1027 unsigned Byte = Value & 0x7f;
1028 Value >>= 7;
1029 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1030 if (IsMore) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001031 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001032 if (IsMore) O << ", ";
1033 } while (IsMore);
1034}
1035
Jim Laskey063e7652006-01-17 17:31:53 +00001036/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
1037/// value.
1038unsigned DwarfWriter::SizeSLEB128(int Value) {
1039 unsigned Size = 0;
1040 int Sign = Value >> (8 * sizeof(Value) - 1);
1041 bool IsMore;
1042
1043 do {
1044 unsigned Byte = Value & 0x7f;
1045 Value >>= 7;
1046 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1047 Size += sizeof(int8_t);
1048 } while (IsMore);
1049 return Size;
1050}
1051
Jim Laskeyda427fa2006-01-27 20:31:25 +00001052/// EmitInt8 - Emit a byte directive and value.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001053///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001054void DwarfWriter::EmitInt8(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001055 O << Asm->Data8bitsDirective;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001056 PrintHex(Value & 0xFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001057}
1058
Jim Laskeyda427fa2006-01-27 20:31:25 +00001059/// EmitInt16 - Emit a short directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001060///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001061void DwarfWriter::EmitInt16(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001062 O << Asm->Data16bitsDirective;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001063 PrintHex(Value & 0xFFFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001064}
1065
Jim Laskeyda427fa2006-01-27 20:31:25 +00001066/// EmitInt32 - Emit a long directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001067///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001068void DwarfWriter::EmitInt32(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001069 O << Asm->Data32bitsDirective;
1070 PrintHex(Value);
1071}
1072
Jim Laskeyda427fa2006-01-27 20:31:25 +00001073/// EmitInt64 - Emit a long long directive and value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001074///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001075void DwarfWriter::EmitInt64(uint64_t Value) const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001076 if (Asm->Data64bitsDirective) {
1077 O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
1078 } else {
Owen Andersona69571c2006-05-03 01:29:57 +00001079 if (TD->isBigEndian()) {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001080 EmitInt32(unsigned(Value >> 32)); O << "\n";
1081 EmitInt32(unsigned(Value));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001082 } else {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001083 EmitInt32(unsigned(Value)); O << "\n";
1084 EmitInt32(unsigned(Value >> 32));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001085 }
1086 }
1087}
1088
Jim Laskey063e7652006-01-17 17:31:53 +00001089/// EmitString - Emit a string with quotes and a null terminator.
1090/// Special characters are emitted properly. (Eg. '\t')
1091void DwarfWriter::EmitString(const std::string &String) const {
1092 O << Asm->AsciiDirective
1093 << "\"";
Jim Laskey52060a02006-01-24 00:49:18 +00001094 for (unsigned i = 0, N = String.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001095 unsigned char C = String[i];
1096
1097 if (!isascii(C) || iscntrl(C)) {
1098 switch(C) {
1099 case '\b': O << "\\b"; break;
1100 case '\f': O << "\\f"; break;
1101 case '\n': O << "\\n"; break;
1102 case '\r': O << "\\r"; break;
1103 case '\t': O << "\\t"; break;
1104 default:
1105 O << '\\';
1106 O << char('0' + (C >> 6));
1107 O << char('0' + (C >> 3));
1108 O << char('0' + (C >> 0));
1109 break;
1110 }
1111 } else if (C == '\"') {
1112 O << "\\\"";
1113 } else if (C == '\'') {
1114 O << "\\\'";
1115 } else {
1116 O << C;
1117 }
1118 }
1119 O << "\\0\"";
1120}
1121
1122/// PrintLabelName - Print label name in form used by Dwarf writer.
1123///
1124void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001125 O << Asm->PrivateGlobalPrefix
1126 << "debug_"
Jim Laskey0420f2a2006-02-22 19:02:11 +00001127 << Tag;
1128 if (Number) O << Number;
Jim Laskeyb2efb852006-01-04 22:28:25 +00001129}
1130
Jim Laskey063e7652006-01-17 17:31:53 +00001131/// EmitLabel - Emit location label for internal use by Dwarf.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001132///
Jim Laskey063e7652006-01-17 17:31:53 +00001133void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
1134 PrintLabelName(Tag, Number);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001135 O << ":\n";
1136}
1137
Jim Laskeye719a7c2006-01-18 16:54:26 +00001138/// EmitReference - Emit a reference to a label.
Jim Laskey063e7652006-01-17 17:31:53 +00001139///
Jim Laskeye719a7c2006-01-18 16:54:26 +00001140void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001141 if (AddressSize == 4)
1142 O << Asm->Data32bitsDirective;
1143 else
1144 O << Asm->Data64bitsDirective;
1145
1146 PrintLabelName(Tag, Number);
1147}
Jim Laskey73683212006-01-21 00:59:54 +00001148void DwarfWriter::EmitReference(const std::string &Name) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001149 if (AddressSize == 4)
1150 O << Asm->Data32bitsDirective;
1151 else
1152 O << Asm->Data64bitsDirective;
1153
1154 O << Name;
1155}
Jim Laskey063e7652006-01-17 17:31:53 +00001156
1157/// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
1158/// assemblers do not accept absolute expressions with data directives, so there
1159/// is an option (needsSet) to use an intermediary 'set' expression.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001160void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
1161 const char *TagLo, unsigned NumberLo) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001162 if (needsSet) {
1163 static unsigned SetCounter = 0;
Jim Laskeyd18e2892006-01-20 20:34:06 +00001164
Jim Laskey063e7652006-01-17 17:31:53 +00001165 O << "\t.set\t";
1166 PrintLabelName("set", SetCounter);
1167 O << ",";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001168 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001169 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001170 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001171 O << "\n";
1172
1173 if (AddressSize == sizeof(int32_t))
1174 O << Asm->Data32bitsDirective;
1175 else
1176 O << Asm->Data64bitsDirective;
1177
1178 PrintLabelName("set", SetCounter);
1179
Jim Laskey52060a02006-01-24 00:49:18 +00001180 ++SetCounter;
Jim Laskey063e7652006-01-17 17:31:53 +00001181 } else {
1182 if (AddressSize == sizeof(int32_t))
1183 O << Asm->Data32bitsDirective;
1184 else
1185 O << Asm->Data64bitsDirective;
1186
Jim Laskeyd18e2892006-01-20 20:34:06 +00001187 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001188 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001189 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001190 }
1191}
1192
Jim Laskeyd18e2892006-01-20 20:34:06 +00001193/// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
Jim Laskey063e7652006-01-17 17:31:53 +00001194///
Jim Laskeyd18e2892006-01-20 20:34:06 +00001195unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
1196 return Abbreviations.insert(*Abbrev);
1197}
1198
1199/// NewString - Add a string to the constant pool and returns a label.
1200///
1201DWLabel DwarfWriter::NewString(const std::string &String) {
1202 unsigned StringID = StringPool.insert(String);
1203 return DWLabel("string", StringID);
1204}
1205
Jim Laskeyb8509c52006-03-23 18:07:55 +00001206/// AddSourceLine - Add location information to specified debug information
1207/// entry.
Jim Laskeyd16f2a72006-06-19 19:49:42 +00001208void DwarfWriter::AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line){
Jim Laskeyb8509c52006-03-23 18:07:55 +00001209 if (File && Line) {
1210 CompileUnit *FileUnit = FindCompileUnit(File);
1211 unsigned FileID = FileUnit->getID();
1212 Die->AddUInt(DW_AT_decl_file, 0, FileID);
1213 Die->AddUInt(DW_AT_decl_line, 0, Line);
1214 }
1215}
1216
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001217/// AddAddress - Add an address attribute to a die based on the location
1218/// provided.
1219void DwarfWriter::AddAddress(DIE *Die, unsigned Attribute,
Jim Laskey41886992006-04-07 16:34:46 +00001220 const MachineLocation &Location) {
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001221 DIEBlock *Block = new DIEBlock();
1222 if (Location.isRegister()) {
Jim Laskey41886992006-04-07 16:34:46 +00001223 Block->AddUInt(DW_FORM_data1,
1224 DW_OP_reg0 + RI->getDwarfRegNum(Location.getRegister()));
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001225 } else {
Jim Laskey41886992006-04-07 16:34:46 +00001226 Block->AddUInt(DW_FORM_data1,
1227 DW_OP_breg0 + RI->getDwarfRegNum(Location.getRegister()));
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001228 Block->AddUInt(DW_FORM_sdata, Location.getOffset());
1229 }
1230 Block->ComputeSize(*this);
1231 Die->AddBlock(Attribute, 0, Block);
1232}
1233
Jim Laskey90c79d72006-03-23 23:02:34 +00001234/// getDieMapSlotFor - Returns the debug information entry map slot for the
1235/// specified debug descriptor.
1236DIE *&DwarfWriter::getDieMapSlotFor(DebugInfoDesc *DD) {
1237 return DescToDieMap[DD];
Jim Laskey0420f2a2006-02-22 19:02:11 +00001238}
Jim Laskey90c79d72006-03-23 23:02:34 +00001239
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001240/// NewType - Create a new type DIE.
1241///
Jim Laskey90c79d72006-03-23 23:02:34 +00001242DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit) {
1243 if (!TyDesc) {
1244 // FIXME - Hack for missing types
1245 DIE *Die = new DIE(DW_TAG_base_type);
1246 Die->AddUInt(DW_AT_byte_size, 0, 4);
1247 Die->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1248 Unit->getDie()->AddChild(Die);
1249 return Die;
1250 }
Jim Laskey92ae7402006-03-01 18:20:30 +00001251
1252 // FIXME - Should handle other contexts that compile units.
Jim Laskey69906002006-02-24 16:46:40 +00001253
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001254 // Check for pre-existence.
Jim Laskey90c79d72006-03-23 23:02:34 +00001255 DIE *&Slot = Unit->getDieMapSlotFor(TyDesc);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001256 if (Slot) return Slot;
1257
1258 // Get core information.
1259 const std::string &Name = TyDesc->getName();
Jim Laskey288fe0f2006-03-01 18:13:05 +00001260 uint64_t Size = TyDesc->getSize() >> 3;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001261
Jim Laskey434b40b2006-02-23 22:37:30 +00001262 DIE *Ty = NULL;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001263
Jim Laskey434b40b2006-02-23 22:37:30 +00001264 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001265 // Fundamental types like int, float, bool
Jim Laskey434b40b2006-02-23 22:37:30 +00001266 Slot = Ty = new DIE(DW_TAG_base_type);
1267 unsigned Encoding = BasicTy->getEncoding();
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001268 Ty->AddUInt(DW_AT_encoding, DW_FORM_data1, Encoding);
Jim Laskey69906002006-02-24 16:46:40 +00001269 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001270 // Create specific DIE.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001271 Slot = Ty = new DIE(DerivedTy->getTag());
Jim Laskey69906002006-02-24 16:46:40 +00001272
1273 // Map to main type, void will not have a type.
1274 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001275 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1276 NewType(Context, FromTy, Unit));
Jim Laskey69906002006-02-24 16:46:40 +00001277 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001278 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
Jim Laskey7089f452006-06-16 13:14:03 +00001279 // Fetch tag
1280 unsigned Tag = CompTy->getTag();
1281
Jim Laskeyf8913f12006-03-01 17:53:02 +00001282 // Create specific DIE.
Jim Laskey7089f452006-06-16 13:14:03 +00001283 Slot = Ty = Tag == DW_TAG_vector_type ? new DIE(DW_TAG_array_type) :
1284 new DIE(Tag);
1285
Jim Laskeyf8913f12006-03-01 17:53:02 +00001286 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1287
Jim Laskey7089f452006-06-16 13:14:03 +00001288 switch (Tag) {
1289 case DW_TAG_vector_type: Ty->AddUInt(DW_AT_GNU_vector, DW_FORM_flag, 1);
1290 // Fall thru
Jim Laskey9c4447a2006-03-01 20:39:36 +00001291 case DW_TAG_array_type: {
Jim Laskeyf8913f12006-03-01 17:53:02 +00001292 // Add element type.
1293 if (TypeDesc *FromTy = CompTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001294 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1295 NewType(Context, FromTy, Unit));
Jim Laskeyf8913f12006-03-01 17:53:02 +00001296 }
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001297
Jim Laskeyf8913f12006-03-01 17:53:02 +00001298 // Don't emit size attribute.
1299 Size = 0;
1300
1301 // Construct an anonymous type for index type.
1302 DIE *IndexTy = new DIE(DW_TAG_base_type);
1303 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1304 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1305 // Add to context.
Jim Laskey92ae7402006-03-01 18:20:30 +00001306 Context->AddChild(IndexTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001307
1308 // Add subranges to array type.
1309 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1310 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1311 int64_t Lo = SRD->getLo();
1312 int64_t Hi = SRD->getHi();
1313 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1314
1315 // If a range is available.
1316 if (Lo != Hi) {
1317 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1318 // Only add low if non-zero.
Jim Laskey6a3eb012006-03-01 23:52:37 +00001319 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1320 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001321 }
1322 Ty->AddChild(Subrange);
1323 }
1324
1325 break;
1326 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001327 case DW_TAG_structure_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +00001328 case DW_TAG_union_type: {
Jim Laskeyf01e5472006-03-03 15:06:57 +00001329 // FIXME - this is just the basics.
1330 // Add elements to structure type.
1331 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1332 DerivedTypeDesc *MemberDesc = cast<DerivedTypeDesc>(Elements[i]);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001333
1334 // Extract the basic information.
Jim Laskeyf01e5472006-03-03 15:06:57 +00001335 const std::string &Name = MemberDesc->getName();
Jim Laskeyf01e5472006-03-03 15:06:57 +00001336 TypeDesc *MemTy = MemberDesc->getFromType();
1337 uint64_t Size = MemberDesc->getSize();
Chris Lattner2695de42006-03-09 17:48:46 +00001338 uint64_t Align = MemberDesc->getAlign();
Jim Laskeyf01e5472006-03-03 15:06:57 +00001339 uint64_t Offset = MemberDesc->getOffset();
1340
Jim Laskeyb8509c52006-03-23 18:07:55 +00001341 // Construct member debug information entry.
Jim Laskeyf01e5472006-03-03 15:06:57 +00001342 DIE *Member = new DIE(DW_TAG_member);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001343
Jim Laskeyb8509c52006-03-23 18:07:55 +00001344 // Add name if not "".
Jim Laskeyf01e5472006-03-03 15:06:57 +00001345 if (!Name.empty()) Member->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001346 // Add location if available.
1347 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001348
Jim Laskey54689c22006-03-09 13:28:47 +00001349 // Most of the time the field info is the same as the members.
1350 uint64_t FieldSize = Size;
1351 uint64_t FieldAlign = Align;
1352 uint64_t FieldOffset = Offset;
Jim Laskey20c3ed82006-03-07 15:51:33 +00001353
Jim Laskeyf01e5472006-03-03 15:06:57 +00001354 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001355 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1356 NewType(Context, FromTy, Unit));
1357 FieldSize = FromTy->getSize();
1358 FieldAlign = FromTy->getSize();
Jim Laskey20c3ed82006-03-07 15:51:33 +00001359 }
1360
Jim Laskey54689c22006-03-09 13:28:47 +00001361 // Unless we have a bit field.
1362 if (FieldSize != Size) {
1363 // Construct the alignment mask.
1364 uint64_t AlignMask = ~(FieldAlign - 1);
1365 // Determine the high bit + 1 of the declared size.
1366 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1367 // Work backwards to determine the base offset of the field.
1368 FieldOffset = HiMark - FieldSize;
1369 // Now normalize offset to the field.
1370 Offset -= FieldOffset;
1371
Jim Laskey41886992006-04-07 16:34:46 +00001372 // Maybe we need to work from the other end.
Owen Andersona69571c2006-05-03 01:29:57 +00001373 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Jim Laskey54689c22006-03-09 13:28:47 +00001374
1375 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1376 Member->AddUInt(DW_AT_bit_size, 0, Size);
1377 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
Jim Laskeyf01e5472006-03-03 15:06:57 +00001378 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001379
1380 // Add computation for offset.
1381 DIEBlock *Block = new DIEBlock();
1382 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
Jim Laskey54689c22006-03-09 13:28:47 +00001383 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001384 Block->ComputeSize(*this);
1385 Member->AddBlock(DW_AT_data_member_location, 0, Block);
1386
Jim Laskeyf01e5472006-03-03 15:06:57 +00001387 Ty->AddChild(Member);
1388 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001389 break;
1390 }
Jim Laskey9c4447a2006-03-01 20:39:36 +00001391 case DW_TAG_enumeration_type: {
Jim Laskey6a3eb012006-03-01 23:52:37 +00001392 // Add enumerators to enumeration type.
1393 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1394 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1395 const std::string &Name = ED->getName();
1396 int64_t Value = ED->getValue();
1397 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1398 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1399 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1400 Ty->AddChild(Enumerator);
1401 }
1402
Jim Laskeyf8913f12006-03-01 17:53:02 +00001403 break;
1404 }
Jim Laskey650f6092006-06-20 19:41:06 +00001405 case DW_TAG_subroutine_type: {
1406 // Add prototype flag.
1407 Ty->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
1408 // Add return type.
1409 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1410 NewType(Context, cast<TypeDesc>(Elements[0]), Unit));
1411
1412 // Add arguments.
1413 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1414 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1415 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1416 NewType(Context, cast<TypeDesc>(Elements[i]), Unit));
1417 Ty->AddChild(Arg);
1418 }
1419
1420 break;
1421 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001422 default: break;
1423 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001424 }
Jim Laskey434b40b2006-02-23 22:37:30 +00001425
1426 assert(Ty && "Type not supported yet");
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001427
Jim Laskey69906002006-02-24 16:46:40 +00001428 // Add size if non-zero (derived types don't have a size.)
Jim Laskey434b40b2006-02-23 22:37:30 +00001429 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
Jim Laskey69906002006-02-24 16:46:40 +00001430 // Add name if not anonymous or intermediate type.
Jim Laskey434b40b2006-02-23 22:37:30 +00001431 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001432 // Add source line info if available.
1433 AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
Jim Laskey434b40b2006-02-23 22:37:30 +00001434
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001435 // Add to context owner.
Jim Laskey92ae7402006-03-01 18:20:30 +00001436 Context->AddChild(Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001437
1438 return Slot;
1439}
1440
Jim Laskeyb8509c52006-03-23 18:07:55 +00001441/// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey063e7652006-01-17 17:31:53 +00001442///
Jim Laskeybd761842006-02-27 17:27:12 +00001443CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1444 unsigned ID) {
1445 // Construct debug information entry.
1446 DIE *Die = new DIE(DW_TAG_compile_unit);
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001447 Die->AddDelta (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0),
1448 DWLabel("section_line", 0));
Jim Laskeybd761842006-02-27 17:27:12 +00001449 Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1450 Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
1451 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1452 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1453 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1454 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1455
Jim Laskeyb8509c52006-03-23 18:07:55 +00001456 // Add debug information entry to descriptor map.
Jim Laskey90c79d72006-03-23 23:02:34 +00001457 DIE *&Slot = getDieMapSlotFor(UnitDesc);
1458 Slot = Die;
Jim Laskeybd761842006-02-27 17:27:12 +00001459
1460 // Construct compile unit.
1461 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1462
1463 // Add Unit to compile unit map.
1464 DescToUnitMap[UnitDesc] = Unit;
1465
1466 return Unit;
1467}
Jim Laskey0420f2a2006-02-22 19:02:11 +00001468
Jim Laskeybd761842006-02-27 17:27:12 +00001469/// FindCompileUnit - Get the compile unit for the given descriptor.
1470///
1471CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1472 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1473 assert(Unit && "Missing compile unit.");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001474 return Unit;
Jim Laskey063e7652006-01-17 17:31:53 +00001475}
1476
Jim Laskey0420f2a2006-02-22 19:02:11 +00001477/// NewGlobalVariable - Add a new global variable DIE.
1478///
1479DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001480 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001481 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1482 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey90c79d72006-03-23 23:02:34 +00001483
1484 // Check for pre-existence.
1485 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1486 if (Slot) return Slot;
1487
Jim Laskey0420f2a2006-02-22 19:02:11 +00001488 // Get the global variable itself.
1489 GlobalVariable *GV = GVD->getGlobalVariable();
1490 // Generate the mangled name.
1491 std::string MangledName = Asm->Mang->getValueName(GV);
1492
1493 // Gather the details (simplify add attribute code.)
1494 const std::string &Name = GVD->getName();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001495
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001496 // Get the global's type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001497 DIE *Type = NewType(Unit->getDie(), GVD->getType(), Unit);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001498
1499 // Create the globale variable DIE.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001500 DIE *VariableDie = new DIE(DW_TAG_variable);
1501 VariableDie->AddString (DW_AT_name, DW_FORM_string, Name);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001502 VariableDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1503 VariableDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001504
1505 // Add source line info if available.
1506 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001507
Jim Laskeyb8509c52006-03-23 18:07:55 +00001508 // Add address.
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001509 DIEBlock *Block = new DIEBlock();
1510 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
1511 Block->AddObjectLabel(DW_FORM_udata, MangledName);
1512 Block->ComputeSize(*this);
1513 VariableDie->AddBlock(DW_AT_location, 0, Block);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001514
1515 // Add to map.
1516 Slot = VariableDie;
1517
1518 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001519 Unit->getDie()->AddChild(VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001520
1521 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001522 // FIXME - need to check external flag.
1523 Unit->AddGlobal(Name, VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001524
1525 return VariableDie;
1526}
1527
1528/// NewSubprogram - Add a new subprogram DIE.
1529///
1530DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001531 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001532 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1533 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001534
Jim Laskey90c79d72006-03-23 23:02:34 +00001535 // Check for pre-existence.
1536 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1537 if (Slot) return Slot;
1538
Jim Laskey0420f2a2006-02-22 19:02:11 +00001539 // Gather the details (simplify add attribute code.)
1540 const std::string &Name = SPD->getName();
Jim Laskey90c79d72006-03-23 23:02:34 +00001541 DIE *Type = NewType(Unit->getDie(), SPD->getType(), Unit);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001542 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
Jim Laskey0420f2a2006-02-22 19:02:11 +00001543
Jim Laskey8a8e9752006-02-27 20:37:42 +00001544 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001545 SubprogramDie->AddString (DW_AT_name, DW_FORM_string, Name);
Jim Laskey41886992006-04-07 16:34:46 +00001546 if (Type) {
1547 SubprogramDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1548 }
1549 SubprogramDie->AddUInt (DW_AT_external, DW_FORM_flag, IsExternal);
1550 SubprogramDie->AddUInt (DW_AT_prototyped, DW_FORM_flag, 1);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001551
Jim Laskeyb8509c52006-03-23 18:07:55 +00001552 // Add source line info if available.
1553 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1554
Jim Laskey0420f2a2006-02-22 19:02:11 +00001555 // Add to map.
1556 Slot = SubprogramDie;
1557
1558 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00001559 Unit->getDie()->AddChild(SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001560
1561 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00001562 Unit->AddGlobal(Name, SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001563
1564 return SubprogramDie;
1565}
1566
Jim Laskeyb8509c52006-03-23 18:07:55 +00001567/// NewScopeVariable - Create a new scope variable.
1568///
1569DIE *DwarfWriter::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1570 // Get the descriptor.
1571 VariableDesc *VD = DV->getDesc();
1572
1573 // Translate tag to proper Dwarf tag. The result variable is dropped for now.
1574 unsigned Tag;
1575 switch (VD->getTag()) {
1576 case DW_TAG_return_variable: return NULL;
1577 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1578 case DW_TAG_auto_variable: // fall thru
1579 default: Tag = DW_TAG_variable; break;
1580 }
1581
1582 // Define variable debug information entry.
1583 DIE *VariableDie = new DIE(Tag);
1584 VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
1585
1586 // Add source line info if available.
1587 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1588
1589 // Add variable type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001590 DIE *Type = NewType(Unit->getDie(), VD->getType(), Unit);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001591 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1592
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001593 // Add variable address.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001594 MachineLocation Location;
Jim Laskey41886992006-04-07 16:34:46 +00001595 RI->getLocation(*MF, DV->getFrameIndex(), Location);
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001596 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001597
1598 return VariableDie;
1599}
1600
1601/// ConstructScope - Construct the components of a scope.
1602///
1603void DwarfWriter::ConstructScope(DebugScope *ParentScope,
1604 DIE *ParentDie, CompileUnit *Unit) {
1605 // Add variables to scope.
1606 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1607 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1608 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1609 if (VariableDie) ParentDie->AddChild(VariableDie);
1610 }
1611
1612 // Add nested scopes.
1613 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1614 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1615 // Define the Scope debug information entry.
1616 DebugScope *Scope = Scopes[j];
1617 // FIXME - Ignore inlined functions for the time being.
Jim Laskeyd16f2a72006-06-19 19:49:42 +00001618 if (!Scope->getParent()) continue;
Jim Laskeyb8509c52006-03-23 18:07:55 +00001619
1620 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1621
1622 // Add the scope bounds.
1623 if (unsigned StartID = Scope->getStartLabelID()) {
1624 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1625 DWLabel("loc", StartID));
1626 } else {
1627 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1628 DWLabel("func_begin", SubprogramCount));
1629 }
1630 if (unsigned EndID = Scope->getEndLabelID()) {
1631 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1632 DWLabel("loc", EndID));
1633 } else {
1634 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1635 DWLabel("func_end", SubprogramCount));
1636 }
1637
1638 // Add the scope contents.
1639 ConstructScope(Scope, ScopeDie, Unit);
1640 ParentDie->AddChild(ScopeDie);
1641 }
1642}
1643
1644/// ConstructRootScope - Construct the scope for the subprogram.
1645///
1646void DwarfWriter::ConstructRootScope(DebugScope *RootScope) {
1647 // Exit if there is no root scope.
1648 if (!RootScope) return;
1649
1650 // Get the subprogram debug information entry.
1651 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Jim Laskey90c79d72006-03-23 23:02:34 +00001652
1653 // Get the compile unit context.
1654 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey41886992006-04-07 16:34:46 +00001655 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1656
Jim Laskey90c79d72006-03-23 23:02:34 +00001657 // Get the subprogram die.
1658 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001659 assert(SPDie && "Missing subprogram descriptor");
1660
1661 // Add the function bounds.
Jim Laskey6b92b8e2006-04-07 20:44:42 +00001662 SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1663 DWLabel("func_begin", SubprogramCount));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001664 SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1665 DWLabel("func_end", SubprogramCount));
Jim Laskey41886992006-04-07 16:34:46 +00001666 MachineLocation Location(RI->getFrameRegister(*MF));
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001667 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001668
Jim Laskeyb8509c52006-03-23 18:07:55 +00001669 ConstructScope(RootScope, SPDie, Unit);
1670}
1671
Jim Laskey063e7652006-01-17 17:31:53 +00001672/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1673/// tools to recognize the object file contains Dwarf information.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001674///
Jim Laskey014f98c2006-06-14 11:35:03 +00001675void DwarfWriter::EmitInitial() {
1676 // Check to see if we already emitted intial headers.
1677 if (didInitial) return;
1678 didInitial = true;
1679
Jim Laskey063e7652006-01-17 17:31:53 +00001680 // Dwarf sections base addresses.
Chris Lattner4632d7a2006-05-09 04:59:56 +00001681 Asm->SwitchToDataSection(DwarfFrameSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001682 EmitLabel("section_frame", 0);
Chris Lattner4632d7a2006-05-09 04:59:56 +00001683 Asm->SwitchToDataSection(DwarfInfoSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001684 EmitLabel("section_info", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001685 EmitLabel("info", 0);
Chris Lattner4632d7a2006-05-09 04:59:56 +00001686 Asm->SwitchToDataSection(DwarfAbbrevSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001687 EmitLabel("section_abbrev", 0);
1688 EmitLabel("abbrev", 0);
Chris Lattner4632d7a2006-05-09 04:59:56 +00001689 Asm->SwitchToDataSection(DwarfARangesSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001690 EmitLabel("section_aranges", 0);
Chris Lattner4632d7a2006-05-09 04:59:56 +00001691 Asm->SwitchToDataSection(DwarfMacInfoSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001692 EmitLabel("section_macinfo", 0);
Chris Lattner4632d7a2006-05-09 04:59:56 +00001693 Asm->SwitchToDataSection(DwarfLineSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001694 EmitLabel("section_line", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001695 EmitLabel("line", 0);
Chris Lattner4632d7a2006-05-09 04:59:56 +00001696 Asm->SwitchToDataSection(DwarfLocSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001697 EmitLabel("section_loc", 0);
Chris Lattner4632d7a2006-05-09 04:59:56 +00001698 Asm->SwitchToDataSection(DwarfPubNamesSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001699 EmitLabel("section_pubnames", 0);
Chris Lattner4632d7a2006-05-09 04:59:56 +00001700 Asm->SwitchToDataSection(DwarfStrSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001701 EmitLabel("section_str", 0);
Chris Lattner4632d7a2006-05-09 04:59:56 +00001702 Asm->SwitchToDataSection(DwarfRangesSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001703 EmitLabel("section_ranges", 0);
1704
Chris Lattner4632d7a2006-05-09 04:59:56 +00001705 Asm->SwitchToDataSection(TextSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001706 EmitLabel("text_begin", 0);
Chris Lattner4632d7a2006-05-09 04:59:56 +00001707 Asm->SwitchToDataSection(DataSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001708 EmitLabel("data_begin", 0);
Jim Laskey014f98c2006-06-14 11:35:03 +00001709
1710 // Emit common frame information.
1711 EmitInitialDebugFrame();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001712}
1713
Jim Laskey063e7652006-01-17 17:31:53 +00001714/// EmitDIE - Recusively Emits a debug information entry.
1715///
1716void DwarfWriter::EmitDIE(DIE *Die) const {
1717 // Get the abbreviation for this DIE.
1718 unsigned AbbrevID = Die->getAbbrevID();
1719 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey0d086af2006-02-27 12:43:29 +00001720
1721 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001722
1723 // Emit the code (index) for the abbreviation.
1724 EmitULEB128Bytes(AbbrevID);
1725 EOL(std::string("Abbrev [" +
1726 utostr(AbbrevID) +
Jim Laskey0d086af2006-02-27 12:43:29 +00001727 "] 0x" + utohexstr(Die->getOffset()) +
1728 ":0x" + utohexstr(Die->getSize()) + " " +
1729 TagString(Abbrev.getTag())));
Jim Laskey063e7652006-01-17 17:31:53 +00001730
1731 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001732 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001733
1734 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001735 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001736 unsigned Attr = AbbrevData[i].getAttribute();
1737 unsigned Form = AbbrevData[i].getForm();
Jim Laskey063e7652006-01-17 17:31:53 +00001738 assert(Form && "Too many attributes for DIE (check abbreviation)");
1739
1740 switch (Attr) {
1741 case DW_AT_sibling: {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001742 EmitInt32(Die->SiblingOffset());
Jim Laskey063e7652006-01-17 17:31:53 +00001743 break;
1744 }
1745 default: {
1746 // Emit an attribute using the defined form.
1747 Values[i]->EmitValue(*this, Form);
1748 break;
1749 }
1750 }
1751
1752 EOL(AttributeString(Attr));
1753 }
1754
1755 // Emit the DIE children if any.
1756 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1757 const std::vector<DIE *> &Children = Die->getChildren();
1758
Jim Laskey52060a02006-01-24 00:49:18 +00001759 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskey063e7652006-01-17 17:31:53 +00001760 EmitDIE(Children[j]);
1761 }
1762
Jim Laskeyda427fa2006-01-27 20:31:25 +00001763 EmitInt8(0); EOL("End Of Children Mark");
Jim Laskey063e7652006-01-17 17:31:53 +00001764 }
1765}
1766
1767/// SizeAndOffsetDie - Compute the size and offset of a DIE.
1768///
Jim Laskeyb8509c52006-03-23 18:07:55 +00001769unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1770 // Get the children.
1771 const std::vector<DIE *> &Children = Die->getChildren();
1772
1773 // If not last sibling and has children then add sibling offset attribute.
1774 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1775
Jim Laskey0420f2a2006-02-22 19:02:11 +00001776 // Record the abbreviation.
1777 Die->Complete(*this);
1778
Jim Laskey063e7652006-01-17 17:31:53 +00001779 // Get the abbreviation for this DIE.
1780 unsigned AbbrevID = Die->getAbbrevID();
1781 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1782
1783 // Set DIE offset
1784 Die->setOffset(Offset);
1785
1786 // Start the size with the size of abbreviation code.
1787 Offset += SizeULEB128(AbbrevID);
1788
1789 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001790 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001791
1792 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001793 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001794 // Size attribute value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001795 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
Jim Laskey063e7652006-01-17 17:31:53 +00001796 }
1797
1798 // Emit the DIE children if any.
Jim Laskeyb8509c52006-03-23 18:07:55 +00001799 if (!Children.empty()) {
1800 assert(Abbrev.getChildrenFlag() == DW_CHILDREN_yes &&
1801 "Children flag not set");
Jim Laskey063e7652006-01-17 17:31:53 +00001802
Jim Laskey52060a02006-01-24 00:49:18 +00001803 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001804 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
Jim Laskey063e7652006-01-17 17:31:53 +00001805 }
1806
1807 // End of children marker.
1808 Offset += sizeof(int8_t);
1809 }
1810
1811 Die->setSize(Offset - Die->getOffset());
1812 return Offset;
1813}
1814
1815/// SizeAndOffsets - Compute the size and offset of all the DIEs.
1816///
1817void DwarfWriter::SizeAndOffsets() {
Jim Laskey063e7652006-01-17 17:31:53 +00001818
1819 // Process each compile unit.
Jim Laskey52060a02006-01-24 00:49:18 +00001820 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00001821 CompileUnit *Unit = CompileUnits[i];
1822 if (Unit->hasContent()) {
1823 // Compute size of compile unit header
1824 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1825 sizeof(int16_t) + // DWARF version number
1826 sizeof(int32_t) + // Offset Into Abbrev. Section
1827 sizeof(int8_t); // Pointer Size (in bytes)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001828 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
Jim Laskeybd761842006-02-27 17:27:12 +00001829 }
Jim Laskey063e7652006-01-17 17:31:53 +00001830 }
1831}
1832
Jim Laskey41886992006-04-07 16:34:46 +00001833/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1834/// frame.
1835void DwarfWriter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
1836 std::vector<MachineMove *> &Moves) {
1837 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1838 MachineMove *Move = Moves[i];
1839 unsigned LabelID = Move->getLabelID();
1840 const MachineLocation &Dst = Move->getDestination();
1841 const MachineLocation &Src = Move->getSource();
1842
1843 // Advance row if new location.
1844 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
1845 EmitULEB128Bytes(DW_CFA_advance_loc4);
1846 EOL("DW_CFA_advance_loc4");
1847 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
1848 EOL("");
1849
1850 BaseLabelID = LabelID;
1851 BaseLabel = "loc";
1852 }
1853
1854 // If advancing cfa.
1855 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
1856 if (!Src.isRegister()) {
1857 if (Src.getRegister() == MachineLocation::VirtualFP) {
1858 EmitULEB128Bytes(DW_CFA_def_cfa_offset);
1859 EOL("DW_CFA_def_cfa_offset");
1860 } else {
1861 EmitULEB128Bytes(DW_CFA_def_cfa);
1862 EOL("DW_CFA_def_cfa");
1863
1864 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
1865 EOL("Register");
1866 }
Jim Laskey1069fbd2006-04-10 23:09:19 +00001867
1868 int stackGrowth =
1869 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1870 TargetFrameInfo::StackGrowsUp ?
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001871 AddressSize : -AddressSize;
Jim Laskey1069fbd2006-04-10 23:09:19 +00001872
1873 EmitULEB128Bytes(Src.getOffset() / stackGrowth);
Jim Laskey41886992006-04-07 16:34:46 +00001874 EOL("Offset");
1875 } else {
1876 }
1877 } else {
1878 }
1879 }
1880}
1881
Jim Laskey063e7652006-01-17 17:31:53 +00001882/// EmitDebugInfo - Emit the debug info section.
1883///
1884void DwarfWriter::EmitDebugInfo() const {
1885 // Start debug info section.
Chris Lattner4632d7a2006-05-09 04:59:56 +00001886 Asm->SwitchToDataSection(DwarfInfoSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001887
Jim Laskeybd761842006-02-27 17:27:12 +00001888 // Process each compile unit.
1889 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1890 CompileUnit *Unit = CompileUnits[i];
1891
1892 if (Unit->hasContent()) {
1893 DIE *Die = Unit->getDie();
Jim Laskey0d086af2006-02-27 12:43:29 +00001894 // Emit the compile units header.
Jim Laskeybd761842006-02-27 17:27:12 +00001895 EmitLabel("info_begin", Unit->getID());
Jim Laskey0d086af2006-02-27 12:43:29 +00001896 // Emit size of content not including length itself
Jim Laskeybd761842006-02-27 17:27:12 +00001897 unsigned ContentSize = Die->getSize() +
Jim Laskey0d086af2006-02-27 12:43:29 +00001898 sizeof(int16_t) + // DWARF version number
1899 sizeof(int32_t) + // Offset Into Abbrev. Section
1900 sizeof(int8_t); // Pointer Size (in bytes)
1901
1902 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
1903 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001904 EmitDifference("abbrev_begin", 0, "section_abbrev", 0);
1905 EOL("Offset Into Abbrev. Section");
Jim Laskey0d086af2006-02-27 12:43:29 +00001906 EmitInt8(AddressSize); EOL("Address Size (in bytes)");
1907
Jim Laskeybd761842006-02-27 17:27:12 +00001908 EmitDIE(Die);
1909 EmitLabel("info_end", Unit->getID());
Jim Laskey063e7652006-01-17 17:31:53 +00001910 }
Jim Laskey0d086af2006-02-27 12:43:29 +00001911
1912 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001913 }
1914}
1915
1916/// EmitAbbreviations - Emit the abbreviation section.
1917///
1918void DwarfWriter::EmitAbbreviations() const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001919 // Check to see if it is worth the effort.
1920 if (!Abbreviations.empty()) {
1921 // Start the debug abbrev section.
Chris Lattner4632d7a2006-05-09 04:59:56 +00001922 Asm->SwitchToDataSection(DwarfAbbrevSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001923
Jim Laskeyd18e2892006-01-20 20:34:06 +00001924 EmitLabel("abbrev_begin", 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001925
Jim Laskeyd18e2892006-01-20 20:34:06 +00001926 // For each abbrevation.
1927 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001928 AbbrevID <= NAID; ++AbbrevID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001929 // Get abbreviation data
1930 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey063e7652006-01-17 17:31:53 +00001931
Jim Laskeyd18e2892006-01-20 20:34:06 +00001932 // Emit the abbrevations code (base 1 index.)
1933 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
Jim Laskey063e7652006-01-17 17:31:53 +00001934
Jim Laskeyd18e2892006-01-20 20:34:06 +00001935 // Emit the abbreviations data.
1936 Abbrev.Emit(*this);
Jim Laskey0d086af2006-02-27 12:43:29 +00001937
1938 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001939 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00001940
1941 EmitLabel("abbrev_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00001942
1943 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001944 }
1945}
1946
1947/// EmitDebugLines - Emit source line information.
1948///
1949void DwarfWriter::EmitDebugLines() const {
1950 // Minimum line delta, thus ranging from -10..(255-10).
1951 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1952 // Maximum line delta, thus ranging from -10..(255-10).
1953 const int MaxLineDelta = 255 + MinLineDelta;
1954
1955 // Start the dwarf line section.
Chris Lattner4632d7a2006-05-09 04:59:56 +00001956 Asm->SwitchToDataSection(DwarfLineSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001957
1958 // Construct the section header.
1959
1960 EmitDifference("line_end", 0, "line_begin", 0);
1961 EOL("Length of Source Line Info");
1962 EmitLabel("line_begin", 0);
1963
Jim Laskeyda427fa2006-01-27 20:31:25 +00001964 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey063e7652006-01-17 17:31:53 +00001965
1966 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
1967 EOL("Prolog Length");
1968 EmitLabel("line_prolog_begin", 0);
1969
Jim Laskeyda427fa2006-01-27 20:31:25 +00001970 EmitInt8(1); EOL("Minimum Instruction Length");
Jim Laskey063e7652006-01-17 17:31:53 +00001971
Jim Laskeyda427fa2006-01-27 20:31:25 +00001972 EmitInt8(1); EOL("Default is_stmt_start flag");
Jim Laskey063e7652006-01-17 17:31:53 +00001973
Jim Laskeyda427fa2006-01-27 20:31:25 +00001974 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00001975
Jim Laskeyda427fa2006-01-27 20:31:25 +00001976 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00001977
Jim Laskeyda427fa2006-01-27 20:31:25 +00001978 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Jim Laskey063e7652006-01-17 17:31:53 +00001979
1980 // Line number standard opcode encodings argument count
Jim Laskeyda427fa2006-01-27 20:31:25 +00001981 EmitInt8(0); EOL("DW_LNS_copy arg count");
1982 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
1983 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
1984 EmitInt8(1); EOL("DW_LNS_set_file arg count");
1985 EmitInt8(1); EOL("DW_LNS_set_column arg count");
1986 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
1987 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
1988 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
1989 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskey063e7652006-01-17 17:31:53 +00001990
1991 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1992 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1993
1994 // Emit directories.
1995 for (unsigned DirectoryID = 1, NDID = Directories.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001996 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskey063e7652006-01-17 17:31:53 +00001997 EmitString(Directories[DirectoryID]); EOL("Directory");
1998 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00001999 EmitInt8(0); EOL("End of directories");
Jim Laskey063e7652006-01-17 17:31:53 +00002000
2001 // Emit files.
2002 for (unsigned SourceID = 1, NSID = SourceFiles.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002003 SourceID <= NSID; ++SourceID) {
Jim Laskey063e7652006-01-17 17:31:53 +00002004 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2005 EmitString(SourceFile.getName()); EOL("Source");
2006 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2007 EmitULEB128Bytes(0); EOL("Mod date");
2008 EmitULEB128Bytes(0); EOL("File size");
2009 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00002010 EmitInt8(0); EOL("End of files");
Jim Laskey063e7652006-01-17 17:31:53 +00002011
2012 EmitLabel("line_prolog_end", 0);
2013
2014 // Emit line information
2015 const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
2016
2017 // Dwarf assumes we start with first line of first source file.
2018 unsigned Source = 1;
2019 unsigned Line = 1;
2020
2021 // Construct rows of the address, source, line, column matrix.
Jim Laskey52060a02006-01-24 00:49:18 +00002022 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00002023 SourceLineInfo *LineInfo = LineInfos[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002024
2025 if (DwarfVerbose) {
2026 unsigned SourceID = LineInfo->getSourceID();
2027 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2028 unsigned DirectoryID = SourceFile.getDirectoryID();
2029 O << "\t"
2030 << Asm->CommentString << " "
2031 << Directories[DirectoryID]
2032 << SourceFile.getName() << ":"
2033 << LineInfo->getLine() << "\n";
2034 }
Jim Laskey063e7652006-01-17 17:31:53 +00002035
2036 // Define the line address.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002037 EmitInt8(0); EOL("Extended Op");
2038 EmitInt8(4 + 1); EOL("Op size");
2039 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskeyb8509c52006-03-23 18:07:55 +00002040 EmitReference("loc", LineInfo->getLabelID()); EOL("Location label");
Jim Laskey063e7652006-01-17 17:31:53 +00002041
2042 // If change of source, then switch to the new source.
2043 if (Source != LineInfo->getSourceID()) {
2044 Source = LineInfo->getSourceID();
Jim Laskeyda427fa2006-01-27 20:31:25 +00002045 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
Jim Laskeybd761842006-02-27 17:27:12 +00002046 EmitULEB128Bytes(Source); EOL("New Source");
Jim Laskey063e7652006-01-17 17:31:53 +00002047 }
2048
2049 // If change of line.
2050 if (Line != LineInfo->getLine()) {
2051 // Determine offset.
2052 int Offset = LineInfo->getLine() - Line;
2053 int Delta = Offset - MinLineDelta;
2054
2055 // Update line.
2056 Line = LineInfo->getLine();
2057
2058 // If delta is small enough and in range...
2059 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2060 // ... then use fast opcode.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002061 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
Jim Laskey063e7652006-01-17 17:31:53 +00002062 } else {
2063 // ... otherwise use long hand.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002064 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
Jim Laskey063e7652006-01-17 17:31:53 +00002065 EmitSLEB128Bytes(Offset); EOL("Line Offset");
Jim Laskeyda427fa2006-01-27 20:31:25 +00002066 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
Jim Laskey063e7652006-01-17 17:31:53 +00002067 }
2068 } else {
2069 // Copy the previous row (different address or source)
Jim Laskeyda427fa2006-01-27 20:31:25 +00002070 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
Jim Laskey063e7652006-01-17 17:31:53 +00002071 }
2072 }
2073
Jim Laskey0420f2a2006-02-22 19:02:11 +00002074 // Define last address.
2075 EmitInt8(0); EOL("Extended Op");
2076 EmitInt8(4 + 1); EOL("Op size");
2077 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2078 EmitReference("text_end", 0); EOL("Location label");
2079
Jim Laskey063e7652006-01-17 17:31:53 +00002080 // Mark end of matrix.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002081 EmitInt8(0); EOL("DW_LNE_end_sequence");
Jim Laskey063e7652006-01-17 17:31:53 +00002082 EmitULEB128Bytes(1); O << "\n";
Jim Laskeyda427fa2006-01-27 20:31:25 +00002083 EmitInt8(1); O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002084
2085 EmitLabel("line_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002086
2087 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002088}
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002089
Jim Laskey41886992006-04-07 16:34:46 +00002090/// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002091///
Jim Laskey41886992006-04-07 16:34:46 +00002092void DwarfWriter::EmitInitialDebugFrame() {
Jim Laskey1069fbd2006-04-10 23:09:19 +00002093 int stackGrowth =
2094 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2095 TargetFrameInfo::StackGrowsUp ?
2096 AddressSize : -AddressSize;
2097
Jim Laskey41886992006-04-07 16:34:46 +00002098 // Start the dwarf frame section.
Chris Lattner4632d7a2006-05-09 04:59:56 +00002099 Asm->SwitchToDataSection(DwarfFrameSection, 0);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002100
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002101 EmitLabel("frame_common", 0);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002102 EmitDifference("frame_common_end", 0,
2103 "frame_common_begin", 0);
2104 EOL("Length of Common Information Entry");
2105
2106 EmitLabel("frame_common_begin", 0);
2107 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2108 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2109 EmitString(""); EOL("CIE Augmentation");
2110 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
Jim Laskey1069fbd2006-04-10 23:09:19 +00002111 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
Jim Laskey41886992006-04-07 16:34:46 +00002112 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
2113
2114 std::vector<MachineMove *> Moves;
2115 RI->getInitialFrameState(Moves);
2116 EmitFrameMoves(NULL, 0, Moves);
2117 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2118
Jim Laskeyb8509c52006-03-23 18:07:55 +00002119 EmitAlign(2);
2120 EmitLabel("frame_common_end", 0);
2121
2122 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002123}
2124
Jim Laskey41886992006-04-07 16:34:46 +00002125/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2126/// section.
2127void DwarfWriter::EmitFunctionDebugFrame() {
2128 // Start the dwarf frame section.
Chris Lattner4632d7a2006-05-09 04:59:56 +00002129 Asm->SwitchToDataSection(DwarfFrameSection, 0);
Jim Laskey41886992006-04-07 16:34:46 +00002130
2131 EmitDifference("frame_end", SubprogramCount,
2132 "frame_begin", SubprogramCount);
2133 EOL("Length of Frame Information Entry");
2134
2135 EmitLabel("frame_begin", SubprogramCount);
2136
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002137 EmitDifference("frame_common", 0, "section_frame", 0);
2138 EOL("FDE CIE offset");
Jim Laskey41886992006-04-07 16:34:46 +00002139
2140 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2141 EmitDifference("func_end", SubprogramCount,
2142 "func_begin", SubprogramCount);
2143 EOL("FDE address range");
2144
2145 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2146
2147 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2148
2149 EmitAlign(2);
2150 EmitLabel("frame_end", SubprogramCount);
2151
2152 O << "\n";
2153}
2154
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002155/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2156///
2157void DwarfWriter::EmitDebugPubNames() {
Jim Laskeybd761842006-02-27 17:27:12 +00002158 // Start the dwarf pubnames section.
Chris Lattner4632d7a2006-05-09 04:59:56 +00002159 Asm->SwitchToDataSection(DwarfPubNamesSection, 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002160
Jim Laskeybd761842006-02-27 17:27:12 +00002161 // Process each compile unit.
2162 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2163 CompileUnit *Unit = CompileUnits[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00002164
Jim Laskeybd761842006-02-27 17:27:12 +00002165 if (Unit->hasContent()) {
2166 EmitDifference("pubnames_end", Unit->getID(),
2167 "pubnames_begin", Unit->getID());
2168 EOL("Length of Public Names Info");
2169
2170 EmitLabel("pubnames_begin", Unit->getID());
2171
2172 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2173
Jim Laskey067ef412006-06-19 15:48:00 +00002174 EmitDifference("info_begin", Unit->getID(), "section_info", 0);
Jim Laskeybd761842006-02-27 17:27:12 +00002175 EOL("Offset of Compilation Unit Info");
Jim Laskeyd18e2892006-01-20 20:34:06 +00002176
Jim Laskeybd761842006-02-27 17:27:12 +00002177 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2178 EOL("Compilation Unit Length");
2179
2180 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2181
2182 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2183 GE = Globals.end();
2184 GI != GE; ++GI) {
2185 const std::string &Name = GI->first;
2186 DIE * Entity = GI->second;
2187
2188 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2189 EmitString(Name); EOL("External Name");
2190 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002191
Jim Laskeybd761842006-02-27 17:27:12 +00002192 EmitInt32(0); EOL("End Mark");
2193 EmitLabel("pubnames_end", Unit->getID());
2194
2195 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002196 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002197 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002198}
2199
2200/// EmitDebugStr - Emit visible names into a debug str section.
2201///
2202void DwarfWriter::EmitDebugStr() {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002203 // Check to see if it is worth the effort.
2204 if (!StringPool.empty()) {
2205 // Start the dwarf str section.
Chris Lattner4632d7a2006-05-09 04:59:56 +00002206 Asm->SwitchToDataSection(DwarfStrSection, 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002207
Jim Laskeyb8509c52006-03-23 18:07:55 +00002208 // For each of strings in the string pool.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002209 for (unsigned StringID = 1, N = StringPool.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002210 StringID <= N; ++StringID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002211 // Emit a label for reference from debug information entries.
2212 EmitLabel("string", StringID);
2213 // Emit the string itself.
2214 const std::string &String = StringPool[StringID];
2215 EmitString(String); O << "\n";
2216 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002217
2218 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002219 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002220}
2221
2222/// EmitDebugLoc - Emit visible names into a debug loc section.
2223///
2224void DwarfWriter::EmitDebugLoc() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002225 // Start the dwarf loc section.
Chris Lattner4632d7a2006-05-09 04:59:56 +00002226 Asm->SwitchToDataSection(DwarfLocSection, 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002227
2228 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002229}
2230
2231/// EmitDebugARanges - Emit visible names into a debug aranges section.
2232///
2233void DwarfWriter::EmitDebugARanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002234 // Start the dwarf aranges section.
Chris Lattner4632d7a2006-05-09 04:59:56 +00002235 Asm->SwitchToDataSection(DwarfARangesSection, 0);
Jim Laskeye719a7c2006-01-18 16:54:26 +00002236
2237 // FIXME - Mock up
Jim Laskeybd761842006-02-27 17:27:12 +00002238#if 0
2239 // Process each compile unit.
2240 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2241 CompileUnit *Unit = CompileUnits[i];
2242
2243 if (Unit->hasContent()) {
2244 // Don't include size of length
2245 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2246
2247 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2248
2249 EmitReference("info_begin", Unit->getID());
2250 EOL("Offset of Compilation Unit Info");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002251
Jim Laskeybd761842006-02-27 17:27:12 +00002252 EmitInt8(AddressSize); EOL("Size of Address");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002253
Jim Laskeybd761842006-02-27 17:27:12 +00002254 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002255
Jim Laskeybd761842006-02-27 17:27:12 +00002256 EmitInt16(0); EOL("Pad (1)");
2257 EmitInt16(0); EOL("Pad (2)");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002258
Jim Laskeybd761842006-02-27 17:27:12 +00002259 // Range 1
2260 EmitReference("text_begin", 0); EOL("Address");
2261 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002262
Jim Laskeybd761842006-02-27 17:27:12 +00002263 EmitInt32(0); EOL("EOM (1)");
2264 EmitInt32(0); EOL("EOM (2)");
2265
2266 O << "\n";
2267 }
2268 }
2269#endif
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002270}
2271
2272/// EmitDebugRanges - Emit visible names into a debug ranges section.
2273///
2274void DwarfWriter::EmitDebugRanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002275 // Start the dwarf ranges section.
Chris Lattner4632d7a2006-05-09 04:59:56 +00002276 Asm->SwitchToDataSection(DwarfRangesSection, 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002277
2278 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002279}
2280
2281/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2282///
2283void DwarfWriter::EmitDebugMacInfo() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002284 // Start the dwarf macinfo section.
Chris Lattner4632d7a2006-05-09 04:59:56 +00002285 Asm->SwitchToDataSection(DwarfMacInfoSection, 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002286
2287 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002288}
Jim Laskey063e7652006-01-17 17:31:53 +00002289
Jim Laskey52060a02006-01-24 00:49:18 +00002290/// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2291/// header file.
2292void DwarfWriter::ConstructCompileUnitDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002293 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
Jim Laskey6e87c0e2006-01-26 21:22:49 +00002294
2295 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00002296 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
Jim Laskey52060a02006-01-24 00:49:18 +00002297 CompileUnits.push_back(Unit);
2298 }
2299}
2300
2301/// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2302/// variables.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002303void DwarfWriter::ConstructGlobalDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002304 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002305 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00002306
2307 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002308 GlobalVariableDesc *GVD = GlobalVariables[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002309 NewGlobalVariable(GVD);
Jim Laskey52060a02006-01-24 00:49:18 +00002310 }
2311}
2312
Jim Laskey0420f2a2006-02-22 19:02:11 +00002313/// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2314/// subprograms.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002315void DwarfWriter::ConstructSubprogramDIEs() {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002316 std::vector<SubprogramDesc *> Subprograms =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002317 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002318
2319 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2320 SubprogramDesc *SPD = Subprograms[i];
2321 NewSubprogram(SPD);
2322 }
2323}
Jim Laskey52060a02006-01-24 00:49:18 +00002324
Jim Laskey063e7652006-01-17 17:31:53 +00002325//===----------------------------------------------------------------------===//
Jim Laskeyd18e2892006-01-20 20:34:06 +00002326// Main entry points.
Jim Laskey063e7652006-01-17 17:31:53 +00002327//
Jim Laskey52060a02006-01-24 00:49:18 +00002328
2329DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
2330: O(OS)
2331, Asm(A)
Jim Laskey41886992006-04-07 16:34:46 +00002332, TD(Asm->TM.getTargetData())
2333, RI(Asm->TM.getRegisterInfo())
Jim Laskeyb8509c52006-03-23 18:07:55 +00002334, M(NULL)
2335, MF(NULL)
Jim Laskey52060a02006-01-24 00:49:18 +00002336, DebugInfo(NULL)
2337, didInitial(false)
Jim Laskey014f98c2006-06-14 11:35:03 +00002338, shouldEmit(false)
2339, IsNormalText(false)
Jim Laskeyb8509c52006-03-23 18:07:55 +00002340, SubprogramCount(0)
Jim Laskey52060a02006-01-24 00:49:18 +00002341, CompileUnits()
2342, Abbreviations()
Jim Laskey52060a02006-01-24 00:49:18 +00002343, StringPool()
Jim Laskeybd761842006-02-27 17:27:12 +00002344, DescToUnitMap()
Jim Laskey0420f2a2006-02-22 19:02:11 +00002345, DescToDieMap()
2346, TypeToDieMap()
Jim Laskey52060a02006-01-24 00:49:18 +00002347, AddressSize(sizeof(int32_t))
2348, hasLEB128(false)
2349, hasDotLoc(false)
2350, hasDotFile(false)
2351, needsSet(false)
2352, DwarfAbbrevSection(".debug_abbrev")
2353, DwarfInfoSection(".debug_info")
2354, DwarfLineSection(".debug_line")
2355, DwarfFrameSection(".debug_frame")
2356, DwarfPubNamesSection(".debug_pubnames")
2357, DwarfPubTypesSection(".debug_pubtypes")
2358, DwarfStrSection(".debug_str")
2359, DwarfLocSection(".debug_loc")
2360, DwarfARangesSection(".debug_aranges")
2361, DwarfRangesSection(".debug_ranges")
2362, DwarfMacInfoSection(".debug_macinfo")
2363, TextSection(".text")
2364, DataSection(".data")
2365{}
2366DwarfWriter::~DwarfWriter() {
2367 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2368 delete CompileUnits[i];
Jim Laskey063e7652006-01-17 17:31:53 +00002369 }
Jim Laskey52060a02006-01-24 00:49:18 +00002370}
Jim Laskey063e7652006-01-17 17:31:53 +00002371
Jim Laskey41886992006-04-07 16:34:46 +00002372/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2373/// created it. Set by the target AsmPrinter.
2374void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
Jim Laskey014f98c2006-06-14 11:35:03 +00002375 // Make sure initial declarations are made.
2376 if (!DebugInfo && DI->hasInfo()) {
2377 DebugInfo = DI;
2378 shouldEmit = true;
2379
2380 // Emit initial sections
2381 EmitInitial();
2382
2383 // Create all the compile unit DIEs.
2384 ConstructCompileUnitDIEs();
2385
2386 // Create DIEs for each of the externally visible global variables.
2387 ConstructGlobalDIEs();
2388
2389 // Create DIEs for each of the externally visible subprograms.
2390 ConstructSubprogramDIEs();
2391 }
Jim Laskey41886992006-04-07 16:34:46 +00002392}
2393
Jim Laskey063e7652006-01-17 17:31:53 +00002394/// BeginModule - Emit all Dwarf sections that should come prior to the content.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002395///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002396void DwarfWriter::BeginModule(Module *M) {
2397 this->M = M;
2398
Jim Laskeyb2efb852006-01-04 22:28:25 +00002399 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002400 EOL("Dwarf Begin Module");
Jim Laskeyb2efb852006-01-04 22:28:25 +00002401}
2402
Jim Laskey063e7652006-01-17 17:31:53 +00002403/// EndModule - Emit all Dwarf sections that should come after the content.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002404///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002405void DwarfWriter::EndModule() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002406 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002407 EOL("Dwarf End Module");
2408
2409 // Standard sections final addresses.
Chris Lattner4632d7a2006-05-09 04:59:56 +00002410 Asm->SwitchToTextSection(TextSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002411 EmitLabel("text_end", 0);
Chris Lattner4632d7a2006-05-09 04:59:56 +00002412 Asm->SwitchToDataSection(DataSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002413 EmitLabel("data_end", 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002414
Jim Laskey063e7652006-01-17 17:31:53 +00002415 // Compute DIE offsets and sizes.
2416 SizeAndOffsets();
2417
2418 // Emit all the DIEs into a debug info section
2419 EmitDebugInfo();
2420
2421 // Corresponding abbreviations into a abbrev section.
2422 EmitAbbreviations();
2423
2424 // Emit source line correspondence into a debug line section.
2425 EmitDebugLines();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002426
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002427 // Emit info into a debug pubnames section.
2428 EmitDebugPubNames();
2429
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002430 // Emit info into a debug str section.
2431 EmitDebugStr();
2432
2433 // Emit info into a debug loc section.
2434 EmitDebugLoc();
2435
2436 // Emit info into a debug aranges section.
2437 EmitDebugARanges();
2438
2439 // Emit info into a debug ranges section.
2440 EmitDebugRanges();
2441
2442 // Emit info into a debug macinfo section.
2443 EmitDebugMacInfo();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002444}
2445
Jim Laskey6b92b8e2006-04-07 20:44:42 +00002446/// BeginFunction - Gather pre-function debug information. Assumes being
2447/// emitted immediately after the function entry point.
Jim Laskey014f98c2006-06-14 11:35:03 +00002448void DwarfWriter::BeginFunction(MachineFunction *MF, bool IsNormalText) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002449 this->MF = MF;
Jim Laskey014f98c2006-06-14 11:35:03 +00002450 // FIXME - should be able to debug coalesced functions.
2451 this->IsNormalText = IsNormalText;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002452
Jim Laskey014f98c2006-06-14 11:35:03 +00002453 // FIXME - should be able to debug coalesced functions.
2454 if (IsNormalText) {
2455 // Begin accumulating function debug information.
2456 DebugInfo->BeginFunction(MF);
2457
2458 if (!ShouldEmitDwarf()) return;
2459 EOL("Dwarf Begin Function");
Jim Laskey41886992006-04-07 16:34:46 +00002460
Jim Laskey014f98c2006-06-14 11:35:03 +00002461 // Assumes in correct section after the entry point.
2462 EmitLabel("func_begin", ++SubprogramCount);
2463 } else {
2464 ShouldEmitDwarf();
2465 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002466}
2467
Jim Laskeye719a7c2006-01-18 16:54:26 +00002468/// EndFunction - Gather and emit post-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002469///
Jim Laskeyb8509c52006-03-23 18:07:55 +00002470void DwarfWriter::EndFunction() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002471 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002472 EOL("Dwarf End Function");
Jim Laskeyb8509c52006-03-23 18:07:55 +00002473
Jim Laskey014f98c2006-06-14 11:35:03 +00002474 // FIXME - should be able to debug coalesced functions.
2475 if (IsNormalText) {
2476 // Define end label for subprogram.
2477 EmitLabel("func_end", SubprogramCount);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002478
Jim Laskey014f98c2006-06-14 11:35:03 +00002479 // Construct scopes for subprogram.
2480 ConstructRootScope(DebugInfo->getRootScope());
2481
2482 // Emit function frame information.
2483 EmitFunctionDebugFrame();
2484 }
Jim Laskey41886992006-04-07 16:34:46 +00002485
2486 // Clear function debug information.
2487 DebugInfo->EndFunction();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002488}