blob: 537f325aa9750eb660d415a9501f49c3882bec71 [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 Laskey65195462006-10-30 13:35:07 +000017#include "llvm/ADT/UniqueVector.h"
Jim Laskey52060a02006-01-24 00:49:18 +000018#include "llvm/Module.h"
19#include "llvm/Type.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000020#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeyb2efb852006-01-04 22:28:25 +000021#include "llvm/CodeGen/MachineDebugInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000023#include "llvm/CodeGen/MachineLocation.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000024#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000025#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000026#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000027#include "llvm/Support/Mangler.h"
Jim Laskey563321a2006-09-06 18:34:40 +000028#include "llvm/Target/TargetAsmInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000029#include "llvm/Target/MRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000030#include "llvm/Target/TargetData.h"
Jim Laskey52060a02006-01-24 00:49:18 +000031#include "llvm/Target/TargetMachine.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000032#include "llvm/Target/TargetFrameInfo.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000033
Jim Laskeyb2efb852006-01-04 22:28:25 +000034#include <iostream>
Jim Laskey65195462006-10-30 13:35:07 +000035#include <string>
Jim Laskeya7cea6f2006-01-04 13:52:30 +000036
Jim Laskeyb2efb852006-01-04 22:28:25 +000037using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000038using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000039
40static cl::opt<bool>
41DwarfVerbose("dwarf-verbose", cl::Hidden,
Jim Laskeyce50a162006-08-29 16:24:26 +000042 cl::desc("Add comments to Dwarf directives."));
Jim Laskey063e7652006-01-17 17:31:53 +000043
Jim Laskey0d086af2006-02-27 12:43:29 +000044namespace llvm {
Jim Laskey65195462006-10-30 13:35:07 +000045
46//===----------------------------------------------------------------------===//
47// DWLabel - Labels are used to track locations in the assembler file.
48// Labels appear in the form <prefix>debug_<Tag><Number>, where the tag is a
49// category of label (Ex. location) and number is a value unique in that
50// category.
51class DWLabel {
52public:
53 const char *Tag; // Label category tag. Should always be
54 // a staticly declared C string.
55 unsigned Number; // Unique number.
56
57 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
58};
Jim Laskey0d086af2006-02-27 12:43:29 +000059
Jim Laskey063e7652006-01-17 17:31:53 +000060//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +000061// Forward declarations.
62//
Jim Laskey0d086af2006-02-27 12:43:29 +000063class DIE;
Jim Laskey063e7652006-01-17 17:31:53 +000064
Jim Laskey0d086af2006-02-27 12:43:29 +000065//===----------------------------------------------------------------------===//
Jim Laskeyf01e5472006-03-03 15:06:57 +000066// CompileUnit - This dwarf writer support class manages information associate
67// with a source file.
Jim Laskeybd761842006-02-27 17:27:12 +000068class CompileUnit {
69private:
70 CompileUnitDesc *Desc; // Compile unit debug descriptor.
71 unsigned ID; // File ID for source.
Jim Laskeyb8509c52006-03-23 18:07:55 +000072 DIE *Die; // Compile unit debug information entry.
Jim Laskeybd761842006-02-27 17:27:12 +000073 std::map<std::string, DIE *> Globals; // A map of globally visible named
74 // entities for this unit.
Jim Laskey90c79d72006-03-23 23:02:34 +000075 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
76 // Tracks the mapping of unit level
77 // debug informaton descriptors to debug
78 // information entries.
Jim Laskeybd761842006-02-27 17:27:12 +000079
80public:
81 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
82 : Desc(CUD)
83 , ID(I)
84 , Die(D)
85 , Globals()
Jim Laskey90c79d72006-03-23 23:02:34 +000086 , DescToDieMap()
Jim Laskeybd761842006-02-27 17:27:12 +000087 {}
88
89 ~CompileUnit();
90
91 // Accessors.
92 CompileUnitDesc *getDesc() const { return Desc; }
93 unsigned getID() const { return ID; }
94 DIE* getDie() const { return Die; }
95 std::map<std::string, DIE *> &getGlobals() { return Globals; }
96
97 /// hasContent - Return true if this compile unit has something to write out.
98 ///
99 bool hasContent() const;
100
101 /// AddGlobal - Add a new global entity to the compile unit.
102 ///
103 void AddGlobal(const std::string &Name, DIE *Die);
104
Jim Laskey90c79d72006-03-23 23:02:34 +0000105 /// getDieMapSlotFor - Returns the debug information entry map slot for the
106 /// specified debug descriptor.
107 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
108 return DescToDieMap[DD];
109 }
Jim Laskeybd761842006-02-27 17:27:12 +0000110};
111
112//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +0000113// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
114// Dwarf abbreviation.
115class DIEAbbrevData {
116private:
117 unsigned Attribute; // Dwarf attribute code.
118 unsigned Form; // Dwarf form code.
119
120public:
121 DIEAbbrevData(unsigned A, unsigned F)
122 : Attribute(A)
123 , Form(F)
124 {}
125
Jim Laskeybd761842006-02-27 17:27:12 +0000126 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000127 unsigned getAttribute() const { return Attribute; }
128 unsigned getForm() const { return Form; }
129
130 /// operator== - Used by DIEAbbrev to locate entry.
131 ///
132 bool operator==(const DIEAbbrevData &DAD) const {
133 return Attribute == DAD.Attribute && Form == DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000134 }
Jim Laskey063e7652006-01-17 17:31:53 +0000135
Jim Laskey0d086af2006-02-27 12:43:29 +0000136 /// operator!= - Used by DIEAbbrev to locate entry.
137 ///
138 bool operator!=(const DIEAbbrevData &DAD) const {
139 return Attribute != DAD.Attribute || Form != DAD.Form;
Jim Laskey063e7652006-01-17 17:31:53 +0000140 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000141
142 /// operator< - Used by DIEAbbrev to locate entry.
143 ///
144 bool operator<(const DIEAbbrevData &DAD) const {
145 return Attribute < DAD.Attribute ||
146 (Attribute == DAD.Attribute && Form < DAD.Form);
147 }
148};
Jim Laskey063e7652006-01-17 17:31:53 +0000149
Jim Laskey0d086af2006-02-27 12:43:29 +0000150//===----------------------------------------------------------------------===//
151// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
152// information object.
153class DIEAbbrev {
154private:
155 unsigned Tag; // Dwarf tag code.
156 unsigned ChildrenFlag; // Dwarf children flag.
157 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
Jim Laskey063e7652006-01-17 17:31:53 +0000158
Jim Laskey0d086af2006-02-27 12:43:29 +0000159public:
Jim Laskey063e7652006-01-17 17:31:53 +0000160
Jim Laskey0d086af2006-02-27 12:43:29 +0000161 DIEAbbrev(unsigned T, unsigned C)
162 : Tag(T)
163 , ChildrenFlag(C)
164 , Data()
165 {}
166 ~DIEAbbrev() {}
167
Jim Laskeybd761842006-02-27 17:27:12 +0000168 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000169 unsigned getTag() const { return Tag; }
170 unsigned getChildrenFlag() const { return ChildrenFlag; }
171 const std::vector<DIEAbbrevData> &getData() const { return Data; }
172 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskey063e7652006-01-17 17:31:53 +0000173
Jim Laskey0d086af2006-02-27 12:43:29 +0000174 /// operator== - Used by UniqueVector to locate entry.
175 ///
176 bool operator==(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000177
Jim Laskey0d086af2006-02-27 12:43:29 +0000178 /// operator< - Used by UniqueVector to locate entry.
179 ///
180 bool operator<(const DIEAbbrev &DA) const;
Jim Laskey063e7652006-01-17 17:31:53 +0000181
Jim Laskey0d086af2006-02-27 12:43:29 +0000182 /// AddAttribute - Adds another set of attribute information to the
183 /// abbreviation.
184 void AddAttribute(unsigned Attribute, unsigned Form) {
185 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000186 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000187
Jim Laskeyb8509c52006-03-23 18:07:55 +0000188 /// AddFirstAttribute - Adds a set of attribute information to the front
189 /// of the abbreviation.
190 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
191 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
192 }
193
Jim Laskey0d086af2006-02-27 12:43:29 +0000194 /// Emit - Print the abbreviation using the specified Dwarf writer.
195 ///
Jim Laskey65195462006-10-30 13:35:07 +0000196 void Emit(const Dwarf &DW) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000197
198#ifndef NDEBUG
199 void print(std::ostream &O);
200 void dump();
201#endif
202};
Jim Laskey063e7652006-01-17 17:31:53 +0000203
Jim Laskey0d086af2006-02-27 12:43:29 +0000204//===----------------------------------------------------------------------===//
205// DIEValue - A debug information entry value.
206//
207class DIEValue {
208public:
209 enum {
210 isInteger,
211 isString,
212 isLabel,
213 isAsIsLabel,
214 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000215 isEntry,
216 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000217 };
218
219 unsigned Type; // Type of the value
220
221 DIEValue(unsigned T) : Type(T) {}
222 virtual ~DIEValue() {}
223
224 // Implement isa/cast/dyncast.
225 static bool classof(const DIEValue *) { return true; }
226
227 /// EmitValue - Emit value via the Dwarf writer.
228 ///
Jim Laskey65195462006-10-30 13:35:07 +0000229 virtual void EmitValue(const Dwarf &DW, unsigned Form) const = 0;
Jim Laskey0d086af2006-02-27 12:43:29 +0000230
231 /// SizeOf - Return the size of a value in bytes.
232 ///
Jim Laskey65195462006-10-30 13:35:07 +0000233 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const = 0;
Jim Laskey0d086af2006-02-27 12:43:29 +0000234};
Jim Laskey063e7652006-01-17 17:31:53 +0000235
Jim Laskey0d086af2006-02-27 12:43:29 +0000236//===----------------------------------------------------------------------===//
237// DWInteger - An integer value DIE.
238//
239class DIEInteger : public DIEValue {
240private:
241 uint64_t Integer;
242
243public:
244 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000245
Jim Laskey0d086af2006-02-27 12:43:29 +0000246 // Implement isa/cast/dyncast.
247 static bool classof(const DIEInteger *) { return true; }
248 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
249
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000250 /// BestForm - Choose the best form for integer.
251 ///
252 unsigned BestForm(bool IsSigned);
253
Jim Laskey0d086af2006-02-27 12:43:29 +0000254 /// EmitValue - Emit integer of appropriate size.
255 ///
Jim Laskey65195462006-10-30 13:35:07 +0000256 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000257
258 /// SizeOf - Determine size of integer value in bytes.
259 ///
Jim Laskey65195462006-10-30 13:35:07 +0000260 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000261};
Jim Laskey063e7652006-01-17 17:31:53 +0000262
Jim Laskey0d086af2006-02-27 12:43:29 +0000263//===----------------------------------------------------------------------===//
264// DIEString - A string value DIE.
265//
266struct DIEString : public DIEValue {
267 const std::string String;
268
269 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000270
Jim Laskey0d086af2006-02-27 12:43:29 +0000271 // Implement isa/cast/dyncast.
272 static bool classof(const DIEString *) { return true; }
273 static bool classof(const DIEValue *S) { return S->Type == isString; }
274
275 /// EmitValue - Emit string value.
276 ///
Jim Laskey65195462006-10-30 13:35:07 +0000277 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000278
279 /// SizeOf - Determine size of string value in bytes.
280 ///
Jim Laskey65195462006-10-30 13:35:07 +0000281 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000282};
Jim Laskey063e7652006-01-17 17:31:53 +0000283
Jim Laskey0d086af2006-02-27 12:43:29 +0000284//===----------------------------------------------------------------------===//
285// DIEDwarfLabel - A Dwarf internal label expression DIE.
286//
287struct DIEDwarfLabel : public DIEValue {
288 const DWLabel Label;
289
290 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000291
Jim Laskey0d086af2006-02-27 12:43:29 +0000292 // Implement isa/cast/dyncast.
293 static bool classof(const DIEDwarfLabel *) { return true; }
294 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
295
296 /// EmitValue - Emit label value.
297 ///
Jim Laskey65195462006-10-30 13:35:07 +0000298 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000299
300 /// SizeOf - Determine size of label value in bytes.
301 ///
Jim Laskey65195462006-10-30 13:35:07 +0000302 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000303};
Jim Laskey063e7652006-01-17 17:31:53 +0000304
Jim Laskey063e7652006-01-17 17:31:53 +0000305
Jim Laskey0d086af2006-02-27 12:43:29 +0000306//===----------------------------------------------------------------------===//
307// DIEObjectLabel - A label to an object in code or data.
308//
309struct DIEObjectLabel : public DIEValue {
310 const std::string Label;
311
312 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000313
Jim Laskey0d086af2006-02-27 12:43:29 +0000314 // Implement isa/cast/dyncast.
315 static bool classof(const DIEObjectLabel *) { return true; }
316 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
317
318 /// EmitValue - Emit label value.
319 ///
Jim Laskey65195462006-10-30 13:35:07 +0000320 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000321
322 /// SizeOf - Determine size of label value in bytes.
323 ///
Jim Laskey65195462006-10-30 13:35:07 +0000324 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000325};
Jim Laskey063e7652006-01-17 17:31:53 +0000326
Jim Laskey0d086af2006-02-27 12:43:29 +0000327//===----------------------------------------------------------------------===//
328// DIEDelta - A simple label difference DIE.
329//
330struct DIEDelta : public DIEValue {
331 const DWLabel LabelHi;
332 const DWLabel LabelLo;
333
334 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
335 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000336
Jim Laskey0d086af2006-02-27 12:43:29 +0000337 // Implement isa/cast/dyncast.
338 static bool classof(const DIEDelta *) { return true; }
339 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
340
341 /// EmitValue - Emit delta value.
342 ///
Jim Laskey65195462006-10-30 13:35:07 +0000343 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000344
345 /// SizeOf - Determine size of delta value in bytes.
346 ///
Jim Laskey65195462006-10-30 13:35:07 +0000347 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000348};
Jim Laskey063e7652006-01-17 17:31:53 +0000349
Jim Laskey0d086af2006-02-27 12:43:29 +0000350//===----------------------------------------------------------------------===//
351// DIEntry - A pointer to a debug information entry.
352//
353struct DIEntry : public DIEValue {
354 DIE *Entry;
355
356 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
357
358 // Implement isa/cast/dyncast.
359 static bool classof(const DIEntry *) { return true; }
360 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
361
Jim Laskeyb8509c52006-03-23 18:07:55 +0000362 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000363 ///
Jim Laskey65195462006-10-30 13:35:07 +0000364 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000365
Jim Laskeyb8509c52006-03-23 18:07:55 +0000366 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000367 ///
Jim Laskey65195462006-10-30 13:35:07 +0000368 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000369};
370
371//===----------------------------------------------------------------------===//
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000372// DIEBlock - A block of values. Primarily used for location expressions.
373//
374struct DIEBlock : public DIEValue {
375 unsigned Size; // Size in bytes excluding size header.
376 std::vector<unsigned> Forms; // Data forms.
377 std::vector<DIEValue *> Values; // Block values.
378
379 DIEBlock()
380 : DIEValue(isBlock)
381 , Size(0)
382 , Forms()
383 , Values()
384 {}
385 ~DIEBlock();
386
387 // Implement isa/cast/dyncast.
388 static bool classof(const DIEBlock *) { return true; }
389 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
390
391 /// ComputeSize - calculate the size of the block.
392 ///
Jim Laskey65195462006-10-30 13:35:07 +0000393 unsigned ComputeSize(Dwarf &DW);
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000394
395 /// BestForm - Choose the best form for data.
396 ///
397 unsigned BestForm();
398
399 /// EmitValue - Emit block data.
400 ///
Jim Laskey65195462006-10-30 13:35:07 +0000401 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000402
403 /// SizeOf - Determine size of block data in bytes.
404 ///
Jim Laskey65195462006-10-30 13:35:07 +0000405 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000406
407 /// AddUInt - Add an unsigned integer value.
408 ///
409 void AddUInt(unsigned Form, uint64_t Integer);
410
411 /// AddSInt - Add an signed integer value.
412 ///
413 void AddSInt(unsigned Form, int64_t Integer);
414
415 /// AddString - Add a std::string value.
416 ///
417 void AddString(unsigned Form, const std::string &String);
418
419 /// AddLabel - Add a Dwarf label value.
420 ///
421 void AddLabel(unsigned Form, const DWLabel &Label);
422
423 /// AddObjectLabel - Add a non-Dwarf label value.
424 ///
425 void AddObjectLabel(unsigned Form, const std::string &Label);
426
427 /// AddDelta - Add a label delta value.
428 ///
429 void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
430
431 /// AddDIEntry - Add a DIE value.
432 ///
433 void AddDIEntry(unsigned Form, DIE *Entry);
434
435};
436
437//===----------------------------------------------------------------------===//
Jim Laskey0d086af2006-02-27 12:43:29 +0000438// DIE - A structured debug information entry. Has an abbreviation which
439// describes it's organization.
440class DIE {
441private:
442 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
443 unsigned AbbrevID; // Decribing abbreviation ID.
444 unsigned Offset; // Offset in debug info section.
445 unsigned Size; // Size of instance + children.
446 std::vector<DIE *> Children; // Children DIEs.
447 std::vector<DIEValue *> Values; // Attributes values.
448
449public:
450 DIE(unsigned Tag);
451 ~DIE();
452
Jim Laskeybd761842006-02-27 17:27:12 +0000453 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000454 unsigned getAbbrevID() const { return AbbrevID; }
455 unsigned getOffset() const { return Offset; }
456 unsigned getSize() const { return Size; }
457 const std::vector<DIE *> &getChildren() const { return Children; }
458 const std::vector<DIEValue *> &getValues() const { return Values; }
459 void setOffset(unsigned O) { Offset = O; }
460 void setSize(unsigned S) { Size = S; }
461
462 /// SiblingOffset - Return the offset of the debug information entry's
463 /// sibling.
464 unsigned SiblingOffset() const { return Offset + Size; }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000465
466 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
467 ///
468 void AddSiblingOffset();
Jim Laskey0d086af2006-02-27 12:43:29 +0000469
470 /// AddUInt - Add an unsigned integer attribute data and value.
471 ///
472 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
473
474 /// AddSInt - Add an signed integer attribute data and value.
475 ///
476 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
477
478 /// AddString - Add a std::string attribute data and value.
479 ///
480 void AddString(unsigned Attribute, unsigned Form,
481 const std::string &String);
482
483 /// AddLabel - Add a Dwarf label attribute data and value.
484 ///
485 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
486
487 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
488 ///
489 void AddObjectLabel(unsigned Attribute, unsigned Form,
490 const std::string &Label);
491
492 /// AddDelta - Add a label delta attribute data and value.
493 ///
494 void AddDelta(unsigned Attribute, unsigned Form,
495 const DWLabel &Hi, const DWLabel &Lo);
496
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000497 /// AddDIEntry - Add a DIE attribute data and value.
Jim Laskey0d086af2006-02-27 12:43:29 +0000498 ///
499 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
500
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000501 /// AddBlock - Add block data.
502 ///
503 void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
504
Jim Laskey0d086af2006-02-27 12:43:29 +0000505 /// Complete - Indicate that all attributes have been added and
506 /// ready to get an abbreviation ID.
507 ///
Jim Laskey65195462006-10-30 13:35:07 +0000508 void Complete(Dwarf &DW);
Jim Laskey0d086af2006-02-27 12:43:29 +0000509
510 /// AddChild - Add a child to the DIE.
511 void AddChild(DIE *Child);
512};
513
Jim Laskey65195462006-10-30 13:35:07 +0000514//===----------------------------------------------------------------------===//
515// Dwarf - Emits Dwarf debug and exception handling directives.
516//
517class Dwarf {
518
519private:
520
521 //===--------------------------------------------------------------------===//
522 // Core attributes used by the Dwarf writer.
523 //
524
525 //
526 /// O - Stream to .s file.
527 ///
528 std::ostream &O;
529
530 /// Asm - Target of Dwarf emission.
531 ///
532 AsmPrinter *Asm;
533
534 /// TAI - Target Asm Printer.
535 const TargetAsmInfo *TAI;
536
537 /// TD - Target data.
538 const TargetData *TD;
539
540 /// RI - Register Information.
541 const MRegisterInfo *RI;
542
543 /// M - Current module.
544 ///
545 Module *M;
546
547 /// MF - Current machine function.
548 ///
549 MachineFunction *MF;
550
551 /// DebugInfo - Collected debug information.
552 ///
553 MachineDebugInfo *DebugInfo;
554
555 /// didInitial - Flag to indicate if initial emission has been done.
556 ///
557 bool didInitial;
558
559 /// shouldEmit - Flag to indicate if debug information should be emitted.
560 ///
561 bool shouldEmit;
562
563 /// SubprogramCount - The running count of functions being compiled.
564 ///
565 unsigned SubprogramCount;
566
567 //===--------------------------------------------------------------------===//
568 // Attributes used to construct specific Dwarf sections.
569 //
570
571 /// CompileUnits - All the compile units involved in this build. The index
572 /// of each entry in this vector corresponds to the sources in DebugInfo.
573 std::vector<CompileUnit *> CompileUnits;
574
575 /// Abbreviations - A UniqueVector of TAG structure abbreviations.
576 ///
577 UniqueVector<DIEAbbrev> Abbreviations;
578
579 /// StringPool - A UniqueVector of strings used by indirect references.
580 /// UnitMap - Map debug information descriptor to compile unit.
581 ///
582 UniqueVector<std::string> StringPool;
583
584 /// UnitMap - Map debug information descriptor to compile unit.
585 ///
586 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
587
588 /// DescToDieMap - Tracks the mapping of top level debug informaton
589 /// descriptors to debug information entries.
590 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
591
592 /// SectionMap - Provides a unique id per text section.
593 ///
594 UniqueVector<std::string> SectionMap;
595
596 /// SectionSourceLines - Tracks line numbers per text section.
597 ///
598 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
599
600
601public:
602
603 //===--------------------------------------------------------------------===//
604 // Emission and print routines
605 //
606
607 /// PrintHex - Print a value as a hexidecimal value.
608 ///
609 void PrintHex(int Value) const;
610
611 /// EOL - Print a newline character to asm stream. If a comment is present
612 /// then it will be printed first. Comments should not contain '\n'.
613 void EOL(const std::string &Comment) const;
614
615 /// EmitAlign - Print a align directive.
616 ///
617 void EmitAlign(unsigned Alignment) const;
618
619 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
620 /// unsigned leb128 value.
621 void EmitULEB128Bytes(unsigned Value) const;
622
623 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
624 /// signed leb128 value.
625 void EmitSLEB128Bytes(int Value) const;
626
627 /// PrintULEB128 - Print a series of hexidecimal values (separated by
628 /// commas) representing an unsigned leb128 value.
629 void PrintULEB128(unsigned Value) const;
630
631 /// SizeULEB128 - Compute the number of bytes required for an unsigned
632 /// leb128 value.
633 static unsigned SizeULEB128(unsigned Value);
634
635 /// PrintSLEB128 - Print a series of hexidecimal values (separated by
636 /// commas) representing a signed leb128 value.
637 void PrintSLEB128(int Value) const;
638
639 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
640 /// value.
641 static unsigned SizeSLEB128(int Value);
642
643 /// EmitInt8 - Emit a byte directive and value.
644 ///
645 void EmitInt8(int Value) const;
646
647 /// EmitInt16 - Emit a short directive and value.
648 ///
649 void EmitInt16(int Value) const;
650
651 /// EmitInt32 - Emit a long directive and value.
652 ///
653 void EmitInt32(int Value) const;
654
655 /// EmitInt64 - Emit a long long directive and value.
656 ///
657 void EmitInt64(uint64_t Value) const;
658
659 /// EmitString - Emit a string with quotes and a null terminator.
660 /// Special characters are emitted properly.
661 /// \literal (Eg. '\t') \endliteral
662 void EmitString(const std::string &String) const;
663
664 /// PrintLabelName - Print label name in form used by Dwarf writer.
665 ///
666 void PrintLabelName(DWLabel Label) const {
667 PrintLabelName(Label.Tag, Label.Number);
668 }
669 void PrintLabelName(const char *Tag, unsigned Number) const;
670
671 /// EmitLabel - Emit location label for internal use by Dwarf.
672 ///
673 void EmitLabel(DWLabel Label) const {
674 EmitLabel(Label.Tag, Label.Number);
675 }
676 void EmitLabel(const char *Tag, unsigned Number) const;
677
678 /// EmitReference - Emit a reference to a label.
679 ///
680 void EmitReference(DWLabel Label) const {
681 EmitReference(Label.Tag, Label.Number);
682 }
683 void EmitReference(const char *Tag, unsigned Number) const;
684 void EmitReference(const std::string &Name) const;
685
686 /// EmitDifference - Emit the difference between two labels. Some
687 /// assemblers do not behave with absolute expressions with data directives,
688 /// so there is an option (needsSet) to use an intermediary set expression.
689 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo) const {
690 EmitDifference(LabelHi.Tag, LabelHi.Number, LabelLo.Tag, LabelLo.Number);
691 }
692 void EmitDifference(const char *TagHi, unsigned NumberHi,
693 const char *TagLo, unsigned NumberLo) const;
694
695 /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
696 ///
697 unsigned NewAbbreviation(DIEAbbrev *Abbrev);
698
699 /// NewString - Add a string to the constant pool and returns a label.
700 ///
701 DWLabel NewString(const std::string &String);
702
703 /// getDieMapSlotFor - Returns the debug information entry map slot for the
704 /// specified debug descriptor.
705 DIE *&getDieMapSlotFor(DebugInfoDesc *DD);
706
707private:
708
709 /// AddSourceLine - Add location information to specified debug information
710 /// entry.
711 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line);
712
713 /// AddAddress - Add an address attribute to a die based on the location
714 /// provided.
715 void AddAddress(DIE *Die, unsigned Attribute,
716 const MachineLocation &Location);
717
718 /// NewType - Create a new type DIE.
719 ///
720 DIE *NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit);
721
722 /// NewCompileUnit - Create new compile unit and it's die.
723 ///
724 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID);
725
726 /// FindCompileUnit - Get the compile unit for the given descriptor.
727 ///
728 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc);
729
730 /// NewGlobalVariable - Make a new global variable DIE.
731 ///
732 DIE *NewGlobalVariable(GlobalVariableDesc *GVD);
733
734 /// NewSubprogram - Add a new subprogram DIE.
735 ///
736 DIE *NewSubprogram(SubprogramDesc *SPD);
737
738 /// NewScopeVariable - Create a new scope variable.
739 ///
740 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit);
741
742 /// ConstructScope - Construct the components of a scope.
743 ///
744 void ConstructScope(DebugScope *ParentScope, DIE *ParentDie,
745 CompileUnit *Unit);
746
747 /// ConstructRootScope - Construct the scope for the subprogram.
748 ///
749 void ConstructRootScope(DebugScope *RootScope);
750
751 /// EmitInitial - Emit initial Dwarf declarations.
752 ///
753 void EmitInitial();
754
755 /// EmitDIE - Recusively Emits a debug information entry.
756 ///
757 void EmitDIE(DIE *Die) const;
758
759 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
760 ///
761 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last);
762
763 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
764 ///
765 void SizeAndOffsets();
766
767 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
768 /// frame.
769 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
770 std::vector<MachineMove *> &Moves);
771
772 /// EmitDebugInfo - Emit the debug info section.
773 ///
774 void EmitDebugInfo() const;
775
776 /// EmitAbbreviations - Emit the abbreviation section.
777 ///
778 void EmitAbbreviations() const;
779
780 /// EmitDebugLines - Emit source line information.
781 ///
782 void EmitDebugLines() const;
783
784 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
785 ///
786 void EmitInitialDebugFrame();
787
788 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
789 /// section.
790 void EmitFunctionDebugFrame();
791
792 /// EmitDebugPubNames - Emit info into a debug pubnames section.
793 ///
794 void EmitDebugPubNames();
795
796 /// EmitDebugStr - Emit info into a debug str section.
797 ///
798 void EmitDebugStr();
799
800 /// EmitDebugLoc - Emit info into a debug loc section.
801 ///
802 void EmitDebugLoc();
803
804 /// EmitDebugARanges - Emit info into a debug aranges section.
805 ///
806 void EmitDebugARanges();
807
808 /// EmitDebugRanges - Emit info into a debug ranges section.
809 ///
810 void EmitDebugRanges();
811
812 /// EmitDebugMacInfo - Emit info into a debug macinfo section.
813 ///
814 void EmitDebugMacInfo();
815
816 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
817 /// header file.
818 void ConstructCompileUnitDIEs();
819
820 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
821 /// global variables.
822 void ConstructGlobalDIEs();
823
824 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
825 /// subprograms.
826 void ConstructSubprogramDIEs();
827
828 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
829 ///
830 bool ShouldEmitDwarf() const { return shouldEmit; }
831
832public:
833
834 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T);
835 virtual ~Dwarf();
836
837 // Accessors.
838 //
839 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
840
841 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
842 /// created it. Set by the target AsmPrinter.
843 void SetDebugInfo(MachineDebugInfo *DI);
844
845 //===--------------------------------------------------------------------===//
846 // Main entry points.
847 //
848
849 /// BeginModule - Emit all Dwarf sections that should come prior to the
850 /// content.
851 void BeginModule(Module *M);
852
853 /// EndModule - Emit all Dwarf sections that should come after the content.
854 ///
855 void EndModule();
856
857 /// BeginFunction - Gather pre-function debug information. Assumes being
858 /// emitted immediately after the function entry point.
859 void BeginFunction(MachineFunction *MF);
860
861 /// EndFunction - Gather and emit post-function debug information.
862 ///
863 void EndFunction();
864};
865
Jim Laskey0d086af2006-02-27 12:43:29 +0000866} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +0000867
868//===----------------------------------------------------------------------===//
869
Jim Laskeybd761842006-02-27 17:27:12 +0000870CompileUnit::~CompileUnit() {
871 delete Die;
872}
873
874/// hasContent - Return true if this compile unit has something to write out.
875///
876bool CompileUnit::hasContent() const {
877 return !Die->getChildren().empty();
878}
879
880/// AddGlobal - Add a new global entity to the compile unit.
881///
882void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
883 Globals[Name] = Die;
884}
885
886//===----------------------------------------------------------------------===//
887
Jim Laskeyd18e2892006-01-20 20:34:06 +0000888/// operator== - Used by UniqueVector to locate entry.
889///
890bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
891 if (Tag != DA.Tag) return false;
892 if (ChildrenFlag != DA.ChildrenFlag) return false;
893 if (Data.size() != DA.Data.size()) return false;
894
Jim Laskey52060a02006-01-24 00:49:18 +0000895 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000896 if (Data[i] != DA.Data[i]) return false;
Jim Laskeyd18e2892006-01-20 20:34:06 +0000897 }
898
899 return true;
900}
901
902/// operator< - Used by UniqueVector to locate entry.
903///
904bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
905 if (Tag != DA.Tag) return Tag < DA.Tag;
906 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
907 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
908
Jim Laskey52060a02006-01-24 00:49:18 +0000909 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000910 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +0000911 }
912
913 return false;
914}
915
916/// Emit - Print the abbreviation using the specified Dwarf writer.
917///
Jim Laskey65195462006-10-30 13:35:07 +0000918void DIEAbbrev::Emit(const Dwarf &DW) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000919 // Emit its Dwarf tag type.
920 DW.EmitULEB128Bytes(Tag);
921 DW.EOL(TagString(Tag));
922
923 // Emit whether it has children DIEs.
924 DW.EmitULEB128Bytes(ChildrenFlag);
925 DW.EOL(ChildrenString(ChildrenFlag));
926
927 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +0000928 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000929 const DIEAbbrevData &AttrData = Data[i];
930
931 // Emit attribute type.
932 DW.EmitULEB128Bytes(AttrData.getAttribute());
933 DW.EOL(AttributeString(AttrData.getAttribute()));
934
935 // Emit form type.
936 DW.EmitULEB128Bytes(AttrData.getForm());
937 DW.EOL(FormEncodingString(AttrData.getForm()));
938 }
939
940 // Mark end of abbreviation.
941 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
942 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
943}
944
945#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +0000946void DIEAbbrev::print(std::ostream &O) {
947 O << "Abbreviation @"
948 << std::hex << (intptr_t)this << std::dec
949 << " "
950 << TagString(Tag)
951 << " "
952 << ChildrenString(ChildrenFlag)
953 << "\n";
954
955 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
956 O << " "
957 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +0000958 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +0000959 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +0000960 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +0000961 }
Jim Laskeya0f3d172006-09-07 22:06:40 +0000962}
963void DIEAbbrev::dump() { print(std::cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +0000964#endif
965
966//===----------------------------------------------------------------------===//
967
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000968/// BestForm - Choose the best form for integer.
969///
970unsigned DIEInteger::BestForm(bool IsSigned) {
971 if (IsSigned) {
972 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
973 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
974 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
975 } else {
976 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
977 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
978 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
979 }
980 return DW_FORM_data8;
981}
982
Jim Laskey063e7652006-01-17 17:31:53 +0000983/// EmitValue - Emit integer of appropriate size.
984///
Jim Laskey65195462006-10-30 13:35:07 +0000985void DIEInteger::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +0000986 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +0000987 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000988 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000989 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000990 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000991 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000992 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000993 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000994 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000995 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +0000996 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
997 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +0000998 default: assert(0 && "DIE Value form not supported yet"); break;
999 }
1000}
1001
1002/// SizeOf - Determine size of integer value in bytes.
1003///
Jim Laskey65195462006-10-30 13:35:07 +00001004unsigned DIEInteger::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001005 switch (Form) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001006 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00001007 case DW_FORM_ref1: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +00001008 case DW_FORM_data1: return sizeof(int8_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001009 case DW_FORM_ref2: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +00001010 case DW_FORM_data2: return sizeof(int16_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001011 case DW_FORM_ref4: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +00001012 case DW_FORM_data4: return sizeof(int32_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001013 case DW_FORM_ref8: // Fall thru
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001014 case DW_FORM_data8: return sizeof(int64_t);
Jim Laskey40020172006-01-20 21:02:36 +00001015 case DW_FORM_udata: return DW.SizeULEB128(Integer);
1016 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00001017 default: assert(0 && "DIE Value form not supported yet"); break;
1018 }
1019 return 0;
1020}
1021
1022//===----------------------------------------------------------------------===//
1023
1024/// EmitValue - Emit string value.
1025///
Jim Laskey65195462006-10-30 13:35:07 +00001026void DIEString::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001027 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00001028}
1029
1030/// SizeOf - Determine size of string value in bytes.
1031///
Jim Laskey65195462006-10-30 13:35:07 +00001032unsigned DIEString::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey40020172006-01-20 21:02:36 +00001033 return String.size() + sizeof(char); // sizeof('\0');
Jim Laskey063e7652006-01-17 17:31:53 +00001034}
1035
1036//===----------------------------------------------------------------------===//
1037
1038/// EmitValue - Emit label value.
1039///
Jim Laskey65195462006-10-30 13:35:07 +00001040void DIEDwarfLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001041 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +00001042}
1043
1044/// SizeOf - Determine size of label value in bytes.
1045///
Jim Laskey65195462006-10-30 13:35:07 +00001046unsigned DIEDwarfLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001047 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00001048}
1049
1050//===----------------------------------------------------------------------===//
1051
Jim Laskeyd18e2892006-01-20 20:34:06 +00001052/// EmitValue - Emit label value.
1053///
Jim Laskey65195462006-10-30 13:35:07 +00001054void DIEObjectLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001055 DW.EmitReference(Label);
1056}
1057
1058/// SizeOf - Determine size of label value in bytes.
1059///
Jim Laskey65195462006-10-30 13:35:07 +00001060unsigned DIEObjectLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001061 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001062}
1063
1064//===----------------------------------------------------------------------===//
1065
Jim Laskey063e7652006-01-17 17:31:53 +00001066/// EmitValue - Emit delta value.
1067///
Jim Laskey65195462006-10-30 13:35:07 +00001068void DIEDelta::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001069 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001070}
1071
1072/// SizeOf - Determine size of delta value in bytes.
1073///
Jim Laskey65195462006-10-30 13:35:07 +00001074unsigned DIEDelta::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001075 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00001076}
1077
1078//===----------------------------------------------------------------------===//
Jim Laskeyb8509c52006-03-23 18:07:55 +00001079/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001080///
Jim Laskey65195462006-10-30 13:35:07 +00001081void DIEntry::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001082 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00001083}
1084
Jim Laskeyb8509c52006-03-23 18:07:55 +00001085/// SizeOf - Determine size of debug information entry value in bytes.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001086///
Jim Laskey65195462006-10-30 13:35:07 +00001087unsigned DIEntry::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001088 return sizeof(int32_t);
1089}
1090
1091//===----------------------------------------------------------------------===//
1092
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001093DIEBlock::~DIEBlock() {
1094 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1095 delete Values[i];
1096 }
1097}
1098
1099/// ComputeSize - calculate the size of the block.
1100///
Jim Laskey65195462006-10-30 13:35:07 +00001101unsigned DIEBlock::ComputeSize(Dwarf &DW) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001102 Size = 0;
1103 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1104 Size += Values[i]->SizeOf(DW, Forms[i]);
1105 }
1106 return Size;
1107}
1108
1109/// BestForm - Choose the best form for data.
1110///
1111unsigned DIEBlock::BestForm() {
1112 if ((unsigned char)Size == Size) return DW_FORM_block1;
1113 if ((unsigned short)Size == Size) return DW_FORM_block2;
1114 if ((unsigned int)Size == Size) return DW_FORM_block4;
1115 return DW_FORM_block;
1116}
1117
1118/// EmitValue - Emit block data.
1119///
Jim Laskey65195462006-10-30 13:35:07 +00001120void DIEBlock::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001121 switch (Form) {
1122 case DW_FORM_block1: DW.EmitInt8(Size); break;
1123 case DW_FORM_block2: DW.EmitInt16(Size); break;
1124 case DW_FORM_block4: DW.EmitInt32(Size); break;
1125 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
1126 default: assert(0 && "Improper form for block"); break;
1127 }
1128 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1129 DW.EOL("");
1130 Values[i]->EmitValue(DW, Forms[i]);
1131 }
1132}
1133
1134/// SizeOf - Determine size of block data in bytes.
1135///
Jim Laskey65195462006-10-30 13:35:07 +00001136unsigned DIEBlock::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001137 switch (Form) {
1138 case DW_FORM_block1: return Size + sizeof(int8_t);
1139 case DW_FORM_block2: return Size + sizeof(int16_t);
1140 case DW_FORM_block4: return Size + sizeof(int32_t);
1141 case DW_FORM_block: return Size + DW.SizeULEB128(Size);
1142 default: assert(0 && "Improper form for block"); break;
1143 }
1144 return 0;
1145}
1146
1147/// AddUInt - Add an unsigned integer value.
1148///
1149void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
1150 DIEInteger *DI = new DIEInteger(Integer);
1151 Values.push_back(DI);
1152 if (Form == 0) Form = DI->BestForm(false);
1153 Forms.push_back(Form);
1154}
1155
1156/// AddSInt - Add an signed integer value.
1157///
1158void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
1159 DIEInteger *DI = new DIEInteger(Integer);
1160 Values.push_back(DI);
1161 if (Form == 0) Form = DI->BestForm(true);
1162 Forms.push_back(Form);
1163}
1164
1165/// AddString - Add a std::string value.
1166///
1167void DIEBlock::AddString(unsigned Form, const std::string &String) {
1168 Values.push_back(new DIEString(String));
1169 Forms.push_back(Form);
1170}
1171
1172/// AddLabel - Add a Dwarf label value.
1173///
1174void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
1175 Values.push_back(new DIEDwarfLabel(Label));
1176 Forms.push_back(Form);
1177}
1178
1179/// AddObjectLabel - Add a non-Dwarf label value.
1180///
1181void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
1182 Values.push_back(new DIEObjectLabel(Label));
1183 Forms.push_back(Form);
1184}
1185
1186/// AddDelta - Add a label delta value.
1187///
1188void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
1189 Values.push_back(new DIEDelta(Hi, Lo));
1190 Forms.push_back(Form);
1191}
1192
1193/// AddDIEntry - Add a DIE value.
1194///
1195void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
1196 Values.push_back(new DIEntry(Entry));
1197 Forms.push_back(Form);
1198}
1199
1200//===----------------------------------------------------------------------===//
1201
Jim Laskey0420f2a2006-02-22 19:02:11 +00001202DIE::DIE(unsigned Tag)
1203: Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
Jim Laskeyd18e2892006-01-20 20:34:06 +00001204, AbbrevID(0)
1205, Offset(0)
1206, Size(0)
Jim Laskeyd18e2892006-01-20 20:34:06 +00001207, Children()
1208, Values()
1209{}
1210
1211DIE::~DIE() {
1212 if (Abbrev) delete Abbrev;
1213
Jim Laskey52060a02006-01-24 00:49:18 +00001214 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001215 delete Children[i];
1216 }
1217
Jim Laskey52060a02006-01-24 00:49:18 +00001218 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001219 delete Values[j];
1220 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00001221}
1222
Jim Laskeyb8509c52006-03-23 18:07:55 +00001223/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
1224///
1225void DIE::AddSiblingOffset() {
1226 DIEInteger *DI = new DIEInteger(0);
1227 Values.insert(Values.begin(), DI);
1228 Abbrev->AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
1229}
1230
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001231/// AddUInt - Add an unsigned integer attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001232///
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001233void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001234 DIEInteger *DI = new DIEInteger(Integer);
1235 Values.push_back(DI);
1236 if (!Form) Form = DI->BestForm(false);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001237 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001238}
1239
1240/// AddSInt - Add an signed integer attribute data and value.
1241///
1242void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001243 DIEInteger *DI = new DIEInteger(Integer);
1244 Values.push_back(DI);
1245 if (!Form) Form = DI->BestForm(true);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001246 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001247}
1248
1249/// AddString - Add a std::string attribute data and value.
1250///
1251void DIE::AddString(unsigned Attribute, unsigned Form,
1252 const std::string &String) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001253 Values.push_back(new DIEString(String));
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001254 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001255}
1256
1257/// AddLabel - Add a Dwarf label attribute data and value.
1258///
1259void DIE::AddLabel(unsigned Attribute, unsigned Form,
1260 const DWLabel &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +00001261 Values.push_back(new DIEDwarfLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001262 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001263}
1264
Jim Laskey52060a02006-01-24 00:49:18 +00001265/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001266///
Jim Laskey52060a02006-01-24 00:49:18 +00001267void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
1268 const std::string &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +00001269 Values.push_back(new DIEObjectLabel(Label));
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001270 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001271}
1272
1273/// AddDelta - Add a label delta attribute data and value.
1274///
1275void DIE::AddDelta(unsigned Attribute, unsigned Form,
1276 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001277 Values.push_back(new DIEDelta(Hi, Lo));
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001278 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001279}
1280
1281/// AddDIEntry - Add a DIE attribute data and value.
1282///
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001283void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001284 Values.push_back(new DIEntry(Entry));
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001285 Abbrev->AddAttribute(Attribute, Form);
1286}
1287
1288/// AddBlock - Add block data.
1289///
1290void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
1291 assert(Block->Size && "Block size has not been computed");
1292 Values.push_back(Block);
1293 if (!Form) Form = Block->BestForm();
1294 Abbrev->AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001295}
1296
1297/// Complete - Indicate that all attributes have been added and ready to get an
1298/// abbreviation ID.
Jim Laskey65195462006-10-30 13:35:07 +00001299void DIE::Complete(Dwarf &DW) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001300 AbbrevID = DW.NewAbbreviation(Abbrev);
1301 delete Abbrev;
1302 Abbrev = NULL;
1303}
1304
1305/// AddChild - Add a child to the DIE.
1306///
1307void DIE::AddChild(DIE *Child) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001308 assert(Abbrev && "Adding children without an abbreviation");
1309 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001310 Children.push_back(Child);
1311}
1312
1313//===----------------------------------------------------------------------===//
1314
Jim Laskey65195462006-10-30 13:35:07 +00001315/// Dwarf
Jim Laskeyd18e2892006-01-20 20:34:06 +00001316
1317//===----------------------------------------------------------------------===//
Jim Laskey063e7652006-01-17 17:31:53 +00001318
1319/// PrintHex - Print a value as a hexidecimal value.
1320///
Jim Laskey65195462006-10-30 13:35:07 +00001321void Dwarf::PrintHex(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001322 O << "0x" << std::hex << Value << std::dec;
1323}
1324
1325/// EOL - Print a newline character to asm stream. If a comment is present
1326/// then it will be printed first. Comments should not contain '\n'.
Jim Laskey65195462006-10-30 13:35:07 +00001327void Dwarf::EOL(const std::string &Comment) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001328 if (DwarfVerbose && !Comment.empty()) {
Jim Laskey063e7652006-01-17 17:31:53 +00001329 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00001330 << TAI->getCommentString()
Jim Laskey063e7652006-01-17 17:31:53 +00001331 << " "
1332 << Comment;
1333 }
1334 O << "\n";
1335}
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001336
Jim Laskeyb8509c52006-03-23 18:07:55 +00001337/// EmitAlign - Print a align directive.
1338///
Jim Laskey65195462006-10-30 13:35:07 +00001339void Dwarf::EmitAlign(unsigned Alignment) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001340 O << TAI->getAlignDirective() << Alignment << "\n";
Jim Laskeyb8509c52006-03-23 18:07:55 +00001341}
1342
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001343/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
Jim Laskey063e7652006-01-17 17:31:53 +00001344/// unsigned leb128 value.
Jim Laskey65195462006-10-30 13:35:07 +00001345void Dwarf::EmitULEB128Bytes(unsigned Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001346 if (TAI->hasLEB128()) {
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001347 O << "\t.uleb128\t"
1348 << Value;
1349 } else {
Jim Laskey563321a2006-09-06 18:34:40 +00001350 O << TAI->getData8bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001351 PrintULEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001352 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001353}
1354
1355/// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
Jim Laskey063e7652006-01-17 17:31:53 +00001356/// signed leb128 value.
Jim Laskey65195462006-10-30 13:35:07 +00001357void Dwarf::EmitSLEB128Bytes(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001358 if (TAI->hasLEB128()) {
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001359 O << "\t.sleb128\t"
1360 << Value;
1361 } else {
Jim Laskey563321a2006-09-06 18:34:40 +00001362 O << TAI->getData8bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001363 PrintSLEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001364 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001365}
1366
Jim Laskey063e7652006-01-17 17:31:53 +00001367/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +00001368/// representing an unsigned leb128 value.
Jim Laskey65195462006-10-30 13:35:07 +00001369void Dwarf::PrintULEB128(unsigned Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001370 do {
1371 unsigned Byte = Value & 0x7f;
1372 Value >>= 7;
1373 if (Value) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001374 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001375 if (Value) O << ", ";
1376 } while (Value);
1377}
1378
Jim Laskey063e7652006-01-17 17:31:53 +00001379/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
1380/// value.
Jim Laskey65195462006-10-30 13:35:07 +00001381unsigned Dwarf::SizeULEB128(unsigned Value) {
Jim Laskey063e7652006-01-17 17:31:53 +00001382 unsigned Size = 0;
1383 do {
1384 Value >>= 7;
1385 Size += sizeof(int8_t);
1386 } while (Value);
1387 return Size;
1388}
1389
1390/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +00001391/// representing a signed leb128 value.
Jim Laskey65195462006-10-30 13:35:07 +00001392void Dwarf::PrintSLEB128(int Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001393 int Sign = Value >> (8 * sizeof(Value) - 1);
1394 bool IsMore;
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001395
Jim Laskeyb2efb852006-01-04 22:28:25 +00001396 do {
1397 unsigned Byte = Value & 0x7f;
1398 Value >>= 7;
1399 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1400 if (IsMore) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001401 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001402 if (IsMore) O << ", ";
1403 } while (IsMore);
1404}
1405
Jim Laskey063e7652006-01-17 17:31:53 +00001406/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
1407/// value.
Jim Laskey65195462006-10-30 13:35:07 +00001408unsigned Dwarf::SizeSLEB128(int Value) {
Jim Laskey063e7652006-01-17 17:31:53 +00001409 unsigned Size = 0;
1410 int Sign = Value >> (8 * sizeof(Value) - 1);
1411 bool IsMore;
1412
1413 do {
1414 unsigned Byte = Value & 0x7f;
1415 Value >>= 7;
1416 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1417 Size += sizeof(int8_t);
1418 } while (IsMore);
1419 return Size;
1420}
1421
Jim Laskeyda427fa2006-01-27 20:31:25 +00001422/// EmitInt8 - Emit a byte directive and value.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001423///
Jim Laskey65195462006-10-30 13:35:07 +00001424void Dwarf::EmitInt8(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001425 O << TAI->getData8bitsDirective();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001426 PrintHex(Value & 0xFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001427}
1428
Jim Laskeyda427fa2006-01-27 20:31:25 +00001429/// EmitInt16 - Emit a short directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001430///
Jim Laskey65195462006-10-30 13:35:07 +00001431void Dwarf::EmitInt16(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001432 O << TAI->getData16bitsDirective();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001433 PrintHex(Value & 0xFFFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001434}
1435
Jim Laskeyda427fa2006-01-27 20:31:25 +00001436/// EmitInt32 - Emit a long directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001437///
Jim Laskey65195462006-10-30 13:35:07 +00001438void Dwarf::EmitInt32(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001439 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001440 PrintHex(Value);
1441}
1442
Jim Laskeyda427fa2006-01-27 20:31:25 +00001443/// EmitInt64 - Emit a long long directive and value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001444///
Jim Laskey65195462006-10-30 13:35:07 +00001445void Dwarf::EmitInt64(uint64_t Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001446 if (TAI->getData64bitsDirective()) {
1447 O << TAI->getData64bitsDirective() << "0x" << std::hex << Value << std::dec;
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001448 } else {
Owen Andersona69571c2006-05-03 01:29:57 +00001449 if (TD->isBigEndian()) {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001450 EmitInt32(unsigned(Value >> 32)); O << "\n";
1451 EmitInt32(unsigned(Value));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001452 } else {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001453 EmitInt32(unsigned(Value)); O << "\n";
1454 EmitInt32(unsigned(Value >> 32));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001455 }
1456 }
1457}
1458
Jim Laskey063e7652006-01-17 17:31:53 +00001459/// EmitString - Emit a string with quotes and a null terminator.
1460/// Special characters are emitted properly. (Eg. '\t')
Jim Laskey65195462006-10-30 13:35:07 +00001461void Dwarf::EmitString(const std::string &String) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001462 O << TAI->getAsciiDirective()
Jim Laskey063e7652006-01-17 17:31:53 +00001463 << "\"";
Jim Laskey52060a02006-01-24 00:49:18 +00001464 for (unsigned i = 0, N = String.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001465 unsigned char C = String[i];
1466
1467 if (!isascii(C) || iscntrl(C)) {
1468 switch(C) {
1469 case '\b': O << "\\b"; break;
1470 case '\f': O << "\\f"; break;
1471 case '\n': O << "\\n"; break;
1472 case '\r': O << "\\r"; break;
1473 case '\t': O << "\\t"; break;
1474 default:
1475 O << '\\';
Jim Laskey3ebe71d2006-09-01 12:55:05 +00001476 O << char('0' + ((C >> 6) & 7));
1477 O << char('0' + ((C >> 3) & 7));
1478 O << char('0' + ((C >> 0) & 7));
Jim Laskey063e7652006-01-17 17:31:53 +00001479 break;
1480 }
1481 } else if (C == '\"') {
1482 O << "\\\"";
1483 } else if (C == '\'') {
1484 O << "\\\'";
1485 } else {
1486 O << C;
1487 }
1488 }
1489 O << "\\0\"";
1490}
1491
1492/// PrintLabelName - Print label name in form used by Dwarf writer.
1493///
Jim Laskey65195462006-10-30 13:35:07 +00001494void Dwarf::PrintLabelName(const char *Tag, unsigned Number) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001495 O << TAI->getPrivateGlobalPrefix()
Jim Laskeyb2efb852006-01-04 22:28:25 +00001496 << "debug_"
Jim Laskey0420f2a2006-02-22 19:02:11 +00001497 << Tag;
1498 if (Number) O << Number;
Jim Laskeyb2efb852006-01-04 22:28:25 +00001499}
1500
Jim Laskey063e7652006-01-17 17:31:53 +00001501/// EmitLabel - Emit location label for internal use by Dwarf.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001502///
Jim Laskey65195462006-10-30 13:35:07 +00001503void Dwarf::EmitLabel(const char *Tag, unsigned Number) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001504 PrintLabelName(Tag, Number);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001505 O << ":\n";
1506}
1507
Jim Laskeye719a7c2006-01-18 16:54:26 +00001508/// EmitReference - Emit a reference to a label.
Jim Laskey063e7652006-01-17 17:31:53 +00001509///
Jim Laskey65195462006-10-30 13:35:07 +00001510void Dwarf::EmitReference(const char *Tag, unsigned Number) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001511 if (TAI->getAddressSize() == 4)
1512 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001513 else
Jim Laskey563321a2006-09-06 18:34:40 +00001514 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001515
1516 PrintLabelName(Tag, Number);
1517}
Jim Laskey65195462006-10-30 13:35:07 +00001518void Dwarf::EmitReference(const std::string &Name) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001519 if (TAI->getAddressSize() == 4)
1520 O << TAI->getData32bitsDirective();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001521 else
Jim Laskey563321a2006-09-06 18:34:40 +00001522 O << TAI->getData64bitsDirective();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001523
1524 O << Name;
1525}
Jim Laskey063e7652006-01-17 17:31:53 +00001526
1527/// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
1528/// assemblers do not accept absolute expressions with data directives, so there
1529/// is an option (needsSet) to use an intermediary 'set' expression.
Jim Laskey65195462006-10-30 13:35:07 +00001530void Dwarf::EmitDifference(const char *TagHi, unsigned NumberHi,
Jim Laskeyd18e2892006-01-20 20:34:06 +00001531 const char *TagLo, unsigned NumberLo) const {
Jim Laskeya0f3d172006-09-07 22:06:40 +00001532 if (TAI->needsSet()) {
Jim Laskey063e7652006-01-17 17:31:53 +00001533 static unsigned SetCounter = 0;
Jim Laskeyd18e2892006-01-20 20:34:06 +00001534
Jim Laskey063e7652006-01-17 17:31:53 +00001535 O << "\t.set\t";
1536 PrintLabelName("set", SetCounter);
1537 O << ",";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001538 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001539 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001540 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001541 O << "\n";
1542
Jim Laskey563321a2006-09-06 18:34:40 +00001543 if (TAI->getAddressSize() == sizeof(int32_t))
1544 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001545 else
Jim Laskey563321a2006-09-06 18:34:40 +00001546 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001547
1548 PrintLabelName("set", SetCounter);
1549
Jim Laskey52060a02006-01-24 00:49:18 +00001550 ++SetCounter;
Jim Laskey063e7652006-01-17 17:31:53 +00001551 } else {
Jim Laskey563321a2006-09-06 18:34:40 +00001552 if (TAI->getAddressSize() == sizeof(int32_t))
1553 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001554 else
Jim Laskey563321a2006-09-06 18:34:40 +00001555 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001556
Jim Laskeyd18e2892006-01-20 20:34:06 +00001557 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001558 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001559 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001560 }
1561}
1562
Jim Laskeyd18e2892006-01-20 20:34:06 +00001563/// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
Jim Laskey063e7652006-01-17 17:31:53 +00001564///
Jim Laskey65195462006-10-30 13:35:07 +00001565unsigned Dwarf::NewAbbreviation(DIEAbbrev *Abbrev) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001566 return Abbreviations.insert(*Abbrev);
1567}
1568
1569/// NewString - Add a string to the constant pool and returns a label.
1570///
Jim Laskey65195462006-10-30 13:35:07 +00001571DWLabel Dwarf::NewString(const std::string &String) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001572 unsigned StringID = StringPool.insert(String);
1573 return DWLabel("string", StringID);
1574}
1575
Jim Laskeyb8509c52006-03-23 18:07:55 +00001576/// AddSourceLine - Add location information to specified debug information
1577/// entry.
Jim Laskey65195462006-10-30 13:35:07 +00001578void Dwarf::AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line){
Jim Laskeyb8509c52006-03-23 18:07:55 +00001579 if (File && Line) {
1580 CompileUnit *FileUnit = FindCompileUnit(File);
1581 unsigned FileID = FileUnit->getID();
1582 Die->AddUInt(DW_AT_decl_file, 0, FileID);
1583 Die->AddUInt(DW_AT_decl_line, 0, Line);
1584 }
1585}
1586
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001587/// AddAddress - Add an address attribute to a die based on the location
1588/// provided.
Jim Laskey65195462006-10-30 13:35:07 +00001589void Dwarf::AddAddress(DIE *Die, unsigned Attribute,
Jim Laskey41886992006-04-07 16:34:46 +00001590 const MachineLocation &Location) {
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001591 DIEBlock *Block = new DIEBlock();
Jim Laskey3d3d4042006-08-25 19:39:52 +00001592 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1593
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001594 if (Location.isRegister()) {
Jim Laskey3d3d4042006-08-25 19:39:52 +00001595 if (Reg < 32) {
1596 Block->AddUInt(DW_FORM_data1, DW_OP_reg0 + Reg);
1597 } else {
1598 Block->AddUInt(DW_FORM_data1, DW_OP_regx);
1599 Block->AddUInt(DW_FORM_udata, Reg);
1600 }
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001601 } else {
Jim Laskey3d3d4042006-08-25 19:39:52 +00001602 if (Reg < 32) {
1603 Block->AddUInt(DW_FORM_data1, DW_OP_breg0 + Reg);
1604 } else {
1605 Block->AddUInt(DW_FORM_data1, DW_OP_bregx);
1606 Block->AddUInt(DW_FORM_udata, Reg);
1607 }
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001608 Block->AddUInt(DW_FORM_sdata, Location.getOffset());
1609 }
1610 Block->ComputeSize(*this);
1611 Die->AddBlock(Attribute, 0, Block);
1612}
1613
Jim Laskey90c79d72006-03-23 23:02:34 +00001614/// getDieMapSlotFor - Returns the debug information entry map slot for the
1615/// specified debug descriptor.
Jim Laskey65195462006-10-30 13:35:07 +00001616DIE *&Dwarf::getDieMapSlotFor(DebugInfoDesc *DD) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001617 return DescToDieMap[DD];
Jim Laskey0420f2a2006-02-22 19:02:11 +00001618}
Jim Laskey760383e2006-08-21 21:20:18 +00001619
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001620/// NewType - Create a new type DIE.
1621///
Jim Laskey65195462006-10-30 13:35:07 +00001622DIE *Dwarf::NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001623 if (!TyDesc) {
1624 // FIXME - Hack for missing types
1625 DIE *Die = new DIE(DW_TAG_base_type);
1626 Die->AddUInt(DW_AT_byte_size, 0, 4);
1627 Die->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1628 Unit->getDie()->AddChild(Die);
1629 return Die;
1630 }
Jim Laskey0c0feb92006-10-04 10:40:15 +00001631
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001632 // Check for pre-existence.
Jim Laskey90c79d72006-03-23 23:02:34 +00001633 DIE *&Slot = Unit->getDieMapSlotFor(TyDesc);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001634 if (Slot) return Slot;
1635
Jim Laskey0c0feb92006-10-04 10:40:15 +00001636 // Type DIE result.
1637 DIE *Ty = NULL;
1638
Jim Laskey339ec4c2006-10-13 13:02:19 +00001639 // FIXME - Not sure why programs and variables are coming through here.
Jim Laskey0c0feb92006-10-04 10:40:15 +00001640 // Short cut for handling subprogram types (not really a TyDesc.)
1641 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1642 Slot = Ty = new DIE(DW_TAG_pointer_type);
1643 Ty->AddUInt(DW_AT_byte_size, 0, TAI->getAddressSize());
1644 Ty->AddString(DW_AT_name, DW_FORM_string, SubprogramTy->getName());
1645 Context->AddChild(Ty);
1646 return Slot;
1647 }
Jim Laskey339ec4c2006-10-13 13:02:19 +00001648 // Short cut for handling global variable types (not really a TyDesc.)
1649 if (GlobalVariableDesc *GlobalVariableTy =
1650 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1651 Slot = Ty = new DIE(DW_TAG_pointer_type);
1652 Ty->AddUInt(DW_AT_byte_size, 0, TAI->getAddressSize());
1653 Ty->AddString(DW_AT_name, DW_FORM_string, GlobalVariableTy->getName());
1654 Context->AddChild(Ty);
1655 return Slot;
1656 }
Jim Laskey0c0feb92006-10-04 10:40:15 +00001657
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001658 // Get core information.
1659 const std::string &Name = TyDesc->getName();
Jim Laskey288fe0f2006-03-01 18:13:05 +00001660 uint64_t Size = TyDesc->getSize() >> 3;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001661
Jim Laskey434b40b2006-02-23 22:37:30 +00001662 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001663 // Fundamental types like int, float, bool
Jim Laskey434b40b2006-02-23 22:37:30 +00001664 Slot = Ty = new DIE(DW_TAG_base_type);
1665 unsigned Encoding = BasicTy->getEncoding();
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001666 Ty->AddUInt(DW_AT_encoding, DW_FORM_data1, Encoding);
Jim Laskey69906002006-02-24 16:46:40 +00001667 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001668 // Create specific DIE.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001669 Slot = Ty = new DIE(DerivedTy->getTag());
Jim Laskey69906002006-02-24 16:46:40 +00001670
1671 // Map to main type, void will not have a type.
1672 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001673 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1674 NewType(Context, FromTy, Unit));
Jim Laskey69906002006-02-24 16:46:40 +00001675 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001676 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
Jim Laskey7089f452006-06-16 13:14:03 +00001677 // Fetch tag
1678 unsigned Tag = CompTy->getTag();
1679
Jim Laskeyf8913f12006-03-01 17:53:02 +00001680 // Create specific DIE.
Jim Laskey7089f452006-06-16 13:14:03 +00001681 Slot = Ty = Tag == DW_TAG_vector_type ? new DIE(DW_TAG_array_type) :
1682 new DIE(Tag);
1683
Jim Laskeyf8913f12006-03-01 17:53:02 +00001684 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1685
Jim Laskey7089f452006-06-16 13:14:03 +00001686 switch (Tag) {
1687 case DW_TAG_vector_type: Ty->AddUInt(DW_AT_GNU_vector, DW_FORM_flag, 1);
1688 // Fall thru
Jim Laskey9c4447a2006-03-01 20:39:36 +00001689 case DW_TAG_array_type: {
Jim Laskeyf8913f12006-03-01 17:53:02 +00001690 // Add element type.
1691 if (TypeDesc *FromTy = CompTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001692 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1693 NewType(Context, FromTy, Unit));
Jim Laskeyf8913f12006-03-01 17:53:02 +00001694 }
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001695
Jim Laskeyf8913f12006-03-01 17:53:02 +00001696 // Don't emit size attribute.
1697 Size = 0;
1698
1699 // Construct an anonymous type for index type.
1700 DIE *IndexTy = new DIE(DW_TAG_base_type);
1701 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1702 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1703 // Add to context.
Jim Laskey92ae7402006-03-01 18:20:30 +00001704 Context->AddChild(IndexTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001705
1706 // Add subranges to array type.
1707 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1708 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1709 int64_t Lo = SRD->getLo();
1710 int64_t Hi = SRD->getHi();
1711 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1712
1713 // If a range is available.
1714 if (Lo != Hi) {
1715 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1716 // Only add low if non-zero.
Jim Laskey6a3eb012006-03-01 23:52:37 +00001717 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1718 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001719 }
1720 Ty->AddChild(Subrange);
1721 }
1722
1723 break;
1724 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001725 case DW_TAG_structure_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +00001726 case DW_TAG_union_type: {
Jim Laskeyf01e5472006-03-03 15:06:57 +00001727 // Add elements to structure type.
1728 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskey760383e2006-08-21 21:20:18 +00001729 DebugInfoDesc *Element = Elements[i];
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001730
Jim Laskey760383e2006-08-21 21:20:18 +00001731 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)) {
1732 // Add field or base class.
Jim Laskey54689c22006-03-09 13:28:47 +00001733
Jim Laskey760383e2006-08-21 21:20:18 +00001734 unsigned Tag = MemberDesc->getTag();
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001735
Jim Laskey760383e2006-08-21 21:20:18 +00001736 // Extract the basic information.
1737 const std::string &Name = MemberDesc->getName();
1738 TypeDesc *MemTy = MemberDesc->getFromType();
1739 uint64_t Size = MemberDesc->getSize();
1740 uint64_t Align = MemberDesc->getAlign();
1741 uint64_t Offset = MemberDesc->getOffset();
1742
1743 // Construct member debug information entry.
1744 DIE *Member = new DIE(Tag);
1745
1746 // Add name if not "".
1747 if (!Name.empty())Member->AddString(DW_AT_name, DW_FORM_string, Name);
1748 // Add location if available.
1749 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1750
1751 // Most of the time the field info is the same as the members.
1752 uint64_t FieldSize = Size;
1753 uint64_t FieldAlign = Align;
1754 uint64_t FieldOffset = Offset;
1755
1756 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1757 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1758 NewType(Context, FromTy, Unit));
1759 FieldSize = FromTy->getSize();
1760 FieldAlign = FromTy->getSize();
1761 }
1762
1763 // Unless we have a bit field.
1764 if (Tag == DW_TAG_member && FieldSize != Size) {
1765 // Construct the alignment mask.
1766 uint64_t AlignMask = ~(FieldAlign - 1);
1767 // Determine the high bit + 1 of the declared size.
1768 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1769 // Work backwards to determine the base offset of the field.
1770 FieldOffset = HiMark - FieldSize;
1771 // Now normalize offset to the field.
1772 Offset -= FieldOffset;
1773
1774 // Maybe we need to work from the other end.
1775 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1776
1777 // Add size and offset.
1778 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1779 Member->AddUInt(DW_AT_bit_size, 0, Size);
1780 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
1781 }
1782
1783 // Add computation for offset.
1784 DIEBlock *Block = new DIEBlock();
1785 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
1786 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
1787 Block->ComputeSize(*this);
1788 Member->AddBlock(DW_AT_data_member_location, 0, Block);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001789
Jim Laskey760383e2006-08-21 21:20:18 +00001790 // Add accessibility (public default unless is base class.
1791 if (MemberDesc->isProtected()) {
1792 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_protected);
1793 } else if (MemberDesc->isPrivate()) {
1794 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_private);
1795 } else if (Tag == DW_TAG_inheritance) {
1796 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_public);
1797 }
1798
1799 Ty->AddChild(Member);
1800 } else if (GlobalVariableDesc *StaticDesc =
1801 dyn_cast<GlobalVariableDesc>(Element)) {
1802 // Add static member.
1803
1804 // Construct member debug information entry.
1805 DIE *Static = new DIE(DW_TAG_variable);
1806
1807 // Add name and mangled name.
1808 const std::string &Name = StaticDesc->getDisplayName();
1809 const std::string &MangledName = StaticDesc->getName();
1810 Static->AddString(DW_AT_name, DW_FORM_string, Name);
1811 Static->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1812 MangledName);
1813
1814 // Add location.
1815 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1816
1817 // Add type.
1818 if (TypeDesc *StaticTy = StaticDesc->getType()) {
1819 Static->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1820 NewType(Context, StaticTy, Unit));
1821 }
1822
1823 // Add flags.
1824 Static->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1825 Static->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1826
1827 Ty->AddChild(Static);
1828 } else if (SubprogramDesc *MethodDesc =
1829 dyn_cast<SubprogramDesc>(Element)) {
1830 // Add member function.
1831
1832 // Construct member debug information entry.
1833 DIE *Method = new DIE(DW_TAG_subprogram);
1834
1835 // Add name and mangled name.
1836 const std::string &Name = MethodDesc->getDisplayName();
1837 const std::string &MangledName = MethodDesc->getName();
1838 bool IsCTor = false;
1839
1840 if (Name.empty()) {
1841 Method->AddString(DW_AT_name, DW_FORM_string, MangledName);
1842 IsCTor = TyDesc->getName() == MangledName;
1843 } else {
1844 Method->AddString(DW_AT_name, DW_FORM_string, Name);
1845 Method->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1846 MangledName);
1847 }
1848
1849 // Add location.
1850 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1851
1852 // Add type.
1853 if (CompositeTypeDesc *MethodTy =
1854 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1855 // Get argument information.
1856 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1857
1858 // If not a ctor.
1859 if (!IsCTor) {
1860 // Add return type.
1861 Method->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1862 NewType(Context, dyn_cast<TypeDesc>(Args[0]),
1863 Unit));
1864 }
1865
1866 // Add arguments.
1867 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1868 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1869 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1870 NewType(Context, cast<TypeDesc>(Args[i]), Unit));
1871 Arg->AddUInt(DW_AT_artificial, DW_FORM_flag, 1);
1872 Method->AddChild(Arg);
1873 }
1874 }
1875
1876 // Add flags.
1877 Method->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1878 Method->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1879
1880 Ty->AddChild(Method);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001881 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001882 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001883 break;
1884 }
Jim Laskey9c4447a2006-03-01 20:39:36 +00001885 case DW_TAG_enumeration_type: {
Jim Laskey6a3eb012006-03-01 23:52:37 +00001886 // Add enumerators to enumeration type.
1887 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1888 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1889 const std::string &Name = ED->getName();
1890 int64_t Value = ED->getValue();
1891 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1892 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1893 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1894 Ty->AddChild(Enumerator);
1895 }
1896
Jim Laskeyf8913f12006-03-01 17:53:02 +00001897 break;
1898 }
Jim Laskey650f6092006-06-20 19:41:06 +00001899 case DW_TAG_subroutine_type: {
1900 // Add prototype flag.
1901 Ty->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
1902 // Add return type.
1903 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
Jim Laskeyd04c1592006-07-13 15:27:42 +00001904 NewType(Context, dyn_cast<TypeDesc>(Elements[0]), Unit));
Jim Laskey650f6092006-06-20 19:41:06 +00001905
1906 // Add arguments.
1907 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1908 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1909 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1910 NewType(Context, cast<TypeDesc>(Elements[i]), Unit));
1911 Ty->AddChild(Arg);
1912 }
1913
1914 break;
1915 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001916 default: break;
1917 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001918 }
Jim Laskey0c0feb92006-10-04 10:40:15 +00001919
Jim Laskey434b40b2006-02-23 22:37:30 +00001920 assert(Ty && "Type not supported yet");
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001921
Jim Laskey69906002006-02-24 16:46:40 +00001922 // Add size if non-zero (derived types don't have a size.)
Jim Laskey434b40b2006-02-23 22:37:30 +00001923 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
Jim Laskey69906002006-02-24 16:46:40 +00001924 // Add name if not anonymous or intermediate type.
Jim Laskey434b40b2006-02-23 22:37:30 +00001925 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001926 // Add source line info if available.
1927 AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
Jim Laskey434b40b2006-02-23 22:37:30 +00001928
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001929 // Add to context owner.
Jim Laskey92ae7402006-03-01 18:20:30 +00001930 Context->AddChild(Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001931
1932 return Slot;
1933}
1934
Jim Laskeyb8509c52006-03-23 18:07:55 +00001935/// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey063e7652006-01-17 17:31:53 +00001936///
Jim Laskey65195462006-10-30 13:35:07 +00001937CompileUnit *Dwarf::NewCompileUnit(CompileUnitDesc *UnitDesc,
Jim Laskeybd761842006-02-27 17:27:12 +00001938 unsigned ID) {
1939 // Construct debug information entry.
1940 DIE *Die = new DIE(DW_TAG_compile_unit);
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001941 Die->AddDelta (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0),
1942 DWLabel("section_line", 0));
Jim Laskey89d67fa2006-06-23 12:51:53 +00001943// Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1944// Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
Jim Laskeybd761842006-02-27 17:27:12 +00001945 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1946 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1947 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1948 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1949
Jim Laskeyb8509c52006-03-23 18:07:55 +00001950 // Add debug information entry to descriptor map.
Jim Laskey90c79d72006-03-23 23:02:34 +00001951 DIE *&Slot = getDieMapSlotFor(UnitDesc);
1952 Slot = Die;
Jim Laskeybd761842006-02-27 17:27:12 +00001953
1954 // Construct compile unit.
1955 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1956
1957 // Add Unit to compile unit map.
1958 DescToUnitMap[UnitDesc] = Unit;
1959
1960 return Unit;
1961}
Jim Laskey0420f2a2006-02-22 19:02:11 +00001962
Jim Laskeybd761842006-02-27 17:27:12 +00001963/// FindCompileUnit - Get the compile unit for the given descriptor.
1964///
Jim Laskey65195462006-10-30 13:35:07 +00001965CompileUnit *Dwarf::FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeybd761842006-02-27 17:27:12 +00001966 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1967 assert(Unit && "Missing compile unit.");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001968 return Unit;
Jim Laskey063e7652006-01-17 17:31:53 +00001969}
1970
Jim Laskey0420f2a2006-02-22 19:02:11 +00001971/// NewGlobalVariable - Add a new global variable DIE.
1972///
Jim Laskey65195462006-10-30 13:35:07 +00001973DIE *Dwarf::NewGlobalVariable(GlobalVariableDesc *GVD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001974 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001975 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1976 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey90c79d72006-03-23 23:02:34 +00001977
1978 // Check for pre-existence.
1979 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1980 if (Slot) return Slot;
1981
Jim Laskey0420f2a2006-02-22 19:02:11 +00001982 // Get the global variable itself.
1983 GlobalVariable *GV = GVD->getGlobalVariable();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001984
Jim Laskeye85fb672006-09-18 14:47:26 +00001985 const std::string &Name = GVD->hasMangledName() ? GVD->getDisplayName()
1986 : GVD->getName();
1987 const std::string &MangledName = GVD->hasMangledName() ? GVD->getName()
1988 : "";
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001989 // Get the global's type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001990 DIE *Type = NewType(Unit->getDie(), GVD->getType(), Unit);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001991
1992 // Create the globale variable DIE.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001993 DIE *VariableDie = new DIE(DW_TAG_variable);
Jim Laskeye85fb672006-09-18 14:47:26 +00001994 VariableDie->AddString(DW_AT_name, DW_FORM_string, Name);
1995 if (!MangledName.empty()) {
1996 VariableDie->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1997 MangledName);
1998 }
1999 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
2000 VariableDie->AddUInt(DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002001
2002 // Add source line info if available.
2003 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Jim Laskeyba8a2ee2006-10-16 19:38:41 +00002004
2005 // Work up linkage name.
Jim Laskey99e41ee2006-10-17 17:17:24 +00002006 const std::string LinkageName = Asm->getGlobalLinkName(GV);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002007
Jim Laskeyb8509c52006-03-23 18:07:55 +00002008 // Add address.
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002009 DIEBlock *Block = new DIEBlock();
2010 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
Jim Laskeyba8a2ee2006-10-16 19:38:41 +00002011 Block->AddObjectLabel(DW_FORM_udata, LinkageName);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00002012 Block->ComputeSize(*this);
2013 VariableDie->AddBlock(DW_AT_location, 0, Block);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002014
2015 // Add to map.
2016 Slot = VariableDie;
2017
2018 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00002019 Unit->getDie()->AddChild(VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002020
2021 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00002022 // FIXME - need to check external flag.
2023 Unit->AddGlobal(Name, VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002024
2025 return VariableDie;
2026}
2027
2028/// NewSubprogram - Add a new subprogram DIE.
2029///
Jim Laskey65195462006-10-30 13:35:07 +00002030DIE *Dwarf::NewSubprogram(SubprogramDesc *SPD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002031 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00002032 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
2033 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002034
Jim Laskey90c79d72006-03-23 23:02:34 +00002035 // Check for pre-existence.
2036 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
2037 if (Slot) return Slot;
2038
Jim Laskey0420f2a2006-02-22 19:02:11 +00002039 // Gather the details (simplify add attribute code.)
Jim Laskeye85fb672006-09-18 14:47:26 +00002040 const std::string &Name = SPD->hasMangledName() ? SPD->getDisplayName()
2041 : SPD->getName();
2042 const std::string &MangledName = SPD->hasMangledName() ? SPD->getName()
2043 : "";
Jim Laskey90c79d72006-03-23 23:02:34 +00002044 DIE *Type = NewType(Unit->getDie(), SPD->getType(), Unit);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00002045 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
Jim Laskey0420f2a2006-02-22 19:02:11 +00002046
Jim Laskey8a8e9752006-02-27 20:37:42 +00002047 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
Jim Laskeye85fb672006-09-18 14:47:26 +00002048 SubprogramDie->AddString(DW_AT_name, DW_FORM_string, Name);
2049 if (!MangledName.empty()) {
2050 SubprogramDie->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
2051 MangledName);
Jim Laskey41886992006-04-07 16:34:46 +00002052 }
Jim Laskeye85fb672006-09-18 14:47:26 +00002053 if (Type) {
2054 SubprogramDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
2055 }
2056 SubprogramDie->AddUInt(DW_AT_external, DW_FORM_flag, IsExternal);
2057 SubprogramDie->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002058
Jim Laskeyb8509c52006-03-23 18:07:55 +00002059 // Add source line info if available.
2060 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
2061
Jim Laskey0420f2a2006-02-22 19:02:11 +00002062 // Add to map.
2063 Slot = SubprogramDie;
2064
2065 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00002066 Unit->getDie()->AddChild(SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002067
2068 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00002069 Unit->AddGlobal(Name, SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002070
2071 return SubprogramDie;
2072}
2073
Jim Laskeyb8509c52006-03-23 18:07:55 +00002074/// NewScopeVariable - Create a new scope variable.
2075///
Jim Laskey65195462006-10-30 13:35:07 +00002076DIE *Dwarf::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002077 // Get the descriptor.
2078 VariableDesc *VD = DV->getDesc();
2079
2080 // Translate tag to proper Dwarf tag. The result variable is dropped for now.
2081 unsigned Tag;
2082 switch (VD->getTag()) {
2083 case DW_TAG_return_variable: return NULL;
2084 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2085 case DW_TAG_auto_variable: // fall thru
2086 default: Tag = DW_TAG_variable; break;
2087 }
2088
2089 // Define variable debug information entry.
2090 DIE *VariableDie = new DIE(Tag);
2091 VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
2092
2093 // Add source line info if available.
2094 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
2095
2096 // Add variable type.
Jim Laskey90c79d72006-03-23 23:02:34 +00002097 DIE *Type = NewType(Unit->getDie(), VD->getType(), Unit);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002098 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
2099
Jim Laskeyb3e7be22006-03-28 14:58:32 +00002100 // Add variable address.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002101 MachineLocation Location;
Jim Laskey41886992006-04-07 16:34:46 +00002102 RI->getLocation(*MF, DV->getFrameIndex(), Location);
Jim Laskeyb3e7be22006-03-28 14:58:32 +00002103 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002104
2105 return VariableDie;
2106}
2107
2108/// ConstructScope - Construct the components of a scope.
2109///
Jim Laskey65195462006-10-30 13:35:07 +00002110void Dwarf::ConstructScope(DebugScope *ParentScope,
Jim Laskeyb8509c52006-03-23 18:07:55 +00002111 DIE *ParentDie, CompileUnit *Unit) {
2112 // Add variables to scope.
2113 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
2114 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2115 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
2116 if (VariableDie) ParentDie->AddChild(VariableDie);
2117 }
2118
2119 // Add nested scopes.
2120 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
2121 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2122 // Define the Scope debug information entry.
2123 DebugScope *Scope = Scopes[j];
2124 // FIXME - Ignore inlined functions for the time being.
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002125 if (!Scope->getParent()) continue;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002126
Jim Laskey66ebf092006-10-23 14:56:37 +00002127 unsigned StartID = Scope->getStartLabelID();
2128 unsigned EndID = Scope->getEndLabelID();
2129
2130 // Throw out scope if block is discarded.
2131 if (StartID && !DebugInfo->isLabelValid(StartID)) continue;
2132 if (EndID && !DebugInfo->isLabelValid(EndID)) continue;
2133
Jim Laskeyb8509c52006-03-23 18:07:55 +00002134 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2135
2136 // Add the scope bounds.
Jim Laskey66ebf092006-10-23 14:56:37 +00002137 if (StartID) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002138 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
2139 DWLabel("loc", StartID));
2140 } else {
2141 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
2142 DWLabel("func_begin", SubprogramCount));
2143 }
Jim Laskey66ebf092006-10-23 14:56:37 +00002144 if (EndID) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002145 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
2146 DWLabel("loc", EndID));
2147 } else {
2148 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
2149 DWLabel("func_end", SubprogramCount));
2150 }
2151
2152 // Add the scope contents.
2153 ConstructScope(Scope, ScopeDie, Unit);
2154 ParentDie->AddChild(ScopeDie);
2155 }
2156}
2157
2158/// ConstructRootScope - Construct the scope for the subprogram.
2159///
Jim Laskey65195462006-10-30 13:35:07 +00002160void Dwarf::ConstructRootScope(DebugScope *RootScope) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002161 // Exit if there is no root scope.
2162 if (!RootScope) return;
2163
2164 // Get the subprogram debug information entry.
2165 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Jim Laskey90c79d72006-03-23 23:02:34 +00002166
2167 // Get the compile unit context.
2168 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey41886992006-04-07 16:34:46 +00002169 CompileUnit *Unit = FindCompileUnit(UnitDesc);
2170
Jim Laskey90c79d72006-03-23 23:02:34 +00002171 // Get the subprogram die.
2172 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002173 assert(SPDie && "Missing subprogram descriptor");
2174
2175 // Add the function bounds.
Jim Laskey6b92b8e2006-04-07 20:44:42 +00002176 SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
2177 DWLabel("func_begin", SubprogramCount));
Jim Laskeyb8509c52006-03-23 18:07:55 +00002178 SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
2179 DWLabel("func_end", SubprogramCount));
Jim Laskey41886992006-04-07 16:34:46 +00002180 MachineLocation Location(RI->getFrameRegister(*MF));
Jim Laskeyb3e7be22006-03-28 14:58:32 +00002181 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002182
Jim Laskeyb8509c52006-03-23 18:07:55 +00002183 ConstructScope(RootScope, SPDie, Unit);
2184}
2185
Jim Laskey063e7652006-01-17 17:31:53 +00002186/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2187/// tools to recognize the object file contains Dwarf information.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002188///
Jim Laskey65195462006-10-30 13:35:07 +00002189void Dwarf::EmitInitial() {
Jim Laskey014f98c2006-06-14 11:35:03 +00002190 // Check to see if we already emitted intial headers.
2191 if (didInitial) return;
2192 didInitial = true;
2193
Jim Laskey063e7652006-01-17 17:31:53 +00002194 // Dwarf sections base addresses.
Jim Laskey563321a2006-09-06 18:34:40 +00002195 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002196 EmitLabel("section_frame", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002197 Asm->SwitchToDataSection(TAI->getDwarfInfoSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002198 EmitLabel("section_info", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002199 EmitLabel("info", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002200 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002201 EmitLabel("section_abbrev", 0);
2202 EmitLabel("abbrev", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002203 Asm->SwitchToDataSection(TAI->getDwarfARangesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002204 EmitLabel("section_aranges", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002205 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002206 EmitLabel("section_macinfo", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002207 Asm->SwitchToDataSection(TAI->getDwarfLineSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002208 EmitLabel("section_line", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002209 EmitLabel("line", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002210 Asm->SwitchToDataSection(TAI->getDwarfLocSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002211 EmitLabel("section_loc", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002212 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002213 EmitLabel("section_pubnames", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002214 Asm->SwitchToDataSection(TAI->getDwarfStrSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002215 EmitLabel("section_str", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002216 Asm->SwitchToDataSection(TAI->getDwarfRangesSection(), 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002217 EmitLabel("section_ranges", 0);
2218
Jim Laskey563321a2006-09-06 18:34:40 +00002219 Asm->SwitchToTextSection(TAI->getTextSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002220 EmitLabel("text_begin", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002221 Asm->SwitchToDataSection(TAI->getDataSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002222 EmitLabel("data_begin", 0);
Jim Laskey89d67fa2006-06-23 12:51:53 +00002223
Jim Laskey014f98c2006-06-14 11:35:03 +00002224 // Emit common frame information.
2225 EmitInitialDebugFrame();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002226}
2227
Jim Laskey063e7652006-01-17 17:31:53 +00002228/// EmitDIE - Recusively Emits a debug information entry.
2229///
Jim Laskey65195462006-10-30 13:35:07 +00002230void Dwarf::EmitDIE(DIE *Die) const {
Jim Laskey063e7652006-01-17 17:31:53 +00002231 // Get the abbreviation for this DIE.
2232 unsigned AbbrevID = Die->getAbbrevID();
2233 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey0d086af2006-02-27 12:43:29 +00002234
2235 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002236
2237 // Emit the code (index) for the abbreviation.
2238 EmitULEB128Bytes(AbbrevID);
2239 EOL(std::string("Abbrev [" +
2240 utostr(AbbrevID) +
Jim Laskey0d086af2006-02-27 12:43:29 +00002241 "] 0x" + utohexstr(Die->getOffset()) +
2242 ":0x" + utohexstr(Die->getSize()) + " " +
2243 TagString(Abbrev.getTag())));
Jim Laskey063e7652006-01-17 17:31:53 +00002244
2245 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00002246 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00002247
2248 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00002249 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002250 unsigned Attr = AbbrevData[i].getAttribute();
2251 unsigned Form = AbbrevData[i].getForm();
Jim Laskey063e7652006-01-17 17:31:53 +00002252 assert(Form && "Too many attributes for DIE (check abbreviation)");
2253
2254 switch (Attr) {
2255 case DW_AT_sibling: {
Jim Laskeyda427fa2006-01-27 20:31:25 +00002256 EmitInt32(Die->SiblingOffset());
Jim Laskey063e7652006-01-17 17:31:53 +00002257 break;
2258 }
2259 default: {
2260 // Emit an attribute using the defined form.
2261 Values[i]->EmitValue(*this, Form);
2262 break;
2263 }
2264 }
2265
2266 EOL(AttributeString(Attr));
2267 }
2268
2269 // Emit the DIE children if any.
2270 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
2271 const std::vector<DIE *> &Children = Die->getChildren();
2272
Jim Laskey52060a02006-01-24 00:49:18 +00002273 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskey063e7652006-01-17 17:31:53 +00002274 EmitDIE(Children[j]);
2275 }
2276
Jim Laskeyda427fa2006-01-27 20:31:25 +00002277 EmitInt8(0); EOL("End Of Children Mark");
Jim Laskey063e7652006-01-17 17:31:53 +00002278 }
2279}
2280
2281/// SizeAndOffsetDie - Compute the size and offset of a DIE.
2282///
Jim Laskey65195462006-10-30 13:35:07 +00002283unsigned Dwarf::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002284 // Get the children.
2285 const std::vector<DIE *> &Children = Die->getChildren();
2286
2287 // If not last sibling and has children then add sibling offset attribute.
2288 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2289
Jim Laskey0420f2a2006-02-22 19:02:11 +00002290 // Record the abbreviation.
2291 Die->Complete(*this);
2292
Jim Laskey063e7652006-01-17 17:31:53 +00002293 // Get the abbreviation for this DIE.
2294 unsigned AbbrevID = Die->getAbbrevID();
2295 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
2296
2297 // Set DIE offset
2298 Die->setOffset(Offset);
2299
2300 // Start the size with the size of abbreviation code.
2301 Offset += SizeULEB128(AbbrevID);
2302
2303 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00002304 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00002305
2306 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00002307 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00002308 // Size attribute value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002309 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
Jim Laskey063e7652006-01-17 17:31:53 +00002310 }
2311
2312 // Emit the DIE children if any.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002313 if (!Children.empty()) {
2314 assert(Abbrev.getChildrenFlag() == DW_CHILDREN_yes &&
2315 "Children flag not set");
Jim Laskey063e7652006-01-17 17:31:53 +00002316
Jim Laskey52060a02006-01-24 00:49:18 +00002317 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002318 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
Jim Laskey063e7652006-01-17 17:31:53 +00002319 }
2320
2321 // End of children marker.
2322 Offset += sizeof(int8_t);
2323 }
2324
2325 Die->setSize(Offset - Die->getOffset());
2326 return Offset;
2327}
2328
2329/// SizeAndOffsets - Compute the size and offset of all the DIEs.
2330///
Jim Laskey65195462006-10-30 13:35:07 +00002331void Dwarf::SizeAndOffsets() {
Jim Laskey063e7652006-01-17 17:31:53 +00002332
2333 // Process each compile unit.
Jim Laskey52060a02006-01-24 00:49:18 +00002334 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00002335 CompileUnit *Unit = CompileUnits[i];
2336 if (Unit->hasContent()) {
2337 // Compute size of compile unit header
2338 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2339 sizeof(int16_t) + // DWARF version number
2340 sizeof(int32_t) + // Offset Into Abbrev. Section
2341 sizeof(int8_t); // Pointer Size (in bytes)
Jim Laskeyb8509c52006-03-23 18:07:55 +00002342 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
Jim Laskeybd761842006-02-27 17:27:12 +00002343 }
Jim Laskey063e7652006-01-17 17:31:53 +00002344 }
2345}
2346
Jim Laskey41886992006-04-07 16:34:46 +00002347/// EmitFrameMoves - Emit frame instructions to describe the layout of the
2348/// frame.
Jim Laskey65195462006-10-30 13:35:07 +00002349void Dwarf::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Jim Laskey41886992006-04-07 16:34:46 +00002350 std::vector<MachineMove *> &Moves) {
2351 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
2352 MachineMove *Move = Moves[i];
2353 unsigned LabelID = Move->getLabelID();
Jim Laskey66ebf092006-10-23 14:56:37 +00002354
2355 // Throw out move if the label is invalid.
2356 if (LabelID && !DebugInfo->isLabelValid(LabelID)) continue;
2357
Jim Laskey41886992006-04-07 16:34:46 +00002358 const MachineLocation &Dst = Move->getDestination();
2359 const MachineLocation &Src = Move->getSource();
2360
2361 // Advance row if new location.
2362 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
Jim Laskeyce50a162006-08-29 16:24:26 +00002363 EmitInt8(DW_CFA_advance_loc4);
Jim Laskey41886992006-04-07 16:34:46 +00002364 EOL("DW_CFA_advance_loc4");
2365 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
2366 EOL("");
2367
2368 BaseLabelID = LabelID;
2369 BaseLabel = "loc";
2370 }
2371
Jim Laskeyce50a162006-08-29 16:24:26 +00002372 int stackGrowth =
2373 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2374 TargetFrameInfo::StackGrowsUp ?
Jim Laskey563321a2006-09-06 18:34:40 +00002375 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskeyce50a162006-08-29 16:24:26 +00002376
Jim Laskey41886992006-04-07 16:34:46 +00002377 // If advancing cfa.
2378 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
2379 if (!Src.isRegister()) {
2380 if (Src.getRegister() == MachineLocation::VirtualFP) {
Jim Laskeyce50a162006-08-29 16:24:26 +00002381 EmitInt8(DW_CFA_def_cfa_offset);
Jim Laskey41886992006-04-07 16:34:46 +00002382 EOL("DW_CFA_def_cfa_offset");
2383 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00002384 EmitInt8(DW_CFA_def_cfa);
Jim Laskey41886992006-04-07 16:34:46 +00002385 EOL("DW_CFA_def_cfa");
2386
2387 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
2388 EOL("Register");
2389 }
Jim Laskey1069fbd2006-04-10 23:09:19 +00002390
Jim Laskeyce50a162006-08-29 16:24:26 +00002391 int Offset = Src.getOffset() / stackGrowth;
Jim Laskey1069fbd2006-04-10 23:09:19 +00002392
Jim Laskeyce50a162006-08-29 16:24:26 +00002393 EmitULEB128Bytes(Offset);
Jim Laskey41886992006-04-07 16:34:46 +00002394 EOL("Offset");
2395 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00002396 assert(0 && "Machine move no supported yet.");
Jim Laskey41886992006-04-07 16:34:46 +00002397 }
2398 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00002399 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
2400 int Offset = Dst.getOffset() / stackGrowth;
2401
2402 if (Offset < 0) {
2403 EmitInt8(DW_CFA_offset_extended_sf);
2404 EOL("DW_CFA_offset_extended_sf");
2405 EmitULEB128Bytes(Reg);
2406 EOL("Reg");
2407 EmitSLEB128Bytes(Offset);
2408 EOL("Offset");
2409 } else if (Reg < 64) {
2410 EmitInt8(DW_CFA_offset + Reg);
2411 EOL("DW_CFA_offset + Reg");
2412 EmitULEB128Bytes(Offset);
2413 EOL("Offset");
2414 } else {
2415 EmitInt8(DW_CFA_offset_extended);
2416 EOL("DW_CFA_offset_extended");
2417 EmitULEB128Bytes(Reg);
2418 EOL("Reg");
2419 EmitULEB128Bytes(Offset);
2420 EOL("Offset");
2421 }
Jim Laskey41886992006-04-07 16:34:46 +00002422 }
2423 }
2424}
2425
Jim Laskey063e7652006-01-17 17:31:53 +00002426/// EmitDebugInfo - Emit the debug info section.
2427///
Jim Laskey65195462006-10-30 13:35:07 +00002428void Dwarf::EmitDebugInfo() const {
Jim Laskey063e7652006-01-17 17:31:53 +00002429 // Start debug info section.
Jim Laskey563321a2006-09-06 18:34:40 +00002430 Asm->SwitchToDataSection(TAI->getDwarfInfoSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002431
Jim Laskeybd761842006-02-27 17:27:12 +00002432 // Process each compile unit.
2433 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2434 CompileUnit *Unit = CompileUnits[i];
2435
2436 if (Unit->hasContent()) {
2437 DIE *Die = Unit->getDie();
Jim Laskey0d086af2006-02-27 12:43:29 +00002438 // Emit the compile units header.
Jim Laskeybd761842006-02-27 17:27:12 +00002439 EmitLabel("info_begin", Unit->getID());
Jim Laskey0d086af2006-02-27 12:43:29 +00002440 // Emit size of content not including length itself
Jim Laskeybd761842006-02-27 17:27:12 +00002441 unsigned ContentSize = Die->getSize() +
Jim Laskey0d086af2006-02-27 12:43:29 +00002442 sizeof(int16_t) + // DWARF version number
2443 sizeof(int32_t) + // Offset Into Abbrev. Section
2444 sizeof(int8_t); // Pointer Size (in bytes)
2445
2446 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2447 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskeyf8a01a92006-06-15 20:51:43 +00002448 EmitDifference("abbrev_begin", 0, "section_abbrev", 0);
2449 EOL("Offset Into Abbrev. Section");
Jim Laskey563321a2006-09-06 18:34:40 +00002450 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
Jim Laskey0d086af2006-02-27 12:43:29 +00002451
Jim Laskeybd761842006-02-27 17:27:12 +00002452 EmitDIE(Die);
2453 EmitLabel("info_end", Unit->getID());
Jim Laskey063e7652006-01-17 17:31:53 +00002454 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002455
2456 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002457 }
2458}
2459
2460/// EmitAbbreviations - Emit the abbreviation section.
2461///
Jim Laskey65195462006-10-30 13:35:07 +00002462void Dwarf::EmitAbbreviations() const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002463 // Check to see if it is worth the effort.
2464 if (!Abbreviations.empty()) {
2465 // Start the debug abbrev section.
Jim Laskey563321a2006-09-06 18:34:40 +00002466 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002467
Jim Laskeyd18e2892006-01-20 20:34:06 +00002468 EmitLabel("abbrev_begin", 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002469
Jim Laskeyd18e2892006-01-20 20:34:06 +00002470 // For each abbrevation.
2471 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002472 AbbrevID <= NAID; ++AbbrevID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002473 // Get abbreviation data
2474 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey063e7652006-01-17 17:31:53 +00002475
Jim Laskeyd18e2892006-01-20 20:34:06 +00002476 // Emit the abbrevations code (base 1 index.)
2477 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
Jim Laskey063e7652006-01-17 17:31:53 +00002478
Jim Laskeyd18e2892006-01-20 20:34:06 +00002479 // Emit the abbreviations data.
2480 Abbrev.Emit(*this);
Jim Laskey0d086af2006-02-27 12:43:29 +00002481
2482 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002483 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002484
2485 EmitLabel("abbrev_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002486
2487 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002488 }
2489}
2490
2491/// EmitDebugLines - Emit source line information.
2492///
Jim Laskey65195462006-10-30 13:35:07 +00002493void Dwarf::EmitDebugLines() const {
Jim Laskey063e7652006-01-17 17:31:53 +00002494 // Minimum line delta, thus ranging from -10..(255-10).
2495 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2496 // Maximum line delta, thus ranging from -10..(255-10).
2497 const int MaxLineDelta = 255 + MinLineDelta;
2498
2499 // Start the dwarf line section.
Jim Laskey563321a2006-09-06 18:34:40 +00002500 Asm->SwitchToDataSection(TAI->getDwarfLineSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002501
2502 // Construct the section header.
2503
2504 EmitDifference("line_end", 0, "line_begin", 0);
2505 EOL("Length of Source Line Info");
2506 EmitLabel("line_begin", 0);
2507
Jim Laskeyda427fa2006-01-27 20:31:25 +00002508 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey063e7652006-01-17 17:31:53 +00002509
2510 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
2511 EOL("Prolog Length");
2512 EmitLabel("line_prolog_begin", 0);
2513
Jim Laskeyda427fa2006-01-27 20:31:25 +00002514 EmitInt8(1); EOL("Minimum Instruction Length");
Jim Laskey063e7652006-01-17 17:31:53 +00002515
Jim Laskeyda427fa2006-01-27 20:31:25 +00002516 EmitInt8(1); EOL("Default is_stmt_start flag");
Jim Laskey063e7652006-01-17 17:31:53 +00002517
Jim Laskeyda427fa2006-01-27 20:31:25 +00002518 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00002519
Jim Laskeyda427fa2006-01-27 20:31:25 +00002520 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00002521
Jim Laskeyda427fa2006-01-27 20:31:25 +00002522 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Jim Laskey063e7652006-01-17 17:31:53 +00002523
2524 // Line number standard opcode encodings argument count
Jim Laskeyda427fa2006-01-27 20:31:25 +00002525 EmitInt8(0); EOL("DW_LNS_copy arg count");
2526 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2527 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2528 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2529 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2530 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2531 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2532 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2533 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskey063e7652006-01-17 17:31:53 +00002534
2535 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2536 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
2537
2538 // Emit directories.
2539 for (unsigned DirectoryID = 1, NDID = Directories.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002540 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskey063e7652006-01-17 17:31:53 +00002541 EmitString(Directories[DirectoryID]); EOL("Directory");
2542 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00002543 EmitInt8(0); EOL("End of directories");
Jim Laskey063e7652006-01-17 17:31:53 +00002544
2545 // Emit files.
2546 for (unsigned SourceID = 1, NSID = SourceFiles.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002547 SourceID <= NSID; ++SourceID) {
Jim Laskey063e7652006-01-17 17:31:53 +00002548 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2549 EmitString(SourceFile.getName()); EOL("Source");
2550 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2551 EmitULEB128Bytes(0); EOL("Mod date");
2552 EmitULEB128Bytes(0); EOL("File size");
2553 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00002554 EmitInt8(0); EOL("End of files");
Jim Laskey063e7652006-01-17 17:31:53 +00002555
2556 EmitLabel("line_prolog_end", 0);
2557
Jim Laskey89d67fa2006-06-23 12:51:53 +00002558 // A sequence for each text section.
2559 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2560 // Isolate current sections line info.
Chris Lattner8466b212006-10-17 22:06:46 +00002561 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002562
2563 if (DwarfVerbose) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002564 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00002565 << TAI->getCommentString() << " "
Jim Laskey89d67fa2006-06-23 12:51:53 +00002566 << "Section "
2567 << SectionMap[j + 1].c_str() << "\n";
Jim Laskey0420f2a2006-02-22 19:02:11 +00002568 }
Jim Laskey063e7652006-01-17 17:31:53 +00002569
Jim Laskey89d67fa2006-06-23 12:51:53 +00002570 // Dwarf assumes we start with first line of first source file.
2571 unsigned Source = 1;
2572 unsigned Line = 1;
2573
2574 // Construct rows of the address, source, line, column matrix.
2575 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Chris Lattner8466b212006-10-17 22:06:46 +00002576 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey66ebf092006-10-23 14:56:37 +00002577 unsigned LabelID = LineInfo.getLabelID();
2578
Jim Laskeyfcc1d942006-10-24 11:50:43 +00002579 // Source line labels are validated at the MachineDebugInfo level.
Jim Laskey89d67fa2006-06-23 12:51:53 +00002580
2581 if (DwarfVerbose) {
Chris Lattner8466b212006-10-17 22:06:46 +00002582 unsigned SourceID = LineInfo.getSourceID();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002583 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2584 unsigned DirectoryID = SourceFile.getDirectoryID();
2585 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00002586 << TAI->getCommentString() << " "
Jim Laskey89d67fa2006-06-23 12:51:53 +00002587 << Directories[DirectoryID]
2588 << SourceFile.getName() << ":"
Chris Lattner8466b212006-10-17 22:06:46 +00002589 << LineInfo.getLine() << "\n";
Jim Laskey89d67fa2006-06-23 12:51:53 +00002590 }
2591
2592 // Define the line address.
2593 EmitInt8(0); EOL("Extended Op");
2594 EmitInt8(4 + 1); EOL("Op size");
2595 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskey66ebf092006-10-23 14:56:37 +00002596 EmitReference("loc", LabelID); EOL("Location label");
Jim Laskey89d67fa2006-06-23 12:51:53 +00002597
2598 // If change of source, then switch to the new source.
Chris Lattner8466b212006-10-17 22:06:46 +00002599 if (Source != LineInfo.getSourceID()) {
2600 Source = LineInfo.getSourceID();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002601 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2602 EmitULEB128Bytes(Source); EOL("New Source");
2603 }
2604
2605 // If change of line.
Chris Lattner8466b212006-10-17 22:06:46 +00002606 if (Line != LineInfo.getLine()) {
Jim Laskey89d67fa2006-06-23 12:51:53 +00002607 // Determine offset.
Chris Lattner8466b212006-10-17 22:06:46 +00002608 int Offset = LineInfo.getLine() - Line;
Jim Laskey89d67fa2006-06-23 12:51:53 +00002609 int Delta = Offset - MinLineDelta;
2610
2611 // Update line.
Chris Lattner8466b212006-10-17 22:06:46 +00002612 Line = LineInfo.getLine();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002613
2614 // If delta is small enough and in range...
2615 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2616 // ... then use fast opcode.
2617 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2618 } else {
2619 // ... otherwise use long hand.
2620 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2621 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2622 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2623 }
2624 } else {
2625 // Copy the previous row (different address or source)
2626 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2627 }
2628 }
2629
2630 // Define last address of section.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002631 EmitInt8(0); EOL("Extended Op");
2632 EmitInt8(4 + 1); EOL("Op size");
2633 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskey89d67fa2006-06-23 12:51:53 +00002634 EmitReference("section_end", j + 1); EOL("Section end label");
2635
2636 // Mark end of matrix.
2637 EmitInt8(0); EOL("DW_LNE_end_sequence");
2638 EmitULEB128Bytes(1); O << "\n";
2639 EmitInt8(1); O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002640 }
Jim Laskey063e7652006-01-17 17:31:53 +00002641
2642 EmitLabel("line_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002643
2644 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002645}
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002646
Jim Laskey41886992006-04-07 16:34:46 +00002647/// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002648///
Jim Laskey65195462006-10-30 13:35:07 +00002649void Dwarf::EmitInitialDebugFrame() {
Jim Laskey1069fbd2006-04-10 23:09:19 +00002650 int stackGrowth =
2651 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2652 TargetFrameInfo::StackGrowsUp ?
Jim Laskey563321a2006-09-06 18:34:40 +00002653 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskey1069fbd2006-04-10 23:09:19 +00002654
Jim Laskey41886992006-04-07 16:34:46 +00002655 // Start the dwarf frame section.
Jim Laskey563321a2006-09-06 18:34:40 +00002656 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002657
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002658 EmitLabel("frame_common", 0);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002659 EmitDifference("frame_common_end", 0,
2660 "frame_common_begin", 0);
2661 EOL("Length of Common Information Entry");
2662
2663 EmitLabel("frame_common_begin", 0);
2664 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2665 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2666 EmitString(""); EOL("CIE Augmentation");
2667 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
Jim Laskey1069fbd2006-04-10 23:09:19 +00002668 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
Jim Laskey41886992006-04-07 16:34:46 +00002669 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
2670
2671 std::vector<MachineMove *> Moves;
2672 RI->getInitialFrameState(Moves);
2673 EmitFrameMoves(NULL, 0, Moves);
2674 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2675
Jim Laskeyb8509c52006-03-23 18:07:55 +00002676 EmitAlign(2);
2677 EmitLabel("frame_common_end", 0);
2678
2679 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002680}
2681
Jim Laskey41886992006-04-07 16:34:46 +00002682/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2683/// section.
Jim Laskey65195462006-10-30 13:35:07 +00002684void Dwarf::EmitFunctionDebugFrame() {
Jim Laskey41886992006-04-07 16:34:46 +00002685 // Start the dwarf frame section.
Jim Laskey563321a2006-09-06 18:34:40 +00002686 Asm->SwitchToDataSection(TAI->getDwarfFrameSection(), 0);
Jim Laskey41886992006-04-07 16:34:46 +00002687
2688 EmitDifference("frame_end", SubprogramCount,
2689 "frame_begin", SubprogramCount);
2690 EOL("Length of Frame Information Entry");
2691
2692 EmitLabel("frame_begin", SubprogramCount);
2693
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002694 EmitDifference("frame_common", 0, "section_frame", 0);
2695 EOL("FDE CIE offset");
Jim Laskey41886992006-04-07 16:34:46 +00002696
2697 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2698 EmitDifference("func_end", SubprogramCount,
2699 "func_begin", SubprogramCount);
2700 EOL("FDE address range");
2701
2702 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2703
2704 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2705
2706 EmitAlign(2);
2707 EmitLabel("frame_end", SubprogramCount);
2708
2709 O << "\n";
2710}
2711
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002712/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2713///
Jim Laskey65195462006-10-30 13:35:07 +00002714void Dwarf::EmitDebugPubNames() {
Jim Laskeybd761842006-02-27 17:27:12 +00002715 // Start the dwarf pubnames section.
Jim Laskey563321a2006-09-06 18:34:40 +00002716 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection(), 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002717
Jim Laskeybd761842006-02-27 17:27:12 +00002718 // Process each compile unit.
2719 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2720 CompileUnit *Unit = CompileUnits[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00002721
Jim Laskeybd761842006-02-27 17:27:12 +00002722 if (Unit->hasContent()) {
2723 EmitDifference("pubnames_end", Unit->getID(),
2724 "pubnames_begin", Unit->getID());
2725 EOL("Length of Public Names Info");
2726
2727 EmitLabel("pubnames_begin", Unit->getID());
2728
2729 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2730
Jim Laskey067ef412006-06-19 15:48:00 +00002731 EmitDifference("info_begin", Unit->getID(), "section_info", 0);
Jim Laskeybd761842006-02-27 17:27:12 +00002732 EOL("Offset of Compilation Unit Info");
Jim Laskeyd18e2892006-01-20 20:34:06 +00002733
Jim Laskeybd761842006-02-27 17:27:12 +00002734 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2735 EOL("Compilation Unit Length");
2736
2737 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2738
2739 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2740 GE = Globals.end();
2741 GI != GE; ++GI) {
2742 const std::string &Name = GI->first;
2743 DIE * Entity = GI->second;
2744
2745 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2746 EmitString(Name); EOL("External Name");
2747 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002748
Jim Laskeybd761842006-02-27 17:27:12 +00002749 EmitInt32(0); EOL("End Mark");
2750 EmitLabel("pubnames_end", Unit->getID());
2751
2752 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002753 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002754 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002755}
2756
2757/// EmitDebugStr - Emit visible names into a debug str section.
2758///
Jim Laskey65195462006-10-30 13:35:07 +00002759void Dwarf::EmitDebugStr() {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002760 // Check to see if it is worth the effort.
2761 if (!StringPool.empty()) {
2762 // Start the dwarf str section.
Jim Laskey563321a2006-09-06 18:34:40 +00002763 Asm->SwitchToDataSection(TAI->getDwarfStrSection(), 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002764
Jim Laskeyb8509c52006-03-23 18:07:55 +00002765 // For each of strings in the string pool.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002766 for (unsigned StringID = 1, N = StringPool.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002767 StringID <= N; ++StringID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002768 // Emit a label for reference from debug information entries.
2769 EmitLabel("string", StringID);
2770 // Emit the string itself.
2771 const std::string &String = StringPool[StringID];
2772 EmitString(String); O << "\n";
2773 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002774
2775 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002776 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002777}
2778
2779/// EmitDebugLoc - Emit visible names into a debug loc section.
2780///
Jim Laskey65195462006-10-30 13:35:07 +00002781void Dwarf::EmitDebugLoc() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002782 // Start the dwarf loc section.
Jim Laskey563321a2006-09-06 18:34:40 +00002783 Asm->SwitchToDataSection(TAI->getDwarfLocSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002784
2785 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002786}
2787
2788/// EmitDebugARanges - Emit visible names into a debug aranges section.
2789///
Jim Laskey65195462006-10-30 13:35:07 +00002790void Dwarf::EmitDebugARanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002791 // Start the dwarf aranges section.
Jim Laskey563321a2006-09-06 18:34:40 +00002792 Asm->SwitchToDataSection(TAI->getDwarfARangesSection(), 0);
Jim Laskeye719a7c2006-01-18 16:54:26 +00002793
2794 // FIXME - Mock up
Jim Laskeybd761842006-02-27 17:27:12 +00002795#if 0
2796 // Process each compile unit.
2797 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2798 CompileUnit *Unit = CompileUnits[i];
2799
2800 if (Unit->hasContent()) {
2801 // Don't include size of length
2802 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2803
2804 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2805
2806 EmitReference("info_begin", Unit->getID());
2807 EOL("Offset of Compilation Unit Info");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002808
Jim Laskey563321a2006-09-06 18:34:40 +00002809 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002810
Jim Laskeybd761842006-02-27 17:27:12 +00002811 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002812
Jim Laskeybd761842006-02-27 17:27:12 +00002813 EmitInt16(0); EOL("Pad (1)");
2814 EmitInt16(0); EOL("Pad (2)");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002815
Jim Laskeybd761842006-02-27 17:27:12 +00002816 // Range 1
2817 EmitReference("text_begin", 0); EOL("Address");
2818 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002819
Jim Laskeybd761842006-02-27 17:27:12 +00002820 EmitInt32(0); EOL("EOM (1)");
2821 EmitInt32(0); EOL("EOM (2)");
2822
2823 O << "\n";
2824 }
2825 }
2826#endif
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002827}
2828
2829/// EmitDebugRanges - Emit visible names into a debug ranges section.
2830///
Jim Laskey65195462006-10-30 13:35:07 +00002831void Dwarf::EmitDebugRanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002832 // Start the dwarf ranges section.
Jim Laskey563321a2006-09-06 18:34:40 +00002833 Asm->SwitchToDataSection(TAI->getDwarfRangesSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002834
2835 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002836}
2837
2838/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2839///
Jim Laskey65195462006-10-30 13:35:07 +00002840void Dwarf::EmitDebugMacInfo() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002841 // Start the dwarf macinfo section.
Jim Laskey563321a2006-09-06 18:34:40 +00002842 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection(), 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002843
2844 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002845}
Jim Laskey063e7652006-01-17 17:31:53 +00002846
Jim Laskey52060a02006-01-24 00:49:18 +00002847/// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2848/// header file.
Jim Laskey65195462006-10-30 13:35:07 +00002849void Dwarf::ConstructCompileUnitDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002850 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
Jim Laskey6e87c0e2006-01-26 21:22:49 +00002851
2852 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00002853 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
Jim Laskey52060a02006-01-24 00:49:18 +00002854 CompileUnits.push_back(Unit);
2855 }
2856}
2857
2858/// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2859/// variables.
Jim Laskey65195462006-10-30 13:35:07 +00002860void Dwarf::ConstructGlobalDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002861 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002862 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00002863
2864 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002865 GlobalVariableDesc *GVD = GlobalVariables[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002866 NewGlobalVariable(GVD);
Jim Laskey52060a02006-01-24 00:49:18 +00002867 }
2868}
2869
Jim Laskey0420f2a2006-02-22 19:02:11 +00002870/// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2871/// subprograms.
Jim Laskey65195462006-10-30 13:35:07 +00002872void Dwarf::ConstructSubprogramDIEs() {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002873 std::vector<SubprogramDesc *> Subprograms =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002874 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002875
2876 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2877 SubprogramDesc *SPD = Subprograms[i];
2878 NewSubprogram(SPD);
2879 }
2880}
Jim Laskey52060a02006-01-24 00:49:18 +00002881
Jim Laskey063e7652006-01-17 17:31:53 +00002882//===----------------------------------------------------------------------===//
Jim Laskey65195462006-10-30 13:35:07 +00002883// Dwarf implemenation.
Jim Laskey063e7652006-01-17 17:31:53 +00002884//
Jim Laskey52060a02006-01-24 00:49:18 +00002885
Jim Laskey65195462006-10-30 13:35:07 +00002886Dwarf::Dwarf(std::ostream &OS, AsmPrinter *A,
Jim Laskeya0f3d172006-09-07 22:06:40 +00002887 const TargetAsmInfo *T)
Jim Laskey52060a02006-01-24 00:49:18 +00002888: O(OS)
2889, Asm(A)
Jim Laskey563321a2006-09-06 18:34:40 +00002890, TAI(T)
Jim Laskey41886992006-04-07 16:34:46 +00002891, TD(Asm->TM.getTargetData())
2892, RI(Asm->TM.getRegisterInfo())
Jim Laskeyb8509c52006-03-23 18:07:55 +00002893, M(NULL)
2894, MF(NULL)
Jim Laskey52060a02006-01-24 00:49:18 +00002895, DebugInfo(NULL)
2896, didInitial(false)
Jim Laskey014f98c2006-06-14 11:35:03 +00002897, shouldEmit(false)
Jim Laskeyb8509c52006-03-23 18:07:55 +00002898, SubprogramCount(0)
Jim Laskey52060a02006-01-24 00:49:18 +00002899, CompileUnits()
2900, Abbreviations()
Jim Laskey52060a02006-01-24 00:49:18 +00002901, StringPool()
Jim Laskeybd761842006-02-27 17:27:12 +00002902, DescToUnitMap()
Jim Laskey0420f2a2006-02-22 19:02:11 +00002903, DescToDieMap()
Jim Laskey89d67fa2006-06-23 12:51:53 +00002904, SectionMap()
2905, SectionSourceLines()
Jim Laskey52060a02006-01-24 00:49:18 +00002906{}
Jim Laskey65195462006-10-30 13:35:07 +00002907Dwarf::~Dwarf() {
Jim Laskey52060a02006-01-24 00:49:18 +00002908 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2909 delete CompileUnits[i];
Jim Laskey063e7652006-01-17 17:31:53 +00002910 }
Jim Laskey52060a02006-01-24 00:49:18 +00002911}
Jim Laskey063e7652006-01-17 17:31:53 +00002912
Jim Laskey41886992006-04-07 16:34:46 +00002913/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2914/// created it. Set by the target AsmPrinter.
Jim Laskey65195462006-10-30 13:35:07 +00002915void Dwarf::SetDebugInfo(MachineDebugInfo *DI) {
Jim Laskey014f98c2006-06-14 11:35:03 +00002916 // Make sure initial declarations are made.
2917 if (!DebugInfo && DI->hasInfo()) {
2918 DebugInfo = DI;
2919 shouldEmit = true;
2920
2921 // Emit initial sections
2922 EmitInitial();
2923
2924 // Create all the compile unit DIEs.
2925 ConstructCompileUnitDIEs();
2926
2927 // Create DIEs for each of the externally visible global variables.
2928 ConstructGlobalDIEs();
2929
2930 // Create DIEs for each of the externally visible subprograms.
2931 ConstructSubprogramDIEs();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002932
2933 // Prime section data.
Jim Laskey563321a2006-09-06 18:34:40 +00002934 SectionMap.insert(std::string("\t") + TAI->getTextSection());
Jim Laskey014f98c2006-06-14 11:35:03 +00002935 }
Jim Laskey41886992006-04-07 16:34:46 +00002936}
2937
Jim Laskey063e7652006-01-17 17:31:53 +00002938/// BeginModule - Emit all Dwarf sections that should come prior to the content.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002939///
Jim Laskey65195462006-10-30 13:35:07 +00002940void Dwarf::BeginModule(Module *M) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002941 this->M = M;
2942
Jim Laskeyb2efb852006-01-04 22:28:25 +00002943 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002944 EOL("Dwarf Begin Module");
Jim Laskeyb2efb852006-01-04 22:28:25 +00002945}
2946
Jim Laskey063e7652006-01-17 17:31:53 +00002947/// EndModule - Emit all Dwarf sections that should come after the content.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002948///
Jim Laskey65195462006-10-30 13:35:07 +00002949void Dwarf::EndModule() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002950 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002951 EOL("Dwarf End Module");
2952
2953 // Standard sections final addresses.
Jim Laskey563321a2006-09-06 18:34:40 +00002954 Asm->SwitchToTextSection(TAI->getTextSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002955 EmitLabel("text_end", 0);
Jim Laskey563321a2006-09-06 18:34:40 +00002956 Asm->SwitchToDataSection(TAI->getDataSection(), 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002957 EmitLabel("data_end", 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002958
Jim Laskey89d67fa2006-06-23 12:51:53 +00002959 // End text sections.
2960 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2961 Asm->SwitchToTextSection(SectionMap[i].c_str(), 0);
2962 EmitLabel("section_end", i);
2963 }
2964
Jim Laskey063e7652006-01-17 17:31:53 +00002965 // Compute DIE offsets and sizes.
2966 SizeAndOffsets();
2967
2968 // Emit all the DIEs into a debug info section
2969 EmitDebugInfo();
2970
2971 // Corresponding abbreviations into a abbrev section.
2972 EmitAbbreviations();
2973
2974 // Emit source line correspondence into a debug line section.
2975 EmitDebugLines();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002976
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002977 // Emit info into a debug pubnames section.
2978 EmitDebugPubNames();
2979
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002980 // Emit info into a debug str section.
2981 EmitDebugStr();
2982
2983 // Emit info into a debug loc section.
2984 EmitDebugLoc();
2985
2986 // Emit info into a debug aranges section.
2987 EmitDebugARanges();
2988
2989 // Emit info into a debug ranges section.
2990 EmitDebugRanges();
2991
2992 // Emit info into a debug macinfo section.
2993 EmitDebugMacInfo();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002994}
2995
Jim Laskey6b92b8e2006-04-07 20:44:42 +00002996/// BeginFunction - Gather pre-function debug information. Assumes being
2997/// emitted immediately after the function entry point.
Jim Laskey65195462006-10-30 13:35:07 +00002998void Dwarf::BeginFunction(MachineFunction *MF) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002999 this->MF = MF;
3000
Jim Laskey89d67fa2006-06-23 12:51:53 +00003001 if (!ShouldEmitDwarf()) return;
3002 EOL("Dwarf Begin Function");
3003
3004 // Begin accumulating function debug information.
3005 DebugInfo->BeginFunction(MF);
Jim Laskey41886992006-04-07 16:34:46 +00003006
Jim Laskey89d67fa2006-06-23 12:51:53 +00003007 // Assumes in correct section after the entry point.
3008 EmitLabel("func_begin", ++SubprogramCount);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003009}
3010
Jim Laskeye719a7c2006-01-18 16:54:26 +00003011/// EndFunction - Gather and emit post-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003012///
Jim Laskey65195462006-10-30 13:35:07 +00003013void Dwarf::EndFunction() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00003014 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00003015 EOL("Dwarf End Function");
Jim Laskeyb8509c52006-03-23 18:07:55 +00003016
Jim Laskey89d67fa2006-06-23 12:51:53 +00003017 // Define end label for subprogram.
3018 EmitLabel("func_end", SubprogramCount);
Jim Laskey014f98c2006-06-14 11:35:03 +00003019
Jim Laskey89d67fa2006-06-23 12:51:53 +00003020 // Get function line info.
Chris Lattner8466b212006-10-17 22:06:46 +00003021 const std::vector<SourceLineInfo> &LineInfos = DebugInfo->getSourceLines();
Jim Laskey89d67fa2006-06-23 12:51:53 +00003022
3023 if (!LineInfos.empty()) {
3024 // Get section line info.
3025 unsigned ID = SectionMap.insert(Asm->CurrentSection);
3026 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Chris Lattner8466b212006-10-17 22:06:46 +00003027 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskey89d67fa2006-06-23 12:51:53 +00003028 // Append the function info to section info.
3029 SectionLineInfos.insert(SectionLineInfos.end(),
3030 LineInfos.begin(), LineInfos.end());
Jim Laskey014f98c2006-06-14 11:35:03 +00003031 }
Jim Laskey41886992006-04-07 16:34:46 +00003032
Jim Laskey89d67fa2006-06-23 12:51:53 +00003033 // Construct scopes for subprogram.
3034 ConstructRootScope(DebugInfo->getRootScope());
3035
3036 // Emit function frame information.
3037 EmitFunctionDebugFrame();
3038
3039 // Reset the line numbers for the next function.
Chris Lattner8466b212006-10-17 22:06:46 +00003040 DebugInfo->ClearLineInfo();
Jim Laskey89d67fa2006-06-23 12:51:53 +00003041
Jim Laskey41886992006-04-07 16:34:46 +00003042 // Clear function debug information.
3043 DebugInfo->EndFunction();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003044}
Jim Laskey65195462006-10-30 13:35:07 +00003045
3046
3047//===----------------------------------------------------------------------===//
3048/// DwarfWriter Implementation
3049
3050DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3051 const TargetAsmInfo *T) {
3052 DW = new Dwarf(OS, A, T);
3053}
3054
3055DwarfWriter::~DwarfWriter() {
3056 delete DW;
3057}
3058
3059/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
3060/// created it. Set by the target AsmPrinter.
3061void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
3062 DW->SetDebugInfo(DI);
3063}
3064
3065/// BeginModule - Emit all Dwarf sections that should come prior to the
3066/// content.
3067void DwarfWriter::BeginModule(Module *M) {
3068 DW->BeginModule(M);
3069}
3070
3071/// EndModule - Emit all Dwarf sections that should come after the content.
3072///
3073void DwarfWriter::EndModule() {
3074 DW->EndModule();
3075}
3076
3077/// BeginFunction - Gather pre-function debug information. Assumes being
3078/// emitted immediately after the function entry point.
3079void DwarfWriter::BeginFunction(MachineFunction *MF) {
3080 DW->BeginFunction(MF);
3081}
3082
3083/// EndFunction - Gather and emit post-function debug information.
3084///
3085void DwarfWriter::EndFunction() {
3086 DW->EndFunction();
3087}