blob: af4198222558d5254dfcf6fe221ac301e4d70e8f [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"
Devang Patel08f053f2009-01-05 17:57:47 +000026#include "llvm/Analysis/DebugInfo.h"
Jim Laskey3f09fc22007-02-28 18:38:31 +000027#include "llvm/Support/Debug.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000028#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000029#include "llvm/Support/CommandLine.h"
Jim Laskey65195462006-10-30 13:35:07 +000030#include "llvm/Support/DataTypes.h"
Jim Laskey52060a02006-01-24 00:49:18 +000031#include "llvm/Support/Mangler.h"
Owen Andersoncb371882008-08-21 00:14:44 +000032#include "llvm/Support/raw_ostream.h"
Dan Gohman85496362007-09-24 21:32:18 +000033#include "llvm/System/Path.h"
Jim Laskey563321a2006-09-06 18:34:40 +000034#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000035#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000036#include "llvm/Target/TargetData.h"
Jim Laskey1069fbd2006-04-10 23:09:19 +000037#include "llvm/Target/TargetFrameInfo.h"
Duncan Sands53c3a332007-05-16 12:12:23 +000038#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey1b340dc2007-01-29 20:48:32 +000039#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000040#include "llvm/Target/TargetOptions.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000041#include <ostream>
Jim Laskey65195462006-10-30 13:35:07 +000042#include <string>
Jim Laskeyb2efb852006-01-04 22:28:25 +000043using namespace llvm;
Jim Laskey9a777a32006-02-27 22:37:23 +000044using namespace llvm::dwarf;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000045
Jim Laskey0d086af2006-02-27 12:43:29 +000046namespace llvm {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000047
Jim Laskey65195462006-10-30 13:35:07 +000048//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000049
50/// Configuration values for initial hash set sizes (log2).
51///
52static const unsigned InitDiesSetSize = 9; // 512
53static const unsigned InitAbbreviationsSetSize = 9; // 512
54static const unsigned InitValuesSetSize = 9; // 512
55
56//===----------------------------------------------------------------------===//
57/// Forward declarations.
58///
59class DIE;
60class DIEValue;
61
62//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000063/// DWLabel - Labels are used to track locations in the assembler file.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000064/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
65/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer181b6c92007-08-05 20:06:04 +000066/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +000067class DWLabel {
68public:
Jim Laskeyef42a012006-11-02 20:12:39 +000069 /// Tag - Label category tag. Should always be a staticly declared C string.
70 ///
71 const char *Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000072
Jim Laskeyef42a012006-11-02 20:12:39 +000073 /// Number - Value to make label unique.
74 ///
75 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +000076
77 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000078
Jim Laskeyef42a012006-11-02 20:12:39 +000079 void Profile(FoldingSetNodeID &ID) const {
80 ID.AddString(std::string(Tag));
81 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +000082 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000083
Jim Laskeyef42a012006-11-02 20:12:39 +000084#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +000085 void print(std::ostream *O) const {
86 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +000087 }
Jim Laskeyef42a012006-11-02 20:12:39 +000088 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +000089 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +000090 if (Number) O << Number;
91 }
92#endif
Jim Laskeybd761842006-02-27 17:27:12 +000093};
94
95//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +000096/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
97/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +000098class DIEAbbrevData {
99private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000100 /// Attribute - Dwarf attribute code.
101 ///
102 unsigned Attribute;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000103
Jim Laskeyef42a012006-11-02 20:12:39 +0000104 /// Form - Dwarf form code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000105 ///
106 unsigned Form;
107
Jim Laskey0d086af2006-02-27 12:43:29 +0000108public:
109 DIEAbbrevData(unsigned A, unsigned F)
110 : Attribute(A)
111 , Form(F)
112 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000113
Jim Laskeybd761842006-02-27 17:27:12 +0000114 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000115 unsigned getAttribute() const { return Attribute; }
116 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000117
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000118 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000119 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000120 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000121 ID.AddInteger(Attribute);
122 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000123 }
124};
Jim Laskey063e7652006-01-17 17:31:53 +0000125
Jim Laskey0d086af2006-02-27 12:43:29 +0000126//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000127/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
128/// information object.
129class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000130private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000131 /// Tag - Dwarf tag code.
132 ///
133 unsigned Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000134
Jim Laskeyef42a012006-11-02 20:12:39 +0000135 /// Unique number for node.
136 ///
137 unsigned Number;
138
139 /// ChildrenFlag - Dwarf children flag.
140 ///
141 unsigned ChildrenFlag;
142
143 /// Data - Raw data bytes for abbreviation.
144 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000145 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000146
Jim Laskey0d086af2006-02-27 12:43:29 +0000147public:
Jim Laskey063e7652006-01-17 17:31:53 +0000148
Jim Laskey0d086af2006-02-27 12:43:29 +0000149 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000150 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000151 , ChildrenFlag(C)
152 , Data()
153 {}
154 ~DIEAbbrev() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000155
Jim Laskeybd761842006-02-27 17:27:12 +0000156 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000157 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000158 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000159 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000160 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000161 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000162 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000163 void setNumber(unsigned N) { Number = N; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000164
Jim Laskey0d086af2006-02-27 12:43:29 +0000165 /// AddAttribute - Adds another set of attribute information to the
166 /// abbreviation.
167 void AddAttribute(unsigned Attribute, unsigned Form) {
168 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000169 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000170
Jim Laskeyb8509c52006-03-23 18:07:55 +0000171 /// AddFirstAttribute - Adds a set of attribute information to the front
172 /// of the abbreviation.
173 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
174 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
175 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000176
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000177 /// Profile - Used to gather unique data for the abbreviation folding set.
178 ///
179 void Profile(FoldingSetNodeID &ID) {
180 ID.AddInteger(Tag);
181 ID.AddInteger(ChildrenFlag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000182
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000183 // For each attribute description.
184 for (unsigned i = 0, N = Data.size(); i < N; ++i)
185 Data[i].Profile(ID);
186 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000187
Jim Laskey0d086af2006-02-27 12:43:29 +0000188 /// Emit - Print the abbreviation using the specified Dwarf writer.
189 ///
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000190 void Emit(const DwarfDebug &DD) const;
191
Jim Laskey0d086af2006-02-27 12:43:29 +0000192#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000193 void print(std::ostream *O) {
194 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000195 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000196 void print(std::ostream &O);
197 void dump();
198#endif
199};
Jim Laskey063e7652006-01-17 17:31:53 +0000200
Jim Laskey0d086af2006-02-27 12:43:29 +0000201//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000202/// DIE - A structured debug information entry. Has an abbreviation which
203/// describes it's organization.
204class DIE : public FoldingSetNode {
205protected:
206 /// Abbrev - Buffer for constructing abbreviation.
207 ///
208 DIEAbbrev Abbrev;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000209
Jim Laskeyef42a012006-11-02 20:12:39 +0000210 /// Offset - Offset in debug info section.
211 ///
212 unsigned Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000213
Jim Laskeyef42a012006-11-02 20:12:39 +0000214 /// Size - Size of instance + children.
215 ///
216 unsigned Size;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000217
Jim Laskeyef42a012006-11-02 20:12:39 +0000218 /// Children DIEs.
219 ///
220 std::vector<DIE *> Children;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000221
Jim Laskeyef42a012006-11-02 20:12:39 +0000222 /// Attributes values.
223 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000224 SmallVector<DIEValue*, 32> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000225
Jim Laskeyef42a012006-11-02 20:12:39 +0000226public:
Dan Gohman81975f62007-08-27 14:50:10 +0000227 explicit DIE(unsigned Tag)
Jim Laskeyef42a012006-11-02 20:12:39 +0000228 : Abbrev(Tag, DW_CHILDREN_no)
229 , Offset(0)
230 , Size(0)
231 , Children()
232 , Values()
233 {}
234 virtual ~DIE();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000235
Jim Laskeyef42a012006-11-02 20:12:39 +0000236 // Accessors.
237 DIEAbbrev &getAbbrev() { return Abbrev; }
238 unsigned getAbbrevNumber() const {
239 return Abbrev.getNumber();
240 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000241 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000242 unsigned getOffset() const { return Offset; }
243 unsigned getSize() const { return Size; }
244 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000245 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000246 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
247 void setOffset(unsigned O) { Offset = O; }
248 void setSize(unsigned S) { Size = S; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000249
Jim Laskeyef42a012006-11-02 20:12:39 +0000250 /// AddValue - Add a value and attributes to a DIE.
251 ///
252 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
253 Abbrev.AddAttribute(Attribute, Form);
254 Values.push_back(Value);
255 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000256
Jim Laskeyef42a012006-11-02 20:12:39 +0000257 /// SiblingOffset - Return the offset of the debug information entry's
258 /// sibling.
259 unsigned SiblingOffset() const { return Offset + Size; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000260
Jim Laskeyef42a012006-11-02 20:12:39 +0000261 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
262 ///
263 void AddSiblingOffset();
264
265 /// AddChild - Add a child to the DIE.
266 ///
267 void AddChild(DIE *Child) {
268 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
269 Children.push_back(Child);
270 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000271
Jim Laskeyef42a012006-11-02 20:12:39 +0000272 /// Detach - Detaches objects connected to it after copying.
273 ///
274 void Detach() {
275 Children.clear();
276 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000277
Jim Laskeyef42a012006-11-02 20:12:39 +0000278 /// Profile - Used to gather unique data for the value folding set.
279 ///
280 void Profile(FoldingSetNodeID &ID) ;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000281
Jim Laskeyef42a012006-11-02 20:12:39 +0000282#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000283 void print(std::ostream *O, unsigned IncIndent = 0) {
284 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000285 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000286 void print(std::ostream &O, unsigned IncIndent = 0);
287 void dump();
288#endif
289};
290
291//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000292/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000293///
294class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000295public:
296 enum {
297 isInteger,
298 isString,
299 isLabel,
300 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000301 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000302 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000303 isEntry,
304 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000305 };
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000306
Jim Laskeyef42a012006-11-02 20:12:39 +0000307 /// Type - Type of data stored in the value.
308 ///
309 unsigned Type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000310
Dan Gohman81975f62007-08-27 14:50:10 +0000311 explicit DIEValue(unsigned T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000312 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000313 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000314 virtual ~DIEValue() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000315
Jim Laskeyf6733882006-11-02 21:48:18 +0000316 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000317 unsigned getType() const { return Type; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000318
Jim Laskey0d086af2006-02-27 12:43:29 +0000319 // Implement isa/cast/dyncast.
320 static bool classof(const DIEValue *) { return true; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000321
Jim Laskey0d086af2006-02-27 12:43:29 +0000322 /// EmitValue - Emit value via the Dwarf writer.
323 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000324 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000325
Jim Laskey0d086af2006-02-27 12:43:29 +0000326 /// SizeOf - Return the size of a value in bytes.
327 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000328 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000329
Jim Laskeyef42a012006-11-02 20:12:39 +0000330 /// Profile - Used to gather unique data for the value folding set.
331 ///
332 virtual void Profile(FoldingSetNodeID &ID) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000333
Jim Laskeyef42a012006-11-02 20:12:39 +0000334#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000335 void print(std::ostream *O) {
336 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000337 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000338 virtual void print(std::ostream &O) = 0;
339 void dump();
340#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000341};
Jim Laskey063e7652006-01-17 17:31:53 +0000342
Jim Laskey0d086af2006-02-27 12:43:29 +0000343//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000344/// DWInteger - An integer value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000345///
Jim Laskey0d086af2006-02-27 12:43:29 +0000346class DIEInteger : public DIEValue {
347private:
348 uint64_t Integer;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000349
Jim Laskey0d086af2006-02-27 12:43:29 +0000350public:
Dan Gohman81975f62007-08-27 14:50:10 +0000351 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000352
Jim Laskey0d086af2006-02-27 12:43:29 +0000353 // Implement isa/cast/dyncast.
354 static bool classof(const DIEInteger *) { return true; }
355 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000356
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000357 /// BestForm - Choose the best form for integer.
358 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000359 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
360 if (IsSigned) {
361 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
362 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
363 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
364 } else {
365 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
366 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
367 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
368 }
369 return DW_FORM_data8;
370 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000371
Jim Laskey0d086af2006-02-27 12:43:29 +0000372 /// EmitValue - Emit integer of appropriate size.
373 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000374 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000375
Jim Laskey0d086af2006-02-27 12:43:29 +0000376 /// SizeOf - Determine size of integer value in bytes.
377 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000378 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000379
Jim Laskeyef42a012006-11-02 20:12:39 +0000380 /// Profile - Used to gather unique data for the value folding set.
381 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000382 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000383 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000384 ID.AddInteger(Integer);
385 }
Jim Laskey5496f012006-11-09 14:52:14 +0000386 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000387
Jim Laskeyef42a012006-11-02 20:12:39 +0000388#ifndef NDEBUG
389 virtual void print(std::ostream &O) {
390 O << "Int: " << (int64_t)Integer
391 << " 0x" << std::hex << Integer << std::dec;
392 }
393#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000394};
Jim Laskey063e7652006-01-17 17:31:53 +0000395
Jim Laskey0d086af2006-02-27 12:43:29 +0000396//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000397/// DIEString - A string value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000398///
Jim Laskeyef42a012006-11-02 20:12:39 +0000399class DIEString : public DIEValue {
400public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000401 const std::string String;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000402
Dan Gohman81975f62007-08-27 14:50:10 +0000403 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000404
Jim Laskey0d086af2006-02-27 12:43:29 +0000405 // Implement isa/cast/dyncast.
406 static bool classof(const DIEString *) { return true; }
407 static bool classof(const DIEValue *S) { return S->Type == isString; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000408
Jim Laskey0d086af2006-02-27 12:43:29 +0000409 /// EmitValue - Emit string value.
410 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000411 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000412
Jim Laskey0d086af2006-02-27 12:43:29 +0000413 /// SizeOf - Determine size of string value in bytes.
414 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000415 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000416 return String.size() + sizeof(char); // sizeof('\0');
417 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000418
Jim Laskeyef42a012006-11-02 20:12:39 +0000419 /// Profile - Used to gather unique data for the value folding set.
420 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000421 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000422 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000423 ID.AddString(String);
424 }
Jim Laskey5496f012006-11-09 14:52:14 +0000425 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000426
Jim Laskeyef42a012006-11-02 20:12:39 +0000427#ifndef NDEBUG
428 virtual void print(std::ostream &O) {
429 O << "Str: \"" << String << "\"";
430 }
431#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000432};
Jim Laskey063e7652006-01-17 17:31:53 +0000433
Jim Laskey0d086af2006-02-27 12:43:29 +0000434//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000435/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000436//
Jim Laskeyef42a012006-11-02 20:12:39 +0000437class DIEDwarfLabel : public DIEValue {
438public:
439
Jim Laskey0d086af2006-02-27 12:43:29 +0000440 const DWLabel Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000441
Dan Gohman81975f62007-08-27 14:50:10 +0000442 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000443
Jim Laskey0d086af2006-02-27 12:43:29 +0000444 // Implement isa/cast/dyncast.
445 static bool classof(const DIEDwarfLabel *) { return true; }
446 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000447
Jim Laskey0d086af2006-02-27 12:43:29 +0000448 /// EmitValue - Emit label value.
449 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000450 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000451
Jim Laskey0d086af2006-02-27 12:43:29 +0000452 /// SizeOf - Determine size of label value in bytes.
453 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000454 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000455
Jim Laskeyef42a012006-11-02 20:12:39 +0000456 /// Profile - Used to gather unique data for the value folding set.
457 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000458 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000459 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000460 Label.Profile(ID);
461 }
Jim Laskey5496f012006-11-09 14:52:14 +0000462 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000463
Jim Laskeyef42a012006-11-02 20:12:39 +0000464#ifndef NDEBUG
465 virtual void print(std::ostream &O) {
466 O << "Lbl: ";
467 Label.print(O);
468 }
469#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000470};
Jim Laskey063e7652006-01-17 17:31:53 +0000471
Jim Laskey063e7652006-01-17 17:31:53 +0000472
Jim Laskey0d086af2006-02-27 12:43:29 +0000473//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000474/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000475//
Jim Laskeyef42a012006-11-02 20:12:39 +0000476class DIEObjectLabel : public DIEValue {
477public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000478 const std::string Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000479
Dan Gohman81975f62007-08-27 14:50:10 +0000480 explicit DIEObjectLabel(const std::string &L)
481 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000482
Jim Laskey0d086af2006-02-27 12:43:29 +0000483 // Implement isa/cast/dyncast.
484 static bool classof(const DIEObjectLabel *) { return true; }
485 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000486
Jim Laskey0d086af2006-02-27 12:43:29 +0000487 /// EmitValue - Emit label value.
488 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000489 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000490
Jim Laskey0d086af2006-02-27 12:43:29 +0000491 /// SizeOf - Determine size of label value in bytes.
492 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000493 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000494
Jim Laskeyef42a012006-11-02 20:12:39 +0000495 /// Profile - Used to gather unique data for the value folding set.
496 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000497 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000498 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000499 ID.AddString(Label);
500 }
Jim Laskey5496f012006-11-09 14:52:14 +0000501 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000502
503#ifndef NDEBUG
504 virtual void print(std::ostream &O) {
505 O << "Obj: " << Label;
506 }
507#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000508};
Jim Laskey063e7652006-01-17 17:31:53 +0000509
Jim Laskey0d086af2006-02-27 12:43:29 +0000510//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000511/// DIESectionOffset - A section offset DIE.
512//
513class DIESectionOffset : public DIEValue {
514public:
515 const DWLabel Label;
516 const DWLabel Section;
517 bool IsEH : 1;
518 bool UseSet : 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000519
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000520 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
521 bool isEH = false, bool useSet = true)
522 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
523 IsEH(isEH), UseSet(useSet) {}
524
525 // Implement isa/cast/dyncast.
526 static bool classof(const DIESectionOffset *) { return true; }
527 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000528
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000529 /// EmitValue - Emit section offset.
530 ///
531 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000532
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000533 /// SizeOf - Determine size of section offset value in bytes.
534 ///
535 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000536
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000537 /// Profile - Used to gather unique data for the value folding set.
538 ///
539 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
540 const DWLabel &Section) {
541 ID.AddInteger(isSectionOffset);
542 Label.Profile(ID);
543 Section.Profile(ID);
544 // IsEH and UseSet are specific to the Label/Section that we will emit
545 // the offset for; so Label/Section are enough for uniqueness.
546 }
547 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
548
549#ifndef NDEBUG
550 virtual void print(std::ostream &O) {
551 O << "Off: ";
552 Label.print(O);
553 O << "-";
554 Section.print(O);
555 O << "-" << IsEH << "-" << UseSet;
556 }
557#endif
558};
559
560//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000561/// DIEDelta - A simple label difference DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000562///
Jim Laskeyef42a012006-11-02 20:12:39 +0000563class DIEDelta : public DIEValue {
564public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000565 const DWLabel LabelHi;
566 const DWLabel LabelLo;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000567
Jim Laskey0d086af2006-02-27 12:43:29 +0000568 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
569 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000570
Jim Laskey0d086af2006-02-27 12:43:29 +0000571 // Implement isa/cast/dyncast.
572 static bool classof(const DIEDelta *) { return true; }
573 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000574
Jim Laskey0d086af2006-02-27 12:43:29 +0000575 /// EmitValue - Emit delta value.
576 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000577 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000578
Jim Laskey0d086af2006-02-27 12:43:29 +0000579 /// SizeOf - Determine size of delta value in bytes.
580 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000581 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000582
Jim Laskeyef42a012006-11-02 20:12:39 +0000583 /// Profile - Used to gather unique data for the value folding set.
584 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000585 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
586 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000587 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000588 LabelHi.Profile(ID);
589 LabelLo.Profile(ID);
590 }
Jim Laskey5496f012006-11-09 14:52:14 +0000591 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000592
593#ifndef NDEBUG
594 virtual void print(std::ostream &O) {
595 O << "Del: ";
596 LabelHi.print(O);
597 O << "-";
598 LabelLo.print(O);
599 }
600#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000601};
Jim Laskey063e7652006-01-17 17:31:53 +0000602
Jim Laskey0d086af2006-02-27 12:43:29 +0000603//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000604/// DIEntry - A pointer to another debug information entry. An instance of this
605/// class can also be used as a proxy for a debug information entry not yet
606/// defined (ie. types.)
607class DIEntry : public DIEValue {
608public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000609 DIE *Entry;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000610
Dan Gohman81975f62007-08-27 14:50:10 +0000611 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000612
Jim Laskey0d086af2006-02-27 12:43:29 +0000613 // Implement isa/cast/dyncast.
614 static bool classof(const DIEntry *) { return true; }
615 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000616
Jim Laskeyb8509c52006-03-23 18:07:55 +0000617 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000618 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000619 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000620
Jim Laskeyb8509c52006-03-23 18:07:55 +0000621 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000622 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000623 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000624 return sizeof(int32_t);
625 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000626
Jim Laskeyef42a012006-11-02 20:12:39 +0000627 /// Profile - Used to gather unique data for the value folding set.
628 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000629 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
630 ID.AddInteger(isEntry);
631 ID.AddPointer(Entry);
632 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000633 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000634 ID.AddInteger(isEntry);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000635
Jim Laskeyef42a012006-11-02 20:12:39 +0000636 if (Entry) {
637 ID.AddPointer(Entry);
638 } else {
639 ID.AddPointer(this);
640 }
641 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000642
Jim Laskeyef42a012006-11-02 20:12:39 +0000643#ifndef NDEBUG
644 virtual void print(std::ostream &O) {
645 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
646 }
647#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000648};
649
650//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000651/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000652//
Jim Laskeyef42a012006-11-02 20:12:39 +0000653class DIEBlock : public DIEValue, public DIE {
654public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000655 unsigned Size; // Size in bytes excluding size header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000656
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000657 DIEBlock()
658 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000659 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000660 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000661 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000662 ~DIEBlock() {
663 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000664
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000665 // Implement isa/cast/dyncast.
666 static bool classof(const DIEBlock *) { return true; }
667 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000668
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000669 /// ComputeSize - calculate the size of the block.
670 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000671 unsigned ComputeSize(DwarfDebug &DD);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000672
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000673 /// BestForm - Choose the best form for data.
674 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000675 unsigned BestForm() const {
676 if ((unsigned char)Size == Size) return DW_FORM_block1;
677 if ((unsigned short)Size == Size) return DW_FORM_block2;
678 if ((unsigned int)Size == Size) return DW_FORM_block4;
679 return DW_FORM_block;
680 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000681
682 /// EmitValue - Emit block data.
683 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000684 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000685
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000686 /// SizeOf - Determine size of block data in bytes.
687 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000688 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000689
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000690
Jim Laskeyef42a012006-11-02 20:12:39 +0000691 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000692 ///
Reid Spencer97821312006-11-02 23:56:21 +0000693 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000694 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000695 DIE::Profile(ID);
696 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000697
Jim Laskeyef42a012006-11-02 20:12:39 +0000698#ifndef NDEBUG
699 virtual void print(std::ostream &O) {
700 O << "Blk: ";
701 DIE::print(O, 5);
702 }
703#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000704};
705
706//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000707/// CompileUnit - This dwarf writer support class manages information associate
708/// with a source file.
709class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000710private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000711 /// Desc - Compile unit debug descriptor.
712 ///
713 CompileUnitDesc *Desc;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000714
Jim Laskeyef42a012006-11-02 20:12:39 +0000715 /// ID - File identifier for source.
716 ///
717 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000718
Jim Laskeyef42a012006-11-02 20:12:39 +0000719 /// Die - Compile unit debug information entry.
720 ///
721 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000722
Jim Laskeyef42a012006-11-02 20:12:39 +0000723 /// DescToDieMap - Tracks the mapping of unit level debug informaton
724 /// descriptors to debug information entries.
725 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
726
727 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
728 /// descriptors to debug information entries using a DIEntry proxy.
729 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
730
731 /// Globals - A map of globally visible named entities for this unit.
732 ///
733 std::map<std::string, DIE *> Globals;
734
735 /// DiesSet - Used to uniquely define dies within the compile unit.
736 ///
737 FoldingSet<DIE> DiesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000738
Jim Laskeyef42a012006-11-02 20:12:39 +0000739 /// Dies - List of all dies in the compile unit.
740 ///
741 std::vector<DIE *> Dies;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000742
Jim Laskey0d086af2006-02-27 12:43:29 +0000743public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000744 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
745 : Desc(CUD)
746 , ID(I)
747 , Die(D)
748 , DescToDieMap()
749 , DescToDIEntryMap()
750 , Globals()
751 , DiesSet(InitDiesSetSize)
752 , Dies()
753 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000754
Jim Laskeyef42a012006-11-02 20:12:39 +0000755 ~CompileUnit() {
756 delete Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000757
Jim Laskeyef42a012006-11-02 20:12:39 +0000758 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
759 delete Dies[i];
760 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000761
Jim Laskeybd761842006-02-27 17:27:12 +0000762 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000763 CompileUnitDesc *getDesc() const { return Desc; }
764 unsigned getID() const { return ID; }
765 DIE* getDie() const { return Die; }
766 std::map<std::string, DIE *> &getGlobals() { return Globals; }
767
768 /// hasContent - Return true if this compile unit has something to write out.
769 ///
770 bool hasContent() const {
771 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000772 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000773
774 /// AddGlobal - Add a new global entity to the compile unit.
775 ///
776 void AddGlobal(const std::string &Name, DIE *Die) {
777 Globals[Name] = Die;
778 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000779
Jim Laskeyef42a012006-11-02 20:12:39 +0000780 /// getDieMapSlotFor - Returns the debug information entry map slot for the
781 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000782 DIE *&getDieMapSlotFor(DebugInfoDesc *DID) {
783 return DescToDieMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000784 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000785
Jim Laskeyef42a012006-11-02 20:12:39 +0000786 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
787 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000788 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DID) {
789 return DescToDIEntryMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000790 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000791
Jim Laskeyef42a012006-11-02 20:12:39 +0000792 /// AddDie - Adds or interns the DIE to the compile unit.
793 ///
794 DIE *AddDie(DIE &Buffer) {
795 FoldingSetNodeID ID;
796 Buffer.Profile(ID);
797 void *Where;
798 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000799
Jim Laskeyef42a012006-11-02 20:12:39 +0000800 if (!Die) {
801 Die = new DIE(Buffer);
802 DiesSet.InsertNode(Die, Where);
803 this->Die->AddChild(Die);
804 Buffer.Detach();
805 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000806
Jim Laskeyef42a012006-11-02 20:12:39 +0000807 return Die;
808 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000809};
810
Jim Laskey65195462006-10-30 13:35:07 +0000811//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000812/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000813///
Jim Laskey65195462006-10-30 13:35:07 +0000814class Dwarf {
815
Jim Laskey072200c2007-01-29 18:51:14 +0000816protected:
Jim Laskey65195462006-10-30 13:35:07 +0000817
818 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000819 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000820 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000821
Jim Laskey65195462006-10-30 13:35:07 +0000822 //
823 /// O - Stream to .s file.
824 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000825 raw_ostream &O;
Jim Laskey65195462006-10-30 13:35:07 +0000826
827 /// Asm - Target of Dwarf emission.
828 ///
829 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000830
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000831 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000832 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000833
Jim Laskey65195462006-10-30 13:35:07 +0000834 /// TD - Target data.
835 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000836
Jim Laskey65195462006-10-30 13:35:07 +0000837 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000838 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000839
Jim Laskey65195462006-10-30 13:35:07 +0000840 /// M - Current module.
841 ///
842 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000843
Jim Laskey65195462006-10-30 13:35:07 +0000844 /// MF - Current machine function.
845 ///
846 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000847
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000848 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000849 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000850 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000851
Jim Laskey65195462006-10-30 13:35:07 +0000852 /// SubprogramCount - The running count of functions being compiled.
853 ///
854 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000855
Chris Lattner9251f132007-09-24 03:35:37 +0000856 /// Flavor - A unique string indicating what dwarf producer this is, used to
857 /// unique labels.
858 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000859
860 unsigned SetCounter;
Owen Andersoncb371882008-08-21 00:14:44 +0000861 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattner9251f132007-09-24 03:35:37 +0000862 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000863 : O(OS)
864 , Asm(A)
865 , TAI(T)
866 , TD(Asm->TM.getTargetData())
867 , RI(Asm->TM.getRegisterInfo())
868 , M(NULL)
869 , MF(NULL)
870 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000871 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000872 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000873 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000874 {
875 }
876
877public:
878
879 //===--------------------------------------------------------------------===//
880 // Accessors.
881 //
882 AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000883 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000884 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000885 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000886
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000887 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
888 const {
889 if (isInSection && TAI->getDwarfSectionOffsetDirective())
890 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000891 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000892 O << TAI->getData32bitsDirective();
893 else
894 O << TAI->getData64bitsDirective();
895 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000896
Jim Laskeyb82313f2007-02-01 16:31:34 +0000897 /// PrintLabelName - Print label name in form used by Dwarf writer.
898 ///
899 void PrintLabelName(DWLabel Label) const {
900 PrintLabelName(Label.Tag, Label.Number);
901 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000902 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000903 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000904 if (Number) O << Number;
905 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000906
Chris Lattner9251f132007-09-24 03:35:37 +0000907 void PrintLabelName(const char *Tag, unsigned Number,
908 const char *Suffix) const {
909 O << TAI->getPrivateGlobalPrefix() << Tag;
910 if (Number) O << Number;
911 O << Suffix;
912 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000913
Jim Laskeyb82313f2007-02-01 16:31:34 +0000914 /// EmitLabel - Emit location label for internal use by Dwarf.
915 ///
916 void EmitLabel(DWLabel Label) const {
917 EmitLabel(Label.Tag, Label.Number);
918 }
919 void EmitLabel(const char *Tag, unsigned Number) const {
920 PrintLabelName(Tag, Number);
921 O << ":\n";
922 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000923
Jim Laskeyb82313f2007-02-01 16:31:34 +0000924 /// EmitReference - Emit a reference to a label.
925 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000926 void EmitReference(DWLabel Label, bool IsPCRelative = false,
927 bool Force32Bit = false) const {
928 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000929 }
930 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000931 bool IsPCRelative = false, bool Force32Bit = false) const {
932 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000933 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000934
Jim Laskeyb82313f2007-02-01 16:31:34 +0000935 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
936 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000937 void EmitReference(const std::string &Name, bool IsPCRelative = false,
938 bool Force32Bit = false) const {
939 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000940
Jim Laskeyb82313f2007-02-01 16:31:34 +0000941 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000942
Jim Laskeyb82313f2007-02-01 16:31:34 +0000943 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
944 }
945
946 /// EmitDifference - Emit the difference between two labels. Some
947 /// assemblers do not behave with absolute expressions with data directives,
948 /// so there is an option (needsSet) to use an intermediary set expression.
949 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000950 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000951 EmitDifference(LabelHi.Tag, LabelHi.Number,
952 LabelLo.Tag, LabelLo.Number,
953 IsSmall);
954 }
955 void EmitDifference(const char *TagHi, unsigned NumberHi,
956 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000957 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000958 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000959 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000960 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000961 O << ",";
962 PrintLabelName(TagHi, NumberHi);
963 O << "-";
964 PrintLabelName(TagLo, NumberLo);
965 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000966
967 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +0000968 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000969 ++SetCounter;
970 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000971 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000972
Jim Laskeyb82313f2007-02-01 16:31:34 +0000973 PrintLabelName(TagHi, NumberHi);
974 O << "-";
975 PrintLabelName(TagLo, NumberLo);
976 }
977 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000978
979 void EmitSectionOffset(const char* Label, const char* Section,
980 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000981 bool IsSmall = false, bool isEH = false,
982 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000983 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +0000984 if (isEH)
985 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
986 else
987 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
988
989 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000990 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000991 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000992 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000993 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000994
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000995 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000996 O << "-";
997 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000998 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000999 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001000
1001 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001002
Chris Lattner9251f132007-09-24 03:35:37 +00001003 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001004 ++SetCounter;
1005 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001006 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001007
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001008 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001009
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001010 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001011 O << "-";
1012 PrintLabelName(Section, SectionNumber);
1013 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001014 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001015 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001016
Jim Laskeyb82313f2007-02-01 16:31:34 +00001017 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1018 /// frame.
1019 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +00001020 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001021 int stackGrowth =
1022 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1023 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001024 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +00001025 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +00001026
1027 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001028 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +00001029 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001030
Jim Laskeyb82313f2007-02-01 16:31:34 +00001031 if (LabelID) {
1032 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001033
Jim Laskeyb82313f2007-02-01 16:31:34 +00001034 // Throw out move if the label is invalid.
1035 if (!LabelID) continue;
1036 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001037
Jim Laskeyb82313f2007-02-01 16:31:34 +00001038 const MachineLocation &Dst = Move.getDestination();
1039 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001040
Jim Laskeyb82313f2007-02-01 16:31:34 +00001041 // Advance row if new location.
1042 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1043 Asm->EmitInt8(DW_CFA_advance_loc4);
1044 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001045 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1046 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001047
Jim Laskeyb82313f2007-02-01 16:31:34 +00001048 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001049 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001050 IsLocal = true;
1051 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001052
Jim Laskeyb82313f2007-02-01 16:31:34 +00001053 // If advancing cfa.
Dan Gohmand735b802008-10-03 15:45:36 +00001054 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1055 if (!Src.isReg()) {
1056 if (Src.getReg() == MachineLocation::VirtualFP) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001057 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1058 Asm->EOL("DW_CFA_def_cfa_offset");
1059 } else {
1060 Asm->EmitInt8(DW_CFA_def_cfa);
1061 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmand735b802008-10-03 15:45:36 +00001062 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001063 Asm->EOL("Register");
1064 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001065
Jim Laskeyb82313f2007-02-01 16:31:34 +00001066 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001067
Jim Laskeyb82313f2007-02-01 16:31:34 +00001068 Asm->EmitULEB128Bytes(Offset);
1069 Asm->EOL("Offset");
1070 } else {
1071 assert(0 && "Machine move no supported yet.");
1072 }
Dan Gohmand735b802008-10-03 15:45:36 +00001073 } else if (Src.isReg() &&
1074 Src.getReg() == MachineLocation::VirtualFP) {
1075 if (Dst.isReg()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001076 Asm->EmitInt8(DW_CFA_def_cfa_register);
1077 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmand735b802008-10-03 15:45:36 +00001078 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001079 Asm->EOL("Register");
1080 } else {
1081 assert(0 && "Machine move no supported yet.");
1082 }
1083 } else {
Dan Gohmand735b802008-10-03 15:45:36 +00001084 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001085 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001086
Jim Laskeyb82313f2007-02-01 16:31:34 +00001087 if (Offset < 0) {
1088 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1089 Asm->EOL("DW_CFA_offset_extended_sf");
1090 Asm->EmitULEB128Bytes(Reg);
1091 Asm->EOL("Reg");
1092 Asm->EmitSLEB128Bytes(Offset);
1093 Asm->EOL("Offset");
1094 } else if (Reg < 64) {
1095 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Chengd4114192008-07-09 21:53:02 +00001096 if (VerboseAsm)
1097 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1098 else
1099 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001100 Asm->EmitULEB128Bytes(Offset);
1101 Asm->EOL("Offset");
1102 } else {
1103 Asm->EmitInt8(DW_CFA_offset_extended);
1104 Asm->EOL("DW_CFA_offset_extended");
1105 Asm->EmitULEB128Bytes(Reg);
1106 Asm->EOL("Reg");
1107 Asm->EmitULEB128Bytes(Offset);
1108 Asm->EOL("Offset");
1109 }
1110 }
1111 }
1112 }
1113
Jim Laskey072200c2007-01-29 18:51:14 +00001114};
1115
1116//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001117/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001118///
1119class DwarfDebug : public Dwarf {
1120
1121private:
Jim Laskey65195462006-10-30 13:35:07 +00001122 //===--------------------------------------------------------------------===//
1123 // Attributes used to construct specific Dwarf sections.
1124 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001125
Jim Laskey65195462006-10-30 13:35:07 +00001126 /// CompileUnits - All the compile units involved in this build. The index
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001127 /// of each entry in this vector corresponds to the sources in MMI.
Jim Laskey65195462006-10-30 13:35:07 +00001128 std::vector<CompileUnit *> CompileUnits;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001129
Jim Laskeyef42a012006-11-02 20:12:39 +00001130 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001131 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001132 FoldingSet<DIEAbbrev> AbbreviationsSet;
1133
1134 /// Abbreviations - A list of all the unique abbreviations in use.
1135 ///
1136 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001137
Jim Laskeyef42a012006-11-02 20:12:39 +00001138 /// ValuesSet - Used to uniquely define values.
1139 ///
1140 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001141
Jim Laskeyef42a012006-11-02 20:12:39 +00001142 /// Values - A list of all the unique values in use.
1143 ///
1144 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001145
Jim Laskey65195462006-10-30 13:35:07 +00001146 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001147 ///
Jim Laskey65195462006-10-30 13:35:07 +00001148 UniqueVector<std::string> StringPool;
1149
1150 /// UnitMap - Map debug information descriptor to compile unit.
1151 ///
1152 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001153
Jim Laskey65195462006-10-30 13:35:07 +00001154 /// SectionMap - Provides a unique id per text section.
1155 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001156 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001157
Jim Laskey65195462006-10-30 13:35:07 +00001158 /// SectionSourceLines - Tracks line numbers per text section.
1159 ///
1160 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1161
Jim Laskeybacd3042007-02-21 22:48:45 +00001162 /// didInitial - Flag to indicate if initial emission has been done.
1163 ///
1164 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001165
Jim Laskeybacd3042007-02-21 22:48:45 +00001166 /// shouldEmit - Flag to indicate if debug information should be emitted.
1167 ///
1168 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001169
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001170 struct FunctionDebugFrameInfo {
1171 unsigned Number;
1172 std::vector<MachineMove> Moves;
1173
1174 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001175 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001176 };
1177
1178 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001179
Jim Laskey65195462006-10-30 13:35:07 +00001180public:
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001181
Jim Laskeybacd3042007-02-21 22:48:45 +00001182 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1183 ///
1184 bool ShouldEmitDwarf() const { return shouldEmit; }
Jim Laskey65195462006-10-30 13:35:07 +00001185
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001186 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001187 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001188 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1189 // Profile the node so that we can make it unique.
1190 FoldingSetNodeID ID;
1191 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001192
Jim Laskeyef42a012006-11-02 20:12:39 +00001193 // Check the set for priors.
1194 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001195
Jim Laskeyef42a012006-11-02 20:12:39 +00001196 // If it's newly added.
1197 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001198 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001199 Abbreviations.push_back(&Abbrev);
1200 // Assign the vector position + 1 as its number.
1201 Abbrev.setNumber(Abbreviations.size());
1202 } else {
1203 // Assign existing abbreviation number.
1204 Abbrev.setNumber(InSet->getNumber());
1205 }
1206 }
1207
Jim Laskey65195462006-10-30 13:35:07 +00001208 /// NewString - Add a string to the constant pool and returns a label.
1209 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001210 DWLabel NewString(const std::string &String) {
1211 unsigned StringID = StringPool.insert(String);
1212 return DWLabel("string", StringID);
1213 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001214
Jim Laskeyef42a012006-11-02 20:12:39 +00001215 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1216 /// entry.
1217 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1218 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001219
Jim Laskeyef42a012006-11-02 20:12:39 +00001220 if (Entry) {
1221 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001222 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001223 void *Where;
1224 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001225
Jim Laskeyf6733882006-11-02 21:48:18 +00001226 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001227
Jim Laskeyef42a012006-11-02 20:12:39 +00001228 Value = new DIEntry(Entry);
1229 ValuesSet.InsertNode(Value, Where);
1230 } else {
1231 Value = new DIEntry(Entry);
1232 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001233
Jim Laskeyef42a012006-11-02 20:12:39 +00001234 Values.push_back(Value);
1235 return Value;
1236 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001237
Jim Laskeyef42a012006-11-02 20:12:39 +00001238 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1239 ///
1240 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1241 Value->Entry = Entry;
1242 // Add to values set if not already there. If it is, we merely have a
1243 // duplicate in the values list (no harm.)
1244 ValuesSet.GetOrInsertNode(Value);
1245 }
1246
1247 /// AddUInt - Add an unsigned integer attribute data and value.
1248 ///
1249 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1250 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1251
1252 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001253 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001254 void *Where;
1255 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1256 if (!Value) {
1257 Value = new DIEInteger(Integer);
1258 ValuesSet.InsertNode(Value, Where);
1259 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001260 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001261
Jim Laskeyef42a012006-11-02 20:12:39 +00001262 Die->AddValue(Attribute, Form, Value);
1263 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001264
Jim Laskeyef42a012006-11-02 20:12:39 +00001265 /// AddSInt - Add an signed integer attribute data and value.
1266 ///
1267 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1268 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1269
1270 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001271 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001272 void *Where;
1273 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1274 if (!Value) {
1275 Value = new DIEInteger(Integer);
1276 ValuesSet.InsertNode(Value, Where);
1277 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001278 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001279
Jim Laskeyef42a012006-11-02 20:12:39 +00001280 Die->AddValue(Attribute, Form, Value);
1281 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001282
Jim Laskeyef42a012006-11-02 20:12:39 +00001283 /// AddString - Add a std::string attribute data and value.
1284 ///
1285 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1286 const std::string &String) {
1287 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001288 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001289 void *Where;
1290 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1291 if (!Value) {
1292 Value = new DIEString(String);
1293 ValuesSet.InsertNode(Value, Where);
1294 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001295 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001296
Jim Laskeyef42a012006-11-02 20:12:39 +00001297 Die->AddValue(Attribute, Form, Value);
1298 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001299
Jim Laskeyef42a012006-11-02 20:12:39 +00001300 /// AddLabel - Add a Dwarf label attribute data and value.
1301 ///
1302 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1303 const DWLabel &Label) {
1304 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001305 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001306 void *Where;
1307 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1308 if (!Value) {
1309 Value = new DIEDwarfLabel(Label);
1310 ValuesSet.InsertNode(Value, Where);
1311 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001312 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001313
Jim Laskeyef42a012006-11-02 20:12:39 +00001314 Die->AddValue(Attribute, Form, Value);
1315 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001316
Jim Laskeyef42a012006-11-02 20:12:39 +00001317 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1318 ///
1319 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1320 const std::string &Label) {
1321 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001322 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001323 void *Where;
1324 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1325 if (!Value) {
1326 Value = new DIEObjectLabel(Label);
1327 ValuesSet.InsertNode(Value, Where);
1328 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001329 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001330
Jim Laskeyef42a012006-11-02 20:12:39 +00001331 Die->AddValue(Attribute, Form, Value);
1332 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001333
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001334 /// AddSectionOffset - Add a section offset label attribute data and value.
1335 ///
1336 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1337 const DWLabel &Label, const DWLabel &Section,
1338 bool isEH = false, bool useSet = true) {
1339 FoldingSetNodeID ID;
1340 DIESectionOffset::Profile(ID, Label, Section);
1341 void *Where;
1342 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1343 if (!Value) {
1344 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1345 ValuesSet.InsertNode(Value, Where);
1346 Values.push_back(Value);
1347 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001348
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001349 Die->AddValue(Attribute, Form, Value);
1350 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001351
Jim Laskeyef42a012006-11-02 20:12:39 +00001352 /// AddDelta - Add a label delta attribute data and value.
1353 ///
1354 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1355 const DWLabel &Hi, const DWLabel &Lo) {
1356 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001357 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001358 void *Where;
1359 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1360 if (!Value) {
1361 Value = new DIEDelta(Hi, Lo);
1362 ValuesSet.InsertNode(Value, Where);
1363 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001364 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001365
Jim Laskeyef42a012006-11-02 20:12:39 +00001366 Die->AddValue(Attribute, Form, Value);
1367 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001368
Jim Laskeyef42a012006-11-02 20:12:39 +00001369 /// AddDIEntry - Add a DIE attribute data and value.
1370 ///
1371 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1372 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1373 }
1374
1375 /// AddBlock - Add block data.
1376 ///
1377 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1378 Block->ComputeSize(*this);
1379 FoldingSetNodeID ID;
1380 Block->Profile(ID);
1381 void *Where;
1382 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1383 if (!Value) {
1384 Value = Block;
1385 ValuesSet.InsertNode(Value, Where);
1386 Values.push_back(Value);
1387 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001388 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001389 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001390 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001391 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001392
Jim Laskeyef42a012006-11-02 20:12:39 +00001393 Die->AddValue(Attribute, Block->BestForm(), Value);
1394 }
1395
Jim Laskey65195462006-10-30 13:35:07 +00001396private:
1397
1398 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001399 /// entry.
1400 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1401 if (File && Line) {
1402 CompileUnit *FileUnit = FindCompileUnit(File);
1403 unsigned FileID = FileUnit->getID();
1404 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1405 AddUInt(Die, DW_AT_decl_line, 0, Line);
1406 }
1407 }
Jim Laskey65195462006-10-30 13:35:07 +00001408
1409 /// AddAddress - Add an address attribute to a die based on the location
1410 /// provided.
1411 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001412 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001413 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001414 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001415
Dan Gohmand735b802008-10-03 15:45:36 +00001416 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001417 if (Reg < 32) {
1418 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1419 } else {
1420 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1421 AddUInt(Block, 0, DW_FORM_udata, Reg);
1422 }
1423 } else {
1424 if (Reg < 32) {
1425 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1426 } else {
1427 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1428 AddUInt(Block, 0, DW_FORM_udata, Reg);
1429 }
1430 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1431 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001432
Jim Laskeyef42a012006-11-02 20:12:39 +00001433 AddBlock(Die, Attribute, 0, Block);
1434 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001435
Jim Laskeyef42a012006-11-02 20:12:39 +00001436 /// AddBasicType - Add a new basic type attribute to the specified entity.
1437 ///
1438 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1439 const std::string &Name,
1440 unsigned Encoding, unsigned Size) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001441
Jim Laskeyef42a012006-11-02 20:12:39 +00001442 DIE Buffer(DW_TAG_base_type);
1443 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1444 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1445 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel9ede3f22009-01-05 17:44:11 +00001446 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1447 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001448 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001449
Jim Laskeyef42a012006-11-02 20:12:39 +00001450 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1451 ///
1452 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001453 DIE Buffer(DW_TAG_pointer_type);
Dan Gohman82482942007-09-27 23:12:31 +00001454 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Jim Laskeyef42a012006-11-02 20:12:39 +00001455 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelca03c272009-01-05 17:45:59 +00001456 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1457 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001458 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001459
Jim Laskeyef42a012006-11-02 20:12:39 +00001460 /// AddType - Add a new type attribute to the specified entity.
1461 ///
1462 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1463 if (!TyDesc) {
Jim Laskey2b935d52007-01-26 14:19:17 +00001464 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001465 } else {
1466 // Check for pre-existence.
1467 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001468
Jim Laskeyef42a012006-11-02 20:12:39 +00001469 // If it exists then use the existing value.
1470 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001471 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1472 return;
1473 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001474
Jim Laskeyef42a012006-11-02 20:12:39 +00001475 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1476 // FIXME - Not sure why programs and variables are coming through here.
1477 // Short cut for handling subprogram types (not really a TyDesc.)
1478 AddPointerType(Entity, Unit, SubprogramTy->getName());
1479 } else if (GlobalVariableDesc *GlobalTy =
1480 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1481 // FIXME - Not sure why programs and variables are coming through here.
1482 // Short cut for handling global variable types (not really a TyDesc.)
1483 AddPointerType(Entity, Unit, GlobalTy->getName());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001484 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001485 // Set up proxy.
1486 Slot = NewDIEntry();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001487
Jim Laskeyef42a012006-11-02 20:12:39 +00001488 // Construct type.
1489 DIE Buffer(DW_TAG_base_type);
1490 ConstructType(Buffer, TyDesc, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001491
Jim Laskeyef42a012006-11-02 20:12:39 +00001492 // Add debug information entry to entity and unit.
1493 DIE *Die = Unit->AddDie(Buffer);
1494 SetDIEntry(Slot, Die);
1495 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1496 }
1497 }
1498 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001499
Devang Patel08f053f2009-01-05 17:57:47 +00001500 /// ConstructType - Construct basic type die from DIBasicType.
1501 void ConstructType(CompileUnit *DW_Unit, DIE &Buffer,
1502 DIBasicType *BTy) {
1503
1504 // Get core information.
1505 const std::string &Name = BTy->getName();
1506 Buffer.setTag(DW_TAG_base_type);
1507 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy->getEncoding());
1508 // Add name if not anonymous or intermediate type.
1509 if (!Name.empty())
1510 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1511 uint64_t Size = BTy->getSizeInBits() >> 3;
1512 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1513 }
1514
Devang Patel68afdc32009-01-05 18:33:01 +00001515 /// ConstructType - Construct derived type die from DIDerivedType.
Devang Patel08f053f2009-01-05 17:57:47 +00001516 void ConstructType(CompileUnit *DW_Unit, DIE &Buffer,
1517 DIDerivedType *DTy) {
1518
1519 // Get core information.
1520 const std::string &Name = DTy->getName();
1521 uint64_t Size = DTy->getSizeInBits() >> 3;
1522 unsigned Tag = DTy->getTag();
1523 // FIXME - Workaround for templates.
1524 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1525
1526 Buffer.setTag(Tag);
1527 // Map to main type, void will not have a type.
1528 DIType FromTy = DTy->getTypeDerivedFrom();
1529 // FIXME - Enable this. AddType(&Buffer, FromTy, DW_Unit);
1530
1531 // Add name if not anonymous or intermediate type.
1532 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1533
1534 // Add size if non-zero (derived types might be zero-sized.)
1535 if (Size)
1536 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1537
1538 // Add source line info if available and TyDesc is not a forward
1539 // declaration.
1540 // FIXME - Enable this. if (!DTy->isForwardDecl())
1541 // FIXME - Enable this. AddSourceLine(&Buffer, *DTy);
1542 }
1543
Devang Patel68afdc32009-01-05 18:33:01 +00001544 // ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1545 void ConstructSubrangeDIE (DIE &Buffer, DISubrange *SR, DIE *IndexTy) {
1546 int64_t L = SR->getLo();
1547 int64_t H = SR->getHi();
1548 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1549 if (L != H) {
1550 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1551 if (L)
1552 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1553 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
1554 }
1555 Buffer.AddChild(DW_Subrange);
1556 }
1557
1558 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1559 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1560 DICompositeType *CTy) {
1561 Buffer.setTag(DW_TAG_array_type);
1562 if (CTy->getTag() == DW_TAG_vector_type)
1563 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1564
1565 DIArray Elements = CTy->getTypeArray();
1566 // FIXME - Enable this.
1567 // AddType(&Buffer, CTy->getTypeDerivedFrom(), DW_Unit);
1568
1569 // Construct an anonymous type for index type.
1570 DIE IdxBuffer(DW_TAG_base_type);
1571 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1572 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1573 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1574
1575 // Add subranges to array type.
1576 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1577 DISubrange Element = Elements.getElement(i);
1578 ConstructSubrangeDIE(Buffer, &Element, IndexTy);
1579 }
1580 }
1581
1582
Jim Laskeyef42a012006-11-02 20:12:39 +00001583 /// ConstructType - Adds all the required attributes to the type.
1584 ///
1585 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1586 // Get core information.
1587 const std::string &Name = TyDesc->getName();
1588 uint64_t Size = TyDesc->getSize() >> 3;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001589
Jim Laskeyef42a012006-11-02 20:12:39 +00001590 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1591 // Fundamental types like int, float, bool
1592 Buffer.setTag(DW_TAG_base_type);
1593 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1594 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00001595 // Fetch tag.
1596 unsigned Tag = DerivedTy->getTag();
1597 // FIXME - Workaround for templates.
1598 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001599 // Pointers, typedefs et al.
Jim Laskey85f419b2006-11-09 16:32:26 +00001600 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00001601 // Map to main type, void will not have a type.
1602 if (TypeDesc *FromTy = DerivedTy->getFromType())
1603 AddType(&Buffer, FromTy, Unit);
1604 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1605 // Fetch tag.
1606 unsigned Tag = CompTy->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001607
Jim Laskeyef42a012006-11-02 20:12:39 +00001608 // Set tag accordingly.
1609 if (Tag == DW_TAG_vector_type)
1610 Buffer.setTag(DW_TAG_array_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001611 else
Jim Laskeyef42a012006-11-02 20:12:39 +00001612 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00001613
Jim Laskeyef42a012006-11-02 20:12:39 +00001614 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001615
Jim Laskeyef42a012006-11-02 20:12:39 +00001616 switch (Tag) {
1617 case DW_TAG_vector_type:
1618 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1619 // Fall thru
1620 case DW_TAG_array_type: {
1621 // Add element type.
1622 if (TypeDesc *FromTy = CompTy->getFromType())
1623 AddType(&Buffer, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001624
Jim Laskeyef42a012006-11-02 20:12:39 +00001625 // Don't emit size attribute.
1626 Size = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001627
Jim Laskeyef42a012006-11-02 20:12:39 +00001628 // Construct an anonymous type for index type.
Devang Patel9ede3f22009-01-05 17:44:11 +00001629 DIE Buffer(DW_TAG_base_type);
1630 AddUInt(&Buffer, DW_AT_byte_size, 0, sizeof(int32_t));
1631 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1632 DIE *IndexTy = Unit->AddDie(Buffer);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001633
Jim Laskeyef42a012006-11-02 20:12:39 +00001634 // Add subranges to array type.
Evan Chengf8480282008-12-09 17:56:30 +00001635 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001636 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1637 int64_t Lo = SRD->getLo();
1638 int64_t Hi = SRD->getHi();
1639 DIE *Subrange = new DIE(DW_TAG_subrange_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001640
Jim Laskeyef42a012006-11-02 20:12:39 +00001641 // If a range is available.
1642 if (Lo != Hi) {
1643 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1644 // Only add low if non-zero.
1645 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1646 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1647 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001648
Jim Laskeyef42a012006-11-02 20:12:39 +00001649 Buffer.AddChild(Subrange);
1650 }
1651 break;
1652 }
1653 case DW_TAG_structure_type:
1654 case DW_TAG_union_type: {
1655 // Add elements to structure type.
Evan Chengf8480282008-12-09 17:56:30 +00001656 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001657 DebugInfoDesc *Element = Elements[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001658
Jim Laskeyef42a012006-11-02 20:12:39 +00001659 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1660 // Add field or base class.
Jim Laskeyef42a012006-11-02 20:12:39 +00001661 unsigned Tag = MemberDesc->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001662
Jim Laskeyef42a012006-11-02 20:12:39 +00001663 // Extract the basic information.
1664 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001665 uint64_t Size = MemberDesc->getSize();
1666 uint64_t Align = MemberDesc->getAlign();
1667 uint64_t Offset = MemberDesc->getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001668
Jim Laskeyef42a012006-11-02 20:12:39 +00001669 // Construct member debug information entry.
1670 DIE *Member = new DIE(Tag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001671
Jim Laskeyef42a012006-11-02 20:12:39 +00001672 // Add name if not "".
1673 if (!Name.empty())
1674 AddString(Member, DW_AT_name, DW_FORM_string, Name);
Evan Chengf8480282008-12-09 17:56:30 +00001675
Jim Laskeyef42a012006-11-02 20:12:39 +00001676 // Add location if available.
1677 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001678
Jim Laskeyef42a012006-11-02 20:12:39 +00001679 // Most of the time the field info is the same as the members.
1680 uint64_t FieldSize = Size;
1681 uint64_t FieldAlign = Align;
1682 uint64_t FieldOffset = Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001683
Jim Laskeyee5f9272006-12-22 20:03:42 +00001684 // Set the member type.
1685 TypeDesc *FromTy = MemberDesc->getFromType();
1686 AddType(Member, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001687
Jim Laskeyee5f9272006-12-22 20:03:42 +00001688 // Walk up typedefs until a real size is found.
1689 while (FromTy) {
1690 if (FromTy->getTag() != DW_TAG_typedef) {
1691 FieldSize = FromTy->getSize();
Devang Pateld0935c32008-12-23 21:55:38 +00001692 FieldAlign = FromTy->getAlign();
Jim Laskeyee5f9272006-12-22 20:03:42 +00001693 break;
1694 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001695
Dan Gohman275769a2007-07-23 20:24:29 +00001696 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
Jim Laskeyef42a012006-11-02 20:12:39 +00001697 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001698
Jim Laskeyef42a012006-11-02 20:12:39 +00001699 // Unless we have a bit field.
1700 if (Tag == DW_TAG_member && FieldSize != Size) {
1701 // Construct the alignment mask.
1702 uint64_t AlignMask = ~(FieldAlign - 1);
1703 // Determine the high bit + 1 of the declared size.
1704 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1705 // Work backwards to determine the base offset of the field.
1706 FieldOffset = HiMark - FieldSize;
1707 // Now normalize offset to the field.
1708 Offset -= FieldOffset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001709
Jim Laskeyef42a012006-11-02 20:12:39 +00001710 // Maybe we need to work from the other end.
1711 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001712
Jim Laskeyef42a012006-11-02 20:12:39 +00001713 // Add size and offset.
1714 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1715 AddUInt(Member, DW_AT_bit_size, 0, Size);
1716 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1717 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001718
Jim Laskeyef42a012006-11-02 20:12:39 +00001719 // Add computation for offset.
1720 DIEBlock *Block = new DIEBlock();
1721 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1722 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1723 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1724
1725 // Add accessibility (public default unless is base class.
1726 if (MemberDesc->isProtected()) {
1727 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1728 } else if (MemberDesc->isPrivate()) {
1729 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1730 } else if (Tag == DW_TAG_inheritance) {
1731 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1732 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001733
Jim Laskeyef42a012006-11-02 20:12:39 +00001734 Buffer.AddChild(Member);
1735 } else if (GlobalVariableDesc *StaticDesc =
1736 dyn_cast<GlobalVariableDesc>(Element)) {
1737 // Add static member.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001738
Jim Laskeyef42a012006-11-02 20:12:39 +00001739 // Construct member debug information entry.
1740 DIE *Static = new DIE(DW_TAG_variable);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001741
Jim Laskeyef42a012006-11-02 20:12:39 +00001742 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001743 const std::string &Name = StaticDesc->getName();
1744 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001745 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001746 if (!LinkageName.empty()) {
1747 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1748 LinkageName);
1749 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001750
Jim Laskeyef42a012006-11-02 20:12:39 +00001751 // Add location.
1752 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001753
Jim Laskeyef42a012006-11-02 20:12:39 +00001754 // Add type.
1755 if (TypeDesc *StaticTy = StaticDesc->getType())
1756 AddType(Static, StaticTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001757
Jim Laskeyef42a012006-11-02 20:12:39 +00001758 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001759 if (!StaticDesc->isStatic())
1760 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001761 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001762
Jim Laskeyef42a012006-11-02 20:12:39 +00001763 Buffer.AddChild(Static);
1764 } else if (SubprogramDesc *MethodDesc =
1765 dyn_cast<SubprogramDesc>(Element)) {
1766 // Add member function.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001767
Jim Laskeyef42a012006-11-02 20:12:39 +00001768 // Construct member debug information entry.
1769 DIE *Method = new DIE(DW_TAG_subprogram);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001770
Jim Laskeyef42a012006-11-02 20:12:39 +00001771 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00001772 const std::string &Name = MethodDesc->getName();
1773 const std::string &LinkageName = MethodDesc->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001774
1775 AddString(Method, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001776 bool IsCTor = TyDesc->getName() == Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001777
Jim Laskey2172f962006-11-30 14:35:45 +00001778 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001779 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001780 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001781 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001782
Jim Laskeyef42a012006-11-02 20:12:39 +00001783 // Add location.
1784 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001785
Jim Laskeyef42a012006-11-02 20:12:39 +00001786 // Add type.
1787 if (CompositeTypeDesc *MethodTy =
1788 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1789 // Get argument information.
1790 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001791
Jim Laskeyef42a012006-11-02 20:12:39 +00001792 // If not a ctor.
1793 if (!IsCTor) {
1794 // Add return type.
1795 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1796 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001797
Jim Laskeyef42a012006-11-02 20:12:39 +00001798 // Add arguments.
Evan Chengf8480282008-12-09 17:56:30 +00001799 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001800 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1801 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1802 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1803 Method->AddChild(Arg);
1804 }
1805 }
1806
1807 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00001808 if (!MethodDesc->isStatic())
1809 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001810 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001811
Jim Laskeyef42a012006-11-02 20:12:39 +00001812 Buffer.AddChild(Method);
1813 }
1814 }
1815 break;
1816 }
1817 case DW_TAG_enumeration_type: {
1818 // Add enumerators to enumeration type.
Evan Chengf8480282008-12-09 17:56:30 +00001819 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001820 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1821 const std::string &Name = ED->getName();
1822 int64_t Value = ED->getValue();
1823 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1824 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1825 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1826 Buffer.AddChild(Enumerator);
1827 }
1828
1829 break;
1830 }
1831 case DW_TAG_subroutine_type: {
1832 // Add prototype flag.
1833 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1834 // Add return type.
1835 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001836
Jim Laskeyef42a012006-11-02 20:12:39 +00001837 // Add arguments.
Evan Chengf8480282008-12-09 17:56:30 +00001838 for (unsigned i = 1, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001839 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1840 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1841 Buffer.AddChild(Arg);
1842 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001843
Jim Laskeyef42a012006-11-02 20:12:39 +00001844 break;
1845 }
1846 default: break;
1847 }
1848 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001849
Jim Laskeyef42a012006-11-02 20:12:39 +00001850 // Add name if not anonymous or intermediate type.
1851 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Evan Chengf8480282008-12-09 17:56:30 +00001852
Evan Cheng94ea5be2008-12-10 00:15:44 +00001853 // Add size if non-zero (derived types might be zero-sized.)
1854 if (Size)
1855 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1856 else if (isa<CompositeTypeDesc>(TyDesc)) {
1857 // If TyDesc is a composite type, then add size even if it's zero unless
1858 // it's a forward declaration.
1859 if (TyDesc->isForwardDecl())
1860 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1861 else
1862 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1863 }
1864
1865 // Add source line info if available and TyDesc is not a forward
1866 // declaration.
1867 if (!TyDesc->isForwardDecl())
1868 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
Jim Laskeyef42a012006-11-02 20:12:39 +00001869 }
1870
1871 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00001872 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001873 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1874 // Construct debug information entry.
1875 DIE *Die = new DIE(DW_TAG_compile_unit);
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001876 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
1877 DWLabel("section_line", 0), DWLabel("section_line", 0), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001878 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1879 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1880 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
Devang Patel83580992008-12-12 21:57:54 +00001881 if (!UnitDesc->getDirectory().empty())
1882 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001883
Jim Laskeyef42a012006-11-02 20:12:39 +00001884 // Construct compile unit.
1885 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001886
Jim Laskeyef42a012006-11-02 20:12:39 +00001887 // Add Unit to compile unit map.
1888 DescToUnitMap[UnitDesc] = Unit;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001889
Jim Laskeyef42a012006-11-02 20:12:39 +00001890 return Unit;
1891 }
1892
Jim Laskey9d4209f2006-11-07 19:33:46 +00001893 /// GetBaseCompileUnit - Get the main compile unit.
1894 ///
1895 CompileUnit *GetBaseCompileUnit() const {
1896 CompileUnit *Unit = CompileUnits[0];
1897 assert(Unit && "Missing compile unit.");
1898 return Unit;
1899 }
1900
Jim Laskey65195462006-10-30 13:35:07 +00001901 /// FindCompileUnit - Get the compile unit for the given descriptor.
1902 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001903 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001904 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00001905 assert(Unit && "Missing compile unit.");
1906 return Unit;
1907 }
1908
1909 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00001910 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001911 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1912 // Get the compile unit context.
1913 CompileUnitDesc *UnitDesc =
1914 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001915 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001916
1917 // Check for pre-existence.
1918 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1919 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001920
Jim Laskeyef42a012006-11-02 20:12:39 +00001921 // Get the global variable itself.
1922 GlobalVariable *GV = GVD->getGlobalVariable();
1923
Jim Laskey2172f962006-11-30 14:35:45 +00001924 const std::string &Name = GVD->getName();
1925 const std::string &FullName = GVD->getFullName();
1926 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00001927 // Create the global's variable DIE.
1928 DIE *VariableDie = new DIE(DW_TAG_variable);
1929 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001930 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001931 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001932 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001933 }
Jim Laskey6488a072007-01-08 22:15:18 +00001934 AddType(VariableDie, GVD->getType(), Unit);
1935 if (!GVD->isStatic())
1936 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001937
Jim Laskeyef42a012006-11-02 20:12:39 +00001938 // Add source line info if available.
1939 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001940
Jim Laskeyef42a012006-11-02 20:12:39 +00001941 // Add address.
1942 DIEBlock *Block = new DIEBlock();
1943 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00001944 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
1945 AddBlock(VariableDie, DW_AT_location, 0, Block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001946
Jim Laskeyef42a012006-11-02 20:12:39 +00001947 // Add to map.
1948 Slot = VariableDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001949
Jim Laskeyef42a012006-11-02 20:12:39 +00001950 // Add to context owner.
1951 Unit->getDie()->AddChild(VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001952
Jim Laskeyef42a012006-11-02 20:12:39 +00001953 // Expose as global.
1954 // FIXME - need to check external flag.
Jim Laskey2172f962006-11-30 14:35:45 +00001955 Unit->AddGlobal(FullName, VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001956
Jim Laskeyef42a012006-11-02 20:12:39 +00001957 return VariableDie;
1958 }
Jim Laskey65195462006-10-30 13:35:07 +00001959
1960 /// NewSubprogram - Add a new subprogram DIE.
1961 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001962 DIE *NewSubprogram(SubprogramDesc *SPD) {
1963 // Get the compile unit context.
1964 CompileUnitDesc *UnitDesc =
1965 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00001966 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00001967
1968 // Check for pre-existence.
1969 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1970 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001971
Jim Laskeyef42a012006-11-02 20:12:39 +00001972 // Gather the details (simplify add attribute code.)
Jim Laskey2172f962006-11-30 14:35:45 +00001973 const std::string &Name = SPD->getName();
1974 const std::string &FullName = SPD->getFullName();
1975 const std::string &LinkageName = SPD->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001976
Jim Laskeyef42a012006-11-02 20:12:39 +00001977 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1978 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001979 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001980 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00001981 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00001982 }
1983 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
Jim Laskey6488a072007-01-08 22:15:18 +00001984 if (!SPD->isStatic())
1985 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00001986 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001987
Jim Laskeyef42a012006-11-02 20:12:39 +00001988 // Add source line info if available.
1989 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1990
1991 // Add to map.
1992 Slot = SubprogramDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001993
Jim Laskeyef42a012006-11-02 20:12:39 +00001994 // Add to context owner.
1995 Unit->getDie()->AddChild(SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001996
Jim Laskeyef42a012006-11-02 20:12:39 +00001997 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00001998 Unit->AddGlobal(FullName, SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001999
Jim Laskeyef42a012006-11-02 20:12:39 +00002000 return SubprogramDie;
2001 }
Jim Laskey65195462006-10-30 13:35:07 +00002002
2003 /// NewScopeVariable - Create a new scope variable.
2004 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002005 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
2006 // Get the descriptor.
2007 VariableDesc *VD = DV->getDesc();
2008
2009 // Translate tag to proper Dwarf tag. The result variable is dropped for
2010 // now.
2011 unsigned Tag;
2012 switch (VD->getTag()) {
2013 case DW_TAG_return_variable: return NULL;
2014 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2015 case DW_TAG_auto_variable: // fall thru
2016 default: Tag = DW_TAG_variable; break;
2017 }
2018
2019 // Define variable debug information entry.
2020 DIE *VariableDie = new DIE(Tag);
2021 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2022
2023 // Add source line info if available.
2024 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002025
Jim Laskeyef42a012006-11-02 20:12:39 +00002026 // Add variable type.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002027 AddType(VariableDie, VD->getType(), Unit);
2028
Jim Laskeyef42a012006-11-02 20:12:39 +00002029 // Add variable address.
2030 MachineLocation Location;
Evan Cheng72bebb92008-01-31 03:37:28 +00002031 Location.set(RI->getFrameRegister(*MF),
2032 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Jim Laskeyef42a012006-11-02 20:12:39 +00002033 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002034
Jim Laskeyef42a012006-11-02 20:12:39 +00002035 return VariableDie;
2036 }
Jim Laskey65195462006-10-30 13:35:07 +00002037
2038 /// ConstructScope - Construct the components of a scope.
2039 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002040 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00002041 unsigned ParentStartID, unsigned ParentEndID,
2042 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002043 // Add variables to scope.
2044 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
2045 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2046 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
2047 if (VariableDie) ParentDie->AddChild(VariableDie);
2048 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002049
Jim Laskeyef42a012006-11-02 20:12:39 +00002050 // Add nested scopes.
2051 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
2052 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2053 // Define the Scope debug information entry.
2054 DebugScope *Scope = Scopes[j];
2055 // FIXME - Ignore inlined functions for the time being.
2056 if (!Scope->getParent()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002057
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002058 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2059 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00002060
Jim Laskey9d4209f2006-11-07 19:33:46 +00002061 // Ignore empty scopes.
2062 if (StartID == EndID && StartID != 0) continue;
2063 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002064
Jim Laskey36729dd2006-11-29 16:55:57 +00002065 if (StartID == ParentStartID && EndID == ParentEndID) {
2066 // Just add stuff to the parent scope.
2067 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002068 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00002069 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002070
Jim Laskey36729dd2006-11-29 16:55:57 +00002071 // Add the scope bounds.
2072 if (StartID) {
2073 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00002074 DWLabel("label", StartID));
Jim Laskey36729dd2006-11-29 16:55:57 +00002075 } else {
2076 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2077 DWLabel("func_begin", SubprogramCount));
2078 }
2079 if (EndID) {
2080 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00002081 DWLabel("label", EndID));
Jim Laskey36729dd2006-11-29 16:55:57 +00002082 } else {
2083 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2084 DWLabel("func_end", SubprogramCount));
2085 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002086
Jim Laskey36729dd2006-11-29 16:55:57 +00002087 // Add the scope contents.
2088 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
2089 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00002090 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002091 }
2092 }
Jim Laskey65195462006-10-30 13:35:07 +00002093
2094 /// ConstructRootScope - Construct the scope for the subprogram.
2095 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002096 void ConstructRootScope(DebugScope *RootScope) {
2097 // Exit if there is no root scope.
2098 if (!RootScope) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002099
2100 // Get the subprogram debug information entry.
Jim Laskeyef42a012006-11-02 20:12:39 +00002101 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002102
Jim Laskeyef42a012006-11-02 20:12:39 +00002103 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00002104 CompileUnit *Unit = GetBaseCompileUnit();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002105
Jim Laskeyef42a012006-11-02 20:12:39 +00002106 // Get the subprogram die.
2107 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2108 assert(SPDie && "Missing subprogram descriptor");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002109
Jim Laskeyef42a012006-11-02 20:12:39 +00002110 // Add the function bounds.
2111 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2112 DWLabel("func_begin", SubprogramCount));
2113 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2114 DWLabel("func_end", SubprogramCount));
2115 MachineLocation Location(RI->getFrameRegister(*MF));
2116 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002117
Jim Laskey36729dd2006-11-29 16:55:57 +00002118 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002119 }
Jim Laskey65195462006-10-30 13:35:07 +00002120
Bill Wendlingd751c642008-09-26 00:28:12 +00002121 /// ConstructDefaultScope - Construct a default scope for the subprogram.
2122 ///
2123 void ConstructDefaultScope(MachineFunction *MF) {
2124 // Find the correct subprogram descriptor.
2125 std::vector<SubprogramDesc *> Subprograms;
2126 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
2127
2128 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2129 SubprogramDesc *SPD = Subprograms[i];
2130
2131 if (SPD->getName() == MF->getFunction()->getName()) {
2132 // Get the compile unit context.
2133 CompileUnit *Unit = GetBaseCompileUnit();
2134
2135 // Get the subprogram die.
2136 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2137 assert(SPDie && "Missing subprogram descriptor");
2138
2139 // Add the function bounds.
2140 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2141 DWLabel("func_begin", SubprogramCount));
2142 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2143 DWLabel("func_end", SubprogramCount));
2144
2145 MachineLocation Location(RI->getFrameRegister(*MF));
2146 AddAddress(SPDie, DW_AT_frame_base, Location);
2147 return;
2148 }
2149 }
Bill Wendlinga9f0cc42008-10-17 18:48:57 +00002150#if 0
2151 // FIXME: This is causing an abort because C++ mangled names are compared
2152 // with their unmangled counterparts. See PR2885. Don't do this assert.
Bill Wendlingd751c642008-09-26 00:28:12 +00002153 assert(0 && "Couldn't find DIE for machine function!");
Bill Wendlinga9f0cc42008-10-17 18:48:57 +00002154#endif
Bill Wendlingd751c642008-09-26 00:28:12 +00002155 }
2156
Jim Laskeyef42a012006-11-02 20:12:39 +00002157 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2158 /// tools to recognize the object file contains Dwarf information.
2159 void EmitInitial() {
2160 // Check to see if we already emitted intial headers.
2161 if (didInitial) return;
2162 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002163
Jim Laskeyef42a012006-11-02 20:12:39 +00002164 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002165 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002166 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002167 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002168 }
2169 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2170 EmitLabel("section_info", 0);
2171 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2172 EmitLabel("section_abbrev", 0);
2173 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2174 EmitLabel("section_aranges", 0);
2175 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2176 EmitLabel("section_macinfo", 0);
2177 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2178 EmitLabel("section_line", 0);
2179 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2180 EmitLabel("section_loc", 0);
2181 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2182 EmitLabel("section_pubnames", 0);
2183 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2184 EmitLabel("section_str", 0);
2185 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2186 EmitLabel("section_ranges", 0);
2187
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002188 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002189 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002190 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002191 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002192 }
2193
Jim Laskey65195462006-10-30 13:35:07 +00002194 /// EmitDIE - Recusively Emits a debug information entry.
2195 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002196 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002197 // Get the abbreviation for this DIE.
2198 unsigned AbbrevNumber = Die->getAbbrevNumber();
2199 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002200
Jim Laskeybacd3042007-02-21 22:48:45 +00002201 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002202
2203 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002204 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002205
2206 if (VerboseAsm)
2207 Asm->EOL(std::string("Abbrev [" +
2208 utostr(AbbrevNumber) +
2209 "] 0x" + utohexstr(Die->getOffset()) +
2210 ":0x" + utohexstr(Die->getSize()) + " " +
2211 TagString(Abbrev->getTag())));
2212 else
2213 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002214
Owen Anderson873e1b52008-06-24 21:44:59 +00002215 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2216 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002217
Jim Laskeyef42a012006-11-02 20:12:39 +00002218 // Emit the DIE attribute values.
2219 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2220 unsigned Attr = AbbrevData[i].getAttribute();
2221 unsigned Form = AbbrevData[i].getForm();
2222 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002223
Jim Laskeyef42a012006-11-02 20:12:39 +00002224 switch (Attr) {
2225 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002226 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002227 break;
2228 }
2229 default: {
2230 // Emit an attribute using the defined form.
2231 Values[i]->EmitValue(*this, Form);
2232 break;
2233 }
2234 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002235
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002236 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002237 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002238
Jim Laskeyef42a012006-11-02 20:12:39 +00002239 // Emit the DIE children if any.
2240 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2241 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002242
Jim Laskeyef42a012006-11-02 20:12:39 +00002243 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2244 EmitDIE(Children[j]);
2245 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002246
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002247 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002248 }
2249 }
2250
Jim Laskey65195462006-10-30 13:35:07 +00002251 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2252 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002253 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2254 // Get the children.
2255 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002256
Jim Laskeyef42a012006-11-02 20:12:39 +00002257 // If not last sibling and has children then add sibling offset attribute.
2258 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2259
2260 // Record the abbreviation.
2261 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002262
Jim Laskeyef42a012006-11-02 20:12:39 +00002263 // Get the abbreviation for this DIE.
2264 unsigned AbbrevNumber = Die->getAbbrevNumber();
2265 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2266
2267 // Set DIE offset
2268 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002269
Jim Laskeyef42a012006-11-02 20:12:39 +00002270 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002271 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2272
Owen Anderson873e1b52008-06-24 21:44:59 +00002273 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2274 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002275
2276 // Size the DIE attribute values.
2277 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2278 // Size attribute value.
2279 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2280 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002281
Jim Laskeyef42a012006-11-02 20:12:39 +00002282 // Size the DIE children if any.
2283 if (!Children.empty()) {
2284 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2285 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002286
Jim Laskeyef42a012006-11-02 20:12:39 +00002287 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2288 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2289 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002290
Jim Laskeyef42a012006-11-02 20:12:39 +00002291 // End of children marker.
2292 Offset += sizeof(int8_t);
2293 }
2294
2295 Die->setSize(Offset - Die->getOffset());
2296 return Offset;
2297 }
Jim Laskey65195462006-10-30 13:35:07 +00002298
2299 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2300 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002301 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002302 // Process base compile unit.
2303 CompileUnit *Unit = GetBaseCompileUnit();
2304 // Compute size of compile unit header
2305 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2306 sizeof(int16_t) + // DWARF version number
2307 sizeof(int32_t) + // Offset Into Abbrev. Section
2308 sizeof(int8_t); // Pointer Size (in bytes)
2309 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002310 }
2311
Jim Laskey65195462006-10-30 13:35:07 +00002312 /// EmitDebugInfo - Emit the debug info section.
2313 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002314 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002315 // Start debug info section.
2316 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002317
Jim Laskey5496f012006-11-09 14:52:14 +00002318 CompileUnit *Unit = GetBaseCompileUnit();
2319 DIE *Die = Unit->getDie();
2320 // Emit the compile units header.
2321 EmitLabel("info_begin", Unit->getID());
2322 // Emit size of content not including length itself
2323 unsigned ContentSize = Die->getSize() +
2324 sizeof(int16_t) + // DWARF version number
2325 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00002326 sizeof(int8_t) + // Pointer Size (in bytes)
2327 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002328
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002329 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2330 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002331 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002332 Asm->EOL("Offset Into Abbrev. Section");
Dan Gohman82482942007-09-27 23:12:31 +00002333 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002334
Jim Laskey5496f012006-11-09 14:52:14 +00002335 EmitDIE(Die);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002336 // FIXME - extra padding for gdb bug.
2337 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2338 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2339 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2340 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Jim Laskey5496f012006-11-09 14:52:14 +00002341 EmitLabel("info_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002342
Jim Laskeybacd3042007-02-21 22:48:45 +00002343 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002344 }
2345
Jim Laskey65195462006-10-30 13:35:07 +00002346 /// EmitAbbreviations - Emit the abbreviation section.
2347 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002348 void EmitAbbreviations() const {
2349 // Check to see if it is worth the effort.
2350 if (!Abbreviations.empty()) {
2351 // Start the debug abbrev section.
2352 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002353
Jim Laskeyef42a012006-11-02 20:12:39 +00002354 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002355
Jim Laskeyef42a012006-11-02 20:12:39 +00002356 // For each abbrevation.
2357 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2358 // Get abbreviation data
2359 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002360
Jim Laskeyef42a012006-11-02 20:12:39 +00002361 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002362 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2363 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002364
Jim Laskeyef42a012006-11-02 20:12:39 +00002365 // Emit the abbreviations data.
2366 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002367
Jim Laskeybacd3042007-02-21 22:48:45 +00002368 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002369 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002370
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002371 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002372 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002373
Jim Laskeyef42a012006-11-02 20:12:39 +00002374 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002375
Jim Laskeybacd3042007-02-21 22:48:45 +00002376 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002377 }
2378 }
2379
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002380 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2381 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002382 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002383 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2384 // Define last address of section.
2385 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2386 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2387 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2388 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2389
2390 // Mark end of matrix.
2391 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2392 Asm->EmitULEB128Bytes(1); Asm->EOL();
2393 Asm->EmitInt8(1); Asm->EOL();
2394 }
2395
Jim Laskey65195462006-10-30 13:35:07 +00002396 /// EmitDebugLines - Emit source line information.
2397 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002398 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002399 // If the target is using .loc/.file, the assembler will be emitting the
2400 // .debug_line table automatically.
2401 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002402 return;
2403
Jim Laskeyef42a012006-11-02 20:12:39 +00002404 // Minimum line delta, thus ranging from -10..(255-10).
2405 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2406 // Maximum line delta, thus ranging from -10..(255-10).
2407 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002408
Jim Laskeyef42a012006-11-02 20:12:39 +00002409 // Start the dwarf line section.
2410 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002411
Jim Laskeyef42a012006-11-02 20:12:39 +00002412 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002413
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002414 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002415 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002416 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002417
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002418 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002419
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002420 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002421 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002422 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002423
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002424 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002425
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002426 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002427
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002428 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002429
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002430 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002431
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002432 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002433
Jim Laskeyef42a012006-11-02 20:12:39 +00002434 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002435 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2436 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2437 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2438 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2439 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2440 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2441 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2442 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2443 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002444
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002445 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Evan Cheng6547e402008-07-01 23:18:29 +00002446 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
Jim Laskeyef42a012006-11-02 20:12:39 +00002447
2448 // Emit directories.
2449 for (unsigned DirectoryID = 1, NDID = Directories.size();
2450 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002451 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002452 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002453 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002454
Jim Laskeyef42a012006-11-02 20:12:39 +00002455 // Emit files.
2456 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2457 SourceID <= NSID; ++SourceID) {
2458 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002459 Asm->EmitString(SourceFile.getName());
2460 Asm->EOL("Source");
2461 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2462 Asm->EOL("Directory #");
2463 Asm->EmitULEB128Bytes(0);
2464 Asm->EOL("Mod date");
2465 Asm->EmitULEB128Bytes(0);
2466 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002467 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002468 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002469
Jim Laskeyef42a012006-11-02 20:12:39 +00002470 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002471
Jim Laskeyef42a012006-11-02 20:12:39 +00002472 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002473 unsigned SecSrcLinesSize = SectionSourceLines.size();
2474
2475 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002476 // Isolate current sections line info.
2477 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002478
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002479 if (VerboseAsm) {
2480 const Section* S = SectionMap[j + 1];
2481 Asm->EOL(std::string("Section ") + S->getName());
2482 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002483 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002484
2485 // Dwarf assumes we start with first line of first source file.
2486 unsigned Source = 1;
2487 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002488
Jim Laskeyef42a012006-11-02 20:12:39 +00002489 // Construct rows of the address, source, line, column matrix.
2490 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2491 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002492 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002493 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002494
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002495 unsigned SourceID = LineInfo.getSourceID();
2496 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2497 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng6547e402008-07-01 23:18:29 +00002498 if (VerboseAsm)
2499 Asm->EOL(Directories[DirectoryID]
2500 + SourceFile.getName()
2501 + ":"
2502 + utostr_32(LineInfo.getLine()));
2503 else
2504 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002505
2506 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002507 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002508 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002509 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002510 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002511
Jim Laskeyef42a012006-11-02 20:12:39 +00002512 // If change of source, then switch to the new source.
2513 if (Source != LineInfo.getSourceID()) {
2514 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002515 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2516 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002517 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002518
Jim Laskeyef42a012006-11-02 20:12:39 +00002519 // If change of line.
2520 if (Line != LineInfo.getLine()) {
2521 // Determine offset.
2522 int Offset = LineInfo.getLine() - Line;
2523 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002524
Jim Laskeyef42a012006-11-02 20:12:39 +00002525 // Update line.
2526 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002527
Jim Laskeyef42a012006-11-02 20:12:39 +00002528 // If delta is small enough and in range...
2529 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2530 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002531 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002532 } else {
2533 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002534 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2535 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2536 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002537 }
2538 } else {
2539 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002540 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002541 }
2542 }
2543
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002544 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002545 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002546
2547 if (SecSrcLinesSize == 0)
2548 // Because we're emitting a debug_line section, we still need a line
2549 // table. The linker and friends expect it to exist. If there's nothing to
2550 // put into it, emit an empty table.
2551 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002552
Jim Laskeyef42a012006-11-02 20:12:39 +00002553 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002554
Jim Laskeybacd3042007-02-21 22:48:45 +00002555 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002556 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002557
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002558 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002559 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002560 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002561 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002562 return;
2563
2564 int stackGrowth =
2565 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2566 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002567 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002568
2569 // Start the dwarf frame section.
2570 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2571
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002572 EmitLabel("debug_frame_common", 0);
2573 EmitDifference("debug_frame_common_end", 0,
2574 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002575 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002576
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002577 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002578 Asm->EmitInt32((int)DW_CIE_ID);
2579 Asm->EOL("CIE Identifier Tag");
2580 Asm->EmitInt8(DW_CIE_VERSION);
2581 Asm->EOL("CIE Version");
2582 Asm->EmitString("");
2583 Asm->EOL("CIE Augmentation");
2584 Asm->EmitULEB128Bytes(1);
2585 Asm->EOL("CIE Code Alignment Factor");
2586 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002587 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002588 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002589 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002590
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002591 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002592 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002593
Dale Johannesenb97aec62007-11-13 19:13:01 +00002594 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002595
Evan Cheng05548eb2008-02-29 19:36:59 +00002596 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002597 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002598
Jim Laskeybacd3042007-02-21 22:48:45 +00002599 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002600 }
2601
Jim Laskey65195462006-10-30 13:35:07 +00002602 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2603 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002604 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002605 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002606 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002607
Jim Laskeyef42a012006-11-02 20:12:39 +00002608 // Start the dwarf frame section.
2609 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002610
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002611 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2612 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002613 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002614
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002615 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002616
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002617 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2618 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002619 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002620
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002621 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002622 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002623 EmitDifference("func_end", DebugFrameInfo.Number,
2624 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002625 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002626
Dale Johannesenb97aec62007-11-13 19:13:01 +00002627 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002628
Evan Cheng05548eb2008-02-29 19:36:59 +00002629 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002630 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002631
Jim Laskeybacd3042007-02-21 22:48:45 +00002632 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002633 }
2634
2635 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002636 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002637 void EmitDebugPubNames() {
2638 // Start the dwarf pubnames section.
2639 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002640
2641 CompileUnit *Unit = GetBaseCompileUnit();
2642
Jim Laskey5496f012006-11-09 14:52:14 +00002643 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002644 "pubnames_begin", Unit->getID(), true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002645 Asm->EOL("Length of Public Names Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002646
Jim Laskey5496f012006-11-09 14:52:14 +00002647 EmitLabel("pubnames_begin", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002648
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002649 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002650
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002651 EmitSectionOffset("info_begin", "section_info",
2652 Unit->getID(), 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002653 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002654
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002655 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002656 Asm->EOL("Compilation Unit Length");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002657
Jim Laskey5496f012006-11-09 14:52:14 +00002658 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002659
Jim Laskey5496f012006-11-09 14:52:14 +00002660 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2661 GE = Globals.end();
2662 GI != GE; ++GI) {
2663 const std::string &Name = GI->first;
2664 DIE * Entity = GI->second;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002665
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002666 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2667 Asm->EmitString(Name); Asm->EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00002668 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002669
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002670 Asm->EmitInt32(0); Asm->EOL("End Mark");
Jim Laskey5496f012006-11-09 14:52:14 +00002671 EmitLabel("pubnames_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002672
Jim Laskeybacd3042007-02-21 22:48:45 +00002673 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002674 }
2675
2676 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002677 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002678 void EmitDebugStr() {
2679 // Check to see if it is worth the effort.
2680 if (!StringPool.empty()) {
2681 // Start the dwarf str section.
2682 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002683
Jim Laskeyef42a012006-11-02 20:12:39 +00002684 // For each of strings in the string pool.
2685 for (unsigned StringID = 1, N = StringPool.size();
2686 StringID <= N; ++StringID) {
2687 // Emit a label for reference from debug information entries.
2688 EmitLabel("string", StringID);
2689 // Emit the string itself.
2690 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002691 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002692 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002693
Jim Laskeybacd3042007-02-21 22:48:45 +00002694 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002695 }
2696 }
2697
2698 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002699 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002700 void EmitDebugLoc() {
2701 // Start the dwarf loc section.
2702 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002703
Jim Laskeybacd3042007-02-21 22:48:45 +00002704 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002705 }
2706
2707 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002708 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002709 void EmitDebugARanges() {
2710 // Start the dwarf aranges section.
2711 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002712
Jim Laskeyef42a012006-11-02 20:12:39 +00002713 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002714#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002715 CompileUnit *Unit = GetBaseCompileUnit();
2716
Jim Laskey5496f012006-11-09 14:52:14 +00002717 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002718 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002719
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002720 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002721
Jim Laskey5496f012006-11-09 14:52:14 +00002722 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002723 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002724
Dan Gohman82482942007-09-27 23:12:31 +00002725 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002726
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002727 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002728
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002729 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2730 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002731
Jim Laskey5496f012006-11-09 14:52:14 +00002732 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002733 EmitReference("text_begin", 0); Asm->EOL("Address");
2734 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002735
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002736 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2737 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002738#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002739
Jim Laskeybacd3042007-02-21 22:48:45 +00002740 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002741 }
2742
2743 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002744 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002745 void EmitDebugRanges() {
2746 // Start the dwarf ranges section.
2747 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002748
Jim Laskeybacd3042007-02-21 22:48:45 +00002749 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002750 }
2751
2752 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002753 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002754 void EmitDebugMacInfo() {
2755 // Start the dwarf macinfo section.
2756 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002757
Jim Laskeybacd3042007-02-21 22:48:45 +00002758 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002759 }
2760
Jim Laskey65195462006-10-30 13:35:07 +00002761 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2762 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00002763 void ConstructCompileUnitDIEs() {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002764 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002765
Jim Laskeyef42a012006-11-02 20:12:39 +00002766 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002767 unsigned ID = MMI->RecordSource(CUW[i]);
Jim Laskey9d4209f2006-11-07 19:33:46 +00002768 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00002769 CompileUnits.push_back(Unit);
2770 }
2771 }
2772
Jim Laskey65195462006-10-30 13:35:07 +00002773 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2774 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00002775 void ConstructGlobalDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00002776 std::vector<GlobalVariableDesc *> GlobalVariables;
2777 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M, GlobalVariables);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002778
Bill Wendling10fff602008-07-03 22:53:42 +00002779 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2780 GlobalVariableDesc *GVD = GlobalVariables[i];
2781 NewGlobalVariable(GVD);
2782 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002783 }
Jim Laskey65195462006-10-30 13:35:07 +00002784
2785 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2786 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00002787 void ConstructSubprogramDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00002788 std::vector<SubprogramDesc *> Subprograms;
2789 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002790
Bill Wendling10fff602008-07-03 22:53:42 +00002791 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2792 SubprogramDesc *SPD = Subprograms[i];
2793 NewSubprogram(SPD);
2794 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002795 }
Jim Laskey65195462006-10-30 13:35:07 +00002796
Jim Laskey65195462006-10-30 13:35:07 +00002797public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002798 //===--------------------------------------------------------------------===//
2799 // Main entry points.
2800 //
Owen Andersoncb371882008-08-21 00:14:44 +00002801 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00002802 : Dwarf(OS, A, T, "dbg")
Jim Laskeyef42a012006-11-02 20:12:39 +00002803 , CompileUnits()
2804 , AbbreviationsSet(InitAbbreviationsSetSize)
2805 , Abbreviations()
2806 , ValuesSet(InitValuesSetSize)
2807 , Values()
2808 , StringPool()
2809 , DescToUnitMap()
2810 , SectionMap()
2811 , SectionSourceLines()
Jim Laskeybacd3042007-02-21 22:48:45 +00002812 , didInitial(false)
2813 , shouldEmit(false)
Jim Laskeyef42a012006-11-02 20:12:39 +00002814 {
2815 }
Jim Laskey072200c2007-01-29 18:51:14 +00002816 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002817 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2818 delete CompileUnits[i];
2819 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2820 delete Values[j];
2821 }
2822
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002823 /// SetModuleInfo - Set machine module information when it's known that pass
2824 /// manager has created it. Set by the target AsmPrinter.
2825 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002826 // Make sure initial declarations are made.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002827 if (!MMI && mmi->hasDebugInfo()) {
2828 MMI = mmi;
Jim Laskeyef42a012006-11-02 20:12:39 +00002829 shouldEmit = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002830
Jim Laskeyef42a012006-11-02 20:12:39 +00002831 // Create all the compile unit DIEs.
2832 ConstructCompileUnitDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002833
Jim Laskeyef42a012006-11-02 20:12:39 +00002834 // Create DIEs for each of the externally visible global variables.
2835 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00002836
Jim Laskeyef42a012006-11-02 20:12:39 +00002837 // Create DIEs for each of the externally visible subprograms.
2838 ConstructSubprogramDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002839
Jim Laskeyef42a012006-11-02 20:12:39 +00002840 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00002841 SectionMap.insert(TAI->getTextSection());
Dan Gohmand57c3882007-10-01 22:40:20 +00002842
2843 // Print out .file directives to specify files for .loc directives. These
2844 // are printed out early so that they precede any .loc directives.
2845 if (TAI->hasDotLocAndDotFile()) {
2846 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
2847 const UniqueVector<std::string> &Directories = MMI->getDirectories();
2848 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
2849 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
2850 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
2851 assert(AppendOk && "Could not append filename to directory!");
Devang Pateld0935c32008-12-23 21:55:38 +00002852 AppendOk = false;
Dan Gohmand57c3882007-10-01 22:40:20 +00002853 Asm->EmitFile(i, FullPath.toString());
2854 Asm->EOL();
2855 }
2856 }
2857
2858 // Emit initial sections
2859 EmitInitial();
Jim Laskeyef42a012006-11-02 20:12:39 +00002860 }
2861 }
2862
Jim Laskey65195462006-10-30 13:35:07 +00002863 /// BeginModule - Emit all Dwarf sections that should come prior to the
2864 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002865 void BeginModule(Module *M) {
2866 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00002867 }
2868
Jim Laskey65195462006-10-30 13:35:07 +00002869 /// EndModule - Emit all Dwarf sections that should come after the content.
2870 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002871 void EndModule() {
2872 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002873
Jim Laskeyef42a012006-11-02 20:12:39 +00002874 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002875 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002876 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002877 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002878 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002879
Jim Laskeyef42a012006-11-02 20:12:39 +00002880 // End text sections.
2881 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002882 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002883 EmitLabel("section_end", i);
2884 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002885
2886 // Emit common frame information.
2887 EmitCommonDebugFrame();
2888
2889 // Emit function debug frame information
2890 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2891 E = DebugFrames.end(); I != E; ++I)
2892 EmitFunctionDebugFrame(*I);
2893
Jim Laskeyef42a012006-11-02 20:12:39 +00002894 // Compute DIE offsets and sizes.
2895 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002896
Jim Laskeyef42a012006-11-02 20:12:39 +00002897 // Emit all the DIEs into a debug info section
2898 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002899
Jim Laskeyef42a012006-11-02 20:12:39 +00002900 // Corresponding abbreviations into a abbrev section.
2901 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002902
Jim Laskeyef42a012006-11-02 20:12:39 +00002903 // Emit source line correspondence into a debug line section.
2904 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002905
Jim Laskeyef42a012006-11-02 20:12:39 +00002906 // Emit info into a debug pubnames section.
2907 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002908
Jim Laskeyef42a012006-11-02 20:12:39 +00002909 // Emit info into a debug str section.
2910 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002911
Jim Laskeyef42a012006-11-02 20:12:39 +00002912 // Emit info into a debug loc section.
2913 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002914
Jim Laskeyef42a012006-11-02 20:12:39 +00002915 // Emit info into a debug aranges section.
2916 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002917
Jim Laskeyef42a012006-11-02 20:12:39 +00002918 // Emit info into a debug ranges section.
2919 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002920
Jim Laskeyef42a012006-11-02 20:12:39 +00002921 // Emit info into a debug macinfo section.
2922 EmitDebugMacInfo();
2923 }
2924
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002925 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00002926 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002927 void BeginFunction(MachineFunction *MF) {
2928 this->MF = MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002929
Jim Laskeyef42a012006-11-02 20:12:39 +00002930 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002931
2932 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002933 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002934
Jim Laskeyef42a012006-11-02 20:12:39 +00002935 // Assumes in correct section after the entry point.
2936 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00002937
2938 // Emit label for the implicitly defined dbg.stoppoint at the start of
2939 // the function.
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00002940 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
2941 if (!LineInfos.empty()) {
2942 const SourceLineInfo &LineInfo = LineInfos[0];
2943 Asm->printLabel(LineInfo.getLabelID());
2944 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002945 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002946
Jim Laskey65195462006-10-30 13:35:07 +00002947 /// EndFunction - Gather and emit post-function debug information.
2948 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00002949 void EndFunction(MachineFunction *MF) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002950 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002951
Jim Laskeyb82313f2007-02-01 16:31:34 +00002952 // Define end label for subprogram.
2953 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002954
Jim Laskeyef42a012006-11-02 20:12:39 +00002955 // Get function line info.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002956 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
Jim Laskeyef42a012006-11-02 20:12:39 +00002957
2958 if (!LineInfos.empty()) {
2959 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002960 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00002961 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2962 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2963 // Append the function info to section info.
2964 SectionLineInfos.insert(SectionLineInfos.end(),
2965 LineInfos.begin(), LineInfos.end());
2966 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002967
Jim Laskeyef42a012006-11-02 20:12:39 +00002968 // Construct scopes for subprogram.
Bill Wendlingd751c642008-09-26 00:28:12 +00002969 if (MMI->getRootScope())
2970 ConstructRootScope(MMI->getRootScope());
2971 else
2972 // FIXME: This is wrong. We are essentially getting past a problem with
2973 // debug information not being able to handle unreachable blocks that have
2974 // debug information in them. In particular, those unreachable blocks that
2975 // have "region end" info in them. That situation results in the "root
2976 // scope" not being created. If that's the case, then emit a "default"
2977 // scope, i.e., one that encompasses the whole function. This isn't
2978 // desirable. And a better way of handling this (and all of the debugging
2979 // information) needs to be explored.
2980 ConstructDefaultScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002981
2982 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2983 MMI->getFrameMoves()));
Jim Laskeyef42a012006-11-02 20:12:39 +00002984 }
Jim Laskey65195462006-10-30 13:35:07 +00002985};
2986
Jim Laskey072200c2007-01-29 18:51:14 +00002987//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002988/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00002989///
2990class DwarfException : public Dwarf {
2991
Jim Laskeyb82313f2007-02-01 16:31:34 +00002992private:
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002993 struct FunctionEHFrameInfo {
2994 std::string FnName;
2995 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00002996 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002997 bool hasCalls;
2998 bool hasLandingPads;
2999 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003000 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003001
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003002 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3003 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003004 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003005 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003006 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003007 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003008 };
3009
3010 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003011
3012 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3013 /// be emitted.
3014 bool shouldEmitTable;
3015
3016 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3017 /// should be emitted.
3018 bool shouldEmitMoves;
3019
3020 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3021 /// should be emitted.
3022 bool shouldEmitTableModule;
3023
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003024 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003025 /// should be emitted.
3026 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003027
Jim Laskey3f09fc22007-02-28 18:38:31 +00003028 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3029 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003030 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003031 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003032 int stackGrowth =
3033 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3034 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003035 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003036
Jim Laskeybacd3042007-02-21 22:48:45 +00003037 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003038 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003039
3040 if (!TAI->doesRequireNonLocalEHFrameLabel())
3041 O << TAI->getEHGlobalPrefix();
3042 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003043 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003044
Jim Laskeybacd3042007-02-21 22:48:45 +00003045 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003046 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003047
Jim Laskeybacd3042007-02-21 22:48:45 +00003048 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003049 EmitDifference("eh_frame_common_end", Index,
3050 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003051 Asm->EOL("Length of Common Information Entry");
3052
Jim Laskeybacd3042007-02-21 22:48:45 +00003053 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003054 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003055 Asm->EmitInt32((int)0);
3056 Asm->EOL("CIE Identifier Tag");
3057 Asm->EmitInt8(DW_CIE_VERSION);
3058 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003059
Jim Laskeybacd3042007-02-21 22:48:45 +00003060 // The personality presence indicates that language specific information
3061 // will show up in the eh frame.
3062 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003063 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003064
Jim Laskeybacd3042007-02-21 22:48:45 +00003065 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003066 Asm->EmitULEB128Bytes(1);
3067 Asm->EOL("CIE Code Alignment Factor");
3068 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003069 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003070 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003071 Asm->EOL("CIE Return Address Column");
3072
Jim Laskeybacd3042007-02-21 22:48:45 +00003073 // If there is a personality, we need to indicate the functions location.
3074 if (Personality) {
3075 Asm->EmitULEB128Bytes(7);
3076 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003077
Duncan Sands671fa972008-05-07 19:11:09 +00003078 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003079 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003080 Asm->EOL("Personality (pcrel sdata4 indirect)");
3081 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003082 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003083 Asm->EOL("Personality (pcrel sdata4)");
3084 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003085
Duncan Sands671fa972008-05-07 19:11:09 +00003086 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003087 O << TAI->getPersonalityPrefix();
3088 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3089 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003090 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3091 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003092 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003093
Duncan Sands671fa972008-05-07 19:11:09 +00003094 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3095 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003096
3097 if (TAI->doesFDEEncodingRequireSData4()) {
3098 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3099 Asm->EOL("FDE Encoding (pcrel sdata4)");
3100 } else {
3101 Asm->EmitInt8(DW_EH_PE_pcrel);
3102 Asm->EOL("FDE Encoding (pcrel)");
3103 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003104 } else {
3105 Asm->EmitULEB128Bytes(1);
3106 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003107
3108 if (TAI->doesFDEEncodingRequireSData4()) {
3109 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3110 Asm->EOL("FDE Encoding (pcrel sdata4)");
3111 } else {
3112 Asm->EmitInt8(DW_EH_PE_pcrel);
3113 Asm->EOL("FDE Encoding (pcrel)");
3114 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003115 }
3116
3117 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003118 std::vector<MachineMove> Moves;
3119 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003120 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003121
Dale Johannesen21d972a2008-04-30 00:43:29 +00003122 // On Darwin the linker honors the alignment of eh_frame, which means it
3123 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3124 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003125 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003126 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003127 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003128
Jim Laskeybacd3042007-02-21 22:48:45 +00003129 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003130 }
Duncan Sands671fa972008-05-07 19:11:09 +00003131
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003132 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003133 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003134 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003135 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3136
Jim Laskeybacd3042007-02-21 22:48:45 +00003137 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3138
3139 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003140 // If the corresponding function is static, this should not be
3141 // externally visible.
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003142 if (linkage != Function::InternalLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003143 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3144 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3145 }
3146
Dale Johannesen038129d2008-01-10 02:03:30 +00003147 // If corresponding function is weak definition, this should be too.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003148 if ((linkage == Function::WeakLinkage ||
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003149 linkage == Function::LinkOnceLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003150 TAI->getWeakDefDirective())
3151 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3152
3153 // If there are no calls then you can't unwind. This may mean we can
3154 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003155 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003156 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003157 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003158 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003159 !UnwindTablesMandatory &&
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003160 ((linkage != Function::WeakLinkage &&
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003161 linkage != Function::LinkOnceLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003162 !TAI->getWeakDefDirective() ||
3163 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003164 {
Bill Wendling6e198962007-09-18 01:47:22 +00003165 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003166 // This name has no connection to the function, so it might get
3167 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003168 // dead-stripping unconditionally.
3169 if (const char *UsedDirective = TAI->getUsedDirective())
3170 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003171 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003172 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003173
Jim Laskeybacd3042007-02-21 22:48:45 +00003174 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003175 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3176 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003177 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003178
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003179 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003180
Bill Wendling722f5f12008-12-24 08:05:17 +00003181 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3182 PrintRelDirective(true, true);
3183 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3184
3185 if (!TAI->isAbsoluteEHSectionOffsets())
3186 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3187 } else {
3188 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3189 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3190 true, true, false);
3191 }
3192
Jim Laskeybacd3042007-02-21 22:48:45 +00003193 Asm->EOL("FDE CIE offset");
3194
Bill Wendling998dee92008-12-29 22:12:11 +00003195 EmitReference("eh_func_begin", EHFrameInfo.Number, true,
3196 TAI->doesRequire32BitFDEReference());
Jim Laskeybacd3042007-02-21 22:48:45 +00003197 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003198 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling998dee92008-12-29 22:12:11 +00003199 "eh_func_begin", EHFrameInfo.Number,
3200 TAI->doesRequire32BitFDEReference());
Jim Laskeybacd3042007-02-21 22:48:45 +00003201 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003202
Jim Laskey3f09fc22007-02-28 18:38:31 +00003203 // If there is a personality and landing pads then point to the language
3204 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003205 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003206 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003207 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003208
3209 if (EHFrameInfo.hasLandingPads)
3210 EmitReference("exception", EHFrameInfo.Number, true, true);
3211 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003212 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003213 Asm->EOL("Language Specific Data Area");
3214 } else {
3215 Asm->EmitULEB128Bytes(0);
3216 Asm->EOL("Augmentation size");
3217 }
Duncan Sands671fa972008-05-07 19:11:09 +00003218
Jim Laskeybacd3042007-02-21 22:48:45 +00003219 // Indicate locations of function specific callee saved registers in
3220 // frame.
Dale Johannesenb97aec62007-11-13 19:13:01 +00003221 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003222
Dale Johannesen21d972a2008-04-30 00:43:29 +00003223 // On Darwin the linker honors the alignment of eh_frame, which means it
3224 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3225 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003226 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003227 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003228 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003229
3230 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003231 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003232 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003233 // that depends on unused functions (calling undefined externals) being
3234 // dead-stripped to link correctly. Yes, there really is.
3235 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3236 if (const char *UsedDirective = TAI->getUsedDirective())
3237 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3238 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003239 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003240
Duncan Sands57810cd2007-09-05 11:27:52 +00003241 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003242 ///
3243 /// The general organization of the table is complex, but the basic concepts
3244 /// are easy. First there is a header which describes the location and
3245 /// organization of the three components that follow.
3246 /// 1. The landing pad site information describes the range of code covered
3247 /// by the try. In our case it's an accumulation of the ranges covered
3248 /// by the invokes in the try. There is also a reference to the landing
3249 /// pad that handles the exception once processed. Finally an index into
3250 /// the actions table.
3251 /// 2. The action table, in our case, is composed of pairs of type ids
3252 /// and next action offset. Starting with the action index from the
3253 /// landing pad site, each type Id is checked for a match to the current
3254 /// exception. If it matches then the exception and type id are passed
3255 /// on to the landing pad. Otherwise the next action is looked up. This
3256 /// chain is terminated with a next action of zero. If no type id is
3257 /// found the the frame is unwound and handling continues.
3258 /// 3. Type id table contains references to all the C++ typeinfo for all
3259 /// catches in the function. This tables is reversed indexed base 1.
3260
Duncan Sandsb32edb42007-06-06 15:37:31 +00003261 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3262 static unsigned SharedTypeIds(const LandingPadInfo *L,
3263 const LandingPadInfo *R) {
3264 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003265 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003266 unsigned MinSize = LSize < RSize ? LSize : RSize;
3267 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003268
Duncan Sandsb32edb42007-06-06 15:37:31 +00003269 for (; Count != MinSize; ++Count)
3270 if (LIds[Count] != RIds[Count])
3271 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003272
Duncan Sandsb32edb42007-06-06 15:37:31 +00003273 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003274 }
3275
Duncan Sandsb32edb42007-06-06 15:37:31 +00003276 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003277 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003278 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003279 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003280 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003281
Duncan Sandsb32edb42007-06-06 15:37:31 +00003282 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003283 if (LIds[i] != RIds[i])
3284 return LIds[i] < RIds[i];
3285
Duncan Sandsb32edb42007-06-06 15:37:31 +00003286 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003287 }
3288
Duncan Sands53c3a332007-05-16 12:12:23 +00003289 struct KeyInfo {
3290 static inline unsigned getEmptyKey() { return -1U; }
3291 static inline unsigned getTombstoneKey() { return -2U; }
3292 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003293 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003294 static bool isPod() { return true; }
3295 };
3296
Duncan Sands57810cd2007-09-05 11:27:52 +00003297 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003298 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003299 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003300 int NextAction;
3301 struct ActionEntry *Previous;
3302 };
3303
Duncan Sands57810cd2007-09-05 11:27:52 +00003304 /// PadRange - Structure holding a try-range and the associated landing pad.
3305 struct PadRange {
3306 // The index of the landing pad.
3307 unsigned PadIndex;
3308 // The index of the begin and end labels in the landing pad's label lists.
3309 unsigned RangeIndex;
3310 };
3311
3312 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3313
3314 /// CallSiteEntry - Structure describing an entry in the call-site table.
3315 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003316 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003317 unsigned BeginLabel; // zero indicates the start of the function.
3318 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003319 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003320 unsigned PadLabel; // zero indicates that there is no landing pad.
3321 unsigned Action;
3322 };
3323
Jim Laskeybacd3042007-02-21 22:48:45 +00003324 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003325 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003326 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003327 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3328 if (PadInfos.empty()) return;
3329
3330 // Sort the landing pads in order of their type ids. This is used to fold
3331 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003332 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003333 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003334 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003335 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003336 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3337
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003338 // Negative type ids index into FilterIds, positive type ids index into
3339 // TypeInfos. The value written for a positive type id is just the type
3340 // id itself. For a negative type id, however, the value written is the
3341 // (negative) byte offset of the corresponding FilterIds entry. The byte
3342 // offset is usually equal to the type id, because the FilterIds entries
3343 // are written using a variable width encoding which outputs one byte per
3344 // entry as long as the value written is not too large, but can differ.
3345 // This kind of complication does not occur for positive type ids because
3346 // type infos are output using a fixed width encoding.
3347 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3348 SmallVector<int, 16> FilterOffsets;
3349 FilterOffsets.reserve(FilterIds.size());
3350 int Offset = -1;
3351 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3352 E = FilterIds.end(); I != E; ++I) {
3353 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003354 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003355 }
3356
Duncan Sands57810cd2007-09-05 11:27:52 +00003357 // Compute the actions table and gather the first action index for each
3358 // landing pad site.
3359 SmallVector<ActionEntry, 32> Actions;
3360 SmallVector<unsigned, 64> FirstActions;
3361 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003362
Duncan Sandsb32edb42007-06-06 15:37:31 +00003363 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003364 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003365 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003366 const LandingPadInfo *LP = LandingPads[i];
3367 const std::vector<int> &TypeIds = LP->TypeIds;
3368 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003369 unsigned SizeSiteActions = 0;
3370
Duncan Sandsb32edb42007-06-06 15:37:31 +00003371 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003372 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003373 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003374
Duncan Sandsb32edb42007-06-06 15:37:31 +00003375 if (NumShared) {
3376 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3377 assert(Actions.size());
3378 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003379 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3380 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003381 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003382 SizeAction -=
3383 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003384 SizeAction += -PrevAction->NextAction;
3385 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003386 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003387 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003388
Duncan Sandsb32edb42007-06-06 15:37:31 +00003389 // Compute the actions.
3390 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3391 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003392 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3393 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003394 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003395
3396 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003397 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003398 SizeSiteActions += SizeAction;
3399
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003400 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00003401 Actions.push_back(Action);
3402
3403 PrevAction = &Actions.back();
3404 }
3405
3406 // Record the first action of the landing pad site.
3407 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3408 } // else identical - re-use previous FirstAction
3409
3410 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003411
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00003412 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00003413 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00003414 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003415
Duncan Sands481dc722007-12-19 07:36:31 +00003416 // Compute the call-site table. The entry for an invoke has a try-range
3417 // containing the call, a non-zero landing pad and an appropriate action.
3418 // The entry for an ordinary call has a try-range containing the call and
3419 // zero for the landing pad and the action. Calls marked 'nounwind' have
3420 // no entry and must not be contained in the try-range of any entry - they
3421 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003422 SmallVector<CallSiteEntry, 64> CallSites;
3423
3424 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00003425 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3426 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3427 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00003428 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3429 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00003430 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003431 unsigned BeginLabel = LandingPad->BeginLabels[j];
3432 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3433 PadRange P = { i, j };
3434 PadMap[BeginLabel] = P;
3435 }
3436 }
3437
Duncan Sands481dc722007-12-19 07:36:31 +00003438 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00003439 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00003440
3441 // Whether there is a potentially throwing instruction (currently this means
3442 // an ordinary call) between the end of the previous try-range and now.
3443 bool SawPotentiallyThrowing = false;
3444
3445 // Whether the last callsite entry was for an invoke.
3446 bool PreviousIsInvoke = false;
3447
Duncan Sands481dc722007-12-19 07:36:31 +00003448 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003449 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3450 I != E; ++I) {
3451 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3452 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00003453 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00003454 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00003455 continue;
3456 }
3457
Chris Lattner9e330492007-12-30 20:50:28 +00003458 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00003459 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00003460
Duncan Sands481dc722007-12-19 07:36:31 +00003461 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00003462 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00003463 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003464
Duncan Sands481dc722007-12-19 07:36:31 +00003465 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00003466 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00003467 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00003468 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00003469 continue;
3470
3471 PadRange P = L->second;
3472 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3473
3474 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3475 "Inconsistent landing pad map!");
3476
3477 // If some instruction between the previous try-range and this one may
3478 // throw, create a call-site entry with no landing pad for the region
3479 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00003480 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003481 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3482 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00003483 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003484 }
3485
3486 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00003487 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00003488
Duncan Sands481dc722007-12-19 07:36:31 +00003489 if (LandingPad->LandingPadLabel) {
3490 // This try-range is for an invoke.
3491 CallSiteEntry Site = {BeginLabel, LastLabel,
3492 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00003493
Duncan Sands481dc722007-12-19 07:36:31 +00003494 // Try to merge with the previous call-site.
3495 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00003496 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00003497 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3498 // Extend the range of the previous entry.
3499 Prev.EndLabel = Site.EndLabel;
3500 continue;
3501 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003502 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003503
Duncan Sands481dc722007-12-19 07:36:31 +00003504 // Otherwise, create a new call-site.
3505 CallSites.push_back(Site);
3506 PreviousIsInvoke = true;
3507 } else {
3508 // Create a gap.
3509 PreviousIsInvoke = false;
3510 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003511 }
3512 }
3513 // If some instruction between the previous try-range and the end of the
3514 // function may throw, create a call-site entry with no landing pad for the
3515 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00003516 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003517 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3518 CallSites.push_back(Site);
3519 }
3520
Jim Laskeybacd3042007-02-21 22:48:45 +00003521 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00003522
3523 // Call sites.
3524 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3525 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3526 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3527 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3528 SiteLengthSize +
3529 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00003530 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003531 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00003532
Duncan Sands671fa972008-05-07 19:11:09 +00003533 // Type infos.
3534 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3535 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003536
3537 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003538 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003539 SizeSites + SizeActions + SizeTypes;
3540
3541 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3542 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003543 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003544 TypeOffset;
3545
3546 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003547
Jim Laskeybacd3042007-02-21 22:48:45 +00003548 // Begin the exception table.
3549 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00003550 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00003551 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003552 for (unsigned i = 0; i != SizeAlign; ++i) {
3553 Asm->EmitInt8(0);
3554 Asm->EOL("Padding");
3555 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003556 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003557
Jim Laskeybacd3042007-02-21 22:48:45 +00003558 // Emit the header.
3559 Asm->EmitInt8(DW_EH_PE_omit);
3560 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3561 Asm->EmitInt8(DW_EH_PE_absptr);
3562 Asm->EOL("TType format (DW_EH_PE_absptr)");
3563 Asm->EmitULEB128Bytes(TypeOffset);
3564 Asm->EOL("TType base offset");
3565 Asm->EmitInt8(DW_EH_PE_udata4);
3566 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3567 Asm->EmitULEB128Bytes(SizeSites);
3568 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003569
Duncan Sands57810cd2007-09-05 11:27:52 +00003570 // Emit the landing pad site information.
3571 for (unsigned i = 0; i < CallSites.size(); ++i) {
3572 CallSiteEntry &S = CallSites[i];
3573 const char *BeginTag;
3574 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00003575
Duncan Sands57810cd2007-09-05 11:27:52 +00003576 if (!S.BeginLabel) {
3577 BeginTag = "eh_func_begin";
3578 BeginNumber = SubprogramCount;
3579 } else {
3580 BeginTag = "label";
3581 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00003582 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003583
Duncan Sands57810cd2007-09-05 11:27:52 +00003584 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003585 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003586 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00003587
Duncan Sands57810cd2007-09-05 11:27:52 +00003588 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00003589 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00003590 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003591 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00003592 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003593 }
3594 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003595
Duncan Sands671fa972008-05-07 19:11:09 +00003596 if (!S.PadLabel)
3597 Asm->EmitInt32(0);
3598 else
Duncan Sands57810cd2007-09-05 11:27:52 +00003599 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003600 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003601 Asm->EOL("Landing pad");
3602
3603 Asm->EmitULEB128Bytes(S.Action);
3604 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003605 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003606
Jim Laskeybacd3042007-02-21 22:48:45 +00003607 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003608 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3609 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00003610
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003611 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003612 Asm->EOL("TypeInfo index");
3613 Asm->EmitSLEB128Bytes(Action.NextAction);
3614 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003615 }
3616
3617 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00003618 for (unsigned M = TypeInfos.size(); M; --M) {
3619 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00003620
3621 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00003622
3623 if (GV)
3624 O << Asm->getGlobalLinkName(GV);
3625 else
3626 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00003627
Jim Laskeybacd3042007-02-21 22:48:45 +00003628 Asm->EOL("TypeInfo");
3629 }
3630
Duncan Sands73ef58a2007-06-02 16:53:42 +00003631 // Emit the filter typeids.
3632 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3633 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00003634 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00003635 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00003636 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003637
Evan Cheng05548eb2008-02-29 19:36:59 +00003638 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003639 }
3640
Jim Laskey072200c2007-01-29 18:51:14 +00003641public:
3642 //===--------------------------------------------------------------------===//
3643 // Main entry points.
3644 //
Owen Andersoncb371882008-08-21 00:14:44 +00003645 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00003646 : Dwarf(OS, A, T, "eh")
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003647 , shouldEmitTable(false)
3648 , shouldEmitMoves(false)
3649 , shouldEmitTableModule(false)
3650 , shouldEmitMovesModule(false)
Jim Laskey072200c2007-01-29 18:51:14 +00003651 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003652
Jim Laskey072200c2007-01-29 18:51:14 +00003653 virtual ~DwarfException() {}
3654
3655 /// SetModuleInfo - Set machine module information when it's known that pass
3656 /// manager has created it. Set by the target AsmPrinter.
3657 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00003658 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00003659 }
3660
3661 /// BeginModule - Emit all exception information that should come prior to the
3662 /// content.
3663 void BeginModule(Module *M) {
3664 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00003665 }
3666
3667 /// EndModule - Emit all exception information that should come after the
3668 /// content.
3669 void EndModule() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003670 if (shouldEmitMovesModule || shouldEmitTableModule) {
3671 const std::vector<Function *> Personalities = MMI->getPersonalities();
3672 for (unsigned i =0; i < Personalities.size(); ++i)
3673 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003674
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003675 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3676 E = EHFrames.end(); I != E; ++I)
3677 EmitEHFrame(*I);
3678 }
Jim Laskey072200c2007-01-29 18:51:14 +00003679 }
3680
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003681 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00003682 /// emitted immediately after the function entry point.
3683 void BeginFunction(MachineFunction *MF) {
3684 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003685 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesene0040622008-04-02 17:04:45 +00003686 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003687
3688 // Map all labels and get rid of any dead landing pads.
3689 MMI->TidyLandingPads();
3690 // If any landing pads survive, we need an EH table.
3691 if (MMI->getLandingPads().size())
3692 shouldEmitTable = true;
3693
3694 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00003695 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003696 shouldEmitMoves = true;
3697
3698 if (shouldEmitMoves || shouldEmitTable)
3699 // Assumes in correct section after the entry point.
3700 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00003701 }
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003702 shouldEmitTableModule |= shouldEmitTable;
3703 shouldEmitMovesModule |= shouldEmitMoves;
Jim Laskey072200c2007-01-29 18:51:14 +00003704 }
3705
3706 /// EndFunction - Gather and emit post-function exception information.
3707 ///
3708 void EndFunction() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003709 if (shouldEmitMoves || shouldEmitTable) {
3710 EmitLabel("eh_func_end", SubprogramCount);
3711 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003712
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003713 // Save EH frame information
3714 EHFrames.
3715 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling6e198962007-09-18 01:47:22 +00003716 SubprogramCount,
3717 MMI->getPersonalityIndex(),
3718 MF->getFrameInfo()->hasCalls(),
3719 !MMI->getLandingPads().empty(),
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003720 MMI->getFrameMoves(),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003721 MF->getFunction()));
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003722 }
Jim Laskey072200c2007-01-29 18:51:14 +00003723 }
3724};
3725
Jim Laskey0d086af2006-02-27 12:43:29 +00003726} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00003727
3728//===----------------------------------------------------------------------===//
3729
Jim Laskeyd18e2892006-01-20 20:34:06 +00003730/// Emit - Print the abbreviation using the specified Dwarf writer.
3731///
Jim Laskey072200c2007-01-29 18:51:14 +00003732void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003733 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00003734 DD.getAsm()->EmitULEB128Bytes(Tag);
3735 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003736
Jim Laskeyd18e2892006-01-20 20:34:06 +00003737 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00003738 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3739 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003740
Jim Laskeyd18e2892006-01-20 20:34:06 +00003741 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00003742 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003743 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003744
Jim Laskeyd18e2892006-01-20 20:34:06 +00003745 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00003746 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3747 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003748
Jim Laskeyd18e2892006-01-20 20:34:06 +00003749 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00003750 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3751 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003752 }
3753
3754 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00003755 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3756 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00003757}
3758
3759#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00003760void DIEAbbrev::print(std::ostream &O) {
3761 O << "Abbreviation @"
3762 << std::hex << (intptr_t)this << std::dec
3763 << " "
3764 << TagString(Tag)
3765 << " "
3766 << ChildrenString(ChildrenFlag)
3767 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003768
Jim Laskeya0f3d172006-09-07 22:06:40 +00003769 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3770 O << " "
3771 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003772 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00003773 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003774 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00003775 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00003776}
Bill Wendlinge8156192006-12-07 01:30:32 +00003777void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00003778#endif
3779
3780//===----------------------------------------------------------------------===//
3781
Jim Laskeyef42a012006-11-02 20:12:39 +00003782#ifndef NDEBUG
3783void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003784 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003785}
Jim Laskeyef42a012006-11-02 20:12:39 +00003786#endif
3787
3788//===----------------------------------------------------------------------===//
3789
Jim Laskey063e7652006-01-17 17:31:53 +00003790/// EmitValue - Emit integer of appropriate size.
3791///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003792void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00003793 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00003794 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00003795 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003796 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003797 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003798 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003799 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003800 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003801 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003802 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
3803 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
3804 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003805 default: assert(0 && "DIE Value form not supported yet"); break;
3806 }
3807}
3808
3809/// SizeOf - Determine size of integer value in bytes.
3810///
Jim Laskey072200c2007-01-29 18:51:14 +00003811unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003812 switch (Form) {
3813 case DW_FORM_flag: // Fall thru
3814 case DW_FORM_ref1: // Fall thru
3815 case DW_FORM_data1: return sizeof(int8_t);
3816 case DW_FORM_ref2: // Fall thru
3817 case DW_FORM_data2: return sizeof(int16_t);
3818 case DW_FORM_ref4: // Fall thru
3819 case DW_FORM_data4: return sizeof(int32_t);
3820 case DW_FORM_ref8: // Fall thru
3821 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003822 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
3823 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00003824 default: assert(0 && "DIE Value form not supported yet"); break;
3825 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003826 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00003827}
3828
Jim Laskey063e7652006-01-17 17:31:53 +00003829//===----------------------------------------------------------------------===//
3830
3831/// EmitValue - Emit string value.
3832///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003833void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003834 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00003835}
3836
Jim Laskey063e7652006-01-17 17:31:53 +00003837//===----------------------------------------------------------------------===//
3838
3839/// EmitValue - Emit label value.
3840///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003841void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003842 bool IsSmall = Form == DW_FORM_data4;
3843 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00003844}
3845
3846/// SizeOf - Determine size of label value in bytes.
3847///
Jim Laskey072200c2007-01-29 18:51:14 +00003848unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003849 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003850 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00003851}
Jim Laskeyef42a012006-11-02 20:12:39 +00003852
Jim Laskey063e7652006-01-17 17:31:53 +00003853//===----------------------------------------------------------------------===//
3854
Jim Laskeyd18e2892006-01-20 20:34:06 +00003855/// EmitValue - Emit label value.
3856///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003857void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003858 bool IsSmall = Form == DW_FORM_data4;
3859 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00003860}
3861
3862/// SizeOf - Determine size of label value in bytes.
3863///
Jim Laskey072200c2007-01-29 18:51:14 +00003864unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003865 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003866 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00003867}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003868
Jim Laskeyd18e2892006-01-20 20:34:06 +00003869//===----------------------------------------------------------------------===//
3870
Jim Laskey063e7652006-01-17 17:31:53 +00003871/// EmitValue - Emit delta value.
3872///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00003873void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
3874 bool IsSmall = Form == DW_FORM_data4;
3875 DD.EmitSectionOffset(Label.Tag, Section.Tag,
3876 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
3877}
3878
3879/// SizeOf - Determine size of delta value in bytes.
3880///
3881unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3882 if (Form == DW_FORM_data4) return 4;
3883 return DD.getTargetData()->getPointerSize();
3884}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003885
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00003886//===----------------------------------------------------------------------===//
3887
3888/// EmitValue - Emit delta value.
3889///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003890void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003891 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00003892 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00003893}
3894
3895/// SizeOf - Determine size of delta value in bytes.
3896///
Jim Laskey072200c2007-01-29 18:51:14 +00003897unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003898 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003899 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00003900}
3901
3902//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003903
Jim Laskeyb8509c52006-03-23 18:07:55 +00003904/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003905///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003906void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003907 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00003908}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003909
Jim Laskeyd18e2892006-01-20 20:34:06 +00003910//===----------------------------------------------------------------------===//
3911
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003912/// ComputeSize - calculate the size of the block.
3913///
Jim Laskey072200c2007-01-29 18:51:14 +00003914unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003915 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00003916 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003917
Jim Laskeyef42a012006-11-02 20:12:39 +00003918 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00003919 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00003920 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003921 }
3922 return Size;
3923}
3924
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003925/// EmitValue - Emit block data.
3926///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003927void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003928 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003929 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
3930 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
3931 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
3932 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003933 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003934 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003935
Owen Anderson873e1b52008-06-24 21:44:59 +00003936 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00003937
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003938 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003939 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00003940 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003941 }
3942}
3943
3944/// SizeOf - Determine size of block data in bytes.
3945///
Jim Laskey072200c2007-01-29 18:51:14 +00003946unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003947 switch (Form) {
3948 case DW_FORM_block1: return Size + sizeof(int8_t);
3949 case DW_FORM_block2: return Size + sizeof(int16_t);
3950 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003951 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003952 default: assert(0 && "Improper form for block"); break;
3953 }
3954 return 0;
3955}
3956
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003957//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00003958/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00003959
3960DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003961 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00003962 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00003963}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003964
Jim Laskeyb8509c52006-03-23 18:07:55 +00003965/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
3966///
3967void DIE::AddSiblingOffset() {
3968 DIEInteger *DI = new DIEInteger(0);
3969 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00003970 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00003971}
3972
Jim Laskeyef42a012006-11-02 20:12:39 +00003973/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00003974///
Jim Laskeyef42a012006-11-02 20:12:39 +00003975void DIE::Profile(FoldingSetNodeID &ID) {
3976 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003977
Jim Laskeyef42a012006-11-02 20:12:39 +00003978 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3979 ID.AddPointer(Children[i]);
3980
3981 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3982 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00003983}
Jim Laskeyef42a012006-11-02 20:12:39 +00003984
3985#ifndef NDEBUG
3986void DIE::print(std::ostream &O, unsigned IncIndent) {
3987 static unsigned IndentCount = 0;
3988 IndentCount += IncIndent;
3989 const std::string Indent(IndentCount, ' ');
3990 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003991
Jim Laskeyef42a012006-11-02 20:12:39 +00003992 if (!isBlock) {
3993 O << Indent
3994 << "Die: "
3995 << "0x" << std::hex << (intptr_t)this << std::dec
3996 << ", Offset: " << Offset
3997 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003998 << "\n";
3999
Jim Laskeyef42a012006-11-02 20:12:39 +00004000 O << Indent
4001 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004002 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004003 << ChildrenString(Abbrev.getChildrenFlag());
4004 } else {
4005 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004006 }
4007 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004008
Owen Anderson873e1b52008-06-24 21:44:59 +00004009 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004010
Jim Laskeyef42a012006-11-02 20:12:39 +00004011 IndentCount += 2;
4012 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4013 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004014
4015 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004016 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004017 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004018 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004019
Jim Laskeyef42a012006-11-02 20:12:39 +00004020 O << " "
4021 << FormEncodingString(Data[i].getForm())
4022 << " ";
4023 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004024 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004025 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004026 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004027
Jim Laskeyef42a012006-11-02 20:12:39 +00004028 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4029 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004030 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004031
Jim Laskeyef42a012006-11-02 20:12:39 +00004032 if (!isBlock) O << "\n";
4033 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004034}
4035
Jim Laskeyef42a012006-11-02 20:12:39 +00004036void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004037 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004038}
Jim Laskeybd761842006-02-27 17:27:12 +00004039#endif
Jim Laskey65195462006-10-30 13:35:07 +00004040
4041//===----------------------------------------------------------------------===//
4042/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004043///
Jim Laskey65195462006-10-30 13:35:07 +00004044
Owen Andersoncb371882008-08-21 00:14:44 +00004045DwarfWriter::DwarfWriter(raw_ostream &OS, AsmPrinter *A,
Jim Laskey65195462006-10-30 13:35:07 +00004046 const TargetAsmInfo *T) {
Jim Laskey072200c2007-01-29 18:51:14 +00004047 DE = new DwarfException(OS, A, T);
4048 DD = new DwarfDebug(OS, A, T);
Jim Laskey65195462006-10-30 13:35:07 +00004049}
4050
4051DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004052 delete DE;
4053 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004054}
4055
Jim Laskey44c3b9f2007-01-26 21:22:28 +00004056/// SetModuleInfo - Set machine module info when it's known that pass manager
4057/// has created it. Set by the target AsmPrinter.
4058void DwarfWriter::SetModuleInfo(MachineModuleInfo *MMI) {
Jim Laskey072200c2007-01-29 18:51:14 +00004059 DD->SetModuleInfo(MMI);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004060 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004061}
4062
4063/// BeginModule - Emit all Dwarf sections that should come prior to the
4064/// content.
4065void DwarfWriter::BeginModule(Module *M) {
Jim Laskey072200c2007-01-29 18:51:14 +00004066 DE->BeginModule(M);
4067 DD->BeginModule(M);
Jim Laskey65195462006-10-30 13:35:07 +00004068}
4069
4070/// EndModule - Emit all Dwarf sections that should come after the content.
4071///
4072void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004073 DE->EndModule();
4074 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004075}
4076
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004077/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004078/// emitted immediately after the function entry point.
4079void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004080 DE->BeginFunction(MF);
4081 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004082}
4083
4084/// EndFunction - Gather and emit post-function debug information.
4085///
Bill Wendlingd751c642008-09-26 00:28:12 +00004086void DwarfWriter::EndFunction(MachineFunction *MF) {
4087 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004088 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004089
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004090 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004091 // Clear function debug information.
4092 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004093}