blob: 7bec4d3cc4ef2f7e6ef5c50521636b9f53c38196 [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 Laskeya9c83fe2006-10-30 15:59:54 +000016#include "llvm/ADT/FoldingSet.h"
Jim Laskey063e7652006-01-17 17:31:53 +000017#include "llvm/ADT/StringExtras.h"
Jim Laskey65195462006-10-30 13:35:07 +000018#include "llvm/ADT/UniqueVector.h"
Jim Laskey52060a02006-01-24 00:49:18 +000019#include "llvm/Module.h"
20#include "llvm/Type.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000021#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeyb2efb852006-01-04 22:28:25 +000022#include "llvm/CodeGen/MachineDebugInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000024#include "llvm/CodeGen/MachineLocation.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000025#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000026#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000027#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000028#include "llvm/Support/Mangler.h"
Jim Laskey563321a2006-09-06 18:34:40 +000029#include "llvm/Target/TargetAsmInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000030#include "llvm/Target/MRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000031#include "llvm/Target/TargetData.h"
Jim Laskey52060a02006-01-24 00:49:18 +000032#include "llvm/Target/TargetMachine.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000033#include "llvm/Target/TargetFrameInfo.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000034
Jim Laskeyb2efb852006-01-04 22:28:25 +000035#include <iostream>
Jim Laskey65195462006-10-30 13:35:07 +000036#include <string>
Jim Laskeya7cea6f2006-01-04 13:52:30 +000037
Jim Laskeyb2efb852006-01-04 22:28:25 +000038using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000039using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000040
41static cl::opt<bool>
42DwarfVerbose("dwarf-verbose", cl::Hidden,
Jim Laskeyce50a162006-08-29 16:24:26 +000043 cl::desc("Add comments to Dwarf directives."));
Jim Laskey063e7652006-01-17 17:31:53 +000044
Jim Laskey0d086af2006-02-27 12:43:29 +000045namespace llvm {
Jim Laskey65195462006-10-30 13:35:07 +000046
47//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000048/// DWLabel - Labels are used to track locations in the assembler file.
49/// Labels appear in the form <prefix>debug_<Tag><Number>, where the tag is a
50/// category of label (Ex. location) and number is a value unique in that
51/// category.
Jim Laskey65195462006-10-30 13:35:07 +000052class DWLabel {
53public:
54 const char *Tag; // Label category tag. Should always be
55 // a staticly declared C string.
56 unsigned Number; // Unique number.
57
58 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
59};
Jim Laskey0d086af2006-02-27 12:43:29 +000060
Jim Laskey063e7652006-01-17 17:31:53 +000061//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000062/// Forward declarations.
Jim Laskey0d086af2006-02-27 12:43:29 +000063//
Jim Laskey0d086af2006-02-27 12:43:29 +000064class DIE;
Jim Laskey063e7652006-01-17 17:31:53 +000065
Jim Laskey0d086af2006-02-27 12:43:29 +000066//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000067/// CompileUnit - This dwarf writer support class manages information associate
68/// with a source file.
Jim Laskeybd761842006-02-27 17:27:12 +000069class CompileUnit {
70private:
71 CompileUnitDesc *Desc; // Compile unit debug descriptor.
72 unsigned ID; // File ID for source.
Jim Laskeyb8509c52006-03-23 18:07:55 +000073 DIE *Die; // Compile unit debug information entry.
Jim Laskeybd761842006-02-27 17:27:12 +000074 std::map<std::string, DIE *> Globals; // A map of globally visible named
75 // entities for this unit.
Jim Laskey90c79d72006-03-23 23:02:34 +000076 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
77 // Tracks the mapping of unit level
78 // debug informaton descriptors to debug
79 // information entries.
Jim Laskeybd761842006-02-27 17:27:12 +000080
81public:
82 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
83 : Desc(CUD)
84 , ID(I)
85 , Die(D)
86 , Globals()
Jim Laskey90c79d72006-03-23 23:02:34 +000087 , DescToDieMap()
Jim Laskeybd761842006-02-27 17:27:12 +000088 {}
89
90 ~CompileUnit();
91
92 // Accessors.
93 CompileUnitDesc *getDesc() const { return Desc; }
94 unsigned getID() const { return ID; }
95 DIE* getDie() const { return Die; }
96 std::map<std::string, DIE *> &getGlobals() { return Globals; }
97
98 /// hasContent - Return true if this compile unit has something to write out.
99 ///
100 bool hasContent() const;
101
102 /// AddGlobal - Add a new global entity to the compile unit.
103 ///
104 void AddGlobal(const std::string &Name, DIE *Die);
105
Jim Laskey90c79d72006-03-23 23:02:34 +0000106 /// getDieMapSlotFor - Returns the debug information entry map slot for the
107 /// specified debug descriptor.
108 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
109 return DescToDieMap[DD];
110 }
Jim Laskeybd761842006-02-27 17:27:12 +0000111};
112
113//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000114/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
115/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000116class DIEAbbrevData {
117private:
118 unsigned Attribute; // Dwarf attribute code.
119 unsigned Form; // Dwarf form code.
120
121public:
122 DIEAbbrevData(unsigned A, unsigned F)
123 : Attribute(A)
124 , Form(F)
125 {}
126
Jim Laskeybd761842006-02-27 17:27:12 +0000127 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000128 unsigned getAttribute() const { return Attribute; }
129 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000130
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000131 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000132 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000133 void Profile(FoldingSetNodeID &ID) {
134 ID.AddInteger(Attribute);
135 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000136 }
137};
Jim Laskey063e7652006-01-17 17:31:53 +0000138
Jim Laskey0d086af2006-02-27 12:43:29 +0000139//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000140/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
141/// information object.
142class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000143private:
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000144 unsigned Number; // Unique number for abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000145 unsigned Tag; // Dwarf tag code.
146 unsigned ChildrenFlag; // Dwarf children flag.
147 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
Jim Laskey063e7652006-01-17 17:31:53 +0000148
Jim Laskey0d086af2006-02-27 12:43:29 +0000149public:
Jim Laskey063e7652006-01-17 17:31:53 +0000150
Jim Laskey0d086af2006-02-27 12:43:29 +0000151 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000152 : Number(0)
153 , Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000154 , ChildrenFlag(C)
155 , Data()
156 {}
157 ~DIEAbbrev() {}
158
Jim Laskeybd761842006-02-27 17:27:12 +0000159 // Accessors.
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000160 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000161 unsigned getTag() const { return Tag; }
162 unsigned getChildrenFlag() const { return ChildrenFlag; }
163 const std::vector<DIEAbbrevData> &getData() const { return Data; }
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000164 void setNumber(unsigned N) { Number = N; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000165 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskey063e7652006-01-17 17:31:53 +0000166
Jim Laskey0d086af2006-02-27 12:43:29 +0000167 /// AddAttribute - Adds another set of attribute information to the
168 /// abbreviation.
169 void AddAttribute(unsigned Attribute, unsigned Form) {
170 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000171 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000172
Jim Laskeyb8509c52006-03-23 18:07:55 +0000173 /// AddFirstAttribute - Adds a set of attribute information to the front
174 /// of the abbreviation.
175 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
176 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
177 }
178
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000179 /// Profile - Used to gather unique data for the abbreviation folding set.
180 ///
181 void Profile(FoldingSetNodeID &ID) {
182 ID.AddInteger(Tag);
183 ID.AddInteger(ChildrenFlag);
184
185 // For each attribute description.
186 for (unsigned i = 0, N = Data.size(); i < N; ++i)
187 Data[i].Profile(ID);
188 }
189
Jim Laskey0d086af2006-02-27 12:43:29 +0000190 /// Emit - Print the abbreviation using the specified Dwarf writer.
191 ///
Jim Laskey65195462006-10-30 13:35:07 +0000192 void Emit(const Dwarf &DW) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000193
194#ifndef NDEBUG
195 void print(std::ostream &O);
196 void dump();
197#endif
198};
Jim Laskey063e7652006-01-17 17:31:53 +0000199
Jim Laskey0d086af2006-02-27 12:43:29 +0000200//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000201/// DIEValue - A debug information entry value.
Jim Laskey0d086af2006-02-27 12:43:29 +0000202//
203class DIEValue {
204public:
205 enum {
206 isInteger,
207 isString,
208 isLabel,
209 isAsIsLabel,
210 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000211 isEntry,
212 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000213 };
214
215 unsigned Type; // Type of the value
216
217 DIEValue(unsigned T) : Type(T) {}
218 virtual ~DIEValue() {}
219
220 // Implement isa/cast/dyncast.
221 static bool classof(const DIEValue *) { return true; }
222
223 /// EmitValue - Emit value via the Dwarf writer.
224 ///
Jim Laskey65195462006-10-30 13:35:07 +0000225 virtual void EmitValue(const Dwarf &DW, unsigned Form) const = 0;
Jim Laskey0d086af2006-02-27 12:43:29 +0000226
227 /// SizeOf - Return the size of a value in bytes.
228 ///
Jim Laskey65195462006-10-30 13:35:07 +0000229 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const = 0;
Jim Laskey0d086af2006-02-27 12:43:29 +0000230};
Jim Laskey063e7652006-01-17 17:31:53 +0000231
Jim Laskey0d086af2006-02-27 12:43:29 +0000232//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000233/// DWInteger - An integer value DIE.
234///
Jim Laskey0d086af2006-02-27 12:43:29 +0000235class DIEInteger : public DIEValue {
236private:
237 uint64_t Integer;
238
239public:
240 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000241
Jim Laskey0d086af2006-02-27 12:43:29 +0000242 // Implement isa/cast/dyncast.
243 static bool classof(const DIEInteger *) { return true; }
244 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
245
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000246 /// BestForm - Choose the best form for integer.
247 ///
248 unsigned BestForm(bool IsSigned);
249
Jim Laskey0d086af2006-02-27 12:43:29 +0000250 /// EmitValue - Emit integer of appropriate size.
251 ///
Jim Laskey65195462006-10-30 13:35:07 +0000252 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000253
254 /// SizeOf - Determine size of integer value in bytes.
255 ///
Jim Laskey65195462006-10-30 13:35:07 +0000256 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000257};
Jim Laskey063e7652006-01-17 17:31:53 +0000258
Jim Laskey0d086af2006-02-27 12:43:29 +0000259//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000260/// DIEString - A string value DIE.
261///
Jim Laskey0d086af2006-02-27 12:43:29 +0000262struct DIEString : public DIEValue {
263 const std::string String;
264
265 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000266
Jim Laskey0d086af2006-02-27 12:43:29 +0000267 // Implement isa/cast/dyncast.
268 static bool classof(const DIEString *) { return true; }
269 static bool classof(const DIEValue *S) { return S->Type == isString; }
270
271 /// EmitValue - Emit string value.
272 ///
Jim Laskey65195462006-10-30 13:35:07 +0000273 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000274
275 /// SizeOf - Determine size of string value in bytes.
276 ///
Jim Laskey65195462006-10-30 13:35:07 +0000277 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000278};
Jim Laskey063e7652006-01-17 17:31:53 +0000279
Jim Laskey0d086af2006-02-27 12:43:29 +0000280//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000281/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000282//
283struct DIEDwarfLabel : public DIEValue {
284 const DWLabel Label;
285
286 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000287
Jim Laskey0d086af2006-02-27 12:43:29 +0000288 // Implement isa/cast/dyncast.
289 static bool classof(const DIEDwarfLabel *) { return true; }
290 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
291
292 /// EmitValue - Emit label value.
293 ///
Jim Laskey65195462006-10-30 13:35:07 +0000294 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000295
296 /// SizeOf - Determine size of label value in bytes.
297 ///
Jim Laskey65195462006-10-30 13:35:07 +0000298 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000299};
Jim Laskey063e7652006-01-17 17:31:53 +0000300
Jim Laskey063e7652006-01-17 17:31:53 +0000301
Jim Laskey0d086af2006-02-27 12:43:29 +0000302//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000303/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000304//
305struct DIEObjectLabel : public DIEValue {
306 const std::string Label;
307
308 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000309
Jim Laskey0d086af2006-02-27 12:43:29 +0000310 // Implement isa/cast/dyncast.
311 static bool classof(const DIEObjectLabel *) { return true; }
312 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
313
314 /// EmitValue - Emit label value.
315 ///
Jim Laskey65195462006-10-30 13:35:07 +0000316 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000317
318 /// SizeOf - Determine size of label value in bytes.
319 ///
Jim Laskey65195462006-10-30 13:35:07 +0000320 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000321};
Jim Laskey063e7652006-01-17 17:31:53 +0000322
Jim Laskey0d086af2006-02-27 12:43:29 +0000323//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000324/// DIEDelta - A simple label difference DIE.
325///
Jim Laskey0d086af2006-02-27 12:43:29 +0000326struct DIEDelta : public DIEValue {
327 const DWLabel LabelHi;
328 const DWLabel LabelLo;
329
330 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
331 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000332
Jim Laskey0d086af2006-02-27 12:43:29 +0000333 // Implement isa/cast/dyncast.
334 static bool classof(const DIEDelta *) { return true; }
335 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
336
337 /// EmitValue - Emit delta value.
338 ///
Jim Laskey65195462006-10-30 13:35:07 +0000339 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000340
341 /// SizeOf - Determine size of delta value in bytes.
342 ///
Jim Laskey65195462006-10-30 13:35:07 +0000343 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000344};
Jim Laskey063e7652006-01-17 17:31:53 +0000345
Jim Laskey0d086af2006-02-27 12:43:29 +0000346//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000347/// DIEntry - A pointer to a debug information entry.
348///
Jim Laskey0d086af2006-02-27 12:43:29 +0000349struct DIEntry : public DIEValue {
350 DIE *Entry;
351
352 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
353
354 // Implement isa/cast/dyncast.
355 static bool classof(const DIEntry *) { return true; }
356 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
357
Jim Laskeyb8509c52006-03-23 18:07:55 +0000358 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000359 ///
Jim Laskey65195462006-10-30 13:35:07 +0000360 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000361
Jim Laskeyb8509c52006-03-23 18:07:55 +0000362 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000363 ///
Jim Laskey65195462006-10-30 13:35:07 +0000364 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000365};
366
367//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000368/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000369//
370struct DIEBlock : public DIEValue {
371 unsigned Size; // Size in bytes excluding size header.
372 std::vector<unsigned> Forms; // Data forms.
373 std::vector<DIEValue *> Values; // Block values.
374
375 DIEBlock()
376 : DIEValue(isBlock)
377 , Size(0)
378 , Forms()
379 , Values()
380 {}
381 ~DIEBlock();
382
383 // Implement isa/cast/dyncast.
384 static bool classof(const DIEBlock *) { return true; }
385 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
386
387 /// ComputeSize - calculate the size of the block.
388 ///
Jim Laskey65195462006-10-30 13:35:07 +0000389 unsigned ComputeSize(Dwarf &DW);
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000390
391 /// BestForm - Choose the best form for data.
392 ///
393 unsigned BestForm();
394
395 /// EmitValue - Emit block data.
396 ///
Jim Laskey65195462006-10-30 13:35:07 +0000397 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000398
399 /// SizeOf - Determine size of block data in bytes.
400 ///
Jim Laskey65195462006-10-30 13:35:07 +0000401 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000402
403 /// AddUInt - Add an unsigned integer value.
404 ///
405 void AddUInt(unsigned Form, uint64_t Integer);
406
407 /// AddSInt - Add an signed integer value.
408 ///
409 void AddSInt(unsigned Form, int64_t Integer);
410
411 /// AddString - Add a std::string value.
412 ///
413 void AddString(unsigned Form, const std::string &String);
414
415 /// AddLabel - Add a Dwarf label value.
416 ///
417 void AddLabel(unsigned Form, const DWLabel &Label);
418
419 /// AddObjectLabel - Add a non-Dwarf label value.
420 ///
421 void AddObjectLabel(unsigned Form, const std::string &Label);
422
423 /// AddDelta - Add a label delta value.
424 ///
425 void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
426
427 /// AddDIEntry - Add a DIE value.
428 ///
429 void AddDIEntry(unsigned Form, DIE *Entry);
430
431};
432
433//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000434/// DIE - A structured debug information entry. Has an abbreviation which
435/// describes it's organization.
Jim Laskey0d086af2006-02-27 12:43:29 +0000436class DIE {
437private:
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000438 DIEAbbrev Abbrev; // Buffer for constructing abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000439 unsigned Offset; // Offset in debug info section.
440 unsigned Size; // Size of instance + children.
441 std::vector<DIE *> Children; // Children DIEs.
442 std::vector<DIEValue *> Values; // Attributes values.
443
444public:
445 DIE(unsigned Tag);
446 ~DIE();
447
Jim Laskeybd761842006-02-27 17:27:12 +0000448 // Accessors.
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000449 unsigned getAbbrevNumber() const {
450 return Abbrev.getNumber();
451 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000452 unsigned getOffset() const { return Offset; }
453 unsigned getSize() const { return Size; }
454 const std::vector<DIE *> &getChildren() const { return Children; }
455 const std::vector<DIEValue *> &getValues() const { return Values; }
456 void setOffset(unsigned O) { Offset = O; }
457 void setSize(unsigned S) { Size = S; }
458
459 /// SiblingOffset - Return the offset of the debug information entry's
460 /// sibling.
461 unsigned SiblingOffset() const { return Offset + Size; }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000462
463 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
464 ///
465 void AddSiblingOffset();
Jim Laskey0d086af2006-02-27 12:43:29 +0000466
467 /// AddUInt - Add an unsigned integer attribute data and value.
468 ///
469 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
470
471 /// AddSInt - Add an signed integer attribute data and value.
472 ///
473 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
474
475 /// AddString - Add a std::string attribute data and value.
476 ///
477 void AddString(unsigned Attribute, unsigned Form,
478 const std::string &String);
479
480 /// AddLabel - Add a Dwarf label attribute data and value.
481 ///
482 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
483
484 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
485 ///
486 void AddObjectLabel(unsigned Attribute, unsigned Form,
487 const std::string &Label);
488
489 /// AddDelta - Add a label delta attribute data and value.
490 ///
491 void AddDelta(unsigned Attribute, unsigned Form,
492 const DWLabel &Hi, const DWLabel &Lo);
493
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000494 /// AddDIEntry - Add a DIE attribute data and value.
Jim Laskey0d086af2006-02-27 12:43:29 +0000495 ///
496 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
497
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000498 /// AddBlock - Add block data.
499 ///
500 void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
501
Jim Laskey0d086af2006-02-27 12:43:29 +0000502 /// Complete - Indicate that all attributes have been added and
503 /// ready to get an abbreviation ID.
504 ///
Jim Laskey65195462006-10-30 13:35:07 +0000505 void Complete(Dwarf &DW);
Jim Laskey0d086af2006-02-27 12:43:29 +0000506
507 /// AddChild - Add a child to the DIE.
508 void AddChild(DIE *Child);
509};
510
Jim Laskey65195462006-10-30 13:35:07 +0000511//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000512/// Dwarf - Emits Dwarf debug and exception handling directives.
Jim Laskey65195462006-10-30 13:35:07 +0000513//
514class Dwarf {
515
516private:
517
518 //===--------------------------------------------------------------------===//
519 // Core attributes used by the Dwarf writer.
520 //
521
522 //
523 /// O - Stream to .s file.
524 ///
525 std::ostream &O;
526
527 /// Asm - Target of Dwarf emission.
528 ///
529 AsmPrinter *Asm;
530
531 /// TAI - Target Asm Printer.
532 const TargetAsmInfo *TAI;
533
534 /// TD - Target data.
535 const TargetData *TD;
536
537 /// RI - Register Information.
538 const MRegisterInfo *RI;
539
540 /// M - Current module.
541 ///
542 Module *M;
543
544 /// MF - Current machine function.
545 ///
546 MachineFunction *MF;
547
548 /// DebugInfo - Collected debug information.
549 ///
550 MachineDebugInfo *DebugInfo;
551
552 /// didInitial - Flag to indicate if initial emission has been done.
553 ///
554 bool didInitial;
555
556 /// shouldEmit - Flag to indicate if debug information should be emitted.
557 ///
558 bool shouldEmit;
559
560 /// SubprogramCount - The running count of functions being compiled.
561 ///
562 unsigned SubprogramCount;
563
564 //===--------------------------------------------------------------------===//
565 // Attributes used to construct specific Dwarf sections.
566 //
567
568 /// CompileUnits - All the compile units involved in this build. The index
569 /// of each entry in this vector corresponds to the sources in DebugInfo.
570 std::vector<CompileUnit *> CompileUnits;
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000571
572 /// AbbreviationsSet - Used to uniquely define the abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +0000573 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000574 FoldingSet<DIEAbbrev> AbbreviationsSet;
575
576 /// Abbreviations - A list of all the unique abbreviations in use.
577 ///
578 std::vector<DIEAbbrev *> Abbreviations;
Jim Laskey65195462006-10-30 13:35:07 +0000579
580 /// StringPool - A UniqueVector of strings used by indirect references.
581 /// UnitMap - Map debug information descriptor to compile unit.
582 ///
583 UniqueVector<std::string> StringPool;
584
585 /// UnitMap - Map debug information descriptor to compile unit.
586 ///
587 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
588
589 /// DescToDieMap - Tracks the mapping of top level debug informaton
590 /// descriptors to debug information entries.
591 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
592
593 /// SectionMap - Provides a unique id per text section.
594 ///
595 UniqueVector<std::string> SectionMap;
596
597 /// SectionSourceLines - Tracks line numbers per text section.
598 ///
599 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
600
601
602public:
603
604 //===--------------------------------------------------------------------===//
605 // Emission and print routines
606 //
607
608 /// PrintHex - Print a value as a hexidecimal value.
609 ///
610 void PrintHex(int Value) const;
611
612 /// EOL - Print a newline character to asm stream. If a comment is present
613 /// then it will be printed first. Comments should not contain '\n'.
614 void EOL(const std::string &Comment) const;
615
616 /// EmitAlign - Print a align directive.
617 ///
618 void EmitAlign(unsigned Alignment) const;
619
620 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
621 /// unsigned leb128 value.
622 void EmitULEB128Bytes(unsigned Value) const;
623
624 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
625 /// signed leb128 value.
626 void EmitSLEB128Bytes(int Value) const;
627
628 /// PrintULEB128 - Print a series of hexidecimal values (separated by
629 /// commas) representing an unsigned leb128 value.
630 void PrintULEB128(unsigned Value) const;
631
632 /// SizeULEB128 - Compute the number of bytes required for an unsigned
633 /// leb128 value.
634 static unsigned SizeULEB128(unsigned Value);
635
636 /// PrintSLEB128 - Print a series of hexidecimal values (separated by
637 /// commas) representing a signed leb128 value.
638 void PrintSLEB128(int Value) const;
639
640 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
641 /// value.
642 static unsigned SizeSLEB128(int Value);
643
644 /// EmitInt8 - Emit a byte directive and value.
645 ///
646 void EmitInt8(int Value) const;
647
648 /// EmitInt16 - Emit a short directive and value.
649 ///
650 void EmitInt16(int Value) const;
651
652 /// EmitInt32 - Emit a long directive and value.
653 ///
654 void EmitInt32(int Value) const;
655
656 /// EmitInt64 - Emit a long long directive and value.
657 ///
658 void EmitInt64(uint64_t Value) const;
659
660 /// EmitString - Emit a string with quotes and a null terminator.
661 /// Special characters are emitted properly.
662 /// \literal (Eg. '\t') \endliteral
663 void EmitString(const std::string &String) const;
664
665 /// PrintLabelName - Print label name in form used by Dwarf writer.
666 ///
667 void PrintLabelName(DWLabel Label) const {
668 PrintLabelName(Label.Tag, Label.Number);
669 }
670 void PrintLabelName(const char *Tag, unsigned Number) const;
671
672 /// EmitLabel - Emit location label for internal use by Dwarf.
673 ///
674 void EmitLabel(DWLabel Label) const {
675 EmitLabel(Label.Tag, Label.Number);
676 }
677 void EmitLabel(const char *Tag, unsigned Number) const;
678
679 /// EmitReference - Emit a reference to a label.
680 ///
681 void EmitReference(DWLabel Label) const {
682 EmitReference(Label.Tag, Label.Number);
683 }
684 void EmitReference(const char *Tag, unsigned Number) const;
685 void EmitReference(const std::string &Name) const;
686
687 /// EmitDifference - Emit the difference between two labels. Some
688 /// assemblers do not behave with absolute expressions with data directives,
689 /// so there is an option (needsSet) to use an intermediary set expression.
690 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo) const {
691 EmitDifference(LabelHi.Tag, LabelHi.Number, LabelLo.Tag, LabelLo.Number);
692 }
693 void EmitDifference(const char *TagHi, unsigned NumberHi,
694 const char *TagLo, unsigned NumberLo) const;
695
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000696 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Jim Laskey65195462006-10-30 13:35:07 +0000697 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000698 void AssignAbbrevNumber(DIEAbbrev *Abbrev);
Jim Laskey65195462006-10-30 13:35:07 +0000699
700 /// NewString - Add a string to the constant pool and returns a label.
701 ///
702 DWLabel NewString(const std::string &String);
703
704 /// getDieMapSlotFor - Returns the debug information entry map slot for the
705 /// specified debug descriptor.
706 DIE *&getDieMapSlotFor(DebugInfoDesc *DD);
707
708private:
709
710 /// AddSourceLine - Add location information to specified debug information
711 /// entry.
712 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line);
713
714 /// AddAddress - Add an address attribute to a die based on the location
715 /// provided.
716 void AddAddress(DIE *Die, unsigned Attribute,
717 const MachineLocation &Location);
718
719 /// NewType - Create a new type DIE.
720 ///
721 DIE *NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit);
722
723 /// NewCompileUnit - Create new compile unit and it's die.
724 ///
725 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID);
726
727 /// FindCompileUnit - Get the compile unit for the given descriptor.
728 ///
729 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc);
730
731 /// NewGlobalVariable - Make a new global variable DIE.
732 ///
733 DIE *NewGlobalVariable(GlobalVariableDesc *GVD);
734
735 /// NewSubprogram - Add a new subprogram DIE.
736 ///
737 DIE *NewSubprogram(SubprogramDesc *SPD);
738
739 /// NewScopeVariable - Create a new scope variable.
740 ///
741 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit);
742
743 /// ConstructScope - Construct the components of a scope.
744 ///
745 void ConstructScope(DebugScope *ParentScope, DIE *ParentDie,
746 CompileUnit *Unit);
747
748 /// ConstructRootScope - Construct the scope for the subprogram.
749 ///
750 void ConstructRootScope(DebugScope *RootScope);
751
752 /// EmitInitial - Emit initial Dwarf declarations.
753 ///
754 void EmitInitial();
755
756 /// EmitDIE - Recusively Emits a debug information entry.
757 ///
758 void EmitDIE(DIE *Die) const;
759
760 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
761 ///
762 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last);
763
764 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
765 ///
766 void SizeAndOffsets();
767
768 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
769 /// frame.
770 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
771 std::vector<MachineMove *> &Moves);
772
773 /// EmitDebugInfo - Emit the debug info section.
774 ///
775 void EmitDebugInfo() const;
776
777 /// EmitAbbreviations - Emit the abbreviation section.
778 ///
779 void EmitAbbreviations() const;
780
781 /// EmitDebugLines - Emit source line information.
782 ///
783 void EmitDebugLines() const;
784
785 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
786 ///
787 void EmitInitialDebugFrame();
788
789 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
790 /// section.
791 void EmitFunctionDebugFrame();
792
793 /// EmitDebugPubNames - Emit info into a debug pubnames section.
794 ///
795 void EmitDebugPubNames();
796
797 /// EmitDebugStr - Emit info into a debug str section.
798 ///
799 void EmitDebugStr();
800
801 /// EmitDebugLoc - Emit info into a debug loc section.
802 ///
803 void EmitDebugLoc();
804
805 /// EmitDebugARanges - Emit info into a debug aranges section.
806 ///
807 void EmitDebugARanges();
808
809 /// EmitDebugRanges - Emit info into a debug ranges section.
810 ///
811 void EmitDebugRanges();
812
813 /// EmitDebugMacInfo - Emit info into a debug macinfo section.
814 ///
815 void EmitDebugMacInfo();
816
817 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
818 /// header file.
819 void ConstructCompileUnitDIEs();
820
821 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
822 /// global variables.
823 void ConstructGlobalDIEs();
824
825 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
826 /// subprograms.
827 void ConstructSubprogramDIEs();
828
829 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
830 ///
831 bool ShouldEmitDwarf() const { return shouldEmit; }
832
833public:
834
835 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T);
836 virtual ~Dwarf();
837
838 // Accessors.
839 //
840 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
841
842 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
843 /// created it. Set by the target AsmPrinter.
844 void SetDebugInfo(MachineDebugInfo *DI);
845
846 //===--------------------------------------------------------------------===//
847 // Main entry points.
848 //
849
850 /// BeginModule - Emit all Dwarf sections that should come prior to the
851 /// content.
852 void BeginModule(Module *M);
853
854 /// EndModule - Emit all Dwarf sections that should come after the content.
855 ///
856 void EndModule();
857
858 /// BeginFunction - Gather pre-function debug information. Assumes being
859 /// emitted immediately after the function entry point.
860 void BeginFunction(MachineFunction *MF);
861
862 /// EndFunction - Gather and emit post-function debug information.
863 ///
864 void EndFunction();
865};
866
Jim Laskey0d086af2006-02-27 12:43:29 +0000867} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +0000868
869//===----------------------------------------------------------------------===//
870
Jim Laskeybd761842006-02-27 17:27:12 +0000871CompileUnit::~CompileUnit() {
872 delete Die;
873}
874
875/// hasContent - Return true if this compile unit has something to write out.
876///
877bool CompileUnit::hasContent() const {
878 return !Die->getChildren().empty();
879}
880
881/// AddGlobal - Add a new global entity to the compile unit.
882///
883void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
884 Globals[Name] = Die;
885}
886
887//===----------------------------------------------------------------------===//
888
Jim Laskeyd18e2892006-01-20 20:34:06 +0000889/// Emit - Print the abbreviation using the specified Dwarf writer.
890///
Jim Laskey65195462006-10-30 13:35:07 +0000891void DIEAbbrev::Emit(const Dwarf &DW) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000892 // Emit its Dwarf tag type.
893 DW.EmitULEB128Bytes(Tag);
894 DW.EOL(TagString(Tag));
895
896 // Emit whether it has children DIEs.
897 DW.EmitULEB128Bytes(ChildrenFlag);
898 DW.EOL(ChildrenString(ChildrenFlag));
899
900 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +0000901 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000902 const DIEAbbrevData &AttrData = Data[i];
903
904 // Emit attribute type.
905 DW.EmitULEB128Bytes(AttrData.getAttribute());
906 DW.EOL(AttributeString(AttrData.getAttribute()));
907
908 // Emit form type.
909 DW.EmitULEB128Bytes(AttrData.getForm());
910 DW.EOL(FormEncodingString(AttrData.getForm()));
911 }
912
913 // Mark end of abbreviation.
914 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
915 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
916}
917
918#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +0000919void DIEAbbrev::print(std::ostream &O) {
920 O << "Abbreviation @"
921 << std::hex << (intptr_t)this << std::dec
922 << " "
923 << TagString(Tag)
924 << " "
925 << ChildrenString(ChildrenFlag)
926 << "\n";
927
928 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
929 O << " "
930 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +0000931 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +0000932 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +0000933 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +0000934 }
Jim Laskeya0f3d172006-09-07 22:06:40 +0000935}
936void DIEAbbrev::dump() { print(std::cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +0000937#endif
938
939//===----------------------------------------------------------------------===//
940
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000941/// BestForm - Choose the best form for integer.
942///
943unsigned DIEInteger::BestForm(bool IsSigned) {
944 if (IsSigned) {
945 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
946 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
947 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
948 } else {
949 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
950 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
951 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
952 }
953 return DW_FORM_data8;
954}
955
Jim Laskey063e7652006-01-17 17:31:53 +0000956/// EmitValue - Emit integer of appropriate size.
957///
Jim Laskey65195462006-10-30 13:35:07 +0000958void DIEInteger::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +0000959 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +0000960 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000961 case DW_FORM_ref1: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000962 case DW_FORM_data1: DW.EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000963 case DW_FORM_ref2: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000964 case DW_FORM_data2: DW.EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000965 case DW_FORM_ref4: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000966 case DW_FORM_data4: DW.EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000967 case DW_FORM_ref8: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000968 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +0000969 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
970 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +0000971 default: assert(0 && "DIE Value form not supported yet"); break;
972 }
973}
974
975/// SizeOf - Determine size of integer value in bytes.
976///
Jim Laskey65195462006-10-30 13:35:07 +0000977unsigned DIEInteger::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +0000978 switch (Form) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000979 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +0000980 case DW_FORM_ref1: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000981 case DW_FORM_data1: return sizeof(int8_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000982 case DW_FORM_ref2: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000983 case DW_FORM_data2: return sizeof(int16_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000984 case DW_FORM_ref4: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000985 case DW_FORM_data4: return sizeof(int32_t);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000986 case DW_FORM_ref8: // Fall thru
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000987 case DW_FORM_data8: return sizeof(int64_t);
Jim Laskey40020172006-01-20 21:02:36 +0000988 case DW_FORM_udata: return DW.SizeULEB128(Integer);
989 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +0000990 default: assert(0 && "DIE Value form not supported yet"); break;
991 }
992 return 0;
993}
994
995//===----------------------------------------------------------------------===//
996
997/// EmitValue - Emit string value.
998///
Jim Laskey65195462006-10-30 13:35:07 +0000999void DIEString::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001000 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00001001}
1002
1003/// SizeOf - Determine size of string value in bytes.
1004///
Jim Laskey65195462006-10-30 13:35:07 +00001005unsigned DIEString::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey40020172006-01-20 21:02:36 +00001006 return String.size() + sizeof(char); // sizeof('\0');
Jim Laskey063e7652006-01-17 17:31:53 +00001007}
1008
1009//===----------------------------------------------------------------------===//
1010
1011/// EmitValue - Emit label value.
1012///
Jim Laskey65195462006-10-30 13:35:07 +00001013void DIEDwarfLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001014 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +00001015}
1016
1017/// SizeOf - Determine size of label value in bytes.
1018///
Jim Laskey65195462006-10-30 13:35:07 +00001019unsigned DIEDwarfLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001020 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00001021}
1022
1023//===----------------------------------------------------------------------===//
1024
Jim Laskeyd18e2892006-01-20 20:34:06 +00001025/// EmitValue - Emit label value.
1026///
Jim Laskey65195462006-10-30 13:35:07 +00001027void DIEObjectLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001028 DW.EmitReference(Label);
1029}
1030
1031/// SizeOf - Determine size of label value in bytes.
1032///
Jim Laskey65195462006-10-30 13:35:07 +00001033unsigned DIEObjectLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001034 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001035}
1036
1037//===----------------------------------------------------------------------===//
1038
Jim Laskey063e7652006-01-17 17:31:53 +00001039/// EmitValue - Emit delta value.
1040///
Jim Laskey65195462006-10-30 13:35:07 +00001041void DIEDelta::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001042 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001043}
1044
1045/// SizeOf - Determine size of delta value in bytes.
1046///
Jim Laskey65195462006-10-30 13:35:07 +00001047unsigned DIEDelta::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001048 return DW.getTargetAsmInfo()->getAddressSize();
Jim Laskey063e7652006-01-17 17:31:53 +00001049}
1050
1051//===----------------------------------------------------------------------===//
Jim Laskeyb8509c52006-03-23 18:07:55 +00001052/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001053///
Jim Laskey65195462006-10-30 13:35:07 +00001054void DIEntry::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001055 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00001056}
1057
Jim Laskeyb8509c52006-03-23 18:07:55 +00001058/// SizeOf - Determine size of debug information entry value in bytes.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001059///
Jim Laskey65195462006-10-30 13:35:07 +00001060unsigned DIEntry::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001061 return sizeof(int32_t);
1062}
1063
1064//===----------------------------------------------------------------------===//
1065
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001066DIEBlock::~DIEBlock() {
1067 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1068 delete Values[i];
1069 }
1070}
1071
1072/// ComputeSize - calculate the size of the block.
1073///
Jim Laskey65195462006-10-30 13:35:07 +00001074unsigned DIEBlock::ComputeSize(Dwarf &DW) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001075 Size = 0;
1076 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1077 Size += Values[i]->SizeOf(DW, Forms[i]);
1078 }
1079 return Size;
1080}
1081
1082/// BestForm - Choose the best form for data.
1083///
1084unsigned DIEBlock::BestForm() {
1085 if ((unsigned char)Size == Size) return DW_FORM_block1;
1086 if ((unsigned short)Size == Size) return DW_FORM_block2;
1087 if ((unsigned int)Size == Size) return DW_FORM_block4;
1088 return DW_FORM_block;
1089}
1090
1091/// EmitValue - Emit block data.
1092///
Jim Laskey65195462006-10-30 13:35:07 +00001093void DIEBlock::EmitValue(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001094 switch (Form) {
1095 case DW_FORM_block1: DW.EmitInt8(Size); break;
1096 case DW_FORM_block2: DW.EmitInt16(Size); break;
1097 case DW_FORM_block4: DW.EmitInt32(Size); break;
1098 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
1099 default: assert(0 && "Improper form for block"); break;
1100 }
1101 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1102 DW.EOL("");
1103 Values[i]->EmitValue(DW, Forms[i]);
1104 }
1105}
1106
1107/// SizeOf - Determine size of block data in bytes.
1108///
Jim Laskey65195462006-10-30 13:35:07 +00001109unsigned DIEBlock::SizeOf(const Dwarf &DW, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001110 switch (Form) {
1111 case DW_FORM_block1: return Size + sizeof(int8_t);
1112 case DW_FORM_block2: return Size + sizeof(int16_t);
1113 case DW_FORM_block4: return Size + sizeof(int32_t);
1114 case DW_FORM_block: return Size + DW.SizeULEB128(Size);
1115 default: assert(0 && "Improper form for block"); break;
1116 }
1117 return 0;
1118}
1119
1120/// AddUInt - Add an unsigned integer value.
1121///
1122void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
1123 DIEInteger *DI = new DIEInteger(Integer);
1124 Values.push_back(DI);
1125 if (Form == 0) Form = DI->BestForm(false);
1126 Forms.push_back(Form);
1127}
1128
1129/// AddSInt - Add an signed integer value.
1130///
1131void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
1132 DIEInteger *DI = new DIEInteger(Integer);
1133 Values.push_back(DI);
1134 if (Form == 0) Form = DI->BestForm(true);
1135 Forms.push_back(Form);
1136}
1137
1138/// AddString - Add a std::string value.
1139///
1140void DIEBlock::AddString(unsigned Form, const std::string &String) {
1141 Values.push_back(new DIEString(String));
1142 Forms.push_back(Form);
1143}
1144
1145/// AddLabel - Add a Dwarf label value.
1146///
1147void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
1148 Values.push_back(new DIEDwarfLabel(Label));
1149 Forms.push_back(Form);
1150}
1151
1152/// AddObjectLabel - Add a non-Dwarf label value.
1153///
1154void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
1155 Values.push_back(new DIEObjectLabel(Label));
1156 Forms.push_back(Form);
1157}
1158
1159/// AddDelta - Add a label delta value.
1160///
1161void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
1162 Values.push_back(new DIEDelta(Hi, Lo));
1163 Forms.push_back(Form);
1164}
1165
1166/// AddDIEntry - Add a DIE value.
1167///
1168void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
1169 Values.push_back(new DIEntry(Entry));
1170 Forms.push_back(Form);
1171}
1172
1173//===----------------------------------------------------------------------===//
1174
Jim Laskey0420f2a2006-02-22 19:02:11 +00001175DIE::DIE(unsigned Tag)
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001176: Abbrev(Tag, DW_CHILDREN_no)
Jim Laskeyd18e2892006-01-20 20:34:06 +00001177, Offset(0)
1178, Size(0)
Jim Laskeyd18e2892006-01-20 20:34:06 +00001179, Children()
1180, Values()
1181{}
1182
1183DIE::~DIE() {
Jim Laskey52060a02006-01-24 00:49:18 +00001184 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001185 delete Children[i];
1186 }
1187
Jim Laskey52060a02006-01-24 00:49:18 +00001188 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001189 delete Values[j];
1190 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00001191}
1192
Jim Laskeyb8509c52006-03-23 18:07:55 +00001193/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
1194///
1195void DIE::AddSiblingOffset() {
1196 DIEInteger *DI = new DIEInteger(0);
1197 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001198 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001199}
1200
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001201/// AddUInt - Add an unsigned integer attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001202///
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001203void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001204 DIEInteger *DI = new DIEInteger(Integer);
1205 Values.push_back(DI);
1206 if (!Form) Form = DI->BestForm(false);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001207 Abbrev.AddAttribute(Attribute, Form);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001208}
1209
1210/// AddSInt - Add an signed integer attribute data and value.
1211///
1212void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001213 DIEInteger *DI = new DIEInteger(Integer);
1214 Values.push_back(DI);
1215 if (!Form) Form = DI->BestForm(true);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001216 Abbrev.AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001217}
1218
1219/// AddString - Add a std::string attribute data and value.
1220///
1221void DIE::AddString(unsigned Attribute, unsigned Form,
1222 const std::string &String) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001223 Values.push_back(new DIEString(String));
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001224 Abbrev.AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001225}
1226
1227/// AddLabel - Add a Dwarf label attribute data and value.
1228///
1229void DIE::AddLabel(unsigned Attribute, unsigned Form,
1230 const DWLabel &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +00001231 Values.push_back(new DIEDwarfLabel(Label));
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001232 Abbrev.AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001233}
1234
Jim Laskey52060a02006-01-24 00:49:18 +00001235/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001236///
Jim Laskey52060a02006-01-24 00:49:18 +00001237void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
1238 const std::string &Label) {
Jim Laskey52060a02006-01-24 00:49:18 +00001239 Values.push_back(new DIEObjectLabel(Label));
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001240 Abbrev.AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001241}
1242
1243/// AddDelta - Add a label delta attribute data and value.
1244///
1245void DIE::AddDelta(unsigned Attribute, unsigned Form,
1246 const DWLabel &Hi, const DWLabel &Lo) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001247 Values.push_back(new DIEDelta(Hi, Lo));
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001248 Abbrev.AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001249}
1250
1251/// AddDIEntry - Add a DIE attribute data and value.
1252///
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001253void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001254 Values.push_back(new DIEntry(Entry));
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001255 Abbrev.AddAttribute(Attribute, Form);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001256}
1257
1258/// AddBlock - Add block data.
1259///
1260void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
1261 assert(Block->Size && "Block size has not been computed");
1262 Values.push_back(Block);
1263 if (!Form) Form = Block->BestForm();
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001264 Abbrev.AddAttribute(Attribute, Form);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001265}
1266
1267/// Complete - Indicate that all attributes have been added and ready to get an
1268/// abbreviation ID.
Jim Laskey65195462006-10-30 13:35:07 +00001269void DIE::Complete(Dwarf &DW) {
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001270 DW.AssignAbbrevNumber(&Abbrev);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001271}
1272
1273/// AddChild - Add a child to the DIE.
1274///
1275void DIE::AddChild(DIE *Child) {
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001276 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001277 Children.push_back(Child);
1278}
1279
1280//===----------------------------------------------------------------------===//
1281
Jim Laskey65195462006-10-30 13:35:07 +00001282/// Dwarf
Jim Laskeyd18e2892006-01-20 20:34:06 +00001283
1284//===----------------------------------------------------------------------===//
Jim Laskey063e7652006-01-17 17:31:53 +00001285
1286/// PrintHex - Print a value as a hexidecimal value.
1287///
Jim Laskey65195462006-10-30 13:35:07 +00001288void Dwarf::PrintHex(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001289 O << "0x" << std::hex << Value << std::dec;
1290}
1291
1292/// EOL - Print a newline character to asm stream. If a comment is present
1293/// then it will be printed first. Comments should not contain '\n'.
Jim Laskey65195462006-10-30 13:35:07 +00001294void Dwarf::EOL(const std::string &Comment) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001295 if (DwarfVerbose && !Comment.empty()) {
Jim Laskey063e7652006-01-17 17:31:53 +00001296 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00001297 << TAI->getCommentString()
Jim Laskey063e7652006-01-17 17:31:53 +00001298 << " "
1299 << Comment;
1300 }
1301 O << "\n";
1302}
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001303
Jim Laskeyb8509c52006-03-23 18:07:55 +00001304/// EmitAlign - Print a align directive.
1305///
Jim Laskey65195462006-10-30 13:35:07 +00001306void Dwarf::EmitAlign(unsigned Alignment) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001307 O << TAI->getAlignDirective() << Alignment << "\n";
Jim Laskeyb8509c52006-03-23 18:07:55 +00001308}
1309
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001310/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
Jim Laskey063e7652006-01-17 17:31:53 +00001311/// unsigned leb128 value.
Jim Laskey65195462006-10-30 13:35:07 +00001312void Dwarf::EmitULEB128Bytes(unsigned Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001313 if (TAI->hasLEB128()) {
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001314 O << "\t.uleb128\t"
1315 << Value;
1316 } else {
Jim Laskey563321a2006-09-06 18:34:40 +00001317 O << TAI->getData8bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001318 PrintULEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001319 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001320}
1321
1322/// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
Jim Laskey063e7652006-01-17 17:31:53 +00001323/// signed leb128 value.
Jim Laskey65195462006-10-30 13:35:07 +00001324void Dwarf::EmitSLEB128Bytes(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001325 if (TAI->hasLEB128()) {
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001326 O << "\t.sleb128\t"
1327 << Value;
1328 } else {
Jim Laskey563321a2006-09-06 18:34:40 +00001329 O << TAI->getData8bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001330 PrintSLEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001331 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001332}
1333
Jim Laskey063e7652006-01-17 17:31:53 +00001334/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +00001335/// representing an unsigned leb128 value.
Jim Laskey65195462006-10-30 13:35:07 +00001336void Dwarf::PrintULEB128(unsigned Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001337 do {
1338 unsigned Byte = Value & 0x7f;
1339 Value >>= 7;
1340 if (Value) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001341 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001342 if (Value) O << ", ";
1343 } while (Value);
1344}
1345
Jim Laskey063e7652006-01-17 17:31:53 +00001346/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
1347/// value.
Jim Laskey65195462006-10-30 13:35:07 +00001348unsigned Dwarf::SizeULEB128(unsigned Value) {
Jim Laskey063e7652006-01-17 17:31:53 +00001349 unsigned Size = 0;
1350 do {
1351 Value >>= 7;
1352 Size += sizeof(int8_t);
1353 } while (Value);
1354 return Size;
1355}
1356
1357/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +00001358/// representing a signed leb128 value.
Jim Laskey65195462006-10-30 13:35:07 +00001359void Dwarf::PrintSLEB128(int Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001360 int Sign = Value >> (8 * sizeof(Value) - 1);
1361 bool IsMore;
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001362
Jim Laskeyb2efb852006-01-04 22:28:25 +00001363 do {
1364 unsigned Byte = Value & 0x7f;
1365 Value >>= 7;
1366 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1367 if (IsMore) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +00001368 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001369 if (IsMore) O << ", ";
1370 } while (IsMore);
1371}
1372
Jim Laskey063e7652006-01-17 17:31:53 +00001373/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
1374/// value.
Jim Laskey65195462006-10-30 13:35:07 +00001375unsigned Dwarf::SizeSLEB128(int Value) {
Jim Laskey063e7652006-01-17 17:31:53 +00001376 unsigned Size = 0;
1377 int Sign = Value >> (8 * sizeof(Value) - 1);
1378 bool IsMore;
1379
1380 do {
1381 unsigned Byte = Value & 0x7f;
1382 Value >>= 7;
1383 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1384 Size += sizeof(int8_t);
1385 } while (IsMore);
1386 return Size;
1387}
1388
Jim Laskeyda427fa2006-01-27 20:31:25 +00001389/// EmitInt8 - Emit a byte directive and value.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001390///
Jim Laskey65195462006-10-30 13:35:07 +00001391void Dwarf::EmitInt8(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001392 O << TAI->getData8bitsDirective();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001393 PrintHex(Value & 0xFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001394}
1395
Jim Laskeyda427fa2006-01-27 20:31:25 +00001396/// EmitInt16 - Emit a short directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001397///
Jim Laskey65195462006-10-30 13:35:07 +00001398void Dwarf::EmitInt16(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001399 O << TAI->getData16bitsDirective();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001400 PrintHex(Value & 0xFFFF);
Jim Laskey063e7652006-01-17 17:31:53 +00001401}
1402
Jim Laskeyda427fa2006-01-27 20:31:25 +00001403/// EmitInt32 - Emit a long directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +00001404///
Jim Laskey65195462006-10-30 13:35:07 +00001405void Dwarf::EmitInt32(int Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001406 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001407 PrintHex(Value);
1408}
1409
Jim Laskeyda427fa2006-01-27 20:31:25 +00001410/// EmitInt64 - Emit a long long directive and value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001411///
Jim Laskey65195462006-10-30 13:35:07 +00001412void Dwarf::EmitInt64(uint64_t Value) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001413 if (TAI->getData64bitsDirective()) {
1414 O << TAI->getData64bitsDirective() << "0x" << std::hex << Value << std::dec;
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001415 } else {
Owen Andersona69571c2006-05-03 01:29:57 +00001416 if (TD->isBigEndian()) {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001417 EmitInt32(unsigned(Value >> 32)); O << "\n";
1418 EmitInt32(unsigned(Value));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001419 } else {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001420 EmitInt32(unsigned(Value)); O << "\n";
1421 EmitInt32(unsigned(Value >> 32));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001422 }
1423 }
1424}
1425
Jim Laskey063e7652006-01-17 17:31:53 +00001426/// EmitString - Emit a string with quotes and a null terminator.
1427/// Special characters are emitted properly. (Eg. '\t')
Jim Laskey65195462006-10-30 13:35:07 +00001428void Dwarf::EmitString(const std::string &String) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001429 O << TAI->getAsciiDirective()
Jim Laskey063e7652006-01-17 17:31:53 +00001430 << "\"";
Jim Laskey52060a02006-01-24 00:49:18 +00001431 for (unsigned i = 0, N = String.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001432 unsigned char C = String[i];
1433
1434 if (!isascii(C) || iscntrl(C)) {
1435 switch(C) {
1436 case '\b': O << "\\b"; break;
1437 case '\f': O << "\\f"; break;
1438 case '\n': O << "\\n"; break;
1439 case '\r': O << "\\r"; break;
1440 case '\t': O << "\\t"; break;
1441 default:
1442 O << '\\';
Jim Laskey3ebe71d2006-09-01 12:55:05 +00001443 O << char('0' + ((C >> 6) & 7));
1444 O << char('0' + ((C >> 3) & 7));
1445 O << char('0' + ((C >> 0) & 7));
Jim Laskey063e7652006-01-17 17:31:53 +00001446 break;
1447 }
1448 } else if (C == '\"') {
1449 O << "\\\"";
1450 } else if (C == '\'') {
1451 O << "\\\'";
1452 } else {
1453 O << C;
1454 }
1455 }
1456 O << "\\0\"";
1457}
1458
1459/// PrintLabelName - Print label name in form used by Dwarf writer.
1460///
Jim Laskey65195462006-10-30 13:35:07 +00001461void Dwarf::PrintLabelName(const char *Tag, unsigned Number) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001462 O << TAI->getPrivateGlobalPrefix()
Jim Laskeyb2efb852006-01-04 22:28:25 +00001463 << "debug_"
Jim Laskey0420f2a2006-02-22 19:02:11 +00001464 << Tag;
1465 if (Number) O << Number;
Jim Laskeyb2efb852006-01-04 22:28:25 +00001466}
1467
Jim Laskey063e7652006-01-17 17:31:53 +00001468/// EmitLabel - Emit location label for internal use by Dwarf.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001469///
Jim Laskey65195462006-10-30 13:35:07 +00001470void Dwarf::EmitLabel(const char *Tag, unsigned Number) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001471 PrintLabelName(Tag, Number);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001472 O << ":\n";
1473}
1474
Jim Laskeye719a7c2006-01-18 16:54:26 +00001475/// EmitReference - Emit a reference to a label.
Jim Laskey063e7652006-01-17 17:31:53 +00001476///
Jim Laskey65195462006-10-30 13:35:07 +00001477void Dwarf::EmitReference(const char *Tag, unsigned Number) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001478 if (TAI->getAddressSize() == 4)
1479 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001480 else
Jim Laskey563321a2006-09-06 18:34:40 +00001481 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001482
1483 PrintLabelName(Tag, Number);
1484}
Jim Laskey65195462006-10-30 13:35:07 +00001485void Dwarf::EmitReference(const std::string &Name) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001486 if (TAI->getAddressSize() == 4)
1487 O << TAI->getData32bitsDirective();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001488 else
Jim Laskey563321a2006-09-06 18:34:40 +00001489 O << TAI->getData64bitsDirective();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001490
1491 O << Name;
1492}
Jim Laskey063e7652006-01-17 17:31:53 +00001493
1494/// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
1495/// assemblers do not accept absolute expressions with data directives, so there
1496/// is an option (needsSet) to use an intermediary 'set' expression.
Jim Laskey65195462006-10-30 13:35:07 +00001497void Dwarf::EmitDifference(const char *TagHi, unsigned NumberHi,
Jim Laskeyd18e2892006-01-20 20:34:06 +00001498 const char *TagLo, unsigned NumberLo) const {
Jim Laskeya0f3d172006-09-07 22:06:40 +00001499 if (TAI->needsSet()) {
Jim Laskey063e7652006-01-17 17:31:53 +00001500 static unsigned SetCounter = 0;
Jim Laskeyd18e2892006-01-20 20:34:06 +00001501
Jim Laskey063e7652006-01-17 17:31:53 +00001502 O << "\t.set\t";
1503 PrintLabelName("set", SetCounter);
1504 O << ",";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001505 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001506 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001507 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001508 O << "\n";
1509
Jim Laskey563321a2006-09-06 18:34:40 +00001510 if (TAI->getAddressSize() == sizeof(int32_t))
1511 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001512 else
Jim Laskey563321a2006-09-06 18:34:40 +00001513 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001514
1515 PrintLabelName("set", SetCounter);
1516
Jim Laskey52060a02006-01-24 00:49:18 +00001517 ++SetCounter;
Jim Laskey063e7652006-01-17 17:31:53 +00001518 } else {
Jim Laskey563321a2006-09-06 18:34:40 +00001519 if (TAI->getAddressSize() == sizeof(int32_t))
1520 O << TAI->getData32bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001521 else
Jim Laskey563321a2006-09-06 18:34:40 +00001522 O << TAI->getData64bitsDirective();
Jim Laskey063e7652006-01-17 17:31:53 +00001523
Jim Laskeyd18e2892006-01-20 20:34:06 +00001524 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001525 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001526 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001527 }
1528}
1529
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001530/// AssignAbbrevNumber - Define a unique number for the abbreviation.
Jim Laskey063e7652006-01-17 17:31:53 +00001531///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001532void Dwarf::AssignAbbrevNumber(DIEAbbrev *Abbrev) {
1533 // Profile the node so that we can make it unique.
1534 FoldingSetNodeID ID;
1535 Abbrev->Profile(ID);
1536
1537 // Check the set for priors.
1538 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(Abbrev);
1539
1540 // If it's newly added.
1541 if (InSet == Abbrev) {
1542 // Add to abbreviation list.
1543 Abbreviations.push_back(Abbrev);
1544 // Assign the vector position + 1 as its number.
1545 Abbrev->setNumber(Abbreviations.size());
1546 } else {
1547 // Assign existing abbreviation number.
1548 Abbrev->setNumber(InSet->getNumber());
1549 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00001550}
1551
1552/// NewString - Add a string to the constant pool and returns a label.
1553///
Jim Laskey65195462006-10-30 13:35:07 +00001554DWLabel Dwarf::NewString(const std::string &String) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001555 unsigned StringID = StringPool.insert(String);
1556 return DWLabel("string", StringID);
1557}
1558
Jim Laskeyb8509c52006-03-23 18:07:55 +00001559/// AddSourceLine - Add location information to specified debug information
1560/// entry.
Jim Laskey65195462006-10-30 13:35:07 +00001561void Dwarf::AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line){
Jim Laskeyb8509c52006-03-23 18:07:55 +00001562 if (File && Line) {
1563 CompileUnit *FileUnit = FindCompileUnit(File);
1564 unsigned FileID = FileUnit->getID();
1565 Die->AddUInt(DW_AT_decl_file, 0, FileID);
1566 Die->AddUInt(DW_AT_decl_line, 0, Line);
1567 }
1568}
1569
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001570/// AddAddress - Add an address attribute to a die based on the location
1571/// provided.
Jim Laskey65195462006-10-30 13:35:07 +00001572void Dwarf::AddAddress(DIE *Die, unsigned Attribute,
Jim Laskey41886992006-04-07 16:34:46 +00001573 const MachineLocation &Location) {
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001574 DIEBlock *Block = new DIEBlock();
Jim Laskey3d3d4042006-08-25 19:39:52 +00001575 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1576
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001577 if (Location.isRegister()) {
Jim Laskey3d3d4042006-08-25 19:39:52 +00001578 if (Reg < 32) {
1579 Block->AddUInt(DW_FORM_data1, DW_OP_reg0 + Reg);
1580 } else {
1581 Block->AddUInt(DW_FORM_data1, DW_OP_regx);
1582 Block->AddUInt(DW_FORM_udata, Reg);
1583 }
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001584 } else {
Jim Laskey3d3d4042006-08-25 19:39:52 +00001585 if (Reg < 32) {
1586 Block->AddUInt(DW_FORM_data1, DW_OP_breg0 + Reg);
1587 } else {
1588 Block->AddUInt(DW_FORM_data1, DW_OP_bregx);
1589 Block->AddUInt(DW_FORM_udata, Reg);
1590 }
Jim Laskeyb3e7be22006-03-28 14:58:32 +00001591 Block->AddUInt(DW_FORM_sdata, Location.getOffset());
1592 }
1593 Block->ComputeSize(*this);
1594 Die->AddBlock(Attribute, 0, Block);
1595}
1596
Jim Laskey90c79d72006-03-23 23:02:34 +00001597/// getDieMapSlotFor - Returns the debug information entry map slot for the
1598/// specified debug descriptor.
Jim Laskey65195462006-10-30 13:35:07 +00001599DIE *&Dwarf::getDieMapSlotFor(DebugInfoDesc *DD) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001600 return DescToDieMap[DD];
Jim Laskey0420f2a2006-02-22 19:02:11 +00001601}
Jim Laskey760383e2006-08-21 21:20:18 +00001602
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001603/// NewType - Create a new type DIE.
1604///
Jim Laskey65195462006-10-30 13:35:07 +00001605DIE *Dwarf::NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001606 if (!TyDesc) {
1607 // FIXME - Hack for missing types
1608 DIE *Die = new DIE(DW_TAG_base_type);
1609 Die->AddUInt(DW_AT_byte_size, 0, 4);
1610 Die->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1611 Unit->getDie()->AddChild(Die);
1612 return Die;
1613 }
Jim Laskey0c0feb92006-10-04 10:40:15 +00001614
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001615 // Check for pre-existence.
Jim Laskey90c79d72006-03-23 23:02:34 +00001616 DIE *&Slot = Unit->getDieMapSlotFor(TyDesc);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001617 if (Slot) return Slot;
1618
Jim Laskey0c0feb92006-10-04 10:40:15 +00001619 // Type DIE result.
1620 DIE *Ty = NULL;
1621
Jim Laskey339ec4c2006-10-13 13:02:19 +00001622 // FIXME - Not sure why programs and variables are coming through here.
Jim Laskey0c0feb92006-10-04 10:40:15 +00001623 // Short cut for handling subprogram types (not really a TyDesc.)
1624 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1625 Slot = Ty = new DIE(DW_TAG_pointer_type);
1626 Ty->AddUInt(DW_AT_byte_size, 0, TAI->getAddressSize());
1627 Ty->AddString(DW_AT_name, DW_FORM_string, SubprogramTy->getName());
1628 Context->AddChild(Ty);
1629 return Slot;
1630 }
Jim Laskey339ec4c2006-10-13 13:02:19 +00001631 // Short cut for handling global variable types (not really a TyDesc.)
1632 if (GlobalVariableDesc *GlobalVariableTy =
1633 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1634 Slot = Ty = new DIE(DW_TAG_pointer_type);
1635 Ty->AddUInt(DW_AT_byte_size, 0, TAI->getAddressSize());
1636 Ty->AddString(DW_AT_name, DW_FORM_string, GlobalVariableTy->getName());
1637 Context->AddChild(Ty);
1638 return Slot;
1639 }
Jim Laskey0c0feb92006-10-04 10:40:15 +00001640
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001641 // Get core information.
1642 const std::string &Name = TyDesc->getName();
Jim Laskey288fe0f2006-03-01 18:13:05 +00001643 uint64_t Size = TyDesc->getSize() >> 3;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001644
Jim Laskey434b40b2006-02-23 22:37:30 +00001645 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001646 // Fundamental types like int, float, bool
Jim Laskey434b40b2006-02-23 22:37:30 +00001647 Slot = Ty = new DIE(DW_TAG_base_type);
1648 unsigned Encoding = BasicTy->getEncoding();
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001649 Ty->AddUInt(DW_AT_encoding, DW_FORM_data1, Encoding);
Jim Laskey69906002006-02-24 16:46:40 +00001650 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey69906002006-02-24 16:46:40 +00001651 // Create specific DIE.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001652 Slot = Ty = new DIE(DerivedTy->getTag());
Jim Laskey69906002006-02-24 16:46:40 +00001653
1654 // Map to main type, void will not have a type.
1655 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001656 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1657 NewType(Context, FromTy, Unit));
Jim Laskey69906002006-02-24 16:46:40 +00001658 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001659 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
Jim Laskey7089f452006-06-16 13:14:03 +00001660 // Fetch tag
1661 unsigned Tag = CompTy->getTag();
1662
Jim Laskeyf8913f12006-03-01 17:53:02 +00001663 // Create specific DIE.
Jim Laskey7089f452006-06-16 13:14:03 +00001664 Slot = Ty = Tag == DW_TAG_vector_type ? new DIE(DW_TAG_array_type) :
1665 new DIE(Tag);
1666
Jim Laskeyf8913f12006-03-01 17:53:02 +00001667 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1668
Jim Laskey7089f452006-06-16 13:14:03 +00001669 switch (Tag) {
1670 case DW_TAG_vector_type: Ty->AddUInt(DW_AT_GNU_vector, DW_FORM_flag, 1);
1671 // Fall thru
Jim Laskey9c4447a2006-03-01 20:39:36 +00001672 case DW_TAG_array_type: {
Jim Laskeyf8913f12006-03-01 17:53:02 +00001673 // Add element type.
1674 if (TypeDesc *FromTy = CompTy->getFromType()) {
Jim Laskey90c79d72006-03-23 23:02:34 +00001675 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1676 NewType(Context, FromTy, Unit));
Jim Laskeyf8913f12006-03-01 17:53:02 +00001677 }
Jim Laskeyf8a01a92006-06-15 20:51:43 +00001678
Jim Laskeyf8913f12006-03-01 17:53:02 +00001679 // Don't emit size attribute.
1680 Size = 0;
1681
1682 // Construct an anonymous type for index type.
1683 DIE *IndexTy = new DIE(DW_TAG_base_type);
1684 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1685 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1686 // Add to context.
Jim Laskey92ae7402006-03-01 18:20:30 +00001687 Context->AddChild(IndexTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001688
1689 // Add subranges to array type.
1690 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1691 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1692 int64_t Lo = SRD->getLo();
1693 int64_t Hi = SRD->getHi();
1694 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1695
1696 // If a range is available.
1697 if (Lo != Hi) {
1698 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1699 // Only add low if non-zero.
Jim Laskey6a3eb012006-03-01 23:52:37 +00001700 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1701 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
Jim Laskeyf8913f12006-03-01 17:53:02 +00001702 }
1703 Ty->AddChild(Subrange);
1704 }
1705
1706 break;
1707 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001708 case DW_TAG_structure_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +00001709 case DW_TAG_union_type: {
Jim Laskeyf01e5472006-03-03 15:06:57 +00001710 // Add elements to structure type.
1711 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskey760383e2006-08-21 21:20:18 +00001712 DebugInfoDesc *Element = Elements[i];
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001713
Jim Laskey760383e2006-08-21 21:20:18 +00001714 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)) {
1715 // Add field or base class.
Jim Laskey54689c22006-03-09 13:28:47 +00001716
Jim Laskey760383e2006-08-21 21:20:18 +00001717 unsigned Tag = MemberDesc->getTag();
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001718
Jim Laskey760383e2006-08-21 21:20:18 +00001719 // Extract the basic information.
1720 const std::string &Name = MemberDesc->getName();
1721 TypeDesc *MemTy = MemberDesc->getFromType();
1722 uint64_t Size = MemberDesc->getSize();
1723 uint64_t Align = MemberDesc->getAlign();
1724 uint64_t Offset = MemberDesc->getOffset();
1725
1726 // Construct member debug information entry.
1727 DIE *Member = new DIE(Tag);
1728
1729 // Add name if not "".
1730 if (!Name.empty())Member->AddString(DW_AT_name, DW_FORM_string, Name);
1731 // Add location if available.
1732 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1733
1734 // Most of the time the field info is the same as the members.
1735 uint64_t FieldSize = Size;
1736 uint64_t FieldAlign = Align;
1737 uint64_t FieldOffset = Offset;
1738
1739 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1740 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1741 NewType(Context, FromTy, Unit));
1742 FieldSize = FromTy->getSize();
1743 FieldAlign = FromTy->getSize();
1744 }
1745
1746 // Unless we have a bit field.
1747 if (Tag == DW_TAG_member && FieldSize != Size) {
1748 // Construct the alignment mask.
1749 uint64_t AlignMask = ~(FieldAlign - 1);
1750 // Determine the high bit + 1 of the declared size.
1751 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1752 // Work backwards to determine the base offset of the field.
1753 FieldOffset = HiMark - FieldSize;
1754 // Now normalize offset to the field.
1755 Offset -= FieldOffset;
1756
1757 // Maybe we need to work from the other end.
1758 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1759
1760 // Add size and offset.
1761 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1762 Member->AddUInt(DW_AT_bit_size, 0, Size);
1763 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
1764 }
1765
1766 // Add computation for offset.
1767 DIEBlock *Block = new DIEBlock();
1768 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
1769 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
1770 Block->ComputeSize(*this);
1771 Member->AddBlock(DW_AT_data_member_location, 0, Block);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001772
Jim Laskey760383e2006-08-21 21:20:18 +00001773 // Add accessibility (public default unless is base class.
1774 if (MemberDesc->isProtected()) {
1775 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_protected);
1776 } else if (MemberDesc->isPrivate()) {
1777 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_private);
1778 } else if (Tag == DW_TAG_inheritance) {
1779 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_public);
1780 }
1781
1782 Ty->AddChild(Member);
1783 } else if (GlobalVariableDesc *StaticDesc =
1784 dyn_cast<GlobalVariableDesc>(Element)) {
1785 // Add static member.
1786
1787 // Construct member debug information entry.
1788 DIE *Static = new DIE(DW_TAG_variable);
1789
1790 // Add name and mangled name.
1791 const std::string &Name = StaticDesc->getDisplayName();
1792 const std::string &MangledName = StaticDesc->getName();
1793 Static->AddString(DW_AT_name, DW_FORM_string, Name);
1794 Static->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1795 MangledName);
1796
1797 // Add location.
1798 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1799
1800 // Add type.
1801 if (TypeDesc *StaticTy = StaticDesc->getType()) {
1802 Static->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1803 NewType(Context, StaticTy, Unit));
1804 }
1805
1806 // Add flags.
1807 Static->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1808 Static->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1809
1810 Ty->AddChild(Static);
1811 } else if (SubprogramDesc *MethodDesc =
1812 dyn_cast<SubprogramDesc>(Element)) {
1813 // Add member function.
1814
1815 // Construct member debug information entry.
1816 DIE *Method = new DIE(DW_TAG_subprogram);
1817
1818 // Add name and mangled name.
1819 const std::string &Name = MethodDesc->getDisplayName();
1820 const std::string &MangledName = MethodDesc->getName();
1821 bool IsCTor = false;
1822
1823 if (Name.empty()) {
1824 Method->AddString(DW_AT_name, DW_FORM_string, MangledName);
1825 IsCTor = TyDesc->getName() == MangledName;
1826 } else {
1827 Method->AddString(DW_AT_name, DW_FORM_string, Name);
1828 Method->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1829 MangledName);
1830 }
1831
1832 // Add location.
1833 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1834
1835 // Add type.
1836 if (CompositeTypeDesc *MethodTy =
1837 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1838 // Get argument information.
1839 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1840
1841 // If not a ctor.
1842 if (!IsCTor) {
1843 // Add return type.
1844 Method->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1845 NewType(Context, dyn_cast<TypeDesc>(Args[0]),
1846 Unit));
1847 }
1848
1849 // Add arguments.
1850 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1851 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1852 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1853 NewType(Context, cast<TypeDesc>(Args[i]), Unit));
1854 Arg->AddUInt(DW_AT_artificial, DW_FORM_flag, 1);
1855 Method->AddChild(Arg);
1856 }
1857 }
1858
1859 // Add flags.
1860 Method->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1861 Method->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1862
1863 Ty->AddChild(Method);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001864 }
Jim Laskeyf01e5472006-03-03 15:06:57 +00001865 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001866 break;
1867 }
Jim Laskey9c4447a2006-03-01 20:39:36 +00001868 case DW_TAG_enumeration_type: {
Jim Laskey6a3eb012006-03-01 23:52:37 +00001869 // Add enumerators to enumeration type.
1870 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1871 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1872 const std::string &Name = ED->getName();
1873 int64_t Value = ED->getValue();
1874 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1875 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1876 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1877 Ty->AddChild(Enumerator);
1878 }
1879
Jim Laskeyf8913f12006-03-01 17:53:02 +00001880 break;
1881 }
Jim Laskey650f6092006-06-20 19:41:06 +00001882 case DW_TAG_subroutine_type: {
1883 // Add prototype flag.
1884 Ty->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
1885 // Add return type.
1886 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
Jim Laskeyd04c1592006-07-13 15:27:42 +00001887 NewType(Context, dyn_cast<TypeDesc>(Elements[0]), Unit));
Jim Laskey650f6092006-06-20 19:41:06 +00001888
1889 // Add arguments.
1890 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1891 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1892 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1893 NewType(Context, cast<TypeDesc>(Elements[i]), Unit));
1894 Ty->AddChild(Arg);
1895 }
1896
1897 break;
1898 }
Jim Laskeyf8913f12006-03-01 17:53:02 +00001899 default: break;
1900 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001901 }
Jim Laskey0c0feb92006-10-04 10:40:15 +00001902
Jim Laskey434b40b2006-02-23 22:37:30 +00001903 assert(Ty && "Type not supported yet");
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001904
Jim Laskey69906002006-02-24 16:46:40 +00001905 // Add size if non-zero (derived types don't have a size.)
Jim Laskey434b40b2006-02-23 22:37:30 +00001906 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
Jim Laskey69906002006-02-24 16:46:40 +00001907 // Add name if not anonymous or intermediate type.
Jim Laskey434b40b2006-02-23 22:37:30 +00001908 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001909 // Add source line info if available.
1910 AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
Jim Laskey434b40b2006-02-23 22:37:30 +00001911
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001912 // Add to context owner.
Jim Laskey92ae7402006-03-01 18:20:30 +00001913 Context->AddChild(Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001914
1915 return Slot;
1916}
1917
Jim Laskeyb8509c52006-03-23 18:07:55 +00001918/// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey063e7652006-01-17 17:31:53 +00001919///
Jim Laskey65195462006-10-30 13:35:07 +00001920CompileUnit *Dwarf::NewCompileUnit(CompileUnitDesc *UnitDesc,
Jim Laskeybd761842006-02-27 17:27:12 +00001921 unsigned ID) {
1922 // Construct debug information entry.
1923 DIE *Die = new DIE(DW_TAG_compile_unit);
Reid Spencer02b85112006-10-30 22:32:30 +00001924 Die->AddDelta (DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0),
1925 DWLabel("section_line", 0));
Jim Laskey89d67fa2006-06-23 12:51:53 +00001926// Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1927// Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
Jim Laskeybd761842006-02-27 17:27:12 +00001928 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1929 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1930 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1931 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1932
Jim Laskeyb8509c52006-03-23 18:07:55 +00001933 // Add debug information entry to descriptor map.
Jim Laskey90c79d72006-03-23 23:02:34 +00001934 DIE *&Slot = getDieMapSlotFor(UnitDesc);
1935 Slot = Die;
Jim Laskeybd761842006-02-27 17:27:12 +00001936
1937 // Construct compile unit.
1938 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1939
1940 // Add Unit to compile unit map.
1941 DescToUnitMap[UnitDesc] = Unit;
1942
1943 return Unit;
1944}
Jim Laskey0420f2a2006-02-22 19:02:11 +00001945
Jim Laskeybd761842006-02-27 17:27:12 +00001946/// FindCompileUnit - Get the compile unit for the given descriptor.
1947///
Jim Laskey65195462006-10-30 13:35:07 +00001948CompileUnit *Dwarf::FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeybd761842006-02-27 17:27:12 +00001949 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1950 assert(Unit && "Missing compile unit.");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001951 return Unit;
Jim Laskey063e7652006-01-17 17:31:53 +00001952}
1953
Jim Laskey0420f2a2006-02-22 19:02:11 +00001954/// NewGlobalVariable - Add a new global variable DIE.
1955///
Jim Laskey65195462006-10-30 13:35:07 +00001956DIE *Dwarf::NewGlobalVariable(GlobalVariableDesc *GVD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001957 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00001958 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1959 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey90c79d72006-03-23 23:02:34 +00001960
1961 // Check for pre-existence.
1962 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1963 if (Slot) return Slot;
1964
Jim Laskey0420f2a2006-02-22 19:02:11 +00001965 // Get the global variable itself.
1966 GlobalVariable *GV = GVD->getGlobalVariable();
Jim Laskey0420f2a2006-02-22 19:02:11 +00001967
Jim Laskeye85fb672006-09-18 14:47:26 +00001968 const std::string &Name = GVD->hasMangledName() ? GVD->getDisplayName()
1969 : GVD->getName();
1970 const std::string &MangledName = GVD->hasMangledName() ? GVD->getName()
1971 : "";
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001972 // Get the global's type.
Jim Laskey90c79d72006-03-23 23:02:34 +00001973 DIE *Type = NewType(Unit->getDie(), GVD->getType(), Unit);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001974
1975 // Create the globale variable DIE.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001976 DIE *VariableDie = new DIE(DW_TAG_variable);
Jim Laskeye85fb672006-09-18 14:47:26 +00001977 VariableDie->AddString(DW_AT_name, DW_FORM_string, Name);
1978 if (!MangledName.empty()) {
1979 VariableDie->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1980 MangledName);
1981 }
1982 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1983 VariableDie->AddUInt(DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001984
1985 // Add source line info if available.
1986 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Jim Laskeyba8a2ee2006-10-16 19:38:41 +00001987
1988 // Work up linkage name.
Jim Laskey99e41ee2006-10-17 17:17:24 +00001989 const std::string LinkageName = Asm->getGlobalLinkName(GV);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001990
Jim Laskeyb8509c52006-03-23 18:07:55 +00001991 // Add address.
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001992 DIEBlock *Block = new DIEBlock();
1993 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
Jim Laskeyba8a2ee2006-10-16 19:38:41 +00001994 Block->AddObjectLabel(DW_FORM_udata, LinkageName);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00001995 Block->ComputeSize(*this);
1996 VariableDie->AddBlock(DW_AT_location, 0, Block);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001997
1998 // Add to map.
1999 Slot = VariableDie;
2000
2001 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00002002 Unit->getDie()->AddChild(VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002003
2004 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00002005 // FIXME - need to check external flag.
2006 Unit->AddGlobal(Name, VariableDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002007
2008 return VariableDie;
2009}
2010
2011/// NewSubprogram - Add a new subprogram DIE.
2012///
Jim Laskey65195462006-10-30 13:35:07 +00002013DIE *Dwarf::NewSubprogram(SubprogramDesc *SPD) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002014 // Get the compile unit context.
Jim Laskeybd761842006-02-27 17:27:12 +00002015 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
2016 CompileUnit *Unit = FindCompileUnit(UnitDesc);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002017
Jim Laskey90c79d72006-03-23 23:02:34 +00002018 // Check for pre-existence.
2019 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
2020 if (Slot) return Slot;
2021
Jim Laskey0420f2a2006-02-22 19:02:11 +00002022 // Gather the details (simplify add attribute code.)
Jim Laskeye85fb672006-09-18 14:47:26 +00002023 const std::string &Name = SPD->hasMangledName() ? SPD->getDisplayName()
2024 : SPD->getName();
2025 const std::string &MangledName = SPD->hasMangledName() ? SPD->getName()
2026 : "";
Jim Laskey90c79d72006-03-23 23:02:34 +00002027 DIE *Type = NewType(Unit->getDie(), SPD->getType(), Unit);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00002028 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
Jim Laskey0420f2a2006-02-22 19:02:11 +00002029
Jim Laskey8a8e9752006-02-27 20:37:42 +00002030 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
Jim Laskeye85fb672006-09-18 14:47:26 +00002031 SubprogramDie->AddString(DW_AT_name, DW_FORM_string, Name);
2032 if (!MangledName.empty()) {
2033 SubprogramDie->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
2034 MangledName);
Jim Laskey41886992006-04-07 16:34:46 +00002035 }
Jim Laskeye85fb672006-09-18 14:47:26 +00002036 if (Type) {
2037 SubprogramDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
2038 }
2039 SubprogramDie->AddUInt(DW_AT_external, DW_FORM_flag, IsExternal);
2040 SubprogramDie->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002041
Jim Laskeyb8509c52006-03-23 18:07:55 +00002042 // Add source line info if available.
2043 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
2044
Jim Laskey0420f2a2006-02-22 19:02:11 +00002045 // Add to map.
2046 Slot = SubprogramDie;
2047
2048 // Add to context owner.
Jim Laskeybd761842006-02-27 17:27:12 +00002049 Unit->getDie()->AddChild(SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002050
2051 // Expose as global.
Jim Laskeybd761842006-02-27 17:27:12 +00002052 Unit->AddGlobal(Name, SubprogramDie);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002053
2054 return SubprogramDie;
2055}
2056
Jim Laskeyb8509c52006-03-23 18:07:55 +00002057/// NewScopeVariable - Create a new scope variable.
2058///
Jim Laskey65195462006-10-30 13:35:07 +00002059DIE *Dwarf::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002060 // Get the descriptor.
2061 VariableDesc *VD = DV->getDesc();
2062
2063 // Translate tag to proper Dwarf tag. The result variable is dropped for now.
2064 unsigned Tag;
2065 switch (VD->getTag()) {
2066 case DW_TAG_return_variable: return NULL;
2067 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2068 case DW_TAG_auto_variable: // fall thru
2069 default: Tag = DW_TAG_variable; break;
2070 }
2071
2072 // Define variable debug information entry.
2073 DIE *VariableDie = new DIE(Tag);
2074 VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
2075
2076 // Add source line info if available.
2077 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
2078
2079 // Add variable type.
Jim Laskey90c79d72006-03-23 23:02:34 +00002080 DIE *Type = NewType(Unit->getDie(), VD->getType(), Unit);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002081 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
2082
Jim Laskeyb3e7be22006-03-28 14:58:32 +00002083 // Add variable address.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002084 MachineLocation Location;
Jim Laskey41886992006-04-07 16:34:46 +00002085 RI->getLocation(*MF, DV->getFrameIndex(), Location);
Jim Laskeyb3e7be22006-03-28 14:58:32 +00002086 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002087
2088 return VariableDie;
2089}
2090
2091/// ConstructScope - Construct the components of a scope.
2092///
Jim Laskey65195462006-10-30 13:35:07 +00002093void Dwarf::ConstructScope(DebugScope *ParentScope,
Jim Laskeyb8509c52006-03-23 18:07:55 +00002094 DIE *ParentDie, CompileUnit *Unit) {
2095 // Add variables to scope.
2096 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
2097 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2098 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
2099 if (VariableDie) ParentDie->AddChild(VariableDie);
2100 }
2101
2102 // Add nested scopes.
2103 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
2104 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2105 // Define the Scope debug information entry.
2106 DebugScope *Scope = Scopes[j];
2107 // FIXME - Ignore inlined functions for the time being.
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002108 if (!Scope->getParent()) continue;
Jim Laskeyb8509c52006-03-23 18:07:55 +00002109
Jim Laskey66ebf092006-10-23 14:56:37 +00002110 unsigned StartID = Scope->getStartLabelID();
2111 unsigned EndID = Scope->getEndLabelID();
2112
2113 // Throw out scope if block is discarded.
2114 if (StartID && !DebugInfo->isLabelValid(StartID)) continue;
2115 if (EndID && !DebugInfo->isLabelValid(EndID)) continue;
2116
Jim Laskeyb8509c52006-03-23 18:07:55 +00002117 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2118
2119 // Add the scope bounds.
Jim Laskey66ebf092006-10-23 14:56:37 +00002120 if (StartID) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002121 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
2122 DWLabel("loc", StartID));
2123 } else {
2124 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
2125 DWLabel("func_begin", SubprogramCount));
2126 }
Jim Laskey66ebf092006-10-23 14:56:37 +00002127 if (EndID) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002128 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
2129 DWLabel("loc", EndID));
2130 } else {
2131 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
2132 DWLabel("func_end", SubprogramCount));
2133 }
2134
2135 // Add the scope contents.
2136 ConstructScope(Scope, ScopeDie, Unit);
2137 ParentDie->AddChild(ScopeDie);
2138 }
2139}
2140
2141/// ConstructRootScope - Construct the scope for the subprogram.
2142///
Jim Laskey65195462006-10-30 13:35:07 +00002143void Dwarf::ConstructRootScope(DebugScope *RootScope) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002144 // Exit if there is no root scope.
2145 if (!RootScope) return;
2146
2147 // Get the subprogram debug information entry.
2148 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Jim Laskey90c79d72006-03-23 23:02:34 +00002149
2150 // Get the compile unit context.
2151 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey41886992006-04-07 16:34:46 +00002152 CompileUnit *Unit = FindCompileUnit(UnitDesc);
2153
Jim Laskey90c79d72006-03-23 23:02:34 +00002154 // Get the subprogram die.
2155 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002156 assert(SPDie && "Missing subprogram descriptor");
2157
2158 // Add the function bounds.
Jim Laskey6b92b8e2006-04-07 20:44:42 +00002159 SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
2160 DWLabel("func_begin", SubprogramCount));
Jim Laskeyb8509c52006-03-23 18:07:55 +00002161 SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
2162 DWLabel("func_end", SubprogramCount));
Jim Laskey41886992006-04-07 16:34:46 +00002163 MachineLocation Location(RI->getFrameRegister(*MF));
Jim Laskeyb3e7be22006-03-28 14:58:32 +00002164 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002165
Jim Laskeyb8509c52006-03-23 18:07:55 +00002166 ConstructScope(RootScope, SPDie, Unit);
2167}
2168
Jim Laskey063e7652006-01-17 17:31:53 +00002169/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2170/// tools to recognize the object file contains Dwarf information.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002171///
Jim Laskey65195462006-10-30 13:35:07 +00002172void Dwarf::EmitInitial() {
Jim Laskey014f98c2006-06-14 11:35:03 +00002173 // Check to see if we already emitted intial headers.
2174 if (didInitial) return;
2175 didInitial = true;
2176
Jim Laskey063e7652006-01-17 17:31:53 +00002177 // Dwarf sections base addresses.
Reid Spencer02b85112006-10-30 22:32:30 +00002178 if (TAI->getDwarfRequiresFrameSection()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002179 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Reid Spencer02b85112006-10-30 22:32:30 +00002180 EmitLabel("section_frame", 0);
2181 }
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002182 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Jim Laskey0420f2a2006-02-22 19:02:11 +00002183 EmitLabel("section_info", 0);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002184 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Jim Laskey0420f2a2006-02-22 19:02:11 +00002185 EmitLabel("section_abbrev", 0);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002186 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Jim Laskey0420f2a2006-02-22 19:02:11 +00002187 EmitLabel("section_aranges", 0);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002188 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Jim Laskey0420f2a2006-02-22 19:02:11 +00002189 EmitLabel("section_macinfo", 0);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002190 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Jim Laskey0420f2a2006-02-22 19:02:11 +00002191 EmitLabel("section_line", 0);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002192 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Jim Laskey0420f2a2006-02-22 19:02:11 +00002193 EmitLabel("section_loc", 0);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002194 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Jim Laskey0420f2a2006-02-22 19:02:11 +00002195 EmitLabel("section_pubnames", 0);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002196 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Jim Laskey0420f2a2006-02-22 19:02:11 +00002197 EmitLabel("section_str", 0);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002198 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Jim Laskey0420f2a2006-02-22 19:02:11 +00002199 EmitLabel("section_ranges", 0);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002200 Asm->SwitchToTextSection(TAI->getTextSection());
Jim Laskey063e7652006-01-17 17:31:53 +00002201 EmitLabel("text_begin", 0);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002202 Asm->SwitchToDataSection(TAI->getDataSection());
Jim Laskey063e7652006-01-17 17:31:53 +00002203 EmitLabel("data_begin", 0);
Jim Laskey89d67fa2006-06-23 12:51:53 +00002204
Jim Laskey014f98c2006-06-14 11:35:03 +00002205 // Emit common frame information.
2206 EmitInitialDebugFrame();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002207}
2208
Jim Laskey063e7652006-01-17 17:31:53 +00002209/// EmitDIE - Recusively Emits a debug information entry.
2210///
Jim Laskey65195462006-10-30 13:35:07 +00002211void Dwarf::EmitDIE(DIE *Die) const {
Jim Laskey063e7652006-01-17 17:31:53 +00002212 // Get the abbreviation for this DIE.
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002213 unsigned AbbrevNumber = Die->getAbbrevNumber();
2214 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Jim Laskey0d086af2006-02-27 12:43:29 +00002215
2216 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002217
2218 // Emit the code (index) for the abbreviation.
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002219 EmitULEB128Bytes(AbbrevNumber);
Jim Laskey063e7652006-01-17 17:31:53 +00002220 EOL(std::string("Abbrev [" +
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002221 utostr(AbbrevNumber) +
Jim Laskey0d086af2006-02-27 12:43:29 +00002222 "] 0x" + utohexstr(Die->getOffset()) +
2223 ":0x" + utohexstr(Die->getSize()) + " " +
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002224 TagString(Abbrev->getTag())));
Jim Laskey063e7652006-01-17 17:31:53 +00002225
2226 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002227 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
Jim Laskey063e7652006-01-17 17:31:53 +00002228
2229 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00002230 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002231 unsigned Attr = AbbrevData[i].getAttribute();
2232 unsigned Form = AbbrevData[i].getForm();
Jim Laskey063e7652006-01-17 17:31:53 +00002233 assert(Form && "Too many attributes for DIE (check abbreviation)");
2234
2235 switch (Attr) {
2236 case DW_AT_sibling: {
Jim Laskeyda427fa2006-01-27 20:31:25 +00002237 EmitInt32(Die->SiblingOffset());
Jim Laskey063e7652006-01-17 17:31:53 +00002238 break;
2239 }
2240 default: {
2241 // Emit an attribute using the defined form.
2242 Values[i]->EmitValue(*this, Form);
2243 break;
2244 }
2245 }
2246
2247 EOL(AttributeString(Attr));
2248 }
2249
2250 // Emit the DIE children if any.
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002251 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
Jim Laskey063e7652006-01-17 17:31:53 +00002252 const std::vector<DIE *> &Children = Die->getChildren();
2253
Jim Laskey52060a02006-01-24 00:49:18 +00002254 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskey063e7652006-01-17 17:31:53 +00002255 EmitDIE(Children[j]);
2256 }
2257
Jim Laskeyda427fa2006-01-27 20:31:25 +00002258 EmitInt8(0); EOL("End Of Children Mark");
Jim Laskey063e7652006-01-17 17:31:53 +00002259 }
2260}
2261
2262/// SizeAndOffsetDie - Compute the size and offset of a DIE.
2263///
Jim Laskey65195462006-10-30 13:35:07 +00002264unsigned Dwarf::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002265 // Get the children.
2266 const std::vector<DIE *> &Children = Die->getChildren();
2267
2268 // If not last sibling and has children then add sibling offset attribute.
2269 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2270
Jim Laskey0420f2a2006-02-22 19:02:11 +00002271 // Record the abbreviation.
2272 Die->Complete(*this);
2273
Jim Laskey063e7652006-01-17 17:31:53 +00002274 // Get the abbreviation for this DIE.
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002275 unsigned AbbrevNumber = Die->getAbbrevNumber();
2276 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Jim Laskey063e7652006-01-17 17:31:53 +00002277
2278 // Set DIE offset
2279 Die->setOffset(Offset);
2280
2281 // Start the size with the size of abbreviation code.
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002282 Offset += SizeULEB128(AbbrevNumber);
Jim Laskey063e7652006-01-17 17:31:53 +00002283
2284 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002285 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
Jim Laskey063e7652006-01-17 17:31:53 +00002286
2287 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00002288 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00002289 // Size attribute value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002290 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
Jim Laskey063e7652006-01-17 17:31:53 +00002291 }
2292
2293 // Emit the DIE children if any.
Jim Laskeyb8509c52006-03-23 18:07:55 +00002294 if (!Children.empty()) {
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002295 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
Jim Laskeyb8509c52006-03-23 18:07:55 +00002296 "Children flag not set");
Jim Laskey063e7652006-01-17 17:31:53 +00002297
Jim Laskey52060a02006-01-24 00:49:18 +00002298 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002299 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
Jim Laskey063e7652006-01-17 17:31:53 +00002300 }
2301
2302 // End of children marker.
2303 Offset += sizeof(int8_t);
2304 }
2305
2306 Die->setSize(Offset - Die->getOffset());
2307 return Offset;
2308}
2309
2310/// SizeAndOffsets - Compute the size and offset of all the DIEs.
2311///
Jim Laskey65195462006-10-30 13:35:07 +00002312void Dwarf::SizeAndOffsets() {
Jim Laskey063e7652006-01-17 17:31:53 +00002313
2314 // Process each compile unit.
Jim Laskey52060a02006-01-24 00:49:18 +00002315 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00002316 CompileUnit *Unit = CompileUnits[i];
2317 if (Unit->hasContent()) {
2318 // Compute size of compile unit header
2319 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2320 sizeof(int16_t) + // DWARF version number
2321 sizeof(int32_t) + // Offset Into Abbrev. Section
2322 sizeof(int8_t); // Pointer Size (in bytes)
Jim Laskeyb8509c52006-03-23 18:07:55 +00002323 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
Jim Laskeybd761842006-02-27 17:27:12 +00002324 }
Jim Laskey063e7652006-01-17 17:31:53 +00002325 }
2326}
2327
Jim Laskey41886992006-04-07 16:34:46 +00002328/// EmitFrameMoves - Emit frame instructions to describe the layout of the
2329/// frame.
Jim Laskey65195462006-10-30 13:35:07 +00002330void Dwarf::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Jim Laskey41886992006-04-07 16:34:46 +00002331 std::vector<MachineMove *> &Moves) {
2332 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
2333 MachineMove *Move = Moves[i];
2334 unsigned LabelID = Move->getLabelID();
Jim Laskey66ebf092006-10-23 14:56:37 +00002335
2336 // Throw out move if the label is invalid.
2337 if (LabelID && !DebugInfo->isLabelValid(LabelID)) continue;
2338
Jim Laskey41886992006-04-07 16:34:46 +00002339 const MachineLocation &Dst = Move->getDestination();
2340 const MachineLocation &Src = Move->getSource();
2341
2342 // Advance row if new location.
2343 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
Jim Laskeyce50a162006-08-29 16:24:26 +00002344 EmitInt8(DW_CFA_advance_loc4);
Jim Laskey41886992006-04-07 16:34:46 +00002345 EOL("DW_CFA_advance_loc4");
2346 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
2347 EOL("");
2348
2349 BaseLabelID = LabelID;
2350 BaseLabel = "loc";
2351 }
2352
Jim Laskeyce50a162006-08-29 16:24:26 +00002353 int stackGrowth =
2354 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2355 TargetFrameInfo::StackGrowsUp ?
Jim Laskey563321a2006-09-06 18:34:40 +00002356 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskeyce50a162006-08-29 16:24:26 +00002357
Jim Laskey41886992006-04-07 16:34:46 +00002358 // If advancing cfa.
2359 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
2360 if (!Src.isRegister()) {
2361 if (Src.getRegister() == MachineLocation::VirtualFP) {
Jim Laskeyce50a162006-08-29 16:24:26 +00002362 EmitInt8(DW_CFA_def_cfa_offset);
Jim Laskey41886992006-04-07 16:34:46 +00002363 EOL("DW_CFA_def_cfa_offset");
2364 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00002365 EmitInt8(DW_CFA_def_cfa);
Jim Laskey41886992006-04-07 16:34:46 +00002366 EOL("DW_CFA_def_cfa");
2367
2368 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
2369 EOL("Register");
2370 }
Jim Laskey1069fbd2006-04-10 23:09:19 +00002371
Jim Laskeyce50a162006-08-29 16:24:26 +00002372 int Offset = Src.getOffset() / stackGrowth;
Jim Laskey1069fbd2006-04-10 23:09:19 +00002373
Jim Laskeyce50a162006-08-29 16:24:26 +00002374 EmitULEB128Bytes(Offset);
Jim Laskey41886992006-04-07 16:34:46 +00002375 EOL("Offset");
2376 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00002377 assert(0 && "Machine move no supported yet.");
Jim Laskey41886992006-04-07 16:34:46 +00002378 }
2379 } else {
Jim Laskeyce50a162006-08-29 16:24:26 +00002380 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
2381 int Offset = Dst.getOffset() / stackGrowth;
2382
2383 if (Offset < 0) {
2384 EmitInt8(DW_CFA_offset_extended_sf);
2385 EOL("DW_CFA_offset_extended_sf");
2386 EmitULEB128Bytes(Reg);
2387 EOL("Reg");
2388 EmitSLEB128Bytes(Offset);
2389 EOL("Offset");
2390 } else if (Reg < 64) {
2391 EmitInt8(DW_CFA_offset + Reg);
2392 EOL("DW_CFA_offset + Reg");
2393 EmitULEB128Bytes(Offset);
2394 EOL("Offset");
2395 } else {
2396 EmitInt8(DW_CFA_offset_extended);
2397 EOL("DW_CFA_offset_extended");
2398 EmitULEB128Bytes(Reg);
2399 EOL("Reg");
2400 EmitULEB128Bytes(Offset);
2401 EOL("Offset");
2402 }
Jim Laskey41886992006-04-07 16:34:46 +00002403 }
2404 }
2405}
2406
Jim Laskey063e7652006-01-17 17:31:53 +00002407/// EmitDebugInfo - Emit the debug info section.
2408///
Jim Laskey65195462006-10-30 13:35:07 +00002409void Dwarf::EmitDebugInfo() const {
Jim Laskey063e7652006-01-17 17:31:53 +00002410 // Start debug info section.
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002411 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Jim Laskey063e7652006-01-17 17:31:53 +00002412
Jim Laskeybd761842006-02-27 17:27:12 +00002413 // Process each compile unit.
2414 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2415 CompileUnit *Unit = CompileUnits[i];
2416
2417 if (Unit->hasContent()) {
2418 DIE *Die = Unit->getDie();
Jim Laskey0d086af2006-02-27 12:43:29 +00002419 // Emit the compile units header.
Jim Laskeybd761842006-02-27 17:27:12 +00002420 EmitLabel("info_begin", Unit->getID());
Jim Laskey0d086af2006-02-27 12:43:29 +00002421 // Emit size of content not including length itself
Jim Laskeybd761842006-02-27 17:27:12 +00002422 unsigned ContentSize = Die->getSize() +
Jim Laskey0d086af2006-02-27 12:43:29 +00002423 sizeof(int16_t) + // DWARF version number
2424 sizeof(int32_t) + // Offset Into Abbrev. Section
2425 sizeof(int8_t); // Pointer Size (in bytes)
2426
2427 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2428 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskeyf8a01a92006-06-15 20:51:43 +00002429 EmitDifference("abbrev_begin", 0, "section_abbrev", 0);
2430 EOL("Offset Into Abbrev. Section");
Jim Laskey563321a2006-09-06 18:34:40 +00002431 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
Jim Laskey0d086af2006-02-27 12:43:29 +00002432
Jim Laskeybd761842006-02-27 17:27:12 +00002433 EmitDIE(Die);
2434 EmitLabel("info_end", Unit->getID());
Jim Laskey063e7652006-01-17 17:31:53 +00002435 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002436
2437 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002438 }
2439}
2440
2441/// EmitAbbreviations - Emit the abbreviation section.
2442///
Jim Laskey65195462006-10-30 13:35:07 +00002443void Dwarf::EmitAbbreviations() const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002444 // Check to see if it is worth the effort.
2445 if (!Abbreviations.empty()) {
2446 // Start the debug abbrev section.
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002447 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Jim Laskey063e7652006-01-17 17:31:53 +00002448
Jim Laskeyd18e2892006-01-20 20:34:06 +00002449 EmitLabel("abbrev_begin", 0);
Jim Laskey063e7652006-01-17 17:31:53 +00002450
Jim Laskeyd18e2892006-01-20 20:34:06 +00002451 // For each abbrevation.
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002452 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002453 // Get abbreviation data
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002454 const DIEAbbrev *Abbrev = Abbreviations[i];
Jim Laskey063e7652006-01-17 17:31:53 +00002455
Jim Laskeyd18e2892006-01-20 20:34:06 +00002456 // Emit the abbrevations code (base 1 index.)
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002457 EmitULEB128Bytes(Abbrev->getNumber()); EOL("Abbreviation Code");
Jim Laskey063e7652006-01-17 17:31:53 +00002458
Jim Laskeyd18e2892006-01-20 20:34:06 +00002459 // Emit the abbreviations data.
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002460 Abbrev->Emit(*this);
Jim Laskey0d086af2006-02-27 12:43:29 +00002461
2462 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002463 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002464
2465 EmitLabel("abbrev_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002466
2467 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002468 }
2469}
2470
2471/// EmitDebugLines - Emit source line information.
2472///
Jim Laskey65195462006-10-30 13:35:07 +00002473void Dwarf::EmitDebugLines() const {
Jim Laskey063e7652006-01-17 17:31:53 +00002474 // Minimum line delta, thus ranging from -10..(255-10).
2475 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2476 // Maximum line delta, thus ranging from -10..(255-10).
2477 const int MaxLineDelta = 255 + MinLineDelta;
2478
2479 // Start the dwarf line section.
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002480 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Jim Laskey063e7652006-01-17 17:31:53 +00002481
2482 // Construct the section header.
2483
2484 EmitDifference("line_end", 0, "line_begin", 0);
2485 EOL("Length of Source Line Info");
2486 EmitLabel("line_begin", 0);
2487
Jim Laskeyda427fa2006-01-27 20:31:25 +00002488 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey063e7652006-01-17 17:31:53 +00002489
2490 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
2491 EOL("Prolog Length");
2492 EmitLabel("line_prolog_begin", 0);
2493
Jim Laskeyda427fa2006-01-27 20:31:25 +00002494 EmitInt8(1); EOL("Minimum Instruction Length");
Jim Laskey063e7652006-01-17 17:31:53 +00002495
Jim Laskeyda427fa2006-01-27 20:31:25 +00002496 EmitInt8(1); EOL("Default is_stmt_start flag");
Jim Laskey063e7652006-01-17 17:31:53 +00002497
Jim Laskeyda427fa2006-01-27 20:31:25 +00002498 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00002499
Jim Laskeyda427fa2006-01-27 20:31:25 +00002500 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00002501
Jim Laskeyda427fa2006-01-27 20:31:25 +00002502 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Jim Laskey063e7652006-01-17 17:31:53 +00002503
2504 // Line number standard opcode encodings argument count
Jim Laskeyda427fa2006-01-27 20:31:25 +00002505 EmitInt8(0); EOL("DW_LNS_copy arg count");
2506 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2507 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2508 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2509 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2510 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2511 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2512 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2513 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskey063e7652006-01-17 17:31:53 +00002514
2515 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2516 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
2517
2518 // Emit directories.
2519 for (unsigned DirectoryID = 1, NDID = Directories.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002520 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskey063e7652006-01-17 17:31:53 +00002521 EmitString(Directories[DirectoryID]); EOL("Directory");
2522 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00002523 EmitInt8(0); EOL("End of directories");
Jim Laskey063e7652006-01-17 17:31:53 +00002524
2525 // Emit files.
2526 for (unsigned SourceID = 1, NSID = SourceFiles.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002527 SourceID <= NSID; ++SourceID) {
Jim Laskey063e7652006-01-17 17:31:53 +00002528 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2529 EmitString(SourceFile.getName()); EOL("Source");
2530 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2531 EmitULEB128Bytes(0); EOL("Mod date");
2532 EmitULEB128Bytes(0); EOL("File size");
2533 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00002534 EmitInt8(0); EOL("End of files");
Jim Laskey063e7652006-01-17 17:31:53 +00002535
2536 EmitLabel("line_prolog_end", 0);
2537
Jim Laskey89d67fa2006-06-23 12:51:53 +00002538 // A sequence for each text section.
2539 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2540 // Isolate current sections line info.
Chris Lattner8466b212006-10-17 22:06:46 +00002541 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002542
2543 if (DwarfVerbose) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002544 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00002545 << TAI->getCommentString() << " "
Jim Laskey89d67fa2006-06-23 12:51:53 +00002546 << "Section "
2547 << SectionMap[j + 1].c_str() << "\n";
Jim Laskey0420f2a2006-02-22 19:02:11 +00002548 }
Jim Laskey063e7652006-01-17 17:31:53 +00002549
Jim Laskey89d67fa2006-06-23 12:51:53 +00002550 // Dwarf assumes we start with first line of first source file.
2551 unsigned Source = 1;
2552 unsigned Line = 1;
2553
2554 // Construct rows of the address, source, line, column matrix.
2555 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Chris Lattner8466b212006-10-17 22:06:46 +00002556 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey66ebf092006-10-23 14:56:37 +00002557 unsigned LabelID = LineInfo.getLabelID();
2558
Jim Laskeyfcc1d942006-10-24 11:50:43 +00002559 // Source line labels are validated at the MachineDebugInfo level.
Jim Laskey89d67fa2006-06-23 12:51:53 +00002560
2561 if (DwarfVerbose) {
Chris Lattner8466b212006-10-17 22:06:46 +00002562 unsigned SourceID = LineInfo.getSourceID();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002563 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2564 unsigned DirectoryID = SourceFile.getDirectoryID();
2565 O << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +00002566 << TAI->getCommentString() << " "
Jim Laskey89d67fa2006-06-23 12:51:53 +00002567 << Directories[DirectoryID]
2568 << SourceFile.getName() << ":"
Chris Lattner8466b212006-10-17 22:06:46 +00002569 << LineInfo.getLine() << "\n";
Jim Laskey89d67fa2006-06-23 12:51:53 +00002570 }
2571
2572 // Define the line address.
2573 EmitInt8(0); EOL("Extended Op");
2574 EmitInt8(4 + 1); EOL("Op size");
2575 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskey66ebf092006-10-23 14:56:37 +00002576 EmitReference("loc", LabelID); EOL("Location label");
Jim Laskey89d67fa2006-06-23 12:51:53 +00002577
2578 // If change of source, then switch to the new source.
Chris Lattner8466b212006-10-17 22:06:46 +00002579 if (Source != LineInfo.getSourceID()) {
2580 Source = LineInfo.getSourceID();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002581 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2582 EmitULEB128Bytes(Source); EOL("New Source");
2583 }
2584
2585 // If change of line.
Chris Lattner8466b212006-10-17 22:06:46 +00002586 if (Line != LineInfo.getLine()) {
Jim Laskey89d67fa2006-06-23 12:51:53 +00002587 // Determine offset.
Chris Lattner8466b212006-10-17 22:06:46 +00002588 int Offset = LineInfo.getLine() - Line;
Jim Laskey89d67fa2006-06-23 12:51:53 +00002589 int Delta = Offset - MinLineDelta;
2590
2591 // Update line.
Chris Lattner8466b212006-10-17 22:06:46 +00002592 Line = LineInfo.getLine();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002593
2594 // If delta is small enough and in range...
2595 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2596 // ... then use fast opcode.
2597 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2598 } else {
2599 // ... otherwise use long hand.
2600 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2601 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2602 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2603 }
2604 } else {
2605 // Copy the previous row (different address or source)
2606 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2607 }
2608 }
2609
2610 // Define last address of section.
Jim Laskeyda427fa2006-01-27 20:31:25 +00002611 EmitInt8(0); EOL("Extended Op");
2612 EmitInt8(4 + 1); EOL("Op size");
2613 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskey89d67fa2006-06-23 12:51:53 +00002614 EmitReference("section_end", j + 1); EOL("Section end label");
2615
2616 // Mark end of matrix.
2617 EmitInt8(0); EOL("DW_LNE_end_sequence");
2618 EmitULEB128Bytes(1); O << "\n";
2619 EmitInt8(1); O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002620 }
Jim Laskey063e7652006-01-17 17:31:53 +00002621
2622 EmitLabel("line_end", 0);
Jim Laskey0d086af2006-02-27 12:43:29 +00002623
2624 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00002625}
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002626
Jim Laskey41886992006-04-07 16:34:46 +00002627/// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002628///
Jim Laskey65195462006-10-30 13:35:07 +00002629void Dwarf::EmitInitialDebugFrame() {
Reid Spencer0de0c5d2006-10-30 23:34:32 +00002630 if (!TAI->getDwarfRequiresFrameSection())
Reid Spencer02b85112006-10-30 22:32:30 +00002631 return;
2632
Jim Laskey1069fbd2006-04-10 23:09:19 +00002633 int stackGrowth =
2634 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2635 TargetFrameInfo::StackGrowsUp ?
Jim Laskey563321a2006-09-06 18:34:40 +00002636 TAI->getAddressSize() : -TAI->getAddressSize();
Jim Laskey1069fbd2006-04-10 23:09:19 +00002637
Jim Laskey41886992006-04-07 16:34:46 +00002638 // Start the dwarf frame section.
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002639 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Jim Laskeyb8509c52006-03-23 18:07:55 +00002640
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002641 EmitLabel("frame_common", 0);
Jim Laskeyb8509c52006-03-23 18:07:55 +00002642 EmitDifference("frame_common_end", 0,
2643 "frame_common_begin", 0);
2644 EOL("Length of Common Information Entry");
2645
2646 EmitLabel("frame_common_begin", 0);
2647 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2648 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2649 EmitString(""); EOL("CIE Augmentation");
2650 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
Jim Laskey1069fbd2006-04-10 23:09:19 +00002651 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
Jim Laskey41886992006-04-07 16:34:46 +00002652 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
2653
2654 std::vector<MachineMove *> Moves;
2655 RI->getInitialFrameState(Moves);
2656 EmitFrameMoves(NULL, 0, Moves);
2657 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2658
Jim Laskeyb8509c52006-03-23 18:07:55 +00002659 EmitAlign(2);
2660 EmitLabel("frame_common_end", 0);
2661
2662 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002663}
2664
Jim Laskey41886992006-04-07 16:34:46 +00002665/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2666/// section.
Jim Laskey65195462006-10-30 13:35:07 +00002667void Dwarf::EmitFunctionDebugFrame() {
Reid Spencer0de0c5d2006-10-30 23:34:32 +00002668 if (!TAI->getDwarfRequiresFrameSection())
Reid Spencer02b85112006-10-30 22:32:30 +00002669 return;
2670
Jim Laskey41886992006-04-07 16:34:46 +00002671 // Start the dwarf frame section.
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002672 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Jim Laskey41886992006-04-07 16:34:46 +00002673
2674 EmitDifference("frame_end", SubprogramCount,
2675 "frame_begin", SubprogramCount);
2676 EOL("Length of Frame Information Entry");
2677
2678 EmitLabel("frame_begin", SubprogramCount);
2679
Jim Laskeyd16f2a72006-06-19 19:49:42 +00002680 EmitDifference("frame_common", 0, "section_frame", 0);
2681 EOL("FDE CIE offset");
Jim Laskey41886992006-04-07 16:34:46 +00002682
2683 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2684 EmitDifference("func_end", SubprogramCount,
2685 "func_begin", SubprogramCount);
2686 EOL("FDE address range");
2687
2688 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2689
2690 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2691
2692 EmitAlign(2);
2693 EmitLabel("frame_end", SubprogramCount);
2694
2695 O << "\n";
2696}
2697
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002698/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2699///
Jim Laskey65195462006-10-30 13:35:07 +00002700void Dwarf::EmitDebugPubNames() {
Jim Laskeybd761842006-02-27 17:27:12 +00002701 // Start the dwarf pubnames section.
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002702 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Jim Laskeyd18e2892006-01-20 20:34:06 +00002703
Jim Laskeybd761842006-02-27 17:27:12 +00002704 // Process each compile unit.
2705 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2706 CompileUnit *Unit = CompileUnits[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00002707
Jim Laskeybd761842006-02-27 17:27:12 +00002708 if (Unit->hasContent()) {
2709 EmitDifference("pubnames_end", Unit->getID(),
2710 "pubnames_begin", Unit->getID());
2711 EOL("Length of Public Names Info");
2712
2713 EmitLabel("pubnames_begin", Unit->getID());
2714
2715 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2716
Jim Laskey067ef412006-06-19 15:48:00 +00002717 EmitDifference("info_begin", Unit->getID(), "section_info", 0);
Jim Laskeybd761842006-02-27 17:27:12 +00002718 EOL("Offset of Compilation Unit Info");
Jim Laskeyd18e2892006-01-20 20:34:06 +00002719
Jim Laskeybd761842006-02-27 17:27:12 +00002720 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2721 EOL("Compilation Unit Length");
2722
2723 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2724
2725 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2726 GE = Globals.end();
2727 GI != GE; ++GI) {
2728 const std::string &Name = GI->first;
2729 DIE * Entity = GI->second;
2730
2731 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2732 EmitString(Name); EOL("External Name");
2733 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002734
Jim Laskeybd761842006-02-27 17:27:12 +00002735 EmitInt32(0); EOL("End Mark");
2736 EmitLabel("pubnames_end", Unit->getID());
2737
2738 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002739 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00002740 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002741}
2742
2743/// EmitDebugStr - Emit visible names into a debug str section.
2744///
Jim Laskey65195462006-10-30 13:35:07 +00002745void Dwarf::EmitDebugStr() {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002746 // Check to see if it is worth the effort.
2747 if (!StringPool.empty()) {
2748 // Start the dwarf str section.
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002749 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Jim Laskeyd18e2892006-01-20 20:34:06 +00002750
Jim Laskeyb8509c52006-03-23 18:07:55 +00002751 // For each of strings in the string pool.
Jim Laskeyd18e2892006-01-20 20:34:06 +00002752 for (unsigned StringID = 1, N = StringPool.size();
Jim Laskey52060a02006-01-24 00:49:18 +00002753 StringID <= N; ++StringID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00002754 // Emit a label for reference from debug information entries.
2755 EmitLabel("string", StringID);
2756 // Emit the string itself.
2757 const std::string &String = StringPool[StringID];
2758 EmitString(String); O << "\n";
2759 }
Jim Laskey0d086af2006-02-27 12:43:29 +00002760
2761 O << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00002762 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002763}
2764
2765/// EmitDebugLoc - Emit visible names into a debug loc section.
2766///
Jim Laskey65195462006-10-30 13:35:07 +00002767void Dwarf::EmitDebugLoc() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002768 // Start the dwarf loc section.
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002769 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Jim Laskey0d086af2006-02-27 12:43:29 +00002770
2771 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002772}
2773
2774/// EmitDebugARanges - Emit visible names into a debug aranges section.
2775///
Jim Laskey65195462006-10-30 13:35:07 +00002776void Dwarf::EmitDebugARanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002777 // Start the dwarf aranges section.
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002778 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Jim Laskeye719a7c2006-01-18 16:54:26 +00002779
2780 // FIXME - Mock up
Jim Laskeybd761842006-02-27 17:27:12 +00002781#if 0
2782 // Process each compile unit.
2783 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2784 CompileUnit *Unit = CompileUnits[i];
2785
2786 if (Unit->hasContent()) {
2787 // Don't include size of length
2788 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2789
2790 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2791
2792 EmitReference("info_begin", Unit->getID());
2793 EOL("Offset of Compilation Unit Info");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002794
Jim Laskey563321a2006-09-06 18:34:40 +00002795 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002796
Jim Laskeybd761842006-02-27 17:27:12 +00002797 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002798
Jim Laskeybd761842006-02-27 17:27:12 +00002799 EmitInt16(0); EOL("Pad (1)");
2800 EmitInt16(0); EOL("Pad (2)");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002801
Jim Laskeybd761842006-02-27 17:27:12 +00002802 // Range 1
2803 EmitReference("text_begin", 0); EOL("Address");
2804 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
Jim Laskeye719a7c2006-01-18 16:54:26 +00002805
Jim Laskeybd761842006-02-27 17:27:12 +00002806 EmitInt32(0); EOL("EOM (1)");
2807 EmitInt32(0); EOL("EOM (2)");
2808
2809 O << "\n";
2810 }
2811 }
2812#endif
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002813}
2814
2815/// EmitDebugRanges - Emit visible names into a debug ranges section.
2816///
Jim Laskey65195462006-10-30 13:35:07 +00002817void Dwarf::EmitDebugRanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002818 // Start the dwarf ranges section.
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002819 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Jim Laskey0d086af2006-02-27 12:43:29 +00002820
2821 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002822}
2823
2824/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2825///
Jim Laskey65195462006-10-30 13:35:07 +00002826void Dwarf::EmitDebugMacInfo() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00002827 // Start the dwarf macinfo section.
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002828 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Jim Laskey0d086af2006-02-27 12:43:29 +00002829
2830 O << "\n";
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002831}
Jim Laskey063e7652006-01-17 17:31:53 +00002832
Jim Laskey52060a02006-01-24 00:49:18 +00002833/// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2834/// header file.
Jim Laskey65195462006-10-30 13:35:07 +00002835void Dwarf::ConstructCompileUnitDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002836 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
Jim Laskey6e87c0e2006-01-26 21:22:49 +00002837
2838 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskeybd761842006-02-27 17:27:12 +00002839 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
Jim Laskey52060a02006-01-24 00:49:18 +00002840 CompileUnits.push_back(Unit);
2841 }
2842}
2843
2844/// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2845/// variables.
Jim Laskey65195462006-10-30 13:35:07 +00002846void Dwarf::ConstructGlobalDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002847 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002848 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00002849
2850 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00002851 GlobalVariableDesc *GVD = GlobalVariables[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00002852 NewGlobalVariable(GVD);
Jim Laskey52060a02006-01-24 00:49:18 +00002853 }
2854}
2855
Jim Laskey0420f2a2006-02-22 19:02:11 +00002856/// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2857/// subprograms.
Jim Laskey65195462006-10-30 13:35:07 +00002858void Dwarf::ConstructSubprogramDIEs() {
Jim Laskey0420f2a2006-02-22 19:02:11 +00002859 std::vector<SubprogramDesc *> Subprograms =
Jim Laskeyb8509c52006-03-23 18:07:55 +00002860 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
Jim Laskey0420f2a2006-02-22 19:02:11 +00002861
2862 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2863 SubprogramDesc *SPD = Subprograms[i];
2864 NewSubprogram(SPD);
2865 }
2866}
Jim Laskey52060a02006-01-24 00:49:18 +00002867
Jim Laskey063e7652006-01-17 17:31:53 +00002868//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002869/// Dwarf implemenation.
Jim Laskey063e7652006-01-17 17:31:53 +00002870//
Jim Laskey52060a02006-01-24 00:49:18 +00002871
Jim Laskey65195462006-10-30 13:35:07 +00002872Dwarf::Dwarf(std::ostream &OS, AsmPrinter *A,
Jim Laskeya0f3d172006-09-07 22:06:40 +00002873 const TargetAsmInfo *T)
Jim Laskey52060a02006-01-24 00:49:18 +00002874: O(OS)
2875, Asm(A)
Jim Laskey563321a2006-09-06 18:34:40 +00002876, TAI(T)
Jim Laskey41886992006-04-07 16:34:46 +00002877, TD(Asm->TM.getTargetData())
2878, RI(Asm->TM.getRegisterInfo())
Jim Laskeyb8509c52006-03-23 18:07:55 +00002879, M(NULL)
2880, MF(NULL)
Jim Laskey52060a02006-01-24 00:49:18 +00002881, DebugInfo(NULL)
2882, didInitial(false)
Jim Laskey014f98c2006-06-14 11:35:03 +00002883, shouldEmit(false)
Jim Laskeyb8509c52006-03-23 18:07:55 +00002884, SubprogramCount(0)
Jim Laskey52060a02006-01-24 00:49:18 +00002885, CompileUnits()
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002886, AbbreviationsSet()
Jim Laskey52060a02006-01-24 00:49:18 +00002887, Abbreviations()
Jim Laskey52060a02006-01-24 00:49:18 +00002888, StringPool()
Jim Laskeybd761842006-02-27 17:27:12 +00002889, DescToUnitMap()
Jim Laskey0420f2a2006-02-22 19:02:11 +00002890, DescToDieMap()
Jim Laskey89d67fa2006-06-23 12:51:53 +00002891, SectionMap()
2892, SectionSourceLines()
Jim Laskeya9c83fe2006-10-30 15:59:54 +00002893{
2894}
Jim Laskey65195462006-10-30 13:35:07 +00002895Dwarf::~Dwarf() {
Jim Laskey52060a02006-01-24 00:49:18 +00002896 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2897 delete CompileUnits[i];
Jim Laskey063e7652006-01-17 17:31:53 +00002898 }
Jim Laskey52060a02006-01-24 00:49:18 +00002899}
Jim Laskey063e7652006-01-17 17:31:53 +00002900
Jim Laskey41886992006-04-07 16:34:46 +00002901/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2902/// created it. Set by the target AsmPrinter.
Jim Laskey65195462006-10-30 13:35:07 +00002903void Dwarf::SetDebugInfo(MachineDebugInfo *DI) {
Jim Laskey014f98c2006-06-14 11:35:03 +00002904 // Make sure initial declarations are made.
2905 if (!DebugInfo && DI->hasInfo()) {
2906 DebugInfo = DI;
2907 shouldEmit = true;
2908
2909 // Emit initial sections
2910 EmitInitial();
2911
2912 // Create all the compile unit DIEs.
2913 ConstructCompileUnitDIEs();
2914
2915 // Create DIEs for each of the externally visible global variables.
2916 ConstructGlobalDIEs();
2917
2918 // Create DIEs for each of the externally visible subprograms.
2919 ConstructSubprogramDIEs();
Jim Laskey89d67fa2006-06-23 12:51:53 +00002920
2921 // Prime section data.
Jim Laskey563321a2006-09-06 18:34:40 +00002922 SectionMap.insert(std::string("\t") + TAI->getTextSection());
Jim Laskey014f98c2006-06-14 11:35:03 +00002923 }
Jim Laskey41886992006-04-07 16:34:46 +00002924}
2925
Jim Laskey063e7652006-01-17 17:31:53 +00002926/// BeginModule - Emit all Dwarf sections that should come prior to the content.
Jim Laskeyb2efb852006-01-04 22:28:25 +00002927///
Jim Laskey65195462006-10-30 13:35:07 +00002928void Dwarf::BeginModule(Module *M) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002929 this->M = M;
2930
Jim Laskeyb2efb852006-01-04 22:28:25 +00002931 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002932 EOL("Dwarf Begin Module");
Jim Laskeyb2efb852006-01-04 22:28:25 +00002933}
2934
Jim Laskey063e7652006-01-17 17:31:53 +00002935/// EndModule - Emit all Dwarf sections that should come after the content.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002936///
Jim Laskey65195462006-10-30 13:35:07 +00002937void Dwarf::EndModule() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002938 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002939 EOL("Dwarf End Module");
2940
2941 // Standard sections final addresses.
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002942 Asm->SwitchToTextSection(TAI->getTextSection());
Jim Laskey063e7652006-01-17 17:31:53 +00002943 EmitLabel("text_end", 0);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002944 Asm->SwitchToDataSection(TAI->getDataSection());
Jim Laskey063e7652006-01-17 17:31:53 +00002945 EmitLabel("data_end", 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00002946
Jim Laskey89d67fa2006-06-23 12:51:53 +00002947 // End text sections.
2948 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00002949 Asm->SwitchToTextSection(SectionMap[i].c_str());
Jim Laskey89d67fa2006-06-23 12:51:53 +00002950 EmitLabel("section_end", i);
2951 }
2952
Jim Laskey063e7652006-01-17 17:31:53 +00002953 // Compute DIE offsets and sizes.
2954 SizeAndOffsets();
2955
2956 // Emit all the DIEs into a debug info section
2957 EmitDebugInfo();
2958
2959 // Corresponding abbreviations into a abbrev section.
2960 EmitAbbreviations();
2961
2962 // Emit source line correspondence into a debug line section.
2963 EmitDebugLines();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002964
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002965 // Emit info into a debug pubnames section.
2966 EmitDebugPubNames();
2967
Jim Laskey19ef4ef2006-01-17 20:41:40 +00002968 // Emit info into a debug str section.
2969 EmitDebugStr();
2970
2971 // Emit info into a debug loc section.
2972 EmitDebugLoc();
2973
2974 // Emit info into a debug aranges section.
2975 EmitDebugARanges();
2976
2977 // Emit info into a debug ranges section.
2978 EmitDebugRanges();
2979
2980 // Emit info into a debug macinfo section.
2981 EmitDebugMacInfo();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002982}
2983
Jim Laskey6b92b8e2006-04-07 20:44:42 +00002984/// BeginFunction - Gather pre-function debug information. Assumes being
2985/// emitted immediately after the function entry point.
Jim Laskey65195462006-10-30 13:35:07 +00002986void Dwarf::BeginFunction(MachineFunction *MF) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00002987 this->MF = MF;
2988
Jim Laskey89d67fa2006-06-23 12:51:53 +00002989 if (!ShouldEmitDwarf()) return;
2990 EOL("Dwarf Begin Function");
2991
2992 // Begin accumulating function debug information.
2993 DebugInfo->BeginFunction(MF);
Jim Laskey41886992006-04-07 16:34:46 +00002994
Jim Laskey89d67fa2006-06-23 12:51:53 +00002995 // Assumes in correct section after the entry point.
2996 EmitLabel("func_begin", ++SubprogramCount);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002997}
2998
Jim Laskeye719a7c2006-01-18 16:54:26 +00002999/// EndFunction - Gather and emit post-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003000///
Jim Laskey65195462006-10-30 13:35:07 +00003001void Dwarf::EndFunction() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00003002 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00003003 EOL("Dwarf End Function");
Jim Laskeyb8509c52006-03-23 18:07:55 +00003004
Jim Laskey89d67fa2006-06-23 12:51:53 +00003005 // Define end label for subprogram.
3006 EmitLabel("func_end", SubprogramCount);
Jim Laskey014f98c2006-06-14 11:35:03 +00003007
Jim Laskey89d67fa2006-06-23 12:51:53 +00003008 // Get function line info.
Chris Lattner8466b212006-10-17 22:06:46 +00003009 const std::vector<SourceLineInfo> &LineInfos = DebugInfo->getSourceLines();
Jim Laskey89d67fa2006-06-23 12:51:53 +00003010
3011 if (!LineInfos.empty()) {
3012 // Get section line info.
3013 unsigned ID = SectionMap.insert(Asm->CurrentSection);
3014 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Chris Lattner8466b212006-10-17 22:06:46 +00003015 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskey89d67fa2006-06-23 12:51:53 +00003016 // Append the function info to section info.
3017 SectionLineInfos.insert(SectionLineInfos.end(),
3018 LineInfos.begin(), LineInfos.end());
Jim Laskey014f98c2006-06-14 11:35:03 +00003019 }
Jim Laskey41886992006-04-07 16:34:46 +00003020
Jim Laskey89d67fa2006-06-23 12:51:53 +00003021 // Construct scopes for subprogram.
3022 ConstructRootScope(DebugInfo->getRootScope());
3023
3024 // Emit function frame information.
3025 EmitFunctionDebugFrame();
3026
3027 // Reset the line numbers for the next function.
Chris Lattner8466b212006-10-17 22:06:46 +00003028 DebugInfo->ClearLineInfo();
Jim Laskey89d67fa2006-06-23 12:51:53 +00003029
Jim Laskey41886992006-04-07 16:34:46 +00003030 // Clear function debug information.
3031 DebugInfo->EndFunction();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003032}
Jim Laskey65195462006-10-30 13:35:07 +00003033
3034
3035//===----------------------------------------------------------------------===//
3036/// DwarfWriter Implementation
3037
3038DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3039 const TargetAsmInfo *T) {
3040 DW = new Dwarf(OS, A, T);
3041}
3042
3043DwarfWriter::~DwarfWriter() {
3044 delete DW;
3045}
3046
3047/// SetDebugInfo - Set DebugInfo when it's known that pass manager has
3048/// created it. Set by the target AsmPrinter.
3049void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
3050 DW->SetDebugInfo(DI);
3051}
3052
3053/// BeginModule - Emit all Dwarf sections that should come prior to the
3054/// content.
3055void DwarfWriter::BeginModule(Module *M) {
3056 DW->BeginModule(M);
3057}
3058
3059/// EndModule - Emit all Dwarf sections that should come after the content.
3060///
3061void DwarfWriter::EndModule() {
3062 DW->EndModule();
3063}
3064
3065/// BeginFunction - Gather pre-function debug information. Assumes being
3066/// emitted immediately after the function entry point.
3067void DwarfWriter::BeginFunction(MachineFunction *MF) {
3068 DW->BeginFunction(MF);
3069}
3070
3071/// EndFunction - Gather and emit post-function debug information.
3072///
3073void DwarfWriter::EndFunction() {
3074 DW->EndFunction();
3075}