blob: 68ec6c49ac35efde57552af2310b51e7efb018b6 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskeye5032892005-12-21 19:48:16 +00007//
8//===----------------------------------------------------------------------===//
9//
Jim Laskey072200c2007-01-29 18:51:14 +000010// This file contains support for writing dwarf info into asm files.
Jim Laskeye5032892005-12-21 19:48:16 +000011//
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
Duncan Sands53c3a332007-05-16 12:12:23 +000016#include "llvm/ADT/DenseMap.h"
Jim Laskeya9c83fe2006-10-30 15:59:54 +000017#include "llvm/ADT/FoldingSet.h"
Jim Laskey063e7652006-01-17 17:31:53 +000018#include "llvm/ADT/StringExtras.h"
Jim Laskey65195462006-10-30 13:35:07 +000019#include "llvm/ADT/UniqueVector.h"
Jim Laskey52060a02006-01-24 00:49:18 +000020#include "llvm/Module.h"
21#include "llvm/Type.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000022#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendling305635a2008-06-27 00:09:40 +000023#include "llvm/CodeGen/MachineDebugInfoDesc.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000026#include "llvm/CodeGen/MachineLocation.h"
Jim Laskey3f09fc22007-02-28 18:38:31 +000027#include "llvm/Support/Debug.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000028#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000029#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000030#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000031#include "llvm/Support/Mangler.h"
Dan Gohman85496362007-09-24 21:32:18 +000032#include "llvm/System/Path.h"
Jim Laskey563321a2006-09-06 18:34:40 +000033#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000034#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000035#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000036#include "llvm/Target/TargetFrameInfo.h"
Duncan Sands53c3a332007-05-16 12:12:23 +000037#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000038#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000039#include "llvm/Target/TargetOptions.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000040#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000041#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000042using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000043using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000044
Jim Laskey0d086af2006-02-27 12:43:29 +000045namespace llvm {
Jim Laskey65195462006-10-30 13:35:07 +000046
47//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000048
49/// Configuration values for initial hash set sizes (log2).
50///
51static const unsigned InitDiesSetSize = 9; // 512
52static const unsigned InitAbbreviationsSetSize = 9; // 512
53static const unsigned InitValuesSetSize = 9; // 512
54
55//===----------------------------------------------------------------------===//
56/// Forward declarations.
57///
58class DIE;
59class DIEValue;
60
61//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000062/// DWLabel - Labels are used to track locations in the assembler file.
Reid Spencer181b6c92007-08-05 20:06:04 +000063/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
64/// where the tag is a category of label (Ex. location) and number is a value
65/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +000066class DWLabel {
67public:
Jim Laskeyef42a012006-11-02 20:12:39 +000068 /// Tag - Label category tag. Should always be a staticly declared C string.
69 ///
70 const char *Tag;
71
72 /// Number - Value to make label unique.
73 ///
74 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +000075
76 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Jim Laskeybd761842006-02-27 17:27:12 +000077
Jim Laskeyef42a012006-11-02 20:12:39 +000078 void Profile(FoldingSetNodeID &ID) const {
79 ID.AddString(std::string(Tag));
80 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +000081 }
Jim Laskeyef42a012006-11-02 20:12:39 +000082
83#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +000084 void print(std::ostream *O) const {
85 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +000086 }
Jim Laskeyef42a012006-11-02 20:12:39 +000087 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +000088 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +000089 if (Number) O << Number;
90 }
91#endif
Jim Laskeybd761842006-02-27 17:27:12 +000092};
93
94//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000095/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
96/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +000097class DIEAbbrevData {
98private:
Jim Laskeyef42a012006-11-02 20:12:39 +000099 /// Attribute - Dwarf attribute code.
100 ///
101 unsigned Attribute;
102
103 /// Form - Dwarf form code.
104 ///
105 unsigned Form;
Jim Laskey0d086af2006-02-27 12:43:29 +0000106
107public:
108 DIEAbbrevData(unsigned A, unsigned F)
109 : Attribute(A)
110 , Form(F)
111 {}
112
Jim Laskeybd761842006-02-27 17:27:12 +0000113 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000114 unsigned getAttribute() const { return Attribute; }
115 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000116
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000117 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000118 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000119 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000120 ID.AddInteger(Attribute);
121 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000122 }
123};
Jim Laskey063e7652006-01-17 17:31:53 +0000124
Jim Laskey0d086af2006-02-27 12:43:29 +0000125//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000126/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
127/// information object.
128class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000129private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000130 /// Tag - Dwarf tag code.
131 ///
132 unsigned Tag;
133
134 /// Unique number for node.
135 ///
136 unsigned Number;
137
138 /// ChildrenFlag - Dwarf children flag.
139 ///
140 unsigned ChildrenFlag;
141
142 /// Data - Raw data bytes for abbreviation.
143 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000144 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000145
Jim Laskey0d086af2006-02-27 12:43:29 +0000146public:
Jim Laskey063e7652006-01-17 17:31:53 +0000147
Jim Laskey0d086af2006-02-27 12:43:29 +0000148 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000149 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000150 , ChildrenFlag(C)
151 , Data()
152 {}
153 ~DIEAbbrev() {}
154
Jim Laskeybd761842006-02-27 17:27:12 +0000155 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000156 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000157 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000158 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000159 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000160 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000161 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000162 void setNumber(unsigned N) { Number = N; }
163
Jim Laskey0d086af2006-02-27 12:43:29 +0000164 /// AddAttribute - Adds another set of attribute information to the
165 /// abbreviation.
166 void AddAttribute(unsigned Attribute, unsigned Form) {
167 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000168 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000169
Jim Laskeyb8509c52006-03-23 18:07:55 +0000170 /// AddFirstAttribute - Adds a set of attribute information to the front
171 /// of the abbreviation.
172 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
173 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
174 }
175
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000176 /// Profile - Used to gather unique data for the abbreviation folding set.
177 ///
178 void Profile(FoldingSetNodeID &ID) {
179 ID.AddInteger(Tag);
180 ID.AddInteger(ChildrenFlag);
181
182 // For each attribute description.
183 for (unsigned i = 0, N = Data.size(); i < N; ++i)
184 Data[i].Profile(ID);
185 }
186
Jim Laskey0d086af2006-02-27 12:43:29 +0000187 /// Emit - Print the abbreviation using the specified Dwarf writer.
188 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000189 void Emit(const DwarfDebug &DD) const;
Jim Laskey0d086af2006-02-27 12:43:29 +0000190
191#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000192 void print(std::ostream *O) {
193 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000194 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000195 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 Laskeyef42a012006-11-02 20:12:39 +0000201/// DIE - A structured debug information entry. Has an abbreviation which
202/// describes it's organization.
203class DIE : public FoldingSetNode {
204protected:
205 /// Abbrev - Buffer for constructing abbreviation.
206 ///
207 DIEAbbrev Abbrev;
208
209 /// Offset - Offset in debug info section.
210 ///
211 unsigned Offset;
212
213 /// Size - Size of instance + children.
214 ///
215 unsigned Size;
216
217 /// Children DIEs.
218 ///
219 std::vector<DIE *> Children;
220
221 /// Attributes values.
222 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000223 SmallVector<DIEValue*, 32> Values;
Jim Laskeyef42a012006-11-02 20:12:39 +0000224
225public:
Dan Gohman81975f62007-08-27 14:50:10 +0000226 explicit DIE(unsigned Tag)
Jim Laskeyef42a012006-11-02 20:12:39 +0000227 : Abbrev(Tag, DW_CHILDREN_no)
228 , Offset(0)
229 , Size(0)
230 , Children()
231 , Values()
232 {}
233 virtual ~DIE();
234
235 // Accessors.
236 DIEAbbrev &getAbbrev() { return Abbrev; }
237 unsigned getAbbrevNumber() const {
238 return Abbrev.getNumber();
239 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000240 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000241 unsigned getOffset() const { return Offset; }
242 unsigned getSize() const { return Size; }
243 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000244 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000245 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
246 void setOffset(unsigned O) { Offset = O; }
247 void setSize(unsigned S) { Size = S; }
248
249 /// AddValue - Add a value and attributes to a DIE.
250 ///
251 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
252 Abbrev.AddAttribute(Attribute, Form);
253 Values.push_back(Value);
254 }
255
256 /// SiblingOffset - Return the offset of the debug information entry's
257 /// sibling.
258 unsigned SiblingOffset() const { return Offset + Size; }
259
260 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
261 ///
262 void AddSiblingOffset();
263
264 /// AddChild - Add a child to the DIE.
265 ///
266 void AddChild(DIE *Child) {
267 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
268 Children.push_back(Child);
269 }
270
271 /// Detach - Detaches objects connected to it after copying.
272 ///
273 void Detach() {
274 Children.clear();
275 }
276
277 /// Profile - Used to gather unique data for the value folding set.
278 ///
279 void Profile(FoldingSetNodeID &ID) ;
280
281#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000282 void print(std::ostream *O, unsigned IncIndent = 0) {
283 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000284 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000285 void print(std::ostream &O, unsigned IncIndent = 0);
286 void dump();
287#endif
288};
289
290//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000291/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000292///
293class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000294public:
295 enum {
296 isInteger,
297 isString,
298 isLabel,
299 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000300 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000301 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000302 isEntry,
303 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000304 };
305
Jim Laskeyef42a012006-11-02 20:12:39 +0000306 /// Type - Type of data stored in the value.
307 ///
308 unsigned Type;
Jim Laskey0d086af2006-02-27 12:43:29 +0000309
Dan Gohman81975f62007-08-27 14:50:10 +0000310 explicit DIEValue(unsigned T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000311 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000312 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000313 virtual ~DIEValue() {}
314
Jim Laskeyf6733882006-11-02 21:48:18 +0000315 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000316 unsigned getType() const { return Type; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000317
Jim Laskey0d086af2006-02-27 12:43:29 +0000318 // Implement isa/cast/dyncast.
319 static bool classof(const DIEValue *) { return true; }
320
321 /// EmitValue - Emit value via the Dwarf writer.
322 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000323 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Jim Laskey0d086af2006-02-27 12:43:29 +0000324
325 /// SizeOf - Return the size of a value in bytes.
326 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000327 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Jim Laskeyef42a012006-11-02 20:12:39 +0000328
329 /// Profile - Used to gather unique data for the value folding set.
330 ///
331 virtual void Profile(FoldingSetNodeID &ID) = 0;
332
333#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000334 void print(std::ostream *O) {
335 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000336 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000337 virtual void print(std::ostream &O) = 0;
338 void dump();
339#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000340};
Jim Laskey063e7652006-01-17 17:31:53 +0000341
Jim Laskey0d086af2006-02-27 12:43:29 +0000342//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000343/// DWInteger - An integer value DIE.
344///
Jim Laskey0d086af2006-02-27 12:43:29 +0000345class DIEInteger : public DIEValue {
346private:
347 uint64_t Integer;
348
349public:
Dan Gohman81975f62007-08-27 14:50:10 +0000350 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000351
Jim Laskey0d086af2006-02-27 12:43:29 +0000352 // Implement isa/cast/dyncast.
353 static bool classof(const DIEInteger *) { return true; }
354 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
355
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000356 /// BestForm - Choose the best form for integer.
357 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000358 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
359 if (IsSigned) {
360 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
361 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
362 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
363 } else {
364 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
365 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
366 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
367 }
368 return DW_FORM_data8;
369 }
370
Jim Laskey0d086af2006-02-27 12:43:29 +0000371 /// EmitValue - Emit integer of appropriate size.
372 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000373 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000374
375 /// SizeOf - Determine size of integer value in bytes.
376 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000377 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000378
379 /// Profile - Used to gather unique data for the value folding set.
380 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000381 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000382 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000383 ID.AddInteger(Integer);
384 }
Jim Laskey5496f012006-11-09 14:52:14 +0000385 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000386
387#ifndef NDEBUG
388 virtual void print(std::ostream &O) {
389 O << "Int: " << (int64_t)Integer
390 << " 0x" << std::hex << Integer << std::dec;
391 }
392#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000393};
Jim Laskey063e7652006-01-17 17:31:53 +0000394
Jim Laskey0d086af2006-02-27 12:43:29 +0000395//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000396/// DIEString - A string value DIE.
397///
Jim Laskeyef42a012006-11-02 20:12:39 +0000398class DIEString : public DIEValue {
399public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000400 const std::string String;
401
Dan Gohman81975f62007-08-27 14:50:10 +0000402 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000403
Jim Laskey0d086af2006-02-27 12:43:29 +0000404 // Implement isa/cast/dyncast.
405 static bool classof(const DIEString *) { return true; }
406 static bool classof(const DIEValue *S) { return S->Type == isString; }
407
408 /// EmitValue - Emit string value.
409 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000410 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000411
412 /// SizeOf - Determine size of string value in bytes.
413 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000414 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000415 return String.size() + sizeof(char); // sizeof('\0');
416 }
417
418 /// Profile - Used to gather unique data for the value folding set.
419 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000420 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000421 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000422 ID.AddString(String);
423 }
Jim Laskey5496f012006-11-09 14:52:14 +0000424 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000425
426#ifndef NDEBUG
427 virtual void print(std::ostream &O) {
428 O << "Str: \"" << String << "\"";
429 }
430#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000431};
Jim Laskey063e7652006-01-17 17:31:53 +0000432
Jim Laskey0d086af2006-02-27 12:43:29 +0000433//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000434/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000435//
Jim Laskeyef42a012006-11-02 20:12:39 +0000436class DIEDwarfLabel : public DIEValue {
437public:
438
Jim Laskey0d086af2006-02-27 12:43:29 +0000439 const DWLabel Label;
440
Dan Gohman81975f62007-08-27 14:50:10 +0000441 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000442
Jim Laskey0d086af2006-02-27 12:43:29 +0000443 // Implement isa/cast/dyncast.
444 static bool classof(const DIEDwarfLabel *) { return true; }
445 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
446
447 /// EmitValue - Emit label value.
448 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000449 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000450
451 /// SizeOf - Determine size of label value in bytes.
452 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000453 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000454
455 /// Profile - Used to gather unique data for the value folding set.
456 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000457 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000458 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000459 Label.Profile(ID);
460 }
Jim Laskey5496f012006-11-09 14:52:14 +0000461 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000462
463#ifndef NDEBUG
464 virtual void print(std::ostream &O) {
465 O << "Lbl: ";
466 Label.print(O);
467 }
468#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000469};
Jim Laskey063e7652006-01-17 17:31:53 +0000470
Jim Laskey063e7652006-01-17 17:31:53 +0000471
Jim Laskey0d086af2006-02-27 12:43:29 +0000472//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000473/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000474//
Jim Laskeyef42a012006-11-02 20:12:39 +0000475class DIEObjectLabel : public DIEValue {
476public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000477 const std::string Label;
478
Dan Gohman81975f62007-08-27 14:50:10 +0000479 explicit DIEObjectLabel(const std::string &L)
480 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000481
Jim Laskey0d086af2006-02-27 12:43:29 +0000482 // Implement isa/cast/dyncast.
483 static bool classof(const DIEObjectLabel *) { return true; }
484 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
485
486 /// EmitValue - Emit label value.
487 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000488 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000489
490 /// SizeOf - Determine size of label value in bytes.
491 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000492 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000493
494 /// Profile - Used to gather unique data for the value folding set.
495 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000496 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000497 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000498 ID.AddString(Label);
499 }
Jim Laskey5496f012006-11-09 14:52:14 +0000500 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000501
502#ifndef NDEBUG
503 virtual void print(std::ostream &O) {
504 O << "Obj: " << Label;
505 }
506#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000507};
Jim Laskey063e7652006-01-17 17:31:53 +0000508
Jim Laskey0d086af2006-02-27 12:43:29 +0000509//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000510/// DIESectionOffset - A section offset DIE.
511//
512class DIESectionOffset : public DIEValue {
513public:
514 const DWLabel Label;
515 const DWLabel Section;
516 bool IsEH : 1;
517 bool UseSet : 1;
518
519 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
520 bool isEH = false, bool useSet = true)
521 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
522 IsEH(isEH), UseSet(useSet) {}
523
524 // Implement isa/cast/dyncast.
525 static bool classof(const DIESectionOffset *) { return true; }
526 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
527
528 /// EmitValue - Emit section offset.
529 ///
530 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
531
532 /// SizeOf - Determine size of section offset value in bytes.
533 ///
534 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
535
536 /// Profile - Used to gather unique data for the value folding set.
537 ///
538 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
539 const DWLabel &Section) {
540 ID.AddInteger(isSectionOffset);
541 Label.Profile(ID);
542 Section.Profile(ID);
543 // IsEH and UseSet are specific to the Label/Section that we will emit
544 // the offset for; so Label/Section are enough for uniqueness.
545 }
546 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
547
548#ifndef NDEBUG
549 virtual void print(std::ostream &O) {
550 O << "Off: ";
551 Label.print(O);
552 O << "-";
553 Section.print(O);
554 O << "-" << IsEH << "-" << UseSet;
555 }
556#endif
557};
558
559//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000560/// DIEDelta - A simple label difference DIE.
561///
Jim Laskeyef42a012006-11-02 20:12:39 +0000562class DIEDelta : public DIEValue {
563public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000564 const DWLabel LabelHi;
565 const DWLabel LabelLo;
566
567 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
568 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000569
Jim Laskey0d086af2006-02-27 12:43:29 +0000570 // Implement isa/cast/dyncast.
571 static bool classof(const DIEDelta *) { return true; }
572 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
573
574 /// EmitValue - Emit delta value.
575 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000576 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000577
578 /// SizeOf - Determine size of delta value in bytes.
579 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000580 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000581
582 /// Profile - Used to gather unique data for the value folding set.
583 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000584 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
585 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000586 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000587 LabelHi.Profile(ID);
588 LabelLo.Profile(ID);
589 }
Jim Laskey5496f012006-11-09 14:52:14 +0000590 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000591
592#ifndef NDEBUG
593 virtual void print(std::ostream &O) {
594 O << "Del: ";
595 LabelHi.print(O);
596 O << "-";
597 LabelLo.print(O);
598 }
599#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000600};
Jim Laskey063e7652006-01-17 17:31:53 +0000601
Jim Laskey0d086af2006-02-27 12:43:29 +0000602//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000603/// DIEntry - A pointer to another debug information entry. An instance of this
604/// class can also be used as a proxy for a debug information entry not yet
605/// defined (ie. types.)
606class DIEntry : public DIEValue {
607public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000608 DIE *Entry;
609
Dan Gohman81975f62007-08-27 14:50:10 +0000610 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000611
Jim Laskey0d086af2006-02-27 12:43:29 +0000612 // Implement isa/cast/dyncast.
613 static bool classof(const DIEntry *) { return true; }
614 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
615
Jim Laskeyb8509c52006-03-23 18:07:55 +0000616 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000617 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000618 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000619
Jim Laskeyb8509c52006-03-23 18:07:55 +0000620 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000621 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000622 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000623 return sizeof(int32_t);
624 }
625
626 /// Profile - Used to gather unique data for the value folding set.
627 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000628 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
629 ID.AddInteger(isEntry);
630 ID.AddPointer(Entry);
631 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000632 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000633 ID.AddInteger(isEntry);
634
Jim Laskeyef42a012006-11-02 20:12:39 +0000635 if (Entry) {
636 ID.AddPointer(Entry);
637 } else {
638 ID.AddPointer(this);
639 }
640 }
641
642#ifndef NDEBUG
643 virtual void print(std::ostream &O) {
644 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
645 }
646#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000647};
648
649//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000650/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000651//
Jim Laskeyef42a012006-11-02 20:12:39 +0000652class DIEBlock : public DIEValue, public DIE {
653public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000654 unsigned Size; // Size in bytes excluding size header.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000655
656 DIEBlock()
657 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000658 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000659 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000660 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000661 ~DIEBlock() {
662 }
663
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000664 // Implement isa/cast/dyncast.
665 static bool classof(const DIEBlock *) { return true; }
666 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
667
668 /// ComputeSize - calculate the size of the block.
669 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000670 unsigned ComputeSize(DwarfDebug &DD);
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000671
672 /// BestForm - Choose the best form for data.
673 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000674 unsigned BestForm() const {
675 if ((unsigned char)Size == Size) return DW_FORM_block1;
676 if ((unsigned short)Size == Size) return DW_FORM_block2;
677 if ((unsigned int)Size == Size) return DW_FORM_block4;
678 return DW_FORM_block;
679 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000680
681 /// EmitValue - Emit block data.
682 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000683 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000684
685 /// SizeOf - Determine size of block data in bytes.
686 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000687 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Jim Laskeyef42a012006-11-02 20:12:39 +0000688
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000689
Jim Laskeyef42a012006-11-02 20:12:39 +0000690 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000691 ///
Reid Spencer97821312006-11-02 23:56:21 +0000692 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000693 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000694 DIE::Profile(ID);
695 }
696
697#ifndef NDEBUG
698 virtual void print(std::ostream &O) {
699 O << "Blk: ";
700 DIE::print(O, 5);
701 }
702#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000703};
704
705//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000706/// CompileUnit - This dwarf writer support class manages information associate
707/// with a source file.
708class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000709private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000710 /// Desc - Compile unit debug descriptor.
711 ///
712 CompileUnitDesc *Desc;
713
714 /// ID - File identifier for source.
715 ///
716 unsigned ID;
717
718 /// Die - Compile unit debug information entry.
719 ///
720 DIE *Die;
721
722 /// DescToDieMap - Tracks the mapping of unit level debug informaton
723 /// descriptors to debug information entries.
724 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
725
726 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
727 /// descriptors to debug information entries using a DIEntry proxy.
728 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
729
730 /// Globals - A map of globally visible named entities for this unit.
731 ///
732 std::map<std::string, DIE *> Globals;
733
734 /// DiesSet - Used to uniquely define dies within the compile unit.
735 ///
736 FoldingSet<DIE> DiesSet;
737
738 /// Dies - List of all dies in the compile unit.
739 ///
740 std::vector<DIE *> Dies;
Jim Laskey0d086af2006-02-27 12:43:29 +0000741
742public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000743 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
744 : Desc(CUD)
745 , ID(I)
746 , Die(D)
747 , DescToDieMap()
748 , DescToDIEntryMap()
749 , Globals()
750 , DiesSet(InitDiesSetSize)
751 , Dies()
752 {}
753
754 ~CompileUnit() {
755 delete Die;
756
757 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
758 delete Dies[i];
759 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000760
Jim Laskeybd761842006-02-27 17:27:12 +0000761 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000762 CompileUnitDesc *getDesc() const { return Desc; }
763 unsigned getID() const { return ID; }
764 DIE* getDie() const { return Die; }
765 std::map<std::string, DIE *> &getGlobals() { return Globals; }
766
767 /// hasContent - Return true if this compile unit has something to write out.
768 ///
769 bool hasContent() const {
770 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000771 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000772
773 /// AddGlobal - Add a new global entity to the compile unit.
774 ///
775 void AddGlobal(const std::string &Name, DIE *Die) {
776 Globals[Name] = Die;
777 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000778
Jim Laskeyef42a012006-11-02 20:12:39 +0000779 /// getDieMapSlotFor - Returns the debug information entry map slot for the
780 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000781 DIE *&getDieMapSlotFor(DebugInfoDesc *DID) {
782 return DescToDieMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000783 }
Jim Laskeyb8509c52006-03-23 18:07:55 +0000784
Jim Laskeyef42a012006-11-02 20:12:39 +0000785 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
786 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000787 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DID) {
788 return DescToDIEntryMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000789 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000790
Jim Laskeyef42a012006-11-02 20:12:39 +0000791 /// AddDie - Adds or interns the DIE to the compile unit.
792 ///
793 DIE *AddDie(DIE &Buffer) {
794 FoldingSetNodeID ID;
795 Buffer.Profile(ID);
796 void *Where;
797 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
798
799 if (!Die) {
800 Die = new DIE(Buffer);
801 DiesSet.InsertNode(Die, Where);
802 this->Die->AddChild(Die);
803 Buffer.Detach();
804 }
805
806 return Die;
807 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000808};
809
Jim Laskey65195462006-10-30 13:35:07 +0000810//===----------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000811/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000812///
Jim Laskey65195462006-10-30 13:35:07 +0000813class Dwarf {
814
Jim Laskey072200c2007-01-29 18:51:14 +0000815protected:
Jim Laskey65195462006-10-30 13:35:07 +0000816
817 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000818 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000819 //
820
821 //
822 /// O - Stream to .s file.
823 ///
824 std::ostream &O;
825
826 /// Asm - Target of Dwarf emission.
827 ///
828 AsmPrinter *Asm;
829
830 /// TAI - Target Asm Printer.
831 const TargetAsmInfo *TAI;
832
833 /// TD - Target data.
834 const TargetData *TD;
835
836 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000837 const TargetRegisterInfo *RI;
Jim Laskey65195462006-10-30 13:35:07 +0000838
839 /// M - Current module.
840 ///
841 Module *M;
842
843 /// MF - Current machine function.
844 ///
845 MachineFunction *MF;
846
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000847 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000848 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000849 MachineModuleInfo *MMI;
Jim Laskey65195462006-10-30 13:35:07 +0000850
Jim Laskey65195462006-10-30 13:35:07 +0000851 /// SubprogramCount - The running count of functions being compiled.
852 ///
853 unsigned SubprogramCount;
Chris Lattner9251f132007-09-24 03:35:37 +0000854
855 /// Flavor - A unique string indicating what dwarf producer this is, used to
856 /// unique labels.
857 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000858
859 unsigned SetCounter;
Chris Lattner9251f132007-09-24 03:35:37 +0000860 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
861 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000862 : O(OS)
863 , Asm(A)
864 , TAI(T)
865 , TD(Asm->TM.getTargetData())
866 , RI(Asm->TM.getRegisterInfo())
867 , M(NULL)
868 , MF(NULL)
869 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000870 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000871 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000872 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000873 {
874 }
875
876public:
877
878 //===--------------------------------------------------------------------===//
879 // Accessors.
880 //
881 AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000882 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000883 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000884 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000885
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000886 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
887 const {
888 if (isInSection && TAI->getDwarfSectionOffsetDirective())
889 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000890 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000891 O << TAI->getData32bitsDirective();
892 else
893 O << TAI->getData64bitsDirective();
894 }
895
Jim Laskeyb82313f2007-02-01 16:31:34 +0000896 /// PrintLabelName - Print label name in form used by Dwarf writer.
897 ///
898 void PrintLabelName(DWLabel Label) const {
899 PrintLabelName(Label.Tag, Label.Number);
900 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000901 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000902 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000903 if (Number) O << Number;
904 }
905
Chris Lattner9251f132007-09-24 03:35:37 +0000906 void PrintLabelName(const char *Tag, unsigned Number,
907 const char *Suffix) const {
908 O << TAI->getPrivateGlobalPrefix() << Tag;
909 if (Number) O << Number;
910 O << Suffix;
911 }
912
Jim Laskeyb82313f2007-02-01 16:31:34 +0000913 /// EmitLabel - Emit location label for internal use by Dwarf.
914 ///
915 void EmitLabel(DWLabel Label) const {
916 EmitLabel(Label.Tag, Label.Number);
917 }
918 void EmitLabel(const char *Tag, unsigned Number) const {
919 PrintLabelName(Tag, Number);
920 O << ":\n";
921 }
922
923 /// EmitReference - Emit a reference to a label.
924 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000925 void EmitReference(DWLabel Label, bool IsPCRelative = false,
926 bool Force32Bit = false) const {
927 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000928 }
929 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000930 bool IsPCRelative = false, bool Force32Bit = false) const {
931 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000932 PrintLabelName(Tag, Number);
933
934 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
935 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000936 void EmitReference(const std::string &Name, bool IsPCRelative = false,
937 bool Force32Bit = false) const {
938 PrintRelDirective(Force32Bit);
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000939
Jim Laskeyb82313f2007-02-01 16:31:34 +0000940 O << Name;
941
942 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
943 }
944
945 /// EmitDifference - Emit the difference between two labels. Some
946 /// assemblers do not behave with absolute expressions with data directives,
947 /// so there is an option (needsSet) to use an intermediary set expression.
948 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000949 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000950 EmitDifference(LabelHi.Tag, LabelHi.Number,
951 LabelLo.Tag, LabelLo.Number,
952 IsSmall);
953 }
954 void EmitDifference(const char *TagHi, unsigned NumberHi,
955 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000956 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000957 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000958 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000959 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000960 O << ",";
961 PrintLabelName(TagHi, NumberHi);
962 O << "-";
963 PrintLabelName(TagLo, NumberLo);
964 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000965
966 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +0000967 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000968 ++SetCounter;
969 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000970 PrintRelDirective(IsSmall);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000971
972 PrintLabelName(TagHi, NumberHi);
973 O << "-";
974 PrintLabelName(TagLo, NumberLo);
975 }
976 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000977
978 void EmitSectionOffset(const char* Label, const char* Section,
979 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000980 bool IsSmall = false, bool isEH = false,
981 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000982 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000983 if (isEH)
984 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
985 else
986 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
987
988 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000989 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000990 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000991 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000992 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000993
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000994 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000995 O << "-";
996 PrintLabelName(Section, SectionNumber);
997 }
998 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000999
1000 PrintRelDirective(IsSmall);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001001
Chris Lattner9251f132007-09-24 03:35:37 +00001002 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001003 ++SetCounter;
1004 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001005 PrintRelDirective(IsSmall, true);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001006
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001007 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001008
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001009 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001010 O << "-";
1011 PrintLabelName(Section, SectionNumber);
1012 }
1013 }
1014 }
1015
Jim Laskeyb82313f2007-02-01 16:31:34 +00001016 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1017 /// frame.
1018 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +00001019 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001020 int stackGrowth =
1021 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1022 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001023 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +00001024 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +00001025
1026 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001027 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +00001028 unsigned LabelID = Move.getLabelID();
1029
1030 if (LabelID) {
1031 LabelID = MMI->MappedLabel(LabelID);
1032
1033 // Throw out move if the label is invalid.
1034 if (!LabelID) continue;
1035 }
1036
1037 const MachineLocation &Dst = Move.getDestination();
1038 const MachineLocation &Src = Move.getSource();
1039
1040 // Advance row if new location.
1041 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1042 Asm->EmitInt8(DW_CFA_advance_loc4);
1043 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001044 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1045 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001046
1047 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001048 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001049 IsLocal = true;
1050 }
1051
1052 // If advancing cfa.
1053 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
1054 if (!Src.isRegister()) {
1055 if (Src.getRegister() == MachineLocation::VirtualFP) {
1056 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1057 Asm->EOL("DW_CFA_def_cfa_offset");
1058 } else {
1059 Asm->EmitInt8(DW_CFA_def_cfa);
1060 Asm->EOL("DW_CFA_def_cfa");
Dale Johannesenb97aec62007-11-13 19:13:01 +00001061 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001062 Asm->EOL("Register");
1063 }
1064
1065 int Offset = -Src.getOffset();
1066
1067 Asm->EmitULEB128Bytes(Offset);
1068 Asm->EOL("Offset");
1069 } else {
1070 assert(0 && "Machine move no supported yet.");
1071 }
1072 } else if (Src.isRegister() &&
1073 Src.getRegister() == MachineLocation::VirtualFP) {
1074 if (Dst.isRegister()) {
1075 Asm->EmitInt8(DW_CFA_def_cfa_register);
1076 Asm->EOL("DW_CFA_def_cfa_register");
Dale Johannesenb97aec62007-11-13 19:13:01 +00001077 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getRegister(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001078 Asm->EOL("Register");
1079 } else {
1080 assert(0 && "Machine move no supported yet.");
1081 }
1082 } else {
Dale Johannesenb97aec62007-11-13 19:13:01 +00001083 unsigned Reg = RI->getDwarfRegNum(Src.getRegister(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001084 int Offset = Dst.getOffset() / stackGrowth;
1085
1086 if (Offset < 0) {
1087 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1088 Asm->EOL("DW_CFA_offset_extended_sf");
1089 Asm->EmitULEB128Bytes(Reg);
1090 Asm->EOL("Reg");
1091 Asm->EmitSLEB128Bytes(Offset);
1092 Asm->EOL("Offset");
1093 } else if (Reg < 64) {
1094 Asm->EmitInt8(DW_CFA_offset + Reg);
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +00001095 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
Jim Laskeyb82313f2007-02-01 16:31:34 +00001096 Asm->EmitULEB128Bytes(Offset);
1097 Asm->EOL("Offset");
1098 } else {
1099 Asm->EmitInt8(DW_CFA_offset_extended);
1100 Asm->EOL("DW_CFA_offset_extended");
1101 Asm->EmitULEB128Bytes(Reg);
1102 Asm->EOL("Reg");
1103 Asm->EmitULEB128Bytes(Offset);
1104 Asm->EOL("Offset");
1105 }
1106 }
1107 }
1108 }
1109
Jim Laskey072200c2007-01-29 18:51:14 +00001110};
1111
1112//===----------------------------------------------------------------------===//
1113/// DwarfDebug - Emits Dwarf debug directives.
1114///
1115class DwarfDebug : public Dwarf {
1116
1117private:
Jim Laskey65195462006-10-30 13:35:07 +00001118 //===--------------------------------------------------------------------===//
1119 // Attributes used to construct specific Dwarf sections.
1120 //
1121
1122 /// CompileUnits - All the compile units involved in this build. The index
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001123 /// of each entry in this vector corresponds to the sources in MMI.
Jim Laskey65195462006-10-30 13:35:07 +00001124 std::vector<CompileUnit *> CompileUnits;
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001125
Jim Laskeyef42a012006-11-02 20:12:39 +00001126 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001127 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001128 FoldingSet<DIEAbbrev> AbbreviationsSet;
1129
1130 /// Abbreviations - A list of all the unique abbreviations in use.
1131 ///
1132 std::vector<DIEAbbrev *> Abbreviations;
Jim Laskey65195462006-10-30 13:35:07 +00001133
Jim Laskeyef42a012006-11-02 20:12:39 +00001134 /// ValuesSet - Used to uniquely define values.
1135 ///
1136 FoldingSet<DIEValue> ValuesSet;
1137
1138 /// Values - A list of all the unique values in use.
1139 ///
1140 std::vector<DIEValue *> Values;
1141
Jim Laskey65195462006-10-30 13:35:07 +00001142 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001143 ///
Jim Laskey65195462006-10-30 13:35:07 +00001144 UniqueVector<std::string> StringPool;
1145
1146 /// UnitMap - Map debug information descriptor to compile unit.
1147 ///
1148 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
1149
Jim Laskey65195462006-10-30 13:35:07 +00001150 /// SectionMap - Provides a unique id per text section.
1151 ///
1152 UniqueVector<std::string> SectionMap;
1153
1154 /// SectionSourceLines - Tracks line numbers per text section.
1155 ///
1156 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1157
Jim Laskeybacd3042007-02-21 22:48:45 +00001158 /// didInitial - Flag to indicate if initial emission has been done.
1159 ///
1160 bool didInitial;
1161
1162 /// shouldEmit - Flag to indicate if debug information should be emitted.
1163 ///
1164 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001165
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001166 struct FunctionDebugFrameInfo {
1167 unsigned Number;
1168 std::vector<MachineMove> Moves;
1169
1170 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001171 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001172 };
1173
1174 std::vector<FunctionDebugFrameInfo> DebugFrames;
1175
Jim Laskey65195462006-10-30 13:35:07 +00001176public:
Jim Laskeybacd3042007-02-21 22:48:45 +00001177
1178 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1179 ///
1180 bool ShouldEmitDwarf() const { return shouldEmit; }
Jim Laskey65195462006-10-30 13:35:07 +00001181
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001182 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Jim Laskey65195462006-10-30 13:35:07 +00001183 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001184 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1185 // Profile the node so that we can make it unique.
1186 FoldingSetNodeID ID;
1187 Abbrev.Profile(ID);
1188
1189 // Check the set for priors.
1190 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
1191
1192 // If it's newly added.
1193 if (InSet == &Abbrev) {
1194 // Add to abbreviation list.
1195 Abbreviations.push_back(&Abbrev);
1196 // Assign the vector position + 1 as its number.
1197 Abbrev.setNumber(Abbreviations.size());
1198 } else {
1199 // Assign existing abbreviation number.
1200 Abbrev.setNumber(InSet->getNumber());
1201 }
1202 }
1203
Jim Laskey65195462006-10-30 13:35:07 +00001204 /// NewString - Add a string to the constant pool and returns a label.
1205 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001206 DWLabel NewString(const std::string &String) {
1207 unsigned StringID = StringPool.insert(String);
1208 return DWLabel("string", StringID);
1209 }
Jim Laskey65195462006-10-30 13:35:07 +00001210
Jim Laskeyef42a012006-11-02 20:12:39 +00001211 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1212 /// entry.
1213 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1214 DIEntry *Value;
1215
1216 if (Entry) {
1217 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001218 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001219 void *Where;
1220 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
1221
Jim Laskeyf6733882006-11-02 21:48:18 +00001222 if (Value) return Value;
Jim Laskeyef42a012006-11-02 20:12:39 +00001223
1224 Value = new DIEntry(Entry);
1225 ValuesSet.InsertNode(Value, Where);
1226 } else {
1227 Value = new DIEntry(Entry);
1228 }
1229
1230 Values.push_back(Value);
1231 return Value;
1232 }
1233
1234 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1235 ///
1236 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1237 Value->Entry = Entry;
1238 // Add to values set if not already there. If it is, we merely have a
1239 // duplicate in the values list (no harm.)
1240 ValuesSet.GetOrInsertNode(Value);
1241 }
1242
1243 /// AddUInt - Add an unsigned integer attribute data and value.
1244 ///
1245 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1246 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1247
1248 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001249 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001250 void *Where;
1251 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1252 if (!Value) {
1253 Value = new DIEInteger(Integer);
1254 ValuesSet.InsertNode(Value, Where);
1255 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001256 }
1257
1258 Die->AddValue(Attribute, Form, Value);
1259 }
1260
1261 /// AddSInt - Add an signed integer attribute data and value.
1262 ///
1263 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1264 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1265
1266 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001267 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001268 void *Where;
1269 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1270 if (!Value) {
1271 Value = new DIEInteger(Integer);
1272 ValuesSet.InsertNode(Value, Where);
1273 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001274 }
1275
1276 Die->AddValue(Attribute, Form, Value);
1277 }
1278
1279 /// AddString - Add a std::string attribute data and value.
1280 ///
1281 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1282 const std::string &String) {
1283 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001284 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001285 void *Where;
1286 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1287 if (!Value) {
1288 Value = new DIEString(String);
1289 ValuesSet.InsertNode(Value, Where);
1290 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001291 }
1292
1293 Die->AddValue(Attribute, Form, Value);
1294 }
1295
1296 /// AddLabel - Add a Dwarf label attribute data and value.
1297 ///
1298 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1299 const DWLabel &Label) {
1300 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001301 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001302 void *Where;
1303 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1304 if (!Value) {
1305 Value = new DIEDwarfLabel(Label);
1306 ValuesSet.InsertNode(Value, Where);
1307 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001308 }
1309
1310 Die->AddValue(Attribute, Form, Value);
1311 }
1312
1313 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1314 ///
1315 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1316 const std::string &Label) {
1317 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001318 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001319 void *Where;
1320 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1321 if (!Value) {
1322 Value = new DIEObjectLabel(Label);
1323 ValuesSet.InsertNode(Value, Where);
1324 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001325 }
1326
1327 Die->AddValue(Attribute, Form, Value);
1328 }
1329
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001330 /// AddSectionOffset - Add a section offset label attribute data and value.
1331 ///
1332 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1333 const DWLabel &Label, const DWLabel &Section,
1334 bool isEH = false, bool useSet = true) {
1335 FoldingSetNodeID ID;
1336 DIESectionOffset::Profile(ID, Label, Section);
1337 void *Where;
1338 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1339 if (!Value) {
1340 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1341 ValuesSet.InsertNode(Value, Where);
1342 Values.push_back(Value);
1343 }
1344
1345 Die->AddValue(Attribute, Form, Value);
1346 }
1347
Jim Laskeyef42a012006-11-02 20:12:39 +00001348 /// AddDelta - Add a label delta attribute data and value.
1349 ///
1350 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1351 const DWLabel &Hi, const DWLabel &Lo) {
1352 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001353 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001354 void *Where;
1355 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1356 if (!Value) {
1357 Value = new DIEDelta(Hi, Lo);
1358 ValuesSet.InsertNode(Value, Where);
1359 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001360 }
1361
1362 Die->AddValue(Attribute, Form, Value);
1363 }
1364
1365 /// AddDIEntry - Add a DIE attribute data and value.
1366 ///
1367 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1368 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1369 }
1370
1371 /// AddBlock - Add block data.
1372 ///
1373 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1374 Block->ComputeSize(*this);
1375 FoldingSetNodeID ID;
1376 Block->Profile(ID);
1377 void *Where;
1378 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1379 if (!Value) {
1380 Value = Block;
1381 ValuesSet.InsertNode(Value, Where);
1382 Values.push_back(Value);
1383 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001384 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001385 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001386 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001387 }
1388
1389 Die->AddValue(Attribute, Block->BestForm(), Value);
1390 }
1391
Jim Laskey65195462006-10-30 13:35:07 +00001392private:
1393
1394 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001395 /// entry.
1396 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1397 if (File && Line) {
1398 CompileUnit *FileUnit = FindCompileUnit(File);
1399 unsigned FileID = FileUnit->getID();
1400 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1401 AddUInt(Die, DW_AT_decl_line, 0, Line);
1402 }
1403 }
Jim Laskey65195462006-10-30 13:35:07 +00001404
1405 /// AddAddress - Add an address attribute to a die based on the location
1406 /// provided.
1407 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001408 const MachineLocation &Location) {
Dale Johannesenb97aec62007-11-13 19:13:01 +00001409 unsigned Reg = RI->getDwarfRegNum(Location.getRegister(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001410 DIEBlock *Block = new DIEBlock();
1411
1412 if (Location.isRegister()) {
1413 if (Reg < 32) {
1414 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1415 } else {
1416 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1417 AddUInt(Block, 0, DW_FORM_udata, Reg);
1418 }
1419 } else {
1420 if (Reg < 32) {
1421 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1422 } else {
1423 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1424 AddUInt(Block, 0, DW_FORM_udata, Reg);
1425 }
1426 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1427 }
1428
1429 AddBlock(Die, Attribute, 0, Block);
1430 }
1431
1432 /// AddBasicType - Add a new basic type attribute to the specified entity.
1433 ///
1434 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1435 const std::string &Name,
1436 unsigned Encoding, unsigned Size) {
1437 DIE *Die = ConstructBasicType(Unit, Name, Encoding, Size);
1438 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1439 }
1440
1441 /// ConstructBasicType - Construct a new basic type.
1442 ///
1443 DIE *ConstructBasicType(CompileUnit *Unit,
1444 const std::string &Name,
1445 unsigned Encoding, unsigned Size) {
1446 DIE Buffer(DW_TAG_base_type);
1447 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1448 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1449 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1450 return Unit->AddDie(Buffer);
1451 }
1452
1453 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1454 ///
1455 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1456 DIE *Die = ConstructPointerType(Unit, Name);
1457 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1458 }
1459
1460 /// ConstructPointerType - Construct a new pointer type.
1461 ///
1462 DIE *ConstructPointerType(CompileUnit *Unit, const std::string &Name) {
1463 DIE Buffer(DW_TAG_pointer_type);
Dan Gohman82482942007-09-27 23:12:31 +00001464 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Jim Laskeyef42a012006-11-02 20:12:39 +00001465 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1466 return Unit->AddDie(Buffer);
1467 }
1468
1469 /// AddType - Add a new type attribute to the specified entity.
1470 ///
1471 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1472 if (!TyDesc) {
Jim Laskey2b935d52007-01-26 14:19:17 +00001473 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001474 } else {
1475 // Check for pre-existence.
1476 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
1477
1478 // If it exists then use the existing value.
1479 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001480 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1481 return;
1482 }
1483
1484 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1485 // FIXME - Not sure why programs and variables are coming through here.
1486 // Short cut for handling subprogram types (not really a TyDesc.)
1487 AddPointerType(Entity, Unit, SubprogramTy->getName());
1488 } else if (GlobalVariableDesc *GlobalTy =
1489 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1490 // FIXME - Not sure why programs and variables are coming through here.
1491 // Short cut for handling global variable types (not really a TyDesc.)
1492 AddPointerType(Entity, Unit, GlobalTy->getName());
1493 } else {
1494 // Set up proxy.
1495 Slot = NewDIEntry();
1496
1497 // Construct type.
1498 DIE Buffer(DW_TAG_base_type);
1499 ConstructType(Buffer, TyDesc, Unit);
1500
1501 // Add debug information entry to entity and unit.
1502 DIE *Die = Unit->AddDie(Buffer);
1503 SetDIEntry(Slot, Die);
1504 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1505 }
1506 }
1507 }
1508
1509 /// ConstructType - Adds all the required attributes to the type.
1510 ///
1511 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1512 // Get core information.
1513 const std::string &Name = TyDesc->getName();
1514 uint64_t Size = TyDesc->getSize() >> 3;
1515
1516 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1517 // Fundamental types like int, float, bool
1518 Buffer.setTag(DW_TAG_base_type);
1519 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1520 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00001521 // Fetch tag.
1522 unsigned Tag = DerivedTy->getTag();
1523 // FIXME - Workaround for templates.
1524 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1525 // Pointers, typedefs et al.
1526 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00001527 // Map to main type, void will not have a type.
1528 if (TypeDesc *FromTy = DerivedTy->getFromType())
1529 AddType(&Buffer, FromTy, Unit);
1530 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1531 // Fetch tag.
1532 unsigned Tag = CompTy->getTag();
1533
1534 // Set tag accordingly.
1535 if (Tag == DW_TAG_vector_type)
1536 Buffer.setTag(DW_TAG_array_type);
1537 else
1538 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00001539
Jim Laskeyef42a012006-11-02 20:12:39 +00001540 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1541
1542 switch (Tag) {
1543 case DW_TAG_vector_type:
1544 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1545 // Fall thru
1546 case DW_TAG_array_type: {
1547 // Add element type.
1548 if (TypeDesc *FromTy = CompTy->getFromType())
1549 AddType(&Buffer, FromTy, Unit);
1550
1551 // Don't emit size attribute.
1552 Size = 0;
1553
1554 // Construct an anonymous type for index type.
Jim Laskey2b935d52007-01-26 14:19:17 +00001555 DIE *IndexTy = ConstructBasicType(Unit, "", DW_ATE_signed,
1556 sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001557
1558 // Add subranges to array type.
1559 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1560 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1561 int64_t Lo = SRD->getLo();
1562 int64_t Hi = SRD->getHi();
1563 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1564
1565 // If a range is available.
1566 if (Lo != Hi) {
1567 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1568 // Only add low if non-zero.
1569 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1570 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1571 }
1572
1573 Buffer.AddChild(Subrange);
1574 }
1575 break;
1576 }
1577 case DW_TAG_structure_type:
1578 case DW_TAG_union_type: {
1579 // Add elements to structure type.
1580 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1581 DebugInfoDesc *Element = Elements[i];
1582
1583 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1584 // Add field or base class.
1585
1586 unsigned Tag = MemberDesc->getTag();
1587
1588 // Extract the basic information.
1589 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001590 uint64_t Size = MemberDesc->getSize();
1591 uint64_t Align = MemberDesc->getAlign();
1592 uint64_t Offset = MemberDesc->getOffset();
1593
1594 // Construct member debug information entry.
1595 DIE *Member = new DIE(Tag);
1596
1597 // Add name if not "".
1598 if (!Name.empty())
1599 AddString(Member, DW_AT_name, DW_FORM_string, Name);
1600 // Add location if available.
1601 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1602
1603 // Most of the time the field info is the same as the members.
1604 uint64_t FieldSize = Size;
1605 uint64_t FieldAlign = Align;
1606 uint64_t FieldOffset = Offset;
1607
Jim Laskeyee5f9272006-12-22 20:03:42 +00001608 // Set the member type.
1609 TypeDesc *FromTy = MemberDesc->getFromType();
1610 AddType(Member, FromTy, Unit);
1611
1612 // Walk up typedefs until a real size is found.
1613 while (FromTy) {
1614 if (FromTy->getTag() != DW_TAG_typedef) {
1615 FieldSize = FromTy->getSize();
1616 FieldAlign = FromTy->getSize();
1617 break;
1618 }
1619
Dan Gohman275769a2007-07-23 20:24:29 +00001620 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
Jim Laskeyef42a012006-11-02 20:12:39 +00001621 }
1622
1623 // Unless we have a bit field.
1624 if (Tag == DW_TAG_member && FieldSize != Size) {
1625 // Construct the alignment mask.
1626 uint64_t AlignMask = ~(FieldAlign - 1);
1627 // Determine the high bit + 1 of the declared size.
1628 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1629 // Work backwards to determine the base offset of the field.
1630 FieldOffset = HiMark - FieldSize;
1631 // Now normalize offset to the field.
1632 Offset -= FieldOffset;
1633
1634 // Maybe we need to work from the other end.
1635 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1636
1637 // Add size and offset.
1638 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1639 AddUInt(Member, DW_AT_bit_size, 0, Size);
1640 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1641 }
1642
1643 // Add computation for offset.
1644 DIEBlock *Block = new DIEBlock();
1645 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1646 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1647 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1648
1649 // Add accessibility (public default unless is base class.
1650 if (MemberDesc->isProtected()) {
1651 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1652 } else if (MemberDesc->isPrivate()) {
1653 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1654 } else if (Tag == DW_TAG_inheritance) {
1655 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1656 }
1657
1658 Buffer.AddChild(Member);
1659 } else if (GlobalVariableDesc *StaticDesc =
1660 dyn_cast<GlobalVariableDesc>(Element)) {
1661 // Add static member.
1662
1663 // Construct member debug information entry.
1664 DIE *Static = new DIE(DW_TAG_variable);
1665
1666 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001667 const std::string &Name = StaticDesc->getName();
1668 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001669 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001670 if (!LinkageName.empty()) {
1671 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1672 LinkageName);
1673 }
Jim Laskeyef42a012006-11-02 20:12:39 +00001674
1675 // Add location.
1676 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1677
1678 // Add type.
1679 if (TypeDesc *StaticTy = StaticDesc->getType())
1680 AddType(Static, StaticTy, Unit);
1681
1682 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001683 if (!StaticDesc->isStatic())
1684 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001685 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
1686
1687 Buffer.AddChild(Static);
1688 } else if (SubprogramDesc *MethodDesc =
1689 dyn_cast<SubprogramDesc>(Element)) {
1690 // Add member function.
1691
1692 // Construct member debug information entry.
1693 DIE *Method = new DIE(DW_TAG_subprogram);
1694
1695 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001696 const std::string &Name = MethodDesc->getName();
1697 const std::string &LinkageName = MethodDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001698
Jim Laskey2172f962006-11-30 14:35:45 +00001699 AddString(Method, DW_AT_name, DW_FORM_string, Name);
1700 bool IsCTor = TyDesc->getName() == Name;
1701
1702 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001703 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001704 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001705 }
1706
1707 // Add location.
1708 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1709
1710 // Add type.
1711 if (CompositeTypeDesc *MethodTy =
1712 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1713 // Get argument information.
1714 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1715
1716 // If not a ctor.
1717 if (!IsCTor) {
1718 // Add return type.
1719 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1720 }
1721
1722 // Add arguments.
1723 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1724 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1725 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1726 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1727 Method->AddChild(Arg);
1728 }
1729 }
1730
1731 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001732 if (!MethodDesc->isStatic())
1733 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001734 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
1735
1736 Buffer.AddChild(Method);
1737 }
1738 }
1739 break;
1740 }
1741 case DW_TAG_enumeration_type: {
1742 // Add enumerators to enumeration type.
1743 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1744 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1745 const std::string &Name = ED->getName();
1746 int64_t Value = ED->getValue();
1747 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1748 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1749 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1750 Buffer.AddChild(Enumerator);
1751 }
1752
1753 break;
1754 }
1755 case DW_TAG_subroutine_type: {
1756 // Add prototype flag.
1757 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1758 // Add return type.
1759 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
1760
1761 // Add arguments.
1762 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1763 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1764 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1765 Buffer.AddChild(Arg);
1766 }
1767
1768 break;
1769 }
1770 default: break;
1771 }
1772 }
1773
1774 // Add size if non-zero (derived types don't have a size.)
1775 if (Size) AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1776 // Add name if not anonymous or intermediate type.
1777 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1778 // Add source line info if available.
1779 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
1780 }
1781
1782 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00001783 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001784 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1785 // Construct debug information entry.
1786 DIE *Die = new DIE(DW_TAG_compile_unit);
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001787 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
1788 DWLabel("section_line", 0), DWLabel("section_line", 0), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001789 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1790 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1791 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1792 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1793
1794 // Construct compile unit.
1795 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1796
1797 // Add Unit to compile unit map.
1798 DescToUnitMap[UnitDesc] = Unit;
1799
1800 return Unit;
1801 }
1802
Jim Laskey9d4209f2006-11-07 19:33:46 +00001803 /// GetBaseCompileUnit - Get the main compile unit.
1804 ///
1805 CompileUnit *GetBaseCompileUnit() const {
1806 CompileUnit *Unit = CompileUnits[0];
1807 assert(Unit && "Missing compile unit.");
1808 return Unit;
1809 }
1810
Jim Laskey65195462006-10-30 13:35:07 +00001811 /// FindCompileUnit - Get the compile unit for the given descriptor.
1812 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001813 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001814 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00001815 assert(Unit && "Missing compile unit.");
1816 return Unit;
1817 }
1818
1819 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00001820 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001821 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1822 // Get the compile unit context.
1823 CompileUnitDesc *UnitDesc =
1824 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001825 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001826
1827 // Check for pre-existence.
1828 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1829 if (Slot) return Slot;
1830
1831 // Get the global variable itself.
1832 GlobalVariable *GV = GVD->getGlobalVariable();
1833
Jim Laskey2172f962006-11-30 14:35:45 +00001834 const std::string &Name = GVD->getName();
1835 const std::string &FullName = GVD->getFullName();
1836 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001837 // Create the global's variable DIE.
1838 DIE *VariableDie = new DIE(DW_TAG_variable);
1839 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001840 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001841 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001842 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001843 }
Jim Laskey6488a072007-01-08 22:15:18 +00001844 AddType(VariableDie, GVD->getType(), Unit);
1845 if (!GVD->isStatic())
1846 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001847
1848 // Add source line info if available.
1849 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1850
Jim Laskeyef42a012006-11-02 20:12:39 +00001851 // Add address.
1852 DIEBlock *Block = new DIEBlock();
1853 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00001854 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
1855 AddBlock(VariableDie, DW_AT_location, 0, Block);
Jim Laskeyef42a012006-11-02 20:12:39 +00001856
1857 // Add to map.
1858 Slot = VariableDie;
1859
1860 // Add to context owner.
1861 Unit->getDie()->AddChild(VariableDie);
1862
1863 // Expose as global.
1864 // FIXME - need to check external flag.
Jim Laskey2172f962006-11-30 14:35:45 +00001865 Unit->AddGlobal(FullName, VariableDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001866
1867 return VariableDie;
1868 }
Jim Laskey65195462006-10-30 13:35:07 +00001869
1870 /// NewSubprogram - Add a new subprogram DIE.
1871 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001872 DIE *NewSubprogram(SubprogramDesc *SPD) {
1873 // Get the compile unit context.
1874 CompileUnitDesc *UnitDesc =
1875 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001876 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001877
1878 // Check for pre-existence.
1879 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1880 if (Slot) return Slot;
1881
1882 // Gather the details (simplify add attribute code.)
Jim Laskey2172f962006-11-30 14:35:45 +00001883 const std::string &Name = SPD->getName();
1884 const std::string &FullName = SPD->getFullName();
1885 const std::string &LinkageName = SPD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001886
1887 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1888 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001889 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001890 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001891 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001892 }
1893 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
Jim Laskey6488a072007-01-08 22:15:18 +00001894 if (!SPD->isStatic())
1895 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001896 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
1897
1898 // Add source line info if available.
1899 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1900
1901 // Add to map.
1902 Slot = SubprogramDie;
1903
1904 // Add to context owner.
1905 Unit->getDie()->AddChild(SubprogramDie);
1906
1907 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00001908 Unit->AddGlobal(FullName, SubprogramDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001909
1910 return SubprogramDie;
1911 }
Jim Laskey65195462006-10-30 13:35:07 +00001912
1913 /// NewScopeVariable - Create a new scope variable.
1914 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001915 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1916 // Get the descriptor.
1917 VariableDesc *VD = DV->getDesc();
1918
1919 // Translate tag to proper Dwarf tag. The result variable is dropped for
1920 // now.
1921 unsigned Tag;
1922 switch (VD->getTag()) {
1923 case DW_TAG_return_variable: return NULL;
1924 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1925 case DW_TAG_auto_variable: // fall thru
1926 default: Tag = DW_TAG_variable; break;
1927 }
1928
1929 // Define variable debug information entry.
1930 DIE *VariableDie = new DIE(Tag);
1931 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1932
1933 // Add source line info if available.
1934 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1935
1936 // Add variable type.
1937 AddType(VariableDie, VD->getType(), Unit);
1938
1939 // Add variable address.
1940 MachineLocation Location;
Evan Cheng72bebb92008-01-31 03:37:28 +00001941 Location.set(RI->getFrameRegister(*MF),
1942 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Jim Laskeyef42a012006-11-02 20:12:39 +00001943 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001944
Jim Laskeyef42a012006-11-02 20:12:39 +00001945 return VariableDie;
1946 }
Jim Laskey65195462006-10-30 13:35:07 +00001947
1948 /// ConstructScope - Construct the components of a scope.
1949 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001950 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00001951 unsigned ParentStartID, unsigned ParentEndID,
1952 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001953 // Add variables to scope.
1954 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1955 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1956 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1957 if (VariableDie) ParentDie->AddChild(VariableDie);
1958 }
1959
1960 // Add nested scopes.
1961 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1962 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1963 // Define the Scope debug information entry.
1964 DebugScope *Scope = Scopes[j];
1965 // FIXME - Ignore inlined functions for the time being.
1966 if (!Scope->getParent()) continue;
1967
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001968 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1969 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00001970
Jim Laskey9d4209f2006-11-07 19:33:46 +00001971 // Ignore empty scopes.
1972 if (StartID == EndID && StartID != 0) continue;
1973 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00001974
Jim Laskey36729dd2006-11-29 16:55:57 +00001975 if (StartID == ParentStartID && EndID == ParentEndID) {
1976 // Just add stuff to the parent scope.
1977 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001978 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00001979 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1980
1981 // Add the scope bounds.
1982 if (StartID) {
1983 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00001984 DWLabel("label", StartID));
Jim Laskey36729dd2006-11-29 16:55:57 +00001985 } else {
1986 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1987 DWLabel("func_begin", SubprogramCount));
1988 }
1989 if (EndID) {
1990 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00001991 DWLabel("label", EndID));
Jim Laskey36729dd2006-11-29 16:55:57 +00001992 } else {
1993 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1994 DWLabel("func_end", SubprogramCount));
1995 }
1996
1997 // Add the scope contents.
1998 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
1999 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00002000 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002001 }
2002 }
Jim Laskey65195462006-10-30 13:35:07 +00002003
2004 /// ConstructRootScope - Construct the scope for the subprogram.
2005 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002006 void ConstructRootScope(DebugScope *RootScope) {
2007 // Exit if there is no root scope.
2008 if (!RootScope) return;
2009
2010 // Get the subprogram debug information entry.
2011 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
2012
2013 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00002014 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002015
2016 // Get the subprogram die.
2017 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2018 assert(SPDie && "Missing subprogram descriptor");
2019
2020 // Add the function bounds.
2021 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2022 DWLabel("func_begin", SubprogramCount));
2023 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2024 DWLabel("func_end", SubprogramCount));
2025 MachineLocation Location(RI->getFrameRegister(*MF));
2026 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002027
Jim Laskey36729dd2006-11-29 16:55:57 +00002028 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002029 }
Jim Laskey65195462006-10-30 13:35:07 +00002030
Jim Laskeyef42a012006-11-02 20:12:39 +00002031 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2032 /// tools to recognize the object file contains Dwarf information.
2033 void EmitInitial() {
2034 // Check to see if we already emitted intial headers.
2035 if (didInitial) return;
2036 didInitial = true;
2037
2038 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002039 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002040 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002041 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002042 }
2043 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2044 EmitLabel("section_info", 0);
2045 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2046 EmitLabel("section_abbrev", 0);
2047 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2048 EmitLabel("section_aranges", 0);
2049 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2050 EmitLabel("section_macinfo", 0);
2051 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2052 EmitLabel("section_line", 0);
2053 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2054 EmitLabel("section_loc", 0);
2055 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2056 EmitLabel("section_pubnames", 0);
2057 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2058 EmitLabel("section_str", 0);
2059 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2060 EmitLabel("section_ranges", 0);
2061
2062 Asm->SwitchToTextSection(TAI->getTextSection());
2063 EmitLabel("text_begin", 0);
2064 Asm->SwitchToDataSection(TAI->getDataSection());
2065 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002066 }
2067
Jim Laskey65195462006-10-30 13:35:07 +00002068 /// EmitDIE - Recusively Emits a debug information entry.
2069 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002070 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002071 // Get the abbreviation for this DIE.
2072 unsigned AbbrevNumber = Die->getAbbrevNumber();
2073 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2074
Jim Laskeybacd3042007-02-21 22:48:45 +00002075 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002076
2077 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002078 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002079
2080 if (VerboseAsm)
2081 Asm->EOL(std::string("Abbrev [" +
2082 utostr(AbbrevNumber) +
2083 "] 0x" + utohexstr(Die->getOffset()) +
2084 ":0x" + utohexstr(Die->getSize()) + " " +
2085 TagString(Abbrev->getTag())));
2086 else
2087 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002088
Owen Anderson873e1b52008-06-24 21:44:59 +00002089 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2090 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002091
2092 // Emit the DIE attribute values.
2093 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2094 unsigned Attr = AbbrevData[i].getAttribute();
2095 unsigned Form = AbbrevData[i].getForm();
2096 assert(Form && "Too many attributes for DIE (check abbreviation)");
2097
2098 switch (Attr) {
2099 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002100 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002101 break;
2102 }
2103 default: {
2104 // Emit an attribute using the defined form.
2105 Values[i]->EmitValue(*this, Form);
2106 break;
2107 }
2108 }
2109
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002110 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002111 }
2112
2113 // Emit the DIE children if any.
2114 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2115 const std::vector<DIE *> &Children = Die->getChildren();
2116
2117 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2118 EmitDIE(Children[j]);
2119 }
2120
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002121 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002122 }
2123 }
2124
Jim Laskey65195462006-10-30 13:35:07 +00002125 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2126 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002127 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2128 // Get the children.
2129 const std::vector<DIE *> &Children = Die->getChildren();
2130
2131 // If not last sibling and has children then add sibling offset attribute.
2132 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2133
2134 // Record the abbreviation.
2135 AssignAbbrevNumber(Die->getAbbrev());
2136
2137 // Get the abbreviation for this DIE.
2138 unsigned AbbrevNumber = Die->getAbbrevNumber();
2139 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2140
2141 // Set DIE offset
2142 Die->setOffset(Offset);
2143
2144 // Start the size with the size of abbreviation code.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002145 Offset += Asm->SizeULEB128(AbbrevNumber);
Jim Laskeyef42a012006-11-02 20:12:39 +00002146
Owen Anderson873e1b52008-06-24 21:44:59 +00002147 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2148 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002149
2150 // Size the DIE attribute values.
2151 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2152 // Size attribute value.
2153 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2154 }
2155
2156 // Size the DIE children if any.
2157 if (!Children.empty()) {
2158 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2159 "Children flag not set");
2160
2161 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2162 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2163 }
2164
2165 // End of children marker.
2166 Offset += sizeof(int8_t);
2167 }
2168
2169 Die->setSize(Offset - Die->getOffset());
2170 return Offset;
2171 }
Jim Laskey65195462006-10-30 13:35:07 +00002172
2173 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2174 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002175 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002176 // Process base compile unit.
2177 CompileUnit *Unit = GetBaseCompileUnit();
2178 // Compute size of compile unit header
2179 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2180 sizeof(int16_t) + // DWARF version number
2181 sizeof(int32_t) + // Offset Into Abbrev. Section
2182 sizeof(int8_t); // Pointer Size (in bytes)
2183 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002184 }
2185
Jim Laskey65195462006-10-30 13:35:07 +00002186 /// EmitDebugInfo - Emit the debug info section.
2187 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002188 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002189 // Start debug info section.
2190 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2191
Jim Laskey5496f012006-11-09 14:52:14 +00002192 CompileUnit *Unit = GetBaseCompileUnit();
2193 DIE *Die = Unit->getDie();
2194 // Emit the compile units header.
2195 EmitLabel("info_begin", Unit->getID());
2196 // Emit size of content not including length itself
2197 unsigned ContentSize = Die->getSize() +
2198 sizeof(int16_t) + // DWARF version number
2199 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00002200 sizeof(int8_t) + // Pointer Size (in bytes)
2201 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Laskey5496f012006-11-09 14:52:14 +00002202
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002203 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2204 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002205 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002206 Asm->EOL("Offset Into Abbrev. Section");
Dan Gohman82482942007-09-27 23:12:31 +00002207 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
Jim Laskey5496f012006-11-09 14:52:14 +00002208
2209 EmitDIE(Die);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002210 // FIXME - extra padding for gdb bug.
2211 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2212 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2213 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2214 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Jim Laskey5496f012006-11-09 14:52:14 +00002215 EmitLabel("info_end", Unit->getID());
2216
Jim Laskeybacd3042007-02-21 22:48:45 +00002217 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002218 }
2219
Jim Laskey65195462006-10-30 13:35:07 +00002220 /// EmitAbbreviations - Emit the abbreviation section.
2221 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002222 void EmitAbbreviations() const {
2223 // Check to see if it is worth the effort.
2224 if (!Abbreviations.empty()) {
2225 // Start the debug abbrev section.
2226 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2227
2228 EmitLabel("abbrev_begin", 0);
2229
2230 // For each abbrevation.
2231 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2232 // Get abbreviation data
2233 const DIEAbbrev *Abbrev = Abbreviations[i];
2234
2235 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002236 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2237 Asm->EOL("Abbreviation Code");
Jim Laskeyef42a012006-11-02 20:12:39 +00002238
2239 // Emit the abbreviations data.
2240 Abbrev->Emit(*this);
2241
Jim Laskeybacd3042007-02-21 22:48:45 +00002242 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002243 }
2244
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002245 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002246 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002247
Jim Laskeyef42a012006-11-02 20:12:39 +00002248 EmitLabel("abbrev_end", 0);
2249
Jim Laskeybacd3042007-02-21 22:48:45 +00002250 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002251 }
2252 }
2253
Jim Laskey65195462006-10-30 13:35:07 +00002254 /// EmitDebugLines - Emit source line information.
2255 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002256 void EmitDebugLines() {
Dan Gohman81a148b2007-09-24 21:43:52 +00002257 // If there are no lines to emit (such as when we're using .loc directives
2258 // to emit .debug_line information) don't emit a .debug_line header.
2259 if (SectionSourceLines.empty())
2260 return;
2261
Jim Laskeyef42a012006-11-02 20:12:39 +00002262 // Minimum line delta, thus ranging from -10..(255-10).
2263 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2264 // Maximum line delta, thus ranging from -10..(255-10).
2265 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002266
Jim Laskeyef42a012006-11-02 20:12:39 +00002267 // Start the dwarf line section.
2268 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2269
2270 // Construct the section header.
2271
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002272 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002273 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002274 EmitLabel("line_begin", 0);
2275
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002276 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Jim Laskeyef42a012006-11-02 20:12:39 +00002277
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002278 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002279 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002280 EmitLabel("line_prolog_begin", 0);
2281
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002282 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002283
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002284 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002285
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002286 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002287
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002288 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002289
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002290 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Jim Laskeyef42a012006-11-02 20:12:39 +00002291
2292 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002293 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2294 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2295 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2296 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2297 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2298 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2299 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2300 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2301 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002302
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002303 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Evan Cheng6547e402008-07-01 23:18:29 +00002304 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
Jim Laskeyef42a012006-11-02 20:12:39 +00002305
2306 // Emit directories.
2307 for (unsigned DirectoryID = 1, NDID = Directories.size();
2308 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002309 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002310 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002311 Asm->EmitInt8(0); Asm->EOL("End of directories");
Jim Laskeyef42a012006-11-02 20:12:39 +00002312
2313 // Emit files.
2314 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2315 SourceID <= NSID; ++SourceID) {
2316 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002317 Asm->EmitString(SourceFile.getName());
2318 Asm->EOL("Source");
2319 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2320 Asm->EOL("Directory #");
2321 Asm->EmitULEB128Bytes(0);
2322 Asm->EOL("Mod date");
2323 Asm->EmitULEB128Bytes(0);
2324 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002325 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002326 Asm->EmitInt8(0); Asm->EOL("End of files");
Jim Laskeyef42a012006-11-02 20:12:39 +00002327
2328 EmitLabel("line_prolog_end", 0);
2329
2330 // A sequence for each text section.
2331 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2332 // Isolate current sections line info.
2333 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002334
2335 if (VerboseAsm)
2336 Asm->EOL(std::string("Section ") + SectionMap[j + 1]);
2337 else
2338 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002339
2340 // Dwarf assumes we start with first line of first source file.
2341 unsigned Source = 1;
2342 unsigned Line = 1;
2343
2344 // Construct rows of the address, source, line, column matrix.
2345 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2346 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002347 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002348 if (!LabelID) continue;
Jim Laskeyef42a012006-11-02 20:12:39 +00002349
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002350 unsigned SourceID = LineInfo.getSourceID();
2351 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2352 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng6547e402008-07-01 23:18:29 +00002353 if (VerboseAsm)
2354 Asm->EOL(Directories[DirectoryID]
2355 + SourceFile.getName()
2356 + ":"
2357 + utostr_32(LineInfo.getLine()));
2358 else
2359 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002360
2361 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002362 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002363 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002364 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002365 EmitReference("label", LabelID); Asm->EOL("Location label");
Jim Laskeyef42a012006-11-02 20:12:39 +00002366
2367 // If change of source, then switch to the new source.
2368 if (Source != LineInfo.getSourceID()) {
2369 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002370 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2371 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002372 }
2373
2374 // If change of line.
2375 if (Line != LineInfo.getLine()) {
2376 // Determine offset.
2377 int Offset = LineInfo.getLine() - Line;
2378 int Delta = Offset - MinLineDelta;
2379
2380 // Update line.
2381 Line = LineInfo.getLine();
2382
2383 // If delta is small enough and in range...
2384 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2385 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002386 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002387 } else {
2388 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002389 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2390 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2391 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002392 }
2393 } else {
2394 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002395 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002396 }
2397 }
2398
2399 // Define last address of section.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002400 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002401 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002402 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2403 EmitReference("section_end", j + 1); Asm->EOL("Section end label");
Jim Laskeyef42a012006-11-02 20:12:39 +00002404
2405 // Mark end of matrix.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002406 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
Jim Laskeybacd3042007-02-21 22:48:45 +00002407 Asm->EmitULEB128Bytes(1); Asm->EOL();
2408 Asm->EmitInt8(1); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002409 }
2410
2411 EmitLabel("line_end", 0);
2412
Jim Laskeybacd3042007-02-21 22:48:45 +00002413 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002414 }
2415
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002416 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002417 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002418 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002419 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002420 return;
2421
2422 int stackGrowth =
2423 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2424 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002425 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002426
2427 // Start the dwarf frame section.
2428 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2429
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002430 EmitLabel("debug_frame_common", 0);
2431 EmitDifference("debug_frame_common_end", 0,
2432 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002433 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002434
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002435 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002436 Asm->EmitInt32((int)DW_CIE_ID);
2437 Asm->EOL("CIE Identifier Tag");
2438 Asm->EmitInt8(DW_CIE_VERSION);
2439 Asm->EOL("CIE Version");
2440 Asm->EmitString("");
2441 Asm->EOL("CIE Augmentation");
2442 Asm->EmitULEB128Bytes(1);
2443 Asm->EOL("CIE Code Alignment Factor");
2444 Asm->EmitSLEB128Bytes(stackGrowth);
2445 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002446 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002447 Asm->EOL("CIE RA Column");
Jim Laskey65195462006-10-30 13:35:07 +00002448
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002449 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002450 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002451
Dale Johannesenb97aec62007-11-13 19:13:01 +00002452 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002453
Evan Cheng05548eb2008-02-29 19:36:59 +00002454 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002455 EmitLabel("debug_frame_common_end", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002456
Jim Laskeybacd3042007-02-21 22:48:45 +00002457 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002458 }
2459
Jim Laskey65195462006-10-30 13:35:07 +00002460 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2461 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002462 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002463 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002464 return;
Jim Laskey9d4209f2006-11-07 19:33:46 +00002465
Jim Laskeyef42a012006-11-02 20:12:39 +00002466 // Start the dwarf frame section.
2467 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2468
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002469 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2470 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002471 Asm->EOL("Length of Frame Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002472
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002473 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002474
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002475 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2476 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002477 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002478
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002479 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002480 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002481 EmitDifference("func_end", DebugFrameInfo.Number,
2482 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002483 Asm->EOL("FDE address range");
Jim Laskeyef42a012006-11-02 20:12:39 +00002484
Dale Johannesenb97aec62007-11-13 19:13:01 +00002485 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002486
Evan Cheng05548eb2008-02-29 19:36:59 +00002487 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002488 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002489
Jim Laskeybacd3042007-02-21 22:48:45 +00002490 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002491 }
2492
2493 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002494 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002495 void EmitDebugPubNames() {
2496 // Start the dwarf pubnames section.
2497 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2498
Jim Laskey5496f012006-11-09 14:52:14 +00002499 CompileUnit *Unit = GetBaseCompileUnit();
2500
2501 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002502 "pubnames_begin", Unit->getID(), true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002503 Asm->EOL("Length of Public Names Info");
Jim Laskey5496f012006-11-09 14:52:14 +00002504
2505 EmitLabel("pubnames_begin", Unit->getID());
2506
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002507 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002508
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002509 EmitSectionOffset("info_begin", "section_info",
2510 Unit->getID(), 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002511 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002512
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002513 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002514 Asm->EOL("Compilation Unit Length");
Jim Laskey5496f012006-11-09 14:52:14 +00002515
2516 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2517
2518 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2519 GE = Globals.end();
2520 GI != GE; ++GI) {
2521 const std::string &Name = GI->first;
2522 DIE * Entity = GI->second;
Jim Laskeyef42a012006-11-02 20:12:39 +00002523
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002524 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2525 Asm->EmitString(Name); Asm->EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00002526 }
Jim Laskey5496f012006-11-09 14:52:14 +00002527
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002528 Asm->EmitInt32(0); Asm->EOL("End Mark");
Jim Laskey5496f012006-11-09 14:52:14 +00002529 EmitLabel("pubnames_end", Unit->getID());
2530
Jim Laskeybacd3042007-02-21 22:48:45 +00002531 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002532 }
2533
2534 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002535 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002536 void EmitDebugStr() {
2537 // Check to see if it is worth the effort.
2538 if (!StringPool.empty()) {
2539 // Start the dwarf str section.
2540 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2541
2542 // For each of strings in the string pool.
2543 for (unsigned StringID = 1, N = StringPool.size();
2544 StringID <= N; ++StringID) {
2545 // Emit a label for reference from debug information entries.
2546 EmitLabel("string", StringID);
2547 // Emit the string itself.
2548 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002549 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002550 }
2551
Jim Laskeybacd3042007-02-21 22:48:45 +00002552 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002553 }
2554 }
2555
2556 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002557 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002558 void EmitDebugLoc() {
2559 // Start the dwarf loc section.
2560 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2561
Jim Laskeybacd3042007-02-21 22:48:45 +00002562 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002563 }
2564
2565 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002566 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002567 void EmitDebugARanges() {
2568 // Start the dwarf aranges section.
2569 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2570
2571 // FIXME - Mock up
2572 #if 0
Jim Laskey5496f012006-11-09 14:52:14 +00002573 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002574
Jim Laskey5496f012006-11-09 14:52:14 +00002575 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002576 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Jim Laskey5496f012006-11-09 14:52:14 +00002577
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002578 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Jim Laskey5496f012006-11-09 14:52:14 +00002579
2580 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002581 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002582
Dan Gohman82482942007-09-27 23:12:31 +00002583 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002584
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002585 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002586
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002587 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2588 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002589
Jim Laskey5496f012006-11-09 14:52:14 +00002590 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002591 EmitReference("text_begin", 0); Asm->EOL("Address");
2592 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002593
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002594 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2595 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Dan Gohman44de9262007-09-24 21:36:21 +00002596 #endif
Jim Laskey5496f012006-11-09 14:52:14 +00002597
Jim Laskeybacd3042007-02-21 22:48:45 +00002598 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002599 }
2600
2601 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002602 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002603 void EmitDebugRanges() {
2604 // Start the dwarf ranges section.
2605 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2606
Jim Laskeybacd3042007-02-21 22:48:45 +00002607 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002608 }
2609
2610 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002611 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002612 void EmitDebugMacInfo() {
2613 // Start the dwarf macinfo section.
2614 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2615
Jim Laskeybacd3042007-02-21 22:48:45 +00002616 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002617 }
2618
Jim Laskey65195462006-10-30 13:35:07 +00002619 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2620 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002621 void ConstructCompileUnitDIEs() {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002622 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
Jim Laskeyef42a012006-11-02 20:12:39 +00002623
2624 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002625 unsigned ID = MMI->RecordSource(CUW[i]);
Jim Laskey9d4209f2006-11-07 19:33:46 +00002626 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002627 CompileUnits.push_back(Unit);
2628 }
2629 }
2630
Jim Laskey65195462006-10-30 13:35:07 +00002631 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2632 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002633 void ConstructGlobalDIEs() {
Bill Wendling305635a2008-06-27 00:09:40 +00002634 std::vector<void*> GlobalVariables;
2635 GlobalVariableDesc GVD;
2636 MMI->getAnchoredDescriptors(*M, &GVD, GlobalVariables);
Jim Laskeyef42a012006-11-02 20:12:39 +00002637
Bill Wendling305635a2008-06-27 00:09:40 +00002638 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i)
2639 NewGlobalVariable((GlobalVariableDesc *)GlobalVariables[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002640 }
Jim Laskey65195462006-10-30 13:35:07 +00002641
2642 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2643 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002644 void ConstructSubprogramDIEs() {
Bill Wendling305635a2008-06-27 00:09:40 +00002645 std::vector<void*> Subprograms;
2646 SubprogramDesc SPD;
2647 MMI->getAnchoredDescriptors(*M, &SPD, Subprograms);
Jim Laskeyef42a012006-11-02 20:12:39 +00002648
Bill Wendling305635a2008-06-27 00:09:40 +00002649 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i)
2650 NewSubprogram((SubprogramDesc*)Subprograms[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002651 }
Jim Laskey65195462006-10-30 13:35:07 +00002652
Jim Laskey65195462006-10-30 13:35:07 +00002653public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002654 //===--------------------------------------------------------------------===//
2655 // Main entry points.
2656 //
Jim Laskey072200c2007-01-29 18:51:14 +00002657 DwarfDebug(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00002658 : Dwarf(OS, A, T, "dbg")
Jim Laskeyef42a012006-11-02 20:12:39 +00002659 , CompileUnits()
2660 , AbbreviationsSet(InitAbbreviationsSetSize)
2661 , Abbreviations()
2662 , ValuesSet(InitValuesSetSize)
2663 , Values()
2664 , StringPool()
2665 , DescToUnitMap()
2666 , SectionMap()
2667 , SectionSourceLines()
Jim Laskeybacd3042007-02-21 22:48:45 +00002668 , didInitial(false)
2669 , shouldEmit(false)
Jim Laskeyef42a012006-11-02 20:12:39 +00002670 {
2671 }
Jim Laskey072200c2007-01-29 18:51:14 +00002672 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002673 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2674 delete CompileUnits[i];
2675 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2676 delete Values[j];
2677 }
2678
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002679 /// SetModuleInfo - Set machine module information when it's known that pass
2680 /// manager has created it. Set by the target AsmPrinter.
2681 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002682 // Make sure initial declarations are made.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002683 if (!MMI && mmi->hasDebugInfo()) {
2684 MMI = mmi;
Jim Laskeyef42a012006-11-02 20:12:39 +00002685 shouldEmit = true;
2686
Jim Laskeyef42a012006-11-02 20:12:39 +00002687 // Create all the compile unit DIEs.
2688 ConstructCompileUnitDIEs();
2689
2690 // Create DIEs for each of the externally visible global variables.
2691 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002692
Jim Laskeyef42a012006-11-02 20:12:39 +00002693 // Create DIEs for each of the externally visible subprograms.
2694 ConstructSubprogramDIEs();
2695
2696 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00002697 SectionMap.insert(TAI->getTextSection());
Dan Gohmand57c3882007-10-01 22:40:20 +00002698
2699 // Print out .file directives to specify files for .loc directives. These
2700 // are printed out early so that they precede any .loc directives.
2701 if (TAI->hasDotLocAndDotFile()) {
2702 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
2703 const UniqueVector<std::string> &Directories = MMI->getDirectories();
2704 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
2705 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
2706 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
2707 assert(AppendOk && "Could not append filename to directory!");
2708 Asm->EmitFile(i, FullPath.toString());
2709 Asm->EOL();
2710 }
2711 }
2712
2713 // Emit initial sections
2714 EmitInitial();
Jim Laskeyef42a012006-11-02 20:12:39 +00002715 }
2716 }
2717
Jim Laskey65195462006-10-30 13:35:07 +00002718 /// BeginModule - Emit all Dwarf sections that should come prior to the
2719 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002720 void BeginModule(Module *M) {
2721 this->M = M;
2722
2723 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002724 }
2725
Jim Laskey65195462006-10-30 13:35:07 +00002726 /// EndModule - Emit all Dwarf sections that should come after the content.
2727 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002728 void EndModule() {
2729 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002730
2731 // Standard sections final addresses.
2732 Asm->SwitchToTextSection(TAI->getTextSection());
2733 EmitLabel("text_end", 0);
2734 Asm->SwitchToDataSection(TAI->getDataSection());
2735 EmitLabel("data_end", 0);
2736
2737 // End text sections.
2738 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2739 Asm->SwitchToTextSection(SectionMap[i].c_str());
2740 EmitLabel("section_end", i);
2741 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002742
2743 // Emit common frame information.
2744 EmitCommonDebugFrame();
2745
2746 // Emit function debug frame information
2747 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2748 E = DebugFrames.end(); I != E; ++I)
2749 EmitFunctionDebugFrame(*I);
2750
Jim Laskeyef42a012006-11-02 20:12:39 +00002751 // Compute DIE offsets and sizes.
2752 SizeAndOffsets();
2753
2754 // Emit all the DIEs into a debug info section
2755 EmitDebugInfo();
2756
2757 // Corresponding abbreviations into a abbrev section.
2758 EmitAbbreviations();
2759
2760 // Emit source line correspondence into a debug line section.
2761 EmitDebugLines();
2762
2763 // Emit info into a debug pubnames section.
2764 EmitDebugPubNames();
2765
2766 // Emit info into a debug str section.
2767 EmitDebugStr();
2768
2769 // Emit info into a debug loc section.
2770 EmitDebugLoc();
2771
2772 // Emit info into a debug aranges section.
2773 EmitDebugARanges();
2774
2775 // Emit info into a debug ranges section.
2776 EmitDebugRanges();
2777
2778 // Emit info into a debug macinfo section.
2779 EmitDebugMacInfo();
2780 }
2781
Jim Laskey65195462006-10-30 13:35:07 +00002782 /// BeginFunction - Gather pre-function debug information. Assumes being
2783 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002784 void BeginFunction(MachineFunction *MF) {
2785 this->MF = MF;
2786
2787 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002788
2789 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002790 MMI->BeginFunction(MF);
Jim Laskeyef42a012006-11-02 20:12:39 +00002791
2792 // Assumes in correct section after the entry point.
2793 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00002794
2795 // Emit label for the implicitly defined dbg.stoppoint at the start of
2796 // the function.
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00002797 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
2798 if (!LineInfos.empty()) {
2799 const SourceLineInfo &LineInfo = LineInfos[0];
2800 Asm->printLabel(LineInfo.getLabelID());
2801 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002802 }
Jim Laskey072200c2007-01-29 18:51:14 +00002803
Jim Laskey65195462006-10-30 13:35:07 +00002804 /// EndFunction - Gather and emit post-function debug information.
2805 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002806 void EndFunction() {
2807 if (!ShouldEmitDwarf()) return;
Jim Laskeyb82313f2007-02-01 16:31:34 +00002808
2809 // Define end label for subprogram.
2810 EmitLabel("func_end", SubprogramCount);
Jim Laskeyef42a012006-11-02 20:12:39 +00002811
2812 // Get function line info.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002813 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
Jim Laskeyef42a012006-11-02 20:12:39 +00002814
2815 if (!LineInfos.empty()) {
2816 // Get section line info.
2817 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2818 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2819 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2820 // Append the function info to section info.
2821 SectionLineInfos.insert(SectionLineInfos.end(),
2822 LineInfos.begin(), LineInfos.end());
2823 }
2824
2825 // Construct scopes for subprogram.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002826 ConstructRootScope(MMI->getRootScope());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002827
2828 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2829 MMI->getFrameMoves()));
Jim Laskeyef42a012006-11-02 20:12:39 +00002830 }
Jim Laskey65195462006-10-30 13:35:07 +00002831};
2832
Jim Laskey072200c2007-01-29 18:51:14 +00002833//===----------------------------------------------------------------------===//
2834/// DwarfException - Emits Dwarf exception handling directives.
2835///
2836class DwarfException : public Dwarf {
2837
Jim Laskeyb82313f2007-02-01 16:31:34 +00002838private:
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002839 struct FunctionEHFrameInfo {
2840 std::string FnName;
2841 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002842 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002843 bool hasCalls;
2844 bool hasLandingPads;
2845 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002846 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00002847
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002848 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
2849 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00002850 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002851 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002852 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002853 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002854 };
2855
2856 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00002857
2858 /// shouldEmitTable - Per-function flag to indicate if EH tables should
2859 /// be emitted.
2860 bool shouldEmitTable;
2861
2862 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
2863 /// should be emitted.
2864 bool shouldEmitMoves;
2865
2866 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
2867 /// should be emitted.
2868 bool shouldEmitTableModule;
2869
2870 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
2871 /// should be emitted.
2872 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00002873
Jim Laskey3f09fc22007-02-28 18:38:31 +00002874 /// EmitCommonEHFrame - Emit the common eh unwind frame.
2875 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002876 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00002877 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002878 int stackGrowth =
2879 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2880 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002881 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00002882
Jim Laskeybacd3042007-02-21 22:48:45 +00002883 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002884 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002885 O << "EH_frame" << Index << ":\n";
2886 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002887
Jim Laskeybacd3042007-02-21 22:48:45 +00002888 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002889 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00002890
Jim Laskeybacd3042007-02-21 22:48:45 +00002891 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002892 EmitDifference("eh_frame_common_end", Index,
2893 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002894 Asm->EOL("Length of Common Information Entry");
2895
Jim Laskeybacd3042007-02-21 22:48:45 +00002896 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002897 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002898 Asm->EmitInt32((int)0);
2899 Asm->EOL("CIE Identifier Tag");
2900 Asm->EmitInt8(DW_CIE_VERSION);
2901 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00002902
Jim Laskeybacd3042007-02-21 22:48:45 +00002903 // The personality presence indicates that language specific information
2904 // will show up in the eh frame.
2905 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00002906 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00002907
Jim Laskeybacd3042007-02-21 22:48:45 +00002908 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002909 Asm->EmitULEB128Bytes(1);
2910 Asm->EOL("CIE Code Alignment Factor");
2911 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00002912 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002913 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00002914 Asm->EOL("CIE Return Address Column");
2915
Jim Laskeybacd3042007-02-21 22:48:45 +00002916 // If there is a personality, we need to indicate the functions location.
2917 if (Personality) {
2918 Asm->EmitULEB128Bytes(7);
2919 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00002920
Duncan Sands671fa972008-05-07 19:11:09 +00002921 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00002922 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00002923 Asm->EOL("Personality (pcrel sdata4 indirect)");
2924 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00002925 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00002926 Asm->EOL("Personality (pcrel sdata4)");
2927 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00002928
Duncan Sands671fa972008-05-07 19:11:09 +00002929 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00002930 O << TAI->getPersonalityPrefix();
2931 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
2932 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00002933 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
2934 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00002935 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00002936
Duncan Sands671fa972008-05-07 19:11:09 +00002937 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2938 Asm->EOL("LSDA Encoding (pcrel sdata4)");
2939 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2940 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00002941 } else {
2942 Asm->EmitULEB128Bytes(1);
2943 Asm->EOL("Augmentation Size");
Duncan Sands671fa972008-05-07 19:11:09 +00002944 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2945 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00002946 }
2947
2948 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002949 std::vector<MachineMove> Moves;
2950 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00002951 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002952
Dale Johannesen21d972a2008-04-30 00:43:29 +00002953 // On Darwin the linker honors the alignment of eh_frame, which means it
2954 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
2955 // you get holes which confuse readers of eh_frame.
Dale Johannesen7b251e02008-04-29 22:58:20 +00002956 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
2957 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002958 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00002959
Jim Laskeybacd3042007-02-21 22:48:45 +00002960 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00002961 }
Duncan Sands671fa972008-05-07 19:11:09 +00002962
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002963 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002964 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002965 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002966 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
2967
Jim Laskeybacd3042007-02-21 22:48:45 +00002968 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
2969
2970 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00002971 // If the corresponding function is static, this should not be
2972 // externally visible.
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002973 if (linkage != Function::InternalLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00002974 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
2975 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
2976 }
2977
Dale Johannesen038129d2008-01-10 02:03:30 +00002978 // If corresponding function is weak definition, this should be too.
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002979 if ((linkage == Function::WeakLinkage ||
2980 linkage == Function::LinkOnceLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00002981 TAI->getWeakDefDirective())
2982 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
2983
2984 // If there are no calls then you can't unwind. This may mean we can
2985 // omit the EH Frame, but some environments do not handle weak absolute
Dale Johannesen4e1b7942008-04-08 00:10:24 +00002986 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00002987 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00002988 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00002989 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00002990 !UnwindTablesMandatory &&
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002991 ((linkage != Function::WeakLinkage &&
2992 linkage != Function::LinkOnceLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00002993 !TAI->getWeakDefDirective() ||
2994 TAI->getSupportsWeakOmittedEHFrame()))
2995 {
Bill Wendling6e198962007-09-18 01:47:22 +00002996 O << EHFrameInfo.FnName << " = 0\n";
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002997 // This name has no connection to the function, so it might get
2998 // dead-stripped when the function is not, erroneously. Prohibit
2999 // dead-stripping unconditionally.
3000 if (const char *UsedDirective = TAI->getUsedDirective())
3001 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003002 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003003 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003004
Jim Laskeybacd3042007-02-21 22:48:45 +00003005 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003006 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3007 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003008 Asm->EOL("Length of Frame Information Entry");
3009
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003010 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003011
Anton Korobeynikov070280e2007-05-23 11:08:31 +00003012 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003013 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +00003014 true, true, false);
Jim Laskeybacd3042007-02-21 22:48:45 +00003015 Asm->EOL("FDE CIE offset");
3016
Duncan Sands671fa972008-05-07 19:11:09 +00003017 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003018 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003019 EmitDifference("eh_func_end", EHFrameInfo.Number,
Duncan Sands671fa972008-05-07 19:11:09 +00003020 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003021 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003022
Jim Laskey3f09fc22007-02-28 18:38:31 +00003023 // If there is a personality and landing pads then point to the language
3024 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003025 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003026 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003027 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003028
3029 if (EHFrameInfo.hasLandingPads)
3030 EmitReference("exception", EHFrameInfo.Number, true, true);
3031 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003032 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003033 Asm->EOL("Language Specific Data Area");
3034 } else {
3035 Asm->EmitULEB128Bytes(0);
3036 Asm->EOL("Augmentation size");
3037 }
Duncan Sands671fa972008-05-07 19:11:09 +00003038
Jim Laskeybacd3042007-02-21 22:48:45 +00003039 // Indicate locations of function specific callee saved registers in
3040 // frame.
Dale Johannesenb97aec62007-11-13 19:13:01 +00003041 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003042
Dale Johannesen21d972a2008-04-30 00:43:29 +00003043 // On Darwin the linker honors the alignment of eh_frame, which means it
3044 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3045 // you get holes which confuse readers of eh_frame.
Dale Johannesen7b251e02008-04-29 22:58:20 +00003046 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
3047 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003048 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003049
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003050 // If the function is marked used, this table should be also. We cannot
3051 // make the mark unconditional in this case, since retaining the table
3052 // also retains the function in this case, and there is code around
3053 // that depends on unused functions (calling undefined externals) being
3054 // dead-stripped to link correctly. Yes, there really is.
3055 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3056 if (const char *UsedDirective = TAI->getUsedDirective())
3057 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3058 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003059 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003060
Duncan Sands57810cd2007-09-05 11:27:52 +00003061 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003062 ///
3063 /// The general organization of the table is complex, but the basic concepts
3064 /// are easy. First there is a header which describes the location and
3065 /// organization of the three components that follow.
3066 /// 1. The landing pad site information describes the range of code covered
3067 /// by the try. In our case it's an accumulation of the ranges covered
3068 /// by the invokes in the try. There is also a reference to the landing
3069 /// pad that handles the exception once processed. Finally an index into
3070 /// the actions table.
3071 /// 2. The action table, in our case, is composed of pairs of type ids
3072 /// and next action offset. Starting with the action index from the
3073 /// landing pad site, each type Id is checked for a match to the current
3074 /// exception. If it matches then the exception and type id are passed
3075 /// on to the landing pad. Otherwise the next action is looked up. This
3076 /// chain is terminated with a next action of zero. If no type id is
3077 /// found the the frame is unwound and handling continues.
3078 /// 3. Type id table contains references to all the C++ typeinfo for all
3079 /// catches in the function. This tables is reversed indexed base 1.
3080
Duncan Sandsb32edb42007-06-06 15:37:31 +00003081 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3082 static unsigned SharedTypeIds(const LandingPadInfo *L,
3083 const LandingPadInfo *R) {
3084 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003085 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003086 unsigned MinSize = LSize < RSize ? LSize : RSize;
3087 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003088
Duncan Sandsb32edb42007-06-06 15:37:31 +00003089 for (; Count != MinSize; ++Count)
3090 if (LIds[Count] != RIds[Count])
3091 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003092
Duncan Sandsb32edb42007-06-06 15:37:31 +00003093 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003094 }
3095
Duncan Sandsb32edb42007-06-06 15:37:31 +00003096 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003097 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003098 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003099 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003100 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003101
Duncan Sandsb32edb42007-06-06 15:37:31 +00003102 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003103 if (LIds[i] != RIds[i])
3104 return LIds[i] < RIds[i];
3105
Duncan Sandsb32edb42007-06-06 15:37:31 +00003106 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003107 }
3108
Duncan Sands53c3a332007-05-16 12:12:23 +00003109 struct KeyInfo {
3110 static inline unsigned getEmptyKey() { return -1U; }
3111 static inline unsigned getTombstoneKey() { return -2U; }
3112 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003113 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003114 static bool isPod() { return true; }
3115 };
3116
Duncan Sands57810cd2007-09-05 11:27:52 +00003117 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003118 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003119 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003120 int NextAction;
3121 struct ActionEntry *Previous;
3122 };
3123
Duncan Sands57810cd2007-09-05 11:27:52 +00003124 /// PadRange - Structure holding a try-range and the associated landing pad.
3125 struct PadRange {
3126 // The index of the landing pad.
3127 unsigned PadIndex;
3128 // The index of the begin and end labels in the landing pad's label lists.
3129 unsigned RangeIndex;
3130 };
3131
3132 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3133
3134 /// CallSiteEntry - Structure describing an entry in the call-site table.
3135 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003136 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003137 unsigned BeginLabel; // zero indicates the start of the function.
3138 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003139 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003140 unsigned PadLabel; // zero indicates that there is no landing pad.
3141 unsigned Action;
3142 };
3143
Jim Laskeybacd3042007-02-21 22:48:45 +00003144 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003145 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003146 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003147 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3148 if (PadInfos.empty()) return;
3149
3150 // Sort the landing pads in order of their type ids. This is used to fold
3151 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003152 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003153 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003154 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003155 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003156 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3157
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003158 // Negative type ids index into FilterIds, positive type ids index into
3159 // TypeInfos. The value written for a positive type id is just the type
3160 // id itself. For a negative type id, however, the value written is the
3161 // (negative) byte offset of the corresponding FilterIds entry. The byte
3162 // offset is usually equal to the type id, because the FilterIds entries
3163 // are written using a variable width encoding which outputs one byte per
3164 // entry as long as the value written is not too large, but can differ.
3165 // This kind of complication does not occur for positive type ids because
3166 // type infos are output using a fixed width encoding.
3167 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3168 SmallVector<int, 16> FilterOffsets;
3169 FilterOffsets.reserve(FilterIds.size());
3170 int Offset = -1;
3171 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3172 E = FilterIds.end(); I != E; ++I) {
3173 FilterOffsets.push_back(Offset);
Duncan Sandsbb821dd2007-07-12 13:51:39 +00003174 Offset -= Asm->SizeULEB128(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003175 }
3176
Duncan Sands57810cd2007-09-05 11:27:52 +00003177 // Compute the actions table and gather the first action index for each
3178 // landing pad site.
3179 SmallVector<ActionEntry, 32> Actions;
3180 SmallVector<unsigned, 64> FirstActions;
3181 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003182
Duncan Sandsb32edb42007-06-06 15:37:31 +00003183 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003184 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003185 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003186 const LandingPadInfo *LP = LandingPads[i];
3187 const std::vector<int> &TypeIds = LP->TypeIds;
3188 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003189 unsigned SizeSiteActions = 0;
3190
Duncan Sandsb32edb42007-06-06 15:37:31 +00003191 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003192 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003193 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003194
Duncan Sandsb32edb42007-06-06 15:37:31 +00003195 if (NumShared) {
3196 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3197 assert(Actions.size());
3198 PrevAction = &Actions.back();
3199 SizeAction = Asm->SizeSLEB128(PrevAction->NextAction) +
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003200 Asm->SizeSLEB128(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003201 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003202 SizeAction -= Asm->SizeSLEB128(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003203 SizeAction += -PrevAction->NextAction;
3204 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003205 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003206 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003207
Duncan Sandsb32edb42007-06-06 15:37:31 +00003208 // Compute the actions.
3209 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3210 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003211 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3212 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
3213 unsigned SizeTypeID = Asm->SizeSLEB128(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003214
3215 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
3216 SizeAction = SizeTypeID + Asm->SizeSLEB128(NextAction);
3217 SizeSiteActions += SizeAction;
3218
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003219 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00003220 Actions.push_back(Action);
3221
3222 PrevAction = &Actions.back();
3223 }
3224
3225 // Record the first action of the landing pad site.
3226 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3227 } // else identical - re-use previous FirstAction
3228
3229 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003230
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00003231 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00003232 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00003233 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003234
Duncan Sands481dc722007-12-19 07:36:31 +00003235 // Compute the call-site table. The entry for an invoke has a try-range
3236 // containing the call, a non-zero landing pad and an appropriate action.
3237 // The entry for an ordinary call has a try-range containing the call and
3238 // zero for the landing pad and the action. Calls marked 'nounwind' have
3239 // no entry and must not be contained in the try-range of any entry - they
3240 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003241 SmallVector<CallSiteEntry, 64> CallSites;
3242
3243 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00003244 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3245 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3246 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00003247 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3248 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00003249 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003250 unsigned BeginLabel = LandingPad->BeginLabels[j];
3251 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3252 PadRange P = { i, j };
3253 PadMap[BeginLabel] = P;
3254 }
3255 }
3256
Duncan Sands481dc722007-12-19 07:36:31 +00003257 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00003258 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00003259
3260 // Whether there is a potentially throwing instruction (currently this means
3261 // an ordinary call) between the end of the previous try-range and now.
3262 bool SawPotentiallyThrowing = false;
3263
3264 // Whether the last callsite entry was for an invoke.
3265 bool PreviousIsInvoke = false;
3266
Duncan Sands481dc722007-12-19 07:36:31 +00003267 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003268 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3269 I != E; ++I) {
3270 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3271 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00003272 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00003273 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00003274 continue;
3275 }
3276
Chris Lattner9e330492007-12-30 20:50:28 +00003277 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00003278 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00003279
Duncan Sands481dc722007-12-19 07:36:31 +00003280 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00003281 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00003282 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003283
Duncan Sands481dc722007-12-19 07:36:31 +00003284 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00003285 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00003286 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00003287 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00003288 continue;
3289
3290 PadRange P = L->second;
3291 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3292
3293 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3294 "Inconsistent landing pad map!");
3295
3296 // If some instruction between the previous try-range and this one may
3297 // throw, create a call-site entry with no landing pad for the region
3298 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00003299 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003300 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3301 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00003302 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003303 }
3304
3305 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00003306 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00003307
Duncan Sands481dc722007-12-19 07:36:31 +00003308 if (LandingPad->LandingPadLabel) {
3309 // This try-range is for an invoke.
3310 CallSiteEntry Site = {BeginLabel, LastLabel,
3311 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00003312
Duncan Sands481dc722007-12-19 07:36:31 +00003313 // Try to merge with the previous call-site.
3314 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00003315 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00003316 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3317 // Extend the range of the previous entry.
3318 Prev.EndLabel = Site.EndLabel;
3319 continue;
3320 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003321 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003322
Duncan Sands481dc722007-12-19 07:36:31 +00003323 // Otherwise, create a new call-site.
3324 CallSites.push_back(Site);
3325 PreviousIsInvoke = true;
3326 } else {
3327 // Create a gap.
3328 PreviousIsInvoke = false;
3329 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003330 }
3331 }
3332 // If some instruction between the previous try-range and the end of the
3333 // function may throw, create a call-site entry with no landing pad for the
3334 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00003335 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003336 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3337 CallSites.push_back(Site);
3338 }
3339
Jim Laskeybacd3042007-02-21 22:48:45 +00003340 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00003341
3342 // Call sites.
3343 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3344 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3345 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3346 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3347 SiteLengthSize +
3348 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00003349 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
3350 SizeSites += Asm->SizeULEB128(CallSites[i].Action);
3351
Duncan Sands671fa972008-05-07 19:11:09 +00003352 // Type infos.
3353 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3354 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003355
3356 unsigned TypeOffset = sizeof(int8_t) + // Call site format
3357 Asm->SizeULEB128(SizeSites) + // Call-site table length
3358 SizeSites + SizeActions + SizeTypes;
3359
3360 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3361 sizeof(int8_t) + // TType format
3362 Asm->SizeULEB128(TypeOffset) + // TType base offset
3363 TypeOffset;
3364
3365 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003366
Jim Laskeybacd3042007-02-21 22:48:45 +00003367 // Begin the exception table.
3368 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
3369 O << "GCC_except_table" << SubprogramCount << ":\n";
Evan Cheng05548eb2008-02-29 19:36:59 +00003370 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003371 for (unsigned i = 0; i != SizeAlign; ++i) {
3372 Asm->EmitInt8(0);
3373 Asm->EOL("Padding");
3374 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003375 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003376
Jim Laskeybacd3042007-02-21 22:48:45 +00003377 // Emit the header.
3378 Asm->EmitInt8(DW_EH_PE_omit);
3379 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3380 Asm->EmitInt8(DW_EH_PE_absptr);
3381 Asm->EOL("TType format (DW_EH_PE_absptr)");
3382 Asm->EmitULEB128Bytes(TypeOffset);
3383 Asm->EOL("TType base offset");
3384 Asm->EmitInt8(DW_EH_PE_udata4);
3385 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3386 Asm->EmitULEB128Bytes(SizeSites);
3387 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003388
Duncan Sands57810cd2007-09-05 11:27:52 +00003389 // Emit the landing pad site information.
3390 for (unsigned i = 0; i < CallSites.size(); ++i) {
3391 CallSiteEntry &S = CallSites[i];
3392 const char *BeginTag;
3393 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00003394
Duncan Sands57810cd2007-09-05 11:27:52 +00003395 if (!S.BeginLabel) {
3396 BeginTag = "eh_func_begin";
3397 BeginNumber = SubprogramCount;
3398 } else {
3399 BeginTag = "label";
3400 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00003401 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003402
Duncan Sands57810cd2007-09-05 11:27:52 +00003403 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003404 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003405 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00003406
Duncan Sands57810cd2007-09-05 11:27:52 +00003407 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00003408 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00003409 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003410 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00003411 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003412 }
3413 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003414
Duncan Sands671fa972008-05-07 19:11:09 +00003415 if (!S.PadLabel)
3416 Asm->EmitInt32(0);
3417 else
Duncan Sands57810cd2007-09-05 11:27:52 +00003418 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003419 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003420 Asm->EOL("Landing pad");
3421
3422 Asm->EmitULEB128Bytes(S.Action);
3423 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003424 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003425
Jim Laskeybacd3042007-02-21 22:48:45 +00003426 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003427 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3428 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00003429
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003430 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003431 Asm->EOL("TypeInfo index");
3432 Asm->EmitSLEB128Bytes(Action.NextAction);
3433 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003434 }
3435
3436 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00003437 for (unsigned M = TypeInfos.size(); M; --M) {
3438 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00003439
3440 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00003441
3442 if (GV)
3443 O << Asm->getGlobalLinkName(GV);
3444 else
3445 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00003446
Jim Laskeybacd3042007-02-21 22:48:45 +00003447 Asm->EOL("TypeInfo");
3448 }
3449
Duncan Sands73ef58a2007-06-02 16:53:42 +00003450 // Emit the filter typeids.
3451 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3452 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00003453 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00003454 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00003455 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003456
Evan Cheng05548eb2008-02-29 19:36:59 +00003457 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003458 }
3459
Jim Laskey072200c2007-01-29 18:51:14 +00003460public:
3461 //===--------------------------------------------------------------------===//
3462 // Main entry points.
3463 //
3464 DwarfException(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00003465 : Dwarf(OS, A, T, "eh")
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003466 , shouldEmitTable(false)
3467 , shouldEmitMoves(false)
3468 , shouldEmitTableModule(false)
3469 , shouldEmitMovesModule(false)
Jim Laskey072200c2007-01-29 18:51:14 +00003470 {}
3471
3472 virtual ~DwarfException() {}
3473
3474 /// SetModuleInfo - Set machine module information when it's known that pass
3475 /// manager has created it. Set by the target AsmPrinter.
3476 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00003477 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00003478 }
3479
3480 /// BeginModule - Emit all exception information that should come prior to the
3481 /// content.
3482 void BeginModule(Module *M) {
3483 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00003484 }
3485
3486 /// EndModule - Emit all exception information that should come after the
3487 /// content.
3488 void EndModule() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003489 if (shouldEmitMovesModule || shouldEmitTableModule) {
3490 const std::vector<Function *> Personalities = MMI->getPersonalities();
3491 for (unsigned i =0; i < Personalities.size(); ++i)
3492 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003493
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003494 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3495 E = EHFrames.end(); I != E; ++I)
3496 EmitEHFrame(*I);
3497 }
Jim Laskey072200c2007-01-29 18:51:14 +00003498 }
3499
3500 /// BeginFunction - Gather pre-function exception information. Assumes being
3501 /// emitted immediately after the function entry point.
3502 void BeginFunction(MachineFunction *MF) {
3503 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003504 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesene0040622008-04-02 17:04:45 +00003505 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003506
3507 // Map all labels and get rid of any dead landing pads.
3508 MMI->TidyLandingPads();
3509 // If any landing pads survive, we need an EH table.
3510 if (MMI->getLandingPads().size())
3511 shouldEmitTable = true;
3512
3513 // See if we need frame move info.
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003514 if (MMI->hasDebugInfo() ||
3515 !MF->getFunction()->doesNotThrow() ||
Dale Johannesen3541af72008-04-14 17:54:17 +00003516 UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003517 shouldEmitMoves = true;
3518
3519 if (shouldEmitMoves || shouldEmitTable)
3520 // Assumes in correct section after the entry point.
3521 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00003522 }
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003523 shouldEmitTableModule |= shouldEmitTable;
3524 shouldEmitMovesModule |= shouldEmitMoves;
Jim Laskey072200c2007-01-29 18:51:14 +00003525 }
3526
3527 /// EndFunction - Gather and emit post-function exception information.
3528 ///
3529 void EndFunction() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003530 if (shouldEmitMoves || shouldEmitTable) {
3531 EmitLabel("eh_func_end", SubprogramCount);
3532 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003533
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003534 // Save EH frame information
3535 EHFrames.
3536 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling6e198962007-09-18 01:47:22 +00003537 SubprogramCount,
3538 MMI->getPersonalityIndex(),
3539 MF->getFrameInfo()->hasCalls(),
3540 !MMI->getLandingPads().empty(),
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003541 MMI->getFrameMoves(),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003542 MF->getFunction()));
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003543 }
Jim Laskey072200c2007-01-29 18:51:14 +00003544 }
3545};
3546
Jim Laskey0d086af2006-02-27 12:43:29 +00003547} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00003548
3549//===----------------------------------------------------------------------===//
3550
Jim Laskeyd18e2892006-01-20 20:34:06 +00003551/// Emit - Print the abbreviation using the specified Dwarf writer.
3552///
Jim Laskey072200c2007-01-29 18:51:14 +00003553void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003554 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00003555 DD.getAsm()->EmitULEB128Bytes(Tag);
3556 DD.getAsm()->EOL(TagString(Tag));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003557
3558 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00003559 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3560 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003561
3562 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00003563 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003564 const DIEAbbrevData &AttrData = Data[i];
3565
3566 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00003567 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3568 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003569
3570 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00003571 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3572 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003573 }
3574
3575 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00003576 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3577 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00003578}
3579
3580#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00003581void DIEAbbrev::print(std::ostream &O) {
3582 O << "Abbreviation @"
3583 << std::hex << (intptr_t)this << std::dec
3584 << " "
3585 << TagString(Tag)
3586 << " "
3587 << ChildrenString(ChildrenFlag)
3588 << "\n";
3589
3590 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3591 O << " "
3592 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003593 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00003594 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003595 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00003596 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00003597}
Bill Wendlinge8156192006-12-07 01:30:32 +00003598void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00003599#endif
3600
3601//===----------------------------------------------------------------------===//
3602
Jim Laskeyef42a012006-11-02 20:12:39 +00003603#ifndef NDEBUG
3604void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003605 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003606}
Jim Laskeyef42a012006-11-02 20:12:39 +00003607#endif
3608
3609//===----------------------------------------------------------------------===//
3610
Jim Laskey063e7652006-01-17 17:31:53 +00003611/// EmitValue - Emit integer of appropriate size.
3612///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003613void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00003614 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00003615 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00003616 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003617 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003618 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003619 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003620 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003621 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003622 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003623 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
3624 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
3625 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003626 default: assert(0 && "DIE Value form not supported yet"); break;
3627 }
3628}
3629
3630/// SizeOf - Determine size of integer value in bytes.
3631///
Jim Laskey072200c2007-01-29 18:51:14 +00003632unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003633 switch (Form) {
3634 case DW_FORM_flag: // Fall thru
3635 case DW_FORM_ref1: // Fall thru
3636 case DW_FORM_data1: return sizeof(int8_t);
3637 case DW_FORM_ref2: // Fall thru
3638 case DW_FORM_data2: return sizeof(int16_t);
3639 case DW_FORM_ref4: // Fall thru
3640 case DW_FORM_data4: return sizeof(int32_t);
3641 case DW_FORM_ref8: // Fall thru
3642 case DW_FORM_data8: return sizeof(int64_t);
Jim Laskey072200c2007-01-29 18:51:14 +00003643 case DW_FORM_udata: return DD.getAsm()->SizeULEB128(Integer);
3644 case DW_FORM_sdata: return DD.getAsm()->SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00003645 default: assert(0 && "DIE Value form not supported yet"); break;
3646 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003647 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00003648}
3649
Jim Laskey063e7652006-01-17 17:31:53 +00003650//===----------------------------------------------------------------------===//
3651
3652/// EmitValue - Emit string value.
3653///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003654void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003655 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00003656}
3657
Jim Laskey063e7652006-01-17 17:31:53 +00003658//===----------------------------------------------------------------------===//
3659
3660/// EmitValue - Emit label value.
3661///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003662void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003663 bool IsSmall = Form == DW_FORM_data4;
3664 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00003665}
3666
3667/// SizeOf - Determine size of label value in bytes.
3668///
Jim Laskey072200c2007-01-29 18:51:14 +00003669unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003670 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003671 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00003672}
Jim Laskeyef42a012006-11-02 20:12:39 +00003673
Jim Laskey063e7652006-01-17 17:31:53 +00003674//===----------------------------------------------------------------------===//
3675
Jim Laskeyd18e2892006-01-20 20:34:06 +00003676/// EmitValue - Emit label value.
3677///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003678void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003679 bool IsSmall = Form == DW_FORM_data4;
3680 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00003681}
3682
3683/// SizeOf - Determine size of label value in bytes.
3684///
Jim Laskey072200c2007-01-29 18:51:14 +00003685unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003686 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003687 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00003688}
3689
3690//===----------------------------------------------------------------------===//
3691
Jim Laskey063e7652006-01-17 17:31:53 +00003692/// EmitValue - Emit delta value.
3693///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00003694void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
3695 bool IsSmall = Form == DW_FORM_data4;
3696 DD.EmitSectionOffset(Label.Tag, Section.Tag,
3697 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
3698}
3699
3700/// SizeOf - Determine size of delta value in bytes.
3701///
3702unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3703 if (Form == DW_FORM_data4) return 4;
3704 return DD.getTargetData()->getPointerSize();
3705}
3706
3707//===----------------------------------------------------------------------===//
3708
3709/// EmitValue - Emit delta value.
3710///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003711void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003712 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00003713 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00003714}
3715
3716/// SizeOf - Determine size of delta value in bytes.
3717///
Jim Laskey072200c2007-01-29 18:51:14 +00003718unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003719 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003720 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00003721}
3722
3723//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003724
Jim Laskeyb8509c52006-03-23 18:07:55 +00003725/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003726///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003727void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003728 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00003729}
Jim Laskeyd18e2892006-01-20 20:34:06 +00003730
3731//===----------------------------------------------------------------------===//
3732
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003733/// ComputeSize - calculate the size of the block.
3734///
Jim Laskey072200c2007-01-29 18:51:14 +00003735unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003736 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00003737 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00003738
3739 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00003740 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00003741 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003742 }
3743 return Size;
3744}
3745
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003746/// EmitValue - Emit block data.
3747///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003748void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003749 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003750 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
3751 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
3752 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
3753 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003754 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003755 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003756
Owen Anderson873e1b52008-06-24 21:44:59 +00003757 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00003758
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003759 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003760 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00003761 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003762 }
3763}
3764
3765/// SizeOf - Determine size of block data in bytes.
3766///
Jim Laskey072200c2007-01-29 18:51:14 +00003767unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003768 switch (Form) {
3769 case DW_FORM_block1: return Size + sizeof(int8_t);
3770 case DW_FORM_block2: return Size + sizeof(int16_t);
3771 case DW_FORM_block4: return Size + sizeof(int32_t);
Jim Laskey072200c2007-01-29 18:51:14 +00003772 case DW_FORM_block: return Size + DD.getAsm()->SizeULEB128(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003773 default: assert(0 && "Improper form for block"); break;
3774 }
3775 return 0;
3776}
3777
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003778//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003779/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00003780
3781DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003782 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00003783 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00003784}
Jim Laskeyef42a012006-11-02 20:12:39 +00003785
Jim Laskeyb8509c52006-03-23 18:07:55 +00003786/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
3787///
3788void DIE::AddSiblingOffset() {
3789 DIEInteger *DI = new DIEInteger(0);
3790 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00003791 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00003792}
3793
Jim Laskeyef42a012006-11-02 20:12:39 +00003794/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003795///
Jim Laskeyef42a012006-11-02 20:12:39 +00003796void DIE::Profile(FoldingSetNodeID &ID) {
3797 Abbrev.Profile(ID);
3798
3799 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3800 ID.AddPointer(Children[i]);
3801
3802 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3803 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003804}
Jim Laskeyef42a012006-11-02 20:12:39 +00003805
3806#ifndef NDEBUG
3807void DIE::print(std::ostream &O, unsigned IncIndent) {
3808 static unsigned IndentCount = 0;
3809 IndentCount += IncIndent;
3810 const std::string Indent(IndentCount, ' ');
3811 bool isBlock = Abbrev.getTag() == 0;
3812
3813 if (!isBlock) {
3814 O << Indent
3815 << "Die: "
3816 << "0x" << std::hex << (intptr_t)this << std::dec
3817 << ", Offset: " << Offset
3818 << ", Size: " << Size
3819 << "\n";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003820
Jim Laskeyef42a012006-11-02 20:12:39 +00003821 O << Indent
3822 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00003823 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00003824 << ChildrenString(Abbrev.getChildrenFlag());
3825 } else {
3826 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00003827 }
3828 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003829
Owen Anderson873e1b52008-06-24 21:44:59 +00003830 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003831
Jim Laskeyef42a012006-11-02 20:12:39 +00003832 IndentCount += 2;
3833 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3834 O << Indent;
3835 if (!isBlock) {
3836 O << AttributeString(Data[i].getAttribute());
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003837 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00003838 O << "Blk[" << i << "]";
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003839 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003840 O << " "
3841 << FormEncodingString(Data[i].getForm())
3842 << " ";
3843 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00003844 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00003845 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003846 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00003847
Jim Laskeyef42a012006-11-02 20:12:39 +00003848 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3849 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00003850 }
Jim Laskey063e7652006-01-17 17:31:53 +00003851
Jim Laskeyef42a012006-11-02 20:12:39 +00003852 if (!isBlock) O << "\n";
3853 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00003854}
3855
Jim Laskeyef42a012006-11-02 20:12:39 +00003856void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003857 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00003858}
Jim Laskeybd761842006-02-27 17:27:12 +00003859#endif
Jim Laskey65195462006-10-30 13:35:07 +00003860
3861//===----------------------------------------------------------------------===//
3862/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00003863///
Jim Laskey65195462006-10-30 13:35:07 +00003864
3865DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3866 const TargetAsmInfo *T) {
Jim Laskey072200c2007-01-29 18:51:14 +00003867 DE = new DwarfException(OS, A, T);
3868 DD = new DwarfDebug(OS, A, T);
Jim Laskey65195462006-10-30 13:35:07 +00003869}
3870
3871DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00003872 delete DE;
3873 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00003874}
3875
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003876/// SetModuleInfo - Set machine module info when it's known that pass manager
3877/// has created it. Set by the target AsmPrinter.
3878void DwarfWriter::SetModuleInfo(MachineModuleInfo *MMI) {
Jim Laskey072200c2007-01-29 18:51:14 +00003879 DD->SetModuleInfo(MMI);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003880 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00003881}
3882
3883/// BeginModule - Emit all Dwarf sections that should come prior to the
3884/// content.
3885void DwarfWriter::BeginModule(Module *M) {
Jim Laskey072200c2007-01-29 18:51:14 +00003886 DE->BeginModule(M);
3887 DD->BeginModule(M);
Jim Laskey65195462006-10-30 13:35:07 +00003888}
3889
3890/// EndModule - Emit all Dwarf sections that should come after the content.
3891///
3892void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00003893 DE->EndModule();
3894 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00003895}
3896
3897/// BeginFunction - Gather pre-function debug information. Assumes being
3898/// emitted immediately after the function entry point.
3899void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00003900 DE->BeginFunction(MF);
3901 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00003902}
3903
3904/// EndFunction - Gather and emit post-function debug information.
3905///
3906void DwarfWriter::EndFunction() {
Jim Laskey072200c2007-01-29 18:51:14 +00003907 DD->EndFunction();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003908 DE->EndFunction();
3909
Jim Laskey3f09fc22007-02-28 18:38:31 +00003910 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00003911 // Clear function debug information.
3912 MMI->EndFunction();
3913 }
Jim Laskey65195462006-10-30 13:35:07 +00003914}