blob: 5d6acc30ba812041e1e2640aeec47775383a6420 [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"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey41886992006-04-07 16:34:46 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyb8509c52006-03-23 18:07:55 +000025#include "llvm/CodeGen/MachineLocation.h"
Jim Laskey3f09fc22007-02-28 18:38:31 +000026#include "llvm/Support/Debug.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000027#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000028#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000029#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000030#include "llvm/Support/Mangler.h"
Dan Gohman85496362007-09-24 21:32:18 +000031#include "llvm/System/Path.h"
Jim Laskey563321a2006-09-06 18:34:40 +000032#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000033#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000034#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000035#include "llvm/Target/TargetFrameInfo.h"
Duncan Sands53c3a332007-05-16 12:12:23 +000036#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000037#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000038#include "llvm/Target/TargetOptions.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000039#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000040#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000041using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000042using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000043
Jim Laskey0d086af2006-02-27 12:43:29 +000044namespace llvm {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000045
Jim Laskey65195462006-10-30 13:35:07 +000046//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000047
48/// Configuration values for initial hash set sizes (log2).
49///
50static const unsigned InitDiesSetSize = 9; // 512
51static const unsigned InitAbbreviationsSetSize = 9; // 512
52static const unsigned InitValuesSetSize = 9; // 512
53
54//===----------------------------------------------------------------------===//
55/// Forward declarations.
56///
57class DIE;
58class DIEValue;
59
60//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000061/// DWLabel - Labels are used to track locations in the assembler file.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000062/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
63/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer181b6c92007-08-05 20:06:04 +000064/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +000065class DWLabel {
66public:
Jim Laskeyef42a012006-11-02 20:12:39 +000067 /// Tag - Label category tag. Should always be a staticly declared C string.
68 ///
69 const char *Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000070
Jim Laskeyef42a012006-11-02 20:12:39 +000071 /// Number - Value to make label unique.
72 ///
73 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +000074
75 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000076
Jim Laskeyef42a012006-11-02 20:12:39 +000077 void Profile(FoldingSetNodeID &ID) const {
78 ID.AddString(std::string(Tag));
79 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +000080 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000081
Jim Laskeyef42a012006-11-02 20:12:39 +000082#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +000083 void print(std::ostream *O) const {
84 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +000085 }
Jim Laskeyef42a012006-11-02 20:12:39 +000086 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +000087 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +000088 if (Number) O << Number;
89 }
90#endif
Jim Laskeybd761842006-02-27 17:27:12 +000091};
92
93//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000094/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
95/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +000096class DIEAbbrevData {
97private:
Jim Laskeyef42a012006-11-02 20:12:39 +000098 /// Attribute - Dwarf attribute code.
99 ///
100 unsigned Attribute;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000101
Jim Laskeyef42a012006-11-02 20:12:39 +0000102 /// Form - Dwarf form code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000103 ///
104 unsigned Form;
105
Jim Laskey0d086af2006-02-27 12:43:29 +0000106public:
107 DIEAbbrevData(unsigned A, unsigned F)
108 : Attribute(A)
109 , Form(F)
110 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000111
Jim Laskeybd761842006-02-27 17:27:12 +0000112 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000113 unsigned getAttribute() const { return Attribute; }
114 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000115
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000116 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000117 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000118 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000119 ID.AddInteger(Attribute);
120 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000121 }
122};
Jim Laskey063e7652006-01-17 17:31:53 +0000123
Jim Laskey0d086af2006-02-27 12:43:29 +0000124//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000125/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
126/// information object.
127class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000128private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000129 /// Tag - Dwarf tag code.
130 ///
131 unsigned Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000132
Jim Laskeyef42a012006-11-02 20:12:39 +0000133 /// Unique number for node.
134 ///
135 unsigned Number;
136
137 /// ChildrenFlag - Dwarf children flag.
138 ///
139 unsigned ChildrenFlag;
140
141 /// Data - Raw data bytes for abbreviation.
142 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000143 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000144
Jim Laskey0d086af2006-02-27 12:43:29 +0000145public:
Jim Laskey063e7652006-01-17 17:31:53 +0000146
Jim Laskey0d086af2006-02-27 12:43:29 +0000147 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000148 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000149 , ChildrenFlag(C)
150 , Data()
151 {}
152 ~DIEAbbrev() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000153
Jim Laskeybd761842006-02-27 17:27:12 +0000154 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000155 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000156 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000157 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000158 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000159 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000160 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000161 void setNumber(unsigned N) { Number = N; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000162
Jim Laskey0d086af2006-02-27 12:43:29 +0000163 /// AddAttribute - Adds another set of attribute information to the
164 /// abbreviation.
165 void AddAttribute(unsigned Attribute, unsigned Form) {
166 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000167 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000168
Jim Laskeyb8509c52006-03-23 18:07:55 +0000169 /// AddFirstAttribute - Adds a set of attribute information to the front
170 /// of the abbreviation.
171 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
172 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
173 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000174
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000175 /// Profile - Used to gather unique data for the abbreviation folding set.
176 ///
177 void Profile(FoldingSetNodeID &ID) {
178 ID.AddInteger(Tag);
179 ID.AddInteger(ChildrenFlag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000180
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000181 // For each attribute description.
182 for (unsigned i = 0, N = Data.size(); i < N; ++i)
183 Data[i].Profile(ID);
184 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000185
Jim Laskey0d086af2006-02-27 12:43:29 +0000186 /// Emit - Print the abbreviation using the specified Dwarf writer.
187 ///
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000188 void Emit(const DwarfDebug &DD) const;
189
Jim Laskey0d086af2006-02-27 12:43:29 +0000190#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000191 void print(std::ostream *O) {
192 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000193 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000194 void print(std::ostream &O);
195 void dump();
196#endif
197};
Jim Laskey063e7652006-01-17 17:31:53 +0000198
Jim Laskey0d086af2006-02-27 12:43:29 +0000199//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000200/// DIE - A structured debug information entry. Has an abbreviation which
201/// describes it's organization.
202class DIE : public FoldingSetNode {
203protected:
204 /// Abbrev - Buffer for constructing abbreviation.
205 ///
206 DIEAbbrev Abbrev;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000207
Jim Laskeyef42a012006-11-02 20:12:39 +0000208 /// Offset - Offset in debug info section.
209 ///
210 unsigned Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000211
Jim Laskeyef42a012006-11-02 20:12:39 +0000212 /// Size - Size of instance + children.
213 ///
214 unsigned Size;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000215
Jim Laskeyef42a012006-11-02 20:12:39 +0000216 /// Children DIEs.
217 ///
218 std::vector<DIE *> Children;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000219
Jim Laskeyef42a012006-11-02 20:12:39 +0000220 /// Attributes values.
221 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000222 SmallVector<DIEValue*, 32> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000223
Jim Laskeyef42a012006-11-02 20:12:39 +0000224public:
Dan Gohman81975f62007-08-27 14:50:10 +0000225 explicit DIE(unsigned Tag)
Jim Laskeyef42a012006-11-02 20:12:39 +0000226 : Abbrev(Tag, DW_CHILDREN_no)
227 , Offset(0)
228 , Size(0)
229 , Children()
230 , Values()
231 {}
232 virtual ~DIE();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000233
Jim Laskeyef42a012006-11-02 20:12:39 +0000234 // Accessors.
235 DIEAbbrev &getAbbrev() { return Abbrev; }
236 unsigned getAbbrevNumber() const {
237 return Abbrev.getNumber();
238 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000239 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000240 unsigned getOffset() const { return Offset; }
241 unsigned getSize() const { return Size; }
242 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000243 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000244 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
245 void setOffset(unsigned O) { Offset = O; }
246 void setSize(unsigned S) { Size = S; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000247
Jim Laskeyef42a012006-11-02 20:12:39 +0000248 /// AddValue - Add a value and attributes to a DIE.
249 ///
250 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
251 Abbrev.AddAttribute(Attribute, Form);
252 Values.push_back(Value);
253 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000254
Jim Laskeyef42a012006-11-02 20:12:39 +0000255 /// SiblingOffset - Return the offset of the debug information entry's
256 /// sibling.
257 unsigned SiblingOffset() const { return Offset + Size; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000258
Jim Laskeyef42a012006-11-02 20:12:39 +0000259 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
260 ///
261 void AddSiblingOffset();
262
263 /// AddChild - Add a child to the DIE.
264 ///
265 void AddChild(DIE *Child) {
266 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
267 Children.push_back(Child);
268 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000269
Jim Laskeyef42a012006-11-02 20:12:39 +0000270 /// Detach - Detaches objects connected to it after copying.
271 ///
272 void Detach() {
273 Children.clear();
274 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000275
Jim Laskeyef42a012006-11-02 20:12:39 +0000276 /// Profile - Used to gather unique data for the value folding set.
277 ///
278 void Profile(FoldingSetNodeID &ID) ;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000279
Jim Laskeyef42a012006-11-02 20:12:39 +0000280#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000281 void print(std::ostream *O, unsigned IncIndent = 0) {
282 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000283 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000284 void print(std::ostream &O, unsigned IncIndent = 0);
285 void dump();
286#endif
287};
288
289//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000290/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000291///
292class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000293public:
294 enum {
295 isInteger,
296 isString,
297 isLabel,
298 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000299 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000300 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000301 isEntry,
302 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000303 };
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000304
Jim Laskeyef42a012006-11-02 20:12:39 +0000305 /// Type - Type of data stored in the value.
306 ///
307 unsigned Type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000308
Dan Gohman81975f62007-08-27 14:50:10 +0000309 explicit DIEValue(unsigned T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000310 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000311 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000312 virtual ~DIEValue() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000313
Jim Laskeyf6733882006-11-02 21:48:18 +0000314 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000315 unsigned getType() const { return Type; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000316
Jim Laskey0d086af2006-02-27 12:43:29 +0000317 // Implement isa/cast/dyncast.
318 static bool classof(const DIEValue *) { return true; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000319
Jim Laskey0d086af2006-02-27 12:43:29 +0000320 /// EmitValue - Emit value via the Dwarf writer.
321 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000322 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000323
Jim Laskey0d086af2006-02-27 12:43:29 +0000324 /// SizeOf - Return the size of a value in bytes.
325 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000326 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000327
Jim Laskeyef42a012006-11-02 20:12:39 +0000328 /// Profile - Used to gather unique data for the value folding set.
329 ///
330 virtual void Profile(FoldingSetNodeID &ID) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000331
Jim Laskeyef42a012006-11-02 20:12:39 +0000332#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000333 void print(std::ostream *O) {
334 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000335 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000336 virtual void print(std::ostream &O) = 0;
337 void dump();
338#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000339};
Jim Laskey063e7652006-01-17 17:31:53 +0000340
Jim Laskey0d086af2006-02-27 12:43:29 +0000341//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000342/// DWInteger - An integer value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000343///
Jim Laskey0d086af2006-02-27 12:43:29 +0000344class DIEInteger : public DIEValue {
345private:
346 uint64_t Integer;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000347
Jim Laskey0d086af2006-02-27 12:43:29 +0000348public:
Dan Gohman81975f62007-08-27 14:50:10 +0000349 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000350
Jim Laskey0d086af2006-02-27 12:43:29 +0000351 // Implement isa/cast/dyncast.
352 static bool classof(const DIEInteger *) { return true; }
353 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000354
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000355 /// BestForm - Choose the best form for integer.
356 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000357 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
358 if (IsSigned) {
359 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
360 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
361 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
362 } else {
363 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
364 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
365 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
366 }
367 return DW_FORM_data8;
368 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000369
Jim Laskey0d086af2006-02-27 12:43:29 +0000370 /// EmitValue - Emit integer of appropriate size.
371 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000372 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000373
Jim Laskey0d086af2006-02-27 12:43:29 +0000374 /// SizeOf - Determine size of integer value in bytes.
375 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000376 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000377
Jim Laskeyef42a012006-11-02 20:12:39 +0000378 /// Profile - Used to gather unique data for the value folding set.
379 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000380 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000381 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000382 ID.AddInteger(Integer);
383 }
Jim Laskey5496f012006-11-09 14:52:14 +0000384 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000385
Jim Laskeyef42a012006-11-02 20:12:39 +0000386#ifndef NDEBUG
387 virtual void print(std::ostream &O) {
388 O << "Int: " << (int64_t)Integer
389 << " 0x" << std::hex << Integer << std::dec;
390 }
391#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000392};
Jim Laskey063e7652006-01-17 17:31:53 +0000393
Jim Laskey0d086af2006-02-27 12:43:29 +0000394//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000395/// DIEString - A string value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000396///
Jim Laskeyef42a012006-11-02 20:12:39 +0000397class DIEString : public DIEValue {
398public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000399 const std::string String;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000400
Dan Gohman81975f62007-08-27 14:50:10 +0000401 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000402
Jim Laskey0d086af2006-02-27 12:43:29 +0000403 // Implement isa/cast/dyncast.
404 static bool classof(const DIEString *) { return true; }
405 static bool classof(const DIEValue *S) { return S->Type == isString; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000406
Jim Laskey0d086af2006-02-27 12:43:29 +0000407 /// EmitValue - Emit string value.
408 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000409 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000410
Jim Laskey0d086af2006-02-27 12:43:29 +0000411 /// SizeOf - Determine size of string value in bytes.
412 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000413 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000414 return String.size() + sizeof(char); // sizeof('\0');
415 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000416
Jim Laskeyef42a012006-11-02 20:12:39 +0000417 /// Profile - Used to gather unique data for the value folding set.
418 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000419 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000420 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000421 ID.AddString(String);
422 }
Jim Laskey5496f012006-11-09 14:52:14 +0000423 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000424
Jim Laskeyef42a012006-11-02 20:12:39 +0000425#ifndef NDEBUG
426 virtual void print(std::ostream &O) {
427 O << "Str: \"" << String << "\"";
428 }
429#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000430};
Jim Laskey063e7652006-01-17 17:31:53 +0000431
Jim Laskey0d086af2006-02-27 12:43:29 +0000432//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000433/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000434//
Jim Laskeyef42a012006-11-02 20:12:39 +0000435class DIEDwarfLabel : public DIEValue {
436public:
437
Jim Laskey0d086af2006-02-27 12:43:29 +0000438 const DWLabel Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000439
Dan Gohman81975f62007-08-27 14:50:10 +0000440 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000441
Jim Laskey0d086af2006-02-27 12:43:29 +0000442 // Implement isa/cast/dyncast.
443 static bool classof(const DIEDwarfLabel *) { return true; }
444 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000445
Jim Laskey0d086af2006-02-27 12:43:29 +0000446 /// EmitValue - Emit label value.
447 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000448 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000449
Jim Laskey0d086af2006-02-27 12:43:29 +0000450 /// SizeOf - Determine size of label value in bytes.
451 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000452 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000453
Jim Laskeyef42a012006-11-02 20:12:39 +0000454 /// Profile - Used to gather unique data for the value folding set.
455 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000456 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000457 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000458 Label.Profile(ID);
459 }
Jim Laskey5496f012006-11-09 14:52:14 +0000460 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000461
Jim Laskeyef42a012006-11-02 20:12:39 +0000462#ifndef NDEBUG
463 virtual void print(std::ostream &O) {
464 O << "Lbl: ";
465 Label.print(O);
466 }
467#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000468};
Jim Laskey063e7652006-01-17 17:31:53 +0000469
Jim Laskey063e7652006-01-17 17:31:53 +0000470
Jim Laskey0d086af2006-02-27 12:43:29 +0000471//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000472/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000473//
Jim Laskeyef42a012006-11-02 20:12:39 +0000474class DIEObjectLabel : public DIEValue {
475public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000476 const std::string Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000477
Dan Gohman81975f62007-08-27 14:50:10 +0000478 explicit DIEObjectLabel(const std::string &L)
479 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000480
Jim Laskey0d086af2006-02-27 12:43:29 +0000481 // Implement isa/cast/dyncast.
482 static bool classof(const DIEObjectLabel *) { return true; }
483 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000484
Jim Laskey0d086af2006-02-27 12:43:29 +0000485 /// EmitValue - Emit label value.
486 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000487 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000488
Jim Laskey0d086af2006-02-27 12:43:29 +0000489 /// SizeOf - Determine size of label value in bytes.
490 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000491 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000492
Jim Laskeyef42a012006-11-02 20:12:39 +0000493 /// Profile - Used to gather unique data for the value folding set.
494 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000495 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000496 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000497 ID.AddString(Label);
498 }
Jim Laskey5496f012006-11-09 14:52:14 +0000499 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000500
501#ifndef NDEBUG
502 virtual void print(std::ostream &O) {
503 O << "Obj: " << Label;
504 }
505#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000506};
Jim Laskey063e7652006-01-17 17:31:53 +0000507
Jim Laskey0d086af2006-02-27 12:43:29 +0000508//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000509/// DIESectionOffset - A section offset DIE.
510//
511class DIESectionOffset : public DIEValue {
512public:
513 const DWLabel Label;
514 const DWLabel Section;
515 bool IsEH : 1;
516 bool UseSet : 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000517
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000518 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
519 bool isEH = false, bool useSet = true)
520 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
521 IsEH(isEH), UseSet(useSet) {}
522
523 // Implement isa/cast/dyncast.
524 static bool classof(const DIESectionOffset *) { return true; }
525 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000526
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000527 /// EmitValue - Emit section offset.
528 ///
529 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000530
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000531 /// SizeOf - Determine size of section offset value in bytes.
532 ///
533 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000534
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000535 /// Profile - Used to gather unique data for the value folding set.
536 ///
537 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
538 const DWLabel &Section) {
539 ID.AddInteger(isSectionOffset);
540 Label.Profile(ID);
541 Section.Profile(ID);
542 // IsEH and UseSet are specific to the Label/Section that we will emit
543 // the offset for; so Label/Section are enough for uniqueness.
544 }
545 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
546
547#ifndef NDEBUG
548 virtual void print(std::ostream &O) {
549 O << "Off: ";
550 Label.print(O);
551 O << "-";
552 Section.print(O);
553 O << "-" << IsEH << "-" << UseSet;
554 }
555#endif
556};
557
558//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000559/// DIEDelta - A simple label difference DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000560///
Jim Laskeyef42a012006-11-02 20:12:39 +0000561class DIEDelta : public DIEValue {
562public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000563 const DWLabel LabelHi;
564 const DWLabel LabelLo;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000565
Jim Laskey0d086af2006-02-27 12:43:29 +0000566 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
567 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000568
Jim Laskey0d086af2006-02-27 12:43:29 +0000569 // Implement isa/cast/dyncast.
570 static bool classof(const DIEDelta *) { return true; }
571 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000572
Jim Laskey0d086af2006-02-27 12:43:29 +0000573 /// EmitValue - Emit delta value.
574 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000575 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000576
Jim Laskey0d086af2006-02-27 12:43:29 +0000577 /// SizeOf - Determine size of delta value in bytes.
578 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000579 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000580
Jim Laskeyef42a012006-11-02 20:12:39 +0000581 /// Profile - Used to gather unique data for the value folding set.
582 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000583 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
584 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000585 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000586 LabelHi.Profile(ID);
587 LabelLo.Profile(ID);
588 }
Jim Laskey5496f012006-11-09 14:52:14 +0000589 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000590
591#ifndef NDEBUG
592 virtual void print(std::ostream &O) {
593 O << "Del: ";
594 LabelHi.print(O);
595 O << "-";
596 LabelLo.print(O);
597 }
598#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000599};
Jim Laskey063e7652006-01-17 17:31:53 +0000600
Jim Laskey0d086af2006-02-27 12:43:29 +0000601//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000602/// DIEntry - A pointer to another debug information entry. An instance of this
603/// class can also be used as a proxy for a debug information entry not yet
604/// defined (ie. types.)
605class DIEntry : public DIEValue {
606public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000607 DIE *Entry;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000608
Dan Gohman81975f62007-08-27 14:50:10 +0000609 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000610
Jim Laskey0d086af2006-02-27 12:43:29 +0000611 // Implement isa/cast/dyncast.
612 static bool classof(const DIEntry *) { return true; }
613 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000614
Jim Laskeyb8509c52006-03-23 18:07:55 +0000615 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000616 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000617 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000618
Jim Laskeyb8509c52006-03-23 18:07:55 +0000619 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000620 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000621 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000622 return sizeof(int32_t);
623 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000624
Jim Laskeyef42a012006-11-02 20:12:39 +0000625 /// Profile - Used to gather unique data for the value folding set.
626 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000627 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
628 ID.AddInteger(isEntry);
629 ID.AddPointer(Entry);
630 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000631 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000632 ID.AddInteger(isEntry);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000633
Jim Laskeyef42a012006-11-02 20:12:39 +0000634 if (Entry) {
635 ID.AddPointer(Entry);
636 } else {
637 ID.AddPointer(this);
638 }
639 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000640
Jim Laskeyef42a012006-11-02 20:12:39 +0000641#ifndef NDEBUG
642 virtual void print(std::ostream &O) {
643 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
644 }
645#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000646};
647
648//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000649/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000650//
Jim Laskeyef42a012006-11-02 20:12:39 +0000651class DIEBlock : public DIEValue, public DIE {
652public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000653 unsigned Size; // Size in bytes excluding size header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000654
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000655 DIEBlock()
656 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000657 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000658 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000659 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000660 ~DIEBlock() {
661 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000662
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000663 // Implement isa/cast/dyncast.
664 static bool classof(const DIEBlock *) { return true; }
665 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000666
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000667 /// ComputeSize - calculate the size of the block.
668 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000669 unsigned ComputeSize(DwarfDebug &DD);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000670
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000671 /// BestForm - Choose the best form for data.
672 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000673 unsigned BestForm() const {
674 if ((unsigned char)Size == Size) return DW_FORM_block1;
675 if ((unsigned short)Size == Size) return DW_FORM_block2;
676 if ((unsigned int)Size == Size) return DW_FORM_block4;
677 return DW_FORM_block;
678 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000679
680 /// EmitValue - Emit block data.
681 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000682 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000683
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000684 /// SizeOf - Determine size of block data in bytes.
685 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000686 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000687
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000688
Jim Laskeyef42a012006-11-02 20:12:39 +0000689 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000690 ///
Reid Spencer97821312006-11-02 23:56:21 +0000691 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000692 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000693 DIE::Profile(ID);
694 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000695
Jim Laskeyef42a012006-11-02 20:12:39 +0000696#ifndef NDEBUG
697 virtual void print(std::ostream &O) {
698 O << "Blk: ";
699 DIE::print(O, 5);
700 }
701#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000702};
703
704//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000705/// CompileUnit - This dwarf writer support class manages information associate
706/// with a source file.
707class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000708private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000709 /// Desc - Compile unit debug descriptor.
710 ///
711 CompileUnitDesc *Desc;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000712
Jim Laskeyef42a012006-11-02 20:12:39 +0000713 /// ID - File identifier for source.
714 ///
715 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000716
Jim Laskeyef42a012006-11-02 20:12:39 +0000717 /// Die - Compile unit debug information entry.
718 ///
719 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000720
Jim Laskeyef42a012006-11-02 20:12:39 +0000721 /// DescToDieMap - Tracks the mapping of unit level debug informaton
722 /// descriptors to debug information entries.
723 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
724
725 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
726 /// descriptors to debug information entries using a DIEntry proxy.
727 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
728
729 /// Globals - A map of globally visible named entities for this unit.
730 ///
731 std::map<std::string, DIE *> Globals;
732
733 /// DiesSet - Used to uniquely define dies within the compile unit.
734 ///
735 FoldingSet<DIE> DiesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000736
Jim Laskeyef42a012006-11-02 20:12:39 +0000737 /// Dies - List of all dies in the compile unit.
738 ///
739 std::vector<DIE *> Dies;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000740
Jim Laskey0d086af2006-02-27 12:43:29 +0000741public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000742 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
743 : Desc(CUD)
744 , ID(I)
745 , Die(D)
746 , DescToDieMap()
747 , DescToDIEntryMap()
748 , Globals()
749 , DiesSet(InitDiesSetSize)
750 , Dies()
751 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000752
Jim Laskeyef42a012006-11-02 20:12:39 +0000753 ~CompileUnit() {
754 delete Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000755
Jim Laskeyef42a012006-11-02 20:12:39 +0000756 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
757 delete Dies[i];
758 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000759
Jim Laskeybd761842006-02-27 17:27:12 +0000760 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000761 CompileUnitDesc *getDesc() const { return Desc; }
762 unsigned getID() const { return ID; }
763 DIE* getDie() const { return Die; }
764 std::map<std::string, DIE *> &getGlobals() { return Globals; }
765
766 /// hasContent - Return true if this compile unit has something to write out.
767 ///
768 bool hasContent() const {
769 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000770 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000771
772 /// AddGlobal - Add a new global entity to the compile unit.
773 ///
774 void AddGlobal(const std::string &Name, DIE *Die) {
775 Globals[Name] = Die;
776 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000777
Jim Laskeyef42a012006-11-02 20:12:39 +0000778 /// getDieMapSlotFor - Returns the debug information entry map slot for the
779 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000780 DIE *&getDieMapSlotFor(DebugInfoDesc *DID) {
781 return DescToDieMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000782 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000783
Jim Laskeyef42a012006-11-02 20:12:39 +0000784 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
785 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000786 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DID) {
787 return DescToDIEntryMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000788 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000789
Jim Laskeyef42a012006-11-02 20:12:39 +0000790 /// AddDie - Adds or interns the DIE to the compile unit.
791 ///
792 DIE *AddDie(DIE &Buffer) {
793 FoldingSetNodeID ID;
794 Buffer.Profile(ID);
795 void *Where;
796 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000797
Jim Laskeyef42a012006-11-02 20:12:39 +0000798 if (!Die) {
799 Die = new DIE(Buffer);
800 DiesSet.InsertNode(Die, Where);
801 this->Die->AddChild(Die);
802 Buffer.Detach();
803 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000804
Jim Laskeyef42a012006-11-02 20:12:39 +0000805 return Die;
806 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000807};
808
Jim Laskey65195462006-10-30 13:35:07 +0000809//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000810/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000811///
Jim Laskey65195462006-10-30 13:35:07 +0000812class Dwarf {
813
Jim Laskey072200c2007-01-29 18:51:14 +0000814protected:
Jim Laskey65195462006-10-30 13:35:07 +0000815
816 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000817 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000818 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000819
Jim Laskey65195462006-10-30 13:35:07 +0000820 //
821 /// O - Stream to .s file.
822 ///
823 std::ostream &O;
824
825 /// Asm - Target of Dwarf emission.
826 ///
827 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000828
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000829 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000830 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000831
Jim Laskey65195462006-10-30 13:35:07 +0000832 /// TD - Target data.
833 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000834
Jim Laskey65195462006-10-30 13:35:07 +0000835 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000836 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000837
Jim Laskey65195462006-10-30 13:35:07 +0000838 /// M - Current module.
839 ///
840 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000841
Jim Laskey65195462006-10-30 13:35:07 +0000842 /// MF - Current machine function.
843 ///
844 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000845
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000846 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000847 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000848 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000849
Jim Laskey65195462006-10-30 13:35:07 +0000850 /// SubprogramCount - The running count of functions being compiled.
851 ///
852 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000853
Chris Lattner9251f132007-09-24 03:35:37 +0000854 /// Flavor - A unique string indicating what dwarf producer this is, used to
855 /// unique labels.
856 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000857
858 unsigned SetCounter;
Chris Lattner9251f132007-09-24 03:35:37 +0000859 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
860 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000861 : O(OS)
862 , Asm(A)
863 , TAI(T)
864 , TD(Asm->TM.getTargetData())
865 , RI(Asm->TM.getRegisterInfo())
866 , M(NULL)
867 , MF(NULL)
868 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000869 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000870 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000871 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000872 {
873 }
874
875public:
876
877 //===--------------------------------------------------------------------===//
878 // Accessors.
879 //
880 AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000881 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000882 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000883 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000884
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000885 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
886 const {
887 if (isInSection && TAI->getDwarfSectionOffsetDirective())
888 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000889 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000890 O << TAI->getData32bitsDirective();
891 else
892 O << TAI->getData64bitsDirective();
893 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000894
Jim Laskeyb82313f2007-02-01 16:31:34 +0000895 /// PrintLabelName - Print label name in form used by Dwarf writer.
896 ///
897 void PrintLabelName(DWLabel Label) const {
898 PrintLabelName(Label.Tag, Label.Number);
899 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000900 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000901 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000902 if (Number) O << Number;
903 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000904
Chris Lattner9251f132007-09-24 03:35:37 +0000905 void PrintLabelName(const char *Tag, unsigned Number,
906 const char *Suffix) const {
907 O << TAI->getPrivateGlobalPrefix() << Tag;
908 if (Number) O << Number;
909 O << Suffix;
910 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000911
Jim Laskeyb82313f2007-02-01 16:31:34 +0000912 /// EmitLabel - Emit location label for internal use by Dwarf.
913 ///
914 void EmitLabel(DWLabel Label) const {
915 EmitLabel(Label.Tag, Label.Number);
916 }
917 void EmitLabel(const char *Tag, unsigned Number) const {
918 PrintLabelName(Tag, Number);
919 O << ":\n";
920 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000921
Jim Laskeyb82313f2007-02-01 16:31:34 +0000922 /// EmitReference - Emit a reference to a label.
923 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000924 void EmitReference(DWLabel Label, bool IsPCRelative = false,
925 bool Force32Bit = false) const {
926 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000927 }
928 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000929 bool IsPCRelative = false, bool Force32Bit = false) const {
930 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000931 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000932
Jim Laskeyb82313f2007-02-01 16:31:34 +0000933 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
934 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000935 void EmitReference(const std::string &Name, bool IsPCRelative = false,
936 bool Force32Bit = false) const {
937 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000938
Jim Laskeyb82313f2007-02-01 16:31:34 +0000939 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000940
Jim Laskeyb82313f2007-02-01 16:31:34 +0000941 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
942 }
943
944 /// EmitDifference - Emit the difference between two labels. Some
945 /// assemblers do not behave with absolute expressions with data directives,
946 /// so there is an option (needsSet) to use an intermediary set expression.
947 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000948 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000949 EmitDifference(LabelHi.Tag, LabelHi.Number,
950 LabelLo.Tag, LabelLo.Number,
951 IsSmall);
952 }
953 void EmitDifference(const char *TagHi, unsigned NumberHi,
954 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000955 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000956 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000957 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000958 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000959 O << ",";
960 PrintLabelName(TagHi, NumberHi);
961 O << "-";
962 PrintLabelName(TagLo, NumberLo);
963 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000964
965 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +0000966 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000967 ++SetCounter;
968 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000969 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000970
Jim Laskeyb82313f2007-02-01 16:31:34 +0000971 PrintLabelName(TagHi, NumberHi);
972 O << "-";
973 PrintLabelName(TagLo, NumberLo);
974 }
975 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000976
977 void EmitSectionOffset(const char* Label, const char* Section,
978 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000979 bool IsSmall = false, bool isEH = false,
980 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000981 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000982 if (isEH)
983 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
984 else
985 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
986
987 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000988 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000989 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000990 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000991 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000992
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000993 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000994 O << "-";
995 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000996 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000997 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000998
999 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001000
Chris Lattner9251f132007-09-24 03:35:37 +00001001 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001002 ++SetCounter;
1003 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001004 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001005
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001006 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001007
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001008 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001009 O << "-";
1010 PrintLabelName(Section, SectionNumber);
1011 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001012 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001013 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001014
Jim Laskeyb82313f2007-02-01 16:31:34 +00001015 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1016 /// frame.
1017 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +00001018 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001019 int stackGrowth =
1020 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1021 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001022 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +00001023 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +00001024
1025 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001026 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +00001027 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001028
Jim Laskeyb82313f2007-02-01 16:31:34 +00001029 if (LabelID) {
1030 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001031
Jim Laskeyb82313f2007-02-01 16:31:34 +00001032 // Throw out move if the label is invalid.
1033 if (!LabelID) continue;
1034 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001035
Jim Laskeyb82313f2007-02-01 16:31:34 +00001036 const MachineLocation &Dst = Move.getDestination();
1037 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001038
Jim Laskeyb82313f2007-02-01 16:31:34 +00001039 // Advance row if new location.
1040 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1041 Asm->EmitInt8(DW_CFA_advance_loc4);
1042 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001043 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1044 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001045
Jim Laskeyb82313f2007-02-01 16:31:34 +00001046 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001047 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001048 IsLocal = true;
1049 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001050
Jim Laskeyb82313f2007-02-01 16:31:34 +00001051 // If advancing cfa.
1052 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
1053 if (!Src.isRegister()) {
1054 if (Src.getRegister() == MachineLocation::VirtualFP) {
1055 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1056 Asm->EOL("DW_CFA_def_cfa_offset");
1057 } else {
1058 Asm->EmitInt8(DW_CFA_def_cfa);
1059 Asm->EOL("DW_CFA_def_cfa");
Dale Johannesenb97aec62007-11-13 19:13:01 +00001060 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001061 Asm->EOL("Register");
1062 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001063
Jim Laskeyb82313f2007-02-01 16:31:34 +00001064 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001065
Jim Laskeyb82313f2007-02-01 16:31:34 +00001066 Asm->EmitULEB128Bytes(Offset);
1067 Asm->EOL("Offset");
1068 } else {
1069 assert(0 && "Machine move no supported yet.");
1070 }
1071 } else if (Src.isRegister() &&
1072 Src.getRegister() == MachineLocation::VirtualFP) {
1073 if (Dst.isRegister()) {
1074 Asm->EmitInt8(DW_CFA_def_cfa_register);
1075 Asm->EOL("DW_CFA_def_cfa_register");
Dale Johannesenb97aec62007-11-13 19:13:01 +00001076 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getRegister(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001077 Asm->EOL("Register");
1078 } else {
1079 assert(0 && "Machine move no supported yet.");
1080 }
1081 } else {
Dale Johannesenb97aec62007-11-13 19:13:01 +00001082 unsigned Reg = RI->getDwarfRegNum(Src.getRegister(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001083 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001084
Jim Laskeyb82313f2007-02-01 16:31:34 +00001085 if (Offset < 0) {
1086 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1087 Asm->EOL("DW_CFA_offset_extended_sf");
1088 Asm->EmitULEB128Bytes(Reg);
1089 Asm->EOL("Reg");
1090 Asm->EmitSLEB128Bytes(Offset);
1091 Asm->EOL("Offset");
1092 } else if (Reg < 64) {
1093 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Chengd4114192008-07-09 21:53:02 +00001094 if (VerboseAsm)
1095 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1096 else
1097 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001098 Asm->EmitULEB128Bytes(Offset);
1099 Asm->EOL("Offset");
1100 } else {
1101 Asm->EmitInt8(DW_CFA_offset_extended);
1102 Asm->EOL("DW_CFA_offset_extended");
1103 Asm->EmitULEB128Bytes(Reg);
1104 Asm->EOL("Reg");
1105 Asm->EmitULEB128Bytes(Offset);
1106 Asm->EOL("Offset");
1107 }
1108 }
1109 }
1110 }
1111
Jim Laskey072200c2007-01-29 18:51:14 +00001112};
1113
1114//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001115/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001116///
1117class DwarfDebug : public Dwarf {
1118
1119private:
Jim Laskey65195462006-10-30 13:35:07 +00001120 //===--------------------------------------------------------------------===//
1121 // Attributes used to construct specific Dwarf sections.
1122 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001123
Jim Laskey65195462006-10-30 13:35:07 +00001124 /// CompileUnits - All the compile units involved in this build. The index
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001125 /// of each entry in this vector corresponds to the sources in MMI.
Jim Laskey65195462006-10-30 13:35:07 +00001126 std::vector<CompileUnit *> CompileUnits;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001127
Jim Laskeyef42a012006-11-02 20:12:39 +00001128 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001129 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001130 FoldingSet<DIEAbbrev> AbbreviationsSet;
1131
1132 /// Abbreviations - A list of all the unique abbreviations in use.
1133 ///
1134 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001135
Jim Laskeyef42a012006-11-02 20:12:39 +00001136 /// ValuesSet - Used to uniquely define values.
1137 ///
1138 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001139
Jim Laskeyef42a012006-11-02 20:12:39 +00001140 /// Values - A list of all the unique values in use.
1141 ///
1142 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001143
Jim Laskey65195462006-10-30 13:35:07 +00001144 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001145 ///
Jim Laskey65195462006-10-30 13:35:07 +00001146 UniqueVector<std::string> StringPool;
1147
1148 /// UnitMap - Map debug information descriptor to compile unit.
1149 ///
1150 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001151
Jim Laskey65195462006-10-30 13:35:07 +00001152 /// SectionMap - Provides a unique id per text section.
1153 ///
1154 UniqueVector<std::string> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001155
Jim Laskey65195462006-10-30 13:35:07 +00001156 /// SectionSourceLines - Tracks line numbers per text section.
1157 ///
1158 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1159
Jim Laskeybacd3042007-02-21 22:48:45 +00001160 /// didInitial - Flag to indicate if initial emission has been done.
1161 ///
1162 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001163
Jim Laskeybacd3042007-02-21 22:48:45 +00001164 /// shouldEmit - Flag to indicate if debug information should be emitted.
1165 ///
1166 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001167
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001168 struct FunctionDebugFrameInfo {
1169 unsigned Number;
1170 std::vector<MachineMove> Moves;
1171
1172 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001173 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001174 };
1175
1176 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001177
Jim Laskey65195462006-10-30 13:35:07 +00001178public:
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001179
Jim Laskeybacd3042007-02-21 22:48:45 +00001180 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1181 ///
1182 bool ShouldEmitDwarf() const { return shouldEmit; }
Jim Laskey65195462006-10-30 13:35:07 +00001183
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001184 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001185 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001186 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1187 // Profile the node so that we can make it unique.
1188 FoldingSetNodeID ID;
1189 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001190
Jim Laskeyef42a012006-11-02 20:12:39 +00001191 // Check the set for priors.
1192 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001193
Jim Laskeyef42a012006-11-02 20:12:39 +00001194 // If it's newly added.
1195 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001196 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001197 Abbreviations.push_back(&Abbrev);
1198 // Assign the vector position + 1 as its number.
1199 Abbrev.setNumber(Abbreviations.size());
1200 } else {
1201 // Assign existing abbreviation number.
1202 Abbrev.setNumber(InSet->getNumber());
1203 }
1204 }
1205
Jim Laskey65195462006-10-30 13:35:07 +00001206 /// NewString - Add a string to the constant pool and returns a label.
1207 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001208 DWLabel NewString(const std::string &String) {
1209 unsigned StringID = StringPool.insert(String);
1210 return DWLabel("string", StringID);
1211 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001212
Jim Laskeyef42a012006-11-02 20:12:39 +00001213 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1214 /// entry.
1215 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1216 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001217
Jim Laskeyef42a012006-11-02 20:12:39 +00001218 if (Entry) {
1219 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001220 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001221 void *Where;
1222 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001223
Jim Laskeyf6733882006-11-02 21:48:18 +00001224 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001225
Jim Laskeyef42a012006-11-02 20:12:39 +00001226 Value = new DIEntry(Entry);
1227 ValuesSet.InsertNode(Value, Where);
1228 } else {
1229 Value = new DIEntry(Entry);
1230 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001231
Jim Laskeyef42a012006-11-02 20:12:39 +00001232 Values.push_back(Value);
1233 return Value;
1234 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001235
Jim Laskeyef42a012006-11-02 20:12:39 +00001236 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1237 ///
1238 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1239 Value->Entry = Entry;
1240 // Add to values set if not already there. If it is, we merely have a
1241 // duplicate in the values list (no harm.)
1242 ValuesSet.GetOrInsertNode(Value);
1243 }
1244
1245 /// AddUInt - Add an unsigned integer attribute data and value.
1246 ///
1247 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1248 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1249
1250 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001251 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001252 void *Where;
1253 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1254 if (!Value) {
1255 Value = new DIEInteger(Integer);
1256 ValuesSet.InsertNode(Value, Where);
1257 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001258 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001259
Jim Laskeyef42a012006-11-02 20:12:39 +00001260 Die->AddValue(Attribute, Form, Value);
1261 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001262
Jim Laskeyef42a012006-11-02 20:12:39 +00001263 /// AddSInt - Add an signed integer attribute data and value.
1264 ///
1265 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1266 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1267
1268 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001269 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001270 void *Where;
1271 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1272 if (!Value) {
1273 Value = new DIEInteger(Integer);
1274 ValuesSet.InsertNode(Value, Where);
1275 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001276 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001277
Jim Laskeyef42a012006-11-02 20:12:39 +00001278 Die->AddValue(Attribute, Form, Value);
1279 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001280
Jim Laskeyef42a012006-11-02 20:12:39 +00001281 /// AddString - Add a std::string attribute data and value.
1282 ///
1283 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1284 const std::string &String) {
1285 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001286 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001287 void *Where;
1288 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1289 if (!Value) {
1290 Value = new DIEString(String);
1291 ValuesSet.InsertNode(Value, Where);
1292 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001293 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001294
Jim Laskeyef42a012006-11-02 20:12:39 +00001295 Die->AddValue(Attribute, Form, Value);
1296 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001297
Jim Laskeyef42a012006-11-02 20:12:39 +00001298 /// AddLabel - Add a Dwarf label attribute data and value.
1299 ///
1300 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1301 const DWLabel &Label) {
1302 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001303 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001304 void *Where;
1305 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1306 if (!Value) {
1307 Value = new DIEDwarfLabel(Label);
1308 ValuesSet.InsertNode(Value, Where);
1309 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001310 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001311
Jim Laskeyef42a012006-11-02 20:12:39 +00001312 Die->AddValue(Attribute, Form, Value);
1313 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001314
Jim Laskeyef42a012006-11-02 20:12:39 +00001315 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1316 ///
1317 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1318 const std::string &Label) {
1319 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001320 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001321 void *Where;
1322 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1323 if (!Value) {
1324 Value = new DIEObjectLabel(Label);
1325 ValuesSet.InsertNode(Value, Where);
1326 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001327 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001328
Jim Laskeyef42a012006-11-02 20:12:39 +00001329 Die->AddValue(Attribute, Form, Value);
1330 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001331
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001332 /// AddSectionOffset - Add a section offset label attribute data and value.
1333 ///
1334 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1335 const DWLabel &Label, const DWLabel &Section,
1336 bool isEH = false, bool useSet = true) {
1337 FoldingSetNodeID ID;
1338 DIESectionOffset::Profile(ID, Label, Section);
1339 void *Where;
1340 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1341 if (!Value) {
1342 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1343 ValuesSet.InsertNode(Value, Where);
1344 Values.push_back(Value);
1345 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001346
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001347 Die->AddValue(Attribute, Form, Value);
1348 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001349
Jim Laskeyef42a012006-11-02 20:12:39 +00001350 /// AddDelta - Add a label delta attribute data and value.
1351 ///
1352 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1353 const DWLabel &Hi, const DWLabel &Lo) {
1354 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001355 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001356 void *Where;
1357 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1358 if (!Value) {
1359 Value = new DIEDelta(Hi, Lo);
1360 ValuesSet.InsertNode(Value, Where);
1361 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001362 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001363
Jim Laskeyef42a012006-11-02 20:12:39 +00001364 Die->AddValue(Attribute, Form, Value);
1365 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001366
Jim Laskeyef42a012006-11-02 20:12:39 +00001367 /// AddDIEntry - Add a DIE attribute data and value.
1368 ///
1369 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1370 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1371 }
1372
1373 /// AddBlock - Add block data.
1374 ///
1375 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1376 Block->ComputeSize(*this);
1377 FoldingSetNodeID ID;
1378 Block->Profile(ID);
1379 void *Where;
1380 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1381 if (!Value) {
1382 Value = Block;
1383 ValuesSet.InsertNode(Value, Where);
1384 Values.push_back(Value);
1385 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001386 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001387 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001388 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001389 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001390
Jim Laskeyef42a012006-11-02 20:12:39 +00001391 Die->AddValue(Attribute, Block->BestForm(), Value);
1392 }
1393
Jim Laskey65195462006-10-30 13:35:07 +00001394private:
1395
1396 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001397 /// entry.
1398 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1399 if (File && Line) {
1400 CompileUnit *FileUnit = FindCompileUnit(File);
1401 unsigned FileID = FileUnit->getID();
1402 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1403 AddUInt(Die, DW_AT_decl_line, 0, Line);
1404 }
1405 }
Jim Laskey65195462006-10-30 13:35:07 +00001406
1407 /// AddAddress - Add an address attribute to a die based on the location
1408 /// provided.
1409 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001410 const MachineLocation &Location) {
Dale Johannesenb97aec62007-11-13 19:13:01 +00001411 unsigned Reg = RI->getDwarfRegNum(Location.getRegister(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001412 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001413
Jim Laskeyef42a012006-11-02 20:12:39 +00001414 if (Location.isRegister()) {
1415 if (Reg < 32) {
1416 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1417 } else {
1418 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1419 AddUInt(Block, 0, DW_FORM_udata, Reg);
1420 }
1421 } else {
1422 if (Reg < 32) {
1423 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1424 } else {
1425 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1426 AddUInt(Block, 0, DW_FORM_udata, Reg);
1427 }
1428 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1429 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001430
Jim Laskeyef42a012006-11-02 20:12:39 +00001431 AddBlock(Die, Attribute, 0, Block);
1432 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001433
Jim Laskeyef42a012006-11-02 20:12:39 +00001434 /// AddBasicType - Add a new basic type attribute to the specified entity.
1435 ///
1436 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1437 const std::string &Name,
1438 unsigned Encoding, unsigned Size) {
1439 DIE *Die = ConstructBasicType(Unit, Name, Encoding, Size);
1440 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1441 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001442
Jim Laskeyef42a012006-11-02 20:12:39 +00001443 /// ConstructBasicType - Construct a new basic type.
1444 ///
1445 DIE *ConstructBasicType(CompileUnit *Unit,
1446 const std::string &Name,
1447 unsigned Encoding, unsigned Size) {
1448 DIE Buffer(DW_TAG_base_type);
1449 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1450 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1451 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1452 return Unit->AddDie(Buffer);
1453 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001454
Jim Laskeyef42a012006-11-02 20:12:39 +00001455 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1456 ///
1457 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1458 DIE *Die = ConstructPointerType(Unit, Name);
1459 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1460 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001461
Jim Laskeyef42a012006-11-02 20:12:39 +00001462 /// ConstructPointerType - Construct a new pointer type.
1463 ///
1464 DIE *ConstructPointerType(CompileUnit *Unit, const std::string &Name) {
1465 DIE Buffer(DW_TAG_pointer_type);
Dan Gohman82482942007-09-27 23:12:31 +00001466 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Jim Laskeyef42a012006-11-02 20:12:39 +00001467 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1468 return Unit->AddDie(Buffer);
1469 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001470
Jim Laskeyef42a012006-11-02 20:12:39 +00001471 /// AddType - Add a new type attribute to the specified entity.
1472 ///
1473 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1474 if (!TyDesc) {
Jim Laskey2b935d52007-01-26 14:19:17 +00001475 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001476 } else {
1477 // Check for pre-existence.
1478 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001479
Jim Laskeyef42a012006-11-02 20:12:39 +00001480 // If it exists then use the existing value.
1481 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001482 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1483 return;
1484 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001485
Jim Laskeyef42a012006-11-02 20:12:39 +00001486 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1487 // FIXME - Not sure why programs and variables are coming through here.
1488 // Short cut for handling subprogram types (not really a TyDesc.)
1489 AddPointerType(Entity, Unit, SubprogramTy->getName());
1490 } else if (GlobalVariableDesc *GlobalTy =
1491 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1492 // FIXME - Not sure why programs and variables are coming through here.
1493 // Short cut for handling global variable types (not really a TyDesc.)
1494 AddPointerType(Entity, Unit, GlobalTy->getName());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001495 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001496 // Set up proxy.
1497 Slot = NewDIEntry();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001498
Jim Laskeyef42a012006-11-02 20:12:39 +00001499 // Construct type.
1500 DIE Buffer(DW_TAG_base_type);
1501 ConstructType(Buffer, TyDesc, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001502
Jim Laskeyef42a012006-11-02 20:12:39 +00001503 // Add debug information entry to entity and unit.
1504 DIE *Die = Unit->AddDie(Buffer);
1505 SetDIEntry(Slot, Die);
1506 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1507 }
1508 }
1509 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001510
Jim Laskeyef42a012006-11-02 20:12:39 +00001511 /// ConstructType - Adds all the required attributes to the type.
1512 ///
1513 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1514 // Get core information.
1515 const std::string &Name = TyDesc->getName();
1516 uint64_t Size = TyDesc->getSize() >> 3;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001517
Jim Laskeyef42a012006-11-02 20:12:39 +00001518 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1519 // Fundamental types like int, float, bool
1520 Buffer.setTag(DW_TAG_base_type);
1521 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1522 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00001523 // Fetch tag.
1524 unsigned Tag = DerivedTy->getTag();
1525 // FIXME - Workaround for templates.
1526 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001527 // Pointers, typedefs et al.
Jim Laskey85f419b2006-11-09 16:32:26 +00001528 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00001529 // Map to main type, void will not have a type.
1530 if (TypeDesc *FromTy = DerivedTy->getFromType())
1531 AddType(&Buffer, FromTy, Unit);
1532 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1533 // Fetch tag.
1534 unsigned Tag = CompTy->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001535
Jim Laskeyef42a012006-11-02 20:12:39 +00001536 // Set tag accordingly.
1537 if (Tag == DW_TAG_vector_type)
1538 Buffer.setTag(DW_TAG_array_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001539 else
Jim Laskeyef42a012006-11-02 20:12:39 +00001540 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00001541
Jim Laskeyef42a012006-11-02 20:12:39 +00001542 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001543
Jim Laskeyef42a012006-11-02 20:12:39 +00001544 switch (Tag) {
1545 case DW_TAG_vector_type:
1546 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1547 // Fall thru
1548 case DW_TAG_array_type: {
1549 // Add element type.
1550 if (TypeDesc *FromTy = CompTy->getFromType())
1551 AddType(&Buffer, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001552
Jim Laskeyef42a012006-11-02 20:12:39 +00001553 // Don't emit size attribute.
1554 Size = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001555
Jim Laskeyef42a012006-11-02 20:12:39 +00001556 // Construct an anonymous type for index type.
Jim Laskey2b935d52007-01-26 14:19:17 +00001557 DIE *IndexTy = ConstructBasicType(Unit, "", DW_ATE_signed,
1558 sizeof(int32_t));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001559
Jim Laskeyef42a012006-11-02 20:12:39 +00001560 // Add subranges to array type.
1561 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1562 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1563 int64_t Lo = SRD->getLo();
1564 int64_t Hi = SRD->getHi();
1565 DIE *Subrange = new DIE(DW_TAG_subrange_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001566
Jim Laskeyef42a012006-11-02 20:12:39 +00001567 // If a range is available.
1568 if (Lo != Hi) {
1569 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1570 // Only add low if non-zero.
1571 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1572 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1573 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001574
Jim Laskeyef42a012006-11-02 20:12:39 +00001575 Buffer.AddChild(Subrange);
1576 }
1577 break;
1578 }
1579 case DW_TAG_structure_type:
1580 case DW_TAG_union_type: {
1581 // Add elements to structure type.
1582 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1583 DebugInfoDesc *Element = Elements[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001584
Jim Laskeyef42a012006-11-02 20:12:39 +00001585 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1586 // Add field or base class.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001587
Jim Laskeyef42a012006-11-02 20:12:39 +00001588 unsigned Tag = MemberDesc->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001589
Jim Laskeyef42a012006-11-02 20:12:39 +00001590 // Extract the basic information.
1591 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001592 uint64_t Size = MemberDesc->getSize();
1593 uint64_t Align = MemberDesc->getAlign();
1594 uint64_t Offset = MemberDesc->getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001595
Jim Laskeyef42a012006-11-02 20:12:39 +00001596 // Construct member debug information entry.
1597 DIE *Member = new DIE(Tag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001598
Jim Laskeyef42a012006-11-02 20:12:39 +00001599 // Add name if not "".
1600 if (!Name.empty())
1601 AddString(Member, DW_AT_name, DW_FORM_string, Name);
1602 // Add location if available.
1603 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001604
Jim Laskeyef42a012006-11-02 20:12:39 +00001605 // Most of the time the field info is the same as the members.
1606 uint64_t FieldSize = Size;
1607 uint64_t FieldAlign = Align;
1608 uint64_t FieldOffset = Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001609
Jim Laskeyee5f9272006-12-22 20:03:42 +00001610 // Set the member type.
1611 TypeDesc *FromTy = MemberDesc->getFromType();
1612 AddType(Member, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001613
Jim Laskeyee5f9272006-12-22 20:03:42 +00001614 // Walk up typedefs until a real size is found.
1615 while (FromTy) {
1616 if (FromTy->getTag() != DW_TAG_typedef) {
1617 FieldSize = FromTy->getSize();
1618 FieldAlign = FromTy->getSize();
1619 break;
1620 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001621
Dan Gohman275769a2007-07-23 20:24:29 +00001622 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
Jim Laskeyef42a012006-11-02 20:12:39 +00001623 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001624
Jim Laskeyef42a012006-11-02 20:12:39 +00001625 // Unless we have a bit field.
1626 if (Tag == DW_TAG_member && FieldSize != Size) {
1627 // Construct the alignment mask.
1628 uint64_t AlignMask = ~(FieldAlign - 1);
1629 // Determine the high bit + 1 of the declared size.
1630 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1631 // Work backwards to determine the base offset of the field.
1632 FieldOffset = HiMark - FieldSize;
1633 // Now normalize offset to the field.
1634 Offset -= FieldOffset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001635
Jim Laskeyef42a012006-11-02 20:12:39 +00001636 // Maybe we need to work from the other end.
1637 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001638
Jim Laskeyef42a012006-11-02 20:12:39 +00001639 // Add size and offset.
1640 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1641 AddUInt(Member, DW_AT_bit_size, 0, Size);
1642 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1643 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001644
Jim Laskeyef42a012006-11-02 20:12:39 +00001645 // Add computation for offset.
1646 DIEBlock *Block = new DIEBlock();
1647 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1648 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1649 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1650
1651 // Add accessibility (public default unless is base class.
1652 if (MemberDesc->isProtected()) {
1653 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1654 } else if (MemberDesc->isPrivate()) {
1655 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1656 } else if (Tag == DW_TAG_inheritance) {
1657 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1658 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001659
Jim Laskeyef42a012006-11-02 20:12:39 +00001660 Buffer.AddChild(Member);
1661 } else if (GlobalVariableDesc *StaticDesc =
1662 dyn_cast<GlobalVariableDesc>(Element)) {
1663 // Add static member.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001664
Jim Laskeyef42a012006-11-02 20:12:39 +00001665 // Construct member debug information entry.
1666 DIE *Static = new DIE(DW_TAG_variable);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001667
Jim Laskeyef42a012006-11-02 20:12:39 +00001668 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001669 const std::string &Name = StaticDesc->getName();
1670 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001671 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001672 if (!LinkageName.empty()) {
1673 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1674 LinkageName);
1675 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001676
Jim Laskeyef42a012006-11-02 20:12:39 +00001677 // Add location.
1678 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001679
Jim Laskeyef42a012006-11-02 20:12:39 +00001680 // Add type.
1681 if (TypeDesc *StaticTy = StaticDesc->getType())
1682 AddType(Static, StaticTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001683
Jim Laskeyef42a012006-11-02 20:12:39 +00001684 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001685 if (!StaticDesc->isStatic())
1686 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001687 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001688
Jim Laskeyef42a012006-11-02 20:12:39 +00001689 Buffer.AddChild(Static);
1690 } else if (SubprogramDesc *MethodDesc =
1691 dyn_cast<SubprogramDesc>(Element)) {
1692 // Add member function.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001693
Jim Laskeyef42a012006-11-02 20:12:39 +00001694 // Construct member debug information entry.
1695 DIE *Method = new DIE(DW_TAG_subprogram);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001696
Jim Laskeyef42a012006-11-02 20:12:39 +00001697 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001698 const std::string &Name = MethodDesc->getName();
1699 const std::string &LinkageName = MethodDesc->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001700
1701 AddString(Method, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001702 bool IsCTor = TyDesc->getName() == Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001703
Jim Laskey2172f962006-11-30 14:35:45 +00001704 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001705 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001706 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001707 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001708
Jim Laskeyef42a012006-11-02 20:12:39 +00001709 // Add location.
1710 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001711
Jim Laskeyef42a012006-11-02 20:12:39 +00001712 // Add type.
1713 if (CompositeTypeDesc *MethodTy =
1714 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1715 // Get argument information.
1716 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001717
Jim Laskeyef42a012006-11-02 20:12:39 +00001718 // If not a ctor.
1719 if (!IsCTor) {
1720 // Add return type.
1721 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1722 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001723
Jim Laskeyef42a012006-11-02 20:12:39 +00001724 // Add arguments.
1725 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1726 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1727 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1728 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1729 Method->AddChild(Arg);
1730 }
1731 }
1732
1733 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001734 if (!MethodDesc->isStatic())
1735 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001736 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001737
Jim Laskeyef42a012006-11-02 20:12:39 +00001738 Buffer.AddChild(Method);
1739 }
1740 }
1741 break;
1742 }
1743 case DW_TAG_enumeration_type: {
1744 // Add enumerators to enumeration type.
1745 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1746 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1747 const std::string &Name = ED->getName();
1748 int64_t Value = ED->getValue();
1749 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1750 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1751 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1752 Buffer.AddChild(Enumerator);
1753 }
1754
1755 break;
1756 }
1757 case DW_TAG_subroutine_type: {
1758 // Add prototype flag.
1759 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1760 // Add return type.
1761 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001762
Jim Laskeyef42a012006-11-02 20:12:39 +00001763 // Add arguments.
1764 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1765 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1766 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1767 Buffer.AddChild(Arg);
1768 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001769
Jim Laskeyef42a012006-11-02 20:12:39 +00001770 break;
1771 }
1772 default: break;
1773 }
1774 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001775
Jim Laskeyef42a012006-11-02 20:12:39 +00001776 // Add size if non-zero (derived types don't have a size.)
1777 if (Size) AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1778 // Add name if not anonymous or intermediate type.
1779 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1780 // Add source line info if available.
1781 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
1782 }
1783
1784 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00001785 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001786 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1787 // Construct debug information entry.
1788 DIE *Die = new DIE(DW_TAG_compile_unit);
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001789 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
1790 DWLabel("section_line", 0), DWLabel("section_line", 0), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001791 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1792 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1793 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1794 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001795
Jim Laskeyef42a012006-11-02 20:12:39 +00001796 // Construct compile unit.
1797 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001798
Jim Laskeyef42a012006-11-02 20:12:39 +00001799 // Add Unit to compile unit map.
1800 DescToUnitMap[UnitDesc] = Unit;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001801
Jim Laskeyef42a012006-11-02 20:12:39 +00001802 return Unit;
1803 }
1804
Jim Laskey9d4209f2006-11-07 19:33:46 +00001805 /// GetBaseCompileUnit - Get the main compile unit.
1806 ///
1807 CompileUnit *GetBaseCompileUnit() const {
1808 CompileUnit *Unit = CompileUnits[0];
1809 assert(Unit && "Missing compile unit.");
1810 return Unit;
1811 }
1812
Jim Laskey65195462006-10-30 13:35:07 +00001813 /// FindCompileUnit - Get the compile unit for the given descriptor.
1814 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001815 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001816 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00001817 assert(Unit && "Missing compile unit.");
1818 return Unit;
1819 }
1820
1821 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00001822 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001823 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1824 // Get the compile unit context.
1825 CompileUnitDesc *UnitDesc =
1826 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001827 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001828
1829 // Check for pre-existence.
1830 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1831 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001832
Jim Laskeyef42a012006-11-02 20:12:39 +00001833 // Get the global variable itself.
1834 GlobalVariable *GV = GVD->getGlobalVariable();
1835
Jim Laskey2172f962006-11-30 14:35:45 +00001836 const std::string &Name = GVD->getName();
1837 const std::string &FullName = GVD->getFullName();
1838 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001839 // Create the global's variable DIE.
1840 DIE *VariableDie = new DIE(DW_TAG_variable);
1841 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001842 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001843 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001844 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001845 }
Jim Laskey6488a072007-01-08 22:15:18 +00001846 AddType(VariableDie, GVD->getType(), Unit);
1847 if (!GVD->isStatic())
1848 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001849
Jim Laskeyef42a012006-11-02 20:12:39 +00001850 // Add source line info if available.
1851 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001852
Jim Laskeyef42a012006-11-02 20:12:39 +00001853 // Add address.
1854 DIEBlock *Block = new DIEBlock();
1855 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00001856 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
1857 AddBlock(VariableDie, DW_AT_location, 0, Block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001858
Jim Laskeyef42a012006-11-02 20:12:39 +00001859 // Add to map.
1860 Slot = VariableDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001861
Jim Laskeyef42a012006-11-02 20:12:39 +00001862 // Add to context owner.
1863 Unit->getDie()->AddChild(VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001864
Jim Laskeyef42a012006-11-02 20:12:39 +00001865 // Expose as global.
1866 // FIXME - need to check external flag.
Jim Laskey2172f962006-11-30 14:35:45 +00001867 Unit->AddGlobal(FullName, VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001868
Jim Laskeyef42a012006-11-02 20:12:39 +00001869 return VariableDie;
1870 }
Jim Laskey65195462006-10-30 13:35:07 +00001871
1872 /// NewSubprogram - Add a new subprogram DIE.
1873 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001874 DIE *NewSubprogram(SubprogramDesc *SPD) {
1875 // Get the compile unit context.
1876 CompileUnitDesc *UnitDesc =
1877 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001878 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001879
1880 // Check for pre-existence.
1881 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1882 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001883
Jim Laskeyef42a012006-11-02 20:12:39 +00001884 // Gather the details (simplify add attribute code.)
Jim Laskey2172f962006-11-30 14:35:45 +00001885 const std::string &Name = SPD->getName();
1886 const std::string &FullName = SPD->getFullName();
1887 const std::string &LinkageName = SPD->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001888
Jim Laskeyef42a012006-11-02 20:12:39 +00001889 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1890 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001891 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001892 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001893 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001894 }
1895 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
Jim Laskey6488a072007-01-08 22:15:18 +00001896 if (!SPD->isStatic())
1897 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001898 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001899
Jim Laskeyef42a012006-11-02 20:12:39 +00001900 // Add source line info if available.
1901 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1902
1903 // Add to map.
1904 Slot = SubprogramDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001905
Jim Laskeyef42a012006-11-02 20:12:39 +00001906 // Add to context owner.
1907 Unit->getDie()->AddChild(SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001908
Jim Laskeyef42a012006-11-02 20:12:39 +00001909 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00001910 Unit->AddGlobal(FullName, SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001911
Jim Laskeyef42a012006-11-02 20:12:39 +00001912 return SubprogramDie;
1913 }
Jim Laskey65195462006-10-30 13:35:07 +00001914
1915 /// NewScopeVariable - Create a new scope variable.
1916 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001917 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1918 // Get the descriptor.
1919 VariableDesc *VD = DV->getDesc();
1920
1921 // Translate tag to proper Dwarf tag. The result variable is dropped for
1922 // now.
1923 unsigned Tag;
1924 switch (VD->getTag()) {
1925 case DW_TAG_return_variable: return NULL;
1926 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1927 case DW_TAG_auto_variable: // fall thru
1928 default: Tag = DW_TAG_variable; break;
1929 }
1930
1931 // Define variable debug information entry.
1932 DIE *VariableDie = new DIE(Tag);
1933 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1934
1935 // Add source line info if available.
1936 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001937
Jim Laskeyef42a012006-11-02 20:12:39 +00001938 // Add variable type.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001939 AddType(VariableDie, VD->getType(), Unit);
1940
Jim Laskeyef42a012006-11-02 20:12:39 +00001941 // Add variable address.
1942 MachineLocation Location;
Evan Cheng72bebb92008-01-31 03:37:28 +00001943 Location.set(RI->getFrameRegister(*MF),
1944 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Jim Laskeyef42a012006-11-02 20:12:39 +00001945 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001946
Jim Laskeyef42a012006-11-02 20:12:39 +00001947 return VariableDie;
1948 }
Jim Laskey65195462006-10-30 13:35:07 +00001949
1950 /// ConstructScope - Construct the components of a scope.
1951 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001952 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00001953 unsigned ParentStartID, unsigned ParentEndID,
1954 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001955 // Add variables to scope.
1956 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1957 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1958 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1959 if (VariableDie) ParentDie->AddChild(VariableDie);
1960 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001961
Jim Laskeyef42a012006-11-02 20:12:39 +00001962 // Add nested scopes.
1963 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1964 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1965 // Define the Scope debug information entry.
1966 DebugScope *Scope = Scopes[j];
1967 // FIXME - Ignore inlined functions for the time being.
1968 if (!Scope->getParent()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001969
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001970 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1971 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00001972
Jim Laskey9d4209f2006-11-07 19:33:46 +00001973 // Ignore empty scopes.
1974 if (StartID == EndID && StartID != 0) continue;
1975 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001976
Jim Laskey36729dd2006-11-29 16:55:57 +00001977 if (StartID == ParentStartID && EndID == ParentEndID) {
1978 // Just add stuff to the parent scope.
1979 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001980 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00001981 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001982
Jim Laskey36729dd2006-11-29 16:55:57 +00001983 // Add the scope bounds.
1984 if (StartID) {
1985 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00001986 DWLabel("label", StartID));
Jim Laskey36729dd2006-11-29 16:55:57 +00001987 } else {
1988 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1989 DWLabel("func_begin", SubprogramCount));
1990 }
1991 if (EndID) {
1992 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00001993 DWLabel("label", EndID));
Jim Laskey36729dd2006-11-29 16:55:57 +00001994 } else {
1995 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1996 DWLabel("func_end", SubprogramCount));
1997 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001998
Jim Laskey36729dd2006-11-29 16:55:57 +00001999 // Add the scope contents.
2000 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
2001 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00002002 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002003 }
2004 }
Jim Laskey65195462006-10-30 13:35:07 +00002005
2006 /// ConstructRootScope - Construct the scope for the subprogram.
2007 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002008 void ConstructRootScope(DebugScope *RootScope) {
2009 // Exit if there is no root scope.
2010 if (!RootScope) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002011
2012 // Get the subprogram debug information entry.
Jim Laskeyef42a012006-11-02 20:12:39 +00002013 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002014
Jim Laskeyef42a012006-11-02 20:12:39 +00002015 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00002016 CompileUnit *Unit = GetBaseCompileUnit();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002017
Jim Laskeyef42a012006-11-02 20:12:39 +00002018 // Get the subprogram die.
2019 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2020 assert(SPDie && "Missing subprogram descriptor");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002021
Jim Laskeyef42a012006-11-02 20:12:39 +00002022 // Add the function bounds.
2023 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2024 DWLabel("func_begin", SubprogramCount));
2025 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2026 DWLabel("func_end", SubprogramCount));
2027 MachineLocation Location(RI->getFrameRegister(*MF));
2028 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002029
Jim Laskey36729dd2006-11-29 16:55:57 +00002030 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002031 }
Jim Laskey65195462006-10-30 13:35:07 +00002032
Jim Laskeyef42a012006-11-02 20:12:39 +00002033 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2034 /// tools to recognize the object file contains Dwarf information.
2035 void EmitInitial() {
2036 // Check to see if we already emitted intial headers.
2037 if (didInitial) return;
2038 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002039
Jim Laskeyef42a012006-11-02 20:12:39 +00002040 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002041 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002042 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002043 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002044 }
2045 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2046 EmitLabel("section_info", 0);
2047 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2048 EmitLabel("section_abbrev", 0);
2049 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2050 EmitLabel("section_aranges", 0);
2051 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2052 EmitLabel("section_macinfo", 0);
2053 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2054 EmitLabel("section_line", 0);
2055 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2056 EmitLabel("section_loc", 0);
2057 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2058 EmitLabel("section_pubnames", 0);
2059 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2060 EmitLabel("section_str", 0);
2061 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2062 EmitLabel("section_ranges", 0);
2063
2064 Asm->SwitchToTextSection(TAI->getTextSection());
2065 EmitLabel("text_begin", 0);
2066 Asm->SwitchToDataSection(TAI->getDataSection());
2067 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002068 }
2069
Jim Laskey65195462006-10-30 13:35:07 +00002070 /// EmitDIE - Recusively Emits a debug information entry.
2071 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002072 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002073 // Get the abbreviation for this DIE.
2074 unsigned AbbrevNumber = Die->getAbbrevNumber();
2075 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002076
Jim Laskeybacd3042007-02-21 22:48:45 +00002077 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002078
2079 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002080 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002081
2082 if (VerboseAsm)
2083 Asm->EOL(std::string("Abbrev [" +
2084 utostr(AbbrevNumber) +
2085 "] 0x" + utohexstr(Die->getOffset()) +
2086 ":0x" + utohexstr(Die->getSize()) + " " +
2087 TagString(Abbrev->getTag())));
2088 else
2089 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002090
Owen Anderson873e1b52008-06-24 21:44:59 +00002091 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2092 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002093
Jim Laskeyef42a012006-11-02 20:12:39 +00002094 // Emit the DIE attribute values.
2095 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2096 unsigned Attr = AbbrevData[i].getAttribute();
2097 unsigned Form = AbbrevData[i].getForm();
2098 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002099
Jim Laskeyef42a012006-11-02 20:12:39 +00002100 switch (Attr) {
2101 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002102 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002103 break;
2104 }
2105 default: {
2106 // Emit an attribute using the defined form.
2107 Values[i]->EmitValue(*this, Form);
2108 break;
2109 }
2110 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002111
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002112 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002113 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002114
Jim Laskeyef42a012006-11-02 20:12:39 +00002115 // Emit the DIE children if any.
2116 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2117 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002118
Jim Laskeyef42a012006-11-02 20:12:39 +00002119 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2120 EmitDIE(Children[j]);
2121 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002122
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002123 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002124 }
2125 }
2126
Jim Laskey65195462006-10-30 13:35:07 +00002127 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2128 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002129 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2130 // Get the children.
2131 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002132
Jim Laskeyef42a012006-11-02 20:12:39 +00002133 // If not last sibling and has children then add sibling offset attribute.
2134 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2135
2136 // Record the abbreviation.
2137 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002138
Jim Laskeyef42a012006-11-02 20:12:39 +00002139 // Get the abbreviation for this DIE.
2140 unsigned AbbrevNumber = Die->getAbbrevNumber();
2141 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2142
2143 // Set DIE offset
2144 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002145
Jim Laskeyef42a012006-11-02 20:12:39 +00002146 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002147 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2148
Owen Anderson873e1b52008-06-24 21:44:59 +00002149 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2150 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002151
2152 // Size the DIE attribute values.
2153 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2154 // Size attribute value.
2155 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2156 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002157
Jim Laskeyef42a012006-11-02 20:12:39 +00002158 // Size the DIE children if any.
2159 if (!Children.empty()) {
2160 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2161 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002162
Jim Laskeyef42a012006-11-02 20:12:39 +00002163 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2164 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2165 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002166
Jim Laskeyef42a012006-11-02 20:12:39 +00002167 // End of children marker.
2168 Offset += sizeof(int8_t);
2169 }
2170
2171 Die->setSize(Offset - Die->getOffset());
2172 return Offset;
2173 }
Jim Laskey65195462006-10-30 13:35:07 +00002174
2175 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2176 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002177 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002178 // Process base compile unit.
2179 CompileUnit *Unit = GetBaseCompileUnit();
2180 // Compute size of compile unit header
2181 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2182 sizeof(int16_t) + // DWARF version number
2183 sizeof(int32_t) + // Offset Into Abbrev. Section
2184 sizeof(int8_t); // Pointer Size (in bytes)
2185 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002186 }
2187
Jim Laskey65195462006-10-30 13:35:07 +00002188 /// EmitDebugInfo - Emit the debug info section.
2189 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002190 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002191 // Start debug info section.
2192 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002193
Jim Laskey5496f012006-11-09 14:52:14 +00002194 CompileUnit *Unit = GetBaseCompileUnit();
2195 DIE *Die = Unit->getDie();
2196 // Emit the compile units header.
2197 EmitLabel("info_begin", Unit->getID());
2198 // Emit size of content not including length itself
2199 unsigned ContentSize = Die->getSize() +
2200 sizeof(int16_t) + // DWARF version number
2201 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00002202 sizeof(int8_t) + // Pointer Size (in bytes)
2203 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002204
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002205 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2206 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002207 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002208 Asm->EOL("Offset Into Abbrev. Section");
Dan Gohman82482942007-09-27 23:12:31 +00002209 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002210
Jim Laskey5496f012006-11-09 14:52:14 +00002211 EmitDIE(Die);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002212 // FIXME - extra padding for gdb bug.
2213 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2214 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2215 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2216 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Jim Laskey5496f012006-11-09 14:52:14 +00002217 EmitLabel("info_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002218
Jim Laskeybacd3042007-02-21 22:48:45 +00002219 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002220 }
2221
Jim Laskey65195462006-10-30 13:35:07 +00002222 /// EmitAbbreviations - Emit the abbreviation section.
2223 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002224 void EmitAbbreviations() const {
2225 // Check to see if it is worth the effort.
2226 if (!Abbreviations.empty()) {
2227 // Start the debug abbrev section.
2228 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002229
Jim Laskeyef42a012006-11-02 20:12:39 +00002230 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002231
Jim Laskeyef42a012006-11-02 20:12:39 +00002232 // For each abbrevation.
2233 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2234 // Get abbreviation data
2235 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002236
Jim Laskeyef42a012006-11-02 20:12:39 +00002237 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002238 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2239 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002240
Jim Laskeyef42a012006-11-02 20:12:39 +00002241 // Emit the abbreviations data.
2242 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002243
Jim Laskeybacd3042007-02-21 22:48:45 +00002244 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002245 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002246
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002247 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002248 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002249
Jim Laskeyef42a012006-11-02 20:12:39 +00002250 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002251
Jim Laskeybacd3042007-02-21 22:48:45 +00002252 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002253 }
2254 }
2255
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002256 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2257 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002258 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002259 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2260 // Define last address of section.
2261 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2262 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2263 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2264 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2265
2266 // Mark end of matrix.
2267 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2268 Asm->EmitULEB128Bytes(1); Asm->EOL();
2269 Asm->EmitInt8(1); Asm->EOL();
2270 }
2271
Jim Laskey65195462006-10-30 13:35:07 +00002272 /// EmitDebugLines - Emit source line information.
2273 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002274 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002275 // If the target is using .loc/.file, the assembler will be emitting the
2276 // .debug_line table automatically.
2277 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002278 return;
2279
Jim Laskeyef42a012006-11-02 20:12:39 +00002280 // Minimum line delta, thus ranging from -10..(255-10).
2281 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2282 // Maximum line delta, thus ranging from -10..(255-10).
2283 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002284
Jim Laskeyef42a012006-11-02 20:12:39 +00002285 // Start the dwarf line section.
2286 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002287
Jim Laskeyef42a012006-11-02 20:12:39 +00002288 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002289
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002290 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002291 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002292 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002293
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002294 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002295
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002296 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002297 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002298 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002299
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002300 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002301
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002302 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002303
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002304 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002305
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002306 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002307
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002308 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002309
Jim Laskeyef42a012006-11-02 20:12:39 +00002310 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002311 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2312 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2313 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2314 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2315 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2316 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2317 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2318 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2319 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002320
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002321 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Evan Cheng6547e402008-07-01 23:18:29 +00002322 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
Jim Laskeyef42a012006-11-02 20:12:39 +00002323
2324 // Emit directories.
2325 for (unsigned DirectoryID = 1, NDID = Directories.size();
2326 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002327 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002328 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002329 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002330
Jim Laskeyef42a012006-11-02 20:12:39 +00002331 // Emit files.
2332 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2333 SourceID <= NSID; ++SourceID) {
2334 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002335 Asm->EmitString(SourceFile.getName());
2336 Asm->EOL("Source");
2337 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2338 Asm->EOL("Directory #");
2339 Asm->EmitULEB128Bytes(0);
2340 Asm->EOL("Mod date");
2341 Asm->EmitULEB128Bytes(0);
2342 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002343 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002344 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002345
Jim Laskeyef42a012006-11-02 20:12:39 +00002346 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002347
Jim Laskeyef42a012006-11-02 20:12:39 +00002348 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002349 unsigned SecSrcLinesSize = SectionSourceLines.size();
2350
2351 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002352 // Isolate current sections line info.
2353 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002354
2355 if (VerboseAsm)
2356 Asm->EOL(std::string("Section ") + SectionMap[j + 1]);
2357 else
2358 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002359
2360 // Dwarf assumes we start with first line of first source file.
2361 unsigned Source = 1;
2362 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002363
Jim Laskeyef42a012006-11-02 20:12:39 +00002364 // Construct rows of the address, source, line, column matrix.
2365 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2366 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002367 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002368 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002369
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002370 unsigned SourceID = LineInfo.getSourceID();
2371 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2372 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng6547e402008-07-01 23:18:29 +00002373 if (VerboseAsm)
2374 Asm->EOL(Directories[DirectoryID]
2375 + SourceFile.getName()
2376 + ":"
2377 + utostr_32(LineInfo.getLine()));
2378 else
2379 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002380
2381 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002382 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002383 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002384 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002385 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002386
Jim Laskeyef42a012006-11-02 20:12:39 +00002387 // If change of source, then switch to the new source.
2388 if (Source != LineInfo.getSourceID()) {
2389 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002390 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2391 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002392 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002393
Jim Laskeyef42a012006-11-02 20:12:39 +00002394 // If change of line.
2395 if (Line != LineInfo.getLine()) {
2396 // Determine offset.
2397 int Offset = LineInfo.getLine() - Line;
2398 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002399
Jim Laskeyef42a012006-11-02 20:12:39 +00002400 // Update line.
2401 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002402
Jim Laskeyef42a012006-11-02 20:12:39 +00002403 // If delta is small enough and in range...
2404 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2405 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002406 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002407 } else {
2408 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002409 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2410 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2411 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002412 }
2413 } else {
2414 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002415 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002416 }
2417 }
2418
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002419 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002420 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002421
2422 if (SecSrcLinesSize == 0)
2423 // Because we're emitting a debug_line section, we still need a line
2424 // table. The linker and friends expect it to exist. If there's nothing to
2425 // put into it, emit an empty table.
2426 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002427
Jim Laskeyef42a012006-11-02 20:12:39 +00002428 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002429
Jim Laskeybacd3042007-02-21 22:48:45 +00002430 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002431 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002432
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002433 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002434 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002435 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002436 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002437 return;
2438
2439 int stackGrowth =
2440 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2441 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002442 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002443
2444 // Start the dwarf frame section.
2445 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2446
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002447 EmitLabel("debug_frame_common", 0);
2448 EmitDifference("debug_frame_common_end", 0,
2449 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002450 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002451
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002452 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002453 Asm->EmitInt32((int)DW_CIE_ID);
2454 Asm->EOL("CIE Identifier Tag");
2455 Asm->EmitInt8(DW_CIE_VERSION);
2456 Asm->EOL("CIE Version");
2457 Asm->EmitString("");
2458 Asm->EOL("CIE Augmentation");
2459 Asm->EmitULEB128Bytes(1);
2460 Asm->EOL("CIE Code Alignment Factor");
2461 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002462 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002463 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002464 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002465
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002466 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002467 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002468
Dale Johannesenb97aec62007-11-13 19:13:01 +00002469 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002470
Evan Cheng05548eb2008-02-29 19:36:59 +00002471 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002472 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002473
Jim Laskeybacd3042007-02-21 22:48:45 +00002474 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002475 }
2476
Jim Laskey65195462006-10-30 13:35:07 +00002477 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2478 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002479 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002480 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002481 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002482
Jim Laskeyef42a012006-11-02 20:12:39 +00002483 // Start the dwarf frame section.
2484 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002485
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002486 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2487 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002488 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002489
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002490 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002491
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002492 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2493 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002494 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002495
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002496 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002497 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002498 EmitDifference("func_end", DebugFrameInfo.Number,
2499 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002500 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002501
Dale Johannesenb97aec62007-11-13 19:13:01 +00002502 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002503
Evan Cheng05548eb2008-02-29 19:36:59 +00002504 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002505 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002506
Jim Laskeybacd3042007-02-21 22:48:45 +00002507 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002508 }
2509
2510 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002511 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002512 void EmitDebugPubNames() {
2513 // Start the dwarf pubnames section.
2514 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002515
2516 CompileUnit *Unit = GetBaseCompileUnit();
2517
Jim Laskey5496f012006-11-09 14:52:14 +00002518 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002519 "pubnames_begin", Unit->getID(), true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002520 Asm->EOL("Length of Public Names Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002521
Jim Laskey5496f012006-11-09 14:52:14 +00002522 EmitLabel("pubnames_begin", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002523
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002524 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002525
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002526 EmitSectionOffset("info_begin", "section_info",
2527 Unit->getID(), 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002528 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002529
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002530 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002531 Asm->EOL("Compilation Unit Length");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002532
Jim Laskey5496f012006-11-09 14:52:14 +00002533 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002534
Jim Laskey5496f012006-11-09 14:52:14 +00002535 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2536 GE = Globals.end();
2537 GI != GE; ++GI) {
2538 const std::string &Name = GI->first;
2539 DIE * Entity = GI->second;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002540
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002541 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2542 Asm->EmitString(Name); Asm->EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00002543 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002544
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002545 Asm->EmitInt32(0); Asm->EOL("End Mark");
Jim Laskey5496f012006-11-09 14:52:14 +00002546 EmitLabel("pubnames_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002547
Jim Laskeybacd3042007-02-21 22:48:45 +00002548 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002549 }
2550
2551 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002552 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002553 void EmitDebugStr() {
2554 // Check to see if it is worth the effort.
2555 if (!StringPool.empty()) {
2556 // Start the dwarf str section.
2557 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002558
Jim Laskeyef42a012006-11-02 20:12:39 +00002559 // For each of strings in the string pool.
2560 for (unsigned StringID = 1, N = StringPool.size();
2561 StringID <= N; ++StringID) {
2562 // Emit a label for reference from debug information entries.
2563 EmitLabel("string", StringID);
2564 // Emit the string itself.
2565 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002566 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002567 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002568
Jim Laskeybacd3042007-02-21 22:48:45 +00002569 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002570 }
2571 }
2572
2573 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002574 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002575 void EmitDebugLoc() {
2576 // Start the dwarf loc section.
2577 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002578
Jim Laskeybacd3042007-02-21 22:48:45 +00002579 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002580 }
2581
2582 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002583 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002584 void EmitDebugARanges() {
2585 // Start the dwarf aranges section.
2586 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002587
Jim Laskeyef42a012006-11-02 20:12:39 +00002588 // FIXME - Mock up
2589 #if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002590 CompileUnit *Unit = GetBaseCompileUnit();
2591
Jim Laskey5496f012006-11-09 14:52:14 +00002592 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002593 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002594
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002595 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002596
Jim Laskey5496f012006-11-09 14:52:14 +00002597 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002598 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002599
Dan Gohman82482942007-09-27 23:12:31 +00002600 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002601
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002602 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002603
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002604 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2605 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002606
Jim Laskey5496f012006-11-09 14:52:14 +00002607 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002608 EmitReference("text_begin", 0); Asm->EOL("Address");
2609 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002610
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002611 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2612 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Dan Gohman44de9262007-09-24 21:36:21 +00002613 #endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002614
Jim Laskeybacd3042007-02-21 22:48:45 +00002615 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002616 }
2617
2618 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002619 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002620 void EmitDebugRanges() {
2621 // Start the dwarf ranges section.
2622 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002623
Jim Laskeybacd3042007-02-21 22:48:45 +00002624 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002625 }
2626
2627 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002628 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002629 void EmitDebugMacInfo() {
2630 // Start the dwarf macinfo section.
2631 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002632
Jim Laskeybacd3042007-02-21 22:48:45 +00002633 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002634 }
2635
Jim Laskey65195462006-10-30 13:35:07 +00002636 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2637 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002638 void ConstructCompileUnitDIEs() {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002639 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002640
Jim Laskeyef42a012006-11-02 20:12:39 +00002641 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002642 unsigned ID = MMI->RecordSource(CUW[i]);
Jim Laskey9d4209f2006-11-07 19:33:46 +00002643 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002644 CompileUnits.push_back(Unit);
2645 }
2646 }
2647
Jim Laskey65195462006-10-30 13:35:07 +00002648 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2649 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002650 void ConstructGlobalDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00002651 std::vector<GlobalVariableDesc *> GlobalVariables;
2652 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M, GlobalVariables);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002653
Bill Wendling10fff602008-07-03 22:53:42 +00002654 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2655 GlobalVariableDesc *GVD = GlobalVariables[i];
2656 NewGlobalVariable(GVD);
2657 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002658 }
Jim Laskey65195462006-10-30 13:35:07 +00002659
2660 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2661 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002662 void ConstructSubprogramDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00002663 std::vector<SubprogramDesc *> Subprograms;
2664 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002665
Bill Wendling10fff602008-07-03 22:53:42 +00002666 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2667 SubprogramDesc *SPD = Subprograms[i];
2668 NewSubprogram(SPD);
2669 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002670 }
Jim Laskey65195462006-10-30 13:35:07 +00002671
Jim Laskey65195462006-10-30 13:35:07 +00002672public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002673 //===--------------------------------------------------------------------===//
2674 // Main entry points.
2675 //
Jim Laskey072200c2007-01-29 18:51:14 +00002676 DwarfDebug(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00002677 : Dwarf(OS, A, T, "dbg")
Jim Laskeyef42a012006-11-02 20:12:39 +00002678 , CompileUnits()
2679 , AbbreviationsSet(InitAbbreviationsSetSize)
2680 , Abbreviations()
2681 , ValuesSet(InitValuesSetSize)
2682 , Values()
2683 , StringPool()
2684 , DescToUnitMap()
2685 , SectionMap()
2686 , SectionSourceLines()
Jim Laskeybacd3042007-02-21 22:48:45 +00002687 , didInitial(false)
2688 , shouldEmit(false)
Jim Laskeyef42a012006-11-02 20:12:39 +00002689 {
2690 }
Jim Laskey072200c2007-01-29 18:51:14 +00002691 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002692 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2693 delete CompileUnits[i];
2694 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2695 delete Values[j];
2696 }
2697
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002698 /// SetModuleInfo - Set machine module information when it's known that pass
2699 /// manager has created it. Set by the target AsmPrinter.
2700 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002701 // Make sure initial declarations are made.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002702 if (!MMI && mmi->hasDebugInfo()) {
2703 MMI = mmi;
Jim Laskeyef42a012006-11-02 20:12:39 +00002704 shouldEmit = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002705
Jim Laskeyef42a012006-11-02 20:12:39 +00002706 // Create all the compile unit DIEs.
2707 ConstructCompileUnitDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002708
Jim Laskeyef42a012006-11-02 20:12:39 +00002709 // Create DIEs for each of the externally visible global variables.
2710 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002711
Jim Laskeyef42a012006-11-02 20:12:39 +00002712 // Create DIEs for each of the externally visible subprograms.
2713 ConstructSubprogramDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002714
Jim Laskeyef42a012006-11-02 20:12:39 +00002715 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00002716 SectionMap.insert(TAI->getTextSection());
Dan Gohmand57c3882007-10-01 22:40:20 +00002717
2718 // Print out .file directives to specify files for .loc directives. These
2719 // are printed out early so that they precede any .loc directives.
2720 if (TAI->hasDotLocAndDotFile()) {
2721 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
2722 const UniqueVector<std::string> &Directories = MMI->getDirectories();
2723 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
2724 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
2725 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
2726 assert(AppendOk && "Could not append filename to directory!");
2727 Asm->EmitFile(i, FullPath.toString());
2728 Asm->EOL();
2729 }
2730 }
2731
2732 // Emit initial sections
2733 EmitInitial();
Jim Laskeyef42a012006-11-02 20:12:39 +00002734 }
2735 }
2736
Jim Laskey65195462006-10-30 13:35:07 +00002737 /// BeginModule - Emit all Dwarf sections that should come prior to the
2738 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002739 void BeginModule(Module *M) {
2740 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00002741 }
2742
Jim Laskey65195462006-10-30 13:35:07 +00002743 /// EndModule - Emit all Dwarf sections that should come after the content.
2744 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002745 void EndModule() {
2746 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002747
Jim Laskeyef42a012006-11-02 20:12:39 +00002748 // Standard sections final addresses.
2749 Asm->SwitchToTextSection(TAI->getTextSection());
2750 EmitLabel("text_end", 0);
2751 Asm->SwitchToDataSection(TAI->getDataSection());
2752 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002753
Jim Laskeyef42a012006-11-02 20:12:39 +00002754 // End text sections.
2755 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2756 Asm->SwitchToTextSection(SectionMap[i].c_str());
2757 EmitLabel("section_end", i);
2758 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002759
2760 // Emit common frame information.
2761 EmitCommonDebugFrame();
2762
2763 // Emit function debug frame information
2764 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2765 E = DebugFrames.end(); I != E; ++I)
2766 EmitFunctionDebugFrame(*I);
2767
Jim Laskeyef42a012006-11-02 20:12:39 +00002768 // Compute DIE offsets and sizes.
2769 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002770
Jim Laskeyef42a012006-11-02 20:12:39 +00002771 // Emit all the DIEs into a debug info section
2772 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002773
Jim Laskeyef42a012006-11-02 20:12:39 +00002774 // Corresponding abbreviations into a abbrev section.
2775 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002776
Jim Laskeyef42a012006-11-02 20:12:39 +00002777 // Emit source line correspondence into a debug line section.
2778 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002779
Jim Laskeyef42a012006-11-02 20:12:39 +00002780 // Emit info into a debug pubnames section.
2781 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002782
Jim Laskeyef42a012006-11-02 20:12:39 +00002783 // Emit info into a debug str section.
2784 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002785
Jim Laskeyef42a012006-11-02 20:12:39 +00002786 // Emit info into a debug loc section.
2787 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002788
Jim Laskeyef42a012006-11-02 20:12:39 +00002789 // Emit info into a debug aranges section.
2790 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002791
Jim Laskeyef42a012006-11-02 20:12:39 +00002792 // Emit info into a debug ranges section.
2793 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002794
Jim Laskeyef42a012006-11-02 20:12:39 +00002795 // Emit info into a debug macinfo section.
2796 EmitDebugMacInfo();
2797 }
2798
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002799 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00002800 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002801 void BeginFunction(MachineFunction *MF) {
2802 this->MF = MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002803
Jim Laskeyef42a012006-11-02 20:12:39 +00002804 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002805
2806 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002807 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002808
Jim Laskeyef42a012006-11-02 20:12:39 +00002809 // Assumes in correct section after the entry point.
2810 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00002811
2812 // Emit label for the implicitly defined dbg.stoppoint at the start of
2813 // the function.
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00002814 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
2815 if (!LineInfos.empty()) {
2816 const SourceLineInfo &LineInfo = LineInfos[0];
2817 Asm->printLabel(LineInfo.getLabelID());
2818 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002819 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002820
Jim Laskey65195462006-10-30 13:35:07 +00002821 /// EndFunction - Gather and emit post-function debug information.
2822 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002823 void EndFunction() {
2824 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002825
Jim Laskeyb82313f2007-02-01 16:31:34 +00002826 // Define end label for subprogram.
2827 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002828
Jim Laskeyef42a012006-11-02 20:12:39 +00002829 // Get function line info.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002830 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
Jim Laskeyef42a012006-11-02 20:12:39 +00002831
2832 if (!LineInfos.empty()) {
2833 // Get section line info.
2834 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2835 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2836 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2837 // Append the function info to section info.
2838 SectionLineInfos.insert(SectionLineInfos.end(),
2839 LineInfos.begin(), LineInfos.end());
2840 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002841
Jim Laskeyef42a012006-11-02 20:12:39 +00002842 // Construct scopes for subprogram.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002843 ConstructRootScope(MMI->getRootScope());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002844
2845 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2846 MMI->getFrameMoves()));
Jim Laskeyef42a012006-11-02 20:12:39 +00002847 }
Jim Laskey65195462006-10-30 13:35:07 +00002848};
2849
Jim Laskey072200c2007-01-29 18:51:14 +00002850//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002851/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00002852///
2853class DwarfException : public Dwarf {
2854
Jim Laskeyb82313f2007-02-01 16:31:34 +00002855private:
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002856 struct FunctionEHFrameInfo {
2857 std::string FnName;
2858 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002859 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002860 bool hasCalls;
2861 bool hasLandingPads;
2862 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002863 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00002864
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002865 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
2866 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00002867 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002868 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002869 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002870 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002871 };
2872
2873 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00002874
2875 /// shouldEmitTable - Per-function flag to indicate if EH tables should
2876 /// be emitted.
2877 bool shouldEmitTable;
2878
2879 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
2880 /// should be emitted.
2881 bool shouldEmitMoves;
2882
2883 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
2884 /// should be emitted.
2885 bool shouldEmitTableModule;
2886
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002887 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00002888 /// should be emitted.
2889 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00002890
Jim Laskey3f09fc22007-02-28 18:38:31 +00002891 /// EmitCommonEHFrame - Emit the common eh unwind frame.
2892 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002893 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00002894 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002895 int stackGrowth =
2896 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2897 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002898 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00002899
Jim Laskeybacd3042007-02-21 22:48:45 +00002900 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002901 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002902 O << "EH_frame" << Index << ":\n";
2903 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002904
Jim Laskeybacd3042007-02-21 22:48:45 +00002905 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002906 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00002907
Jim Laskeybacd3042007-02-21 22:48:45 +00002908 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002909 EmitDifference("eh_frame_common_end", Index,
2910 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002911 Asm->EOL("Length of Common Information Entry");
2912
Jim Laskeybacd3042007-02-21 22:48:45 +00002913 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002914 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002915 Asm->EmitInt32((int)0);
2916 Asm->EOL("CIE Identifier Tag");
2917 Asm->EmitInt8(DW_CIE_VERSION);
2918 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00002919
Jim Laskeybacd3042007-02-21 22:48:45 +00002920 // The personality presence indicates that language specific information
2921 // will show up in the eh frame.
2922 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00002923 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00002924
Jim Laskeybacd3042007-02-21 22:48:45 +00002925 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002926 Asm->EmitULEB128Bytes(1);
2927 Asm->EOL("CIE Code Alignment Factor");
2928 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00002929 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002930 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00002931 Asm->EOL("CIE Return Address Column");
2932
Jim Laskeybacd3042007-02-21 22:48:45 +00002933 // If there is a personality, we need to indicate the functions location.
2934 if (Personality) {
2935 Asm->EmitULEB128Bytes(7);
2936 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00002937
Duncan Sands671fa972008-05-07 19:11:09 +00002938 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00002939 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00002940 Asm->EOL("Personality (pcrel sdata4 indirect)");
2941 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00002942 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00002943 Asm->EOL("Personality (pcrel sdata4)");
2944 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00002945
Duncan Sands671fa972008-05-07 19:11:09 +00002946 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00002947 O << TAI->getPersonalityPrefix();
2948 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
2949 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00002950 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
2951 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00002952 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00002953
Duncan Sands671fa972008-05-07 19:11:09 +00002954 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2955 Asm->EOL("LSDA Encoding (pcrel sdata4)");
2956 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2957 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00002958 } else {
2959 Asm->EmitULEB128Bytes(1);
2960 Asm->EOL("Augmentation Size");
Duncan Sands671fa972008-05-07 19:11:09 +00002961 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2962 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00002963 }
2964
2965 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002966 std::vector<MachineMove> Moves;
2967 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00002968 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002969
Dale Johannesen21d972a2008-04-30 00:43:29 +00002970 // On Darwin the linker honors the alignment of eh_frame, which means it
2971 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
2972 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002973 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00002974 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002975 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00002976
Jim Laskeybacd3042007-02-21 22:48:45 +00002977 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00002978 }
Duncan Sands671fa972008-05-07 19:11:09 +00002979
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002980 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002981 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002982 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002983 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
2984
Jim Laskeybacd3042007-02-21 22:48:45 +00002985 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
2986
2987 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00002988 // If the corresponding function is static, this should not be
2989 // externally visible.
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002990 if (linkage != Function::InternalLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00002991 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
2992 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
2993 }
2994
Dale Johannesen038129d2008-01-10 02:03:30 +00002995 // If corresponding function is weak definition, this should be too.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002996 if ((linkage == Function::WeakLinkage ||
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002997 linkage == Function::LinkOnceLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00002998 TAI->getWeakDefDirective())
2999 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3000
3001 // If there are no calls then you can't unwind. This may mean we can
3002 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003003 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003004 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003005 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003006 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003007 !UnwindTablesMandatory &&
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003008 ((linkage != Function::WeakLinkage &&
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003009 linkage != Function::LinkOnceLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003010 !TAI->getWeakDefDirective() ||
3011 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003012 {
Bill Wendling6e198962007-09-18 01:47:22 +00003013 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003014 // This name has no connection to the function, so it might get
3015 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003016 // dead-stripping unconditionally.
3017 if (const char *UsedDirective = TAI->getUsedDirective())
3018 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003019 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003020 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003021
Jim Laskeybacd3042007-02-21 22:48:45 +00003022 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003023 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3024 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003025 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003026
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003027 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003028
Anton Korobeynikov070280e2007-05-23 11:08:31 +00003029 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003030 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +00003031 true, true, false);
Jim Laskeybacd3042007-02-21 22:48:45 +00003032 Asm->EOL("FDE CIE offset");
3033
Duncan Sands671fa972008-05-07 19:11:09 +00003034 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003035 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003036 EmitDifference("eh_func_end", EHFrameInfo.Number,
Duncan Sands671fa972008-05-07 19:11:09 +00003037 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003038 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003039
Jim Laskey3f09fc22007-02-28 18:38:31 +00003040 // If there is a personality and landing pads then point to the language
3041 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003042 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003043 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003044 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003045
3046 if (EHFrameInfo.hasLandingPads)
3047 EmitReference("exception", EHFrameInfo.Number, true, true);
3048 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003049 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003050 Asm->EOL("Language Specific Data Area");
3051 } else {
3052 Asm->EmitULEB128Bytes(0);
3053 Asm->EOL("Augmentation size");
3054 }
Duncan Sands671fa972008-05-07 19:11:09 +00003055
Jim Laskeybacd3042007-02-21 22:48:45 +00003056 // Indicate locations of function specific callee saved registers in
3057 // frame.
Dale Johannesenb97aec62007-11-13 19:13:01 +00003058 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003059
Dale Johannesen21d972a2008-04-30 00:43:29 +00003060 // On Darwin the linker honors the alignment of eh_frame, which means it
3061 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3062 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003063 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003064 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003065 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003066
3067 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003068 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003069 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003070 // that depends on unused functions (calling undefined externals) being
3071 // dead-stripped to link correctly. Yes, there really is.
3072 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3073 if (const char *UsedDirective = TAI->getUsedDirective())
3074 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3075 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003076 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003077
Duncan Sands57810cd2007-09-05 11:27:52 +00003078 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003079 ///
3080 /// The general organization of the table is complex, but the basic concepts
3081 /// are easy. First there is a header which describes the location and
3082 /// organization of the three components that follow.
3083 /// 1. The landing pad site information describes the range of code covered
3084 /// by the try. In our case it's an accumulation of the ranges covered
3085 /// by the invokes in the try. There is also a reference to the landing
3086 /// pad that handles the exception once processed. Finally an index into
3087 /// the actions table.
3088 /// 2. The action table, in our case, is composed of pairs of type ids
3089 /// and next action offset. Starting with the action index from the
3090 /// landing pad site, each type Id is checked for a match to the current
3091 /// exception. If it matches then the exception and type id are passed
3092 /// on to the landing pad. Otherwise the next action is looked up. This
3093 /// chain is terminated with a next action of zero. If no type id is
3094 /// found the the frame is unwound and handling continues.
3095 /// 3. Type id table contains references to all the C++ typeinfo for all
3096 /// catches in the function. This tables is reversed indexed base 1.
3097
Duncan Sandsb32edb42007-06-06 15:37:31 +00003098 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3099 static unsigned SharedTypeIds(const LandingPadInfo *L,
3100 const LandingPadInfo *R) {
3101 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003102 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003103 unsigned MinSize = LSize < RSize ? LSize : RSize;
3104 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003105
Duncan Sandsb32edb42007-06-06 15:37:31 +00003106 for (; Count != MinSize; ++Count)
3107 if (LIds[Count] != RIds[Count])
3108 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003109
Duncan Sandsb32edb42007-06-06 15:37:31 +00003110 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003111 }
3112
Duncan Sandsb32edb42007-06-06 15:37:31 +00003113 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003114 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003115 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003116 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003117 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003118
Duncan Sandsb32edb42007-06-06 15:37:31 +00003119 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003120 if (LIds[i] != RIds[i])
3121 return LIds[i] < RIds[i];
3122
Duncan Sandsb32edb42007-06-06 15:37:31 +00003123 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003124 }
3125
Duncan Sands53c3a332007-05-16 12:12:23 +00003126 struct KeyInfo {
3127 static inline unsigned getEmptyKey() { return -1U; }
3128 static inline unsigned getTombstoneKey() { return -2U; }
3129 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003130 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003131 static bool isPod() { return true; }
3132 };
3133
Duncan Sands57810cd2007-09-05 11:27:52 +00003134 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003135 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003136 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003137 int NextAction;
3138 struct ActionEntry *Previous;
3139 };
3140
Duncan Sands57810cd2007-09-05 11:27:52 +00003141 /// PadRange - Structure holding a try-range and the associated landing pad.
3142 struct PadRange {
3143 // The index of the landing pad.
3144 unsigned PadIndex;
3145 // The index of the begin and end labels in the landing pad's label lists.
3146 unsigned RangeIndex;
3147 };
3148
3149 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3150
3151 /// CallSiteEntry - Structure describing an entry in the call-site table.
3152 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003153 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003154 unsigned BeginLabel; // zero indicates the start of the function.
3155 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003156 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003157 unsigned PadLabel; // zero indicates that there is no landing pad.
3158 unsigned Action;
3159 };
3160
Jim Laskeybacd3042007-02-21 22:48:45 +00003161 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003162 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003163 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003164 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3165 if (PadInfos.empty()) return;
3166
3167 // Sort the landing pads in order of their type ids. This is used to fold
3168 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003169 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003170 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003171 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003172 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003173 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3174
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003175 // Negative type ids index into FilterIds, positive type ids index into
3176 // TypeInfos. The value written for a positive type id is just the type
3177 // id itself. For a negative type id, however, the value written is the
3178 // (negative) byte offset of the corresponding FilterIds entry. The byte
3179 // offset is usually equal to the type id, because the FilterIds entries
3180 // are written using a variable width encoding which outputs one byte per
3181 // entry as long as the value written is not too large, but can differ.
3182 // This kind of complication does not occur for positive type ids because
3183 // type infos are output using a fixed width encoding.
3184 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3185 SmallVector<int, 16> FilterOffsets;
3186 FilterOffsets.reserve(FilterIds.size());
3187 int Offset = -1;
3188 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3189 E = FilterIds.end(); I != E; ++I) {
3190 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003191 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003192 }
3193
Duncan Sands57810cd2007-09-05 11:27:52 +00003194 // Compute the actions table and gather the first action index for each
3195 // landing pad site.
3196 SmallVector<ActionEntry, 32> Actions;
3197 SmallVector<unsigned, 64> FirstActions;
3198 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003199
Duncan Sandsb32edb42007-06-06 15:37:31 +00003200 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003201 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003202 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003203 const LandingPadInfo *LP = LandingPads[i];
3204 const std::vector<int> &TypeIds = LP->TypeIds;
3205 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003206 unsigned SizeSiteActions = 0;
3207
Duncan Sandsb32edb42007-06-06 15:37:31 +00003208 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003209 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003210 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003211
Duncan Sandsb32edb42007-06-06 15:37:31 +00003212 if (NumShared) {
3213 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3214 assert(Actions.size());
3215 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003216 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3217 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003218 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003219 SizeAction -=
3220 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003221 SizeAction += -PrevAction->NextAction;
3222 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003223 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003224 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003225
Duncan Sandsb32edb42007-06-06 15:37:31 +00003226 // Compute the actions.
3227 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3228 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003229 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3230 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003231 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003232
3233 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003234 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003235 SizeSiteActions += SizeAction;
3236
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003237 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00003238 Actions.push_back(Action);
3239
3240 PrevAction = &Actions.back();
3241 }
3242
3243 // Record the first action of the landing pad site.
3244 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3245 } // else identical - re-use previous FirstAction
3246
3247 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003248
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00003249 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00003250 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00003251 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003252
Duncan Sands481dc722007-12-19 07:36:31 +00003253 // Compute the call-site table. The entry for an invoke has a try-range
3254 // containing the call, a non-zero landing pad and an appropriate action.
3255 // The entry for an ordinary call has a try-range containing the call and
3256 // zero for the landing pad and the action. Calls marked 'nounwind' have
3257 // no entry and must not be contained in the try-range of any entry - they
3258 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003259 SmallVector<CallSiteEntry, 64> CallSites;
3260
3261 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00003262 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3263 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3264 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00003265 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3266 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00003267 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003268 unsigned BeginLabel = LandingPad->BeginLabels[j];
3269 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3270 PadRange P = { i, j };
3271 PadMap[BeginLabel] = P;
3272 }
3273 }
3274
Duncan Sands481dc722007-12-19 07:36:31 +00003275 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00003276 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00003277
3278 // Whether there is a potentially throwing instruction (currently this means
3279 // an ordinary call) between the end of the previous try-range and now.
3280 bool SawPotentiallyThrowing = false;
3281
3282 // Whether the last callsite entry was for an invoke.
3283 bool PreviousIsInvoke = false;
3284
Duncan Sands481dc722007-12-19 07:36:31 +00003285 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003286 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3287 I != E; ++I) {
3288 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3289 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00003290 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00003291 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00003292 continue;
3293 }
3294
Chris Lattner9e330492007-12-30 20:50:28 +00003295 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00003296 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00003297
Duncan Sands481dc722007-12-19 07:36:31 +00003298 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00003299 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00003300 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003301
Duncan Sands481dc722007-12-19 07:36:31 +00003302 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00003303 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00003304 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00003305 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00003306 continue;
3307
3308 PadRange P = L->second;
3309 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3310
3311 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3312 "Inconsistent landing pad map!");
3313
3314 // If some instruction between the previous try-range and this one may
3315 // throw, create a call-site entry with no landing pad for the region
3316 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00003317 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003318 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3319 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00003320 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003321 }
3322
3323 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00003324 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00003325
Duncan Sands481dc722007-12-19 07:36:31 +00003326 if (LandingPad->LandingPadLabel) {
3327 // This try-range is for an invoke.
3328 CallSiteEntry Site = {BeginLabel, LastLabel,
3329 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00003330
Duncan Sands481dc722007-12-19 07:36:31 +00003331 // Try to merge with the previous call-site.
3332 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00003333 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00003334 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3335 // Extend the range of the previous entry.
3336 Prev.EndLabel = Site.EndLabel;
3337 continue;
3338 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003339 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003340
Duncan Sands481dc722007-12-19 07:36:31 +00003341 // Otherwise, create a new call-site.
3342 CallSites.push_back(Site);
3343 PreviousIsInvoke = true;
3344 } else {
3345 // Create a gap.
3346 PreviousIsInvoke = false;
3347 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003348 }
3349 }
3350 // If some instruction between the previous try-range and the end of the
3351 // function may throw, create a call-site entry with no landing pad for the
3352 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00003353 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003354 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3355 CallSites.push_back(Site);
3356 }
3357
Jim Laskeybacd3042007-02-21 22:48:45 +00003358 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00003359
3360 // Call sites.
3361 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3362 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3363 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3364 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3365 SiteLengthSize +
3366 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00003367 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003368 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00003369
Duncan Sands671fa972008-05-07 19:11:09 +00003370 // Type infos.
3371 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3372 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003373
3374 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003375 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003376 SizeSites + SizeActions + SizeTypes;
3377
3378 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3379 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003380 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003381 TypeOffset;
3382
3383 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003384
Jim Laskeybacd3042007-02-21 22:48:45 +00003385 // Begin the exception table.
3386 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
3387 O << "GCC_except_table" << SubprogramCount << ":\n";
Evan Cheng05548eb2008-02-29 19:36:59 +00003388 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003389 for (unsigned i = 0; i != SizeAlign; ++i) {
3390 Asm->EmitInt8(0);
3391 Asm->EOL("Padding");
3392 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003393 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003394
Jim Laskeybacd3042007-02-21 22:48:45 +00003395 // Emit the header.
3396 Asm->EmitInt8(DW_EH_PE_omit);
3397 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3398 Asm->EmitInt8(DW_EH_PE_absptr);
3399 Asm->EOL("TType format (DW_EH_PE_absptr)");
3400 Asm->EmitULEB128Bytes(TypeOffset);
3401 Asm->EOL("TType base offset");
3402 Asm->EmitInt8(DW_EH_PE_udata4);
3403 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3404 Asm->EmitULEB128Bytes(SizeSites);
3405 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003406
Duncan Sands57810cd2007-09-05 11:27:52 +00003407 // Emit the landing pad site information.
3408 for (unsigned i = 0; i < CallSites.size(); ++i) {
3409 CallSiteEntry &S = CallSites[i];
3410 const char *BeginTag;
3411 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00003412
Duncan Sands57810cd2007-09-05 11:27:52 +00003413 if (!S.BeginLabel) {
3414 BeginTag = "eh_func_begin";
3415 BeginNumber = SubprogramCount;
3416 } else {
3417 BeginTag = "label";
3418 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00003419 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003420
Duncan Sands57810cd2007-09-05 11:27:52 +00003421 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003422 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003423 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00003424
Duncan Sands57810cd2007-09-05 11:27:52 +00003425 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00003426 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00003427 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003428 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00003429 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003430 }
3431 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003432
Duncan Sands671fa972008-05-07 19:11:09 +00003433 if (!S.PadLabel)
3434 Asm->EmitInt32(0);
3435 else
Duncan Sands57810cd2007-09-05 11:27:52 +00003436 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003437 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003438 Asm->EOL("Landing pad");
3439
3440 Asm->EmitULEB128Bytes(S.Action);
3441 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003442 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003443
Jim Laskeybacd3042007-02-21 22:48:45 +00003444 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003445 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3446 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00003447
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003448 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003449 Asm->EOL("TypeInfo index");
3450 Asm->EmitSLEB128Bytes(Action.NextAction);
3451 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003452 }
3453
3454 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00003455 for (unsigned M = TypeInfos.size(); M; --M) {
3456 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00003457
3458 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00003459
3460 if (GV)
3461 O << Asm->getGlobalLinkName(GV);
3462 else
3463 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00003464
Jim Laskeybacd3042007-02-21 22:48:45 +00003465 Asm->EOL("TypeInfo");
3466 }
3467
Duncan Sands73ef58a2007-06-02 16:53:42 +00003468 // Emit the filter typeids.
3469 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3470 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00003471 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00003472 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00003473 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003474
Evan Cheng05548eb2008-02-29 19:36:59 +00003475 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003476 }
3477
Jim Laskey072200c2007-01-29 18:51:14 +00003478public:
3479 //===--------------------------------------------------------------------===//
3480 // Main entry points.
3481 //
3482 DwarfException(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00003483 : Dwarf(OS, A, T, "eh")
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003484 , shouldEmitTable(false)
3485 , shouldEmitMoves(false)
3486 , shouldEmitTableModule(false)
3487 , shouldEmitMovesModule(false)
Jim Laskey072200c2007-01-29 18:51:14 +00003488 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003489
Jim Laskey072200c2007-01-29 18:51:14 +00003490 virtual ~DwarfException() {}
3491
3492 /// SetModuleInfo - Set machine module information when it's known that pass
3493 /// manager has created it. Set by the target AsmPrinter.
3494 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00003495 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00003496 }
3497
3498 /// BeginModule - Emit all exception information that should come prior to the
3499 /// content.
3500 void BeginModule(Module *M) {
3501 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00003502 }
3503
3504 /// EndModule - Emit all exception information that should come after the
3505 /// content.
3506 void EndModule() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003507 if (shouldEmitMovesModule || shouldEmitTableModule) {
3508 const std::vector<Function *> Personalities = MMI->getPersonalities();
3509 for (unsigned i =0; i < Personalities.size(); ++i)
3510 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003511
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003512 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3513 E = EHFrames.end(); I != E; ++I)
3514 EmitEHFrame(*I);
3515 }
Jim Laskey072200c2007-01-29 18:51:14 +00003516 }
3517
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003518 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00003519 /// emitted immediately after the function entry point.
3520 void BeginFunction(MachineFunction *MF) {
3521 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003522 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesene0040622008-04-02 17:04:45 +00003523 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003524
3525 // Map all labels and get rid of any dead landing pads.
3526 MMI->TidyLandingPads();
3527 // If any landing pads survive, we need an EH table.
3528 if (MMI->getLandingPads().size())
3529 shouldEmitTable = true;
3530
3531 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00003532 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003533 shouldEmitMoves = true;
3534
3535 if (shouldEmitMoves || shouldEmitTable)
3536 // Assumes in correct section after the entry point.
3537 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00003538 }
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003539 shouldEmitTableModule |= shouldEmitTable;
3540 shouldEmitMovesModule |= shouldEmitMoves;
Jim Laskey072200c2007-01-29 18:51:14 +00003541 }
3542
3543 /// EndFunction - Gather and emit post-function exception information.
3544 ///
3545 void EndFunction() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003546 if (shouldEmitMoves || shouldEmitTable) {
3547 EmitLabel("eh_func_end", SubprogramCount);
3548 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003549
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003550 // Save EH frame information
3551 EHFrames.
3552 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling6e198962007-09-18 01:47:22 +00003553 SubprogramCount,
3554 MMI->getPersonalityIndex(),
3555 MF->getFrameInfo()->hasCalls(),
3556 !MMI->getLandingPads().empty(),
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003557 MMI->getFrameMoves(),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003558 MF->getFunction()));
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003559 }
Jim Laskey072200c2007-01-29 18:51:14 +00003560 }
3561};
3562
Jim Laskey0d086af2006-02-27 12:43:29 +00003563} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00003564
3565//===----------------------------------------------------------------------===//
3566
Jim Laskeyd18e2892006-01-20 20:34:06 +00003567/// Emit - Print the abbreviation using the specified Dwarf writer.
3568///
Jim Laskey072200c2007-01-29 18:51:14 +00003569void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003570 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00003571 DD.getAsm()->EmitULEB128Bytes(Tag);
3572 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003573
Jim Laskeyd18e2892006-01-20 20:34:06 +00003574 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00003575 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3576 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003577
Jim Laskeyd18e2892006-01-20 20:34:06 +00003578 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00003579 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003580 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003581
Jim Laskeyd18e2892006-01-20 20:34:06 +00003582 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00003583 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3584 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003585
Jim Laskeyd18e2892006-01-20 20:34:06 +00003586 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00003587 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3588 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003589 }
3590
3591 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00003592 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3593 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00003594}
3595
3596#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00003597void DIEAbbrev::print(std::ostream &O) {
3598 O << "Abbreviation @"
3599 << std::hex << (intptr_t)this << std::dec
3600 << " "
3601 << TagString(Tag)
3602 << " "
3603 << ChildrenString(ChildrenFlag)
3604 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003605
Jim Laskeya0f3d172006-09-07 22:06:40 +00003606 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3607 O << " "
3608 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003609 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00003610 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003611 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00003612 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00003613}
Bill Wendlinge8156192006-12-07 01:30:32 +00003614void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00003615#endif
3616
3617//===----------------------------------------------------------------------===//
3618
Jim Laskeyef42a012006-11-02 20:12:39 +00003619#ifndef NDEBUG
3620void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003621 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003622}
Jim Laskeyef42a012006-11-02 20:12:39 +00003623#endif
3624
3625//===----------------------------------------------------------------------===//
3626
Jim Laskey063e7652006-01-17 17:31:53 +00003627/// EmitValue - Emit integer of appropriate size.
3628///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003629void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00003630 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00003631 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00003632 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003633 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003634 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003635 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003636 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003637 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003638 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003639 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
3640 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
3641 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003642 default: assert(0 && "DIE Value form not supported yet"); break;
3643 }
3644}
3645
3646/// SizeOf - Determine size of integer value in bytes.
3647///
Jim Laskey072200c2007-01-29 18:51:14 +00003648unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003649 switch (Form) {
3650 case DW_FORM_flag: // Fall thru
3651 case DW_FORM_ref1: // Fall thru
3652 case DW_FORM_data1: return sizeof(int8_t);
3653 case DW_FORM_ref2: // Fall thru
3654 case DW_FORM_data2: return sizeof(int16_t);
3655 case DW_FORM_ref4: // Fall thru
3656 case DW_FORM_data4: return sizeof(int32_t);
3657 case DW_FORM_ref8: // Fall thru
3658 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003659 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
3660 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00003661 default: assert(0 && "DIE Value form not supported yet"); break;
3662 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003663 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00003664}
3665
Jim Laskey063e7652006-01-17 17:31:53 +00003666//===----------------------------------------------------------------------===//
3667
3668/// EmitValue - Emit string value.
3669///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003670void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003671 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00003672}
3673
Jim Laskey063e7652006-01-17 17:31:53 +00003674//===----------------------------------------------------------------------===//
3675
3676/// EmitValue - Emit label value.
3677///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003678void DIEDwarfLabel::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 Laskey063e7652006-01-17 17:31:53 +00003681}
3682
3683/// SizeOf - Determine size of label value in bytes.
3684///
Jim Laskey072200c2007-01-29 18:51:14 +00003685unsigned DIEDwarfLabel::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 Laskey063e7652006-01-17 17:31:53 +00003688}
Jim Laskeyef42a012006-11-02 20:12:39 +00003689
Jim Laskey063e7652006-01-17 17:31:53 +00003690//===----------------------------------------------------------------------===//
3691
Jim Laskeyd18e2892006-01-20 20:34:06 +00003692/// EmitValue - Emit label value.
3693///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003694void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003695 bool IsSmall = Form == DW_FORM_data4;
3696 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00003697}
3698
3699/// SizeOf - Determine size of label value in bytes.
3700///
Jim Laskey072200c2007-01-29 18:51:14 +00003701unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003702 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003703 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00003704}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003705
Jim Laskeyd18e2892006-01-20 20:34:06 +00003706//===----------------------------------------------------------------------===//
3707
Jim Laskey063e7652006-01-17 17:31:53 +00003708/// EmitValue - Emit delta value.
3709///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00003710void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
3711 bool IsSmall = Form == DW_FORM_data4;
3712 DD.EmitSectionOffset(Label.Tag, Section.Tag,
3713 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
3714}
3715
3716/// SizeOf - Determine size of delta value in bytes.
3717///
3718unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3719 if (Form == DW_FORM_data4) return 4;
3720 return DD.getTargetData()->getPointerSize();
3721}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003722
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00003723//===----------------------------------------------------------------------===//
3724
3725/// EmitValue - Emit delta value.
3726///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003727void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003728 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00003729 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00003730}
3731
3732/// SizeOf - Determine size of delta value in bytes.
3733///
Jim Laskey072200c2007-01-29 18:51:14 +00003734unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003735 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003736 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00003737}
3738
3739//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003740
Jim Laskeyb8509c52006-03-23 18:07:55 +00003741/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003742///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003743void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003744 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00003745}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003746
Jim Laskeyd18e2892006-01-20 20:34:06 +00003747//===----------------------------------------------------------------------===//
3748
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003749/// ComputeSize - calculate the size of the block.
3750///
Jim Laskey072200c2007-01-29 18:51:14 +00003751unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003752 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00003753 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003754
Jim Laskeyef42a012006-11-02 20:12:39 +00003755 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00003756 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00003757 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003758 }
3759 return Size;
3760}
3761
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003762/// EmitValue - Emit block data.
3763///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003764void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003765 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003766 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
3767 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
3768 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
3769 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003770 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003771 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003772
Owen Anderson873e1b52008-06-24 21:44:59 +00003773 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00003774
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003775 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003776 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00003777 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003778 }
3779}
3780
3781/// SizeOf - Determine size of block data in bytes.
3782///
Jim Laskey072200c2007-01-29 18:51:14 +00003783unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003784 switch (Form) {
3785 case DW_FORM_block1: return Size + sizeof(int8_t);
3786 case DW_FORM_block2: return Size + sizeof(int16_t);
3787 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003788 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003789 default: assert(0 && "Improper form for block"); break;
3790 }
3791 return 0;
3792}
3793
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003794//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003795/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00003796
3797DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003798 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00003799 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00003800}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003801
Jim Laskeyb8509c52006-03-23 18:07:55 +00003802/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
3803///
3804void DIE::AddSiblingOffset() {
3805 DIEInteger *DI = new DIEInteger(0);
3806 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00003807 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00003808}
3809
Jim Laskeyef42a012006-11-02 20:12:39 +00003810/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003811///
Jim Laskeyef42a012006-11-02 20:12:39 +00003812void DIE::Profile(FoldingSetNodeID &ID) {
3813 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003814
Jim Laskeyef42a012006-11-02 20:12:39 +00003815 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3816 ID.AddPointer(Children[i]);
3817
3818 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3819 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003820}
Jim Laskeyef42a012006-11-02 20:12:39 +00003821
3822#ifndef NDEBUG
3823void DIE::print(std::ostream &O, unsigned IncIndent) {
3824 static unsigned IndentCount = 0;
3825 IndentCount += IncIndent;
3826 const std::string Indent(IndentCount, ' ');
3827 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003828
Jim Laskeyef42a012006-11-02 20:12:39 +00003829 if (!isBlock) {
3830 O << Indent
3831 << "Die: "
3832 << "0x" << std::hex << (intptr_t)this << std::dec
3833 << ", Offset: " << Offset
3834 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003835 << "\n";
3836
Jim Laskeyef42a012006-11-02 20:12:39 +00003837 O << Indent
3838 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00003839 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00003840 << ChildrenString(Abbrev.getChildrenFlag());
3841 } else {
3842 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00003843 }
3844 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003845
Owen Anderson873e1b52008-06-24 21:44:59 +00003846 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003847
Jim Laskeyef42a012006-11-02 20:12:39 +00003848 IndentCount += 2;
3849 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3850 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00003851
3852 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00003853 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00003854 else
Jim Laskeyef42a012006-11-02 20:12:39 +00003855 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00003856
Jim Laskeyef42a012006-11-02 20:12:39 +00003857 O << " "
3858 << FormEncodingString(Data[i].getForm())
3859 << " ";
3860 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00003861 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00003862 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003863 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00003864
Jim Laskeyef42a012006-11-02 20:12:39 +00003865 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3866 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00003867 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003868
Jim Laskeyef42a012006-11-02 20:12:39 +00003869 if (!isBlock) O << "\n";
3870 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00003871}
3872
Jim Laskeyef42a012006-11-02 20:12:39 +00003873void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003874 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00003875}
Jim Laskeybd761842006-02-27 17:27:12 +00003876#endif
Jim Laskey65195462006-10-30 13:35:07 +00003877
3878//===----------------------------------------------------------------------===//
3879/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00003880///
Jim Laskey65195462006-10-30 13:35:07 +00003881
3882DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3883 const TargetAsmInfo *T) {
Jim Laskey072200c2007-01-29 18:51:14 +00003884 DE = new DwarfException(OS, A, T);
3885 DD = new DwarfDebug(OS, A, T);
Jim Laskey65195462006-10-30 13:35:07 +00003886}
3887
3888DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00003889 delete DE;
3890 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00003891}
3892
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003893/// SetModuleInfo - Set machine module info when it's known that pass manager
3894/// has created it. Set by the target AsmPrinter.
3895void DwarfWriter::SetModuleInfo(MachineModuleInfo *MMI) {
Jim Laskey072200c2007-01-29 18:51:14 +00003896 DD->SetModuleInfo(MMI);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003897 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00003898}
3899
3900/// BeginModule - Emit all Dwarf sections that should come prior to the
3901/// content.
3902void DwarfWriter::BeginModule(Module *M) {
Jim Laskey072200c2007-01-29 18:51:14 +00003903 DE->BeginModule(M);
3904 DD->BeginModule(M);
Jim Laskey65195462006-10-30 13:35:07 +00003905}
3906
3907/// EndModule - Emit all Dwarf sections that should come after the content.
3908///
3909void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00003910 DE->EndModule();
3911 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00003912}
3913
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003914/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003915/// emitted immediately after the function entry point.
3916void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00003917 DE->BeginFunction(MF);
3918 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00003919}
3920
3921/// EndFunction - Gather and emit post-function debug information.
3922///
3923void DwarfWriter::EndFunction() {
Jim Laskey072200c2007-01-29 18:51:14 +00003924 DD->EndFunction();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003925 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003926
Bill Wendlingc91d0b92008-07-22 00:53:37 +00003927 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00003928 // Clear function debug information.
3929 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00003930}