blob: 16d1531a47f89d3d573f92872e566415d0f9f940 [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"
Devang Pateld1ca9252009-01-05 23:03:32 +000021#include "llvm/DerivedTypes.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//===----------------------------------------------------------------------===//
Devang Pateld1ca9252009-01-05 23:03:32 +000063/// Utility routines.
64///
65/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
66/// specified value in their initializer somewhere.
67static void
68getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
69 // Scan though value users.
70 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
71 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
72 // If the user is a GlobalVariable then add to result.
73 Result.push_back(GV);
74 } else if (Constant *C = dyn_cast<Constant>(*I)) {
75 // If the user is a constant variable then scan its users
76 getGlobalVariablesUsing(C, Result);
77 }
78 }
79}
80
81/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
82/// named GlobalVariable.
83static void
84getGlobalVariablesUsing(Module &M, const std::string &RootName,
85 std::vector<GlobalVariable*> &Result) {
86 std::vector<const Type*> FieldTypes;
87 FieldTypes.push_back(Type::Int32Ty);
88 FieldTypes.push_back(Type::Int32Ty);
89
90 // Get the GlobalVariable root.
91 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
92 StructType::get(FieldTypes));
93
94 // If present and linkonce then scan for users.
95 if (UseRoot && UseRoot->hasLinkOnceLinkage())
96 getGlobalVariablesUsing(UseRoot, Result);
97}
98
99//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000100/// DWLabel - Labels are used to track locations in the assembler file.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000101/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
102/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer181b6c92007-08-05 20:06:04 +0000103/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +0000104class DWLabel {
105public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000106 /// Tag - Label category tag. Should always be a staticly declared C string.
107 ///
108 const char *Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000109
Jim Laskeyef42a012006-11-02 20:12:39 +0000110 /// Number - Value to make label unique.
111 ///
112 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +0000113
114 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000115
Jim Laskeyef42a012006-11-02 20:12:39 +0000116 void Profile(FoldingSetNodeID &ID) const {
117 ID.AddString(std::string(Tag));
118 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +0000119 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000120
Jim Laskeyef42a012006-11-02 20:12:39 +0000121#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000122 void print(std::ostream *O) const {
123 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000124 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000125 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +0000126 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +0000127 if (Number) O << Number;
128 }
129#endif
Jim Laskeybd761842006-02-27 17:27:12 +0000130};
131
132//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000133/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
134/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000135class DIEAbbrevData {
136private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000137 /// Attribute - Dwarf attribute code.
138 ///
139 unsigned Attribute;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000140
Jim Laskeyef42a012006-11-02 20:12:39 +0000141 /// Form - Dwarf form code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000142 ///
143 unsigned Form;
144
Jim Laskey0d086af2006-02-27 12:43:29 +0000145public:
146 DIEAbbrevData(unsigned A, unsigned F)
147 : Attribute(A)
148 , Form(F)
149 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000150
Jim Laskeybd761842006-02-27 17:27:12 +0000151 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000152 unsigned getAttribute() const { return Attribute; }
153 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000154
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000155 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000156 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000157 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000158 ID.AddInteger(Attribute);
159 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000160 }
161};
Jim Laskey063e7652006-01-17 17:31:53 +0000162
Jim Laskey0d086af2006-02-27 12:43:29 +0000163//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000164/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
165/// information object.
166class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000167private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000168 /// Tag - Dwarf tag code.
169 ///
170 unsigned Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000171
Jim Laskeyef42a012006-11-02 20:12:39 +0000172 /// Unique number for node.
173 ///
174 unsigned Number;
175
176 /// ChildrenFlag - Dwarf children flag.
177 ///
178 unsigned ChildrenFlag;
179
180 /// Data - Raw data bytes for abbreviation.
181 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000182 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000183
Jim Laskey0d086af2006-02-27 12:43:29 +0000184public:
Jim Laskey063e7652006-01-17 17:31:53 +0000185
Jim Laskey0d086af2006-02-27 12:43:29 +0000186 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000187 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000188 , ChildrenFlag(C)
189 , Data()
190 {}
191 ~DIEAbbrev() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000192
Jim Laskeybd761842006-02-27 17:27:12 +0000193 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000194 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000195 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000196 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000197 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000198 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000199 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000200 void setNumber(unsigned N) { Number = N; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000201
Jim Laskey0d086af2006-02-27 12:43:29 +0000202 /// AddAttribute - Adds another set of attribute information to the
203 /// abbreviation.
204 void AddAttribute(unsigned Attribute, unsigned Form) {
205 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000206 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000207
Jim Laskeyb8509c52006-03-23 18:07:55 +0000208 /// AddFirstAttribute - Adds a set of attribute information to the front
209 /// of the abbreviation.
210 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
211 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
212 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000213
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000214 /// Profile - Used to gather unique data for the abbreviation folding set.
215 ///
216 void Profile(FoldingSetNodeID &ID) {
217 ID.AddInteger(Tag);
218 ID.AddInteger(ChildrenFlag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000219
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000220 // For each attribute description.
221 for (unsigned i = 0, N = Data.size(); i < N; ++i)
222 Data[i].Profile(ID);
223 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000224
Jim Laskey0d086af2006-02-27 12:43:29 +0000225 /// Emit - Print the abbreviation using the specified Dwarf writer.
226 ///
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000227 void Emit(const DwarfDebug &DD) const;
228
Jim Laskey0d086af2006-02-27 12:43:29 +0000229#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000230 void print(std::ostream *O) {
231 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000232 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000233 void print(std::ostream &O);
234 void dump();
235#endif
236};
Jim Laskey063e7652006-01-17 17:31:53 +0000237
Jim Laskey0d086af2006-02-27 12:43:29 +0000238//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000239/// DIE - A structured debug information entry. Has an abbreviation which
240/// describes it's organization.
241class DIE : public FoldingSetNode {
242protected:
243 /// Abbrev - Buffer for constructing abbreviation.
244 ///
245 DIEAbbrev Abbrev;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000246
Jim Laskeyef42a012006-11-02 20:12:39 +0000247 /// Offset - Offset in debug info section.
248 ///
249 unsigned Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000250
Jim Laskeyef42a012006-11-02 20:12:39 +0000251 /// Size - Size of instance + children.
252 ///
253 unsigned Size;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000254
Jim Laskeyef42a012006-11-02 20:12:39 +0000255 /// Children DIEs.
256 ///
257 std::vector<DIE *> Children;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000258
Jim Laskeyef42a012006-11-02 20:12:39 +0000259 /// Attributes values.
260 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000261 SmallVector<DIEValue*, 32> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000262
Jim Laskeyef42a012006-11-02 20:12:39 +0000263public:
Dan Gohman81975f62007-08-27 14:50:10 +0000264 explicit DIE(unsigned Tag)
Jim Laskeyef42a012006-11-02 20:12:39 +0000265 : Abbrev(Tag, DW_CHILDREN_no)
266 , Offset(0)
267 , Size(0)
268 , Children()
269 , Values()
270 {}
271 virtual ~DIE();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000272
Jim Laskeyef42a012006-11-02 20:12:39 +0000273 // Accessors.
274 DIEAbbrev &getAbbrev() { return Abbrev; }
275 unsigned getAbbrevNumber() const {
276 return Abbrev.getNumber();
277 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000278 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000279 unsigned getOffset() const { return Offset; }
280 unsigned getSize() const { return Size; }
281 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000282 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000283 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
284 void setOffset(unsigned O) { Offset = O; }
285 void setSize(unsigned S) { Size = S; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000286
Jim Laskeyef42a012006-11-02 20:12:39 +0000287 /// AddValue - Add a value and attributes to a DIE.
288 ///
289 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
290 Abbrev.AddAttribute(Attribute, Form);
291 Values.push_back(Value);
292 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000293
Jim Laskeyef42a012006-11-02 20:12:39 +0000294 /// SiblingOffset - Return the offset of the debug information entry's
295 /// sibling.
296 unsigned SiblingOffset() const { return Offset + Size; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000297
Jim Laskeyef42a012006-11-02 20:12:39 +0000298 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
299 ///
300 void AddSiblingOffset();
301
302 /// AddChild - Add a child to the DIE.
303 ///
304 void AddChild(DIE *Child) {
305 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
306 Children.push_back(Child);
307 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000308
Jim Laskeyef42a012006-11-02 20:12:39 +0000309 /// Detach - Detaches objects connected to it after copying.
310 ///
311 void Detach() {
312 Children.clear();
313 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000314
Jim Laskeyef42a012006-11-02 20:12:39 +0000315 /// Profile - Used to gather unique data for the value folding set.
316 ///
317 void Profile(FoldingSetNodeID &ID) ;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000318
Jim Laskeyef42a012006-11-02 20:12:39 +0000319#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000320 void print(std::ostream *O, unsigned IncIndent = 0) {
321 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000322 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000323 void print(std::ostream &O, unsigned IncIndent = 0);
324 void dump();
325#endif
326};
327
328//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000329/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000330///
331class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000332public:
333 enum {
334 isInteger,
335 isString,
336 isLabel,
337 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000338 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000339 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000340 isEntry,
341 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000342 };
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000343
Jim Laskeyef42a012006-11-02 20:12:39 +0000344 /// Type - Type of data stored in the value.
345 ///
346 unsigned Type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000347
Dan Gohman81975f62007-08-27 14:50:10 +0000348 explicit DIEValue(unsigned T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000349 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000350 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000351 virtual ~DIEValue() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000352
Jim Laskeyf6733882006-11-02 21:48:18 +0000353 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000354 unsigned getType() const { return Type; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000355
Jim Laskey0d086af2006-02-27 12:43:29 +0000356 // Implement isa/cast/dyncast.
357 static bool classof(const DIEValue *) { return true; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000358
Jim Laskey0d086af2006-02-27 12:43:29 +0000359 /// EmitValue - Emit value via the Dwarf writer.
360 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000361 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000362
Jim Laskey0d086af2006-02-27 12:43:29 +0000363 /// SizeOf - Return the size of a value in bytes.
364 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000365 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000366
Jim Laskeyef42a012006-11-02 20:12:39 +0000367 /// Profile - Used to gather unique data for the value folding set.
368 ///
369 virtual void Profile(FoldingSetNodeID &ID) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000370
Jim Laskeyef42a012006-11-02 20:12:39 +0000371#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000372 void print(std::ostream *O) {
373 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000374 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000375 virtual void print(std::ostream &O) = 0;
376 void dump();
377#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000378};
Jim Laskey063e7652006-01-17 17:31:53 +0000379
Jim Laskey0d086af2006-02-27 12:43:29 +0000380//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000381/// DWInteger - An integer value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000382///
Jim Laskey0d086af2006-02-27 12:43:29 +0000383class DIEInteger : public DIEValue {
384private:
385 uint64_t Integer;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000386
Jim Laskey0d086af2006-02-27 12:43:29 +0000387public:
Dan Gohman81975f62007-08-27 14:50:10 +0000388 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000389
Jim Laskey0d086af2006-02-27 12:43:29 +0000390 // Implement isa/cast/dyncast.
391 static bool classof(const DIEInteger *) { return true; }
392 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000393
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000394 /// BestForm - Choose the best form for integer.
395 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000396 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
397 if (IsSigned) {
398 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
399 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
400 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
401 } else {
402 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
403 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
404 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
405 }
406 return DW_FORM_data8;
407 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000408
Jim Laskey0d086af2006-02-27 12:43:29 +0000409 /// EmitValue - Emit integer of appropriate size.
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 integer value in bytes.
414 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000415 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000416
Jim Laskeyef42a012006-11-02 20:12:39 +0000417 /// Profile - Used to gather unique data for the value folding set.
418 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000419 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000420 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000421 ID.AddInteger(Integer);
422 }
Jim Laskey5496f012006-11-09 14:52:14 +0000423 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000424
Jim Laskeyef42a012006-11-02 20:12:39 +0000425#ifndef NDEBUG
426 virtual void print(std::ostream &O) {
427 O << "Int: " << (int64_t)Integer
428 << " 0x" << std::hex << Integer << std::dec;
429 }
430#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000431};
Jim Laskey063e7652006-01-17 17:31:53 +0000432
Jim Laskey0d086af2006-02-27 12:43:29 +0000433//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000434/// DIEString - A string value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000435///
Jim Laskeyef42a012006-11-02 20:12:39 +0000436class DIEString : public DIEValue {
437public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000438 const std::string String;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000439
Dan Gohman81975f62007-08-27 14:50:10 +0000440 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000441
Jim Laskey0d086af2006-02-27 12:43:29 +0000442 // Implement isa/cast/dyncast.
443 static bool classof(const DIEString *) { return true; }
444 static bool classof(const DIEValue *S) { return S->Type == isString; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000445
Jim Laskey0d086af2006-02-27 12:43:29 +0000446 /// EmitValue - Emit string value.
447 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000448 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000449
Jim Laskey0d086af2006-02-27 12:43:29 +0000450 /// SizeOf - Determine size of string value in bytes.
451 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000452 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000453 return String.size() + sizeof(char); // sizeof('\0');
454 }
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 std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000459 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000460 ID.AddString(String);
461 }
Jim Laskey5496f012006-11-09 14:52:14 +0000462 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
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 << "Str: \"" << String << "\"";
467 }
468#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000469};
Jim Laskey063e7652006-01-17 17:31:53 +0000470
Jim Laskey0d086af2006-02-27 12:43:29 +0000471//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000472/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000473//
Jim Laskeyef42a012006-11-02 20:12:39 +0000474class DIEDwarfLabel : public DIEValue {
475public:
476
Jim Laskey0d086af2006-02-27 12:43:29 +0000477 const DWLabel Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000478
Dan Gohman81975f62007-08-27 14:50:10 +0000479 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000480
Jim Laskey0d086af2006-02-27 12:43:29 +0000481 // Implement isa/cast/dyncast.
482 static bool classof(const DIEDwarfLabel *) { return true; }
483 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000484
Jim Laskey0d086af2006-02-27 12:43:29 +0000485 /// EmitValue - Emit label value.
486 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000487 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000488
Jim Laskey0d086af2006-02-27 12:43:29 +0000489 /// SizeOf - Determine size of label value in bytes.
490 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000491 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000492
Jim Laskeyef42a012006-11-02 20:12:39 +0000493 /// Profile - Used to gather unique data for the value folding set.
494 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000495 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000496 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000497 Label.Profile(ID);
498 }
Jim Laskey5496f012006-11-09 14:52:14 +0000499 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000500
Jim Laskeyef42a012006-11-02 20:12:39 +0000501#ifndef NDEBUG
502 virtual void print(std::ostream &O) {
503 O << "Lbl: ";
504 Label.print(O);
505 }
506#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000507};
Jim Laskey063e7652006-01-17 17:31:53 +0000508
Jim Laskey063e7652006-01-17 17:31:53 +0000509
Jim Laskey0d086af2006-02-27 12:43:29 +0000510//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000511/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000512//
Jim Laskeyef42a012006-11-02 20:12:39 +0000513class DIEObjectLabel : public DIEValue {
514public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000515 const std::string Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000516
Dan Gohman81975f62007-08-27 14:50:10 +0000517 explicit DIEObjectLabel(const std::string &L)
518 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000519
Jim Laskey0d086af2006-02-27 12:43:29 +0000520 // Implement isa/cast/dyncast.
521 static bool classof(const DIEObjectLabel *) { return true; }
522 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000523
Jim Laskey0d086af2006-02-27 12:43:29 +0000524 /// EmitValue - Emit label value.
525 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000526 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000527
Jim Laskey0d086af2006-02-27 12:43:29 +0000528 /// SizeOf - Determine size of label value in bytes.
529 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000530 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000531
Jim Laskeyef42a012006-11-02 20:12:39 +0000532 /// Profile - Used to gather unique data for the value folding set.
533 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000534 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000535 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000536 ID.AddString(Label);
537 }
Jim Laskey5496f012006-11-09 14:52:14 +0000538 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000539
540#ifndef NDEBUG
541 virtual void print(std::ostream &O) {
542 O << "Obj: " << Label;
543 }
544#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000545};
Jim Laskey063e7652006-01-17 17:31:53 +0000546
Jim Laskey0d086af2006-02-27 12:43:29 +0000547//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000548/// DIESectionOffset - A section offset DIE.
549//
550class DIESectionOffset : public DIEValue {
551public:
552 const DWLabel Label;
553 const DWLabel Section;
554 bool IsEH : 1;
555 bool UseSet : 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000556
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000557 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
558 bool isEH = false, bool useSet = true)
559 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
560 IsEH(isEH), UseSet(useSet) {}
561
562 // Implement isa/cast/dyncast.
563 static bool classof(const DIESectionOffset *) { return true; }
564 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000565
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000566 /// EmitValue - Emit section offset.
567 ///
568 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000569
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000570 /// SizeOf - Determine size of section offset value in bytes.
571 ///
572 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000573
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000574 /// Profile - Used to gather unique data for the value folding set.
575 ///
576 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
577 const DWLabel &Section) {
578 ID.AddInteger(isSectionOffset);
579 Label.Profile(ID);
580 Section.Profile(ID);
581 // IsEH and UseSet are specific to the Label/Section that we will emit
582 // the offset for; so Label/Section are enough for uniqueness.
583 }
584 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
585
586#ifndef NDEBUG
587 virtual void print(std::ostream &O) {
588 O << "Off: ";
589 Label.print(O);
590 O << "-";
591 Section.print(O);
592 O << "-" << IsEH << "-" << UseSet;
593 }
594#endif
595};
596
597//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000598/// DIEDelta - A simple label difference DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000599///
Jim Laskeyef42a012006-11-02 20:12:39 +0000600class DIEDelta : public DIEValue {
601public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000602 const DWLabel LabelHi;
603 const DWLabel LabelLo;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000604
Jim Laskey0d086af2006-02-27 12:43:29 +0000605 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
606 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000607
Jim Laskey0d086af2006-02-27 12:43:29 +0000608 // Implement isa/cast/dyncast.
609 static bool classof(const DIEDelta *) { return true; }
610 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000611
Jim Laskey0d086af2006-02-27 12:43:29 +0000612 /// EmitValue - Emit delta value.
613 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000614 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000615
Jim Laskey0d086af2006-02-27 12:43:29 +0000616 /// SizeOf - Determine size of delta value in bytes.
617 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000618 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000619
Jim Laskeyef42a012006-11-02 20:12:39 +0000620 /// Profile - Used to gather unique data for the value folding set.
621 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000622 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
623 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000624 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000625 LabelHi.Profile(ID);
626 LabelLo.Profile(ID);
627 }
Jim Laskey5496f012006-11-09 14:52:14 +0000628 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000629
630#ifndef NDEBUG
631 virtual void print(std::ostream &O) {
632 O << "Del: ";
633 LabelHi.print(O);
634 O << "-";
635 LabelLo.print(O);
636 }
637#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000638};
Jim Laskey063e7652006-01-17 17:31:53 +0000639
Jim Laskey0d086af2006-02-27 12:43:29 +0000640//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000641/// DIEntry - A pointer to another debug information entry. An instance of this
642/// class can also be used as a proxy for a debug information entry not yet
643/// defined (ie. types.)
644class DIEntry : public DIEValue {
645public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000646 DIE *Entry;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000647
Dan Gohman81975f62007-08-27 14:50:10 +0000648 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000649
Jim Laskey0d086af2006-02-27 12:43:29 +0000650 // Implement isa/cast/dyncast.
651 static bool classof(const DIEntry *) { return true; }
652 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000653
Jim Laskeyb8509c52006-03-23 18:07:55 +0000654 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000655 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000656 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000657
Jim Laskeyb8509c52006-03-23 18:07:55 +0000658 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000659 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000660 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000661 return sizeof(int32_t);
662 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000663
Jim Laskeyef42a012006-11-02 20:12:39 +0000664 /// Profile - Used to gather unique data for the value folding set.
665 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000666 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
667 ID.AddInteger(isEntry);
668 ID.AddPointer(Entry);
669 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000670 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000671 ID.AddInteger(isEntry);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000672
Jim Laskeyef42a012006-11-02 20:12:39 +0000673 if (Entry) {
674 ID.AddPointer(Entry);
675 } else {
676 ID.AddPointer(this);
677 }
678 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000679
Jim Laskeyef42a012006-11-02 20:12:39 +0000680#ifndef NDEBUG
681 virtual void print(std::ostream &O) {
682 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
683 }
684#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000685};
686
687//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000688/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000689//
Jim Laskeyef42a012006-11-02 20:12:39 +0000690class DIEBlock : public DIEValue, public DIE {
691public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000692 unsigned Size; // Size in bytes excluding size header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000693
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000694 DIEBlock()
695 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000696 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000697 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000698 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000699 ~DIEBlock() {
700 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000701
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000702 // Implement isa/cast/dyncast.
703 static bool classof(const DIEBlock *) { return true; }
704 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000705
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000706 /// ComputeSize - calculate the size of the block.
707 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000708 unsigned ComputeSize(DwarfDebug &DD);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000709
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000710 /// BestForm - Choose the best form for data.
711 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000712 unsigned BestForm() const {
713 if ((unsigned char)Size == Size) return DW_FORM_block1;
714 if ((unsigned short)Size == Size) return DW_FORM_block2;
715 if ((unsigned int)Size == Size) return DW_FORM_block4;
716 return DW_FORM_block;
717 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000718
719 /// EmitValue - Emit block data.
720 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000721 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000722
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000723 /// SizeOf - Determine size of block data in bytes.
724 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000725 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000726
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000727
Jim Laskeyef42a012006-11-02 20:12:39 +0000728 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000729 ///
Reid Spencer97821312006-11-02 23:56:21 +0000730 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000731 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000732 DIE::Profile(ID);
733 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000734
Jim Laskeyef42a012006-11-02 20:12:39 +0000735#ifndef NDEBUG
736 virtual void print(std::ostream &O) {
737 O << "Blk: ";
738 DIE::print(O, 5);
739 }
740#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000741};
742
743//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000744/// CompileUnit - This dwarf writer support class manages information associate
745/// with a source file.
746class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000747private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000748 /// Desc - Compile unit debug descriptor.
749 ///
750 CompileUnitDesc *Desc;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000751
Jim Laskeyef42a012006-11-02 20:12:39 +0000752 /// ID - File identifier for source.
753 ///
754 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000755
Jim Laskeyef42a012006-11-02 20:12:39 +0000756 /// Die - Compile unit debug information entry.
757 ///
758 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000759
Jim Laskeyef42a012006-11-02 20:12:39 +0000760 /// DescToDieMap - Tracks the mapping of unit level debug informaton
761 /// descriptors to debug information entries.
762 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
Devang Patel48d190f2009-01-05 21:47:57 +0000763 DenseMap<GlobalVariable *, DIE *> GVToDieMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000764
765 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
766 /// descriptors to debug information entries using a DIEntry proxy.
767 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
Devang Patel48d190f2009-01-05 21:47:57 +0000768 DenseMap<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000769
770 /// Globals - A map of globally visible named entities for this unit.
771 ///
772 std::map<std::string, DIE *> Globals;
773
774 /// DiesSet - Used to uniquely define dies within the compile unit.
775 ///
776 FoldingSet<DIE> DiesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000777
Jim Laskeyef42a012006-11-02 20:12:39 +0000778 /// Dies - List of all dies in the compile unit.
779 ///
780 std::vector<DIE *> Dies;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000781
Jim Laskey0d086af2006-02-27 12:43:29 +0000782public:
Devang Pateld1ca9252009-01-05 23:03:32 +0000783 CompileUnit(unsigned I, DIE *D)
784 : ID(I), Die(D), DescToDieMap(), GVToDieMap(), DescToDIEntryMap(),
785 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize), Dies()
786 {}
787
Jim Laskeyef42a012006-11-02 20:12:39 +0000788 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
789 : Desc(CUD)
790 , ID(I)
791 , Die(D)
792 , DescToDieMap()
Devang Patel48d190f2009-01-05 21:47:57 +0000793 , GVToDieMap()
Jim Laskeyef42a012006-11-02 20:12:39 +0000794 , DescToDIEntryMap()
Devang Patel48d190f2009-01-05 21:47:57 +0000795 , GVToDIEntryMap()
Jim Laskeyef42a012006-11-02 20:12:39 +0000796 , Globals()
797 , DiesSet(InitDiesSetSize)
798 , Dies()
799 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000800
Jim Laskeyef42a012006-11-02 20:12:39 +0000801 ~CompileUnit() {
802 delete Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000803
Jim Laskeyef42a012006-11-02 20:12:39 +0000804 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
805 delete Dies[i];
806 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000807
Jim Laskeybd761842006-02-27 17:27:12 +0000808 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000809 CompileUnitDesc *getDesc() const { return Desc; }
810 unsigned getID() const { return ID; }
811 DIE* getDie() const { return Die; }
812 std::map<std::string, DIE *> &getGlobals() { return Globals; }
813
814 /// hasContent - Return true if this compile unit has something to write out.
815 ///
816 bool hasContent() const {
817 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000818 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000819
820 /// AddGlobal - Add a new global entity to the compile unit.
821 ///
822 void AddGlobal(const std::string &Name, DIE *Die) {
823 Globals[Name] = Die;
824 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000825
Jim Laskeyef42a012006-11-02 20:12:39 +0000826 /// getDieMapSlotFor - Returns the debug information entry map slot for the
827 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000828 DIE *&getDieMapSlotFor(DebugInfoDesc *DID) {
829 return DescToDieMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000830 }
Devang Patel48d190f2009-01-05 21:47:57 +0000831 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
832 return GVToDieMap[GV];
833 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000834
Jim Laskeyef42a012006-11-02 20:12:39 +0000835 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
836 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000837 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DID) {
838 return DescToDIEntryMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000839 }
Devang Patel48d190f2009-01-05 21:47:57 +0000840 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
841 return GVToDIEntryMap[GV];
842 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000843
Jim Laskeyef42a012006-11-02 20:12:39 +0000844 /// AddDie - Adds or interns the DIE to the compile unit.
845 ///
846 DIE *AddDie(DIE &Buffer) {
847 FoldingSetNodeID ID;
848 Buffer.Profile(ID);
849 void *Where;
850 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000851
Jim Laskeyef42a012006-11-02 20:12:39 +0000852 if (!Die) {
853 Die = new DIE(Buffer);
854 DiesSet.InsertNode(Die, Where);
855 this->Die->AddChild(Die);
856 Buffer.Detach();
857 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000858
Jim Laskeyef42a012006-11-02 20:12:39 +0000859 return Die;
860 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000861};
862
Jim Laskey65195462006-10-30 13:35:07 +0000863//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000864/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000865///
Jim Laskey65195462006-10-30 13:35:07 +0000866class Dwarf {
867
Jim Laskey072200c2007-01-29 18:51:14 +0000868protected:
Jim Laskey65195462006-10-30 13:35:07 +0000869
870 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000871 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000872 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000873
Jim Laskey65195462006-10-30 13:35:07 +0000874 //
875 /// O - Stream to .s file.
876 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000877 raw_ostream &O;
Jim Laskey65195462006-10-30 13:35:07 +0000878
879 /// Asm - Target of Dwarf emission.
880 ///
881 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000882
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000883 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000884 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000885
Jim Laskey65195462006-10-30 13:35:07 +0000886 /// TD - Target data.
887 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000888
Jim Laskey65195462006-10-30 13:35:07 +0000889 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000890 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000891
Jim Laskey65195462006-10-30 13:35:07 +0000892 /// M - Current module.
893 ///
894 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000895
Jim Laskey65195462006-10-30 13:35:07 +0000896 /// MF - Current machine function.
897 ///
898 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000899
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000900 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000901 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000902 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000903
Jim Laskey65195462006-10-30 13:35:07 +0000904 /// SubprogramCount - The running count of functions being compiled.
905 ///
906 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000907
Chris Lattner9251f132007-09-24 03:35:37 +0000908 /// Flavor - A unique string indicating what dwarf producer this is, used to
909 /// unique labels.
910 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000911
912 unsigned SetCounter;
Owen Andersoncb371882008-08-21 00:14:44 +0000913 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattner9251f132007-09-24 03:35:37 +0000914 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000915 : O(OS)
916 , Asm(A)
917 , TAI(T)
918 , TD(Asm->TM.getTargetData())
919 , RI(Asm->TM.getRegisterInfo())
920 , M(NULL)
921 , MF(NULL)
922 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000923 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000924 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000925 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000926 {
927 }
928
929public:
930
931 //===--------------------------------------------------------------------===//
932 // Accessors.
933 //
934 AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000935 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000936 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000937 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000938
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000939 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
940 const {
941 if (isInSection && TAI->getDwarfSectionOffsetDirective())
942 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000943 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000944 O << TAI->getData32bitsDirective();
945 else
946 O << TAI->getData64bitsDirective();
947 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000948
Jim Laskeyb82313f2007-02-01 16:31:34 +0000949 /// PrintLabelName - Print label name in form used by Dwarf writer.
950 ///
951 void PrintLabelName(DWLabel Label) const {
952 PrintLabelName(Label.Tag, Label.Number);
953 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000954 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000955 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000956 if (Number) O << Number;
957 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000958
Chris Lattner9251f132007-09-24 03:35:37 +0000959 void PrintLabelName(const char *Tag, unsigned Number,
960 const char *Suffix) const {
961 O << TAI->getPrivateGlobalPrefix() << Tag;
962 if (Number) O << Number;
963 O << Suffix;
964 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000965
Jim Laskeyb82313f2007-02-01 16:31:34 +0000966 /// EmitLabel - Emit location label for internal use by Dwarf.
967 ///
968 void EmitLabel(DWLabel Label) const {
969 EmitLabel(Label.Tag, Label.Number);
970 }
971 void EmitLabel(const char *Tag, unsigned Number) const {
972 PrintLabelName(Tag, Number);
973 O << ":\n";
974 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000975
Jim Laskeyb82313f2007-02-01 16:31:34 +0000976 /// EmitReference - Emit a reference to a label.
977 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000978 void EmitReference(DWLabel Label, bool IsPCRelative = false,
979 bool Force32Bit = false) const {
980 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000981 }
982 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000983 bool IsPCRelative = false, bool Force32Bit = false) const {
984 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000985 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000986
Jim Laskeyb82313f2007-02-01 16:31:34 +0000987 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
988 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000989 void EmitReference(const std::string &Name, bool IsPCRelative = false,
990 bool Force32Bit = false) const {
991 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000992
Jim Laskeyb82313f2007-02-01 16:31:34 +0000993 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000994
Jim Laskeyb82313f2007-02-01 16:31:34 +0000995 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
996 }
997
998 /// EmitDifference - Emit the difference between two labels. Some
999 /// assemblers do not behave with absolute expressions with data directives,
1000 /// so there is an option (needsSet) to use an intermediary set expression.
1001 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +00001002 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001003 EmitDifference(LabelHi.Tag, LabelHi.Number,
1004 LabelLo.Tag, LabelLo.Number,
1005 IsSmall);
1006 }
1007 void EmitDifference(const char *TagHi, unsigned NumberHi,
1008 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +00001009 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001010 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001011 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +00001012 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001013 O << ",";
1014 PrintLabelName(TagHi, NumberHi);
1015 O << "-";
1016 PrintLabelName(TagLo, NumberLo);
1017 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001018
1019 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +00001020 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001021 ++SetCounter;
1022 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001023 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001024
Jim Laskeyb82313f2007-02-01 16:31:34 +00001025 PrintLabelName(TagHi, NumberHi);
1026 O << "-";
1027 PrintLabelName(TagLo, NumberLo);
1028 }
1029 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001030
1031 void EmitSectionOffset(const char* Label, const char* Section,
1032 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +00001033 bool IsSmall = false, bool isEH = false,
1034 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001035 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +00001036 if (isEH)
1037 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
1038 else
1039 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
1040
1041 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001042 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +00001043 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001044 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001045 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001046
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001047 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001048 O << "-";
1049 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001050 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001051 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001052
1053 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001054
Chris Lattner9251f132007-09-24 03:35:37 +00001055 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001056 ++SetCounter;
1057 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001058 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001059
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001060 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001061
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001062 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001063 O << "-";
1064 PrintLabelName(Section, SectionNumber);
1065 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001066 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001067 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001068
Jim Laskeyb82313f2007-02-01 16:31:34 +00001069 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1070 /// frame.
1071 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +00001072 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001073 int stackGrowth =
1074 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1075 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001076 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +00001077 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +00001078
1079 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001080 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +00001081 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001082
Jim Laskeyb82313f2007-02-01 16:31:34 +00001083 if (LabelID) {
1084 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001085
Jim Laskeyb82313f2007-02-01 16:31:34 +00001086 // Throw out move if the label is invalid.
1087 if (!LabelID) continue;
1088 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001089
Jim Laskeyb82313f2007-02-01 16:31:34 +00001090 const MachineLocation &Dst = Move.getDestination();
1091 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001092
Jim Laskeyb82313f2007-02-01 16:31:34 +00001093 // Advance row if new location.
1094 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1095 Asm->EmitInt8(DW_CFA_advance_loc4);
1096 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001097 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1098 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001099
Jim Laskeyb82313f2007-02-01 16:31:34 +00001100 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001101 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001102 IsLocal = true;
1103 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001104
Jim Laskeyb82313f2007-02-01 16:31:34 +00001105 // If advancing cfa.
Dan Gohmand735b802008-10-03 15:45:36 +00001106 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1107 if (!Src.isReg()) {
1108 if (Src.getReg() == MachineLocation::VirtualFP) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001109 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1110 Asm->EOL("DW_CFA_def_cfa_offset");
1111 } else {
1112 Asm->EmitInt8(DW_CFA_def_cfa);
1113 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmand735b802008-10-03 15:45:36 +00001114 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001115 Asm->EOL("Register");
1116 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001117
Jim Laskeyb82313f2007-02-01 16:31:34 +00001118 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001119
Jim Laskeyb82313f2007-02-01 16:31:34 +00001120 Asm->EmitULEB128Bytes(Offset);
1121 Asm->EOL("Offset");
1122 } else {
1123 assert(0 && "Machine move no supported yet.");
1124 }
Dan Gohmand735b802008-10-03 15:45:36 +00001125 } else if (Src.isReg() &&
1126 Src.getReg() == MachineLocation::VirtualFP) {
1127 if (Dst.isReg()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001128 Asm->EmitInt8(DW_CFA_def_cfa_register);
1129 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmand735b802008-10-03 15:45:36 +00001130 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001131 Asm->EOL("Register");
1132 } else {
1133 assert(0 && "Machine move no supported yet.");
1134 }
1135 } else {
Dan Gohmand735b802008-10-03 15:45:36 +00001136 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001137 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001138
Jim Laskeyb82313f2007-02-01 16:31:34 +00001139 if (Offset < 0) {
1140 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1141 Asm->EOL("DW_CFA_offset_extended_sf");
1142 Asm->EmitULEB128Bytes(Reg);
1143 Asm->EOL("Reg");
1144 Asm->EmitSLEB128Bytes(Offset);
1145 Asm->EOL("Offset");
1146 } else if (Reg < 64) {
1147 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Chengd4114192008-07-09 21:53:02 +00001148 if (VerboseAsm)
1149 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1150 else
1151 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001152 Asm->EmitULEB128Bytes(Offset);
1153 Asm->EOL("Offset");
1154 } else {
1155 Asm->EmitInt8(DW_CFA_offset_extended);
1156 Asm->EOL("DW_CFA_offset_extended");
1157 Asm->EmitULEB128Bytes(Reg);
1158 Asm->EOL("Reg");
1159 Asm->EmitULEB128Bytes(Offset);
1160 Asm->EOL("Offset");
1161 }
1162 }
1163 }
1164 }
1165
Jim Laskey072200c2007-01-29 18:51:14 +00001166};
1167
1168//===----------------------------------------------------------------------===//
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001169/// SourceLineInfo - This class is used to record source line correspondence.
1170///
1171class SrcLineInfo {
1172 unsigned Line; // Source line number.
1173 unsigned Column; // Source column.
1174 unsigned SourceID; // Source ID number.
1175 unsigned LabelID; // Label in code ID number.
1176public:
1177 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
1178 : Line(L), Column(C), SourceID(S), LabelID(I) {}
1179
1180 // Accessors
1181 unsigned getLine() const { return Line; }
1182 unsigned getColumn() const { return Column; }
1183 unsigned getSourceID() const { return SourceID; }
1184 unsigned getLabelID() const { return LabelID; }
1185};
1186
1187
1188//===----------------------------------------------------------------------===//
Devang Patel8526cc02009-01-05 22:35:52 +00001189/// SrcFileInfo - This class is used to track source information.
1190///
1191class SrcFileInfo {
1192 unsigned DirectoryID; // Directory ID number.
1193 std::string Name; // File name (not including directory.)
1194public:
1195 SrcFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
1196
1197 // Accessors
1198 unsigned getDirectoryID() const { return DirectoryID; }
1199 const std::string &getName() const { return Name; }
1200
1201 /// operator== - Used by UniqueVector to locate entry.
1202 ///
1203 bool operator==(const SourceFileInfo &SI) const {
1204 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
1205 }
1206
1207 /// operator< - Used by UniqueVector to locate entry.
1208 ///
1209 bool operator<(const SrcFileInfo &SI) const {
1210 return getDirectoryID() < SI.getDirectoryID() ||
1211 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
1212 }
1213};
1214
1215//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +00001216/// DbgVariable - This class is used to track local variable information.
1217///
1218class DbgVariable {
1219private:
1220 DIVariable *Var; // Variable Descriptor.
1221 unsigned FrameIndex; // Variable frame index.
1222
1223public:
1224 DbgVariable(DIVariable *V, unsigned I) : Var(V), FrameIndex(I) {}
1225
1226 // Accessors.
1227 DIVariable *getVariable() const { return Var; }
1228 unsigned getFrameIndex() const { return FrameIndex; }
1229};
1230
1231//===----------------------------------------------------------------------===//
1232/// DbgScope - This class is used to track scope information.
1233///
1234class DbgScope {
1235private:
1236 DbgScope *Parent; // Parent to this scope.
1237 DIDescriptor *Desc; // Debug info descriptor for scope.
1238 // Either subprogram or block.
1239 unsigned StartLabelID; // Label ID of the beginning of scope.
1240 unsigned EndLabelID; // Label ID of the end of scope.
1241 SmallVector<DbgScope *, 8> Scopes; // Scopes defined in scope.
1242 SmallVector<DbgVariable *, 32> Variables;// Variables declared in scope.
1243
1244public:
1245 DbgScope(DbgScope *P, DIDescriptor *D)
1246 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1247 {}
1248 ~DbgScope();
1249
1250 // Accessors.
1251 DbgScope *getParent() const { return Parent; }
1252 DIDescriptor *getDesc() const { return Desc; }
1253 unsigned getStartLabelID() const { return StartLabelID; }
1254 unsigned getEndLabelID() const { return EndLabelID; }
1255 SmallVector<DbgScope *, 8> &getScopes() { return Scopes; }
1256 SmallVector<DbgVariable *, 32> &getVariables() { return Variables; }
1257 void setStartLabelID(unsigned S) { StartLabelID = S; }
1258 void setEndLabelID(unsigned E) { EndLabelID = E; }
1259
1260 /// AddScope - Add a scope to the scope.
1261 ///
1262 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1263
1264 /// AddVariable - Add a variable to the scope.
1265 ///
1266 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1267};
1268
1269//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001270/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001271///
1272class DwarfDebug : public Dwarf {
1273
1274private:
Jim Laskey65195462006-10-30 13:35:07 +00001275 //===--------------------------------------------------------------------===//
1276 // Attributes used to construct specific Dwarf sections.
1277 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001278
Jim Laskey65195462006-10-30 13:35:07 +00001279 /// CompileUnits - All the compile units involved in this build. The index
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001280 /// of each entry in this vector corresponds to the sources in MMI.
Jim Laskey65195462006-10-30 13:35:07 +00001281 std::vector<CompileUnit *> CompileUnits;
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001282 DenseMap<Value *, CompileUnit *> DW_CUs;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001283
Jim Laskeyef42a012006-11-02 20:12:39 +00001284 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001285 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001286 FoldingSet<DIEAbbrev> AbbreviationsSet;
1287
1288 /// Abbreviations - A list of all the unique abbreviations in use.
1289 ///
1290 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001291
Jim Laskeyef42a012006-11-02 20:12:39 +00001292 /// ValuesSet - Used to uniquely define values.
1293 ///
Devang Patel8526cc02009-01-05 22:35:52 +00001294 // Directories - Uniquing vector for directories.
1295 UniqueVector<std::string> Directories;
1296
1297 // SourceFiles - Uniquing vector for source files.
1298 UniqueVector<SrcFileInfo> SrcFiles;
1299
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001300 // Lines - List of of source line correspondence.
1301 std::vector<SrcLineInfo> Lines;
1302
Jim Laskeyef42a012006-11-02 20:12:39 +00001303 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001304
Jim Laskeyef42a012006-11-02 20:12:39 +00001305 /// Values - A list of all the unique values in use.
1306 ///
1307 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001308
Jim Laskey65195462006-10-30 13:35:07 +00001309 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001310 ///
Jim Laskey65195462006-10-30 13:35:07 +00001311 UniqueVector<std::string> StringPool;
1312
1313 /// UnitMap - Map debug information descriptor to compile unit.
1314 ///
1315 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001316
Jim Laskey65195462006-10-30 13:35:07 +00001317 /// SectionMap - Provides a unique id per text section.
1318 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001319 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001320
Jim Laskey65195462006-10-30 13:35:07 +00001321 /// SectionSourceLines - Tracks line numbers per text section.
1322 ///
1323 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1324
Jim Laskeybacd3042007-02-21 22:48:45 +00001325 /// didInitial - Flag to indicate if initial emission has been done.
1326 ///
1327 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001328
Jim Laskeybacd3042007-02-21 22:48:45 +00001329 /// shouldEmit - Flag to indicate if debug information should be emitted.
1330 ///
1331 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001332
Devang Patel7a6e5a32009-01-08 02:33:41 +00001333 // RootScope - Top level scope for the current function.
1334 //
1335 DbgScope *RootDbgScope;
1336
1337 // DbgScopeMap - Tracks the scopes in the current function.
1338 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
1339
1340 // DbgLabelIDList - One entry per assigned label. Normally the entry is equal to
1341 // the list index(+1). If the entry is zero then the label has been deleted.
1342 // Any other value indicates the label has been deleted by is mapped to
1343 // another label.
1344 SmallVector<unsigned, 32> DbgLabelIDList;
1345
1346 /// NextLabelID - Return the next unique label id.
1347 ///
1348 unsigned NextLabelID() {
1349 unsigned ID = (unsigned)DbgLabelIDList.size() + 1;
1350 DbgLabelIDList.push_back(ID);
1351 return ID;
1352 }
1353
1354 /// RemapLabel - Indicate that a label has been merged into another.
1355 ///
1356 void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) {
1357 assert(0 < OldLabelID && OldLabelID <= DbgLabelIDList.size() &&
1358 "Old label ID out of range.");
1359 assert(NewLabelID <= DbgLabelIDList.size() &&
1360 "New label ID out of range.");
1361 DbgLabelIDList[OldLabelID - 1] = NewLabelID;
1362 }
1363
1364 /// MappedLabel - Find out the label's final ID. Zero indicates deletion.
1365 /// ID != Mapped ID indicates that the label was folded into another label.
1366 unsigned MappedLabel(unsigned LabelID) const {
1367 assert(LabelID <= DbgLabelIDList.size() && "Debug label ID out of range.");
1368 return LabelID ? DbgLabelIDList[LabelID - 1] : 0;
1369 }
1370
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001371 struct FunctionDebugFrameInfo {
1372 unsigned Number;
1373 std::vector<MachineMove> Moves;
1374
1375 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001376 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001377 };
1378
1379 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001380
Jim Laskey65195462006-10-30 13:35:07 +00001381public:
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001382
Jim Laskeybacd3042007-02-21 22:48:45 +00001383 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1384 ///
1385 bool ShouldEmitDwarf() const { return shouldEmit; }
Jim Laskey65195462006-10-30 13:35:07 +00001386
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001387 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001388 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001389 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1390 // Profile the node so that we can make it unique.
1391 FoldingSetNodeID ID;
1392 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001393
Jim Laskeyef42a012006-11-02 20:12:39 +00001394 // Check the set for priors.
1395 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001396
Jim Laskeyef42a012006-11-02 20:12:39 +00001397 // If it's newly added.
1398 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001399 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001400 Abbreviations.push_back(&Abbrev);
1401 // Assign the vector position + 1 as its number.
1402 Abbrev.setNumber(Abbreviations.size());
1403 } else {
1404 // Assign existing abbreviation number.
1405 Abbrev.setNumber(InSet->getNumber());
1406 }
1407 }
1408
Jim Laskey65195462006-10-30 13:35:07 +00001409 /// NewString - Add a string to the constant pool and returns a label.
1410 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001411 DWLabel NewString(const std::string &String) {
1412 unsigned StringID = StringPool.insert(String);
1413 return DWLabel("string", StringID);
1414 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001415
Jim Laskeyef42a012006-11-02 20:12:39 +00001416 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1417 /// entry.
1418 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1419 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001420
Jim Laskeyef42a012006-11-02 20:12:39 +00001421 if (Entry) {
1422 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001423 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001424 void *Where;
1425 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001426
Jim Laskeyf6733882006-11-02 21:48:18 +00001427 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001428
Jim Laskeyef42a012006-11-02 20:12:39 +00001429 Value = new DIEntry(Entry);
1430 ValuesSet.InsertNode(Value, Where);
1431 } else {
1432 Value = new DIEntry(Entry);
1433 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001434
Jim Laskeyef42a012006-11-02 20:12:39 +00001435 Values.push_back(Value);
1436 return Value;
1437 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001438
Jim Laskeyef42a012006-11-02 20:12:39 +00001439 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1440 ///
1441 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1442 Value->Entry = Entry;
1443 // Add to values set if not already there. If it is, we merely have a
1444 // duplicate in the values list (no harm.)
1445 ValuesSet.GetOrInsertNode(Value);
1446 }
1447
1448 /// AddUInt - Add an unsigned integer attribute data and value.
1449 ///
1450 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1451 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1452
1453 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001454 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001455 void *Where;
1456 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1457 if (!Value) {
1458 Value = new DIEInteger(Integer);
1459 ValuesSet.InsertNode(Value, Where);
1460 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001461 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001462
Jim Laskeyef42a012006-11-02 20:12:39 +00001463 Die->AddValue(Attribute, Form, Value);
1464 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001465
Jim Laskeyef42a012006-11-02 20:12:39 +00001466 /// AddSInt - Add an signed integer attribute data and value.
1467 ///
1468 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1469 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1470
1471 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001472 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001473 void *Where;
1474 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1475 if (!Value) {
1476 Value = new DIEInteger(Integer);
1477 ValuesSet.InsertNode(Value, Where);
1478 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001479 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001480
Jim Laskeyef42a012006-11-02 20:12:39 +00001481 Die->AddValue(Attribute, Form, Value);
1482 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001483
Jim Laskeyef42a012006-11-02 20:12:39 +00001484 /// AddString - Add a std::string attribute data and value.
1485 ///
1486 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1487 const std::string &String) {
1488 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001489 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001490 void *Where;
1491 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1492 if (!Value) {
1493 Value = new DIEString(String);
1494 ValuesSet.InsertNode(Value, Where);
1495 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001496 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001497
Jim Laskeyef42a012006-11-02 20:12:39 +00001498 Die->AddValue(Attribute, Form, Value);
1499 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001500
Jim Laskeyef42a012006-11-02 20:12:39 +00001501 /// AddLabel - Add a Dwarf label attribute data and value.
1502 ///
1503 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1504 const DWLabel &Label) {
1505 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001506 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001507 void *Where;
1508 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1509 if (!Value) {
1510 Value = new DIEDwarfLabel(Label);
1511 ValuesSet.InsertNode(Value, Where);
1512 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001513 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001514
Jim Laskeyef42a012006-11-02 20:12:39 +00001515 Die->AddValue(Attribute, Form, Value);
1516 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001517
Jim Laskeyef42a012006-11-02 20:12:39 +00001518 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1519 ///
1520 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1521 const std::string &Label) {
1522 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001523 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001524 void *Where;
1525 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1526 if (!Value) {
1527 Value = new DIEObjectLabel(Label);
1528 ValuesSet.InsertNode(Value, Where);
1529 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001530 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001531
Jim Laskeyef42a012006-11-02 20:12:39 +00001532 Die->AddValue(Attribute, Form, Value);
1533 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001534
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001535 /// AddSectionOffset - Add a section offset label attribute data and value.
1536 ///
1537 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1538 const DWLabel &Label, const DWLabel &Section,
1539 bool isEH = false, bool useSet = true) {
1540 FoldingSetNodeID ID;
1541 DIESectionOffset::Profile(ID, Label, Section);
1542 void *Where;
1543 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1544 if (!Value) {
1545 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1546 ValuesSet.InsertNode(Value, Where);
1547 Values.push_back(Value);
1548 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001549
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001550 Die->AddValue(Attribute, Form, Value);
1551 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001552
Jim Laskeyef42a012006-11-02 20:12:39 +00001553 /// AddDelta - Add a label delta attribute data and value.
1554 ///
1555 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1556 const DWLabel &Hi, const DWLabel &Lo) {
1557 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001558 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001559 void *Where;
1560 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1561 if (!Value) {
1562 Value = new DIEDelta(Hi, Lo);
1563 ValuesSet.InsertNode(Value, Where);
1564 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001565 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001566
Jim Laskeyef42a012006-11-02 20:12:39 +00001567 Die->AddValue(Attribute, Form, Value);
1568 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001569
Jim Laskeyef42a012006-11-02 20:12:39 +00001570 /// AddDIEntry - Add a DIE attribute data and value.
1571 ///
1572 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1573 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1574 }
1575
1576 /// AddBlock - Add block data.
1577 ///
1578 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1579 Block->ComputeSize(*this);
1580 FoldingSetNodeID ID;
1581 Block->Profile(ID);
1582 void *Where;
1583 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1584 if (!Value) {
1585 Value = Block;
1586 ValuesSet.InsertNode(Value, Where);
1587 Values.push_back(Value);
1588 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001589 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001590 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001591 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001592 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001593
Jim Laskeyef42a012006-11-02 20:12:39 +00001594 Die->AddValue(Attribute, Block->BestForm(), Value);
1595 }
1596
Jim Laskey65195462006-10-30 13:35:07 +00001597private:
1598
1599 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001600 /// entry.
1601 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1602 if (File && Line) {
1603 CompileUnit *FileUnit = FindCompileUnit(File);
1604 unsigned FileID = FileUnit->getID();
1605 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1606 AddUInt(Die, DW_AT_decl_line, 0, Line);
1607 }
1608 }
Jim Laskey65195462006-10-30 13:35:07 +00001609
Devang Patel8526cc02009-01-05 22:35:52 +00001610 /// AddSourceLine - Add location information to specified debug information
1611 /// entry.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001612 void AddSourceLine(DIE *Die, DIVariable *V) {
1613 unsigned FileID = 0;
1614 unsigned Line = V->getLineNumber();
1615 if (V->getVersion() < DIDescriptor::Version7) {
1616 // Version6 or earlier. Use compile unit info to get file id.
1617 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1618 FileID = Unit->getID();
1619 } else {
1620 // Version7 or newer, use filename and directory info from DIVariable
1621 // directly.
1622 unsigned DID = Directories.idFor(V->getDirectory());
1623 FileID = SrcFiles.idFor(SrcFileInfo(DID, V->getFilename()));
1624 }
1625 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1626 AddUInt(Die, DW_AT_decl_line, 0, Line);
1627 }
1628
1629 /// AddSourceLine - Add location information to specified debug information
1630 /// entry.
Devang Patel8526cc02009-01-05 22:35:52 +00001631 void AddSourceLine(DIE *Die, DIGlobal *G) {
1632 unsigned FileID = 0;
1633 unsigned Line = G->getLineNumber();
1634 if (G->getVersion() < DIDescriptor::Version7) {
1635 // Version6 or earlier. Use compile unit info to get file id.
1636 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1637 FileID = Unit->getID();
1638 } else {
1639 // Version7 or newer, use filename and directory info from DIGlobal
1640 // directly.
1641 unsigned DID = Directories.idFor(G->getDirectory());
1642 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1643 }
1644 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1645 AddUInt(Die, DW_AT_decl_line, 0, Line);
1646 }
1647
1648 void AddSourceLine(DIE *Die, DIType *G) {
1649 unsigned FileID = 0;
1650 unsigned Line = G->getLineNumber();
1651 if (G->getVersion() < DIDescriptor::Version7) {
1652 // Version6 or earlier. Use compile unit info to get file id.
1653 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1654 FileID = Unit->getID();
1655 } else {
1656 // Version7 or newer, use filename and directory info from DIGlobal
1657 // directly.
1658 unsigned DID = Directories.idFor(G->getDirectory());
1659 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1660 }
1661 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1662 AddUInt(Die, DW_AT_decl_line, 0, Line);
1663 }
1664
Jim Laskey65195462006-10-30 13:35:07 +00001665 /// AddAddress - Add an address attribute to a die based on the location
1666 /// provided.
1667 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001668 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001669 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001670 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001671
Dan Gohmand735b802008-10-03 15:45:36 +00001672 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001673 if (Reg < 32) {
1674 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1675 } else {
1676 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1677 AddUInt(Block, 0, DW_FORM_udata, Reg);
1678 }
1679 } else {
1680 if (Reg < 32) {
1681 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1682 } else {
1683 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1684 AddUInt(Block, 0, DW_FORM_udata, Reg);
1685 }
1686 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1687 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001688
Jim Laskeyef42a012006-11-02 20:12:39 +00001689 AddBlock(Die, Attribute, 0, Block);
1690 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001691
Jim Laskeyef42a012006-11-02 20:12:39 +00001692 /// AddBasicType - Add a new basic type attribute to the specified entity.
1693 ///
1694 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1695 const std::string &Name,
1696 unsigned Encoding, unsigned Size) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001697
Jim Laskeyef42a012006-11-02 20:12:39 +00001698 DIE Buffer(DW_TAG_base_type);
1699 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1700 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1701 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel9ede3f22009-01-05 17:44:11 +00001702 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1703 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001704 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001705
Jim Laskeyef42a012006-11-02 20:12:39 +00001706 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1707 ///
1708 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001709 DIE Buffer(DW_TAG_pointer_type);
Dan Gohman82482942007-09-27 23:12:31 +00001710 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Jim Laskeyef42a012006-11-02 20:12:39 +00001711 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelca03c272009-01-05 17:45:59 +00001712 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1713 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001714 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001715
Jim Laskeyef42a012006-11-02 20:12:39 +00001716 /// AddType - Add a new type attribute to the specified entity.
1717 ///
1718 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1719 if (!TyDesc) {
Jim Laskey2b935d52007-01-26 14:19:17 +00001720 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001721 } else {
1722 // Check for pre-existence.
1723 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001724
Jim Laskeyef42a012006-11-02 20:12:39 +00001725 // If it exists then use the existing value.
1726 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001727 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1728 return;
1729 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001730
Jim Laskeyef42a012006-11-02 20:12:39 +00001731 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1732 // FIXME - Not sure why programs and variables are coming through here.
1733 // Short cut for handling subprogram types (not really a TyDesc.)
1734 AddPointerType(Entity, Unit, SubprogramTy->getName());
1735 } else if (GlobalVariableDesc *GlobalTy =
1736 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1737 // FIXME - Not sure why programs and variables are coming through here.
1738 // Short cut for handling global variable types (not really a TyDesc.)
1739 AddPointerType(Entity, Unit, GlobalTy->getName());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001740 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001741 // Set up proxy.
1742 Slot = NewDIEntry();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001743
Jim Laskeyef42a012006-11-02 20:12:39 +00001744 // Construct type.
1745 DIE Buffer(DW_TAG_base_type);
1746 ConstructType(Buffer, TyDesc, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001747
Jim Laskeyef42a012006-11-02 20:12:39 +00001748 // Add debug information entry to entity and unit.
1749 DIE *Die = Unit->AddDie(Buffer);
1750 SetDIEntry(Slot, Die);
1751 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1752 }
1753 }
1754 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001755
Devang Patel48d190f2009-01-05 21:47:57 +00001756 /// AddType - Add a new type attribute to the specified entity.
1757 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
1758 if (Ty.isNull()) {
1759 AddBasicType(Entity, DW_Unit, "", DW_ATE_signed, sizeof(int32_t));
1760 return;
1761 }
1762
1763 // Check for pre-existence.
1764 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1765 // If it exists then use the existing value.
1766 if (Slot) {
1767 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1768 return;
1769 }
1770
1771 // Set up proxy.
1772 Slot = NewDIEntry();
1773
1774 // Construct type.
1775 DIE Buffer(DW_TAG_base_type);
1776 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1777 ConstructTypeDIE(DW_Unit, Buffer, BT);
1778 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1779 ConstructTypeDIE(DW_Unit, Buffer, DT);
1780 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1781 ConstructTypeDIE(DW_Unit, Buffer, CT);
1782
1783 // Add debug information entry to entity and unit.
1784 DIE *Die = DW_Unit->AddDie(Buffer);
1785 SetDIEntry(Slot, Die);
1786 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1787 }
1788
Devang Patele5202732009-01-05 19:07:53 +00001789 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1790 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1791 DIBasicType *BTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001792
1793 // Get core information.
1794 const std::string &Name = BTy->getName();
1795 Buffer.setTag(DW_TAG_base_type);
1796 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy->getEncoding());
1797 // Add name if not anonymous or intermediate type.
1798 if (!Name.empty())
1799 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1800 uint64_t Size = BTy->getSizeInBits() >> 3;
1801 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1802 }
1803
Devang Patele5202732009-01-05 19:07:53 +00001804 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1805 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1806 DIDerivedType *DTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001807
1808 // Get core information.
1809 const std::string &Name = DTy->getName();
1810 uint64_t Size = DTy->getSizeInBits() >> 3;
1811 unsigned Tag = DTy->getTag();
1812 // FIXME - Workaround for templates.
1813 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1814
1815 Buffer.setTag(Tag);
1816 // Map to main type, void will not have a type.
1817 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001818 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001819
1820 // Add name if not anonymous or intermediate type.
1821 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1822
1823 // Add size if non-zero (derived types might be zero-sized.)
1824 if (Size)
1825 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1826
1827 // Add source line info if available and TyDesc is not a forward
1828 // declaration.
1829 // FIXME - Enable this. if (!DTy->isForwardDecl())
1830 // FIXME - Enable this. AddSourceLine(&Buffer, *DTy);
1831 }
1832
Devang Patelf4215332009-01-05 19:55:51 +00001833 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1834 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1835 DICompositeType *CTy) {
1836
1837 // Get core information.
1838 const std::string &Name = CTy->getName();
1839 uint64_t Size = CTy->getSizeInBits() >> 3;
1840 unsigned Tag = CTy->getTag();
1841 switch (Tag) {
1842 case DW_TAG_vector_type:
1843 case DW_TAG_array_type:
1844 ConstructArrayTypeDIE(DW_Unit, Buffer, CTy);
1845 break;
1846 //FIXME - Enable this.
1847 // case DW_TAG_enumeration_type:
1848 // DIArray Elements = CTy->getTypeArray();
1849 // // Add enumerators to enumeration type.
1850 // for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i)
1851 // ConstructEnumTypeDIE(Buffer, &Elements.getElement(i));
1852 // break;
1853 case DW_TAG_subroutine_type:
1854 {
1855 // Add prototype flag.
1856 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1857 DIArray Elements = CTy->getTypeArray();
1858 // Add return type.
Devang Patel48d190f2009-01-05 21:47:57 +00001859 DIDescriptor RTy = Elements.getElement(0);
1860 if (DIBasicType *BT = dyn_cast<DIBasicType>(&RTy))
1861 AddType(DW_Unit, &Buffer, *BT);
1862 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&RTy))
1863 AddType(DW_Unit, &Buffer, *DT);
1864 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&RTy))
1865 AddType(DW_Unit, &Buffer, *CT);
1866
1867 //AddType(DW_Unit, &Buffer, Elements.getElement(0));
Devang Patelf4215332009-01-05 19:55:51 +00001868 // Add arguments.
1869 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1870 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001871 DIDescriptor Ty = Elements.getElement(i);
1872 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1873 AddType(DW_Unit, &Buffer, *BT);
1874 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1875 AddType(DW_Unit, &Buffer, *DT);
1876 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1877 AddType(DW_Unit, &Buffer, *CT);
Devang Patelf4215332009-01-05 19:55:51 +00001878 Buffer.AddChild(Arg);
1879 }
1880 }
1881 break;
1882 case DW_TAG_structure_type:
1883 case DW_TAG_union_type:
1884 {
1885 // Add elements to structure type.
1886 DIArray Elements = CTy->getTypeArray();
1887 // Add elements to structure type.
1888 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1889 DIDescriptor Element = Elements.getElement(i);
1890 if (DISubprogram *SP = dyn_cast<DISubprogram>(&Element))
1891 ConstructFieldTypeDIE(DW_Unit, Buffer, SP);
1892 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Element))
1893 ConstructFieldTypeDIE(DW_Unit, Buffer, DT);
1894 else if (DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(&Element))
1895 ConstructFieldTypeDIE(DW_Unit, Buffer, GV);
1896 }
1897 }
1898 break;
1899 default:
1900 break;
1901 }
1902
1903 // Add name if not anonymous or intermediate type.
1904 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1905
1906 // Add size if non-zero (derived types might be zero-sized.)
1907 if (Size)
1908 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1909 else {
1910 // Add zero size even if it is not a forward declaration.
1911 // FIXME - Enable this.
1912 // if (!CTy->isDefinition())
1913 // AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1914 // else
1915 // AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1916 }
1917
1918 // Add source line info if available and TyDesc is not a forward
1919 // declaration.
1920 // FIXME - Enable this.
1921 // if (CTy->isForwardDecl())
1922 // AddSourceLine(&Buffer, *CTy);
1923 }
1924
Devang Patel68afdc32009-01-05 18:33:01 +00001925 // ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1926 void ConstructSubrangeDIE (DIE &Buffer, DISubrange *SR, DIE *IndexTy) {
1927 int64_t L = SR->getLo();
1928 int64_t H = SR->getHi();
1929 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1930 if (L != H) {
1931 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1932 if (L)
1933 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1934 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
1935 }
1936 Buffer.AddChild(DW_Subrange);
1937 }
1938
1939 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1940 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1941 DICompositeType *CTy) {
1942 Buffer.setTag(DW_TAG_array_type);
1943 if (CTy->getTag() == DW_TAG_vector_type)
1944 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1945
1946 DIArray Elements = CTy->getTypeArray();
1947 // FIXME - Enable this.
Devang Patel48d190f2009-01-05 21:47:57 +00001948 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001949
1950 // Construct an anonymous type for index type.
1951 DIE IdxBuffer(DW_TAG_base_type);
1952 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1953 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1954 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1955
1956 // Add subranges to array type.
1957 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001958 DIDescriptor Element = Elements.getElement(i);
1959 if (DISubrange *SR = dyn_cast<DISubrange>(&Element))
1960 ConstructSubrangeDIE(Buffer, SR, IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001961 }
1962 }
1963
Devang Patelc69bf2c2009-01-05 18:38:38 +00001964 /// ConstructEnumTypeDIE - Construct enum type DIE from
1965 /// DIEnumerator.
Devang Patelf4215332009-01-05 19:55:51 +00001966 void ConstructEnumTypeDIE(CompileUnit *DW_Unit,
1967 DIE &Buffer, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001968
1969 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1970 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1971 int64_t Value = ETy->getEnumValue();
1972 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1973 Buffer.AddChild(Enumerator);
1974 }
Devang Patel68afdc32009-01-05 18:33:01 +00001975
Devang Patel86ae1422009-01-05 18:59:44 +00001976 /// ConstructFieldTypeDIE - Construct variable DIE for a struct field.
1977 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1978 DIE &Buffer, DIGlobalVariable *V) {
1979
1980 DIE *VariableDie = new DIE(DW_TAG_variable);
1981 const std::string &LinkageName = V->getLinkageName();
1982 if (!LinkageName.empty())
1983 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1984 LinkageName);
1985 // FIXME - Enable this. AddSourceLine(VariableDie, V);
Devang Patel48d190f2009-01-05 21:47:57 +00001986 AddType(DW_Unit, VariableDie, V->getType());
Devang Patel86ae1422009-01-05 18:59:44 +00001987 if (!V->isLocalToUnit())
1988 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1989 AddUInt(VariableDie, DW_AT_declaration, DW_FORM_flag, 1);
1990 Buffer.AddChild(VariableDie);
1991 }
1992
1993 /// ConstructFieldTypeDIE - Construct subprogram DIE for a struct field.
1994 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1995 DIE &Buffer, DISubprogram *SP,
1996 bool IsConstructor = false) {
1997 DIE *Method = new DIE(DW_TAG_subprogram);
1998 AddString(Method, DW_AT_name, DW_FORM_string, SP->getName());
1999 const std::string &LinkageName = SP->getLinkageName();
2000 if (!LinkageName.empty())
2001 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
2002 // FIXME - Enable this. AddSourceLine(Method, SP);
2003
2004 DICompositeType MTy = SP->getType();
2005 DIArray Args = MTy.getTypeArray();
2006
2007 // Add Return Type.
Devang Patel48d190f2009-01-05 21:47:57 +00002008 if (!IsConstructor) {
2009 DIDescriptor Ty = Args.getElement(0);
2010 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
2011 AddType(DW_Unit, Method, *BT);
2012 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
2013 AddType(DW_Unit, Method, *DT);
2014 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
2015 AddType(DW_Unit, Method, *CT);
2016 }
Devang Patel86ae1422009-01-05 18:59:44 +00002017
2018 // Add arguments.
2019 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
2020 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00002021 DIDescriptor Ty = Args.getElement(i);
2022 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
2023 AddType(DW_Unit, Method, *BT);
2024 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
2025 AddType(DW_Unit, Method, *DT);
2026 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
2027 AddType(DW_Unit, Method, *CT);
Devang Patel86ae1422009-01-05 18:59:44 +00002028 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
2029 Method->AddChild(Arg);
2030 }
2031
2032 if (!SP->isLocalToUnit())
2033 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
2034 Buffer.AddChild(Method);
2035 }
2036
2037 /// COnstructFieldTypeDIE - Construct derived type DIE for a struct field.
2038 void ConstructFieldTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
2039 DIDerivedType *DTy) {
2040 unsigned Tag = DTy->getTag();
2041 DIE *MemberDie = new DIE(Tag);
2042 if (!DTy->getName().empty())
2043 AddString(MemberDie, DW_AT_name, DW_FORM_string, DTy->getName());
2044 // FIXME - Enable this. AddSourceLine(MemberDie, DTy);
2045
2046 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00002047 AddType(DW_Unit, MemberDie, FromTy);
Devang Patel86ae1422009-01-05 18:59:44 +00002048
2049 uint64_t Size = DTy->getSizeInBits();
2050 uint64_t Offset = DTy->getOffsetInBits();
2051
2052 // FIXME Handle bitfields
2053
2054 // Add size.
2055 AddUInt(MemberDie, DW_AT_bit_size, 0, Size);
2056 // Add computation for offset.
2057 DIEBlock *Block = new DIEBlock();
2058 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
2059 AddUInt(Block, 0, DW_FORM_udata, Offset >> 3);
2060 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
2061
2062 // FIXME Handle DW_AT_accessibility.
2063
2064 Buffer.AddChild(MemberDie);
2065 }
2066
Jim Laskeyef42a012006-11-02 20:12:39 +00002067 /// ConstructType - Adds all the required attributes to the type.
2068 ///
2069 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
2070 // Get core information.
2071 const std::string &Name = TyDesc->getName();
2072 uint64_t Size = TyDesc->getSize() >> 3;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002073
Jim Laskeyef42a012006-11-02 20:12:39 +00002074 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
2075 // Fundamental types like int, float, bool
2076 Buffer.setTag(DW_TAG_base_type);
2077 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
2078 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00002079 // Fetch tag.
2080 unsigned Tag = DerivedTy->getTag();
2081 // FIXME - Workaround for templates.
2082 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002083 // Pointers, typedefs et al.
Jim Laskey85f419b2006-11-09 16:32:26 +00002084 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00002085 // Map to main type, void will not have a type.
2086 if (TypeDesc *FromTy = DerivedTy->getFromType())
2087 AddType(&Buffer, FromTy, Unit);
2088 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
2089 // Fetch tag.
2090 unsigned Tag = CompTy->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002091
Jim Laskeyef42a012006-11-02 20:12:39 +00002092 // Set tag accordingly.
2093 if (Tag == DW_TAG_vector_type)
2094 Buffer.setTag(DW_TAG_array_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002095 else
Jim Laskeyef42a012006-11-02 20:12:39 +00002096 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00002097
Jim Laskeyef42a012006-11-02 20:12:39 +00002098 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002099
Jim Laskeyef42a012006-11-02 20:12:39 +00002100 switch (Tag) {
2101 case DW_TAG_vector_type:
2102 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
2103 // Fall thru
2104 case DW_TAG_array_type: {
2105 // Add element type.
2106 if (TypeDesc *FromTy = CompTy->getFromType())
2107 AddType(&Buffer, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002108
Jim Laskeyef42a012006-11-02 20:12:39 +00002109 // Don't emit size attribute.
2110 Size = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002111
Jim Laskeyef42a012006-11-02 20:12:39 +00002112 // Construct an anonymous type for index type.
Devang Patel9ede3f22009-01-05 17:44:11 +00002113 DIE Buffer(DW_TAG_base_type);
2114 AddUInt(&Buffer, DW_AT_byte_size, 0, sizeof(int32_t));
2115 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
2116 DIE *IndexTy = Unit->AddDie(Buffer);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002117
Jim Laskeyef42a012006-11-02 20:12:39 +00002118 // Add subranges to array type.
Evan Chengf8480282008-12-09 17:56:30 +00002119 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002120 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
2121 int64_t Lo = SRD->getLo();
2122 int64_t Hi = SRD->getHi();
2123 DIE *Subrange = new DIE(DW_TAG_subrange_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002124
Jim Laskeyef42a012006-11-02 20:12:39 +00002125 // If a range is available.
2126 if (Lo != Hi) {
2127 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
2128 // Only add low if non-zero.
2129 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
2130 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
2131 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002132
Jim Laskeyef42a012006-11-02 20:12:39 +00002133 Buffer.AddChild(Subrange);
2134 }
2135 break;
2136 }
2137 case DW_TAG_structure_type:
2138 case DW_TAG_union_type: {
2139 // Add elements to structure type.
Evan Chengf8480282008-12-09 17:56:30 +00002140 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002141 DebugInfoDesc *Element = Elements[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002142
Jim Laskeyef42a012006-11-02 20:12:39 +00002143 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
2144 // Add field or base class.
Jim Laskeyef42a012006-11-02 20:12:39 +00002145 unsigned Tag = MemberDesc->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002146
Jim Laskeyef42a012006-11-02 20:12:39 +00002147 // Extract the basic information.
2148 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00002149 uint64_t Size = MemberDesc->getSize();
2150 uint64_t Align = MemberDesc->getAlign();
2151 uint64_t Offset = MemberDesc->getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002152
Jim Laskeyef42a012006-11-02 20:12:39 +00002153 // Construct member debug information entry.
2154 DIE *Member = new DIE(Tag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002155
Jim Laskeyef42a012006-11-02 20:12:39 +00002156 // Add name if not "".
2157 if (!Name.empty())
2158 AddString(Member, DW_AT_name, DW_FORM_string, Name);
Evan Chengf8480282008-12-09 17:56:30 +00002159
Jim Laskeyef42a012006-11-02 20:12:39 +00002160 // Add location if available.
2161 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002162
Jim Laskeyef42a012006-11-02 20:12:39 +00002163 // Most of the time the field info is the same as the members.
2164 uint64_t FieldSize = Size;
2165 uint64_t FieldAlign = Align;
2166 uint64_t FieldOffset = Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002167
Jim Laskeyee5f9272006-12-22 20:03:42 +00002168 // Set the member type.
2169 TypeDesc *FromTy = MemberDesc->getFromType();
2170 AddType(Member, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002171
Jim Laskeyee5f9272006-12-22 20:03:42 +00002172 // Walk up typedefs until a real size is found.
2173 while (FromTy) {
2174 if (FromTy->getTag() != DW_TAG_typedef) {
2175 FieldSize = FromTy->getSize();
Devang Pateld0935c32008-12-23 21:55:38 +00002176 FieldAlign = FromTy->getAlign();
Jim Laskeyee5f9272006-12-22 20:03:42 +00002177 break;
2178 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002179
Dan Gohman275769a2007-07-23 20:24:29 +00002180 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
Jim Laskeyef42a012006-11-02 20:12:39 +00002181 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002182
Jim Laskeyef42a012006-11-02 20:12:39 +00002183 // Unless we have a bit field.
2184 if (Tag == DW_TAG_member && FieldSize != Size) {
2185 // Construct the alignment mask.
2186 uint64_t AlignMask = ~(FieldAlign - 1);
2187 // Determine the high bit + 1 of the declared size.
2188 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
2189 // Work backwards to determine the base offset of the field.
2190 FieldOffset = HiMark - FieldSize;
2191 // Now normalize offset to the field.
2192 Offset -= FieldOffset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002193
Jim Laskeyef42a012006-11-02 20:12:39 +00002194 // Maybe we need to work from the other end.
2195 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002196
Jim Laskeyef42a012006-11-02 20:12:39 +00002197 // Add size and offset.
2198 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
2199 AddUInt(Member, DW_AT_bit_size, 0, Size);
2200 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
2201 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002202
Jim Laskeyef42a012006-11-02 20:12:39 +00002203 // Add computation for offset.
2204 DIEBlock *Block = new DIEBlock();
2205 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
2206 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
2207 AddBlock(Member, DW_AT_data_member_location, 0, Block);
2208
2209 // Add accessibility (public default unless is base class.
2210 if (MemberDesc->isProtected()) {
2211 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
2212 } else if (MemberDesc->isPrivate()) {
2213 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
2214 } else if (Tag == DW_TAG_inheritance) {
2215 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
2216 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002217
Jim Laskeyef42a012006-11-02 20:12:39 +00002218 Buffer.AddChild(Member);
2219 } else if (GlobalVariableDesc *StaticDesc =
2220 dyn_cast<GlobalVariableDesc>(Element)) {
2221 // Add static member.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002222
Jim Laskeyef42a012006-11-02 20:12:39 +00002223 // Construct member debug information entry.
2224 DIE *Static = new DIE(DW_TAG_variable);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002225
Jim Laskeyef42a012006-11-02 20:12:39 +00002226 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00002227 const std::string &Name = StaticDesc->getName();
2228 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00002229 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002230 if (!LinkageName.empty()) {
2231 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
2232 LinkageName);
2233 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002234
Jim Laskeyef42a012006-11-02 20:12:39 +00002235 // Add location.
2236 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002237
Jim Laskeyef42a012006-11-02 20:12:39 +00002238 // Add type.
2239 if (TypeDesc *StaticTy = StaticDesc->getType())
2240 AddType(Static, StaticTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002241
Jim Laskeyef42a012006-11-02 20:12:39 +00002242 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00002243 if (!StaticDesc->isStatic())
2244 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002245 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002246
Jim Laskeyef42a012006-11-02 20:12:39 +00002247 Buffer.AddChild(Static);
2248 } else if (SubprogramDesc *MethodDesc =
2249 dyn_cast<SubprogramDesc>(Element)) {
2250 // Add member function.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002251
Jim Laskeyef42a012006-11-02 20:12:39 +00002252 // Construct member debug information entry.
2253 DIE *Method = new DIE(DW_TAG_subprogram);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002254
Jim Laskeyef42a012006-11-02 20:12:39 +00002255 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00002256 const std::string &Name = MethodDesc->getName();
2257 const std::string &LinkageName = MethodDesc->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002258
2259 AddString(Method, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002260 bool IsCTor = TyDesc->getName() == Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002261
Jim Laskey2172f962006-11-30 14:35:45 +00002262 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002263 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00002264 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00002265 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002266
Jim Laskeyef42a012006-11-02 20:12:39 +00002267 // Add location.
2268 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002269
Jim Laskeyef42a012006-11-02 20:12:39 +00002270 // Add type.
2271 if (CompositeTypeDesc *MethodTy =
2272 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
2273 // Get argument information.
2274 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002275
Jim Laskeyef42a012006-11-02 20:12:39 +00002276 // If not a ctor.
2277 if (!IsCTor) {
2278 // Add return type.
2279 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
2280 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002281
Jim Laskeyef42a012006-11-02 20:12:39 +00002282 // Add arguments.
Evan Chengf8480282008-12-09 17:56:30 +00002283 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002284 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2285 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
2286 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
2287 Method->AddChild(Arg);
2288 }
2289 }
2290
2291 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00002292 if (!MethodDesc->isStatic())
2293 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002294 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002295
Jim Laskeyef42a012006-11-02 20:12:39 +00002296 Buffer.AddChild(Method);
2297 }
2298 }
2299 break;
2300 }
2301 case DW_TAG_enumeration_type: {
2302 // Add enumerators to enumeration type.
Evan Chengf8480282008-12-09 17:56:30 +00002303 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002304 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
2305 const std::string &Name = ED->getName();
2306 int64_t Value = ED->getValue();
2307 DIE *Enumerator = new DIE(DW_TAG_enumerator);
2308 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
2309 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
2310 Buffer.AddChild(Enumerator);
2311 }
2312
2313 break;
2314 }
2315 case DW_TAG_subroutine_type: {
2316 // Add prototype flag.
2317 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
2318 // Add return type.
2319 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002320
Jim Laskeyef42a012006-11-02 20:12:39 +00002321 // Add arguments.
Evan Chengf8480282008-12-09 17:56:30 +00002322 for (unsigned i = 1, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002323 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2324 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
2325 Buffer.AddChild(Arg);
2326 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002327
Jim Laskeyef42a012006-11-02 20:12:39 +00002328 break;
2329 }
2330 default: break;
2331 }
2332 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002333
Jim Laskeyef42a012006-11-02 20:12:39 +00002334 // Add name if not anonymous or intermediate type.
2335 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Evan Chengf8480282008-12-09 17:56:30 +00002336
Evan Cheng94ea5be2008-12-10 00:15:44 +00002337 // Add size if non-zero (derived types might be zero-sized.)
2338 if (Size)
2339 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
2340 else if (isa<CompositeTypeDesc>(TyDesc)) {
2341 // If TyDesc is a composite type, then add size even if it's zero unless
2342 // it's a forward declaration.
2343 if (TyDesc->isForwardDecl())
2344 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
2345 else
2346 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
2347 }
2348
2349 // Add source line info if available and TyDesc is not a forward
2350 // declaration.
2351 if (!TyDesc->isForwardDecl())
2352 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
Jim Laskeyef42a012006-11-02 20:12:39 +00002353 }
2354
2355 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00002356 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002357 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
2358 // Construct debug information entry.
2359 DIE *Die = new DIE(DW_TAG_compile_unit);
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00002360 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2361 DWLabel("section_line", 0), DWLabel("section_line", 0), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002362 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
2363 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
2364 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
Devang Patel83580992008-12-12 21:57:54 +00002365 if (!UnitDesc->getDirectory().empty())
2366 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002367
Jim Laskeyef42a012006-11-02 20:12:39 +00002368 // Construct compile unit.
2369 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002370
Jim Laskeyef42a012006-11-02 20:12:39 +00002371 // Add Unit to compile unit map.
2372 DescToUnitMap[UnitDesc] = Unit;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002373
Jim Laskeyef42a012006-11-02 20:12:39 +00002374 return Unit;
2375 }
2376
Jim Laskey9d4209f2006-11-07 19:33:46 +00002377 /// GetBaseCompileUnit - Get the main compile unit.
2378 ///
2379 CompileUnit *GetBaseCompileUnit() const {
2380 CompileUnit *Unit = CompileUnits[0];
2381 assert(Unit && "Missing compile unit.");
2382 return Unit;
2383 }
2384
Jim Laskey65195462006-10-30 13:35:07 +00002385 /// FindCompileUnit - Get the compile unit for the given descriptor.
2386 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002387 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002388 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00002389 assert(Unit && "Missing compile unit.");
2390 return Unit;
2391 }
2392
Devang Patel8526cc02009-01-05 22:35:52 +00002393 /// FindCompileUnit - Get the compile unit for the given descriptor.
2394 ///
2395 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
2396 CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
2397 assert(DW_Unit && "Missing compile unit.");
2398 return DW_Unit;
2399 }
2400
Jim Laskeyef42a012006-11-02 20:12:39 +00002401 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00002402 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002403 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
2404 // Get the compile unit context.
2405 CompileUnitDesc *UnitDesc =
2406 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00002407 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002408
2409 // Check for pre-existence.
2410 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
2411 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002412
Jim Laskeyef42a012006-11-02 20:12:39 +00002413 // Get the global variable itself.
2414 GlobalVariable *GV = GVD->getGlobalVariable();
2415
Jim Laskey2172f962006-11-30 14:35:45 +00002416 const std::string &Name = GVD->getName();
2417 const std::string &FullName = GVD->getFullName();
2418 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00002419 // Create the global's variable DIE.
2420 DIE *VariableDie = new DIE(DW_TAG_variable);
2421 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002422 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002423 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00002424 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00002425 }
Jim Laskey6488a072007-01-08 22:15:18 +00002426 AddType(VariableDie, GVD->getType(), Unit);
2427 if (!GVD->isStatic())
2428 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002429
Jim Laskeyef42a012006-11-02 20:12:39 +00002430 // Add source line info if available.
2431 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002432
Jim Laskeyef42a012006-11-02 20:12:39 +00002433 // Add address.
2434 DIEBlock *Block = new DIEBlock();
2435 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00002436 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
2437 AddBlock(VariableDie, DW_AT_location, 0, Block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002438
Jim Laskeyef42a012006-11-02 20:12:39 +00002439 // Add to map.
2440 Slot = VariableDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002441
Jim Laskeyef42a012006-11-02 20:12:39 +00002442 // Add to context owner.
2443 Unit->getDie()->AddChild(VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002444
Jim Laskeyef42a012006-11-02 20:12:39 +00002445 // Expose as global.
2446 // FIXME - need to check external flag.
Jim Laskey2172f962006-11-30 14:35:45 +00002447 Unit->AddGlobal(FullName, VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002448
Jim Laskeyef42a012006-11-02 20:12:39 +00002449 return VariableDie;
2450 }
Jim Laskey65195462006-10-30 13:35:07 +00002451
2452 /// NewSubprogram - Add a new subprogram DIE.
2453 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002454 DIE *NewSubprogram(SubprogramDesc *SPD) {
2455 // Get the compile unit context.
2456 CompileUnitDesc *UnitDesc =
2457 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00002458 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002459
2460 // Check for pre-existence.
2461 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
2462 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002463
Jim Laskeyef42a012006-11-02 20:12:39 +00002464 // Gather the details (simplify add attribute code.)
Jim Laskey2172f962006-11-30 14:35:45 +00002465 const std::string &Name = SPD->getName();
2466 const std::string &FullName = SPD->getFullName();
2467 const std::string &LinkageName = SPD->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002468
Jim Laskeyef42a012006-11-02 20:12:39 +00002469 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
2470 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002471 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002472 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00002473 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00002474 }
2475 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
Jim Laskey6488a072007-01-08 22:15:18 +00002476 if (!SPD->isStatic())
2477 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002478 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002479
Jim Laskeyef42a012006-11-02 20:12:39 +00002480 // Add source line info if available.
2481 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
2482
2483 // Add to map.
2484 Slot = SubprogramDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002485
Jim Laskeyef42a012006-11-02 20:12:39 +00002486 // Add to context owner.
2487 Unit->getDie()->AddChild(SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002488
Jim Laskeyef42a012006-11-02 20:12:39 +00002489 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00002490 Unit->AddGlobal(FullName, SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002491
Jim Laskeyef42a012006-11-02 20:12:39 +00002492 return SubprogramDie;
2493 }
Jim Laskey65195462006-10-30 13:35:07 +00002494
2495 /// NewScopeVariable - Create a new scope variable.
2496 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002497 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
2498 // Get the descriptor.
2499 VariableDesc *VD = DV->getDesc();
2500
2501 // Translate tag to proper Dwarf tag. The result variable is dropped for
2502 // now.
2503 unsigned Tag;
2504 switch (VD->getTag()) {
2505 case DW_TAG_return_variable: return NULL;
2506 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2507 case DW_TAG_auto_variable: // fall thru
2508 default: Tag = DW_TAG_variable; break;
2509 }
2510
2511 // Define variable debug information entry.
2512 DIE *VariableDie = new DIE(Tag);
2513 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2514
2515 // Add source line info if available.
2516 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002517
Jim Laskeyef42a012006-11-02 20:12:39 +00002518 // Add variable type.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002519 AddType(VariableDie, VD->getType(), Unit);
2520
Jim Laskeyef42a012006-11-02 20:12:39 +00002521 // Add variable address.
2522 MachineLocation Location;
Evan Cheng72bebb92008-01-31 03:37:28 +00002523 Location.set(RI->getFrameRegister(*MF),
2524 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Jim Laskeyef42a012006-11-02 20:12:39 +00002525 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002526
Jim Laskeyef42a012006-11-02 20:12:39 +00002527 return VariableDie;
2528 }
Jim Laskey65195462006-10-30 13:35:07 +00002529
Devang Patel7a6e5a32009-01-08 02:33:41 +00002530 /// NewScopeVariable - Create a new scope variable.
2531 ///
2532 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
2533 // Get the descriptor.
2534 DIVariable *VD = DV->getVariable();
2535
2536 // Translate tag to proper Dwarf tag. The result variable is dropped for
2537 // now.
2538 unsigned Tag;
2539 switch (VD->getTag()) {
2540 case DW_TAG_return_variable: return NULL;
2541 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2542 case DW_TAG_auto_variable: // fall thru
2543 default: Tag = DW_TAG_variable; break;
2544 }
2545
2546 // Define variable debug information entry.
2547 DIE *VariableDie = new DIE(Tag);
2548 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2549
2550 // Add source line info if available.
2551 AddSourceLine(VariableDie, VD);
2552
2553 // Add variable type.
2554 AddType(Unit, VariableDie, VD->getType());
2555
2556 // Add variable address.
2557 MachineLocation Location;
2558 Location.set(RI->getFrameRegister(*MF),
2559 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2560 AddAddress(VariableDie, DW_AT_location, Location);
2561
2562 return VariableDie;
2563 }
2564
Devang Patel9f8fcfc2009-01-08 17:19:22 +00002565 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
2566 CompileUnit *Unit = DW_CUs[V];
2567 assert (Unit && "Unable to find CompileUnit");
2568 unsigned ID = NextLabelID();
2569 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
2570 return ID;
2571 }
2572
2573 unsigned getRecordSourceLineCount() {
2574 return Lines.size();
2575 }
2576
2577 unsigned RecordSource(const std::string &Directory,
2578 const std::string &File) {
2579 unsigned DID = Directories.insert(Directory);
2580 return SrcFiles.insert(SrcFileInfo(DID,File));
2581 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002582
Devang Patel2ec78c42009-01-08 02:49:34 +00002583 /// RecordRegionStart - Indicate the start of a region.
2584 ///
2585 unsigned RecordRegionStart(GlobalVariable *V) {
2586 DbgScope *Scope = getOrCreateScope(V);
2587 unsigned ID = NextLabelID();
2588 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
2589 return ID;
2590 }
2591
2592 /// RecordRegionEnd - Indicate the end of a region.
2593 ///
2594 unsigned RecordRegionEnd(GlobalVariable *V) {
2595 DbgScope *Scope = getOrCreateScope(V);
2596 unsigned ID = NextLabelID();
2597 Scope->setEndLabelID(ID);
2598 return ID;
2599 }
2600
2601 /// RecordVariable - Indicate the declaration of a local variable.
2602 ///
2603 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
2604 DbgScope *Scope = getOrCreateScope(GV);
2605 DIVariable *VD = new DIVariable(GV);
2606 DbgVariable *DV = new DbgVariable(VD, FrameIndex);
2607 Scope->AddVariable(DV);
2608 }
2609
Devang Patel7a6e5a32009-01-08 02:33:41 +00002610 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2611 ///
2612 DbgScope *getOrCreateScope(GlobalVariable *V) {
2613 DbgScope *&Slot = DbgScopeMap[V];
2614 if (!Slot) {
2615 // FIXME - breaks down when the context is an inlined function.
2616 DIDescriptor ParentDesc;
2617 DIBlock *DB = new DIBlock(V);
2618 if (DIBlock *Block = dyn_cast<DIBlock>(DB)) {
2619 ParentDesc = Block->getContext();
2620 }
2621 DbgScope *Parent = ParentDesc.isNull() ?
2622 getOrCreateScope(ParentDesc.getGV()) : NULL;
2623 Slot = new DbgScope(Parent, DB);
2624 if (Parent) {
2625 Parent->AddScope(Slot);
2626 } else if (RootDbgScope) {
2627 // FIXME - Add inlined function scopes to the root so we can delete
2628 // them later. Long term, handle inlined functions properly.
2629 RootDbgScope->AddScope(Slot);
2630 } else {
2631 // First function is top level function.
2632 RootDbgScope = Slot;
2633 }
2634 }
2635 return Slot;
2636 }
2637
2638 /// ConstructDbgScope - Construct the components of a scope.
2639 ///
2640 void ConstructDbgScope(DbgScope *ParentScope,
2641 unsigned ParentStartID, unsigned ParentEndID,
2642 DIE *ParentDie, CompileUnit *Unit) {
2643 // Add variables to scope.
2644 SmallVector<DbgVariable *, 32> &Variables = ParentScope->getVariables();
2645 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2646 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2647 if (VariableDie) ParentDie->AddChild(VariableDie);
2648 }
2649
2650 // Add nested scopes.
2651 SmallVector<DbgScope *, 8> &Scopes = ParentScope->getScopes();
2652 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2653 // Define the Scope debug information entry.
2654 DbgScope *Scope = Scopes[j];
2655 // FIXME - Ignore inlined functions for the time being.
2656 if (!Scope->getParent()) continue;
2657
2658 unsigned StartID = MappedLabel(Scope->getStartLabelID());
2659 unsigned EndID = MappedLabel(Scope->getEndLabelID());
2660
2661 // Ignore empty scopes.
2662 if (StartID == EndID && StartID != 0) continue;
2663 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2664
2665 if (StartID == ParentStartID && EndID == ParentEndID) {
2666 // Just add stuff to the parent scope.
2667 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2668 } else {
2669 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2670
2671 // Add the scope bounds.
2672 if (StartID) {
2673 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2674 DWLabel("label", StartID));
2675 } else {
2676 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2677 DWLabel("func_begin", SubprogramCount));
2678 }
2679 if (EndID) {
2680 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2681 DWLabel("label", EndID));
2682 } else {
2683 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2684 DWLabel("func_end", SubprogramCount));
2685 }
2686
2687 // Add the scope contents.
2688 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2689 ParentDie->AddChild(ScopeDie);
2690 }
2691 }
2692 }
2693
2694 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2695 ///
2696 void ConstructRootDbgScope(DbgScope *RootScope) {
2697 // Exit if there is no root scope.
2698 if (!RootScope) return;
2699
2700 // Get the subprogram debug information entry.
2701 DISubprogram *SPD = cast<DISubprogram>(RootScope->getDesc());
2702
2703 // Get the compile unit context.
2704 CompileUnit *Unit = FindCompileUnit(SPD->getCompileUnit());
2705
2706 // Get the subprogram die.
2707 DIE *SPDie = Unit->getDieMapSlotFor(SPD->getGV());
2708 assert(SPDie && "Missing subprogram descriptor");
2709
2710 // Add the function bounds.
2711 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2712 DWLabel("func_begin", SubprogramCount));
2713 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2714 DWLabel("func_end", SubprogramCount));
2715 MachineLocation Location(RI->getFrameRegister(*MF));
2716 AddAddress(SPDie, DW_AT_frame_base, Location);
2717
2718 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2719 }
2720
2721 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2722 ///
2723 void ConstructDefaultDbgScope(MachineFunction *MF) {
2724 // Find the correct subprogram descriptor.
2725 std::string SPName = "llvm.dbg.subprograms";
2726 std::vector<GlobalVariable*> Result;
2727 getGlobalVariablesUsing(*M, SPName, Result);
2728 for (std::vector<GlobalVariable *>::iterator I = Result.begin(),
2729 E = Result.end(); I != E; ++I) {
2730
2731 DISubprogram *SPD = new DISubprogram(*I);
2732
2733 if (SPD->getName() == MF->getFunction()->getName()) {
2734 // Get the compile unit context.
2735 CompileUnit *Unit = FindCompileUnit(SPD->getCompileUnit());
2736
2737 // Get the subprogram die.
2738 DIE *SPDie = Unit->getDieMapSlotFor(SPD->getGV());
2739 assert(SPDie && "Missing subprogram descriptor");
2740
2741 // Add the function bounds.
2742 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2743 DWLabel("func_begin", SubprogramCount));
2744 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2745 DWLabel("func_end", SubprogramCount));
2746
2747 MachineLocation Location(RI->getFrameRegister(*MF));
2748 AddAddress(SPDie, DW_AT_frame_base, Location);
2749 return;
2750 }
2751 }
2752#if 0
2753 // FIXME: This is causing an abort because C++ mangled names are compared
2754 // with their unmangled counterparts. See PR2885. Don't do this assert.
2755 assert(0 && "Couldn't find DIE for machine function!");
2756#endif
2757 }
2758
Jim Laskey65195462006-10-30 13:35:07 +00002759 /// ConstructScope - Construct the components of a scope.
2760 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002761 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00002762 unsigned ParentStartID, unsigned ParentEndID,
2763 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002764 // Add variables to scope.
2765 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
2766 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2767 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
2768 if (VariableDie) ParentDie->AddChild(VariableDie);
2769 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002770
Jim Laskeyef42a012006-11-02 20:12:39 +00002771 // Add nested scopes.
2772 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
2773 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2774 // Define the Scope debug information entry.
2775 DebugScope *Scope = Scopes[j];
2776 // FIXME - Ignore inlined functions for the time being.
2777 if (!Scope->getParent()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002778
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002779 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2780 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00002781
Jim Laskey9d4209f2006-11-07 19:33:46 +00002782 // Ignore empty scopes.
2783 if (StartID == EndID && StartID != 0) continue;
2784 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002785
Jim Laskey36729dd2006-11-29 16:55:57 +00002786 if (StartID == ParentStartID && EndID == ParentEndID) {
2787 // Just add stuff to the parent scope.
2788 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002789 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00002790 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002791
Jim Laskey36729dd2006-11-29 16:55:57 +00002792 // Add the scope bounds.
2793 if (StartID) {
2794 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00002795 DWLabel("label", StartID));
Jim Laskey36729dd2006-11-29 16:55:57 +00002796 } else {
2797 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2798 DWLabel("func_begin", SubprogramCount));
2799 }
2800 if (EndID) {
2801 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00002802 DWLabel("label", EndID));
Jim Laskey36729dd2006-11-29 16:55:57 +00002803 } else {
2804 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2805 DWLabel("func_end", SubprogramCount));
2806 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002807
Jim Laskey36729dd2006-11-29 16:55:57 +00002808 // Add the scope contents.
2809 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
2810 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00002811 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002812 }
2813 }
Jim Laskey65195462006-10-30 13:35:07 +00002814
2815 /// ConstructRootScope - Construct the scope for the subprogram.
2816 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002817 void ConstructRootScope(DebugScope *RootScope) {
2818 // Exit if there is no root scope.
2819 if (!RootScope) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002820
2821 // Get the subprogram debug information entry.
Jim Laskeyef42a012006-11-02 20:12:39 +00002822 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002823
Jim Laskeyef42a012006-11-02 20:12:39 +00002824 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00002825 CompileUnit *Unit = GetBaseCompileUnit();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002826
Jim Laskeyef42a012006-11-02 20:12:39 +00002827 // Get the subprogram die.
2828 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2829 assert(SPDie && "Missing subprogram descriptor");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002830
Jim Laskeyef42a012006-11-02 20:12:39 +00002831 // Add the function bounds.
2832 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2833 DWLabel("func_begin", SubprogramCount));
2834 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2835 DWLabel("func_end", SubprogramCount));
2836 MachineLocation Location(RI->getFrameRegister(*MF));
2837 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002838
Jim Laskey36729dd2006-11-29 16:55:57 +00002839 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002840 }
Jim Laskey65195462006-10-30 13:35:07 +00002841
Bill Wendlingd751c642008-09-26 00:28:12 +00002842 /// ConstructDefaultScope - Construct a default scope for the subprogram.
2843 ///
2844 void ConstructDefaultScope(MachineFunction *MF) {
2845 // Find the correct subprogram descriptor.
2846 std::vector<SubprogramDesc *> Subprograms;
2847 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
2848
2849 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2850 SubprogramDesc *SPD = Subprograms[i];
2851
2852 if (SPD->getName() == MF->getFunction()->getName()) {
2853 // Get the compile unit context.
2854 CompileUnit *Unit = GetBaseCompileUnit();
2855
2856 // Get the subprogram die.
2857 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2858 assert(SPDie && "Missing subprogram descriptor");
2859
2860 // Add the function bounds.
2861 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2862 DWLabel("func_begin", SubprogramCount));
2863 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2864 DWLabel("func_end", SubprogramCount));
2865
2866 MachineLocation Location(RI->getFrameRegister(*MF));
2867 AddAddress(SPDie, DW_AT_frame_base, Location);
2868 return;
2869 }
2870 }
Bill Wendlinga9f0cc42008-10-17 18:48:57 +00002871#if 0
2872 // FIXME: This is causing an abort because C++ mangled names are compared
2873 // with their unmangled counterparts. See PR2885. Don't do this assert.
Bill Wendlingd751c642008-09-26 00:28:12 +00002874 assert(0 && "Couldn't find DIE for machine function!");
Bill Wendlinga9f0cc42008-10-17 18:48:57 +00002875#endif
Bill Wendlingd751c642008-09-26 00:28:12 +00002876 }
2877
Jim Laskeyef42a012006-11-02 20:12:39 +00002878 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2879 /// tools to recognize the object file contains Dwarf information.
2880 void EmitInitial() {
2881 // Check to see if we already emitted intial headers.
2882 if (didInitial) return;
2883 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002884
Jim Laskeyef42a012006-11-02 20:12:39 +00002885 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002886 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002887 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002888 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002889 }
2890 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2891 EmitLabel("section_info", 0);
2892 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2893 EmitLabel("section_abbrev", 0);
2894 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2895 EmitLabel("section_aranges", 0);
2896 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2897 EmitLabel("section_macinfo", 0);
2898 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2899 EmitLabel("section_line", 0);
2900 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2901 EmitLabel("section_loc", 0);
2902 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2903 EmitLabel("section_pubnames", 0);
2904 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2905 EmitLabel("section_str", 0);
2906 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2907 EmitLabel("section_ranges", 0);
2908
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002909 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002910 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002911 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002912 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002913 }
2914
Jim Laskey65195462006-10-30 13:35:07 +00002915 /// EmitDIE - Recusively Emits a debug information entry.
2916 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002917 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002918 // Get the abbreviation for this DIE.
2919 unsigned AbbrevNumber = Die->getAbbrevNumber();
2920 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002921
Jim Laskeybacd3042007-02-21 22:48:45 +00002922 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002923
2924 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002925 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002926
2927 if (VerboseAsm)
2928 Asm->EOL(std::string("Abbrev [" +
2929 utostr(AbbrevNumber) +
2930 "] 0x" + utohexstr(Die->getOffset()) +
2931 ":0x" + utohexstr(Die->getSize()) + " " +
2932 TagString(Abbrev->getTag())));
2933 else
2934 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002935
Owen Anderson873e1b52008-06-24 21:44:59 +00002936 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2937 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002938
Jim Laskeyef42a012006-11-02 20:12:39 +00002939 // Emit the DIE attribute values.
2940 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2941 unsigned Attr = AbbrevData[i].getAttribute();
2942 unsigned Form = AbbrevData[i].getForm();
2943 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002944
Jim Laskeyef42a012006-11-02 20:12:39 +00002945 switch (Attr) {
2946 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002947 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002948 break;
2949 }
2950 default: {
2951 // Emit an attribute using the defined form.
2952 Values[i]->EmitValue(*this, Form);
2953 break;
2954 }
2955 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002956
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002957 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002958 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002959
Jim Laskeyef42a012006-11-02 20:12:39 +00002960 // Emit the DIE children if any.
2961 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2962 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002963
Jim Laskeyef42a012006-11-02 20:12:39 +00002964 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2965 EmitDIE(Children[j]);
2966 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002967
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002968 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002969 }
2970 }
2971
Jim Laskey65195462006-10-30 13:35:07 +00002972 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2973 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002974 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2975 // Get the children.
2976 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002977
Jim Laskeyef42a012006-11-02 20:12:39 +00002978 // If not last sibling and has children then add sibling offset attribute.
2979 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2980
2981 // Record the abbreviation.
2982 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002983
Jim Laskeyef42a012006-11-02 20:12:39 +00002984 // Get the abbreviation for this DIE.
2985 unsigned AbbrevNumber = Die->getAbbrevNumber();
2986 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2987
2988 // Set DIE offset
2989 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002990
Jim Laskeyef42a012006-11-02 20:12:39 +00002991 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002992 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2993
Owen Anderson873e1b52008-06-24 21:44:59 +00002994 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2995 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002996
2997 // Size the DIE attribute values.
2998 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2999 // Size attribute value.
3000 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
3001 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003002
Jim Laskeyef42a012006-11-02 20:12:39 +00003003 // Size the DIE children if any.
3004 if (!Children.empty()) {
3005 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
3006 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003007
Jim Laskeyef42a012006-11-02 20:12:39 +00003008 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3009 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
3010 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003011
Jim Laskeyef42a012006-11-02 20:12:39 +00003012 // End of children marker.
3013 Offset += sizeof(int8_t);
3014 }
3015
3016 Die->setSize(Offset - Die->getOffset());
3017 return Offset;
3018 }
Jim Laskey65195462006-10-30 13:35:07 +00003019
3020 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
3021 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003022 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00003023 // Process base compile unit.
3024 CompileUnit *Unit = GetBaseCompileUnit();
3025 // Compute size of compile unit header
3026 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
3027 sizeof(int16_t) + // DWARF version number
3028 sizeof(int32_t) + // Offset Into Abbrev. Section
3029 sizeof(int8_t); // Pointer Size (in bytes)
3030 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00003031 }
3032
Jim Laskey65195462006-10-30 13:35:07 +00003033 /// EmitDebugInfo - Emit the debug info section.
3034 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003035 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003036 // Start debug info section.
3037 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003038
Jim Laskey5496f012006-11-09 14:52:14 +00003039 CompileUnit *Unit = GetBaseCompileUnit();
3040 DIE *Die = Unit->getDie();
3041 // Emit the compile units header.
3042 EmitLabel("info_begin", Unit->getID());
3043 // Emit size of content not including length itself
3044 unsigned ContentSize = Die->getSize() +
3045 sizeof(int16_t) + // DWARF version number
3046 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00003047 sizeof(int8_t) + // Pointer Size (in bytes)
3048 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003049
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003050 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
3051 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00003052 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003053 Asm->EOL("Offset Into Abbrev. Section");
Dan Gohman82482942007-09-27 23:12:31 +00003054 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003055
Jim Laskey5496f012006-11-09 14:52:14 +00003056 EmitDIE(Die);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003057 // FIXME - extra padding for gdb bug.
3058 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3059 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3060 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3061 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Jim Laskey5496f012006-11-09 14:52:14 +00003062 EmitLabel("info_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003063
Jim Laskeybacd3042007-02-21 22:48:45 +00003064 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003065 }
3066
Jim Laskey65195462006-10-30 13:35:07 +00003067 /// EmitAbbreviations - Emit the abbreviation section.
3068 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003069 void EmitAbbreviations() const {
3070 // Check to see if it is worth the effort.
3071 if (!Abbreviations.empty()) {
3072 // Start the debug abbrev section.
3073 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003074
Jim Laskeyef42a012006-11-02 20:12:39 +00003075 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003076
Jim Laskeyef42a012006-11-02 20:12:39 +00003077 // For each abbrevation.
3078 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3079 // Get abbreviation data
3080 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003081
Jim Laskeyef42a012006-11-02 20:12:39 +00003082 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003083 Asm->EmitULEB128Bytes(Abbrev->getNumber());
3084 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003085
Jim Laskeyef42a012006-11-02 20:12:39 +00003086 // Emit the abbreviations data.
3087 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003088
Jim Laskeybacd3042007-02-21 22:48:45 +00003089 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003090 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003091
Jim Laskey7b1b39d2007-02-22 18:22:42 +00003092 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00003093 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00003094
Jim Laskeyef42a012006-11-02 20:12:39 +00003095 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003096
Jim Laskeybacd3042007-02-21 22:48:45 +00003097 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003098 }
3099 }
3100
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003101 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
3102 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003103 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003104 void EmitEndOfLineMatrix(unsigned SectionEnd) {
3105 // Define last address of section.
3106 Asm->EmitInt8(0); Asm->EOL("Extended Op");
3107 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
3108 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
3109 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
3110
3111 // Mark end of matrix.
3112 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
3113 Asm->EmitULEB128Bytes(1); Asm->EOL();
3114 Asm->EmitInt8(1); Asm->EOL();
3115 }
3116
Jim Laskey65195462006-10-30 13:35:07 +00003117 /// EmitDebugLines - Emit source line information.
3118 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003119 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003120 // If the target is using .loc/.file, the assembler will be emitting the
3121 // .debug_line table automatically.
3122 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00003123 return;
3124
Jim Laskeyef42a012006-11-02 20:12:39 +00003125 // Minimum line delta, thus ranging from -10..(255-10).
3126 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
3127 // Maximum line delta, thus ranging from -10..(255-10).
3128 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00003129
Jim Laskeyef42a012006-11-02 20:12:39 +00003130 // Start the dwarf line section.
3131 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003132
Jim Laskeyef42a012006-11-02 20:12:39 +00003133 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003134
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003135 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003136 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00003137 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003138
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003139 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003140
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003141 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003142 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00003143 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003144
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003145 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00003146
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003147 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00003148
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003149 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003150
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003151 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00003152
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003153 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003154
Jim Laskeyef42a012006-11-02 20:12:39 +00003155 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003156 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
3157 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
3158 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
3159 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
3160 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
3161 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
3162 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
3163 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
3164 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00003165
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003166 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Evan Cheng6547e402008-07-01 23:18:29 +00003167 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
Jim Laskeyef42a012006-11-02 20:12:39 +00003168
3169 // Emit directories.
3170 for (unsigned DirectoryID = 1, NDID = Directories.size();
3171 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003172 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00003173 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003174 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003175
Jim Laskeyef42a012006-11-02 20:12:39 +00003176 // Emit files.
3177 for (unsigned SourceID = 1, NSID = SourceFiles.size();
3178 SourceID <= NSID; ++SourceID) {
3179 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003180 Asm->EmitString(SourceFile.getName());
3181 Asm->EOL("Source");
3182 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
3183 Asm->EOL("Directory #");
3184 Asm->EmitULEB128Bytes(0);
3185 Asm->EOL("Mod date");
3186 Asm->EmitULEB128Bytes(0);
3187 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00003188 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003189 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003190
Jim Laskeyef42a012006-11-02 20:12:39 +00003191 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003192
Jim Laskeyef42a012006-11-02 20:12:39 +00003193 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003194 unsigned SecSrcLinesSize = SectionSourceLines.size();
3195
3196 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003197 // Isolate current sections line info.
3198 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00003199
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003200 if (VerboseAsm) {
3201 const Section* S = SectionMap[j + 1];
3202 Asm->EOL(std::string("Section ") + S->getName());
3203 } else
Evan Cheng6547e402008-07-01 23:18:29 +00003204 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003205
3206 // Dwarf assumes we start with first line of first source file.
3207 unsigned Source = 1;
3208 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003209
Jim Laskeyef42a012006-11-02 20:12:39 +00003210 // Construct rows of the address, source, line, column matrix.
3211 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3212 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003213 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00003214 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003215
Jim Laskey1a4a83c2007-01-25 15:45:58 +00003216 unsigned SourceID = LineInfo.getSourceID();
3217 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
3218 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng6547e402008-07-01 23:18:29 +00003219 if (VerboseAsm)
3220 Asm->EOL(Directories[DirectoryID]
3221 + SourceFile.getName()
3222 + ":"
3223 + utostr_32(LineInfo.getLine()));
3224 else
3225 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003226
3227 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003228 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00003229 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003230 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00003231 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003232
Jim Laskeyef42a012006-11-02 20:12:39 +00003233 // If change of source, then switch to the new source.
3234 if (Source != LineInfo.getSourceID()) {
3235 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003236 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
3237 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00003238 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003239
Jim Laskeyef42a012006-11-02 20:12:39 +00003240 // If change of line.
3241 if (Line != LineInfo.getLine()) {
3242 // Determine offset.
3243 int Offset = LineInfo.getLine() - Line;
3244 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003245
Jim Laskeyef42a012006-11-02 20:12:39 +00003246 // Update line.
3247 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003248
Jim Laskeyef42a012006-11-02 20:12:39 +00003249 // If delta is small enough and in range...
3250 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3251 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003252 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00003253 } else {
3254 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003255 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
3256 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
3257 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00003258 }
3259 } else {
3260 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003261 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00003262 }
3263 }
3264
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003265 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00003266 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003267
3268 if (SecSrcLinesSize == 0)
3269 // Because we're emitting a debug_line section, we still need a line
3270 // table. The linker and friends expect it to exist. If there's nothing to
3271 // put into it, emit an empty table.
3272 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003273
Jim Laskeyef42a012006-11-02 20:12:39 +00003274 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003275
Jim Laskeybacd3042007-02-21 22:48:45 +00003276 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003277 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003278
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003279 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00003280 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003281 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00003282 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00003283 return;
3284
3285 int stackGrowth =
3286 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3287 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003288 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00003289
3290 // Start the dwarf frame section.
3291 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
3292
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003293 EmitLabel("debug_frame_common", 0);
3294 EmitDifference("debug_frame_common_end", 0,
3295 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003296 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00003297
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003298 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003299 Asm->EmitInt32((int)DW_CIE_ID);
3300 Asm->EOL("CIE Identifier Tag");
3301 Asm->EmitInt8(DW_CIE_VERSION);
3302 Asm->EOL("CIE Version");
3303 Asm->EmitString("");
3304 Asm->EOL("CIE Augmentation");
3305 Asm->EmitULEB128Bytes(1);
3306 Asm->EOL("CIE Code Alignment Factor");
3307 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003308 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003309 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003310 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003311
Jim Laskey5e73d5b2007-01-24 18:45:13 +00003312 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00003313 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003314
Dale Johannesenb97aec62007-11-13 19:13:01 +00003315 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00003316
Evan Cheng05548eb2008-02-29 19:36:59 +00003317 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003318 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003319
Jim Laskeybacd3042007-02-21 22:48:45 +00003320 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003321 }
3322
Jim Laskey65195462006-10-30 13:35:07 +00003323 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
3324 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003325 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00003326 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00003327 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003328
Jim Laskeyef42a012006-11-02 20:12:39 +00003329 // Start the dwarf frame section.
3330 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003331
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003332 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
3333 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003334 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003335
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003336 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003337
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003338 EmitSectionOffset("debug_frame_common", "section_debug_frame",
3339 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003340 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00003341
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003342 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003343 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003344 EmitDifference("func_end", DebugFrameInfo.Number,
3345 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003346 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003347
Dale Johannesenb97aec62007-11-13 19:13:01 +00003348 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003349
Evan Cheng05548eb2008-02-29 19:36:59 +00003350 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003351 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00003352
Jim Laskeybacd3042007-02-21 22:48:45 +00003353 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003354 }
3355
3356 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00003357 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003358 void EmitDebugPubNames() {
3359 // Start the dwarf pubnames section.
3360 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003361
3362 CompileUnit *Unit = GetBaseCompileUnit();
3363
Jim Laskey5496f012006-11-09 14:52:14 +00003364 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003365 "pubnames_begin", Unit->getID(), true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003366 Asm->EOL("Length of Public Names Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003367
Jim Laskey5496f012006-11-09 14:52:14 +00003368 EmitLabel("pubnames_begin", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003369
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003370 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003371
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00003372 EmitSectionOffset("info_begin", "section_info",
3373 Unit->getID(), 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003374 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00003375
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003376 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003377 Asm->EOL("Compilation Unit Length");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003378
Jim Laskey5496f012006-11-09 14:52:14 +00003379 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003380
Jim Laskey5496f012006-11-09 14:52:14 +00003381 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
3382 GE = Globals.end();
3383 GI != GE; ++GI) {
3384 const std::string &Name = GI->first;
3385 DIE * Entity = GI->second;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003386
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003387 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
3388 Asm->EmitString(Name); Asm->EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00003389 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003390
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003391 Asm->EmitInt32(0); Asm->EOL("End Mark");
Jim Laskey5496f012006-11-09 14:52:14 +00003392 EmitLabel("pubnames_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003393
Jim Laskeybacd3042007-02-21 22:48:45 +00003394 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003395 }
3396
3397 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00003398 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003399 void EmitDebugStr() {
3400 // Check to see if it is worth the effort.
3401 if (!StringPool.empty()) {
3402 // Start the dwarf str section.
3403 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003404
Jim Laskeyef42a012006-11-02 20:12:39 +00003405 // For each of strings in the string pool.
3406 for (unsigned StringID = 1, N = StringPool.size();
3407 StringID <= N; ++StringID) {
3408 // Emit a label for reference from debug information entries.
3409 EmitLabel("string", StringID);
3410 // Emit the string itself.
3411 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00003412 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003413 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003414
Jim Laskeybacd3042007-02-21 22:48:45 +00003415 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003416 }
3417 }
3418
3419 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00003420 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003421 void EmitDebugLoc() {
3422 // Start the dwarf loc section.
3423 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003424
Jim Laskeybacd3042007-02-21 22:48:45 +00003425 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003426 }
3427
3428 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00003429 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003430 void EmitDebugARanges() {
3431 // Start the dwarf aranges section.
3432 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003433
Jim Laskeyef42a012006-11-02 20:12:39 +00003434 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00003435#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003436 CompileUnit *Unit = GetBaseCompileUnit();
3437
Jim Laskey5496f012006-11-09 14:52:14 +00003438 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003439 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003440
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003441 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003442
Jim Laskey5496f012006-11-09 14:52:14 +00003443 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003444 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00003445
Dan Gohman82482942007-09-27 23:12:31 +00003446 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00003447
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003448 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00003449
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003450 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
3451 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00003452
Jim Laskey5496f012006-11-09 14:52:14 +00003453 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003454 EmitReference("text_begin", 0); Asm->EOL("Address");
3455 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00003456
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003457 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
3458 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00003459#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003460
Jim Laskeybacd3042007-02-21 22:48:45 +00003461 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003462 }
3463
3464 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00003465 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003466 void EmitDebugRanges() {
3467 // Start the dwarf ranges section.
3468 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003469
Jim Laskeybacd3042007-02-21 22:48:45 +00003470 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003471 }
3472
3473 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00003474 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003475 void EmitDebugMacInfo() {
3476 // Start the dwarf macinfo section.
3477 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003478
Jim Laskeybacd3042007-02-21 22:48:45 +00003479 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003480 }
3481
Devang Patelc4523242009-01-05 23:11:11 +00003482 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00003483 void ConstructCompileUnits() {
3484 std::string CUName = "llvm.dbg.compile_units";
3485 std::vector<GlobalVariable*> Result;
3486 getGlobalVariablesUsing(*M, CUName, Result);
3487 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3488 RE = Result.end(); RI != RE; ++RI) {
3489 DICompileUnit *DIUnit = new DICompileUnit(*RI);
Devang Patel9f8fcfc2009-01-08 17:19:22 +00003490 unsigned ID = RecordSource(DIUnit->getDirectory(),
3491 DIUnit->getFilename());
Devang Pateld1ca9252009-01-05 23:03:32 +00003492
3493 DIE *Die = new DIE(DW_TAG_compile_unit);
3494 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
3495 DWLabel("section_line", 0), DWLabel("section_line", 0),
3496 false);
3497 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit->getProducer());
3498 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit->getLanguage());
3499 AddString(Die, DW_AT_name, DW_FORM_string, DIUnit->getFilename());
3500 if (!DIUnit->getDirectory().empty())
3501 AddString(Die, DW_AT_comp_dir, DW_FORM_string, DIUnit->getDirectory());
3502
3503 CompileUnit *Unit = new CompileUnit(ID, Die);
3504 DW_CUs[DIUnit->getGV()] = Unit;
3505 }
3506 }
3507
Jim Laskey65195462006-10-30 13:35:07 +00003508 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
3509 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00003510 void ConstructCompileUnitDIEs() {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003511 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003512
Jim Laskeyef42a012006-11-02 20:12:39 +00003513 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003514 unsigned ID = MMI->RecordSource(CUW[i]);
Jim Laskey9d4209f2006-11-07 19:33:46 +00003515 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00003516 CompileUnits.push_back(Unit);
3517 }
3518 }
3519
Devang Patelc4523242009-01-05 23:11:11 +00003520 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
3521 /// visible global variables.
3522 void ConstructGlobalVariableDIEs() {
3523 std::string GVName = "llvm.dbg.global_variables";
3524 std::vector<GlobalVariable*> Result;
3525 getGlobalVariablesUsing(*M, GVName, Result);
3526 for (std::vector<GlobalVariable *>::iterator GVI = Result.begin(),
3527 GVE = Result.end(); GVI != GVE; ++GVI) {
3528 DIGlobalVariable *DI_GV = new DIGlobalVariable(*GVI);
3529 CompileUnit *DW_Unit = FindCompileUnit(DI_GV->getCompileUnit());
3530
3531 // Check for pre-existence.
3532 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV->getGV());
3533 if (Slot) continue;
3534
3535 DIE *VariableDie = new DIE(DW_TAG_variable);
3536 AddString(VariableDie, DW_AT_name, DW_FORM_string, DI_GV->getName());
3537 const std::string &LinkageName = DI_GV->getLinkageName();
3538 if (!LinkageName.empty())
3539 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3540 LinkageName);
3541 AddType(DW_Unit, VariableDie, DI_GV->getType());
3542
3543 if (!DI_GV->isLocalToUnit())
3544 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
3545
3546 // Add source line info, if available.
3547 AddSourceLine(VariableDie, DI_GV);
3548
3549 // Add address.
3550 DIEBlock *Block = new DIEBlock();
3551 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
3552 AddObjectLabel(Block, 0, DW_FORM_udata,
3553 Asm->getGlobalLinkName(DI_GV->getGV()));
3554 AddBlock(VariableDie, DW_AT_location, 0, Block);
3555
3556 //Add to map.
3557 Slot = VariableDie;
3558
3559 //Add to context owner.
3560 DW_Unit->getDie()->AddChild(VariableDie);
3561
3562 //Expose as global. FIXME - need to check external flag.
3563 DW_Unit->AddGlobal(DI_GV->getName(), VariableDie);
3564 }
3565 }
3566
Jim Laskey65195462006-10-30 13:35:07 +00003567 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
3568 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00003569 void ConstructGlobalDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00003570 std::vector<GlobalVariableDesc *> GlobalVariables;
3571 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M, GlobalVariables);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003572
Bill Wendling10fff602008-07-03 22:53:42 +00003573 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
3574 GlobalVariableDesc *GVD = GlobalVariables[i];
3575 NewGlobalVariable(GVD);
3576 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003577 }
Jim Laskey65195462006-10-30 13:35:07 +00003578
Devang Patel78eb6ad2009-01-05 23:21:35 +00003579 /// ConstructSubprograms - Create DIEs for each of the externally visible
3580 /// subprograms.
3581 void ConstructSubprograms() {
3582
3583 std::string SPName = "llvm.dbg.subprograms";
3584 std::vector<GlobalVariable*> Result;
3585 getGlobalVariablesUsing(*M, SPName, Result);
3586 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3587 RE = Result.end(); RI != RE; ++RI) {
3588
3589 DISubprogram *SP = new DISubprogram(*RI);
3590 CompileUnit *Unit = FindCompileUnit(SP->getCompileUnit());
3591
3592 // Check for pre-existence.
3593 DIE *&Slot = Unit->getDieMapSlotFor(SP->getGV());
3594 if (Slot) continue;
3595
3596 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
3597 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, SP->getName());
3598 const std::string &LinkageName = SP->getLinkageName();
3599 if (!LinkageName.empty())
3600 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3601 LinkageName);
3602 DIType SPTy = SP->getType();
3603 AddType(Unit, SubprogramDie, SPTy);
3604 if (!SP->isLocalToUnit())
3605 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
3606 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
3607
3608 AddSourceLine(SubprogramDie, SP);
3609 //Add to map.
3610 Slot = SubprogramDie;
3611 //Add to context owner.
3612 Unit->getDie()->AddChild(SubprogramDie);
3613 //Expose as global.
3614 Unit->AddGlobal(SP->getName(), SubprogramDie);
3615 }
3616 }
3617
Jim Laskey65195462006-10-30 13:35:07 +00003618 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
3619 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00003620 void ConstructSubprogramDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00003621 std::vector<SubprogramDesc *> Subprograms;
3622 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003623
Bill Wendling10fff602008-07-03 22:53:42 +00003624 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
3625 SubprogramDesc *SPD = Subprograms[i];
3626 NewSubprogram(SPD);
3627 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003628 }
Jim Laskey65195462006-10-30 13:35:07 +00003629
Jim Laskey65195462006-10-30 13:35:07 +00003630public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003631 //===--------------------------------------------------------------------===//
3632 // Main entry points.
3633 //
Owen Andersoncb371882008-08-21 00:14:44 +00003634 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00003635 : Dwarf(OS, A, T, "dbg")
Jim Laskeyef42a012006-11-02 20:12:39 +00003636 , CompileUnits()
3637 , AbbreviationsSet(InitAbbreviationsSetSize)
3638 , Abbreviations()
3639 , ValuesSet(InitValuesSetSize)
3640 , Values()
3641 , StringPool()
3642 , DescToUnitMap()
3643 , SectionMap()
3644 , SectionSourceLines()
Jim Laskeybacd3042007-02-21 22:48:45 +00003645 , didInitial(false)
3646 , shouldEmit(false)
Devang Patel7a6e5a32009-01-08 02:33:41 +00003647 , RootDbgScope(NULL)
Jim Laskeyef42a012006-11-02 20:12:39 +00003648 {
3649 }
Jim Laskey072200c2007-01-29 18:51:14 +00003650 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003651 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
3652 delete CompileUnits[i];
3653 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3654 delete Values[j];
3655 }
3656
Devang Patel5d315982009-01-06 21:07:30 +00003657 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3658 /// This is inovked by the target AsmPrinter.
3659 void SetDebugInfo() {
3660 // FIXME - Check if the module has debug info or not.
3661 // Create all the compile unit DIEs.
3662 ConstructCompileUnits();
3663
3664 // Create DIEs for each of the externally visible global variables.
3665 ConstructGlobalVariableDIEs();
3666
3667 // Create DIEs for each of the externally visible subprograms.
3668 ConstructSubprograms();
3669
3670 // Prime section data.
3671 SectionMap.insert(TAI->getTextSection());
3672
3673 // Print out .file directives to specify files for .loc directives. These
3674 // are printed out early so that they precede any .loc directives.
3675 if (TAI->hasDotLocAndDotFile()) {
3676 for (unsigned i = 1, e = SrcFiles.size(); i <= e; ++i) {
3677 sys::Path FullPath(Directories[SrcFiles[i].getDirectoryID()]);
3678 bool AppendOk = FullPath.appendComponent(SrcFiles[i].getName());
3679 assert(AppendOk && "Could not append filename to directory!");
3680 AppendOk = false;
3681 Asm->EmitFile(i, FullPath.toString());
3682 Asm->EOL();
3683 }
3684 }
3685
3686 // Emit initial sections
3687 EmitInitial();
3688 }
3689
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003690 /// SetModuleInfo - Set machine module information when it's known that pass
3691 /// manager has created it. Set by the target AsmPrinter.
3692 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003693 // Make sure initial declarations are made.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003694 if (!MMI && mmi->hasDebugInfo()) {
3695 MMI = mmi;
Jim Laskeyef42a012006-11-02 20:12:39 +00003696 shouldEmit = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003697
Jim Laskeyef42a012006-11-02 20:12:39 +00003698 // Create all the compile unit DIEs.
3699 ConstructCompileUnitDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003700
Jim Laskeyef42a012006-11-02 20:12:39 +00003701 // Create DIEs for each of the externally visible global variables.
3702 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00003703
Jim Laskeyef42a012006-11-02 20:12:39 +00003704 // Create DIEs for each of the externally visible subprograms.
3705 ConstructSubprogramDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003706
Jim Laskeyef42a012006-11-02 20:12:39 +00003707 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00003708 SectionMap.insert(TAI->getTextSection());
Dan Gohmand57c3882007-10-01 22:40:20 +00003709
3710 // Print out .file directives to specify files for .loc directives. These
3711 // are printed out early so that they precede any .loc directives.
3712 if (TAI->hasDotLocAndDotFile()) {
3713 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
3714 const UniqueVector<std::string> &Directories = MMI->getDirectories();
3715 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
3716 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
3717 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
3718 assert(AppendOk && "Could not append filename to directory!");
Devang Pateld0935c32008-12-23 21:55:38 +00003719 AppendOk = false;
Dan Gohmand57c3882007-10-01 22:40:20 +00003720 Asm->EmitFile(i, FullPath.toString());
3721 Asm->EOL();
3722 }
3723 }
3724
3725 // Emit initial sections
3726 EmitInitial();
Jim Laskeyef42a012006-11-02 20:12:39 +00003727 }
3728 }
3729
Jim Laskey65195462006-10-30 13:35:07 +00003730 /// BeginModule - Emit all Dwarf sections that should come prior to the
3731 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003732 void BeginModule(Module *M) {
3733 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003734 }
3735
Jim Laskey65195462006-10-30 13:35:07 +00003736 /// EndModule - Emit all Dwarf sections that should come after the content.
3737 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003738 void EndModule() {
3739 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003740
Jim Laskeyef42a012006-11-02 20:12:39 +00003741 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003742 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003743 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003744 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003745 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003746
Jim Laskeyef42a012006-11-02 20:12:39 +00003747 // End text sections.
3748 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003749 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003750 EmitLabel("section_end", i);
3751 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003752
3753 // Emit common frame information.
3754 EmitCommonDebugFrame();
3755
3756 // Emit function debug frame information
3757 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3758 E = DebugFrames.end(); I != E; ++I)
3759 EmitFunctionDebugFrame(*I);
3760
Jim Laskeyef42a012006-11-02 20:12:39 +00003761 // Compute DIE offsets and sizes.
3762 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003763
Jim Laskeyef42a012006-11-02 20:12:39 +00003764 // Emit all the DIEs into a debug info section
3765 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003766
Jim Laskeyef42a012006-11-02 20:12:39 +00003767 // Corresponding abbreviations into a abbrev section.
3768 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003769
Jim Laskeyef42a012006-11-02 20:12:39 +00003770 // Emit source line correspondence into a debug line section.
3771 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003772
Jim Laskeyef42a012006-11-02 20:12:39 +00003773 // Emit info into a debug pubnames section.
3774 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003775
Jim Laskeyef42a012006-11-02 20:12:39 +00003776 // Emit info into a debug str section.
3777 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003778
Jim Laskeyef42a012006-11-02 20:12:39 +00003779 // Emit info into a debug loc section.
3780 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003781
Jim Laskeyef42a012006-11-02 20:12:39 +00003782 // Emit info into a debug aranges section.
3783 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003784
Jim Laskeyef42a012006-11-02 20:12:39 +00003785 // Emit info into a debug ranges section.
3786 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003787
Jim Laskeyef42a012006-11-02 20:12:39 +00003788 // Emit info into a debug macinfo section.
3789 EmitDebugMacInfo();
3790 }
3791
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003792 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003793 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003794 void BeginFunction(MachineFunction *MF) {
3795 this->MF = MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003796
Jim Laskeyef42a012006-11-02 20:12:39 +00003797 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003798
3799 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003800 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003801
Jim Laskeyef42a012006-11-02 20:12:39 +00003802 // Assumes in correct section after the entry point.
3803 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003804
3805 // Emit label for the implicitly defined dbg.stoppoint at the start of
3806 // the function.
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003807 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
3808 if (!LineInfos.empty()) {
3809 const SourceLineInfo &LineInfo = LineInfos[0];
3810 Asm->printLabel(LineInfo.getLabelID());
3811 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003812 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003813
Jim Laskey65195462006-10-30 13:35:07 +00003814 /// EndFunction - Gather and emit post-function debug information.
3815 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003816 void EndFunction(MachineFunction *MF) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003817 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003818
Jim Laskeyb82313f2007-02-01 16:31:34 +00003819 // Define end label for subprogram.
3820 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003821
Jim Laskeyef42a012006-11-02 20:12:39 +00003822 // Get function line info.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003823 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
Jim Laskeyef42a012006-11-02 20:12:39 +00003824
3825 if (!LineInfos.empty()) {
3826 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003827 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003828 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
3829 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
3830 // Append the function info to section info.
3831 SectionLineInfos.insert(SectionLineInfos.end(),
3832 LineInfos.begin(), LineInfos.end());
3833 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003834
Jim Laskeyef42a012006-11-02 20:12:39 +00003835 // Construct scopes for subprogram.
Bill Wendlingd751c642008-09-26 00:28:12 +00003836 if (MMI->getRootScope())
3837 ConstructRootScope(MMI->getRootScope());
3838 else
3839 // FIXME: This is wrong. We are essentially getting past a problem with
3840 // debug information not being able to handle unreachable blocks that have
3841 // debug information in them. In particular, those unreachable blocks that
3842 // have "region end" info in them. That situation results in the "root
3843 // scope" not being created. If that's the case, then emit a "default"
3844 // scope, i.e., one that encompasses the whole function. This isn't
3845 // desirable. And a better way of handling this (and all of the debugging
3846 // information) needs to be explored.
3847 ConstructDefaultScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003848
3849 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3850 MMI->getFrameMoves()));
Jim Laskeyef42a012006-11-02 20:12:39 +00003851 }
Jim Laskey65195462006-10-30 13:35:07 +00003852};
3853
Jim Laskey072200c2007-01-29 18:51:14 +00003854//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003855/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003856///
3857class DwarfException : public Dwarf {
3858
Jim Laskeyb82313f2007-02-01 16:31:34 +00003859private:
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003860 struct FunctionEHFrameInfo {
3861 std::string FnName;
3862 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003863 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003864 bool hasCalls;
3865 bool hasLandingPads;
3866 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003867 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003868
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003869 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3870 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003871 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003872 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003873 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003874 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003875 };
3876
3877 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003878
3879 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3880 /// be emitted.
3881 bool shouldEmitTable;
3882
3883 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3884 /// should be emitted.
3885 bool shouldEmitMoves;
3886
3887 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3888 /// should be emitted.
3889 bool shouldEmitTableModule;
3890
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003891 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003892 /// should be emitted.
3893 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003894
Jim Laskey3f09fc22007-02-28 18:38:31 +00003895 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3896 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003897 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003898 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003899 int stackGrowth =
3900 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3901 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003902 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003903
Jim Laskeybacd3042007-02-21 22:48:45 +00003904 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003905 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003906
3907 if (!TAI->doesRequireNonLocalEHFrameLabel())
3908 O << TAI->getEHGlobalPrefix();
3909 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003910 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003911
Jim Laskeybacd3042007-02-21 22:48:45 +00003912 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003913 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003914
Jim Laskeybacd3042007-02-21 22:48:45 +00003915 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003916 EmitDifference("eh_frame_common_end", Index,
3917 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003918 Asm->EOL("Length of Common Information Entry");
3919
Jim Laskeybacd3042007-02-21 22:48:45 +00003920 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003921 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003922 Asm->EmitInt32((int)0);
3923 Asm->EOL("CIE Identifier Tag");
3924 Asm->EmitInt8(DW_CIE_VERSION);
3925 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003926
Jim Laskeybacd3042007-02-21 22:48:45 +00003927 // The personality presence indicates that language specific information
3928 // will show up in the eh frame.
3929 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003930 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003931
Jim Laskeybacd3042007-02-21 22:48:45 +00003932 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003933 Asm->EmitULEB128Bytes(1);
3934 Asm->EOL("CIE Code Alignment Factor");
3935 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003936 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003937 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003938 Asm->EOL("CIE Return Address Column");
3939
Jim Laskeybacd3042007-02-21 22:48:45 +00003940 // If there is a personality, we need to indicate the functions location.
3941 if (Personality) {
3942 Asm->EmitULEB128Bytes(7);
3943 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003944
Duncan Sands671fa972008-05-07 19:11:09 +00003945 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003946 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003947 Asm->EOL("Personality (pcrel sdata4 indirect)");
3948 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003949 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003950 Asm->EOL("Personality (pcrel sdata4)");
3951 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003952
Duncan Sands671fa972008-05-07 19:11:09 +00003953 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003954 O << TAI->getPersonalityPrefix();
3955 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3956 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003957 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3958 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003959 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003960
Duncan Sands671fa972008-05-07 19:11:09 +00003961 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3962 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003963
Bill Wendlingd60de512009-01-05 22:53:45 +00003964 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3965 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003966 } else {
3967 Asm->EmitULEB128Bytes(1);
3968 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003969
Bill Wendlingd60de512009-01-05 22:53:45 +00003970 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3971 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003972 }
3973
3974 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003975 std::vector<MachineMove> Moves;
3976 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003977 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003978
Dale Johannesen21d972a2008-04-30 00:43:29 +00003979 // On Darwin the linker honors the alignment of eh_frame, which means it
3980 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3981 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003982 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003983 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003984 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003985
Jim Laskeybacd3042007-02-21 22:48:45 +00003986 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003987 }
Duncan Sands671fa972008-05-07 19:11:09 +00003988
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003989 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003990 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003991 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003992 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3993
Jim Laskeybacd3042007-02-21 22:48:45 +00003994 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3995
3996 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003997 // If the corresponding function is static, this should not be
3998 // externally visible.
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003999 if (linkage != Function::InternalLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00004000 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
4001 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
4002 }
4003
Dale Johannesen038129d2008-01-10 02:03:30 +00004004 // If corresponding function is weak definition, this should be too.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004005 if ((linkage == Function::WeakLinkage ||
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004006 linkage == Function::LinkOnceLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00004007 TAI->getWeakDefDirective())
4008 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
4009
4010 // If there are no calls then you can't unwind. This may mean we can
4011 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004012 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00004013 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00004014 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00004015 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00004016 !UnwindTablesMandatory &&
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004017 ((linkage != Function::WeakLinkage &&
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004018 linkage != Function::LinkOnceLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00004019 !TAI->getWeakDefDirective() ||
4020 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004021 {
Bill Wendling6e198962007-09-18 01:47:22 +00004022 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004023 // This name has no connection to the function, so it might get
4024 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004025 // dead-stripping unconditionally.
4026 if (const char *UsedDirective = TAI->getUsedDirective())
4027 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00004028 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00004029 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00004030
Jim Laskeybacd3042007-02-21 22:48:45 +00004031 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004032 EmitDifference("eh_frame_end", EHFrameInfo.Number,
4033 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00004034 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004035
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004036 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00004037
Bill Wendling722f5f12008-12-24 08:05:17 +00004038 if (TAI->doesRequireNonLocalEHFrameLabel()) {
4039 PrintRelDirective(true, true);
4040 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
4041
4042 if (!TAI->isAbsoluteEHSectionOffsets())
4043 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
4044 } else {
4045 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
4046 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
4047 true, true, false);
4048 }
4049
Jim Laskeybacd3042007-02-21 22:48:45 +00004050 Asm->EOL("FDE CIE offset");
4051
Bill Wendling5fe1fac2009-01-06 19:13:55 +00004052 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00004053 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004054 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00004055 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00004056 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00004057
Jim Laskey3f09fc22007-02-28 18:38:31 +00004058 // If there is a personality and landing pads then point to the language
4059 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004060 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00004061 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00004062 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00004063
4064 if (EHFrameInfo.hasLandingPads)
4065 EmitReference("exception", EHFrameInfo.Number, true, true);
4066 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00004067 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00004068 Asm->EOL("Language Specific Data Area");
4069 } else {
4070 Asm->EmitULEB128Bytes(0);
4071 Asm->EOL("Augmentation size");
4072 }
Duncan Sands671fa972008-05-07 19:11:09 +00004073
Jim Laskeybacd3042007-02-21 22:48:45 +00004074 // Indicate locations of function specific callee saved registers in
4075 // frame.
Dale Johannesenb97aec62007-11-13 19:13:01 +00004076 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004077
Dale Johannesen21d972a2008-04-30 00:43:29 +00004078 // On Darwin the linker honors the alignment of eh_frame, which means it
4079 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
4080 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004081 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00004082 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004083 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004084
4085 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004086 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004087 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004088 // that depends on unused functions (calling undefined externals) being
4089 // dead-stripped to link correctly. Yes, there really is.
4090 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
4091 if (const char *UsedDirective = TAI->getUsedDirective())
4092 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
4093 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004094 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00004095
Duncan Sands57810cd2007-09-05 11:27:52 +00004096 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004097 ///
4098 /// The general organization of the table is complex, but the basic concepts
4099 /// are easy. First there is a header which describes the location and
4100 /// organization of the three components that follow.
4101 /// 1. The landing pad site information describes the range of code covered
4102 /// by the try. In our case it's an accumulation of the ranges covered
4103 /// by the invokes in the try. There is also a reference to the landing
4104 /// pad that handles the exception once processed. Finally an index into
4105 /// the actions table.
4106 /// 2. The action table, in our case, is composed of pairs of type ids
4107 /// and next action offset. Starting with the action index from the
4108 /// landing pad site, each type Id is checked for a match to the current
4109 /// exception. If it matches then the exception and type id are passed
4110 /// on to the landing pad. Otherwise the next action is looked up. This
4111 /// chain is terminated with a next action of zero. If no type id is
4112 /// found the the frame is unwound and handling continues.
4113 /// 3. Type id table contains references to all the C++ typeinfo for all
4114 /// catches in the function. This tables is reversed indexed base 1.
4115
Duncan Sandsb32edb42007-06-06 15:37:31 +00004116 /// SharedTypeIds - How many leading type ids two landing pads have in common.
4117 static unsigned SharedTypeIds(const LandingPadInfo *L,
4118 const LandingPadInfo *R) {
4119 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004120 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004121 unsigned MinSize = LSize < RSize ? LSize : RSize;
4122 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004123
Duncan Sandsb32edb42007-06-06 15:37:31 +00004124 for (; Count != MinSize; ++Count)
4125 if (LIds[Count] != RIds[Count])
4126 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004127
Duncan Sandsb32edb42007-06-06 15:37:31 +00004128 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004129 }
4130
Duncan Sandsb32edb42007-06-06 15:37:31 +00004131 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00004132 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004133 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004134 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004135 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004136
Duncan Sandsb32edb42007-06-06 15:37:31 +00004137 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00004138 if (LIds[i] != RIds[i])
4139 return LIds[i] < RIds[i];
4140
Duncan Sandsb32edb42007-06-06 15:37:31 +00004141 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004142 }
4143
Duncan Sands53c3a332007-05-16 12:12:23 +00004144 struct KeyInfo {
4145 static inline unsigned getEmptyKey() { return -1U; }
4146 static inline unsigned getTombstoneKey() { return -2U; }
4147 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00004148 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00004149 static bool isPod() { return true; }
4150 };
4151
Duncan Sands57810cd2007-09-05 11:27:52 +00004152 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004153 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004154 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004155 int NextAction;
4156 struct ActionEntry *Previous;
4157 };
4158
Duncan Sands57810cd2007-09-05 11:27:52 +00004159 /// PadRange - Structure holding a try-range and the associated landing pad.
4160 struct PadRange {
4161 // The index of the landing pad.
4162 unsigned PadIndex;
4163 // The index of the begin and end labels in the landing pad's label lists.
4164 unsigned RangeIndex;
4165 };
4166
4167 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
4168
4169 /// CallSiteEntry - Structure describing an entry in the call-site table.
4170 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00004171 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004172 unsigned BeginLabel; // zero indicates the start of the function.
4173 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00004174 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004175 unsigned PadLabel; // zero indicates that there is no landing pad.
4176 unsigned Action;
4177 };
4178
Jim Laskeybacd3042007-02-21 22:48:45 +00004179 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00004180 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00004181 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00004182 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
4183 if (PadInfos.empty()) return;
4184
4185 // Sort the landing pads in order of their type ids. This is used to fold
4186 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00004187 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00004188 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00004189 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00004190 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004191 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
4192
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004193 // Negative type ids index into FilterIds, positive type ids index into
4194 // TypeInfos. The value written for a positive type id is just the type
4195 // id itself. For a negative type id, however, the value written is the
4196 // (negative) byte offset of the corresponding FilterIds entry. The byte
4197 // offset is usually equal to the type id, because the FilterIds entries
4198 // are written using a variable width encoding which outputs one byte per
4199 // entry as long as the value written is not too large, but can differ.
4200 // This kind of complication does not occur for positive type ids because
4201 // type infos are output using a fixed width encoding.
4202 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
4203 SmallVector<int, 16> FilterOffsets;
4204 FilterOffsets.reserve(FilterIds.size());
4205 int Offset = -1;
4206 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
4207 E = FilterIds.end(); I != E; ++I) {
4208 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004209 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004210 }
4211
Duncan Sands57810cd2007-09-05 11:27:52 +00004212 // Compute the actions table and gather the first action index for each
4213 // landing pad site.
4214 SmallVector<ActionEntry, 32> Actions;
4215 SmallVector<unsigned, 64> FirstActions;
4216 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00004217
Duncan Sandsb32edb42007-06-06 15:37:31 +00004218 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00004219 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004220 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004221 const LandingPadInfo *LP = LandingPads[i];
4222 const std::vector<int> &TypeIds = LP->TypeIds;
4223 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004224 unsigned SizeSiteActions = 0;
4225
Duncan Sandsb32edb42007-06-06 15:37:31 +00004226 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00004227 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00004228 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004229
Duncan Sandsb32edb42007-06-06 15:37:31 +00004230 if (NumShared) {
4231 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
4232 assert(Actions.size());
4233 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004234 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
4235 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004236 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004237 SizeAction -=
4238 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004239 SizeAction += -PrevAction->NextAction;
4240 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004241 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00004242 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00004243
Duncan Sandsb32edb42007-06-06 15:37:31 +00004244 // Compute the actions.
4245 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
4246 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004247 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4248 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004249 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004250
4251 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004252 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004253 SizeSiteActions += SizeAction;
4254
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004255 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00004256 Actions.push_back(Action);
4257
4258 PrevAction = &Actions.back();
4259 }
4260
4261 // Record the first action of the landing pad site.
4262 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4263 } // else identical - re-use previous FirstAction
4264
4265 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004266
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00004267 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00004268 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00004269 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004270
Duncan Sands481dc722007-12-19 07:36:31 +00004271 // Compute the call-site table. The entry for an invoke has a try-range
4272 // containing the call, a non-zero landing pad and an appropriate action.
4273 // The entry for an ordinary call has a try-range containing the call and
4274 // zero for the landing pad and the action. Calls marked 'nounwind' have
4275 // no entry and must not be contained in the try-range of any entry - they
4276 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004277 SmallVector<CallSiteEntry, 64> CallSites;
4278
4279 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00004280 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4281 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4282 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00004283 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4284 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00004285 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004286 unsigned BeginLabel = LandingPad->BeginLabels[j];
4287 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4288 PadRange P = { i, j };
4289 PadMap[BeginLabel] = P;
4290 }
4291 }
4292
Duncan Sands481dc722007-12-19 07:36:31 +00004293 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00004294 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00004295
4296 // Whether there is a potentially throwing instruction (currently this means
4297 // an ordinary call) between the end of the previous try-range and now.
4298 bool SawPotentiallyThrowing = false;
4299
4300 // Whether the last callsite entry was for an invoke.
4301 bool PreviousIsInvoke = false;
4302
Duncan Sands481dc722007-12-19 07:36:31 +00004303 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004304 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4305 I != E; ++I) {
4306 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4307 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00004308 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00004309 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00004310 continue;
4311 }
4312
Chris Lattner9e330492007-12-30 20:50:28 +00004313 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00004314 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00004315
Duncan Sands481dc722007-12-19 07:36:31 +00004316 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00004317 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00004318 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004319
Duncan Sands481dc722007-12-19 07:36:31 +00004320 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00004321 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00004322 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00004323 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00004324 continue;
4325
4326 PadRange P = L->second;
4327 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4328
4329 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4330 "Inconsistent landing pad map!");
4331
4332 // If some instruction between the previous try-range and this one may
4333 // throw, create a call-site entry with no landing pad for the region
4334 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00004335 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004336 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4337 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00004338 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004339 }
4340
4341 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00004342 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00004343
Duncan Sands481dc722007-12-19 07:36:31 +00004344 if (LandingPad->LandingPadLabel) {
4345 // This try-range is for an invoke.
4346 CallSiteEntry Site = {BeginLabel, LastLabel,
4347 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00004348
Duncan Sands481dc722007-12-19 07:36:31 +00004349 // Try to merge with the previous call-site.
4350 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00004351 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00004352 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4353 // Extend the range of the previous entry.
4354 Prev.EndLabel = Site.EndLabel;
4355 continue;
4356 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004357 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004358
Duncan Sands481dc722007-12-19 07:36:31 +00004359 // Otherwise, create a new call-site.
4360 CallSites.push_back(Site);
4361 PreviousIsInvoke = true;
4362 } else {
4363 // Create a gap.
4364 PreviousIsInvoke = false;
4365 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004366 }
4367 }
4368 // If some instruction between the previous try-range and the end of the
4369 // function may throw, create a call-site entry with no landing pad for the
4370 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00004371 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004372 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4373 CallSites.push_back(Site);
4374 }
4375
Jim Laskeybacd3042007-02-21 22:48:45 +00004376 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00004377
4378 // Call sites.
4379 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4380 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4381 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4382 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4383 SiteLengthSize +
4384 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00004385 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004386 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00004387
Duncan Sands671fa972008-05-07 19:11:09 +00004388 // Type infos.
4389 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4390 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004391
4392 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004393 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004394 SizeSites + SizeActions + SizeTypes;
4395
4396 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4397 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004398 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004399 TypeOffset;
4400
4401 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004402
Jim Laskeybacd3042007-02-21 22:48:45 +00004403 // Begin the exception table.
4404 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00004405 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00004406 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004407 for (unsigned i = 0; i != SizeAlign; ++i) {
4408 Asm->EmitInt8(0);
4409 Asm->EOL("Padding");
4410 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004411 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004412
Jim Laskeybacd3042007-02-21 22:48:45 +00004413 // Emit the header.
4414 Asm->EmitInt8(DW_EH_PE_omit);
4415 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4416 Asm->EmitInt8(DW_EH_PE_absptr);
4417 Asm->EOL("TType format (DW_EH_PE_absptr)");
4418 Asm->EmitULEB128Bytes(TypeOffset);
4419 Asm->EOL("TType base offset");
4420 Asm->EmitInt8(DW_EH_PE_udata4);
4421 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4422 Asm->EmitULEB128Bytes(SizeSites);
4423 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004424
Duncan Sands57810cd2007-09-05 11:27:52 +00004425 // Emit the landing pad site information.
4426 for (unsigned i = 0; i < CallSites.size(); ++i) {
4427 CallSiteEntry &S = CallSites[i];
4428 const char *BeginTag;
4429 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00004430
Duncan Sands57810cd2007-09-05 11:27:52 +00004431 if (!S.BeginLabel) {
4432 BeginTag = "eh_func_begin";
4433 BeginNumber = SubprogramCount;
4434 } else {
4435 BeginTag = "label";
4436 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004437 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004438
Duncan Sands57810cd2007-09-05 11:27:52 +00004439 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004440 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004441 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004442
Duncan Sands57810cd2007-09-05 11:27:52 +00004443 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004444 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004445 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004446 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004447 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004448 }
4449 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004450
Duncan Sands671fa972008-05-07 19:11:09 +00004451 if (!S.PadLabel)
4452 Asm->EmitInt32(0);
4453 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004454 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004455 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004456 Asm->EOL("Landing pad");
4457
4458 Asm->EmitULEB128Bytes(S.Action);
4459 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004460 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004461
Jim Laskeybacd3042007-02-21 22:48:45 +00004462 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004463 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4464 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004465
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004466 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004467 Asm->EOL("TypeInfo index");
4468 Asm->EmitSLEB128Bytes(Action.NextAction);
4469 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004470 }
4471
4472 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004473 for (unsigned M = TypeInfos.size(); M; --M) {
4474 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004475
4476 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004477
4478 if (GV)
4479 O << Asm->getGlobalLinkName(GV);
4480 else
4481 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00004482
Jim Laskeybacd3042007-02-21 22:48:45 +00004483 Asm->EOL("TypeInfo");
4484 }
4485
Duncan Sands73ef58a2007-06-02 16:53:42 +00004486 // Emit the filter typeids.
4487 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4488 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004489 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004490 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004491 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004492
Evan Cheng05548eb2008-02-29 19:36:59 +00004493 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004494 }
4495
Jim Laskey072200c2007-01-29 18:51:14 +00004496public:
4497 //===--------------------------------------------------------------------===//
4498 // Main entry points.
4499 //
Owen Andersoncb371882008-08-21 00:14:44 +00004500 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00004501 : Dwarf(OS, A, T, "eh")
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004502 , shouldEmitTable(false)
4503 , shouldEmitMoves(false)
4504 , shouldEmitTableModule(false)
4505 , shouldEmitMovesModule(false)
Jim Laskey072200c2007-01-29 18:51:14 +00004506 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004507
Jim Laskey072200c2007-01-29 18:51:14 +00004508 virtual ~DwarfException() {}
4509
4510 /// SetModuleInfo - Set machine module information when it's known that pass
4511 /// manager has created it. Set by the target AsmPrinter.
4512 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004513 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004514 }
4515
4516 /// BeginModule - Emit all exception information that should come prior to the
4517 /// content.
4518 void BeginModule(Module *M) {
4519 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004520 }
4521
4522 /// EndModule - Emit all exception information that should come after the
4523 /// content.
4524 void EndModule() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004525 if (shouldEmitMovesModule || shouldEmitTableModule) {
4526 const std::vector<Function *> Personalities = MMI->getPersonalities();
4527 for (unsigned i =0; i < Personalities.size(); ++i)
4528 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004529
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004530 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4531 E = EHFrames.end(); I != E; ++I)
4532 EmitEHFrame(*I);
4533 }
Jim Laskey072200c2007-01-29 18:51:14 +00004534 }
4535
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004536 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004537 /// emitted immediately after the function entry point.
4538 void BeginFunction(MachineFunction *MF) {
4539 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004540 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesene0040622008-04-02 17:04:45 +00004541 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004542
4543 // Map all labels and get rid of any dead landing pads.
4544 MMI->TidyLandingPads();
4545 // If any landing pads survive, we need an EH table.
4546 if (MMI->getLandingPads().size())
4547 shouldEmitTable = true;
4548
4549 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004550 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004551 shouldEmitMoves = true;
4552
4553 if (shouldEmitMoves || shouldEmitTable)
4554 // Assumes in correct section after the entry point.
4555 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004556 }
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004557 shouldEmitTableModule |= shouldEmitTable;
4558 shouldEmitMovesModule |= shouldEmitMoves;
Jim Laskey072200c2007-01-29 18:51:14 +00004559 }
4560
4561 /// EndFunction - Gather and emit post-function exception information.
4562 ///
4563 void EndFunction() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004564 if (shouldEmitMoves || shouldEmitTable) {
4565 EmitLabel("eh_func_end", SubprogramCount);
4566 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004567
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004568 // Save EH frame information
4569 EHFrames.
4570 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling6e198962007-09-18 01:47:22 +00004571 SubprogramCount,
4572 MMI->getPersonalityIndex(),
4573 MF->getFrameInfo()->hasCalls(),
4574 !MMI->getLandingPads().empty(),
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00004575 MMI->getFrameMoves(),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004576 MF->getFunction()));
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004577 }
Jim Laskey072200c2007-01-29 18:51:14 +00004578 }
4579};
4580
Jim Laskey0d086af2006-02-27 12:43:29 +00004581} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004582
4583//===----------------------------------------------------------------------===//
4584
Jim Laskeyd18e2892006-01-20 20:34:06 +00004585/// Emit - Print the abbreviation using the specified Dwarf writer.
4586///
Jim Laskey072200c2007-01-29 18:51:14 +00004587void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004588 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004589 DD.getAsm()->EmitULEB128Bytes(Tag);
4590 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004591
Jim Laskeyd18e2892006-01-20 20:34:06 +00004592 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004593 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4594 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004595
Jim Laskeyd18e2892006-01-20 20:34:06 +00004596 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004597 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004598 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004599
Jim Laskeyd18e2892006-01-20 20:34:06 +00004600 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004601 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4602 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004603
Jim Laskeyd18e2892006-01-20 20:34:06 +00004604 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004605 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4606 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004607 }
4608
4609 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004610 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4611 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004612}
4613
4614#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004615void DIEAbbrev::print(std::ostream &O) {
4616 O << "Abbreviation @"
4617 << std::hex << (intptr_t)this << std::dec
4618 << " "
4619 << TagString(Tag)
4620 << " "
4621 << ChildrenString(ChildrenFlag)
4622 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004623
Jim Laskeya0f3d172006-09-07 22:06:40 +00004624 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4625 O << " "
4626 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004627 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004628 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004629 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004630 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004631}
Bill Wendlinge8156192006-12-07 01:30:32 +00004632void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004633#endif
4634
4635//===----------------------------------------------------------------------===//
4636
Jim Laskeyef42a012006-11-02 20:12:39 +00004637#ifndef NDEBUG
4638void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004639 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004640}
Jim Laskeyef42a012006-11-02 20:12:39 +00004641#endif
4642
4643//===----------------------------------------------------------------------===//
4644
Jim Laskey063e7652006-01-17 17:31:53 +00004645/// EmitValue - Emit integer of appropriate size.
4646///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004647void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004648 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004649 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004650 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004651 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004652 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004653 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004654 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004655 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004656 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004657 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4658 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4659 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004660 default: assert(0 && "DIE Value form not supported yet"); break;
4661 }
4662}
4663
4664/// SizeOf - Determine size of integer value in bytes.
4665///
Jim Laskey072200c2007-01-29 18:51:14 +00004666unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004667 switch (Form) {
4668 case DW_FORM_flag: // Fall thru
4669 case DW_FORM_ref1: // Fall thru
4670 case DW_FORM_data1: return sizeof(int8_t);
4671 case DW_FORM_ref2: // Fall thru
4672 case DW_FORM_data2: return sizeof(int16_t);
4673 case DW_FORM_ref4: // Fall thru
4674 case DW_FORM_data4: return sizeof(int32_t);
4675 case DW_FORM_ref8: // Fall thru
4676 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004677 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4678 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004679 default: assert(0 && "DIE Value form not supported yet"); break;
4680 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004681 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004682}
4683
Jim Laskey063e7652006-01-17 17:31:53 +00004684//===----------------------------------------------------------------------===//
4685
4686/// EmitValue - Emit string value.
4687///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004688void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004689 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00004690}
4691
Jim Laskey063e7652006-01-17 17:31:53 +00004692//===----------------------------------------------------------------------===//
4693
4694/// EmitValue - Emit label value.
4695///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004696void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004697 bool IsSmall = Form == DW_FORM_data4;
4698 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004699}
4700
4701/// SizeOf - Determine size of label value in bytes.
4702///
Jim Laskey072200c2007-01-29 18:51:14 +00004703unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004704 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004705 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004706}
Jim Laskeyef42a012006-11-02 20:12:39 +00004707
Jim Laskey063e7652006-01-17 17:31:53 +00004708//===----------------------------------------------------------------------===//
4709
Jim Laskeyd18e2892006-01-20 20:34:06 +00004710/// EmitValue - Emit label value.
4711///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004712void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004713 bool IsSmall = Form == DW_FORM_data4;
4714 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004715}
4716
4717/// SizeOf - Determine size of label value in bytes.
4718///
Jim Laskey072200c2007-01-29 18:51:14 +00004719unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004720 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004721 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004722}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004723
Jim Laskeyd18e2892006-01-20 20:34:06 +00004724//===----------------------------------------------------------------------===//
4725
Jim Laskey063e7652006-01-17 17:31:53 +00004726/// EmitValue - Emit delta value.
4727///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004728void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4729 bool IsSmall = Form == DW_FORM_data4;
4730 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4731 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4732}
4733
4734/// SizeOf - Determine size of delta value in bytes.
4735///
4736unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4737 if (Form == DW_FORM_data4) return 4;
4738 return DD.getTargetData()->getPointerSize();
4739}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004740
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004741//===----------------------------------------------------------------------===//
4742
4743/// EmitValue - Emit delta value.
4744///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004745void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004746 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004747 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004748}
4749
4750/// SizeOf - Determine size of delta value in bytes.
4751///
Jim Laskey072200c2007-01-29 18:51:14 +00004752unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004753 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004754 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004755}
4756
4757//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004758
Jim Laskeyb8509c52006-03-23 18:07:55 +00004759/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004760///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004761void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004762 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004763}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004764
Jim Laskeyd18e2892006-01-20 20:34:06 +00004765//===----------------------------------------------------------------------===//
4766
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004767/// ComputeSize - calculate the size of the block.
4768///
Jim Laskey072200c2007-01-29 18:51:14 +00004769unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004770 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004771 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004772
Jim Laskeyef42a012006-11-02 20:12:39 +00004773 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004774 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004775 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004776 }
4777 return Size;
4778}
4779
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004780/// EmitValue - Emit block data.
4781///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004782void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004783 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004784 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4785 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4786 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4787 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004788 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004789 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004790
Owen Anderson873e1b52008-06-24 21:44:59 +00004791 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004792
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004793 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004794 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004795 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004796 }
4797}
4798
4799/// SizeOf - Determine size of block data in bytes.
4800///
Jim Laskey072200c2007-01-29 18:51:14 +00004801unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004802 switch (Form) {
4803 case DW_FORM_block1: return Size + sizeof(int8_t);
4804 case DW_FORM_block2: return Size + sizeof(int16_t);
4805 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004806 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004807 default: assert(0 && "Improper form for block"); break;
4808 }
4809 return 0;
4810}
4811
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004812//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004813/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004814
4815DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004816 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004817 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004818}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004819
Jim Laskeyb8509c52006-03-23 18:07:55 +00004820/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4821///
4822void DIE::AddSiblingOffset() {
4823 DIEInteger *DI = new DIEInteger(0);
4824 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004825 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004826}
4827
Jim Laskeyef42a012006-11-02 20:12:39 +00004828/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004829///
Jim Laskeyef42a012006-11-02 20:12:39 +00004830void DIE::Profile(FoldingSetNodeID &ID) {
4831 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004832
Jim Laskeyef42a012006-11-02 20:12:39 +00004833 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4834 ID.AddPointer(Children[i]);
4835
4836 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4837 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004838}
Jim Laskeyef42a012006-11-02 20:12:39 +00004839
4840#ifndef NDEBUG
4841void DIE::print(std::ostream &O, unsigned IncIndent) {
4842 static unsigned IndentCount = 0;
4843 IndentCount += IncIndent;
4844 const std::string Indent(IndentCount, ' ');
4845 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004846
Jim Laskeyef42a012006-11-02 20:12:39 +00004847 if (!isBlock) {
4848 O << Indent
4849 << "Die: "
4850 << "0x" << std::hex << (intptr_t)this << std::dec
4851 << ", Offset: " << Offset
4852 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004853 << "\n";
4854
Jim Laskeyef42a012006-11-02 20:12:39 +00004855 O << Indent
4856 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004857 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004858 << ChildrenString(Abbrev.getChildrenFlag());
4859 } else {
4860 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004861 }
4862 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004863
Owen Anderson873e1b52008-06-24 21:44:59 +00004864 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004865
Jim Laskeyef42a012006-11-02 20:12:39 +00004866 IndentCount += 2;
4867 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4868 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004869
4870 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004871 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004872 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004873 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004874
Jim Laskeyef42a012006-11-02 20:12:39 +00004875 O << " "
4876 << FormEncodingString(Data[i].getForm())
4877 << " ";
4878 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004879 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004880 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004881 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004882
Jim Laskeyef42a012006-11-02 20:12:39 +00004883 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4884 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004885 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004886
Jim Laskeyef42a012006-11-02 20:12:39 +00004887 if (!isBlock) O << "\n";
4888 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004889}
4890
Jim Laskeyef42a012006-11-02 20:12:39 +00004891void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004892 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004893}
Jim Laskeybd761842006-02-27 17:27:12 +00004894#endif
Jim Laskey65195462006-10-30 13:35:07 +00004895
4896//===----------------------------------------------------------------------===//
4897/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004898///
Jim Laskey65195462006-10-30 13:35:07 +00004899
Owen Andersoncb371882008-08-21 00:14:44 +00004900DwarfWriter::DwarfWriter(raw_ostream &OS, AsmPrinter *A,
Jim Laskey65195462006-10-30 13:35:07 +00004901 const TargetAsmInfo *T) {
Jim Laskey072200c2007-01-29 18:51:14 +00004902 DE = new DwarfException(OS, A, T);
4903 DD = new DwarfDebug(OS, A, T);
Jim Laskey65195462006-10-30 13:35:07 +00004904}
4905
4906DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004907 delete DE;
4908 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004909}
4910
Jim Laskey44c3b9f2007-01-26 21:22:28 +00004911/// SetModuleInfo - Set machine module info when it's known that pass manager
4912/// has created it. Set by the target AsmPrinter.
4913void DwarfWriter::SetModuleInfo(MachineModuleInfo *MMI) {
Jim Laskey072200c2007-01-29 18:51:14 +00004914 DD->SetModuleInfo(MMI);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004915 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004916}
4917
4918/// BeginModule - Emit all Dwarf sections that should come prior to the
4919/// content.
4920void DwarfWriter::BeginModule(Module *M) {
Jim Laskey072200c2007-01-29 18:51:14 +00004921 DE->BeginModule(M);
4922 DD->BeginModule(M);
Jim Laskey65195462006-10-30 13:35:07 +00004923}
4924
4925/// EndModule - Emit all Dwarf sections that should come after the content.
4926///
4927void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004928 DE->EndModule();
4929 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004930}
4931
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004932/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004933/// emitted immediately after the function entry point.
4934void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004935 DE->BeginFunction(MF);
4936 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004937}
4938
4939/// EndFunction - Gather and emit post-function debug information.
4940///
Bill Wendlingd751c642008-09-26 00:28:12 +00004941void DwarfWriter::EndFunction(MachineFunction *MF) {
4942 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004943 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004944
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004945 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004946 // Clear function debug information.
4947 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004948}