blob: 13ea22cb28908173476debf7649e1a7cce946be9 [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"
Owen Andersoncb371882008-08-21 00:14:44 +000031#include "llvm/Support/raw_ostream.h"
Dan Gohman85496362007-09-24 21:32:18 +000032#include "llvm/System/Path.h"
Jim Laskey563321a2006-09-06 18:34:40 +000033#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000034#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000035#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000036#include "llvm/Target/TargetFrameInfo.h"
Duncan Sands53c3a332007-05-16 12:12:23 +000037#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000038#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000039#include "llvm/Target/TargetOptions.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000040#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000041#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000042using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000043using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000044
Jim Laskey0d086af2006-02-27 12:43:29 +000045namespace llvm {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000046
Jim Laskey65195462006-10-30 13:35:07 +000047//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000048
49/// Configuration values for initial hash set sizes (log2).
50///
51static const unsigned InitDiesSetSize = 9; // 512
52static const unsigned InitAbbreviationsSetSize = 9; // 512
53static const unsigned InitValuesSetSize = 9; // 512
54
55//===----------------------------------------------------------------------===//
56/// Forward declarations.
57///
58class DIE;
59class DIEValue;
60
61//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000062/// DWLabel - Labels are used to track locations in the assembler file.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000063/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
64/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer181b6c92007-08-05 20:06:04 +000065/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +000066class DWLabel {
67public:
Jim Laskeyef42a012006-11-02 20:12:39 +000068 /// Tag - Label category tag. Should always be a staticly declared C string.
69 ///
70 const char *Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000071
Jim Laskeyef42a012006-11-02 20:12:39 +000072 /// Number - Value to make label unique.
73 ///
74 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +000075
76 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000077
Jim Laskeyef42a012006-11-02 20:12:39 +000078 void Profile(FoldingSetNodeID &ID) const {
79 ID.AddString(std::string(Tag));
80 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +000081 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000082
Jim Laskeyef42a012006-11-02 20:12:39 +000083#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +000084 void print(std::ostream *O) const {
85 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +000086 }
Jim Laskeyef42a012006-11-02 20:12:39 +000087 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +000088 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +000089 if (Number) O << Number;
90 }
91#endif
Jim Laskeybd761842006-02-27 17:27:12 +000092};
93
94//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000095/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
96/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +000097class DIEAbbrevData {
98private:
Jim Laskeyef42a012006-11-02 20:12:39 +000099 /// Attribute - Dwarf attribute code.
100 ///
101 unsigned Attribute;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000102
Jim Laskeyef42a012006-11-02 20:12:39 +0000103 /// Form - Dwarf form code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000104 ///
105 unsigned Form;
106
Jim Laskey0d086af2006-02-27 12:43:29 +0000107public:
108 DIEAbbrevData(unsigned A, unsigned F)
109 : Attribute(A)
110 , Form(F)
111 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000112
Jim Laskeybd761842006-02-27 17:27:12 +0000113 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000114 unsigned getAttribute() const { return Attribute; }
115 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000116
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000117 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000118 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000119 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000120 ID.AddInteger(Attribute);
121 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000122 }
123};
Jim Laskey063e7652006-01-17 17:31:53 +0000124
Jim Laskey0d086af2006-02-27 12:43:29 +0000125//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000126/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
127/// information object.
128class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000129private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000130 /// Tag - Dwarf tag code.
131 ///
132 unsigned Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000133
Jim Laskeyef42a012006-11-02 20:12:39 +0000134 /// Unique number for node.
135 ///
136 unsigned Number;
137
138 /// ChildrenFlag - Dwarf children flag.
139 ///
140 unsigned ChildrenFlag;
141
142 /// Data - Raw data bytes for abbreviation.
143 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000144 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000145
Jim Laskey0d086af2006-02-27 12:43:29 +0000146public:
Jim Laskey063e7652006-01-17 17:31:53 +0000147
Jim Laskey0d086af2006-02-27 12:43:29 +0000148 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000149 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000150 , ChildrenFlag(C)
151 , Data()
152 {}
153 ~DIEAbbrev() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000154
Jim Laskeybd761842006-02-27 17:27:12 +0000155 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000156 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000157 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000158 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000159 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000160 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000161 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000162 void setNumber(unsigned N) { Number = N; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000163
Jim Laskey0d086af2006-02-27 12:43:29 +0000164 /// AddAttribute - Adds another set of attribute information to the
165 /// abbreviation.
166 void AddAttribute(unsigned Attribute, unsigned Form) {
167 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000168 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000169
Jim Laskeyb8509c52006-03-23 18:07:55 +0000170 /// AddFirstAttribute - Adds a set of attribute information to the front
171 /// of the abbreviation.
172 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
173 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
174 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000175
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000176 /// Profile - Used to gather unique data for the abbreviation folding set.
177 ///
178 void Profile(FoldingSetNodeID &ID) {
179 ID.AddInteger(Tag);
180 ID.AddInteger(ChildrenFlag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000181
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000182 // For each attribute description.
183 for (unsigned i = 0, N = Data.size(); i < N; ++i)
184 Data[i].Profile(ID);
185 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000186
Jim Laskey0d086af2006-02-27 12:43:29 +0000187 /// Emit - Print the abbreviation using the specified Dwarf writer.
188 ///
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000189 void Emit(const DwarfDebug &DD) const;
190
Jim Laskey0d086af2006-02-27 12:43:29 +0000191#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000192 void print(std::ostream *O) {
193 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000194 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000195 void print(std::ostream &O);
196 void dump();
197#endif
198};
Jim Laskey063e7652006-01-17 17:31:53 +0000199
Jim Laskey0d086af2006-02-27 12:43:29 +0000200//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000201/// DIE - A structured debug information entry. Has an abbreviation which
202/// describes it's organization.
203class DIE : public FoldingSetNode {
204protected:
205 /// Abbrev - Buffer for constructing abbreviation.
206 ///
207 DIEAbbrev Abbrev;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000208
Jim Laskeyef42a012006-11-02 20:12:39 +0000209 /// Offset - Offset in debug info section.
210 ///
211 unsigned Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000212
Jim Laskeyef42a012006-11-02 20:12:39 +0000213 /// Size - Size of instance + children.
214 ///
215 unsigned Size;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000216
Jim Laskeyef42a012006-11-02 20:12:39 +0000217 /// Children DIEs.
218 ///
219 std::vector<DIE *> Children;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000220
Jim Laskeyef42a012006-11-02 20:12:39 +0000221 /// Attributes values.
222 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000223 SmallVector<DIEValue*, 32> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000224
Jim Laskeyef42a012006-11-02 20:12:39 +0000225public:
Dan Gohman81975f62007-08-27 14:50:10 +0000226 explicit DIE(unsigned Tag)
Jim Laskeyef42a012006-11-02 20:12:39 +0000227 : Abbrev(Tag, DW_CHILDREN_no)
228 , Offset(0)
229 , Size(0)
230 , Children()
231 , Values()
232 {}
233 virtual ~DIE();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000234
Jim Laskeyef42a012006-11-02 20:12:39 +0000235 // Accessors.
236 DIEAbbrev &getAbbrev() { return Abbrev; }
237 unsigned getAbbrevNumber() const {
238 return Abbrev.getNumber();
239 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000240 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000241 unsigned getOffset() const { return Offset; }
242 unsigned getSize() const { return Size; }
243 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000244 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000245 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
246 void setOffset(unsigned O) { Offset = O; }
247 void setSize(unsigned S) { Size = S; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000248
Jim Laskeyef42a012006-11-02 20:12:39 +0000249 /// AddValue - Add a value and attributes to a DIE.
250 ///
251 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
252 Abbrev.AddAttribute(Attribute, Form);
253 Values.push_back(Value);
254 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000255
Jim Laskeyef42a012006-11-02 20:12:39 +0000256 /// SiblingOffset - Return the offset of the debug information entry's
257 /// sibling.
258 unsigned SiblingOffset() const { return Offset + Size; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000259
Jim Laskeyef42a012006-11-02 20:12:39 +0000260 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
261 ///
262 void AddSiblingOffset();
263
264 /// AddChild - Add a child to the DIE.
265 ///
266 void AddChild(DIE *Child) {
267 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
268 Children.push_back(Child);
269 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000270
Jim Laskeyef42a012006-11-02 20:12:39 +0000271 /// Detach - Detaches objects connected to it after copying.
272 ///
273 void Detach() {
274 Children.clear();
275 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000276
Jim Laskeyef42a012006-11-02 20:12:39 +0000277 /// Profile - Used to gather unique data for the value folding set.
278 ///
279 void Profile(FoldingSetNodeID &ID) ;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000280
Jim Laskeyef42a012006-11-02 20:12:39 +0000281#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000282 void print(std::ostream *O, unsigned IncIndent = 0) {
283 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000284 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000285 void print(std::ostream &O, unsigned IncIndent = 0);
286 void dump();
287#endif
288};
289
290//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000291/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000292///
293class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000294public:
295 enum {
296 isInteger,
297 isString,
298 isLabel,
299 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000300 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000301 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000302 isEntry,
303 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000304 };
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000305
Jim Laskeyef42a012006-11-02 20:12:39 +0000306 /// Type - Type of data stored in the value.
307 ///
308 unsigned Type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000309
Dan Gohman81975f62007-08-27 14:50:10 +0000310 explicit DIEValue(unsigned T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000311 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000312 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000313 virtual ~DIEValue() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000314
Jim Laskeyf6733882006-11-02 21:48:18 +0000315 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000316 unsigned getType() const { return Type; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000317
Jim Laskey0d086af2006-02-27 12:43:29 +0000318 // Implement isa/cast/dyncast.
319 static bool classof(const DIEValue *) { return true; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000320
Jim Laskey0d086af2006-02-27 12:43:29 +0000321 /// EmitValue - Emit value via the Dwarf writer.
322 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000323 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000324
Jim Laskey0d086af2006-02-27 12:43:29 +0000325 /// SizeOf - Return the size of a value in bytes.
326 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000327 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000328
Jim Laskeyef42a012006-11-02 20:12:39 +0000329 /// Profile - Used to gather unique data for the value folding set.
330 ///
331 virtual void Profile(FoldingSetNodeID &ID) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000332
Jim Laskeyef42a012006-11-02 20:12:39 +0000333#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000334 void print(std::ostream *O) {
335 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000336 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000337 virtual void print(std::ostream &O) = 0;
338 void dump();
339#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000340};
Jim Laskey063e7652006-01-17 17:31:53 +0000341
Jim Laskey0d086af2006-02-27 12:43:29 +0000342//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000343/// DWInteger - An integer value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000344///
Jim Laskey0d086af2006-02-27 12:43:29 +0000345class DIEInteger : public DIEValue {
346private:
347 uint64_t Integer;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000348
Jim Laskey0d086af2006-02-27 12:43:29 +0000349public:
Dan Gohman81975f62007-08-27 14:50:10 +0000350 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000351
Jim Laskey0d086af2006-02-27 12:43:29 +0000352 // Implement isa/cast/dyncast.
353 static bool classof(const DIEInteger *) { return true; }
354 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000355
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000356 /// BestForm - Choose the best form for integer.
357 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000358 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
359 if (IsSigned) {
360 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
361 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
362 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
363 } else {
364 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
365 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
366 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
367 }
368 return DW_FORM_data8;
369 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000370
Jim Laskey0d086af2006-02-27 12:43:29 +0000371 /// EmitValue - Emit integer of appropriate size.
372 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000373 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000374
Jim Laskey0d086af2006-02-27 12:43:29 +0000375 /// SizeOf - Determine size of integer value in bytes.
376 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000377 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000378
Jim Laskeyef42a012006-11-02 20:12:39 +0000379 /// Profile - Used to gather unique data for the value folding set.
380 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000381 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000382 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000383 ID.AddInteger(Integer);
384 }
Jim Laskey5496f012006-11-09 14:52:14 +0000385 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000386
Jim Laskeyef42a012006-11-02 20:12:39 +0000387#ifndef NDEBUG
388 virtual void print(std::ostream &O) {
389 O << "Int: " << (int64_t)Integer
390 << " 0x" << std::hex << Integer << std::dec;
391 }
392#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000393};
Jim Laskey063e7652006-01-17 17:31:53 +0000394
Jim Laskey0d086af2006-02-27 12:43:29 +0000395//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000396/// DIEString - A string value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000397///
Jim Laskeyef42a012006-11-02 20:12:39 +0000398class DIEString : public DIEValue {
399public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000400 const std::string String;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000401
Dan Gohman81975f62007-08-27 14:50:10 +0000402 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000403
Jim Laskey0d086af2006-02-27 12:43:29 +0000404 // Implement isa/cast/dyncast.
405 static bool classof(const DIEString *) { return true; }
406 static bool classof(const DIEValue *S) { return S->Type == isString; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000407
Jim Laskey0d086af2006-02-27 12:43:29 +0000408 /// EmitValue - Emit string value.
409 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000410 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000411
Jim Laskey0d086af2006-02-27 12:43:29 +0000412 /// SizeOf - Determine size of string value in bytes.
413 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000414 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000415 return String.size() + sizeof(char); // sizeof('\0');
416 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000417
Jim Laskeyef42a012006-11-02 20:12:39 +0000418 /// Profile - Used to gather unique data for the value folding set.
419 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000420 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000421 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000422 ID.AddString(String);
423 }
Jim Laskey5496f012006-11-09 14:52:14 +0000424 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000425
Jim Laskeyef42a012006-11-02 20:12:39 +0000426#ifndef NDEBUG
427 virtual void print(std::ostream &O) {
428 O << "Str: \"" << String << "\"";
429 }
430#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000431};
Jim Laskey063e7652006-01-17 17:31:53 +0000432
Jim Laskey0d086af2006-02-27 12:43:29 +0000433//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000434/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000435//
Jim Laskeyef42a012006-11-02 20:12:39 +0000436class DIEDwarfLabel : public DIEValue {
437public:
438
Jim Laskey0d086af2006-02-27 12:43:29 +0000439 const DWLabel Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000440
Dan Gohman81975f62007-08-27 14:50:10 +0000441 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000442
Jim Laskey0d086af2006-02-27 12:43:29 +0000443 // Implement isa/cast/dyncast.
444 static bool classof(const DIEDwarfLabel *) { return true; }
445 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000446
Jim Laskey0d086af2006-02-27 12:43:29 +0000447 /// EmitValue - Emit label value.
448 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000449 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000450
Jim Laskey0d086af2006-02-27 12:43:29 +0000451 /// SizeOf - Determine size of label value in bytes.
452 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000453 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000454
Jim Laskeyef42a012006-11-02 20:12:39 +0000455 /// Profile - Used to gather unique data for the value folding set.
456 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000457 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000458 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000459 Label.Profile(ID);
460 }
Jim Laskey5496f012006-11-09 14:52:14 +0000461 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000462
Jim Laskeyef42a012006-11-02 20:12:39 +0000463#ifndef NDEBUG
464 virtual void print(std::ostream &O) {
465 O << "Lbl: ";
466 Label.print(O);
467 }
468#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000469};
Jim Laskey063e7652006-01-17 17:31:53 +0000470
Jim Laskey063e7652006-01-17 17:31:53 +0000471
Jim Laskey0d086af2006-02-27 12:43:29 +0000472//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000473/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000474//
Jim Laskeyef42a012006-11-02 20:12:39 +0000475class DIEObjectLabel : public DIEValue {
476public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000477 const std::string Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000478
Dan Gohman81975f62007-08-27 14:50:10 +0000479 explicit DIEObjectLabel(const std::string &L)
480 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000481
Jim Laskey0d086af2006-02-27 12:43:29 +0000482 // Implement isa/cast/dyncast.
483 static bool classof(const DIEObjectLabel *) { return true; }
484 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000485
Jim Laskey0d086af2006-02-27 12:43:29 +0000486 /// EmitValue - Emit label value.
487 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000488 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000489
Jim Laskey0d086af2006-02-27 12:43:29 +0000490 /// SizeOf - Determine size of label value in bytes.
491 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000492 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000493
Jim Laskeyef42a012006-11-02 20:12:39 +0000494 /// Profile - Used to gather unique data for the value folding set.
495 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000496 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000497 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000498 ID.AddString(Label);
499 }
Jim Laskey5496f012006-11-09 14:52:14 +0000500 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000501
502#ifndef NDEBUG
503 virtual void print(std::ostream &O) {
504 O << "Obj: " << Label;
505 }
506#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000507};
Jim Laskey063e7652006-01-17 17:31:53 +0000508
Jim Laskey0d086af2006-02-27 12:43:29 +0000509//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000510/// DIESectionOffset - A section offset DIE.
511//
512class DIESectionOffset : public DIEValue {
513public:
514 const DWLabel Label;
515 const DWLabel Section;
516 bool IsEH : 1;
517 bool UseSet : 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000518
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000519 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
520 bool isEH = false, bool useSet = true)
521 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
522 IsEH(isEH), UseSet(useSet) {}
523
524 // Implement isa/cast/dyncast.
525 static bool classof(const DIESectionOffset *) { return true; }
526 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000527
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000528 /// EmitValue - Emit section offset.
529 ///
530 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000531
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000532 /// SizeOf - Determine size of section offset value in bytes.
533 ///
534 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000535
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000536 /// Profile - Used to gather unique data for the value folding set.
537 ///
538 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
539 const DWLabel &Section) {
540 ID.AddInteger(isSectionOffset);
541 Label.Profile(ID);
542 Section.Profile(ID);
543 // IsEH and UseSet are specific to the Label/Section that we will emit
544 // the offset for; so Label/Section are enough for uniqueness.
545 }
546 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
547
548#ifndef NDEBUG
549 virtual void print(std::ostream &O) {
550 O << "Off: ";
551 Label.print(O);
552 O << "-";
553 Section.print(O);
554 O << "-" << IsEH << "-" << UseSet;
555 }
556#endif
557};
558
559//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000560/// DIEDelta - A simple label difference DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000561///
Jim Laskeyef42a012006-11-02 20:12:39 +0000562class DIEDelta : public DIEValue {
563public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000564 const DWLabel LabelHi;
565 const DWLabel LabelLo;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000566
Jim Laskey0d086af2006-02-27 12:43:29 +0000567 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
568 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000569
Jim Laskey0d086af2006-02-27 12:43:29 +0000570 // Implement isa/cast/dyncast.
571 static bool classof(const DIEDelta *) { return true; }
572 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000573
Jim Laskey0d086af2006-02-27 12:43:29 +0000574 /// EmitValue - Emit delta value.
575 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000576 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000577
Jim Laskey0d086af2006-02-27 12:43:29 +0000578 /// SizeOf - Determine size of delta value in bytes.
579 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000580 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000581
Jim Laskeyef42a012006-11-02 20:12:39 +0000582 /// Profile - Used to gather unique data for the value folding set.
583 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000584 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
585 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000586 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000587 LabelHi.Profile(ID);
588 LabelLo.Profile(ID);
589 }
Jim Laskey5496f012006-11-09 14:52:14 +0000590 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000591
592#ifndef NDEBUG
593 virtual void print(std::ostream &O) {
594 O << "Del: ";
595 LabelHi.print(O);
596 O << "-";
597 LabelLo.print(O);
598 }
599#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000600};
Jim Laskey063e7652006-01-17 17:31:53 +0000601
Jim Laskey0d086af2006-02-27 12:43:29 +0000602//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000603/// DIEntry - A pointer to another debug information entry. An instance of this
604/// class can also be used as a proxy for a debug information entry not yet
605/// defined (ie. types.)
606class DIEntry : public DIEValue {
607public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000608 DIE *Entry;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000609
Dan Gohman81975f62007-08-27 14:50:10 +0000610 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000611
Jim Laskey0d086af2006-02-27 12:43:29 +0000612 // Implement isa/cast/dyncast.
613 static bool classof(const DIEntry *) { return true; }
614 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000615
Jim Laskeyb8509c52006-03-23 18:07:55 +0000616 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000617 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000618 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000619
Jim Laskeyb8509c52006-03-23 18:07:55 +0000620 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000621 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000622 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000623 return sizeof(int32_t);
624 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000625
Jim Laskeyef42a012006-11-02 20:12:39 +0000626 /// Profile - Used to gather unique data for the value folding set.
627 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000628 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
629 ID.AddInteger(isEntry);
630 ID.AddPointer(Entry);
631 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000632 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000633 ID.AddInteger(isEntry);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000634
Jim Laskeyef42a012006-11-02 20:12:39 +0000635 if (Entry) {
636 ID.AddPointer(Entry);
637 } else {
638 ID.AddPointer(this);
639 }
640 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000641
Jim Laskeyef42a012006-11-02 20:12:39 +0000642#ifndef NDEBUG
643 virtual void print(std::ostream &O) {
644 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
645 }
646#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000647};
648
649//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000650/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000651//
Jim Laskeyef42a012006-11-02 20:12:39 +0000652class DIEBlock : public DIEValue, public DIE {
653public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000654 unsigned Size; // Size in bytes excluding size header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000655
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000656 DIEBlock()
657 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000658 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000659 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000660 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000661 ~DIEBlock() {
662 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000663
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000664 // Implement isa/cast/dyncast.
665 static bool classof(const DIEBlock *) { return true; }
666 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000667
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000668 /// ComputeSize - calculate the size of the block.
669 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000670 unsigned ComputeSize(DwarfDebug &DD);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000671
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000672 /// BestForm - Choose the best form for data.
673 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000674 unsigned BestForm() const {
675 if ((unsigned char)Size == Size) return DW_FORM_block1;
676 if ((unsigned short)Size == Size) return DW_FORM_block2;
677 if ((unsigned int)Size == Size) return DW_FORM_block4;
678 return DW_FORM_block;
679 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000680
681 /// EmitValue - Emit block data.
682 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000683 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000684
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000685 /// SizeOf - Determine size of block data in bytes.
686 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000687 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000688
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000689
Jim Laskeyef42a012006-11-02 20:12:39 +0000690 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000691 ///
Reid Spencer97821312006-11-02 23:56:21 +0000692 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000693 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000694 DIE::Profile(ID);
695 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000696
Jim Laskeyef42a012006-11-02 20:12:39 +0000697#ifndef NDEBUG
698 virtual void print(std::ostream &O) {
699 O << "Blk: ";
700 DIE::print(O, 5);
701 }
702#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000703};
704
705//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000706/// CompileUnit - This dwarf writer support class manages information associate
707/// with a source file.
708class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000709private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000710 /// Desc - Compile unit debug descriptor.
711 ///
712 CompileUnitDesc *Desc;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000713
Jim Laskeyef42a012006-11-02 20:12:39 +0000714 /// ID - File identifier for source.
715 ///
716 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000717
Jim Laskeyef42a012006-11-02 20:12:39 +0000718 /// Die - Compile unit debug information entry.
719 ///
720 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000721
Jim Laskeyef42a012006-11-02 20:12:39 +0000722 /// DescToDieMap - Tracks the mapping of unit level debug informaton
723 /// descriptors to debug information entries.
724 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
725
726 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
727 /// descriptors to debug information entries using a DIEntry proxy.
728 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
729
730 /// Globals - A map of globally visible named entities for this unit.
731 ///
732 std::map<std::string, DIE *> Globals;
733
734 /// DiesSet - Used to uniquely define dies within the compile unit.
735 ///
736 FoldingSet<DIE> DiesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000737
Jim Laskeyef42a012006-11-02 20:12:39 +0000738 /// Dies - List of all dies in the compile unit.
739 ///
740 std::vector<DIE *> Dies;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000741
Jim Laskey0d086af2006-02-27 12:43:29 +0000742public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000743 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
744 : Desc(CUD)
745 , ID(I)
746 , Die(D)
747 , DescToDieMap()
748 , DescToDIEntryMap()
749 , Globals()
750 , DiesSet(InitDiesSetSize)
751 , Dies()
752 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000753
Jim Laskeyef42a012006-11-02 20:12:39 +0000754 ~CompileUnit() {
755 delete Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000756
Jim Laskeyef42a012006-11-02 20:12:39 +0000757 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
758 delete Dies[i];
759 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000760
Jim Laskeybd761842006-02-27 17:27:12 +0000761 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000762 CompileUnitDesc *getDesc() const { return Desc; }
763 unsigned getID() const { return ID; }
764 DIE* getDie() const { return Die; }
765 std::map<std::string, DIE *> &getGlobals() { return Globals; }
766
767 /// hasContent - Return true if this compile unit has something to write out.
768 ///
769 bool hasContent() const {
770 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000771 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000772
773 /// AddGlobal - Add a new global entity to the compile unit.
774 ///
775 void AddGlobal(const std::string &Name, DIE *Die) {
776 Globals[Name] = Die;
777 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000778
Jim Laskeyef42a012006-11-02 20:12:39 +0000779 /// getDieMapSlotFor - Returns the debug information entry map slot for the
780 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000781 DIE *&getDieMapSlotFor(DebugInfoDesc *DID) {
782 return DescToDieMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000783 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000784
Jim Laskeyef42a012006-11-02 20:12:39 +0000785 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
786 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000787 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DID) {
788 return DescToDIEntryMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000789 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000790
Jim Laskeyef42a012006-11-02 20:12:39 +0000791 /// AddDie - Adds or interns the DIE to the compile unit.
792 ///
793 DIE *AddDie(DIE &Buffer) {
794 FoldingSetNodeID ID;
795 Buffer.Profile(ID);
796 void *Where;
797 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000798
Jim Laskeyef42a012006-11-02 20:12:39 +0000799 if (!Die) {
800 Die = new DIE(Buffer);
801 DiesSet.InsertNode(Die, Where);
802 this->Die->AddChild(Die);
803 Buffer.Detach();
804 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000805
Jim Laskeyef42a012006-11-02 20:12:39 +0000806 return Die;
807 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000808};
809
Jim Laskey65195462006-10-30 13:35:07 +0000810//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000811/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000812///
Jim Laskey65195462006-10-30 13:35:07 +0000813class Dwarf {
814
Jim Laskey072200c2007-01-29 18:51:14 +0000815protected:
Jim Laskey65195462006-10-30 13:35:07 +0000816
817 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000818 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000819 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000820
Jim Laskey65195462006-10-30 13:35:07 +0000821 //
822 /// O - Stream to .s file.
823 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000824 raw_ostream &O;
Jim Laskey65195462006-10-30 13:35:07 +0000825
826 /// Asm - Target of Dwarf emission.
827 ///
828 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000829
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000830 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000831 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000832
Jim Laskey65195462006-10-30 13:35:07 +0000833 /// TD - Target data.
834 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000835
Jim Laskey65195462006-10-30 13:35:07 +0000836 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000837 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000838
Jim Laskey65195462006-10-30 13:35:07 +0000839 /// M - Current module.
840 ///
841 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000842
Jim Laskey65195462006-10-30 13:35:07 +0000843 /// MF - Current machine function.
844 ///
845 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000846
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000847 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000848 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000849 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000850
Jim Laskey65195462006-10-30 13:35:07 +0000851 /// SubprogramCount - The running count of functions being compiled.
852 ///
853 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000854
Chris Lattner9251f132007-09-24 03:35:37 +0000855 /// Flavor - A unique string indicating what dwarf producer this is, used to
856 /// unique labels.
857 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000858
859 unsigned SetCounter;
Owen Andersoncb371882008-08-21 00:14:44 +0000860 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattner9251f132007-09-24 03:35:37 +0000861 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000862 : O(OS)
863 , Asm(A)
864 , TAI(T)
865 , TD(Asm->TM.getTargetData())
866 , RI(Asm->TM.getRegisterInfo())
867 , M(NULL)
868 , MF(NULL)
869 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000870 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000871 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000872 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000873 {
874 }
875
876public:
877
878 //===--------------------------------------------------------------------===//
879 // Accessors.
880 //
881 AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000882 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000883 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000884 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000885
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000886 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
887 const {
888 if (isInSection && TAI->getDwarfSectionOffsetDirective())
889 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000890 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000891 O << TAI->getData32bitsDirective();
892 else
893 O << TAI->getData64bitsDirective();
894 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000895
Jim Laskeyb82313f2007-02-01 16:31:34 +0000896 /// PrintLabelName - Print label name in form used by Dwarf writer.
897 ///
898 void PrintLabelName(DWLabel Label) const {
899 PrintLabelName(Label.Tag, Label.Number);
900 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000901 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000902 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000903 if (Number) O << Number;
904 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000905
Chris Lattner9251f132007-09-24 03:35:37 +0000906 void PrintLabelName(const char *Tag, unsigned Number,
907 const char *Suffix) const {
908 O << TAI->getPrivateGlobalPrefix() << Tag;
909 if (Number) O << Number;
910 O << Suffix;
911 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000912
Jim Laskeyb82313f2007-02-01 16:31:34 +0000913 /// EmitLabel - Emit location label for internal use by Dwarf.
914 ///
915 void EmitLabel(DWLabel Label) const {
916 EmitLabel(Label.Tag, Label.Number);
917 }
918 void EmitLabel(const char *Tag, unsigned Number) const {
919 PrintLabelName(Tag, Number);
920 O << ":\n";
921 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000922
Jim Laskeyb82313f2007-02-01 16:31:34 +0000923 /// EmitReference - Emit a reference to a label.
924 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000925 void EmitReference(DWLabel Label, bool IsPCRelative = false,
926 bool Force32Bit = false) const {
927 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000928 }
929 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000930 bool IsPCRelative = false, bool Force32Bit = false) const {
931 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000932 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000933
Jim Laskeyb82313f2007-02-01 16:31:34 +0000934 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
935 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000936 void EmitReference(const std::string &Name, bool IsPCRelative = false,
937 bool Force32Bit = false) const {
938 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000939
Jim Laskeyb82313f2007-02-01 16:31:34 +0000940 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000941
Jim Laskeyb82313f2007-02-01 16:31:34 +0000942 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
943 }
944
945 /// EmitDifference - Emit the difference between two labels. Some
946 /// assemblers do not behave with absolute expressions with data directives,
947 /// so there is an option (needsSet) to use an intermediary set expression.
948 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000949 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000950 EmitDifference(LabelHi.Tag, LabelHi.Number,
951 LabelLo.Tag, LabelLo.Number,
952 IsSmall);
953 }
954 void EmitDifference(const char *TagHi, unsigned NumberHi,
955 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000956 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000957 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000958 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000959 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000960 O << ",";
961 PrintLabelName(TagHi, NumberHi);
962 O << "-";
963 PrintLabelName(TagLo, NumberLo);
964 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000965
966 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +0000967 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000968 ++SetCounter;
969 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000970 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000971
Jim Laskeyb82313f2007-02-01 16:31:34 +0000972 PrintLabelName(TagHi, NumberHi);
973 O << "-";
974 PrintLabelName(TagLo, NumberLo);
975 }
976 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000977
978 void EmitSectionOffset(const char* Label, const char* Section,
979 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000980 bool IsSmall = false, bool isEH = false,
981 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000982 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000983 if (isEH)
984 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
985 else
986 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
987
988 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000989 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000990 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000991 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000992 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000993
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000994 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000995 O << "-";
996 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000997 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000998 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000999
1000 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001001
Chris Lattner9251f132007-09-24 03:35:37 +00001002 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001003 ++SetCounter;
1004 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001005 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001006
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001007 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001008
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001009 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001010 O << "-";
1011 PrintLabelName(Section, SectionNumber);
1012 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001013 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001014 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001015
Jim Laskeyb82313f2007-02-01 16:31:34 +00001016 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1017 /// frame.
1018 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +00001019 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001020 int stackGrowth =
1021 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1022 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001023 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +00001024 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +00001025
1026 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001027 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +00001028 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001029
Jim Laskeyb82313f2007-02-01 16:31:34 +00001030 if (LabelID) {
1031 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001032
Jim Laskeyb82313f2007-02-01 16:31:34 +00001033 // Throw out move if the label is invalid.
1034 if (!LabelID) continue;
1035 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001036
Jim Laskeyb82313f2007-02-01 16:31:34 +00001037 const MachineLocation &Dst = Move.getDestination();
1038 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001039
Jim Laskeyb82313f2007-02-01 16:31:34 +00001040 // Advance row if new location.
1041 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1042 Asm->EmitInt8(DW_CFA_advance_loc4);
1043 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001044 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1045 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001046
Jim Laskeyb82313f2007-02-01 16:31:34 +00001047 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001048 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001049 IsLocal = true;
1050 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001051
Jim Laskeyb82313f2007-02-01 16:31:34 +00001052 // If advancing cfa.
Dan Gohmand735b802008-10-03 15:45:36 +00001053 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1054 if (!Src.isReg()) {
1055 if (Src.getReg() == MachineLocation::VirtualFP) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001056 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1057 Asm->EOL("DW_CFA_def_cfa_offset");
1058 } else {
1059 Asm->EmitInt8(DW_CFA_def_cfa);
1060 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmand735b802008-10-03 15:45:36 +00001061 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001062 Asm->EOL("Register");
1063 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001064
Jim Laskeyb82313f2007-02-01 16:31:34 +00001065 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001066
Jim Laskeyb82313f2007-02-01 16:31:34 +00001067 Asm->EmitULEB128Bytes(Offset);
1068 Asm->EOL("Offset");
1069 } else {
1070 assert(0 && "Machine move no supported yet.");
1071 }
Dan Gohmand735b802008-10-03 15:45:36 +00001072 } else if (Src.isReg() &&
1073 Src.getReg() == MachineLocation::VirtualFP) {
1074 if (Dst.isReg()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001075 Asm->EmitInt8(DW_CFA_def_cfa_register);
1076 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmand735b802008-10-03 15:45:36 +00001077 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001078 Asm->EOL("Register");
1079 } else {
1080 assert(0 && "Machine move no supported yet.");
1081 }
1082 } else {
Dan Gohmand735b802008-10-03 15:45:36 +00001083 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001084 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001085
Jim Laskeyb82313f2007-02-01 16:31:34 +00001086 if (Offset < 0) {
1087 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1088 Asm->EOL("DW_CFA_offset_extended_sf");
1089 Asm->EmitULEB128Bytes(Reg);
1090 Asm->EOL("Reg");
1091 Asm->EmitSLEB128Bytes(Offset);
1092 Asm->EOL("Offset");
1093 } else if (Reg < 64) {
1094 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Chengd4114192008-07-09 21:53:02 +00001095 if (VerboseAsm)
1096 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1097 else
1098 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001099 Asm->EmitULEB128Bytes(Offset);
1100 Asm->EOL("Offset");
1101 } else {
1102 Asm->EmitInt8(DW_CFA_offset_extended);
1103 Asm->EOL("DW_CFA_offset_extended");
1104 Asm->EmitULEB128Bytes(Reg);
1105 Asm->EOL("Reg");
1106 Asm->EmitULEB128Bytes(Offset);
1107 Asm->EOL("Offset");
1108 }
1109 }
1110 }
1111 }
1112
Jim Laskey072200c2007-01-29 18:51:14 +00001113};
1114
1115//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001116/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001117///
1118class DwarfDebug : public Dwarf {
1119
1120private:
Jim Laskey65195462006-10-30 13:35:07 +00001121 //===--------------------------------------------------------------------===//
1122 // Attributes used to construct specific Dwarf sections.
1123 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001124
Jim Laskey65195462006-10-30 13:35:07 +00001125 /// CompileUnits - All the compile units involved in this build. The index
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001126 /// of each entry in this vector corresponds to the sources in MMI.
Jim Laskey65195462006-10-30 13:35:07 +00001127 std::vector<CompileUnit *> CompileUnits;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001128
Jim Laskeyef42a012006-11-02 20:12:39 +00001129 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001130 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001131 FoldingSet<DIEAbbrev> AbbreviationsSet;
1132
1133 /// Abbreviations - A list of all the unique abbreviations in use.
1134 ///
1135 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001136
Jim Laskeyef42a012006-11-02 20:12:39 +00001137 /// ValuesSet - Used to uniquely define values.
1138 ///
1139 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001140
Jim Laskeyef42a012006-11-02 20:12:39 +00001141 /// Values - A list of all the unique values in use.
1142 ///
1143 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001144
Jim Laskey65195462006-10-30 13:35:07 +00001145 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001146 ///
Jim Laskey65195462006-10-30 13:35:07 +00001147 UniqueVector<std::string> StringPool;
1148
1149 /// UnitMap - Map debug information descriptor to compile unit.
1150 ///
1151 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001152
Jim Laskey65195462006-10-30 13:35:07 +00001153 /// SectionMap - Provides a unique id per text section.
1154 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001155 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001156
Jim Laskey65195462006-10-30 13:35:07 +00001157 /// SectionSourceLines - Tracks line numbers per text section.
1158 ///
1159 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1160
Jim Laskeybacd3042007-02-21 22:48:45 +00001161 /// didInitial - Flag to indicate if initial emission has been done.
1162 ///
1163 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001164
Jim Laskeybacd3042007-02-21 22:48:45 +00001165 /// shouldEmit - Flag to indicate if debug information should be emitted.
1166 ///
1167 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001168
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001169 struct FunctionDebugFrameInfo {
1170 unsigned Number;
1171 std::vector<MachineMove> Moves;
1172
1173 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001174 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001175 };
1176
1177 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001178
Jim Laskey65195462006-10-30 13:35:07 +00001179public:
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001180
Jim Laskeybacd3042007-02-21 22:48:45 +00001181 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1182 ///
1183 bool ShouldEmitDwarf() const { return shouldEmit; }
Jim Laskey65195462006-10-30 13:35:07 +00001184
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001185 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001186 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001187 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1188 // Profile the node so that we can make it unique.
1189 FoldingSetNodeID ID;
1190 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001191
Jim Laskeyef42a012006-11-02 20:12:39 +00001192 // Check the set for priors.
1193 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001194
Jim Laskeyef42a012006-11-02 20:12:39 +00001195 // If it's newly added.
1196 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001197 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001198 Abbreviations.push_back(&Abbrev);
1199 // Assign the vector position + 1 as its number.
1200 Abbrev.setNumber(Abbreviations.size());
1201 } else {
1202 // Assign existing abbreviation number.
1203 Abbrev.setNumber(InSet->getNumber());
1204 }
1205 }
1206
Jim Laskey65195462006-10-30 13:35:07 +00001207 /// NewString - Add a string to the constant pool and returns a label.
1208 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001209 DWLabel NewString(const std::string &String) {
1210 unsigned StringID = StringPool.insert(String);
1211 return DWLabel("string", StringID);
1212 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001213
Jim Laskeyef42a012006-11-02 20:12:39 +00001214 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1215 /// entry.
1216 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1217 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001218
Jim Laskeyef42a012006-11-02 20:12:39 +00001219 if (Entry) {
1220 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001221 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001222 void *Where;
1223 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001224
Jim Laskeyf6733882006-11-02 21:48:18 +00001225 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001226
Jim Laskeyef42a012006-11-02 20:12:39 +00001227 Value = new DIEntry(Entry);
1228 ValuesSet.InsertNode(Value, Where);
1229 } else {
1230 Value = new DIEntry(Entry);
1231 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001232
Jim Laskeyef42a012006-11-02 20:12:39 +00001233 Values.push_back(Value);
1234 return Value;
1235 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001236
Jim Laskeyef42a012006-11-02 20:12:39 +00001237 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1238 ///
1239 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1240 Value->Entry = Entry;
1241 // Add to values set if not already there. If it is, we merely have a
1242 // duplicate in the values list (no harm.)
1243 ValuesSet.GetOrInsertNode(Value);
1244 }
1245
1246 /// AddUInt - Add an unsigned integer attribute data and value.
1247 ///
1248 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1249 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1250
1251 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001252 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001253 void *Where;
1254 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1255 if (!Value) {
1256 Value = new DIEInteger(Integer);
1257 ValuesSet.InsertNode(Value, Where);
1258 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001259 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001260
Jim Laskeyef42a012006-11-02 20:12:39 +00001261 Die->AddValue(Attribute, Form, Value);
1262 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001263
Jim Laskeyef42a012006-11-02 20:12:39 +00001264 /// AddSInt - Add an signed integer attribute data and value.
1265 ///
1266 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1267 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1268
1269 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001270 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001271 void *Where;
1272 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1273 if (!Value) {
1274 Value = new DIEInteger(Integer);
1275 ValuesSet.InsertNode(Value, Where);
1276 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001277 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001278
Jim Laskeyef42a012006-11-02 20:12:39 +00001279 Die->AddValue(Attribute, Form, Value);
1280 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001281
Jim Laskeyef42a012006-11-02 20:12:39 +00001282 /// AddString - Add a std::string attribute data and value.
1283 ///
1284 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1285 const std::string &String) {
1286 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001287 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001288 void *Where;
1289 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1290 if (!Value) {
1291 Value = new DIEString(String);
1292 ValuesSet.InsertNode(Value, Where);
1293 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001294 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001295
Jim Laskeyef42a012006-11-02 20:12:39 +00001296 Die->AddValue(Attribute, Form, Value);
1297 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001298
Jim Laskeyef42a012006-11-02 20:12:39 +00001299 /// AddLabel - Add a Dwarf label attribute data and value.
1300 ///
1301 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1302 const DWLabel &Label) {
1303 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001304 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001305 void *Where;
1306 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1307 if (!Value) {
1308 Value = new DIEDwarfLabel(Label);
1309 ValuesSet.InsertNode(Value, Where);
1310 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001311 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001312
Jim Laskeyef42a012006-11-02 20:12:39 +00001313 Die->AddValue(Attribute, Form, Value);
1314 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001315
Jim Laskeyef42a012006-11-02 20:12:39 +00001316 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1317 ///
1318 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1319 const std::string &Label) {
1320 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001321 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001322 void *Where;
1323 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1324 if (!Value) {
1325 Value = new DIEObjectLabel(Label);
1326 ValuesSet.InsertNode(Value, Where);
1327 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001328 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001329
Jim Laskeyef42a012006-11-02 20:12:39 +00001330 Die->AddValue(Attribute, Form, Value);
1331 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001332
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001333 /// AddSectionOffset - Add a section offset label attribute data and value.
1334 ///
1335 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1336 const DWLabel &Label, const DWLabel &Section,
1337 bool isEH = false, bool useSet = true) {
1338 FoldingSetNodeID ID;
1339 DIESectionOffset::Profile(ID, Label, Section);
1340 void *Where;
1341 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1342 if (!Value) {
1343 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1344 ValuesSet.InsertNode(Value, Where);
1345 Values.push_back(Value);
1346 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001347
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001348 Die->AddValue(Attribute, Form, Value);
1349 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001350
Jim Laskeyef42a012006-11-02 20:12:39 +00001351 /// AddDelta - Add a label delta attribute data and value.
1352 ///
1353 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1354 const DWLabel &Hi, const DWLabel &Lo) {
1355 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001356 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001357 void *Where;
1358 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1359 if (!Value) {
1360 Value = new DIEDelta(Hi, Lo);
1361 ValuesSet.InsertNode(Value, Where);
1362 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001363 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001364
Jim Laskeyef42a012006-11-02 20:12:39 +00001365 Die->AddValue(Attribute, Form, Value);
1366 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001367
Jim Laskeyef42a012006-11-02 20:12:39 +00001368 /// AddDIEntry - Add a DIE attribute data and value.
1369 ///
1370 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1371 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1372 }
1373
1374 /// AddBlock - Add block data.
1375 ///
1376 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1377 Block->ComputeSize(*this);
1378 FoldingSetNodeID ID;
1379 Block->Profile(ID);
1380 void *Where;
1381 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1382 if (!Value) {
1383 Value = Block;
1384 ValuesSet.InsertNode(Value, Where);
1385 Values.push_back(Value);
1386 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001387 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001388 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001389 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001390 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001391
Jim Laskeyef42a012006-11-02 20:12:39 +00001392 Die->AddValue(Attribute, Block->BestForm(), Value);
1393 }
1394
Jim Laskey65195462006-10-30 13:35:07 +00001395private:
1396
1397 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001398 /// entry.
1399 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1400 if (File && Line) {
1401 CompileUnit *FileUnit = FindCompileUnit(File);
1402 unsigned FileID = FileUnit->getID();
1403 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1404 AddUInt(Die, DW_AT_decl_line, 0, Line);
1405 }
1406 }
Jim Laskey65195462006-10-30 13:35:07 +00001407
1408 /// AddAddress - Add an address attribute to a die based on the location
1409 /// provided.
1410 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001411 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001412 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001413 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001414
Dan Gohmand735b802008-10-03 15:45:36 +00001415 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001416 if (Reg < 32) {
1417 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1418 } else {
1419 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1420 AddUInt(Block, 0, DW_FORM_udata, Reg);
1421 }
1422 } else {
1423 if (Reg < 32) {
1424 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1425 } else {
1426 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1427 AddUInt(Block, 0, DW_FORM_udata, Reg);
1428 }
1429 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1430 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001431
Jim Laskeyef42a012006-11-02 20:12:39 +00001432 AddBlock(Die, Attribute, 0, Block);
1433 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001434
Jim Laskeyef42a012006-11-02 20:12:39 +00001435 /// AddBasicType - Add a new basic type attribute to the specified entity.
1436 ///
1437 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1438 const std::string &Name,
1439 unsigned Encoding, unsigned Size) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001440
Jim Laskeyef42a012006-11-02 20:12:39 +00001441 DIE Buffer(DW_TAG_base_type);
1442 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1443 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1444 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel9ede3f22009-01-05 17:44:11 +00001445 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1446 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001447 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001448
Jim Laskeyef42a012006-11-02 20:12:39 +00001449 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1450 ///
1451 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001452 DIE Buffer(DW_TAG_pointer_type);
Dan Gohman82482942007-09-27 23:12:31 +00001453 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Jim Laskeyef42a012006-11-02 20:12:39 +00001454 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelca03c272009-01-05 17:45:59 +00001455 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1456 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001457 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001458
Jim Laskeyef42a012006-11-02 20:12:39 +00001459 /// AddType - Add a new type attribute to the specified entity.
1460 ///
1461 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1462 if (!TyDesc) {
Jim Laskey2b935d52007-01-26 14:19:17 +00001463 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001464 } else {
1465 // Check for pre-existence.
1466 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001467
Jim Laskeyef42a012006-11-02 20:12:39 +00001468 // If it exists then use the existing value.
1469 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001470 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1471 return;
1472 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001473
Jim Laskeyef42a012006-11-02 20:12:39 +00001474 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1475 // FIXME - Not sure why programs and variables are coming through here.
1476 // Short cut for handling subprogram types (not really a TyDesc.)
1477 AddPointerType(Entity, Unit, SubprogramTy->getName());
1478 } else if (GlobalVariableDesc *GlobalTy =
1479 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1480 // FIXME - Not sure why programs and variables are coming through here.
1481 // Short cut for handling global variable types (not really a TyDesc.)
1482 AddPointerType(Entity, Unit, GlobalTy->getName());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001483 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001484 // Set up proxy.
1485 Slot = NewDIEntry();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001486
Jim Laskeyef42a012006-11-02 20:12:39 +00001487 // Construct type.
1488 DIE Buffer(DW_TAG_base_type);
1489 ConstructType(Buffer, TyDesc, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001490
Jim Laskeyef42a012006-11-02 20:12:39 +00001491 // Add debug information entry to entity and unit.
1492 DIE *Die = Unit->AddDie(Buffer);
1493 SetDIEntry(Slot, Die);
1494 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1495 }
1496 }
1497 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001498
Jim Laskeyef42a012006-11-02 20:12:39 +00001499 /// ConstructType - Adds all the required attributes to the type.
1500 ///
1501 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1502 // Get core information.
1503 const std::string &Name = TyDesc->getName();
1504 uint64_t Size = TyDesc->getSize() >> 3;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001505
Jim Laskeyef42a012006-11-02 20:12:39 +00001506 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1507 // Fundamental types like int, float, bool
1508 Buffer.setTag(DW_TAG_base_type);
1509 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1510 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00001511 // Fetch tag.
1512 unsigned Tag = DerivedTy->getTag();
1513 // FIXME - Workaround for templates.
1514 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001515 // Pointers, typedefs et al.
Jim Laskey85f419b2006-11-09 16:32:26 +00001516 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00001517 // Map to main type, void will not have a type.
1518 if (TypeDesc *FromTy = DerivedTy->getFromType())
1519 AddType(&Buffer, FromTy, Unit);
1520 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1521 // Fetch tag.
1522 unsigned Tag = CompTy->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001523
Jim Laskeyef42a012006-11-02 20:12:39 +00001524 // Set tag accordingly.
1525 if (Tag == DW_TAG_vector_type)
1526 Buffer.setTag(DW_TAG_array_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001527 else
Jim Laskeyef42a012006-11-02 20:12:39 +00001528 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00001529
Jim Laskeyef42a012006-11-02 20:12:39 +00001530 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001531
Jim Laskeyef42a012006-11-02 20:12:39 +00001532 switch (Tag) {
1533 case DW_TAG_vector_type:
1534 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1535 // Fall thru
1536 case DW_TAG_array_type: {
1537 // Add element type.
1538 if (TypeDesc *FromTy = CompTy->getFromType())
1539 AddType(&Buffer, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001540
Jim Laskeyef42a012006-11-02 20:12:39 +00001541 // Don't emit size attribute.
1542 Size = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001543
Jim Laskeyef42a012006-11-02 20:12:39 +00001544 // Construct an anonymous type for index type.
Devang Patel9ede3f22009-01-05 17:44:11 +00001545 DIE Buffer(DW_TAG_base_type);
1546 AddUInt(&Buffer, DW_AT_byte_size, 0, sizeof(int32_t));
1547 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1548 DIE *IndexTy = Unit->AddDie(Buffer);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001549
Jim Laskeyef42a012006-11-02 20:12:39 +00001550 // Add subranges to array type.
Evan Chengf8480282008-12-09 17:56:30 +00001551 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001552 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1553 int64_t Lo = SRD->getLo();
1554 int64_t Hi = SRD->getHi();
1555 DIE *Subrange = new DIE(DW_TAG_subrange_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001556
Jim Laskeyef42a012006-11-02 20:12:39 +00001557 // If a range is available.
1558 if (Lo != Hi) {
1559 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1560 // Only add low if non-zero.
1561 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1562 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1563 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001564
Jim Laskeyef42a012006-11-02 20:12:39 +00001565 Buffer.AddChild(Subrange);
1566 }
1567 break;
1568 }
1569 case DW_TAG_structure_type:
1570 case DW_TAG_union_type: {
1571 // Add elements to structure type.
Evan Chengf8480282008-12-09 17:56:30 +00001572 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001573 DebugInfoDesc *Element = Elements[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001574
Jim Laskeyef42a012006-11-02 20:12:39 +00001575 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1576 // Add field or base class.
Jim Laskeyef42a012006-11-02 20:12:39 +00001577 unsigned Tag = MemberDesc->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001578
Jim Laskeyef42a012006-11-02 20:12:39 +00001579 // Extract the basic information.
1580 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001581 uint64_t Size = MemberDesc->getSize();
1582 uint64_t Align = MemberDesc->getAlign();
1583 uint64_t Offset = MemberDesc->getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001584
Jim Laskeyef42a012006-11-02 20:12:39 +00001585 // Construct member debug information entry.
1586 DIE *Member = new DIE(Tag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001587
Jim Laskeyef42a012006-11-02 20:12:39 +00001588 // Add name if not "".
1589 if (!Name.empty())
1590 AddString(Member, DW_AT_name, DW_FORM_string, Name);
Evan Chengf8480282008-12-09 17:56:30 +00001591
Jim Laskeyef42a012006-11-02 20:12:39 +00001592 // Add location if available.
1593 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001594
Jim Laskeyef42a012006-11-02 20:12:39 +00001595 // Most of the time the field info is the same as the members.
1596 uint64_t FieldSize = Size;
1597 uint64_t FieldAlign = Align;
1598 uint64_t FieldOffset = Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001599
Jim Laskeyee5f9272006-12-22 20:03:42 +00001600 // Set the member type.
1601 TypeDesc *FromTy = MemberDesc->getFromType();
1602 AddType(Member, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001603
Jim Laskeyee5f9272006-12-22 20:03:42 +00001604 // Walk up typedefs until a real size is found.
1605 while (FromTy) {
1606 if (FromTy->getTag() != DW_TAG_typedef) {
1607 FieldSize = FromTy->getSize();
Devang Pateld0935c32008-12-23 21:55:38 +00001608 FieldAlign = FromTy->getAlign();
Jim Laskeyee5f9272006-12-22 20:03:42 +00001609 break;
1610 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001611
Dan Gohman275769a2007-07-23 20:24:29 +00001612 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
Jim Laskeyef42a012006-11-02 20:12:39 +00001613 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001614
Jim Laskeyef42a012006-11-02 20:12:39 +00001615 // Unless we have a bit field.
1616 if (Tag == DW_TAG_member && FieldSize != Size) {
1617 // Construct the alignment mask.
1618 uint64_t AlignMask = ~(FieldAlign - 1);
1619 // Determine the high bit + 1 of the declared size.
1620 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1621 // Work backwards to determine the base offset of the field.
1622 FieldOffset = HiMark - FieldSize;
1623 // Now normalize offset to the field.
1624 Offset -= FieldOffset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001625
Jim Laskeyef42a012006-11-02 20:12:39 +00001626 // Maybe we need to work from the other end.
1627 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001628
Jim Laskeyef42a012006-11-02 20:12:39 +00001629 // Add size and offset.
1630 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1631 AddUInt(Member, DW_AT_bit_size, 0, Size);
1632 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1633 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001634
Jim Laskeyef42a012006-11-02 20:12:39 +00001635 // Add computation for offset.
1636 DIEBlock *Block = new DIEBlock();
1637 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1638 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1639 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1640
1641 // Add accessibility (public default unless is base class.
1642 if (MemberDesc->isProtected()) {
1643 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1644 } else if (MemberDesc->isPrivate()) {
1645 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1646 } else if (Tag == DW_TAG_inheritance) {
1647 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1648 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001649
Jim Laskeyef42a012006-11-02 20:12:39 +00001650 Buffer.AddChild(Member);
1651 } else if (GlobalVariableDesc *StaticDesc =
1652 dyn_cast<GlobalVariableDesc>(Element)) {
1653 // Add static member.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001654
Jim Laskeyef42a012006-11-02 20:12:39 +00001655 // Construct member debug information entry.
1656 DIE *Static = new DIE(DW_TAG_variable);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001657
Jim Laskeyef42a012006-11-02 20:12:39 +00001658 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001659 const std::string &Name = StaticDesc->getName();
1660 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001661 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001662 if (!LinkageName.empty()) {
1663 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1664 LinkageName);
1665 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001666
Jim Laskeyef42a012006-11-02 20:12:39 +00001667 // Add location.
1668 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001669
Jim Laskeyef42a012006-11-02 20:12:39 +00001670 // Add type.
1671 if (TypeDesc *StaticTy = StaticDesc->getType())
1672 AddType(Static, StaticTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001673
Jim Laskeyef42a012006-11-02 20:12:39 +00001674 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001675 if (!StaticDesc->isStatic())
1676 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001677 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001678
Jim Laskeyef42a012006-11-02 20:12:39 +00001679 Buffer.AddChild(Static);
1680 } else if (SubprogramDesc *MethodDesc =
1681 dyn_cast<SubprogramDesc>(Element)) {
1682 // Add member function.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001683
Jim Laskeyef42a012006-11-02 20:12:39 +00001684 // Construct member debug information entry.
1685 DIE *Method = new DIE(DW_TAG_subprogram);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001686
Jim Laskeyef42a012006-11-02 20:12:39 +00001687 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001688 const std::string &Name = MethodDesc->getName();
1689 const std::string &LinkageName = MethodDesc->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001690
1691 AddString(Method, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001692 bool IsCTor = TyDesc->getName() == Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001693
Jim Laskey2172f962006-11-30 14:35:45 +00001694 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001695 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001696 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001697 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001698
Jim Laskeyef42a012006-11-02 20:12:39 +00001699 // Add location.
1700 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001701
Jim Laskeyef42a012006-11-02 20:12:39 +00001702 // Add type.
1703 if (CompositeTypeDesc *MethodTy =
1704 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1705 // Get argument information.
1706 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001707
Jim Laskeyef42a012006-11-02 20:12:39 +00001708 // If not a ctor.
1709 if (!IsCTor) {
1710 // Add return type.
1711 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1712 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001713
Jim Laskeyef42a012006-11-02 20:12:39 +00001714 // Add arguments.
Evan Chengf8480282008-12-09 17:56:30 +00001715 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001716 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1717 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1718 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1719 Method->AddChild(Arg);
1720 }
1721 }
1722
1723 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001724 if (!MethodDesc->isStatic())
1725 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001726 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001727
Jim Laskeyef42a012006-11-02 20:12:39 +00001728 Buffer.AddChild(Method);
1729 }
1730 }
1731 break;
1732 }
1733 case DW_TAG_enumeration_type: {
1734 // Add enumerators to enumeration type.
Evan Chengf8480282008-12-09 17:56:30 +00001735 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001736 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1737 const std::string &Name = ED->getName();
1738 int64_t Value = ED->getValue();
1739 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1740 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1741 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1742 Buffer.AddChild(Enumerator);
1743 }
1744
1745 break;
1746 }
1747 case DW_TAG_subroutine_type: {
1748 // Add prototype flag.
1749 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1750 // Add return type.
1751 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001752
Jim Laskeyef42a012006-11-02 20:12:39 +00001753 // Add arguments.
Evan Chengf8480282008-12-09 17:56:30 +00001754 for (unsigned i = 1, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001755 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1756 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1757 Buffer.AddChild(Arg);
1758 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001759
Jim Laskeyef42a012006-11-02 20:12:39 +00001760 break;
1761 }
1762 default: break;
1763 }
1764 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001765
Jim Laskeyef42a012006-11-02 20:12:39 +00001766 // Add name if not anonymous or intermediate type.
1767 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Evan Chengf8480282008-12-09 17:56:30 +00001768
Evan Cheng94ea5be2008-12-10 00:15:44 +00001769 // Add size if non-zero (derived types might be zero-sized.)
1770 if (Size)
1771 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1772 else if (isa<CompositeTypeDesc>(TyDesc)) {
1773 // If TyDesc is a composite type, then add size even if it's zero unless
1774 // it's a forward declaration.
1775 if (TyDesc->isForwardDecl())
1776 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1777 else
1778 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1779 }
1780
1781 // Add source line info if available and TyDesc is not a forward
1782 // declaration.
1783 if (!TyDesc->isForwardDecl())
1784 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
Jim Laskeyef42a012006-11-02 20:12:39 +00001785 }
1786
1787 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00001788 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001789 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1790 // Construct debug information entry.
1791 DIE *Die = new DIE(DW_TAG_compile_unit);
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001792 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
1793 DWLabel("section_line", 0), DWLabel("section_line", 0), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001794 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1795 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1796 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
Devang Patel83580992008-12-12 21:57:54 +00001797 if (!UnitDesc->getDirectory().empty())
1798 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001799
Jim Laskeyef42a012006-11-02 20:12:39 +00001800 // Construct compile unit.
1801 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001802
Jim Laskeyef42a012006-11-02 20:12:39 +00001803 // Add Unit to compile unit map.
1804 DescToUnitMap[UnitDesc] = Unit;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001805
Jim Laskeyef42a012006-11-02 20:12:39 +00001806 return Unit;
1807 }
1808
Jim Laskey9d4209f2006-11-07 19:33:46 +00001809 /// GetBaseCompileUnit - Get the main compile unit.
1810 ///
1811 CompileUnit *GetBaseCompileUnit() const {
1812 CompileUnit *Unit = CompileUnits[0];
1813 assert(Unit && "Missing compile unit.");
1814 return Unit;
1815 }
1816
Jim Laskey65195462006-10-30 13:35:07 +00001817 /// FindCompileUnit - Get the compile unit for the given descriptor.
1818 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001819 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001820 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00001821 assert(Unit && "Missing compile unit.");
1822 return Unit;
1823 }
1824
1825 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00001826 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001827 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1828 // Get the compile unit context.
1829 CompileUnitDesc *UnitDesc =
1830 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001831 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001832
1833 // Check for pre-existence.
1834 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1835 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001836
Jim Laskeyef42a012006-11-02 20:12:39 +00001837 // Get the global variable itself.
1838 GlobalVariable *GV = GVD->getGlobalVariable();
1839
Jim Laskey2172f962006-11-30 14:35:45 +00001840 const std::string &Name = GVD->getName();
1841 const std::string &FullName = GVD->getFullName();
1842 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001843 // Create the global's variable DIE.
1844 DIE *VariableDie = new DIE(DW_TAG_variable);
1845 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001846 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001847 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001848 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001849 }
Jim Laskey6488a072007-01-08 22:15:18 +00001850 AddType(VariableDie, GVD->getType(), Unit);
1851 if (!GVD->isStatic())
1852 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001853
Jim Laskeyef42a012006-11-02 20:12:39 +00001854 // Add source line info if available.
1855 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001856
Jim Laskeyef42a012006-11-02 20:12:39 +00001857 // Add address.
1858 DIEBlock *Block = new DIEBlock();
1859 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00001860 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
1861 AddBlock(VariableDie, DW_AT_location, 0, Block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001862
Jim Laskeyef42a012006-11-02 20:12:39 +00001863 // Add to map.
1864 Slot = VariableDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001865
Jim Laskeyef42a012006-11-02 20:12:39 +00001866 // Add to context owner.
1867 Unit->getDie()->AddChild(VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001868
Jim Laskeyef42a012006-11-02 20:12:39 +00001869 // Expose as global.
1870 // FIXME - need to check external flag.
Jim Laskey2172f962006-11-30 14:35:45 +00001871 Unit->AddGlobal(FullName, VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001872
Jim Laskeyef42a012006-11-02 20:12:39 +00001873 return VariableDie;
1874 }
Jim Laskey65195462006-10-30 13:35:07 +00001875
1876 /// NewSubprogram - Add a new subprogram DIE.
1877 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001878 DIE *NewSubprogram(SubprogramDesc *SPD) {
1879 // Get the compile unit context.
1880 CompileUnitDesc *UnitDesc =
1881 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001882 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001883
1884 // Check for pre-existence.
1885 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1886 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001887
Jim Laskeyef42a012006-11-02 20:12:39 +00001888 // Gather the details (simplify add attribute code.)
Jim Laskey2172f962006-11-30 14:35:45 +00001889 const std::string &Name = SPD->getName();
1890 const std::string &FullName = SPD->getFullName();
1891 const std::string &LinkageName = SPD->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001892
Jim Laskeyef42a012006-11-02 20:12:39 +00001893 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1894 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001895 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001896 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001897 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001898 }
1899 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
Jim Laskey6488a072007-01-08 22:15:18 +00001900 if (!SPD->isStatic())
1901 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001902 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001903
Jim Laskeyef42a012006-11-02 20:12:39 +00001904 // Add source line info if available.
1905 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1906
1907 // Add to map.
1908 Slot = SubprogramDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001909
Jim Laskeyef42a012006-11-02 20:12:39 +00001910 // Add to context owner.
1911 Unit->getDie()->AddChild(SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001912
Jim Laskeyef42a012006-11-02 20:12:39 +00001913 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00001914 Unit->AddGlobal(FullName, SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001915
Jim Laskeyef42a012006-11-02 20:12:39 +00001916 return SubprogramDie;
1917 }
Jim Laskey65195462006-10-30 13:35:07 +00001918
1919 /// NewScopeVariable - Create a new scope variable.
1920 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001921 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1922 // Get the descriptor.
1923 VariableDesc *VD = DV->getDesc();
1924
1925 // Translate tag to proper Dwarf tag. The result variable is dropped for
1926 // now.
1927 unsigned Tag;
1928 switch (VD->getTag()) {
1929 case DW_TAG_return_variable: return NULL;
1930 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1931 case DW_TAG_auto_variable: // fall thru
1932 default: Tag = DW_TAG_variable; break;
1933 }
1934
1935 // Define variable debug information entry.
1936 DIE *VariableDie = new DIE(Tag);
1937 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1938
1939 // Add source line info if available.
1940 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001941
Jim Laskeyef42a012006-11-02 20:12:39 +00001942 // Add variable type.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001943 AddType(VariableDie, VD->getType(), Unit);
1944
Jim Laskeyef42a012006-11-02 20:12:39 +00001945 // Add variable address.
1946 MachineLocation Location;
Evan Cheng72bebb92008-01-31 03:37:28 +00001947 Location.set(RI->getFrameRegister(*MF),
1948 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Jim Laskeyef42a012006-11-02 20:12:39 +00001949 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00001950
Jim Laskeyef42a012006-11-02 20:12:39 +00001951 return VariableDie;
1952 }
Jim Laskey65195462006-10-30 13:35:07 +00001953
1954 /// ConstructScope - Construct the components of a scope.
1955 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001956 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00001957 unsigned ParentStartID, unsigned ParentEndID,
1958 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001959 // Add variables to scope.
1960 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1961 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1962 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1963 if (VariableDie) ParentDie->AddChild(VariableDie);
1964 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001965
Jim Laskeyef42a012006-11-02 20:12:39 +00001966 // Add nested scopes.
1967 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1968 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1969 // Define the Scope debug information entry.
1970 DebugScope *Scope = Scopes[j];
1971 // FIXME - Ignore inlined functions for the time being.
1972 if (!Scope->getParent()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001973
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001974 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1975 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00001976
Jim Laskey9d4209f2006-11-07 19:33:46 +00001977 // Ignore empty scopes.
1978 if (StartID == EndID && StartID != 0) continue;
1979 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001980
Jim Laskey36729dd2006-11-29 16:55:57 +00001981 if (StartID == ParentStartID && EndID == ParentEndID) {
1982 // Just add stuff to the parent scope.
1983 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00001984 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00001985 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001986
Jim Laskey36729dd2006-11-29 16:55:57 +00001987 // Add the scope bounds.
1988 if (StartID) {
1989 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00001990 DWLabel("label", StartID));
Jim Laskey36729dd2006-11-29 16:55:57 +00001991 } else {
1992 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1993 DWLabel("func_begin", SubprogramCount));
1994 }
1995 if (EndID) {
1996 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00001997 DWLabel("label", EndID));
Jim Laskey36729dd2006-11-29 16:55:57 +00001998 } else {
1999 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2000 DWLabel("func_end", SubprogramCount));
2001 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002002
Jim Laskey36729dd2006-11-29 16:55:57 +00002003 // Add the scope contents.
2004 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
2005 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00002006 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002007 }
2008 }
Jim Laskey65195462006-10-30 13:35:07 +00002009
2010 /// ConstructRootScope - Construct the scope for the subprogram.
2011 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002012 void ConstructRootScope(DebugScope *RootScope) {
2013 // Exit if there is no root scope.
2014 if (!RootScope) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002015
2016 // Get the subprogram debug information entry.
Jim Laskeyef42a012006-11-02 20:12:39 +00002017 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002018
Jim Laskeyef42a012006-11-02 20:12:39 +00002019 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00002020 CompileUnit *Unit = GetBaseCompileUnit();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002021
Jim Laskeyef42a012006-11-02 20:12:39 +00002022 // Get the subprogram die.
2023 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2024 assert(SPDie && "Missing subprogram descriptor");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002025
Jim Laskeyef42a012006-11-02 20:12:39 +00002026 // Add the function bounds.
2027 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2028 DWLabel("func_begin", SubprogramCount));
2029 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2030 DWLabel("func_end", SubprogramCount));
2031 MachineLocation Location(RI->getFrameRegister(*MF));
2032 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002033
Jim Laskey36729dd2006-11-29 16:55:57 +00002034 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002035 }
Jim Laskey65195462006-10-30 13:35:07 +00002036
Bill Wendlingd751c642008-09-26 00:28:12 +00002037 /// ConstructDefaultScope - Construct a default scope for the subprogram.
2038 ///
2039 void ConstructDefaultScope(MachineFunction *MF) {
2040 // Find the correct subprogram descriptor.
2041 std::vector<SubprogramDesc *> Subprograms;
2042 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
2043
2044 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2045 SubprogramDesc *SPD = Subprograms[i];
2046
2047 if (SPD->getName() == MF->getFunction()->getName()) {
2048 // Get the compile unit context.
2049 CompileUnit *Unit = GetBaseCompileUnit();
2050
2051 // Get the subprogram die.
2052 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2053 assert(SPDie && "Missing subprogram descriptor");
2054
2055 // Add the function bounds.
2056 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2057 DWLabel("func_begin", SubprogramCount));
2058 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2059 DWLabel("func_end", SubprogramCount));
2060
2061 MachineLocation Location(RI->getFrameRegister(*MF));
2062 AddAddress(SPDie, DW_AT_frame_base, Location);
2063 return;
2064 }
2065 }
Bill Wendlinga9f0cc42008-10-17 18:48:57 +00002066#if 0
2067 // FIXME: This is causing an abort because C++ mangled names are compared
2068 // with their unmangled counterparts. See PR2885. Don't do this assert.
Bill Wendlingd751c642008-09-26 00:28:12 +00002069 assert(0 && "Couldn't find DIE for machine function!");
Bill Wendlinga9f0cc42008-10-17 18:48:57 +00002070#endif
Bill Wendlingd751c642008-09-26 00:28:12 +00002071 }
2072
Jim Laskeyef42a012006-11-02 20:12:39 +00002073 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2074 /// tools to recognize the object file contains Dwarf information.
2075 void EmitInitial() {
2076 // Check to see if we already emitted intial headers.
2077 if (didInitial) return;
2078 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002079
Jim Laskeyef42a012006-11-02 20:12:39 +00002080 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002081 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002082 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002083 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002084 }
2085 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2086 EmitLabel("section_info", 0);
2087 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2088 EmitLabel("section_abbrev", 0);
2089 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2090 EmitLabel("section_aranges", 0);
2091 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2092 EmitLabel("section_macinfo", 0);
2093 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2094 EmitLabel("section_line", 0);
2095 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2096 EmitLabel("section_loc", 0);
2097 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2098 EmitLabel("section_pubnames", 0);
2099 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2100 EmitLabel("section_str", 0);
2101 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2102 EmitLabel("section_ranges", 0);
2103
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002104 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002105 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002106 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002107 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002108 }
2109
Jim Laskey65195462006-10-30 13:35:07 +00002110 /// EmitDIE - Recusively Emits a debug information entry.
2111 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002112 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002113 // Get the abbreviation for this DIE.
2114 unsigned AbbrevNumber = Die->getAbbrevNumber();
2115 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002116
Jim Laskeybacd3042007-02-21 22:48:45 +00002117 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002118
2119 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002120 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002121
2122 if (VerboseAsm)
2123 Asm->EOL(std::string("Abbrev [" +
2124 utostr(AbbrevNumber) +
2125 "] 0x" + utohexstr(Die->getOffset()) +
2126 ":0x" + utohexstr(Die->getSize()) + " " +
2127 TagString(Abbrev->getTag())));
2128 else
2129 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002130
Owen Anderson873e1b52008-06-24 21:44:59 +00002131 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2132 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002133
Jim Laskeyef42a012006-11-02 20:12:39 +00002134 // Emit the DIE attribute values.
2135 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2136 unsigned Attr = AbbrevData[i].getAttribute();
2137 unsigned Form = AbbrevData[i].getForm();
2138 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002139
Jim Laskeyef42a012006-11-02 20:12:39 +00002140 switch (Attr) {
2141 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002142 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002143 break;
2144 }
2145 default: {
2146 // Emit an attribute using the defined form.
2147 Values[i]->EmitValue(*this, Form);
2148 break;
2149 }
2150 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002151
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002152 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002153 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002154
Jim Laskeyef42a012006-11-02 20:12:39 +00002155 // Emit the DIE children if any.
2156 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2157 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002158
Jim Laskeyef42a012006-11-02 20:12:39 +00002159 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2160 EmitDIE(Children[j]);
2161 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002162
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002163 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002164 }
2165 }
2166
Jim Laskey65195462006-10-30 13:35:07 +00002167 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2168 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002169 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2170 // Get the children.
2171 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002172
Jim Laskeyef42a012006-11-02 20:12:39 +00002173 // If not last sibling and has children then add sibling offset attribute.
2174 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2175
2176 // Record the abbreviation.
2177 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002178
Jim Laskeyef42a012006-11-02 20:12:39 +00002179 // Get the abbreviation for this DIE.
2180 unsigned AbbrevNumber = Die->getAbbrevNumber();
2181 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2182
2183 // Set DIE offset
2184 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002185
Jim Laskeyef42a012006-11-02 20:12:39 +00002186 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002187 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2188
Owen Anderson873e1b52008-06-24 21:44:59 +00002189 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2190 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002191
2192 // Size the DIE attribute values.
2193 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2194 // Size attribute value.
2195 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2196 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002197
Jim Laskeyef42a012006-11-02 20:12:39 +00002198 // Size the DIE children if any.
2199 if (!Children.empty()) {
2200 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2201 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002202
Jim Laskeyef42a012006-11-02 20:12:39 +00002203 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2204 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2205 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002206
Jim Laskeyef42a012006-11-02 20:12:39 +00002207 // End of children marker.
2208 Offset += sizeof(int8_t);
2209 }
2210
2211 Die->setSize(Offset - Die->getOffset());
2212 return Offset;
2213 }
Jim Laskey65195462006-10-30 13:35:07 +00002214
2215 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2216 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002217 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002218 // Process base compile unit.
2219 CompileUnit *Unit = GetBaseCompileUnit();
2220 // Compute size of compile unit header
2221 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2222 sizeof(int16_t) + // DWARF version number
2223 sizeof(int32_t) + // Offset Into Abbrev. Section
2224 sizeof(int8_t); // Pointer Size (in bytes)
2225 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002226 }
2227
Jim Laskey65195462006-10-30 13:35:07 +00002228 /// EmitDebugInfo - Emit the debug info section.
2229 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002230 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002231 // Start debug info section.
2232 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002233
Jim Laskey5496f012006-11-09 14:52:14 +00002234 CompileUnit *Unit = GetBaseCompileUnit();
2235 DIE *Die = Unit->getDie();
2236 // Emit the compile units header.
2237 EmitLabel("info_begin", Unit->getID());
2238 // Emit size of content not including length itself
2239 unsigned ContentSize = Die->getSize() +
2240 sizeof(int16_t) + // DWARF version number
2241 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00002242 sizeof(int8_t) + // Pointer Size (in bytes)
2243 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002244
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002245 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2246 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002247 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002248 Asm->EOL("Offset Into Abbrev. Section");
Dan Gohman82482942007-09-27 23:12:31 +00002249 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002250
Jim Laskey5496f012006-11-09 14:52:14 +00002251 EmitDIE(Die);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002252 // FIXME - extra padding for gdb bug.
2253 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2254 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2255 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2256 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Jim Laskey5496f012006-11-09 14:52:14 +00002257 EmitLabel("info_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002258
Jim Laskeybacd3042007-02-21 22:48:45 +00002259 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002260 }
2261
Jim Laskey65195462006-10-30 13:35:07 +00002262 /// EmitAbbreviations - Emit the abbreviation section.
2263 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002264 void EmitAbbreviations() const {
2265 // Check to see if it is worth the effort.
2266 if (!Abbreviations.empty()) {
2267 // Start the debug abbrev section.
2268 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002269
Jim Laskeyef42a012006-11-02 20:12:39 +00002270 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002271
Jim Laskeyef42a012006-11-02 20:12:39 +00002272 // For each abbrevation.
2273 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2274 // Get abbreviation data
2275 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002276
Jim Laskeyef42a012006-11-02 20:12:39 +00002277 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002278 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2279 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002280
Jim Laskeyef42a012006-11-02 20:12:39 +00002281 // Emit the abbreviations data.
2282 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002283
Jim Laskeybacd3042007-02-21 22:48:45 +00002284 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002285 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002286
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002287 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002288 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002289
Jim Laskeyef42a012006-11-02 20:12:39 +00002290 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002291
Jim Laskeybacd3042007-02-21 22:48:45 +00002292 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002293 }
2294 }
2295
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002296 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2297 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002298 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002299 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2300 // Define last address of section.
2301 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2302 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2303 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2304 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2305
2306 // Mark end of matrix.
2307 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2308 Asm->EmitULEB128Bytes(1); Asm->EOL();
2309 Asm->EmitInt8(1); Asm->EOL();
2310 }
2311
Jim Laskey65195462006-10-30 13:35:07 +00002312 /// EmitDebugLines - Emit source line information.
2313 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002314 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002315 // If the target is using .loc/.file, the assembler will be emitting the
2316 // .debug_line table automatically.
2317 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002318 return;
2319
Jim Laskeyef42a012006-11-02 20:12:39 +00002320 // Minimum line delta, thus ranging from -10..(255-10).
2321 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2322 // Maximum line delta, thus ranging from -10..(255-10).
2323 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002324
Jim Laskeyef42a012006-11-02 20:12:39 +00002325 // Start the dwarf line section.
2326 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002327
Jim Laskeyef42a012006-11-02 20:12:39 +00002328 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002329
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002330 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002331 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002332 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002333
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002334 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002335
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002336 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002337 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002338 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002339
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002340 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002341
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002342 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002343
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002344 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002345
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002346 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002347
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002348 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002349
Jim Laskeyef42a012006-11-02 20:12:39 +00002350 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002351 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2352 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2353 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2354 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2355 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2356 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2357 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2358 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2359 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002360
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002361 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Evan Cheng6547e402008-07-01 23:18:29 +00002362 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
Jim Laskeyef42a012006-11-02 20:12:39 +00002363
2364 // Emit directories.
2365 for (unsigned DirectoryID = 1, NDID = Directories.size();
2366 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002367 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002368 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002369 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002370
Jim Laskeyef42a012006-11-02 20:12:39 +00002371 // Emit files.
2372 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2373 SourceID <= NSID; ++SourceID) {
2374 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002375 Asm->EmitString(SourceFile.getName());
2376 Asm->EOL("Source");
2377 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2378 Asm->EOL("Directory #");
2379 Asm->EmitULEB128Bytes(0);
2380 Asm->EOL("Mod date");
2381 Asm->EmitULEB128Bytes(0);
2382 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002383 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002384 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002385
Jim Laskeyef42a012006-11-02 20:12:39 +00002386 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002387
Jim Laskeyef42a012006-11-02 20:12:39 +00002388 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002389 unsigned SecSrcLinesSize = SectionSourceLines.size();
2390
2391 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002392 // Isolate current sections line info.
2393 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002394
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002395 if (VerboseAsm) {
2396 const Section* S = SectionMap[j + 1];
2397 Asm->EOL(std::string("Section ") + S->getName());
2398 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002399 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002400
2401 // Dwarf assumes we start with first line of first source file.
2402 unsigned Source = 1;
2403 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002404
Jim Laskeyef42a012006-11-02 20:12:39 +00002405 // Construct rows of the address, source, line, column matrix.
2406 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2407 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002408 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002409 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002410
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002411 unsigned SourceID = LineInfo.getSourceID();
2412 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2413 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng6547e402008-07-01 23:18:29 +00002414 if (VerboseAsm)
2415 Asm->EOL(Directories[DirectoryID]
2416 + SourceFile.getName()
2417 + ":"
2418 + utostr_32(LineInfo.getLine()));
2419 else
2420 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002421
2422 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002423 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002424 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002425 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002426 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002427
Jim Laskeyef42a012006-11-02 20:12:39 +00002428 // If change of source, then switch to the new source.
2429 if (Source != LineInfo.getSourceID()) {
2430 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002431 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2432 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002433 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002434
Jim Laskeyef42a012006-11-02 20:12:39 +00002435 // If change of line.
2436 if (Line != LineInfo.getLine()) {
2437 // Determine offset.
2438 int Offset = LineInfo.getLine() - Line;
2439 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002440
Jim Laskeyef42a012006-11-02 20:12:39 +00002441 // Update line.
2442 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002443
Jim Laskeyef42a012006-11-02 20:12:39 +00002444 // If delta is small enough and in range...
2445 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2446 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002447 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002448 } else {
2449 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002450 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2451 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2452 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002453 }
2454 } else {
2455 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002456 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002457 }
2458 }
2459
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002460 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002461 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002462
2463 if (SecSrcLinesSize == 0)
2464 // Because we're emitting a debug_line section, we still need a line
2465 // table. The linker and friends expect it to exist. If there's nothing to
2466 // put into it, emit an empty table.
2467 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002468
Jim Laskeyef42a012006-11-02 20:12:39 +00002469 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002470
Jim Laskeybacd3042007-02-21 22:48:45 +00002471 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002472 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002473
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002474 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002475 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002476 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002477 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002478 return;
2479
2480 int stackGrowth =
2481 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2482 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002483 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002484
2485 // Start the dwarf frame section.
2486 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2487
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002488 EmitLabel("debug_frame_common", 0);
2489 EmitDifference("debug_frame_common_end", 0,
2490 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002491 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002492
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002493 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002494 Asm->EmitInt32((int)DW_CIE_ID);
2495 Asm->EOL("CIE Identifier Tag");
2496 Asm->EmitInt8(DW_CIE_VERSION);
2497 Asm->EOL("CIE Version");
2498 Asm->EmitString("");
2499 Asm->EOL("CIE Augmentation");
2500 Asm->EmitULEB128Bytes(1);
2501 Asm->EOL("CIE Code Alignment Factor");
2502 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002503 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002504 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002505 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002506
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002507 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002508 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002509
Dale Johannesenb97aec62007-11-13 19:13:01 +00002510 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002511
Evan Cheng05548eb2008-02-29 19:36:59 +00002512 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002513 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002514
Jim Laskeybacd3042007-02-21 22:48:45 +00002515 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002516 }
2517
Jim Laskey65195462006-10-30 13:35:07 +00002518 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2519 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002520 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002521 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002522 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002523
Jim Laskeyef42a012006-11-02 20:12:39 +00002524 // Start the dwarf frame section.
2525 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002526
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002527 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2528 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002529 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002530
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002531 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002532
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002533 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2534 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002535 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002536
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002537 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002538 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002539 EmitDifference("func_end", DebugFrameInfo.Number,
2540 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002541 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002542
Dale Johannesenb97aec62007-11-13 19:13:01 +00002543 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002544
Evan Cheng05548eb2008-02-29 19:36:59 +00002545 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002546 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002547
Jim Laskeybacd3042007-02-21 22:48:45 +00002548 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002549 }
2550
2551 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002552 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002553 void EmitDebugPubNames() {
2554 // Start the dwarf pubnames section.
2555 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002556
2557 CompileUnit *Unit = GetBaseCompileUnit();
2558
Jim Laskey5496f012006-11-09 14:52:14 +00002559 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002560 "pubnames_begin", Unit->getID(), true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002561 Asm->EOL("Length of Public Names Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002562
Jim Laskey5496f012006-11-09 14:52:14 +00002563 EmitLabel("pubnames_begin", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002564
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002565 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002566
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002567 EmitSectionOffset("info_begin", "section_info",
2568 Unit->getID(), 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002569 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002570
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002571 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002572 Asm->EOL("Compilation Unit Length");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002573
Jim Laskey5496f012006-11-09 14:52:14 +00002574 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002575
Jim Laskey5496f012006-11-09 14:52:14 +00002576 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2577 GE = Globals.end();
2578 GI != GE; ++GI) {
2579 const std::string &Name = GI->first;
2580 DIE * Entity = GI->second;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002581
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002582 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2583 Asm->EmitString(Name); Asm->EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00002584 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002585
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002586 Asm->EmitInt32(0); Asm->EOL("End Mark");
Jim Laskey5496f012006-11-09 14:52:14 +00002587 EmitLabel("pubnames_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002588
Jim Laskeybacd3042007-02-21 22:48:45 +00002589 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002590 }
2591
2592 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002593 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002594 void EmitDebugStr() {
2595 // Check to see if it is worth the effort.
2596 if (!StringPool.empty()) {
2597 // Start the dwarf str section.
2598 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002599
Jim Laskeyef42a012006-11-02 20:12:39 +00002600 // For each of strings in the string pool.
2601 for (unsigned StringID = 1, N = StringPool.size();
2602 StringID <= N; ++StringID) {
2603 // Emit a label for reference from debug information entries.
2604 EmitLabel("string", StringID);
2605 // Emit the string itself.
2606 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002607 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002608 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002609
Jim Laskeybacd3042007-02-21 22:48:45 +00002610 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002611 }
2612 }
2613
2614 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002615 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002616 void EmitDebugLoc() {
2617 // Start the dwarf loc section.
2618 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002619
Jim Laskeybacd3042007-02-21 22:48:45 +00002620 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002621 }
2622
2623 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002624 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002625 void EmitDebugARanges() {
2626 // Start the dwarf aranges section.
2627 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002628
Jim Laskeyef42a012006-11-02 20:12:39 +00002629 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002630#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002631 CompileUnit *Unit = GetBaseCompileUnit();
2632
Jim Laskey5496f012006-11-09 14:52:14 +00002633 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002634 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002635
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002636 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002637
Jim Laskey5496f012006-11-09 14:52:14 +00002638 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002639 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002640
Dan Gohman82482942007-09-27 23:12:31 +00002641 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002642
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002643 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002644
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002645 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2646 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002647
Jim Laskey5496f012006-11-09 14:52:14 +00002648 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002649 EmitReference("text_begin", 0); Asm->EOL("Address");
2650 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002651
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002652 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2653 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002654#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002655
Jim Laskeybacd3042007-02-21 22:48:45 +00002656 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002657 }
2658
2659 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002660 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002661 void EmitDebugRanges() {
2662 // Start the dwarf ranges section.
2663 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002664
Jim Laskeybacd3042007-02-21 22:48:45 +00002665 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002666 }
2667
2668 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002669 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002670 void EmitDebugMacInfo() {
2671 // Start the dwarf macinfo section.
2672 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002673
Jim Laskeybacd3042007-02-21 22:48:45 +00002674 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002675 }
2676
Jim Laskey65195462006-10-30 13:35:07 +00002677 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2678 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002679 void ConstructCompileUnitDIEs() {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002680 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002681
Jim Laskeyef42a012006-11-02 20:12:39 +00002682 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002683 unsigned ID = MMI->RecordSource(CUW[i]);
Jim Laskey9d4209f2006-11-07 19:33:46 +00002684 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002685 CompileUnits.push_back(Unit);
2686 }
2687 }
2688
Jim Laskey65195462006-10-30 13:35:07 +00002689 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2690 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002691 void ConstructGlobalDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00002692 std::vector<GlobalVariableDesc *> GlobalVariables;
2693 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M, GlobalVariables);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002694
Bill Wendling10fff602008-07-03 22:53:42 +00002695 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2696 GlobalVariableDesc *GVD = GlobalVariables[i];
2697 NewGlobalVariable(GVD);
2698 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002699 }
Jim Laskey65195462006-10-30 13:35:07 +00002700
2701 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2702 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002703 void ConstructSubprogramDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00002704 std::vector<SubprogramDesc *> Subprograms;
2705 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002706
Bill Wendling10fff602008-07-03 22:53:42 +00002707 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2708 SubprogramDesc *SPD = Subprograms[i];
2709 NewSubprogram(SPD);
2710 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002711 }
Jim Laskey65195462006-10-30 13:35:07 +00002712
Jim Laskey65195462006-10-30 13:35:07 +00002713public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002714 //===--------------------------------------------------------------------===//
2715 // Main entry points.
2716 //
Owen Andersoncb371882008-08-21 00:14:44 +00002717 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00002718 : Dwarf(OS, A, T, "dbg")
Jim Laskeyef42a012006-11-02 20:12:39 +00002719 , CompileUnits()
2720 , AbbreviationsSet(InitAbbreviationsSetSize)
2721 , Abbreviations()
2722 , ValuesSet(InitValuesSetSize)
2723 , Values()
2724 , StringPool()
2725 , DescToUnitMap()
2726 , SectionMap()
2727 , SectionSourceLines()
Jim Laskeybacd3042007-02-21 22:48:45 +00002728 , didInitial(false)
2729 , shouldEmit(false)
Jim Laskeyef42a012006-11-02 20:12:39 +00002730 {
2731 }
Jim Laskey072200c2007-01-29 18:51:14 +00002732 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002733 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2734 delete CompileUnits[i];
2735 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2736 delete Values[j];
2737 }
2738
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002739 /// SetModuleInfo - Set machine module information when it's known that pass
2740 /// manager has created it. Set by the target AsmPrinter.
2741 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002742 // Make sure initial declarations are made.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002743 if (!MMI && mmi->hasDebugInfo()) {
2744 MMI = mmi;
Jim Laskeyef42a012006-11-02 20:12:39 +00002745 shouldEmit = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002746
Jim Laskeyef42a012006-11-02 20:12:39 +00002747 // Create all the compile unit DIEs.
2748 ConstructCompileUnitDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002749
Jim Laskeyef42a012006-11-02 20:12:39 +00002750 // Create DIEs for each of the externally visible global variables.
2751 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002752
Jim Laskeyef42a012006-11-02 20:12:39 +00002753 // Create DIEs for each of the externally visible subprograms.
2754 ConstructSubprogramDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002755
Jim Laskeyef42a012006-11-02 20:12:39 +00002756 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00002757 SectionMap.insert(TAI->getTextSection());
Dan Gohmand57c3882007-10-01 22:40:20 +00002758
2759 // Print out .file directives to specify files for .loc directives. These
2760 // are printed out early so that they precede any .loc directives.
2761 if (TAI->hasDotLocAndDotFile()) {
2762 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
2763 const UniqueVector<std::string> &Directories = MMI->getDirectories();
2764 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
2765 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
2766 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
2767 assert(AppendOk && "Could not append filename to directory!");
Devang Pateld0935c32008-12-23 21:55:38 +00002768 AppendOk = false;
Dan Gohmand57c3882007-10-01 22:40:20 +00002769 Asm->EmitFile(i, FullPath.toString());
2770 Asm->EOL();
2771 }
2772 }
2773
2774 // Emit initial sections
2775 EmitInitial();
Jim Laskeyef42a012006-11-02 20:12:39 +00002776 }
2777 }
2778
Jim Laskey65195462006-10-30 13:35:07 +00002779 /// BeginModule - Emit all Dwarf sections that should come prior to the
2780 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002781 void BeginModule(Module *M) {
2782 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00002783 }
2784
Jim Laskey65195462006-10-30 13:35:07 +00002785 /// EndModule - Emit all Dwarf sections that should come after the content.
2786 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002787 void EndModule() {
2788 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002789
Jim Laskeyef42a012006-11-02 20:12:39 +00002790 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002791 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002792 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002793 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002794 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002795
Jim Laskeyef42a012006-11-02 20:12:39 +00002796 // End text sections.
2797 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002798 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002799 EmitLabel("section_end", i);
2800 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002801
2802 // Emit common frame information.
2803 EmitCommonDebugFrame();
2804
2805 // Emit function debug frame information
2806 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2807 E = DebugFrames.end(); I != E; ++I)
2808 EmitFunctionDebugFrame(*I);
2809
Jim Laskeyef42a012006-11-02 20:12:39 +00002810 // Compute DIE offsets and sizes.
2811 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002812
Jim Laskeyef42a012006-11-02 20:12:39 +00002813 // Emit all the DIEs into a debug info section
2814 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002815
Jim Laskeyef42a012006-11-02 20:12:39 +00002816 // Corresponding abbreviations into a abbrev section.
2817 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002818
Jim Laskeyef42a012006-11-02 20:12:39 +00002819 // Emit source line correspondence into a debug line section.
2820 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002821
Jim Laskeyef42a012006-11-02 20:12:39 +00002822 // Emit info into a debug pubnames section.
2823 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002824
Jim Laskeyef42a012006-11-02 20:12:39 +00002825 // Emit info into a debug str section.
2826 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002827
Jim Laskeyef42a012006-11-02 20:12:39 +00002828 // Emit info into a debug loc section.
2829 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002830
Jim Laskeyef42a012006-11-02 20:12:39 +00002831 // Emit info into a debug aranges section.
2832 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002833
Jim Laskeyef42a012006-11-02 20:12:39 +00002834 // Emit info into a debug ranges section.
2835 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002836
Jim Laskeyef42a012006-11-02 20:12:39 +00002837 // Emit info into a debug macinfo section.
2838 EmitDebugMacInfo();
2839 }
2840
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002841 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00002842 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002843 void BeginFunction(MachineFunction *MF) {
2844 this->MF = MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002845
Jim Laskeyef42a012006-11-02 20:12:39 +00002846 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002847
2848 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002849 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002850
Jim Laskeyef42a012006-11-02 20:12:39 +00002851 // Assumes in correct section after the entry point.
2852 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00002853
2854 // Emit label for the implicitly defined dbg.stoppoint at the start of
2855 // the function.
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00002856 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
2857 if (!LineInfos.empty()) {
2858 const SourceLineInfo &LineInfo = LineInfos[0];
2859 Asm->printLabel(LineInfo.getLabelID());
2860 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002861 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002862
Jim Laskey65195462006-10-30 13:35:07 +00002863 /// EndFunction - Gather and emit post-function debug information.
2864 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00002865 void EndFunction(MachineFunction *MF) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002866 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002867
Jim Laskeyb82313f2007-02-01 16:31:34 +00002868 // Define end label for subprogram.
2869 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002870
Jim Laskeyef42a012006-11-02 20:12:39 +00002871 // Get function line info.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002872 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
Jim Laskeyef42a012006-11-02 20:12:39 +00002873
2874 if (!LineInfos.empty()) {
2875 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002876 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00002877 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2878 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2879 // Append the function info to section info.
2880 SectionLineInfos.insert(SectionLineInfos.end(),
2881 LineInfos.begin(), LineInfos.end());
2882 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002883
Jim Laskeyef42a012006-11-02 20:12:39 +00002884 // Construct scopes for subprogram.
Bill Wendlingd751c642008-09-26 00:28:12 +00002885 if (MMI->getRootScope())
2886 ConstructRootScope(MMI->getRootScope());
2887 else
2888 // FIXME: This is wrong. We are essentially getting past a problem with
2889 // debug information not being able to handle unreachable blocks that have
2890 // debug information in them. In particular, those unreachable blocks that
2891 // have "region end" info in them. That situation results in the "root
2892 // scope" not being created. If that's the case, then emit a "default"
2893 // scope, i.e., one that encompasses the whole function. This isn't
2894 // desirable. And a better way of handling this (and all of the debugging
2895 // information) needs to be explored.
2896 ConstructDefaultScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002897
2898 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2899 MMI->getFrameMoves()));
Jim Laskeyef42a012006-11-02 20:12:39 +00002900 }
Jim Laskey65195462006-10-30 13:35:07 +00002901};
2902
Jim Laskey072200c2007-01-29 18:51:14 +00002903//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002904/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00002905///
2906class DwarfException : public Dwarf {
2907
Jim Laskeyb82313f2007-02-01 16:31:34 +00002908private:
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002909 struct FunctionEHFrameInfo {
2910 std::string FnName;
2911 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002912 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002913 bool hasCalls;
2914 bool hasLandingPads;
2915 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002916 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00002917
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002918 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
2919 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00002920 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002921 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002922 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00002923 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002924 };
2925
2926 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00002927
2928 /// shouldEmitTable - Per-function flag to indicate if EH tables should
2929 /// be emitted.
2930 bool shouldEmitTable;
2931
2932 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
2933 /// should be emitted.
2934 bool shouldEmitMoves;
2935
2936 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
2937 /// should be emitted.
2938 bool shouldEmitTableModule;
2939
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002940 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00002941 /// should be emitted.
2942 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00002943
Jim Laskey3f09fc22007-02-28 18:38:31 +00002944 /// EmitCommonEHFrame - Emit the common eh unwind frame.
2945 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002946 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00002947 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002948 int stackGrowth =
2949 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2950 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002951 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00002952
Jim Laskeybacd3042007-02-21 22:48:45 +00002953 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002954 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00002955
2956 if (!TAI->doesRequireNonLocalEHFrameLabel())
2957 O << TAI->getEHGlobalPrefix();
2958 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002959 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002960
Jim Laskeybacd3042007-02-21 22:48:45 +00002961 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002962 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00002963
Jim Laskeybacd3042007-02-21 22:48:45 +00002964 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002965 EmitDifference("eh_frame_common_end", Index,
2966 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002967 Asm->EOL("Length of Common Information Entry");
2968
Jim Laskeybacd3042007-02-21 22:48:45 +00002969 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002970 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00002971 Asm->EmitInt32((int)0);
2972 Asm->EOL("CIE Identifier Tag");
2973 Asm->EmitInt8(DW_CIE_VERSION);
2974 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00002975
Jim Laskeybacd3042007-02-21 22:48:45 +00002976 // The personality presence indicates that language specific information
2977 // will show up in the eh frame.
2978 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00002979 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00002980
Jim Laskeybacd3042007-02-21 22:48:45 +00002981 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00002982 Asm->EmitULEB128Bytes(1);
2983 Asm->EOL("CIE Code Alignment Factor");
2984 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00002985 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002986 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00002987 Asm->EOL("CIE Return Address Column");
2988
Jim Laskeybacd3042007-02-21 22:48:45 +00002989 // If there is a personality, we need to indicate the functions location.
2990 if (Personality) {
2991 Asm->EmitULEB128Bytes(7);
2992 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00002993
Duncan Sands671fa972008-05-07 19:11:09 +00002994 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00002995 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00002996 Asm->EOL("Personality (pcrel sdata4 indirect)");
2997 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00002998 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00002999 Asm->EOL("Personality (pcrel sdata4)");
3000 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003001
Duncan Sands671fa972008-05-07 19:11:09 +00003002 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003003 O << TAI->getPersonalityPrefix();
3004 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3005 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003006 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3007 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003008 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003009
Duncan Sands671fa972008-05-07 19:11:09 +00003010 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3011 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003012
3013 if (TAI->doesFDEEncodingRequireSData4()) {
3014 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3015 Asm->EOL("FDE Encoding (pcrel sdata4)");
3016 } else {
3017 Asm->EmitInt8(DW_EH_PE_pcrel);
3018 Asm->EOL("FDE Encoding (pcrel)");
3019 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003020 } else {
3021 Asm->EmitULEB128Bytes(1);
3022 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003023
3024 if (TAI->doesFDEEncodingRequireSData4()) {
3025 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3026 Asm->EOL("FDE Encoding (pcrel sdata4)");
3027 } else {
3028 Asm->EmitInt8(DW_EH_PE_pcrel);
3029 Asm->EOL("FDE Encoding (pcrel)");
3030 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003031 }
3032
3033 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003034 std::vector<MachineMove> Moves;
3035 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003036 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003037
Dale Johannesen21d972a2008-04-30 00:43:29 +00003038 // On Darwin the linker honors the alignment of eh_frame, which means it
3039 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3040 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003041 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003042 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003043 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003044
Jim Laskeybacd3042007-02-21 22:48:45 +00003045 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003046 }
Duncan Sands671fa972008-05-07 19:11:09 +00003047
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003048 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003049 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003050 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003051 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3052
Jim Laskeybacd3042007-02-21 22:48:45 +00003053 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3054
3055 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003056 // If the corresponding function is static, this should not be
3057 // externally visible.
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003058 if (linkage != Function::InternalLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003059 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3060 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3061 }
3062
Dale Johannesen038129d2008-01-10 02:03:30 +00003063 // If corresponding function is weak definition, this should be too.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003064 if ((linkage == Function::WeakLinkage ||
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003065 linkage == Function::LinkOnceLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003066 TAI->getWeakDefDirective())
3067 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3068
3069 // If there are no calls then you can't unwind. This may mean we can
3070 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003071 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003072 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003073 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003074 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003075 !UnwindTablesMandatory &&
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003076 ((linkage != Function::WeakLinkage &&
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003077 linkage != Function::LinkOnceLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003078 !TAI->getWeakDefDirective() ||
3079 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003080 {
Bill Wendling6e198962007-09-18 01:47:22 +00003081 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003082 // This name has no connection to the function, so it might get
3083 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003084 // dead-stripping unconditionally.
3085 if (const char *UsedDirective = TAI->getUsedDirective())
3086 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003087 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003088 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003089
Jim Laskeybacd3042007-02-21 22:48:45 +00003090 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003091 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3092 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003093 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003094
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003095 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003096
Bill Wendling722f5f12008-12-24 08:05:17 +00003097 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3098 PrintRelDirective(true, true);
3099 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3100
3101 if (!TAI->isAbsoluteEHSectionOffsets())
3102 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3103 } else {
3104 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3105 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3106 true, true, false);
3107 }
3108
Jim Laskeybacd3042007-02-21 22:48:45 +00003109 Asm->EOL("FDE CIE offset");
3110
Bill Wendling998dee92008-12-29 22:12:11 +00003111 EmitReference("eh_func_begin", EHFrameInfo.Number, true,
3112 TAI->doesRequire32BitFDEReference());
Jim Laskeybacd3042007-02-21 22:48:45 +00003113 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003114 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling998dee92008-12-29 22:12:11 +00003115 "eh_func_begin", EHFrameInfo.Number,
3116 TAI->doesRequire32BitFDEReference());
Jim Laskeybacd3042007-02-21 22:48:45 +00003117 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003118
Jim Laskey3f09fc22007-02-28 18:38:31 +00003119 // If there is a personality and landing pads then point to the language
3120 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003121 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003122 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003123 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003124
3125 if (EHFrameInfo.hasLandingPads)
3126 EmitReference("exception", EHFrameInfo.Number, true, true);
3127 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003128 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003129 Asm->EOL("Language Specific Data Area");
3130 } else {
3131 Asm->EmitULEB128Bytes(0);
3132 Asm->EOL("Augmentation size");
3133 }
Duncan Sands671fa972008-05-07 19:11:09 +00003134
Jim Laskeybacd3042007-02-21 22:48:45 +00003135 // Indicate locations of function specific callee saved registers in
3136 // frame.
Dale Johannesenb97aec62007-11-13 19:13:01 +00003137 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003138
Dale Johannesen21d972a2008-04-30 00:43:29 +00003139 // On Darwin the linker honors the alignment of eh_frame, which means it
3140 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3141 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003142 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003143 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003144 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003145
3146 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003147 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003148 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003149 // that depends on unused functions (calling undefined externals) being
3150 // dead-stripped to link correctly. Yes, there really is.
3151 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3152 if (const char *UsedDirective = TAI->getUsedDirective())
3153 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3154 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003155 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003156
Duncan Sands57810cd2007-09-05 11:27:52 +00003157 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003158 ///
3159 /// The general organization of the table is complex, but the basic concepts
3160 /// are easy. First there is a header which describes the location and
3161 /// organization of the three components that follow.
3162 /// 1. The landing pad site information describes the range of code covered
3163 /// by the try. In our case it's an accumulation of the ranges covered
3164 /// by the invokes in the try. There is also a reference to the landing
3165 /// pad that handles the exception once processed. Finally an index into
3166 /// the actions table.
3167 /// 2. The action table, in our case, is composed of pairs of type ids
3168 /// and next action offset. Starting with the action index from the
3169 /// landing pad site, each type Id is checked for a match to the current
3170 /// exception. If it matches then the exception and type id are passed
3171 /// on to the landing pad. Otherwise the next action is looked up. This
3172 /// chain is terminated with a next action of zero. If no type id is
3173 /// found the the frame is unwound and handling continues.
3174 /// 3. Type id table contains references to all the C++ typeinfo for all
3175 /// catches in the function. This tables is reversed indexed base 1.
3176
Duncan Sandsb32edb42007-06-06 15:37:31 +00003177 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3178 static unsigned SharedTypeIds(const LandingPadInfo *L,
3179 const LandingPadInfo *R) {
3180 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003181 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003182 unsigned MinSize = LSize < RSize ? LSize : RSize;
3183 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003184
Duncan Sandsb32edb42007-06-06 15:37:31 +00003185 for (; Count != MinSize; ++Count)
3186 if (LIds[Count] != RIds[Count])
3187 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003188
Duncan Sandsb32edb42007-06-06 15:37:31 +00003189 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003190 }
3191
Duncan Sandsb32edb42007-06-06 15:37:31 +00003192 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003193 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003194 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003195 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003196 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003197
Duncan Sandsb32edb42007-06-06 15:37:31 +00003198 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003199 if (LIds[i] != RIds[i])
3200 return LIds[i] < RIds[i];
3201
Duncan Sandsb32edb42007-06-06 15:37:31 +00003202 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003203 }
3204
Duncan Sands53c3a332007-05-16 12:12:23 +00003205 struct KeyInfo {
3206 static inline unsigned getEmptyKey() { return -1U; }
3207 static inline unsigned getTombstoneKey() { return -2U; }
3208 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003209 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003210 static bool isPod() { return true; }
3211 };
3212
Duncan Sands57810cd2007-09-05 11:27:52 +00003213 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003214 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003215 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003216 int NextAction;
3217 struct ActionEntry *Previous;
3218 };
3219
Duncan Sands57810cd2007-09-05 11:27:52 +00003220 /// PadRange - Structure holding a try-range and the associated landing pad.
3221 struct PadRange {
3222 // The index of the landing pad.
3223 unsigned PadIndex;
3224 // The index of the begin and end labels in the landing pad's label lists.
3225 unsigned RangeIndex;
3226 };
3227
3228 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3229
3230 /// CallSiteEntry - Structure describing an entry in the call-site table.
3231 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003232 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003233 unsigned BeginLabel; // zero indicates the start of the function.
3234 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003235 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003236 unsigned PadLabel; // zero indicates that there is no landing pad.
3237 unsigned Action;
3238 };
3239
Jim Laskeybacd3042007-02-21 22:48:45 +00003240 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003241 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003242 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003243 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3244 if (PadInfos.empty()) return;
3245
3246 // Sort the landing pads in order of their type ids. This is used to fold
3247 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003248 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003249 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003250 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003251 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003252 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3253
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003254 // Negative type ids index into FilterIds, positive type ids index into
3255 // TypeInfos. The value written for a positive type id is just the type
3256 // id itself. For a negative type id, however, the value written is the
3257 // (negative) byte offset of the corresponding FilterIds entry. The byte
3258 // offset is usually equal to the type id, because the FilterIds entries
3259 // are written using a variable width encoding which outputs one byte per
3260 // entry as long as the value written is not too large, but can differ.
3261 // This kind of complication does not occur for positive type ids because
3262 // type infos are output using a fixed width encoding.
3263 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3264 SmallVector<int, 16> FilterOffsets;
3265 FilterOffsets.reserve(FilterIds.size());
3266 int Offset = -1;
3267 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3268 E = FilterIds.end(); I != E; ++I) {
3269 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003270 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003271 }
3272
Duncan Sands57810cd2007-09-05 11:27:52 +00003273 // Compute the actions table and gather the first action index for each
3274 // landing pad site.
3275 SmallVector<ActionEntry, 32> Actions;
3276 SmallVector<unsigned, 64> FirstActions;
3277 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003278
Duncan Sandsb32edb42007-06-06 15:37:31 +00003279 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003280 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003281 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003282 const LandingPadInfo *LP = LandingPads[i];
3283 const std::vector<int> &TypeIds = LP->TypeIds;
3284 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003285 unsigned SizeSiteActions = 0;
3286
Duncan Sandsb32edb42007-06-06 15:37:31 +00003287 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003288 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003289 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003290
Duncan Sandsb32edb42007-06-06 15:37:31 +00003291 if (NumShared) {
3292 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3293 assert(Actions.size());
3294 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003295 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3296 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003297 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003298 SizeAction -=
3299 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003300 SizeAction += -PrevAction->NextAction;
3301 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003302 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003303 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003304
Duncan Sandsb32edb42007-06-06 15:37:31 +00003305 // Compute the actions.
3306 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3307 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003308 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3309 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003310 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003311
3312 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003313 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003314 SizeSiteActions += SizeAction;
3315
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003316 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00003317 Actions.push_back(Action);
3318
3319 PrevAction = &Actions.back();
3320 }
3321
3322 // Record the first action of the landing pad site.
3323 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3324 } // else identical - re-use previous FirstAction
3325
3326 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003327
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00003328 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00003329 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00003330 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003331
Duncan Sands481dc722007-12-19 07:36:31 +00003332 // Compute the call-site table. The entry for an invoke has a try-range
3333 // containing the call, a non-zero landing pad and an appropriate action.
3334 // The entry for an ordinary call has a try-range containing the call and
3335 // zero for the landing pad and the action. Calls marked 'nounwind' have
3336 // no entry and must not be contained in the try-range of any entry - they
3337 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003338 SmallVector<CallSiteEntry, 64> CallSites;
3339
3340 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00003341 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3342 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3343 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00003344 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3345 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00003346 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003347 unsigned BeginLabel = LandingPad->BeginLabels[j];
3348 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3349 PadRange P = { i, j };
3350 PadMap[BeginLabel] = P;
3351 }
3352 }
3353
Duncan Sands481dc722007-12-19 07:36:31 +00003354 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00003355 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00003356
3357 // Whether there is a potentially throwing instruction (currently this means
3358 // an ordinary call) between the end of the previous try-range and now.
3359 bool SawPotentiallyThrowing = false;
3360
3361 // Whether the last callsite entry was for an invoke.
3362 bool PreviousIsInvoke = false;
3363
Duncan Sands481dc722007-12-19 07:36:31 +00003364 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003365 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3366 I != E; ++I) {
3367 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3368 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00003369 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00003370 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00003371 continue;
3372 }
3373
Chris Lattner9e330492007-12-30 20:50:28 +00003374 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00003375 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00003376
Duncan Sands481dc722007-12-19 07:36:31 +00003377 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00003378 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00003379 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003380
Duncan Sands481dc722007-12-19 07:36:31 +00003381 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00003382 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00003383 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00003384 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00003385 continue;
3386
3387 PadRange P = L->second;
3388 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3389
3390 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3391 "Inconsistent landing pad map!");
3392
3393 // If some instruction between the previous try-range and this one may
3394 // throw, create a call-site entry with no landing pad for the region
3395 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00003396 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003397 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3398 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00003399 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003400 }
3401
3402 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00003403 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00003404
Duncan Sands481dc722007-12-19 07:36:31 +00003405 if (LandingPad->LandingPadLabel) {
3406 // This try-range is for an invoke.
3407 CallSiteEntry Site = {BeginLabel, LastLabel,
3408 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00003409
Duncan Sands481dc722007-12-19 07:36:31 +00003410 // Try to merge with the previous call-site.
3411 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00003412 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00003413 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3414 // Extend the range of the previous entry.
3415 Prev.EndLabel = Site.EndLabel;
3416 continue;
3417 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003418 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003419
Duncan Sands481dc722007-12-19 07:36:31 +00003420 // Otherwise, create a new call-site.
3421 CallSites.push_back(Site);
3422 PreviousIsInvoke = true;
3423 } else {
3424 // Create a gap.
3425 PreviousIsInvoke = false;
3426 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003427 }
3428 }
3429 // If some instruction between the previous try-range and the end of the
3430 // function may throw, create a call-site entry with no landing pad for the
3431 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00003432 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003433 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3434 CallSites.push_back(Site);
3435 }
3436
Jim Laskeybacd3042007-02-21 22:48:45 +00003437 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00003438
3439 // Call sites.
3440 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3441 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3442 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3443 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3444 SiteLengthSize +
3445 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00003446 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003447 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00003448
Duncan Sands671fa972008-05-07 19:11:09 +00003449 // Type infos.
3450 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3451 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003452
3453 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003454 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003455 SizeSites + SizeActions + SizeTypes;
3456
3457 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3458 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003459 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003460 TypeOffset;
3461
3462 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003463
Jim Laskeybacd3042007-02-21 22:48:45 +00003464 // Begin the exception table.
3465 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00003466 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00003467 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003468 for (unsigned i = 0; i != SizeAlign; ++i) {
3469 Asm->EmitInt8(0);
3470 Asm->EOL("Padding");
3471 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003472 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003473
Jim Laskeybacd3042007-02-21 22:48:45 +00003474 // Emit the header.
3475 Asm->EmitInt8(DW_EH_PE_omit);
3476 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3477 Asm->EmitInt8(DW_EH_PE_absptr);
3478 Asm->EOL("TType format (DW_EH_PE_absptr)");
3479 Asm->EmitULEB128Bytes(TypeOffset);
3480 Asm->EOL("TType base offset");
3481 Asm->EmitInt8(DW_EH_PE_udata4);
3482 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3483 Asm->EmitULEB128Bytes(SizeSites);
3484 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003485
Duncan Sands57810cd2007-09-05 11:27:52 +00003486 // Emit the landing pad site information.
3487 for (unsigned i = 0; i < CallSites.size(); ++i) {
3488 CallSiteEntry &S = CallSites[i];
3489 const char *BeginTag;
3490 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00003491
Duncan Sands57810cd2007-09-05 11:27:52 +00003492 if (!S.BeginLabel) {
3493 BeginTag = "eh_func_begin";
3494 BeginNumber = SubprogramCount;
3495 } else {
3496 BeginTag = "label";
3497 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00003498 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003499
Duncan Sands57810cd2007-09-05 11:27:52 +00003500 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003501 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003502 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00003503
Duncan Sands57810cd2007-09-05 11:27:52 +00003504 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00003505 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00003506 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003507 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00003508 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003509 }
3510 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003511
Duncan Sands671fa972008-05-07 19:11:09 +00003512 if (!S.PadLabel)
3513 Asm->EmitInt32(0);
3514 else
Duncan Sands57810cd2007-09-05 11:27:52 +00003515 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003516 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003517 Asm->EOL("Landing pad");
3518
3519 Asm->EmitULEB128Bytes(S.Action);
3520 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003521 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003522
Jim Laskeybacd3042007-02-21 22:48:45 +00003523 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003524 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3525 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00003526
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003527 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003528 Asm->EOL("TypeInfo index");
3529 Asm->EmitSLEB128Bytes(Action.NextAction);
3530 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003531 }
3532
3533 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00003534 for (unsigned M = TypeInfos.size(); M; --M) {
3535 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00003536
3537 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00003538
3539 if (GV)
3540 O << Asm->getGlobalLinkName(GV);
3541 else
3542 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00003543
Jim Laskeybacd3042007-02-21 22:48:45 +00003544 Asm->EOL("TypeInfo");
3545 }
3546
Duncan Sands73ef58a2007-06-02 16:53:42 +00003547 // Emit the filter typeids.
3548 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3549 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00003550 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00003551 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00003552 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003553
Evan Cheng05548eb2008-02-29 19:36:59 +00003554 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003555 }
3556
Jim Laskey072200c2007-01-29 18:51:14 +00003557public:
3558 //===--------------------------------------------------------------------===//
3559 // Main entry points.
3560 //
Owen Andersoncb371882008-08-21 00:14:44 +00003561 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00003562 : Dwarf(OS, A, T, "eh")
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003563 , shouldEmitTable(false)
3564 , shouldEmitMoves(false)
3565 , shouldEmitTableModule(false)
3566 , shouldEmitMovesModule(false)
Jim Laskey072200c2007-01-29 18:51:14 +00003567 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003568
Jim Laskey072200c2007-01-29 18:51:14 +00003569 virtual ~DwarfException() {}
3570
3571 /// SetModuleInfo - Set machine module information when it's known that pass
3572 /// manager has created it. Set by the target AsmPrinter.
3573 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00003574 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00003575 }
3576
3577 /// BeginModule - Emit all exception information that should come prior to the
3578 /// content.
3579 void BeginModule(Module *M) {
3580 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00003581 }
3582
3583 /// EndModule - Emit all exception information that should come after the
3584 /// content.
3585 void EndModule() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003586 if (shouldEmitMovesModule || shouldEmitTableModule) {
3587 const std::vector<Function *> Personalities = MMI->getPersonalities();
3588 for (unsigned i =0; i < Personalities.size(); ++i)
3589 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003590
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003591 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3592 E = EHFrames.end(); I != E; ++I)
3593 EmitEHFrame(*I);
3594 }
Jim Laskey072200c2007-01-29 18:51:14 +00003595 }
3596
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003597 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00003598 /// emitted immediately after the function entry point.
3599 void BeginFunction(MachineFunction *MF) {
3600 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003601 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesene0040622008-04-02 17:04:45 +00003602 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003603
3604 // Map all labels and get rid of any dead landing pads.
3605 MMI->TidyLandingPads();
3606 // If any landing pads survive, we need an EH table.
3607 if (MMI->getLandingPads().size())
3608 shouldEmitTable = true;
3609
3610 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00003611 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003612 shouldEmitMoves = true;
3613
3614 if (shouldEmitMoves || shouldEmitTable)
3615 // Assumes in correct section after the entry point.
3616 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00003617 }
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003618 shouldEmitTableModule |= shouldEmitTable;
3619 shouldEmitMovesModule |= shouldEmitMoves;
Jim Laskey072200c2007-01-29 18:51:14 +00003620 }
3621
3622 /// EndFunction - Gather and emit post-function exception information.
3623 ///
3624 void EndFunction() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003625 if (shouldEmitMoves || shouldEmitTable) {
3626 EmitLabel("eh_func_end", SubprogramCount);
3627 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003628
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003629 // Save EH frame information
3630 EHFrames.
3631 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling6e198962007-09-18 01:47:22 +00003632 SubprogramCount,
3633 MMI->getPersonalityIndex(),
3634 MF->getFrameInfo()->hasCalls(),
3635 !MMI->getLandingPads().empty(),
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003636 MMI->getFrameMoves(),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003637 MF->getFunction()));
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003638 }
Jim Laskey072200c2007-01-29 18:51:14 +00003639 }
3640};
3641
Jim Laskey0d086af2006-02-27 12:43:29 +00003642} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00003643
3644//===----------------------------------------------------------------------===//
3645
Jim Laskeyd18e2892006-01-20 20:34:06 +00003646/// Emit - Print the abbreviation using the specified Dwarf writer.
3647///
Jim Laskey072200c2007-01-29 18:51:14 +00003648void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003649 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00003650 DD.getAsm()->EmitULEB128Bytes(Tag);
3651 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003652
Jim Laskeyd18e2892006-01-20 20:34:06 +00003653 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00003654 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3655 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003656
Jim Laskeyd18e2892006-01-20 20:34:06 +00003657 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00003658 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003659 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003660
Jim Laskeyd18e2892006-01-20 20:34:06 +00003661 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00003662 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3663 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003664
Jim Laskeyd18e2892006-01-20 20:34:06 +00003665 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00003666 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3667 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003668 }
3669
3670 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00003671 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3672 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00003673}
3674
3675#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00003676void DIEAbbrev::print(std::ostream &O) {
3677 O << "Abbreviation @"
3678 << std::hex << (intptr_t)this << std::dec
3679 << " "
3680 << TagString(Tag)
3681 << " "
3682 << ChildrenString(ChildrenFlag)
3683 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003684
Jim Laskeya0f3d172006-09-07 22:06:40 +00003685 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3686 O << " "
3687 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003688 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00003689 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003690 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00003691 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00003692}
Bill Wendlinge8156192006-12-07 01:30:32 +00003693void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00003694#endif
3695
3696//===----------------------------------------------------------------------===//
3697
Jim Laskeyef42a012006-11-02 20:12:39 +00003698#ifndef NDEBUG
3699void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003700 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003701}
Jim Laskeyef42a012006-11-02 20:12:39 +00003702#endif
3703
3704//===----------------------------------------------------------------------===//
3705
Jim Laskey063e7652006-01-17 17:31:53 +00003706/// EmitValue - Emit integer of appropriate size.
3707///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003708void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00003709 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00003710 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00003711 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003712 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003713 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003714 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003715 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003716 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003717 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003718 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
3719 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
3720 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003721 default: assert(0 && "DIE Value form not supported yet"); break;
3722 }
3723}
3724
3725/// SizeOf - Determine size of integer value in bytes.
3726///
Jim Laskey072200c2007-01-29 18:51:14 +00003727unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003728 switch (Form) {
3729 case DW_FORM_flag: // Fall thru
3730 case DW_FORM_ref1: // Fall thru
3731 case DW_FORM_data1: return sizeof(int8_t);
3732 case DW_FORM_ref2: // Fall thru
3733 case DW_FORM_data2: return sizeof(int16_t);
3734 case DW_FORM_ref4: // Fall thru
3735 case DW_FORM_data4: return sizeof(int32_t);
3736 case DW_FORM_ref8: // Fall thru
3737 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003738 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
3739 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00003740 default: assert(0 && "DIE Value form not supported yet"); break;
3741 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003742 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00003743}
3744
Jim Laskey063e7652006-01-17 17:31:53 +00003745//===----------------------------------------------------------------------===//
3746
3747/// EmitValue - Emit string value.
3748///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003749void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003750 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00003751}
3752
Jim Laskey063e7652006-01-17 17:31:53 +00003753//===----------------------------------------------------------------------===//
3754
3755/// EmitValue - Emit label value.
3756///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003757void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003758 bool IsSmall = Form == DW_FORM_data4;
3759 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00003760}
3761
3762/// SizeOf - Determine size of label value in bytes.
3763///
Jim Laskey072200c2007-01-29 18:51:14 +00003764unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003765 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003766 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00003767}
Jim Laskeyef42a012006-11-02 20:12:39 +00003768
Jim Laskey063e7652006-01-17 17:31:53 +00003769//===----------------------------------------------------------------------===//
3770
Jim Laskeyd18e2892006-01-20 20:34:06 +00003771/// EmitValue - Emit label value.
3772///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003773void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003774 bool IsSmall = Form == DW_FORM_data4;
3775 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00003776}
3777
3778/// SizeOf - Determine size of label value in bytes.
3779///
Jim Laskey072200c2007-01-29 18:51:14 +00003780unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003781 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003782 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00003783}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003784
Jim Laskeyd18e2892006-01-20 20:34:06 +00003785//===----------------------------------------------------------------------===//
3786
Jim Laskey063e7652006-01-17 17:31:53 +00003787/// EmitValue - Emit delta value.
3788///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00003789void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
3790 bool IsSmall = Form == DW_FORM_data4;
3791 DD.EmitSectionOffset(Label.Tag, Section.Tag,
3792 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
3793}
3794
3795/// SizeOf - Determine size of delta value in bytes.
3796///
3797unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3798 if (Form == DW_FORM_data4) return 4;
3799 return DD.getTargetData()->getPointerSize();
3800}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003801
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00003802//===----------------------------------------------------------------------===//
3803
3804/// EmitValue - Emit delta value.
3805///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003806void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003807 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00003808 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00003809}
3810
3811/// SizeOf - Determine size of delta value in bytes.
3812///
Jim Laskey072200c2007-01-29 18:51:14 +00003813unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003814 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003815 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00003816}
3817
3818//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003819
Jim Laskeyb8509c52006-03-23 18:07:55 +00003820/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003821///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003822void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003823 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00003824}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003825
Jim Laskeyd18e2892006-01-20 20:34:06 +00003826//===----------------------------------------------------------------------===//
3827
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003828/// ComputeSize - calculate the size of the block.
3829///
Jim Laskey072200c2007-01-29 18:51:14 +00003830unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003831 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00003832 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003833
Jim Laskeyef42a012006-11-02 20:12:39 +00003834 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00003835 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00003836 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003837 }
3838 return Size;
3839}
3840
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003841/// EmitValue - Emit block data.
3842///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003843void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003844 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003845 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
3846 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
3847 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
3848 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003849 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003850 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003851
Owen Anderson873e1b52008-06-24 21:44:59 +00003852 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00003853
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003854 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003855 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00003856 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003857 }
3858}
3859
3860/// SizeOf - Determine size of block data in bytes.
3861///
Jim Laskey072200c2007-01-29 18:51:14 +00003862unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003863 switch (Form) {
3864 case DW_FORM_block1: return Size + sizeof(int8_t);
3865 case DW_FORM_block2: return Size + sizeof(int16_t);
3866 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003867 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003868 default: assert(0 && "Improper form for block"); break;
3869 }
3870 return 0;
3871}
3872
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003873//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003874/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00003875
3876DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003877 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00003878 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00003879}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003880
Jim Laskeyb8509c52006-03-23 18:07:55 +00003881/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
3882///
3883void DIE::AddSiblingOffset() {
3884 DIEInteger *DI = new DIEInteger(0);
3885 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00003886 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00003887}
3888
Jim Laskeyef42a012006-11-02 20:12:39 +00003889/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003890///
Jim Laskeyef42a012006-11-02 20:12:39 +00003891void DIE::Profile(FoldingSetNodeID &ID) {
3892 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003893
Jim Laskeyef42a012006-11-02 20:12:39 +00003894 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3895 ID.AddPointer(Children[i]);
3896
3897 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3898 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003899}
Jim Laskeyef42a012006-11-02 20:12:39 +00003900
3901#ifndef NDEBUG
3902void DIE::print(std::ostream &O, unsigned IncIndent) {
3903 static unsigned IndentCount = 0;
3904 IndentCount += IncIndent;
3905 const std::string Indent(IndentCount, ' ');
3906 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003907
Jim Laskeyef42a012006-11-02 20:12:39 +00003908 if (!isBlock) {
3909 O << Indent
3910 << "Die: "
3911 << "0x" << std::hex << (intptr_t)this << std::dec
3912 << ", Offset: " << Offset
3913 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003914 << "\n";
3915
Jim Laskeyef42a012006-11-02 20:12:39 +00003916 O << Indent
3917 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00003918 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00003919 << ChildrenString(Abbrev.getChildrenFlag());
3920 } else {
3921 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00003922 }
3923 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00003924
Owen Anderson873e1b52008-06-24 21:44:59 +00003925 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003926
Jim Laskeyef42a012006-11-02 20:12:39 +00003927 IndentCount += 2;
3928 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3929 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00003930
3931 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00003932 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00003933 else
Jim Laskeyef42a012006-11-02 20:12:39 +00003934 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00003935
Jim Laskeyef42a012006-11-02 20:12:39 +00003936 O << " "
3937 << FormEncodingString(Data[i].getForm())
3938 << " ";
3939 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00003940 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00003941 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003942 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00003943
Jim Laskeyef42a012006-11-02 20:12:39 +00003944 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3945 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00003946 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003947
Jim Laskeyef42a012006-11-02 20:12:39 +00003948 if (!isBlock) O << "\n";
3949 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00003950}
3951
Jim Laskeyef42a012006-11-02 20:12:39 +00003952void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003953 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00003954}
Jim Laskeybd761842006-02-27 17:27:12 +00003955#endif
Jim Laskey65195462006-10-30 13:35:07 +00003956
3957//===----------------------------------------------------------------------===//
3958/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00003959///
Jim Laskey65195462006-10-30 13:35:07 +00003960
Owen Andersoncb371882008-08-21 00:14:44 +00003961DwarfWriter::DwarfWriter(raw_ostream &OS, AsmPrinter *A,
Jim Laskey65195462006-10-30 13:35:07 +00003962 const TargetAsmInfo *T) {
Jim Laskey072200c2007-01-29 18:51:14 +00003963 DE = new DwarfException(OS, A, T);
3964 DD = new DwarfDebug(OS, A, T);
Jim Laskey65195462006-10-30 13:35:07 +00003965}
3966
3967DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00003968 delete DE;
3969 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00003970}
3971
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003972/// SetModuleInfo - Set machine module info when it's known that pass manager
3973/// has created it. Set by the target AsmPrinter.
3974void DwarfWriter::SetModuleInfo(MachineModuleInfo *MMI) {
Jim Laskey072200c2007-01-29 18:51:14 +00003975 DD->SetModuleInfo(MMI);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003976 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00003977}
3978
3979/// BeginModule - Emit all Dwarf sections that should come prior to the
3980/// content.
3981void DwarfWriter::BeginModule(Module *M) {
Jim Laskey072200c2007-01-29 18:51:14 +00003982 DE->BeginModule(M);
3983 DD->BeginModule(M);
Jim Laskey65195462006-10-30 13:35:07 +00003984}
3985
3986/// EndModule - Emit all Dwarf sections that should come after the content.
3987///
3988void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00003989 DE->EndModule();
3990 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00003991}
3992
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003993/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003994/// emitted immediately after the function entry point.
3995void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00003996 DE->BeginFunction(MF);
3997 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00003998}
3999
4000/// EndFunction - Gather and emit post-function debug information.
4001///
Bill Wendlingd751c642008-09-26 00:28:12 +00004002void DwarfWriter::EndFunction(MachineFunction *MF) {
4003 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004004 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004005
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004006 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004007 // Clear function debug information.
4008 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004009}