blob: a53874dcd3f7a4a249fc086cd305f8f289053eb9 [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 Patel8526cc02009-01-05 22:35:52 +00001169/// SrcFileInfo - This class is used to track source information.
1170///
1171class SrcFileInfo {
1172 unsigned DirectoryID; // Directory ID number.
1173 std::string Name; // File name (not including directory.)
1174public:
1175 SrcFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
1176
1177 // Accessors
1178 unsigned getDirectoryID() const { return DirectoryID; }
1179 const std::string &getName() const { return Name; }
1180
1181 /// operator== - Used by UniqueVector to locate entry.
1182 ///
1183 bool operator==(const SourceFileInfo &SI) const {
1184 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
1185 }
1186
1187 /// operator< - Used by UniqueVector to locate entry.
1188 ///
1189 bool operator<(const SrcFileInfo &SI) const {
1190 return getDirectoryID() < SI.getDirectoryID() ||
1191 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
1192 }
1193};
1194
1195//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +00001196/// DbgVariable - This class is used to track local variable information.
1197///
1198class DbgVariable {
1199private:
1200 DIVariable *Var; // Variable Descriptor.
1201 unsigned FrameIndex; // Variable frame index.
1202
1203public:
1204 DbgVariable(DIVariable *V, unsigned I) : Var(V), FrameIndex(I) {}
1205
1206 // Accessors.
1207 DIVariable *getVariable() const { return Var; }
1208 unsigned getFrameIndex() const { return FrameIndex; }
1209};
1210
1211//===----------------------------------------------------------------------===//
1212/// DbgScope - This class is used to track scope information.
1213///
1214class DbgScope {
1215private:
1216 DbgScope *Parent; // Parent to this scope.
1217 DIDescriptor *Desc; // Debug info descriptor for scope.
1218 // Either subprogram or block.
1219 unsigned StartLabelID; // Label ID of the beginning of scope.
1220 unsigned EndLabelID; // Label ID of the end of scope.
1221 SmallVector<DbgScope *, 8> Scopes; // Scopes defined in scope.
1222 SmallVector<DbgVariable *, 32> Variables;// Variables declared in scope.
1223
1224public:
1225 DbgScope(DbgScope *P, DIDescriptor *D)
1226 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1227 {}
1228 ~DbgScope();
1229
1230 // Accessors.
1231 DbgScope *getParent() const { return Parent; }
1232 DIDescriptor *getDesc() const { return Desc; }
1233 unsigned getStartLabelID() const { return StartLabelID; }
1234 unsigned getEndLabelID() const { return EndLabelID; }
1235 SmallVector<DbgScope *, 8> &getScopes() { return Scopes; }
1236 SmallVector<DbgVariable *, 32> &getVariables() { return Variables; }
1237 void setStartLabelID(unsigned S) { StartLabelID = S; }
1238 void setEndLabelID(unsigned E) { EndLabelID = E; }
1239
1240 /// AddScope - Add a scope to the scope.
1241 ///
1242 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1243
1244 /// AddVariable - Add a variable to the scope.
1245 ///
1246 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1247};
1248
1249//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001250/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001251///
1252class DwarfDebug : public Dwarf {
1253
1254private:
Jim Laskey65195462006-10-30 13:35:07 +00001255 //===--------------------------------------------------------------------===//
1256 // Attributes used to construct specific Dwarf sections.
1257 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001258
Jim Laskey65195462006-10-30 13:35:07 +00001259 /// CompileUnits - All the compile units involved in this build. The index
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001260 /// of each entry in this vector corresponds to the sources in MMI.
Jim Laskey65195462006-10-30 13:35:07 +00001261 std::vector<CompileUnit *> CompileUnits;
Devang Patel8526cc02009-01-05 22:35:52 +00001262 DenseMap<GlobalVariable *, CompileUnit *> DW_CUs;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001263
Jim Laskeyef42a012006-11-02 20:12:39 +00001264 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001265 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001266 FoldingSet<DIEAbbrev> AbbreviationsSet;
1267
1268 /// Abbreviations - A list of all the unique abbreviations in use.
1269 ///
1270 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001271
Jim Laskeyef42a012006-11-02 20:12:39 +00001272 /// ValuesSet - Used to uniquely define values.
1273 ///
Devang Patel8526cc02009-01-05 22:35:52 +00001274 // Directories - Uniquing vector for directories.
1275 UniqueVector<std::string> Directories;
1276
1277 // SourceFiles - Uniquing vector for source files.
1278 UniqueVector<SrcFileInfo> SrcFiles;
1279
Jim Laskeyef42a012006-11-02 20:12:39 +00001280 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001281
Jim Laskeyef42a012006-11-02 20:12:39 +00001282 /// Values - A list of all the unique values in use.
1283 ///
1284 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001285
Jim Laskey65195462006-10-30 13:35:07 +00001286 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001287 ///
Jim Laskey65195462006-10-30 13:35:07 +00001288 UniqueVector<std::string> StringPool;
1289
1290 /// UnitMap - Map debug information descriptor to compile unit.
1291 ///
1292 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001293
Jim Laskey65195462006-10-30 13:35:07 +00001294 /// SectionMap - Provides a unique id per text section.
1295 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001296 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001297
Jim Laskey65195462006-10-30 13:35:07 +00001298 /// SectionSourceLines - Tracks line numbers per text section.
1299 ///
1300 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1301
Jim Laskeybacd3042007-02-21 22:48:45 +00001302 /// didInitial - Flag to indicate if initial emission has been done.
1303 ///
1304 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001305
Jim Laskeybacd3042007-02-21 22:48:45 +00001306 /// shouldEmit - Flag to indicate if debug information should be emitted.
1307 ///
1308 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001309
Devang Patel7a6e5a32009-01-08 02:33:41 +00001310 // RootScope - Top level scope for the current function.
1311 //
1312 DbgScope *RootDbgScope;
1313
1314 // DbgScopeMap - Tracks the scopes in the current function.
1315 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
1316
1317 // DbgLabelIDList - One entry per assigned label. Normally the entry is equal to
1318 // the list index(+1). If the entry is zero then the label has been deleted.
1319 // Any other value indicates the label has been deleted by is mapped to
1320 // another label.
1321 SmallVector<unsigned, 32> DbgLabelIDList;
1322
1323 /// NextLabelID - Return the next unique label id.
1324 ///
1325 unsigned NextLabelID() {
1326 unsigned ID = (unsigned)DbgLabelIDList.size() + 1;
1327 DbgLabelIDList.push_back(ID);
1328 return ID;
1329 }
1330
1331 /// RemapLabel - Indicate that a label has been merged into another.
1332 ///
1333 void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) {
1334 assert(0 < OldLabelID && OldLabelID <= DbgLabelIDList.size() &&
1335 "Old label ID out of range.");
1336 assert(NewLabelID <= DbgLabelIDList.size() &&
1337 "New label ID out of range.");
1338 DbgLabelIDList[OldLabelID - 1] = NewLabelID;
1339 }
1340
1341 /// MappedLabel - Find out the label's final ID. Zero indicates deletion.
1342 /// ID != Mapped ID indicates that the label was folded into another label.
1343 unsigned MappedLabel(unsigned LabelID) const {
1344 assert(LabelID <= DbgLabelIDList.size() && "Debug label ID out of range.");
1345 return LabelID ? DbgLabelIDList[LabelID - 1] : 0;
1346 }
1347
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001348 struct FunctionDebugFrameInfo {
1349 unsigned Number;
1350 std::vector<MachineMove> Moves;
1351
1352 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001353 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001354 };
1355
1356 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001357
Jim Laskey65195462006-10-30 13:35:07 +00001358public:
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001359
Jim Laskeybacd3042007-02-21 22:48:45 +00001360 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1361 ///
1362 bool ShouldEmitDwarf() const { return shouldEmit; }
Jim Laskey65195462006-10-30 13:35:07 +00001363
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001364 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001365 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001366 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1367 // Profile the node so that we can make it unique.
1368 FoldingSetNodeID ID;
1369 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001370
Jim Laskeyef42a012006-11-02 20:12:39 +00001371 // Check the set for priors.
1372 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001373
Jim Laskeyef42a012006-11-02 20:12:39 +00001374 // If it's newly added.
1375 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001376 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001377 Abbreviations.push_back(&Abbrev);
1378 // Assign the vector position + 1 as its number.
1379 Abbrev.setNumber(Abbreviations.size());
1380 } else {
1381 // Assign existing abbreviation number.
1382 Abbrev.setNumber(InSet->getNumber());
1383 }
1384 }
1385
Jim Laskey65195462006-10-30 13:35:07 +00001386 /// NewString - Add a string to the constant pool and returns a label.
1387 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001388 DWLabel NewString(const std::string &String) {
1389 unsigned StringID = StringPool.insert(String);
1390 return DWLabel("string", StringID);
1391 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001392
Jim Laskeyef42a012006-11-02 20:12:39 +00001393 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1394 /// entry.
1395 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1396 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001397
Jim Laskeyef42a012006-11-02 20:12:39 +00001398 if (Entry) {
1399 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001400 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001401 void *Where;
1402 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001403
Jim Laskeyf6733882006-11-02 21:48:18 +00001404 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001405
Jim Laskeyef42a012006-11-02 20:12:39 +00001406 Value = new DIEntry(Entry);
1407 ValuesSet.InsertNode(Value, Where);
1408 } else {
1409 Value = new DIEntry(Entry);
1410 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001411
Jim Laskeyef42a012006-11-02 20:12:39 +00001412 Values.push_back(Value);
1413 return Value;
1414 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001415
Jim Laskeyef42a012006-11-02 20:12:39 +00001416 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1417 ///
1418 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1419 Value->Entry = Entry;
1420 // Add to values set if not already there. If it is, we merely have a
1421 // duplicate in the values list (no harm.)
1422 ValuesSet.GetOrInsertNode(Value);
1423 }
1424
1425 /// AddUInt - Add an unsigned integer attribute data and value.
1426 ///
1427 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1428 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1429
1430 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001431 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001432 void *Where;
1433 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1434 if (!Value) {
1435 Value = new DIEInteger(Integer);
1436 ValuesSet.InsertNode(Value, Where);
1437 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001438 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001439
Jim Laskeyef42a012006-11-02 20:12:39 +00001440 Die->AddValue(Attribute, Form, Value);
1441 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001442
Jim Laskeyef42a012006-11-02 20:12:39 +00001443 /// AddSInt - Add an signed integer attribute data and value.
1444 ///
1445 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1446 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1447
1448 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001449 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001450 void *Where;
1451 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1452 if (!Value) {
1453 Value = new DIEInteger(Integer);
1454 ValuesSet.InsertNode(Value, Where);
1455 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001456 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001457
Jim Laskeyef42a012006-11-02 20:12:39 +00001458 Die->AddValue(Attribute, Form, Value);
1459 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001460
Jim Laskeyef42a012006-11-02 20:12:39 +00001461 /// AddString - Add a std::string attribute data and value.
1462 ///
1463 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1464 const std::string &String) {
1465 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001466 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001467 void *Where;
1468 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1469 if (!Value) {
1470 Value = new DIEString(String);
1471 ValuesSet.InsertNode(Value, Where);
1472 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001473 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001474
Jim Laskeyef42a012006-11-02 20:12:39 +00001475 Die->AddValue(Attribute, Form, Value);
1476 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001477
Jim Laskeyef42a012006-11-02 20:12:39 +00001478 /// AddLabel - Add a Dwarf label attribute data and value.
1479 ///
1480 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1481 const DWLabel &Label) {
1482 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001483 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001484 void *Where;
1485 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1486 if (!Value) {
1487 Value = new DIEDwarfLabel(Label);
1488 ValuesSet.InsertNode(Value, Where);
1489 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001490 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001491
Jim Laskeyef42a012006-11-02 20:12:39 +00001492 Die->AddValue(Attribute, Form, Value);
1493 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001494
Jim Laskeyef42a012006-11-02 20:12:39 +00001495 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1496 ///
1497 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1498 const std::string &Label) {
1499 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001500 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001501 void *Where;
1502 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1503 if (!Value) {
1504 Value = new DIEObjectLabel(Label);
1505 ValuesSet.InsertNode(Value, Where);
1506 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001507 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001508
Jim Laskeyef42a012006-11-02 20:12:39 +00001509 Die->AddValue(Attribute, Form, Value);
1510 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001511
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001512 /// AddSectionOffset - Add a section offset label attribute data and value.
1513 ///
1514 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1515 const DWLabel &Label, const DWLabel &Section,
1516 bool isEH = false, bool useSet = true) {
1517 FoldingSetNodeID ID;
1518 DIESectionOffset::Profile(ID, Label, Section);
1519 void *Where;
1520 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1521 if (!Value) {
1522 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1523 ValuesSet.InsertNode(Value, Where);
1524 Values.push_back(Value);
1525 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001526
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001527 Die->AddValue(Attribute, Form, Value);
1528 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001529
Jim Laskeyef42a012006-11-02 20:12:39 +00001530 /// AddDelta - Add a label delta attribute data and value.
1531 ///
1532 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1533 const DWLabel &Hi, const DWLabel &Lo) {
1534 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001535 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001536 void *Where;
1537 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1538 if (!Value) {
1539 Value = new DIEDelta(Hi, Lo);
1540 ValuesSet.InsertNode(Value, Where);
1541 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001542 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001543
Jim Laskeyef42a012006-11-02 20:12:39 +00001544 Die->AddValue(Attribute, Form, Value);
1545 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001546
Jim Laskeyef42a012006-11-02 20:12:39 +00001547 /// AddDIEntry - Add a DIE attribute data and value.
1548 ///
1549 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1550 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1551 }
1552
1553 /// AddBlock - Add block data.
1554 ///
1555 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1556 Block->ComputeSize(*this);
1557 FoldingSetNodeID ID;
1558 Block->Profile(ID);
1559 void *Where;
1560 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1561 if (!Value) {
1562 Value = Block;
1563 ValuesSet.InsertNode(Value, Where);
1564 Values.push_back(Value);
1565 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001566 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001567 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001568 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001569 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001570
Jim Laskeyef42a012006-11-02 20:12:39 +00001571 Die->AddValue(Attribute, Block->BestForm(), Value);
1572 }
1573
Jim Laskey65195462006-10-30 13:35:07 +00001574private:
1575
1576 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001577 /// entry.
1578 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1579 if (File && Line) {
1580 CompileUnit *FileUnit = FindCompileUnit(File);
1581 unsigned FileID = FileUnit->getID();
1582 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1583 AddUInt(Die, DW_AT_decl_line, 0, Line);
1584 }
1585 }
Jim Laskey65195462006-10-30 13:35:07 +00001586
Devang Patel8526cc02009-01-05 22:35:52 +00001587 /// AddSourceLine - Add location information to specified debug information
1588 /// entry.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001589 void AddSourceLine(DIE *Die, DIVariable *V) {
1590 unsigned FileID = 0;
1591 unsigned Line = V->getLineNumber();
1592 if (V->getVersion() < DIDescriptor::Version7) {
1593 // Version6 or earlier. Use compile unit info to get file id.
1594 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1595 FileID = Unit->getID();
1596 } else {
1597 // Version7 or newer, use filename and directory info from DIVariable
1598 // directly.
1599 unsigned DID = Directories.idFor(V->getDirectory());
1600 FileID = SrcFiles.idFor(SrcFileInfo(DID, V->getFilename()));
1601 }
1602 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1603 AddUInt(Die, DW_AT_decl_line, 0, Line);
1604 }
1605
1606 /// AddSourceLine - Add location information to specified debug information
1607 /// entry.
Devang Patel8526cc02009-01-05 22:35:52 +00001608 void AddSourceLine(DIE *Die, DIGlobal *G) {
1609 unsigned FileID = 0;
1610 unsigned Line = G->getLineNumber();
1611 if (G->getVersion() < DIDescriptor::Version7) {
1612 // Version6 or earlier. Use compile unit info to get file id.
1613 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1614 FileID = Unit->getID();
1615 } else {
1616 // Version7 or newer, use filename and directory info from DIGlobal
1617 // directly.
1618 unsigned DID = Directories.idFor(G->getDirectory());
1619 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1620 }
1621 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1622 AddUInt(Die, DW_AT_decl_line, 0, Line);
1623 }
1624
1625 void AddSourceLine(DIE *Die, DIType *G) {
1626 unsigned FileID = 0;
1627 unsigned Line = G->getLineNumber();
1628 if (G->getVersion() < DIDescriptor::Version7) {
1629 // Version6 or earlier. Use compile unit info to get file id.
1630 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1631 FileID = Unit->getID();
1632 } else {
1633 // Version7 or newer, use filename and directory info from DIGlobal
1634 // directly.
1635 unsigned DID = Directories.idFor(G->getDirectory());
1636 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1637 }
1638 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1639 AddUInt(Die, DW_AT_decl_line, 0, Line);
1640 }
1641
Jim Laskey65195462006-10-30 13:35:07 +00001642 /// AddAddress - Add an address attribute to a die based on the location
1643 /// provided.
1644 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001645 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001646 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001647 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001648
Dan Gohmand735b802008-10-03 15:45:36 +00001649 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001650 if (Reg < 32) {
1651 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1652 } else {
1653 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1654 AddUInt(Block, 0, DW_FORM_udata, Reg);
1655 }
1656 } else {
1657 if (Reg < 32) {
1658 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1659 } else {
1660 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1661 AddUInt(Block, 0, DW_FORM_udata, Reg);
1662 }
1663 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1664 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001665
Jim Laskeyef42a012006-11-02 20:12:39 +00001666 AddBlock(Die, Attribute, 0, Block);
1667 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001668
Jim Laskeyef42a012006-11-02 20:12:39 +00001669 /// AddBasicType - Add a new basic type attribute to the specified entity.
1670 ///
1671 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1672 const std::string &Name,
1673 unsigned Encoding, unsigned Size) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001674
Jim Laskeyef42a012006-11-02 20:12:39 +00001675 DIE Buffer(DW_TAG_base_type);
1676 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1677 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1678 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel9ede3f22009-01-05 17:44:11 +00001679 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1680 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001681 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001682
Jim Laskeyef42a012006-11-02 20:12:39 +00001683 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1684 ///
1685 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001686 DIE Buffer(DW_TAG_pointer_type);
Dan Gohman82482942007-09-27 23:12:31 +00001687 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Jim Laskeyef42a012006-11-02 20:12:39 +00001688 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelca03c272009-01-05 17:45:59 +00001689 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1690 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001691 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001692
Jim Laskeyef42a012006-11-02 20:12:39 +00001693 /// AddType - Add a new type attribute to the specified entity.
1694 ///
1695 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1696 if (!TyDesc) {
Jim Laskey2b935d52007-01-26 14:19:17 +00001697 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001698 } else {
1699 // Check for pre-existence.
1700 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001701
Jim Laskeyef42a012006-11-02 20:12:39 +00001702 // If it exists then use the existing value.
1703 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001704 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1705 return;
1706 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001707
Jim Laskeyef42a012006-11-02 20:12:39 +00001708 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1709 // FIXME - Not sure why programs and variables are coming through here.
1710 // Short cut for handling subprogram types (not really a TyDesc.)
1711 AddPointerType(Entity, Unit, SubprogramTy->getName());
1712 } else if (GlobalVariableDesc *GlobalTy =
1713 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1714 // FIXME - Not sure why programs and variables are coming through here.
1715 // Short cut for handling global variable types (not really a TyDesc.)
1716 AddPointerType(Entity, Unit, GlobalTy->getName());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001717 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001718 // Set up proxy.
1719 Slot = NewDIEntry();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001720
Jim Laskeyef42a012006-11-02 20:12:39 +00001721 // Construct type.
1722 DIE Buffer(DW_TAG_base_type);
1723 ConstructType(Buffer, TyDesc, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001724
Jim Laskeyef42a012006-11-02 20:12:39 +00001725 // Add debug information entry to entity and unit.
1726 DIE *Die = Unit->AddDie(Buffer);
1727 SetDIEntry(Slot, Die);
1728 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1729 }
1730 }
1731 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001732
Devang Patel48d190f2009-01-05 21:47:57 +00001733 /// AddType - Add a new type attribute to the specified entity.
1734 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
1735 if (Ty.isNull()) {
1736 AddBasicType(Entity, DW_Unit, "", DW_ATE_signed, sizeof(int32_t));
1737 return;
1738 }
1739
1740 // Check for pre-existence.
1741 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1742 // If it exists then use the existing value.
1743 if (Slot) {
1744 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1745 return;
1746 }
1747
1748 // Set up proxy.
1749 Slot = NewDIEntry();
1750
1751 // Construct type.
1752 DIE Buffer(DW_TAG_base_type);
1753 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1754 ConstructTypeDIE(DW_Unit, Buffer, BT);
1755 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1756 ConstructTypeDIE(DW_Unit, Buffer, DT);
1757 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1758 ConstructTypeDIE(DW_Unit, Buffer, CT);
1759
1760 // Add debug information entry to entity and unit.
1761 DIE *Die = DW_Unit->AddDie(Buffer);
1762 SetDIEntry(Slot, Die);
1763 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1764 }
1765
Devang Patele5202732009-01-05 19:07:53 +00001766 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1767 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1768 DIBasicType *BTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001769
1770 // Get core information.
1771 const std::string &Name = BTy->getName();
1772 Buffer.setTag(DW_TAG_base_type);
1773 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy->getEncoding());
1774 // Add name if not anonymous or intermediate type.
1775 if (!Name.empty())
1776 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1777 uint64_t Size = BTy->getSizeInBits() >> 3;
1778 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1779 }
1780
Devang Patele5202732009-01-05 19:07:53 +00001781 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1782 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1783 DIDerivedType *DTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001784
1785 // Get core information.
1786 const std::string &Name = DTy->getName();
1787 uint64_t Size = DTy->getSizeInBits() >> 3;
1788 unsigned Tag = DTy->getTag();
1789 // FIXME - Workaround for templates.
1790 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1791
1792 Buffer.setTag(Tag);
1793 // Map to main type, void will not have a type.
1794 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001795 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001796
1797 // Add name if not anonymous or intermediate type.
1798 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1799
1800 // Add size if non-zero (derived types might be zero-sized.)
1801 if (Size)
1802 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1803
1804 // Add source line info if available and TyDesc is not a forward
1805 // declaration.
1806 // FIXME - Enable this. if (!DTy->isForwardDecl())
1807 // FIXME - Enable this. AddSourceLine(&Buffer, *DTy);
1808 }
1809
Devang Patelf4215332009-01-05 19:55:51 +00001810 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1811 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1812 DICompositeType *CTy) {
1813
1814 // Get core information.
1815 const std::string &Name = CTy->getName();
1816 uint64_t Size = CTy->getSizeInBits() >> 3;
1817 unsigned Tag = CTy->getTag();
1818 switch (Tag) {
1819 case DW_TAG_vector_type:
1820 case DW_TAG_array_type:
1821 ConstructArrayTypeDIE(DW_Unit, Buffer, CTy);
1822 break;
1823 //FIXME - Enable this.
1824 // case DW_TAG_enumeration_type:
1825 // DIArray Elements = CTy->getTypeArray();
1826 // // Add enumerators to enumeration type.
1827 // for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i)
1828 // ConstructEnumTypeDIE(Buffer, &Elements.getElement(i));
1829 // break;
1830 case DW_TAG_subroutine_type:
1831 {
1832 // Add prototype flag.
1833 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1834 DIArray Elements = CTy->getTypeArray();
1835 // Add return type.
Devang Patel48d190f2009-01-05 21:47:57 +00001836 DIDescriptor RTy = Elements.getElement(0);
1837 if (DIBasicType *BT = dyn_cast<DIBasicType>(&RTy))
1838 AddType(DW_Unit, &Buffer, *BT);
1839 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&RTy))
1840 AddType(DW_Unit, &Buffer, *DT);
1841 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&RTy))
1842 AddType(DW_Unit, &Buffer, *CT);
1843
1844 //AddType(DW_Unit, &Buffer, Elements.getElement(0));
Devang Patelf4215332009-01-05 19:55:51 +00001845 // Add arguments.
1846 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1847 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001848 DIDescriptor Ty = Elements.getElement(i);
1849 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1850 AddType(DW_Unit, &Buffer, *BT);
1851 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1852 AddType(DW_Unit, &Buffer, *DT);
1853 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1854 AddType(DW_Unit, &Buffer, *CT);
Devang Patelf4215332009-01-05 19:55:51 +00001855 Buffer.AddChild(Arg);
1856 }
1857 }
1858 break;
1859 case DW_TAG_structure_type:
1860 case DW_TAG_union_type:
1861 {
1862 // Add elements to structure type.
1863 DIArray Elements = CTy->getTypeArray();
1864 // Add elements to structure type.
1865 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1866 DIDescriptor Element = Elements.getElement(i);
1867 if (DISubprogram *SP = dyn_cast<DISubprogram>(&Element))
1868 ConstructFieldTypeDIE(DW_Unit, Buffer, SP);
1869 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Element))
1870 ConstructFieldTypeDIE(DW_Unit, Buffer, DT);
1871 else if (DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(&Element))
1872 ConstructFieldTypeDIE(DW_Unit, Buffer, GV);
1873 }
1874 }
1875 break;
1876 default:
1877 break;
1878 }
1879
1880 // Add name if not anonymous or intermediate type.
1881 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1882
1883 // Add size if non-zero (derived types might be zero-sized.)
1884 if (Size)
1885 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1886 else {
1887 // Add zero size even if it is not a forward declaration.
1888 // FIXME - Enable this.
1889 // if (!CTy->isDefinition())
1890 // AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1891 // else
1892 // AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1893 }
1894
1895 // Add source line info if available and TyDesc is not a forward
1896 // declaration.
1897 // FIXME - Enable this.
1898 // if (CTy->isForwardDecl())
1899 // AddSourceLine(&Buffer, *CTy);
1900 }
1901
Devang Patel68afdc32009-01-05 18:33:01 +00001902 // ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1903 void ConstructSubrangeDIE (DIE &Buffer, DISubrange *SR, DIE *IndexTy) {
1904 int64_t L = SR->getLo();
1905 int64_t H = SR->getHi();
1906 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1907 if (L != H) {
1908 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1909 if (L)
1910 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1911 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
1912 }
1913 Buffer.AddChild(DW_Subrange);
1914 }
1915
1916 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1917 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1918 DICompositeType *CTy) {
1919 Buffer.setTag(DW_TAG_array_type);
1920 if (CTy->getTag() == DW_TAG_vector_type)
1921 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1922
1923 DIArray Elements = CTy->getTypeArray();
1924 // FIXME - Enable this.
Devang Patel48d190f2009-01-05 21:47:57 +00001925 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001926
1927 // Construct an anonymous type for index type.
1928 DIE IdxBuffer(DW_TAG_base_type);
1929 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1930 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1931 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1932
1933 // Add subranges to array type.
1934 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001935 DIDescriptor Element = Elements.getElement(i);
1936 if (DISubrange *SR = dyn_cast<DISubrange>(&Element))
1937 ConstructSubrangeDIE(Buffer, SR, IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001938 }
1939 }
1940
Devang Patelc69bf2c2009-01-05 18:38:38 +00001941 /// ConstructEnumTypeDIE - Construct enum type DIE from
1942 /// DIEnumerator.
Devang Patelf4215332009-01-05 19:55:51 +00001943 void ConstructEnumTypeDIE(CompileUnit *DW_Unit,
1944 DIE &Buffer, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001945
1946 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1947 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1948 int64_t Value = ETy->getEnumValue();
1949 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1950 Buffer.AddChild(Enumerator);
1951 }
Devang Patel68afdc32009-01-05 18:33:01 +00001952
Devang Patel86ae1422009-01-05 18:59:44 +00001953 /// ConstructFieldTypeDIE - Construct variable DIE for a struct field.
1954 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1955 DIE &Buffer, DIGlobalVariable *V) {
1956
1957 DIE *VariableDie = new DIE(DW_TAG_variable);
1958 const std::string &LinkageName = V->getLinkageName();
1959 if (!LinkageName.empty())
1960 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1961 LinkageName);
1962 // FIXME - Enable this. AddSourceLine(VariableDie, V);
Devang Patel48d190f2009-01-05 21:47:57 +00001963 AddType(DW_Unit, VariableDie, V->getType());
Devang Patel86ae1422009-01-05 18:59:44 +00001964 if (!V->isLocalToUnit())
1965 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1966 AddUInt(VariableDie, DW_AT_declaration, DW_FORM_flag, 1);
1967 Buffer.AddChild(VariableDie);
1968 }
1969
1970 /// ConstructFieldTypeDIE - Construct subprogram DIE for a struct field.
1971 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1972 DIE &Buffer, DISubprogram *SP,
1973 bool IsConstructor = false) {
1974 DIE *Method = new DIE(DW_TAG_subprogram);
1975 AddString(Method, DW_AT_name, DW_FORM_string, SP->getName());
1976 const std::string &LinkageName = SP->getLinkageName();
1977 if (!LinkageName.empty())
1978 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1979 // FIXME - Enable this. AddSourceLine(Method, SP);
1980
1981 DICompositeType MTy = SP->getType();
1982 DIArray Args = MTy.getTypeArray();
1983
1984 // Add Return Type.
Devang Patel48d190f2009-01-05 21:47:57 +00001985 if (!IsConstructor) {
1986 DIDescriptor Ty = Args.getElement(0);
1987 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1988 AddType(DW_Unit, Method, *BT);
1989 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1990 AddType(DW_Unit, Method, *DT);
1991 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1992 AddType(DW_Unit, Method, *CT);
1993 }
Devang Patel86ae1422009-01-05 18:59:44 +00001994
1995 // Add arguments.
1996 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1997 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001998 DIDescriptor Ty = Args.getElement(i);
1999 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
2000 AddType(DW_Unit, Method, *BT);
2001 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
2002 AddType(DW_Unit, Method, *DT);
2003 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
2004 AddType(DW_Unit, Method, *CT);
Devang Patel86ae1422009-01-05 18:59:44 +00002005 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
2006 Method->AddChild(Arg);
2007 }
2008
2009 if (!SP->isLocalToUnit())
2010 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
2011 Buffer.AddChild(Method);
2012 }
2013
2014 /// COnstructFieldTypeDIE - Construct derived type DIE for a struct field.
2015 void ConstructFieldTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
2016 DIDerivedType *DTy) {
2017 unsigned Tag = DTy->getTag();
2018 DIE *MemberDie = new DIE(Tag);
2019 if (!DTy->getName().empty())
2020 AddString(MemberDie, DW_AT_name, DW_FORM_string, DTy->getName());
2021 // FIXME - Enable this. AddSourceLine(MemberDie, DTy);
2022
2023 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00002024 AddType(DW_Unit, MemberDie, FromTy);
Devang Patel86ae1422009-01-05 18:59:44 +00002025
2026 uint64_t Size = DTy->getSizeInBits();
2027 uint64_t Offset = DTy->getOffsetInBits();
2028
2029 // FIXME Handle bitfields
2030
2031 // Add size.
2032 AddUInt(MemberDie, DW_AT_bit_size, 0, Size);
2033 // Add computation for offset.
2034 DIEBlock *Block = new DIEBlock();
2035 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
2036 AddUInt(Block, 0, DW_FORM_udata, Offset >> 3);
2037 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
2038
2039 // FIXME Handle DW_AT_accessibility.
2040
2041 Buffer.AddChild(MemberDie);
2042 }
2043
Jim Laskeyef42a012006-11-02 20:12:39 +00002044 /// ConstructType - Adds all the required attributes to the type.
2045 ///
2046 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
2047 // Get core information.
2048 const std::string &Name = TyDesc->getName();
2049 uint64_t Size = TyDesc->getSize() >> 3;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002050
Jim Laskeyef42a012006-11-02 20:12:39 +00002051 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
2052 // Fundamental types like int, float, bool
2053 Buffer.setTag(DW_TAG_base_type);
2054 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
2055 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00002056 // Fetch tag.
2057 unsigned Tag = DerivedTy->getTag();
2058 // FIXME - Workaround for templates.
2059 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002060 // Pointers, typedefs et al.
Jim Laskey85f419b2006-11-09 16:32:26 +00002061 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00002062 // Map to main type, void will not have a type.
2063 if (TypeDesc *FromTy = DerivedTy->getFromType())
2064 AddType(&Buffer, FromTy, Unit);
2065 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
2066 // Fetch tag.
2067 unsigned Tag = CompTy->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002068
Jim Laskeyef42a012006-11-02 20:12:39 +00002069 // Set tag accordingly.
2070 if (Tag == DW_TAG_vector_type)
2071 Buffer.setTag(DW_TAG_array_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002072 else
Jim Laskeyef42a012006-11-02 20:12:39 +00002073 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00002074
Jim Laskeyef42a012006-11-02 20:12:39 +00002075 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002076
Jim Laskeyef42a012006-11-02 20:12:39 +00002077 switch (Tag) {
2078 case DW_TAG_vector_type:
2079 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
2080 // Fall thru
2081 case DW_TAG_array_type: {
2082 // Add element type.
2083 if (TypeDesc *FromTy = CompTy->getFromType())
2084 AddType(&Buffer, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002085
Jim Laskeyef42a012006-11-02 20:12:39 +00002086 // Don't emit size attribute.
2087 Size = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002088
Jim Laskeyef42a012006-11-02 20:12:39 +00002089 // Construct an anonymous type for index type.
Devang Patel9ede3f22009-01-05 17:44:11 +00002090 DIE Buffer(DW_TAG_base_type);
2091 AddUInt(&Buffer, DW_AT_byte_size, 0, sizeof(int32_t));
2092 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
2093 DIE *IndexTy = Unit->AddDie(Buffer);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002094
Jim Laskeyef42a012006-11-02 20:12:39 +00002095 // Add subranges to array type.
Evan Chengf8480282008-12-09 17:56:30 +00002096 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002097 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
2098 int64_t Lo = SRD->getLo();
2099 int64_t Hi = SRD->getHi();
2100 DIE *Subrange = new DIE(DW_TAG_subrange_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002101
Jim Laskeyef42a012006-11-02 20:12:39 +00002102 // If a range is available.
2103 if (Lo != Hi) {
2104 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
2105 // Only add low if non-zero.
2106 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
2107 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
2108 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002109
Jim Laskeyef42a012006-11-02 20:12:39 +00002110 Buffer.AddChild(Subrange);
2111 }
2112 break;
2113 }
2114 case DW_TAG_structure_type:
2115 case DW_TAG_union_type: {
2116 // Add elements to structure type.
Evan Chengf8480282008-12-09 17:56:30 +00002117 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002118 DebugInfoDesc *Element = Elements[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002119
Jim Laskeyef42a012006-11-02 20:12:39 +00002120 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
2121 // Add field or base class.
Jim Laskeyef42a012006-11-02 20:12:39 +00002122 unsigned Tag = MemberDesc->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002123
Jim Laskeyef42a012006-11-02 20:12:39 +00002124 // Extract the basic information.
2125 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00002126 uint64_t Size = MemberDesc->getSize();
2127 uint64_t Align = MemberDesc->getAlign();
2128 uint64_t Offset = MemberDesc->getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002129
Jim Laskeyef42a012006-11-02 20:12:39 +00002130 // Construct member debug information entry.
2131 DIE *Member = new DIE(Tag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002132
Jim Laskeyef42a012006-11-02 20:12:39 +00002133 // Add name if not "".
2134 if (!Name.empty())
2135 AddString(Member, DW_AT_name, DW_FORM_string, Name);
Evan Chengf8480282008-12-09 17:56:30 +00002136
Jim Laskeyef42a012006-11-02 20:12:39 +00002137 // Add location if available.
2138 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002139
Jim Laskeyef42a012006-11-02 20:12:39 +00002140 // Most of the time the field info is the same as the members.
2141 uint64_t FieldSize = Size;
2142 uint64_t FieldAlign = Align;
2143 uint64_t FieldOffset = Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002144
Jim Laskeyee5f9272006-12-22 20:03:42 +00002145 // Set the member type.
2146 TypeDesc *FromTy = MemberDesc->getFromType();
2147 AddType(Member, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002148
Jim Laskeyee5f9272006-12-22 20:03:42 +00002149 // Walk up typedefs until a real size is found.
2150 while (FromTy) {
2151 if (FromTy->getTag() != DW_TAG_typedef) {
2152 FieldSize = FromTy->getSize();
Devang Pateld0935c32008-12-23 21:55:38 +00002153 FieldAlign = FromTy->getAlign();
Jim Laskeyee5f9272006-12-22 20:03:42 +00002154 break;
2155 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002156
Dan Gohman275769a2007-07-23 20:24:29 +00002157 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
Jim Laskeyef42a012006-11-02 20:12:39 +00002158 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002159
Jim Laskeyef42a012006-11-02 20:12:39 +00002160 // Unless we have a bit field.
2161 if (Tag == DW_TAG_member && FieldSize != Size) {
2162 // Construct the alignment mask.
2163 uint64_t AlignMask = ~(FieldAlign - 1);
2164 // Determine the high bit + 1 of the declared size.
2165 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
2166 // Work backwards to determine the base offset of the field.
2167 FieldOffset = HiMark - FieldSize;
2168 // Now normalize offset to the field.
2169 Offset -= FieldOffset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002170
Jim Laskeyef42a012006-11-02 20:12:39 +00002171 // Maybe we need to work from the other end.
2172 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002173
Jim Laskeyef42a012006-11-02 20:12:39 +00002174 // Add size and offset.
2175 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
2176 AddUInt(Member, DW_AT_bit_size, 0, Size);
2177 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
2178 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002179
Jim Laskeyef42a012006-11-02 20:12:39 +00002180 // Add computation for offset.
2181 DIEBlock *Block = new DIEBlock();
2182 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
2183 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
2184 AddBlock(Member, DW_AT_data_member_location, 0, Block);
2185
2186 // Add accessibility (public default unless is base class.
2187 if (MemberDesc->isProtected()) {
2188 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
2189 } else if (MemberDesc->isPrivate()) {
2190 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
2191 } else if (Tag == DW_TAG_inheritance) {
2192 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
2193 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002194
Jim Laskeyef42a012006-11-02 20:12:39 +00002195 Buffer.AddChild(Member);
2196 } else if (GlobalVariableDesc *StaticDesc =
2197 dyn_cast<GlobalVariableDesc>(Element)) {
2198 // Add static member.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002199
Jim Laskeyef42a012006-11-02 20:12:39 +00002200 // Construct member debug information entry.
2201 DIE *Static = new DIE(DW_TAG_variable);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002202
Jim Laskeyef42a012006-11-02 20:12:39 +00002203 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00002204 const std::string &Name = StaticDesc->getName();
2205 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00002206 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002207 if (!LinkageName.empty()) {
2208 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
2209 LinkageName);
2210 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002211
Jim Laskeyef42a012006-11-02 20:12:39 +00002212 // Add location.
2213 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002214
Jim Laskeyef42a012006-11-02 20:12:39 +00002215 // Add type.
2216 if (TypeDesc *StaticTy = StaticDesc->getType())
2217 AddType(Static, StaticTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002218
Jim Laskeyef42a012006-11-02 20:12:39 +00002219 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00002220 if (!StaticDesc->isStatic())
2221 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002222 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002223
Jim Laskeyef42a012006-11-02 20:12:39 +00002224 Buffer.AddChild(Static);
2225 } else if (SubprogramDesc *MethodDesc =
2226 dyn_cast<SubprogramDesc>(Element)) {
2227 // Add member function.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002228
Jim Laskeyef42a012006-11-02 20:12:39 +00002229 // Construct member debug information entry.
2230 DIE *Method = new DIE(DW_TAG_subprogram);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002231
Jim Laskeyef42a012006-11-02 20:12:39 +00002232 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00002233 const std::string &Name = MethodDesc->getName();
2234 const std::string &LinkageName = MethodDesc->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002235
2236 AddString(Method, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002237 bool IsCTor = TyDesc->getName() == Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002238
Jim Laskey2172f962006-11-30 14:35:45 +00002239 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002240 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00002241 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00002242 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002243
Jim Laskeyef42a012006-11-02 20:12:39 +00002244 // Add location.
2245 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002246
Jim Laskeyef42a012006-11-02 20:12:39 +00002247 // Add type.
2248 if (CompositeTypeDesc *MethodTy =
2249 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
2250 // Get argument information.
2251 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002252
Jim Laskeyef42a012006-11-02 20:12:39 +00002253 // If not a ctor.
2254 if (!IsCTor) {
2255 // Add return type.
2256 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
2257 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002258
Jim Laskeyef42a012006-11-02 20:12:39 +00002259 // Add arguments.
Evan Chengf8480282008-12-09 17:56:30 +00002260 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002261 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2262 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
2263 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
2264 Method->AddChild(Arg);
2265 }
2266 }
2267
2268 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00002269 if (!MethodDesc->isStatic())
2270 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002271 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002272
Jim Laskeyef42a012006-11-02 20:12:39 +00002273 Buffer.AddChild(Method);
2274 }
2275 }
2276 break;
2277 }
2278 case DW_TAG_enumeration_type: {
2279 // Add enumerators to enumeration type.
Evan Chengf8480282008-12-09 17:56:30 +00002280 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002281 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
2282 const std::string &Name = ED->getName();
2283 int64_t Value = ED->getValue();
2284 DIE *Enumerator = new DIE(DW_TAG_enumerator);
2285 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
2286 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
2287 Buffer.AddChild(Enumerator);
2288 }
2289
2290 break;
2291 }
2292 case DW_TAG_subroutine_type: {
2293 // Add prototype flag.
2294 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
2295 // Add return type.
2296 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002297
Jim Laskeyef42a012006-11-02 20:12:39 +00002298 // Add arguments.
Evan Chengf8480282008-12-09 17:56:30 +00002299 for (unsigned i = 1, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002300 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2301 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
2302 Buffer.AddChild(Arg);
2303 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002304
Jim Laskeyef42a012006-11-02 20:12:39 +00002305 break;
2306 }
2307 default: break;
2308 }
2309 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002310
Jim Laskeyef42a012006-11-02 20:12:39 +00002311 // Add name if not anonymous or intermediate type.
2312 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Evan Chengf8480282008-12-09 17:56:30 +00002313
Evan Cheng94ea5be2008-12-10 00:15:44 +00002314 // Add size if non-zero (derived types might be zero-sized.)
2315 if (Size)
2316 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
2317 else if (isa<CompositeTypeDesc>(TyDesc)) {
2318 // If TyDesc is a composite type, then add size even if it's zero unless
2319 // it's a forward declaration.
2320 if (TyDesc->isForwardDecl())
2321 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
2322 else
2323 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
2324 }
2325
2326 // Add source line info if available and TyDesc is not a forward
2327 // declaration.
2328 if (!TyDesc->isForwardDecl())
2329 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
Jim Laskeyef42a012006-11-02 20:12:39 +00002330 }
2331
2332 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00002333 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002334 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
2335 // Construct debug information entry.
2336 DIE *Die = new DIE(DW_TAG_compile_unit);
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00002337 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2338 DWLabel("section_line", 0), DWLabel("section_line", 0), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002339 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
2340 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
2341 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
Devang Patel83580992008-12-12 21:57:54 +00002342 if (!UnitDesc->getDirectory().empty())
2343 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002344
Jim Laskeyef42a012006-11-02 20:12:39 +00002345 // Construct compile unit.
2346 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002347
Jim Laskeyef42a012006-11-02 20:12:39 +00002348 // Add Unit to compile unit map.
2349 DescToUnitMap[UnitDesc] = Unit;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002350
Jim Laskeyef42a012006-11-02 20:12:39 +00002351 return Unit;
2352 }
2353
Jim Laskey9d4209f2006-11-07 19:33:46 +00002354 /// GetBaseCompileUnit - Get the main compile unit.
2355 ///
2356 CompileUnit *GetBaseCompileUnit() const {
2357 CompileUnit *Unit = CompileUnits[0];
2358 assert(Unit && "Missing compile unit.");
2359 return Unit;
2360 }
2361
Jim Laskey65195462006-10-30 13:35:07 +00002362 /// FindCompileUnit - Get the compile unit for the given descriptor.
2363 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002364 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002365 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00002366 assert(Unit && "Missing compile unit.");
2367 return Unit;
2368 }
2369
Devang Patel8526cc02009-01-05 22:35:52 +00002370 /// FindCompileUnit - Get the compile unit for the given descriptor.
2371 ///
2372 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
2373 CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
2374 assert(DW_Unit && "Missing compile unit.");
2375 return DW_Unit;
2376 }
2377
Jim Laskeyef42a012006-11-02 20:12:39 +00002378 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00002379 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002380 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
2381 // Get the compile unit context.
2382 CompileUnitDesc *UnitDesc =
2383 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00002384 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002385
2386 // Check for pre-existence.
2387 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
2388 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002389
Jim Laskeyef42a012006-11-02 20:12:39 +00002390 // Get the global variable itself.
2391 GlobalVariable *GV = GVD->getGlobalVariable();
2392
Jim Laskey2172f962006-11-30 14:35:45 +00002393 const std::string &Name = GVD->getName();
2394 const std::string &FullName = GVD->getFullName();
2395 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00002396 // Create the global's variable DIE.
2397 DIE *VariableDie = new DIE(DW_TAG_variable);
2398 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002399 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002400 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00002401 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00002402 }
Jim Laskey6488a072007-01-08 22:15:18 +00002403 AddType(VariableDie, GVD->getType(), Unit);
2404 if (!GVD->isStatic())
2405 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002406
Jim Laskeyef42a012006-11-02 20:12:39 +00002407 // Add source line info if available.
2408 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002409
Jim Laskeyef42a012006-11-02 20:12:39 +00002410 // Add address.
2411 DIEBlock *Block = new DIEBlock();
2412 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00002413 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
2414 AddBlock(VariableDie, DW_AT_location, 0, Block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002415
Jim Laskeyef42a012006-11-02 20:12:39 +00002416 // Add to map.
2417 Slot = VariableDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002418
Jim Laskeyef42a012006-11-02 20:12:39 +00002419 // Add to context owner.
2420 Unit->getDie()->AddChild(VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002421
Jim Laskeyef42a012006-11-02 20:12:39 +00002422 // Expose as global.
2423 // FIXME - need to check external flag.
Jim Laskey2172f962006-11-30 14:35:45 +00002424 Unit->AddGlobal(FullName, VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002425
Jim Laskeyef42a012006-11-02 20:12:39 +00002426 return VariableDie;
2427 }
Jim Laskey65195462006-10-30 13:35:07 +00002428
2429 /// NewSubprogram - Add a new subprogram DIE.
2430 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002431 DIE *NewSubprogram(SubprogramDesc *SPD) {
2432 // Get the compile unit context.
2433 CompileUnitDesc *UnitDesc =
2434 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00002435 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002436
2437 // Check for pre-existence.
2438 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
2439 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002440
Jim Laskeyef42a012006-11-02 20:12:39 +00002441 // Gather the details (simplify add attribute code.)
Jim Laskey2172f962006-11-30 14:35:45 +00002442 const std::string &Name = SPD->getName();
2443 const std::string &FullName = SPD->getFullName();
2444 const std::string &LinkageName = SPD->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002445
Jim Laskeyef42a012006-11-02 20:12:39 +00002446 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
2447 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002448 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002449 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00002450 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00002451 }
2452 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
Jim Laskey6488a072007-01-08 22:15:18 +00002453 if (!SPD->isStatic())
2454 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002455 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002456
Jim Laskeyef42a012006-11-02 20:12:39 +00002457 // Add source line info if available.
2458 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
2459
2460 // Add to map.
2461 Slot = SubprogramDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002462
Jim Laskeyef42a012006-11-02 20:12:39 +00002463 // Add to context owner.
2464 Unit->getDie()->AddChild(SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002465
Jim Laskeyef42a012006-11-02 20:12:39 +00002466 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00002467 Unit->AddGlobal(FullName, SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002468
Jim Laskeyef42a012006-11-02 20:12:39 +00002469 return SubprogramDie;
2470 }
Jim Laskey65195462006-10-30 13:35:07 +00002471
2472 /// NewScopeVariable - Create a new scope variable.
2473 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002474 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
2475 // Get the descriptor.
2476 VariableDesc *VD = DV->getDesc();
2477
2478 // Translate tag to proper Dwarf tag. The result variable is dropped for
2479 // now.
2480 unsigned Tag;
2481 switch (VD->getTag()) {
2482 case DW_TAG_return_variable: return NULL;
2483 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2484 case DW_TAG_auto_variable: // fall thru
2485 default: Tag = DW_TAG_variable; break;
2486 }
2487
2488 // Define variable debug information entry.
2489 DIE *VariableDie = new DIE(Tag);
2490 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2491
2492 // Add source line info if available.
2493 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002494
Jim Laskeyef42a012006-11-02 20:12:39 +00002495 // Add variable type.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002496 AddType(VariableDie, VD->getType(), Unit);
2497
Jim Laskeyef42a012006-11-02 20:12:39 +00002498 // Add variable address.
2499 MachineLocation Location;
Evan Cheng72bebb92008-01-31 03:37:28 +00002500 Location.set(RI->getFrameRegister(*MF),
2501 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Jim Laskeyef42a012006-11-02 20:12:39 +00002502 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002503
Jim Laskeyef42a012006-11-02 20:12:39 +00002504 return VariableDie;
2505 }
Jim Laskey65195462006-10-30 13:35:07 +00002506
Devang Patel7a6e5a32009-01-08 02:33:41 +00002507 /// NewScopeVariable - Create a new scope variable.
2508 ///
2509 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
2510 // Get the descriptor.
2511 DIVariable *VD = DV->getVariable();
2512
2513 // Translate tag to proper Dwarf tag. The result variable is dropped for
2514 // now.
2515 unsigned Tag;
2516 switch (VD->getTag()) {
2517 case DW_TAG_return_variable: return NULL;
2518 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2519 case DW_TAG_auto_variable: // fall thru
2520 default: Tag = DW_TAG_variable; break;
2521 }
2522
2523 // Define variable debug information entry.
2524 DIE *VariableDie = new DIE(Tag);
2525 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2526
2527 // Add source line info if available.
2528 AddSourceLine(VariableDie, VD);
2529
2530 // Add variable type.
2531 AddType(Unit, VariableDie, VD->getType());
2532
2533 // Add variable address.
2534 MachineLocation Location;
2535 Location.set(RI->getFrameRegister(*MF),
2536 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2537 AddAddress(VariableDie, DW_AT_location, Location);
2538
2539 return VariableDie;
2540 }
2541
2542
2543 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2544 ///
2545 DbgScope *getOrCreateScope(GlobalVariable *V) {
2546 DbgScope *&Slot = DbgScopeMap[V];
2547 if (!Slot) {
2548 // FIXME - breaks down when the context is an inlined function.
2549 DIDescriptor ParentDesc;
2550 DIBlock *DB = new DIBlock(V);
2551 if (DIBlock *Block = dyn_cast<DIBlock>(DB)) {
2552 ParentDesc = Block->getContext();
2553 }
2554 DbgScope *Parent = ParentDesc.isNull() ?
2555 getOrCreateScope(ParentDesc.getGV()) : NULL;
2556 Slot = new DbgScope(Parent, DB);
2557 if (Parent) {
2558 Parent->AddScope(Slot);
2559 } else if (RootDbgScope) {
2560 // FIXME - Add inlined function scopes to the root so we can delete
2561 // them later. Long term, handle inlined functions properly.
2562 RootDbgScope->AddScope(Slot);
2563 } else {
2564 // First function is top level function.
2565 RootDbgScope = Slot;
2566 }
2567 }
2568 return Slot;
2569 }
2570
2571 /// ConstructDbgScope - Construct the components of a scope.
2572 ///
2573 void ConstructDbgScope(DbgScope *ParentScope,
2574 unsigned ParentStartID, unsigned ParentEndID,
2575 DIE *ParentDie, CompileUnit *Unit) {
2576 // Add variables to scope.
2577 SmallVector<DbgVariable *, 32> &Variables = ParentScope->getVariables();
2578 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2579 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2580 if (VariableDie) ParentDie->AddChild(VariableDie);
2581 }
2582
2583 // Add nested scopes.
2584 SmallVector<DbgScope *, 8> &Scopes = ParentScope->getScopes();
2585 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2586 // Define the Scope debug information entry.
2587 DbgScope *Scope = Scopes[j];
2588 // FIXME - Ignore inlined functions for the time being.
2589 if (!Scope->getParent()) continue;
2590
2591 unsigned StartID = MappedLabel(Scope->getStartLabelID());
2592 unsigned EndID = MappedLabel(Scope->getEndLabelID());
2593
2594 // Ignore empty scopes.
2595 if (StartID == EndID && StartID != 0) continue;
2596 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2597
2598 if (StartID == ParentStartID && EndID == ParentEndID) {
2599 // Just add stuff to the parent scope.
2600 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2601 } else {
2602 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2603
2604 // Add the scope bounds.
2605 if (StartID) {
2606 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2607 DWLabel("label", StartID));
2608 } else {
2609 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2610 DWLabel("func_begin", SubprogramCount));
2611 }
2612 if (EndID) {
2613 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2614 DWLabel("label", EndID));
2615 } else {
2616 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2617 DWLabel("func_end", SubprogramCount));
2618 }
2619
2620 // Add the scope contents.
2621 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2622 ParentDie->AddChild(ScopeDie);
2623 }
2624 }
2625 }
2626
2627 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2628 ///
2629 void ConstructRootDbgScope(DbgScope *RootScope) {
2630 // Exit if there is no root scope.
2631 if (!RootScope) return;
2632
2633 // Get the subprogram debug information entry.
2634 DISubprogram *SPD = cast<DISubprogram>(RootScope->getDesc());
2635
2636 // Get the compile unit context.
2637 CompileUnit *Unit = FindCompileUnit(SPD->getCompileUnit());
2638
2639 // Get the subprogram die.
2640 DIE *SPDie = Unit->getDieMapSlotFor(SPD->getGV());
2641 assert(SPDie && "Missing subprogram descriptor");
2642
2643 // Add the function bounds.
2644 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2645 DWLabel("func_begin", SubprogramCount));
2646 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2647 DWLabel("func_end", SubprogramCount));
2648 MachineLocation Location(RI->getFrameRegister(*MF));
2649 AddAddress(SPDie, DW_AT_frame_base, Location);
2650
2651 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2652 }
2653
2654 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2655 ///
2656 void ConstructDefaultDbgScope(MachineFunction *MF) {
2657 // Find the correct subprogram descriptor.
2658 std::string SPName = "llvm.dbg.subprograms";
2659 std::vector<GlobalVariable*> Result;
2660 getGlobalVariablesUsing(*M, SPName, Result);
2661 for (std::vector<GlobalVariable *>::iterator I = Result.begin(),
2662 E = Result.end(); I != E; ++I) {
2663
2664 DISubprogram *SPD = new DISubprogram(*I);
2665
2666 if (SPD->getName() == MF->getFunction()->getName()) {
2667 // Get the compile unit context.
2668 CompileUnit *Unit = FindCompileUnit(SPD->getCompileUnit());
2669
2670 // Get the subprogram die.
2671 DIE *SPDie = Unit->getDieMapSlotFor(SPD->getGV());
2672 assert(SPDie && "Missing subprogram descriptor");
2673
2674 // Add the function bounds.
2675 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2676 DWLabel("func_begin", SubprogramCount));
2677 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2678 DWLabel("func_end", SubprogramCount));
2679
2680 MachineLocation Location(RI->getFrameRegister(*MF));
2681 AddAddress(SPDie, DW_AT_frame_base, Location);
2682 return;
2683 }
2684 }
2685#if 0
2686 // FIXME: This is causing an abort because C++ mangled names are compared
2687 // with their unmangled counterparts. See PR2885. Don't do this assert.
2688 assert(0 && "Couldn't find DIE for machine function!");
2689#endif
2690 }
2691
Jim Laskey65195462006-10-30 13:35:07 +00002692 /// ConstructScope - Construct the components of a scope.
2693 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002694 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00002695 unsigned ParentStartID, unsigned ParentEndID,
2696 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002697 // Add variables to scope.
2698 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
2699 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2700 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
2701 if (VariableDie) ParentDie->AddChild(VariableDie);
2702 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002703
Jim Laskeyef42a012006-11-02 20:12:39 +00002704 // Add nested scopes.
2705 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
2706 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2707 // Define the Scope debug information entry.
2708 DebugScope *Scope = Scopes[j];
2709 // FIXME - Ignore inlined functions for the time being.
2710 if (!Scope->getParent()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002711
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002712 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2713 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00002714
Jim Laskey9d4209f2006-11-07 19:33:46 +00002715 // Ignore empty scopes.
2716 if (StartID == EndID && StartID != 0) continue;
2717 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002718
Jim Laskey36729dd2006-11-29 16:55:57 +00002719 if (StartID == ParentStartID && EndID == ParentEndID) {
2720 // Just add stuff to the parent scope.
2721 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002722 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00002723 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002724
Jim Laskey36729dd2006-11-29 16:55:57 +00002725 // Add the scope bounds.
2726 if (StartID) {
2727 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00002728 DWLabel("label", StartID));
Jim Laskey36729dd2006-11-29 16:55:57 +00002729 } else {
2730 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2731 DWLabel("func_begin", SubprogramCount));
2732 }
2733 if (EndID) {
2734 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00002735 DWLabel("label", EndID));
Jim Laskey36729dd2006-11-29 16:55:57 +00002736 } else {
2737 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2738 DWLabel("func_end", SubprogramCount));
2739 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002740
Jim Laskey36729dd2006-11-29 16:55:57 +00002741 // Add the scope contents.
2742 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
2743 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00002744 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002745 }
2746 }
Jim Laskey65195462006-10-30 13:35:07 +00002747
2748 /// ConstructRootScope - Construct the scope for the subprogram.
2749 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002750 void ConstructRootScope(DebugScope *RootScope) {
2751 // Exit if there is no root scope.
2752 if (!RootScope) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002753
2754 // Get the subprogram debug information entry.
Jim Laskeyef42a012006-11-02 20:12:39 +00002755 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002756
Jim Laskeyef42a012006-11-02 20:12:39 +00002757 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00002758 CompileUnit *Unit = GetBaseCompileUnit();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002759
Jim Laskeyef42a012006-11-02 20:12:39 +00002760 // Get the subprogram die.
2761 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2762 assert(SPDie && "Missing subprogram descriptor");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002763
Jim Laskeyef42a012006-11-02 20:12:39 +00002764 // Add the function bounds.
2765 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2766 DWLabel("func_begin", SubprogramCount));
2767 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2768 DWLabel("func_end", SubprogramCount));
2769 MachineLocation Location(RI->getFrameRegister(*MF));
2770 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002771
Jim Laskey36729dd2006-11-29 16:55:57 +00002772 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002773 }
Jim Laskey65195462006-10-30 13:35:07 +00002774
Bill Wendlingd751c642008-09-26 00:28:12 +00002775 /// ConstructDefaultScope - Construct a default scope for the subprogram.
2776 ///
2777 void ConstructDefaultScope(MachineFunction *MF) {
2778 // Find the correct subprogram descriptor.
2779 std::vector<SubprogramDesc *> Subprograms;
2780 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
2781
2782 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2783 SubprogramDesc *SPD = Subprograms[i];
2784
2785 if (SPD->getName() == MF->getFunction()->getName()) {
2786 // Get the compile unit context.
2787 CompileUnit *Unit = GetBaseCompileUnit();
2788
2789 // Get the subprogram die.
2790 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2791 assert(SPDie && "Missing subprogram descriptor");
2792
2793 // Add the function bounds.
2794 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2795 DWLabel("func_begin", SubprogramCount));
2796 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2797 DWLabel("func_end", SubprogramCount));
2798
2799 MachineLocation Location(RI->getFrameRegister(*MF));
2800 AddAddress(SPDie, DW_AT_frame_base, Location);
2801 return;
2802 }
2803 }
Bill Wendlinga9f0cc42008-10-17 18:48:57 +00002804#if 0
2805 // FIXME: This is causing an abort because C++ mangled names are compared
2806 // with their unmangled counterparts. See PR2885. Don't do this assert.
Bill Wendlingd751c642008-09-26 00:28:12 +00002807 assert(0 && "Couldn't find DIE for machine function!");
Bill Wendlinga9f0cc42008-10-17 18:48:57 +00002808#endif
Bill Wendlingd751c642008-09-26 00:28:12 +00002809 }
2810
Jim Laskeyef42a012006-11-02 20:12:39 +00002811 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2812 /// tools to recognize the object file contains Dwarf information.
2813 void EmitInitial() {
2814 // Check to see if we already emitted intial headers.
2815 if (didInitial) return;
2816 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002817
Jim Laskeyef42a012006-11-02 20:12:39 +00002818 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002819 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002820 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002821 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002822 }
2823 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2824 EmitLabel("section_info", 0);
2825 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2826 EmitLabel("section_abbrev", 0);
2827 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2828 EmitLabel("section_aranges", 0);
2829 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2830 EmitLabel("section_macinfo", 0);
2831 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2832 EmitLabel("section_line", 0);
2833 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2834 EmitLabel("section_loc", 0);
2835 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2836 EmitLabel("section_pubnames", 0);
2837 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2838 EmitLabel("section_str", 0);
2839 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2840 EmitLabel("section_ranges", 0);
2841
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002842 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002843 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002844 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002845 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002846 }
2847
Jim Laskey65195462006-10-30 13:35:07 +00002848 /// EmitDIE - Recusively Emits a debug information entry.
2849 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002850 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002851 // Get the abbreviation for this DIE.
2852 unsigned AbbrevNumber = Die->getAbbrevNumber();
2853 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002854
Jim Laskeybacd3042007-02-21 22:48:45 +00002855 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002856
2857 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002858 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002859
2860 if (VerboseAsm)
2861 Asm->EOL(std::string("Abbrev [" +
2862 utostr(AbbrevNumber) +
2863 "] 0x" + utohexstr(Die->getOffset()) +
2864 ":0x" + utohexstr(Die->getSize()) + " " +
2865 TagString(Abbrev->getTag())));
2866 else
2867 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002868
Owen Anderson873e1b52008-06-24 21:44:59 +00002869 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2870 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002871
Jim Laskeyef42a012006-11-02 20:12:39 +00002872 // Emit the DIE attribute values.
2873 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2874 unsigned Attr = AbbrevData[i].getAttribute();
2875 unsigned Form = AbbrevData[i].getForm();
2876 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002877
Jim Laskeyef42a012006-11-02 20:12:39 +00002878 switch (Attr) {
2879 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002880 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002881 break;
2882 }
2883 default: {
2884 // Emit an attribute using the defined form.
2885 Values[i]->EmitValue(*this, Form);
2886 break;
2887 }
2888 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002889
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002890 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002891 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002892
Jim Laskeyef42a012006-11-02 20:12:39 +00002893 // Emit the DIE children if any.
2894 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2895 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002896
Jim Laskeyef42a012006-11-02 20:12:39 +00002897 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2898 EmitDIE(Children[j]);
2899 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002900
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002901 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002902 }
2903 }
2904
Jim Laskey65195462006-10-30 13:35:07 +00002905 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2906 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002907 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2908 // Get the children.
2909 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002910
Jim Laskeyef42a012006-11-02 20:12:39 +00002911 // If not last sibling and has children then add sibling offset attribute.
2912 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2913
2914 // Record the abbreviation.
2915 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002916
Jim Laskeyef42a012006-11-02 20:12:39 +00002917 // Get the abbreviation for this DIE.
2918 unsigned AbbrevNumber = Die->getAbbrevNumber();
2919 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2920
2921 // Set DIE offset
2922 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002923
Jim Laskeyef42a012006-11-02 20:12:39 +00002924 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002925 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2926
Owen Anderson873e1b52008-06-24 21:44:59 +00002927 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2928 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002929
2930 // Size the DIE attribute values.
2931 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2932 // Size attribute value.
2933 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2934 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002935
Jim Laskeyef42a012006-11-02 20:12:39 +00002936 // Size the DIE children if any.
2937 if (!Children.empty()) {
2938 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2939 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002940
Jim Laskeyef42a012006-11-02 20:12:39 +00002941 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2942 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2943 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002944
Jim Laskeyef42a012006-11-02 20:12:39 +00002945 // End of children marker.
2946 Offset += sizeof(int8_t);
2947 }
2948
2949 Die->setSize(Offset - Die->getOffset());
2950 return Offset;
2951 }
Jim Laskey65195462006-10-30 13:35:07 +00002952
2953 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2954 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002955 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002956 // Process base compile unit.
2957 CompileUnit *Unit = GetBaseCompileUnit();
2958 // Compute size of compile unit header
2959 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2960 sizeof(int16_t) + // DWARF version number
2961 sizeof(int32_t) + // Offset Into Abbrev. Section
2962 sizeof(int8_t); // Pointer Size (in bytes)
2963 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00002964 }
2965
Jim Laskey65195462006-10-30 13:35:07 +00002966 /// EmitDebugInfo - Emit the debug info section.
2967 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002968 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002969 // Start debug info section.
2970 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002971
Jim Laskey5496f012006-11-09 14:52:14 +00002972 CompileUnit *Unit = GetBaseCompileUnit();
2973 DIE *Die = Unit->getDie();
2974 // Emit the compile units header.
2975 EmitLabel("info_begin", Unit->getID());
2976 // Emit size of content not including length itself
2977 unsigned ContentSize = Die->getSize() +
2978 sizeof(int16_t) + // DWARF version number
2979 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00002980 sizeof(int8_t) + // Pointer Size (in bytes)
2981 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002982
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002983 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2984 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00002985 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002986 Asm->EOL("Offset Into Abbrev. Section");
Dan Gohman82482942007-09-27 23:12:31 +00002987 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002988
Jim Laskey5496f012006-11-09 14:52:14 +00002989 EmitDIE(Die);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002990 // FIXME - extra padding for gdb bug.
2991 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2992 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2993 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2994 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Jim Laskey5496f012006-11-09 14:52:14 +00002995 EmitLabel("info_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002996
Jim Laskeybacd3042007-02-21 22:48:45 +00002997 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002998 }
2999
Jim Laskey65195462006-10-30 13:35:07 +00003000 /// EmitAbbreviations - Emit the abbreviation section.
3001 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003002 void EmitAbbreviations() const {
3003 // Check to see if it is worth the effort.
3004 if (!Abbreviations.empty()) {
3005 // Start the debug abbrev section.
3006 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003007
Jim Laskeyef42a012006-11-02 20:12:39 +00003008 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003009
Jim Laskeyef42a012006-11-02 20:12:39 +00003010 // For each abbrevation.
3011 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3012 // Get abbreviation data
3013 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003014
Jim Laskeyef42a012006-11-02 20:12:39 +00003015 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003016 Asm->EmitULEB128Bytes(Abbrev->getNumber());
3017 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003018
Jim Laskeyef42a012006-11-02 20:12:39 +00003019 // Emit the abbreviations data.
3020 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003021
Jim Laskeybacd3042007-02-21 22:48:45 +00003022 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003023 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003024
Jim Laskey7b1b39d2007-02-22 18:22:42 +00003025 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00003026 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00003027
Jim Laskeyef42a012006-11-02 20:12:39 +00003028 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003029
Jim Laskeybacd3042007-02-21 22:48:45 +00003030 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003031 }
3032 }
3033
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003034 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
3035 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003036 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003037 void EmitEndOfLineMatrix(unsigned SectionEnd) {
3038 // Define last address of section.
3039 Asm->EmitInt8(0); Asm->EOL("Extended Op");
3040 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
3041 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
3042 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
3043
3044 // Mark end of matrix.
3045 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
3046 Asm->EmitULEB128Bytes(1); Asm->EOL();
3047 Asm->EmitInt8(1); Asm->EOL();
3048 }
3049
Jim Laskey65195462006-10-30 13:35:07 +00003050 /// EmitDebugLines - Emit source line information.
3051 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003052 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003053 // If the target is using .loc/.file, the assembler will be emitting the
3054 // .debug_line table automatically.
3055 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00003056 return;
3057
Jim Laskeyef42a012006-11-02 20:12:39 +00003058 // Minimum line delta, thus ranging from -10..(255-10).
3059 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
3060 // Maximum line delta, thus ranging from -10..(255-10).
3061 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00003062
Jim Laskeyef42a012006-11-02 20:12:39 +00003063 // Start the dwarf line section.
3064 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003065
Jim Laskeyef42a012006-11-02 20:12:39 +00003066 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003067
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003068 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003069 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00003070 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003071
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003072 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003073
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003074 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003075 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00003076 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003077
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003078 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00003079
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003080 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00003081
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003082 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003083
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003084 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00003085
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003086 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003087
Jim Laskeyef42a012006-11-02 20:12:39 +00003088 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003089 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
3090 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
3091 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
3092 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
3093 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
3094 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
3095 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
3096 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
3097 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00003098
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003099 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Evan Cheng6547e402008-07-01 23:18:29 +00003100 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
Jim Laskeyef42a012006-11-02 20:12:39 +00003101
3102 // Emit directories.
3103 for (unsigned DirectoryID = 1, NDID = Directories.size();
3104 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003105 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00003106 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003107 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003108
Jim Laskeyef42a012006-11-02 20:12:39 +00003109 // Emit files.
3110 for (unsigned SourceID = 1, NSID = SourceFiles.size();
3111 SourceID <= NSID; ++SourceID) {
3112 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003113 Asm->EmitString(SourceFile.getName());
3114 Asm->EOL("Source");
3115 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
3116 Asm->EOL("Directory #");
3117 Asm->EmitULEB128Bytes(0);
3118 Asm->EOL("Mod date");
3119 Asm->EmitULEB128Bytes(0);
3120 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00003121 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003122 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003123
Jim Laskeyef42a012006-11-02 20:12:39 +00003124 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003125
Jim Laskeyef42a012006-11-02 20:12:39 +00003126 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003127 unsigned SecSrcLinesSize = SectionSourceLines.size();
3128
3129 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003130 // Isolate current sections line info.
3131 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00003132
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003133 if (VerboseAsm) {
3134 const Section* S = SectionMap[j + 1];
3135 Asm->EOL(std::string("Section ") + S->getName());
3136 } else
Evan Cheng6547e402008-07-01 23:18:29 +00003137 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003138
3139 // Dwarf assumes we start with first line of first source file.
3140 unsigned Source = 1;
3141 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003142
Jim Laskeyef42a012006-11-02 20:12:39 +00003143 // Construct rows of the address, source, line, column matrix.
3144 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3145 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003146 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00003147 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003148
Jim Laskey1a4a83c2007-01-25 15:45:58 +00003149 unsigned SourceID = LineInfo.getSourceID();
3150 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
3151 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng6547e402008-07-01 23:18:29 +00003152 if (VerboseAsm)
3153 Asm->EOL(Directories[DirectoryID]
3154 + SourceFile.getName()
3155 + ":"
3156 + utostr_32(LineInfo.getLine()));
3157 else
3158 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003159
3160 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003161 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00003162 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003163 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00003164 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003165
Jim Laskeyef42a012006-11-02 20:12:39 +00003166 // If change of source, then switch to the new source.
3167 if (Source != LineInfo.getSourceID()) {
3168 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003169 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
3170 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00003171 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003172
Jim Laskeyef42a012006-11-02 20:12:39 +00003173 // If change of line.
3174 if (Line != LineInfo.getLine()) {
3175 // Determine offset.
3176 int Offset = LineInfo.getLine() - Line;
3177 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003178
Jim Laskeyef42a012006-11-02 20:12:39 +00003179 // Update line.
3180 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003181
Jim Laskeyef42a012006-11-02 20:12:39 +00003182 // If delta is small enough and in range...
3183 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3184 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003185 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00003186 } else {
3187 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003188 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
3189 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
3190 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00003191 }
3192 } else {
3193 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003194 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00003195 }
3196 }
3197
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003198 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00003199 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003200
3201 if (SecSrcLinesSize == 0)
3202 // Because we're emitting a debug_line section, we still need a line
3203 // table. The linker and friends expect it to exist. If there's nothing to
3204 // put into it, emit an empty table.
3205 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003206
Jim Laskeyef42a012006-11-02 20:12:39 +00003207 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003208
Jim Laskeybacd3042007-02-21 22:48:45 +00003209 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003210 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003211
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003212 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00003213 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003214 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00003215 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00003216 return;
3217
3218 int stackGrowth =
3219 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3220 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003221 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00003222
3223 // Start the dwarf frame section.
3224 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
3225
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003226 EmitLabel("debug_frame_common", 0);
3227 EmitDifference("debug_frame_common_end", 0,
3228 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003229 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00003230
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003231 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003232 Asm->EmitInt32((int)DW_CIE_ID);
3233 Asm->EOL("CIE Identifier Tag");
3234 Asm->EmitInt8(DW_CIE_VERSION);
3235 Asm->EOL("CIE Version");
3236 Asm->EmitString("");
3237 Asm->EOL("CIE Augmentation");
3238 Asm->EmitULEB128Bytes(1);
3239 Asm->EOL("CIE Code Alignment Factor");
3240 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003241 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003242 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003243 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003244
Jim Laskey5e73d5b2007-01-24 18:45:13 +00003245 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00003246 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003247
Dale Johannesenb97aec62007-11-13 19:13:01 +00003248 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00003249
Evan Cheng05548eb2008-02-29 19:36:59 +00003250 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003251 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003252
Jim Laskeybacd3042007-02-21 22:48:45 +00003253 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003254 }
3255
Jim Laskey65195462006-10-30 13:35:07 +00003256 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
3257 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003258 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00003259 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00003260 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003261
Jim Laskeyef42a012006-11-02 20:12:39 +00003262 // Start the dwarf frame section.
3263 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003264
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003265 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
3266 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003267 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003268
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003269 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003270
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003271 EmitSectionOffset("debug_frame_common", "section_debug_frame",
3272 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003273 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00003274
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003275 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003276 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003277 EmitDifference("func_end", DebugFrameInfo.Number,
3278 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003279 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003280
Dale Johannesenb97aec62007-11-13 19:13:01 +00003281 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003282
Evan Cheng05548eb2008-02-29 19:36:59 +00003283 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003284 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00003285
Jim Laskeybacd3042007-02-21 22:48:45 +00003286 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003287 }
3288
3289 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00003290 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003291 void EmitDebugPubNames() {
3292 // Start the dwarf pubnames section.
3293 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003294
3295 CompileUnit *Unit = GetBaseCompileUnit();
3296
Jim Laskey5496f012006-11-09 14:52:14 +00003297 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003298 "pubnames_begin", Unit->getID(), true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003299 Asm->EOL("Length of Public Names Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003300
Jim Laskey5496f012006-11-09 14:52:14 +00003301 EmitLabel("pubnames_begin", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003302
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003303 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003304
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00003305 EmitSectionOffset("info_begin", "section_info",
3306 Unit->getID(), 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003307 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00003308
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003309 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003310 Asm->EOL("Compilation Unit Length");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003311
Jim Laskey5496f012006-11-09 14:52:14 +00003312 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003313
Jim Laskey5496f012006-11-09 14:52:14 +00003314 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
3315 GE = Globals.end();
3316 GI != GE; ++GI) {
3317 const std::string &Name = GI->first;
3318 DIE * Entity = GI->second;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003319
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003320 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
3321 Asm->EmitString(Name); Asm->EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00003322 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003323
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003324 Asm->EmitInt32(0); Asm->EOL("End Mark");
Jim Laskey5496f012006-11-09 14:52:14 +00003325 EmitLabel("pubnames_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003326
Jim Laskeybacd3042007-02-21 22:48:45 +00003327 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003328 }
3329
3330 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00003331 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003332 void EmitDebugStr() {
3333 // Check to see if it is worth the effort.
3334 if (!StringPool.empty()) {
3335 // Start the dwarf str section.
3336 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003337
Jim Laskeyef42a012006-11-02 20:12:39 +00003338 // For each of strings in the string pool.
3339 for (unsigned StringID = 1, N = StringPool.size();
3340 StringID <= N; ++StringID) {
3341 // Emit a label for reference from debug information entries.
3342 EmitLabel("string", StringID);
3343 // Emit the string itself.
3344 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00003345 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003346 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003347
Jim Laskeybacd3042007-02-21 22:48:45 +00003348 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003349 }
3350 }
3351
3352 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00003353 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003354 void EmitDebugLoc() {
3355 // Start the dwarf loc section.
3356 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003357
Jim Laskeybacd3042007-02-21 22:48:45 +00003358 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003359 }
3360
3361 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00003362 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003363 void EmitDebugARanges() {
3364 // Start the dwarf aranges section.
3365 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003366
Jim Laskeyef42a012006-11-02 20:12:39 +00003367 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00003368#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003369 CompileUnit *Unit = GetBaseCompileUnit();
3370
Jim Laskey5496f012006-11-09 14:52:14 +00003371 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003372 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003373
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003374 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003375
Jim Laskey5496f012006-11-09 14:52:14 +00003376 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003377 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00003378
Dan Gohman82482942007-09-27 23:12:31 +00003379 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00003380
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003381 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00003382
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003383 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
3384 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00003385
Jim Laskey5496f012006-11-09 14:52:14 +00003386 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003387 EmitReference("text_begin", 0); Asm->EOL("Address");
3388 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00003389
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003390 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
3391 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00003392#endif
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 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00003398 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003399 void EmitDebugRanges() {
3400 // Start the dwarf ranges section.
3401 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003402
Jim Laskeybacd3042007-02-21 22:48:45 +00003403 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003404 }
3405
3406 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00003407 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003408 void EmitDebugMacInfo() {
3409 // Start the dwarf macinfo section.
3410 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003411
Jim Laskeybacd3042007-02-21 22:48:45 +00003412 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003413 }
3414
Devang Patelc4523242009-01-05 23:11:11 +00003415 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00003416 void ConstructCompileUnits() {
3417 std::string CUName = "llvm.dbg.compile_units";
3418 std::vector<GlobalVariable*> Result;
3419 getGlobalVariablesUsing(*M, CUName, Result);
3420 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3421 RE = Result.end(); RI != RE; ++RI) {
3422 DICompileUnit *DIUnit = new DICompileUnit(*RI);
3423 unsigned DID = Directories.insert(DIUnit->getDirectory());
3424 unsigned ID = SrcFiles.insert(SrcFileInfo(DID,
3425 DIUnit->getFilename()));
3426
3427 DIE *Die = new DIE(DW_TAG_compile_unit);
3428 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
3429 DWLabel("section_line", 0), DWLabel("section_line", 0),
3430 false);
3431 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit->getProducer());
3432 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit->getLanguage());
3433 AddString(Die, DW_AT_name, DW_FORM_string, DIUnit->getFilename());
3434 if (!DIUnit->getDirectory().empty())
3435 AddString(Die, DW_AT_comp_dir, DW_FORM_string, DIUnit->getDirectory());
3436
3437 CompileUnit *Unit = new CompileUnit(ID, Die);
3438 DW_CUs[DIUnit->getGV()] = Unit;
3439 }
3440 }
3441
Jim Laskey65195462006-10-30 13:35:07 +00003442 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
3443 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00003444 void ConstructCompileUnitDIEs() {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003445 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003446
Jim Laskeyef42a012006-11-02 20:12:39 +00003447 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003448 unsigned ID = MMI->RecordSource(CUW[i]);
Jim Laskey9d4209f2006-11-07 19:33:46 +00003449 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00003450 CompileUnits.push_back(Unit);
3451 }
3452 }
3453
Devang Patelc4523242009-01-05 23:11:11 +00003454 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
3455 /// visible global variables.
3456 void ConstructGlobalVariableDIEs() {
3457 std::string GVName = "llvm.dbg.global_variables";
3458 std::vector<GlobalVariable*> Result;
3459 getGlobalVariablesUsing(*M, GVName, Result);
3460 for (std::vector<GlobalVariable *>::iterator GVI = Result.begin(),
3461 GVE = Result.end(); GVI != GVE; ++GVI) {
3462 DIGlobalVariable *DI_GV = new DIGlobalVariable(*GVI);
3463 CompileUnit *DW_Unit = FindCompileUnit(DI_GV->getCompileUnit());
3464
3465 // Check for pre-existence.
3466 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV->getGV());
3467 if (Slot) continue;
3468
3469 DIE *VariableDie = new DIE(DW_TAG_variable);
3470 AddString(VariableDie, DW_AT_name, DW_FORM_string, DI_GV->getName());
3471 const std::string &LinkageName = DI_GV->getLinkageName();
3472 if (!LinkageName.empty())
3473 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3474 LinkageName);
3475 AddType(DW_Unit, VariableDie, DI_GV->getType());
3476
3477 if (!DI_GV->isLocalToUnit())
3478 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
3479
3480 // Add source line info, if available.
3481 AddSourceLine(VariableDie, DI_GV);
3482
3483 // Add address.
3484 DIEBlock *Block = new DIEBlock();
3485 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
3486 AddObjectLabel(Block, 0, DW_FORM_udata,
3487 Asm->getGlobalLinkName(DI_GV->getGV()));
3488 AddBlock(VariableDie, DW_AT_location, 0, Block);
3489
3490 //Add to map.
3491 Slot = VariableDie;
3492
3493 //Add to context owner.
3494 DW_Unit->getDie()->AddChild(VariableDie);
3495
3496 //Expose as global. FIXME - need to check external flag.
3497 DW_Unit->AddGlobal(DI_GV->getName(), VariableDie);
3498 }
3499 }
3500
Jim Laskey65195462006-10-30 13:35:07 +00003501 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
3502 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00003503 void ConstructGlobalDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00003504 std::vector<GlobalVariableDesc *> GlobalVariables;
3505 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M, GlobalVariables);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003506
Bill Wendling10fff602008-07-03 22:53:42 +00003507 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
3508 GlobalVariableDesc *GVD = GlobalVariables[i];
3509 NewGlobalVariable(GVD);
3510 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003511 }
Jim Laskey65195462006-10-30 13:35:07 +00003512
Devang Patel78eb6ad2009-01-05 23:21:35 +00003513 /// ConstructSubprograms - Create DIEs for each of the externally visible
3514 /// subprograms.
3515 void ConstructSubprograms() {
3516
3517 std::string SPName = "llvm.dbg.subprograms";
3518 std::vector<GlobalVariable*> Result;
3519 getGlobalVariablesUsing(*M, SPName, Result);
3520 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3521 RE = Result.end(); RI != RE; ++RI) {
3522
3523 DISubprogram *SP = new DISubprogram(*RI);
3524 CompileUnit *Unit = FindCompileUnit(SP->getCompileUnit());
3525
3526 // Check for pre-existence.
3527 DIE *&Slot = Unit->getDieMapSlotFor(SP->getGV());
3528 if (Slot) continue;
3529
3530 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
3531 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, SP->getName());
3532 const std::string &LinkageName = SP->getLinkageName();
3533 if (!LinkageName.empty())
3534 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3535 LinkageName);
3536 DIType SPTy = SP->getType();
3537 AddType(Unit, SubprogramDie, SPTy);
3538 if (!SP->isLocalToUnit())
3539 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
3540 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
3541
3542 AddSourceLine(SubprogramDie, SP);
3543 //Add to map.
3544 Slot = SubprogramDie;
3545 //Add to context owner.
3546 Unit->getDie()->AddChild(SubprogramDie);
3547 //Expose as global.
3548 Unit->AddGlobal(SP->getName(), SubprogramDie);
3549 }
3550 }
3551
Jim Laskey65195462006-10-30 13:35:07 +00003552 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
3553 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00003554 void ConstructSubprogramDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00003555 std::vector<SubprogramDesc *> Subprograms;
3556 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003557
Bill Wendling10fff602008-07-03 22:53:42 +00003558 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
3559 SubprogramDesc *SPD = Subprograms[i];
3560 NewSubprogram(SPD);
3561 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003562 }
Jim Laskey65195462006-10-30 13:35:07 +00003563
Jim Laskey65195462006-10-30 13:35:07 +00003564public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003565 //===--------------------------------------------------------------------===//
3566 // Main entry points.
3567 //
Owen Andersoncb371882008-08-21 00:14:44 +00003568 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00003569 : Dwarf(OS, A, T, "dbg")
Jim Laskeyef42a012006-11-02 20:12:39 +00003570 , CompileUnits()
3571 , AbbreviationsSet(InitAbbreviationsSetSize)
3572 , Abbreviations()
3573 , ValuesSet(InitValuesSetSize)
3574 , Values()
3575 , StringPool()
3576 , DescToUnitMap()
3577 , SectionMap()
3578 , SectionSourceLines()
Jim Laskeybacd3042007-02-21 22:48:45 +00003579 , didInitial(false)
3580 , shouldEmit(false)
Devang Patel7a6e5a32009-01-08 02:33:41 +00003581 , RootDbgScope(NULL)
Jim Laskeyef42a012006-11-02 20:12:39 +00003582 {
3583 }
Jim Laskey072200c2007-01-29 18:51:14 +00003584 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003585 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
3586 delete CompileUnits[i];
3587 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3588 delete Values[j];
3589 }
3590
Devang Patel5d315982009-01-06 21:07:30 +00003591 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3592 /// This is inovked by the target AsmPrinter.
3593 void SetDebugInfo() {
3594 // FIXME - Check if the module has debug info or not.
3595 // Create all the compile unit DIEs.
3596 ConstructCompileUnits();
3597
3598 // Create DIEs for each of the externally visible global variables.
3599 ConstructGlobalVariableDIEs();
3600
3601 // Create DIEs for each of the externally visible subprograms.
3602 ConstructSubprograms();
3603
3604 // Prime section data.
3605 SectionMap.insert(TAI->getTextSection());
3606
3607 // Print out .file directives to specify files for .loc directives. These
3608 // are printed out early so that they precede any .loc directives.
3609 if (TAI->hasDotLocAndDotFile()) {
3610 for (unsigned i = 1, e = SrcFiles.size(); i <= e; ++i) {
3611 sys::Path FullPath(Directories[SrcFiles[i].getDirectoryID()]);
3612 bool AppendOk = FullPath.appendComponent(SrcFiles[i].getName());
3613 assert(AppendOk && "Could not append filename to directory!");
3614 AppendOk = false;
3615 Asm->EmitFile(i, FullPath.toString());
3616 Asm->EOL();
3617 }
3618 }
3619
3620 // Emit initial sections
3621 EmitInitial();
3622 }
3623
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003624 /// SetModuleInfo - Set machine module information when it's known that pass
3625 /// manager has created it. Set by the target AsmPrinter.
3626 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003627 // Make sure initial declarations are made.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003628 if (!MMI && mmi->hasDebugInfo()) {
3629 MMI = mmi;
Jim Laskeyef42a012006-11-02 20:12:39 +00003630 shouldEmit = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003631
Jim Laskeyef42a012006-11-02 20:12:39 +00003632 // Create all the compile unit DIEs.
3633 ConstructCompileUnitDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003634
Jim Laskeyef42a012006-11-02 20:12:39 +00003635 // Create DIEs for each of the externally visible global variables.
3636 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00003637
Jim Laskeyef42a012006-11-02 20:12:39 +00003638 // Create DIEs for each of the externally visible subprograms.
3639 ConstructSubprogramDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003640
Jim Laskeyef42a012006-11-02 20:12:39 +00003641 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00003642 SectionMap.insert(TAI->getTextSection());
Dan Gohmand57c3882007-10-01 22:40:20 +00003643
3644 // Print out .file directives to specify files for .loc directives. These
3645 // are printed out early so that they precede any .loc directives.
3646 if (TAI->hasDotLocAndDotFile()) {
3647 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
3648 const UniqueVector<std::string> &Directories = MMI->getDirectories();
3649 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
3650 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
3651 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
3652 assert(AppendOk && "Could not append filename to directory!");
Devang Pateld0935c32008-12-23 21:55:38 +00003653 AppendOk = false;
Dan Gohmand57c3882007-10-01 22:40:20 +00003654 Asm->EmitFile(i, FullPath.toString());
3655 Asm->EOL();
3656 }
3657 }
3658
3659 // Emit initial sections
3660 EmitInitial();
Jim Laskeyef42a012006-11-02 20:12:39 +00003661 }
3662 }
3663
Jim Laskey65195462006-10-30 13:35:07 +00003664 /// BeginModule - Emit all Dwarf sections that should come prior to the
3665 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003666 void BeginModule(Module *M) {
3667 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003668 }
3669
Jim Laskey65195462006-10-30 13:35:07 +00003670 /// EndModule - Emit all Dwarf sections that should come after the content.
3671 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003672 void EndModule() {
3673 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003674
Jim Laskeyef42a012006-11-02 20:12:39 +00003675 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003676 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003677 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003678 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003679 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003680
Jim Laskeyef42a012006-11-02 20:12:39 +00003681 // End text sections.
3682 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003683 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003684 EmitLabel("section_end", i);
3685 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003686
3687 // Emit common frame information.
3688 EmitCommonDebugFrame();
3689
3690 // Emit function debug frame information
3691 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3692 E = DebugFrames.end(); I != E; ++I)
3693 EmitFunctionDebugFrame(*I);
3694
Jim Laskeyef42a012006-11-02 20:12:39 +00003695 // Compute DIE offsets and sizes.
3696 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003697
Jim Laskeyef42a012006-11-02 20:12:39 +00003698 // Emit all the DIEs into a debug info section
3699 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003700
Jim Laskeyef42a012006-11-02 20:12:39 +00003701 // Corresponding abbreviations into a abbrev section.
3702 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003703
Jim Laskeyef42a012006-11-02 20:12:39 +00003704 // Emit source line correspondence into a debug line section.
3705 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003706
Jim Laskeyef42a012006-11-02 20:12:39 +00003707 // Emit info into a debug pubnames section.
3708 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003709
Jim Laskeyef42a012006-11-02 20:12:39 +00003710 // Emit info into a debug str section.
3711 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003712
Jim Laskeyef42a012006-11-02 20:12:39 +00003713 // Emit info into a debug loc section.
3714 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003715
Jim Laskeyef42a012006-11-02 20:12:39 +00003716 // Emit info into a debug aranges section.
3717 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003718
Jim Laskeyef42a012006-11-02 20:12:39 +00003719 // Emit info into a debug ranges section.
3720 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003721
Jim Laskeyef42a012006-11-02 20:12:39 +00003722 // Emit info into a debug macinfo section.
3723 EmitDebugMacInfo();
3724 }
3725
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003726 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003727 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003728 void BeginFunction(MachineFunction *MF) {
3729 this->MF = MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003730
Jim Laskeyef42a012006-11-02 20:12:39 +00003731 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003732
3733 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003734 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003735
Jim Laskeyef42a012006-11-02 20:12:39 +00003736 // Assumes in correct section after the entry point.
3737 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003738
3739 // Emit label for the implicitly defined dbg.stoppoint at the start of
3740 // the function.
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003741 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
3742 if (!LineInfos.empty()) {
3743 const SourceLineInfo &LineInfo = LineInfos[0];
3744 Asm->printLabel(LineInfo.getLabelID());
3745 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003746 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003747
Jim Laskey65195462006-10-30 13:35:07 +00003748 /// EndFunction - Gather and emit post-function debug information.
3749 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003750 void EndFunction(MachineFunction *MF) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003751 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003752
Jim Laskeyb82313f2007-02-01 16:31:34 +00003753 // Define end label for subprogram.
3754 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003755
Jim Laskeyef42a012006-11-02 20:12:39 +00003756 // Get function line info.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003757 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
Jim Laskeyef42a012006-11-02 20:12:39 +00003758
3759 if (!LineInfos.empty()) {
3760 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003761 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003762 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
3763 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
3764 // Append the function info to section info.
3765 SectionLineInfos.insert(SectionLineInfos.end(),
3766 LineInfos.begin(), LineInfos.end());
3767 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003768
Jim Laskeyef42a012006-11-02 20:12:39 +00003769 // Construct scopes for subprogram.
Bill Wendlingd751c642008-09-26 00:28:12 +00003770 if (MMI->getRootScope())
3771 ConstructRootScope(MMI->getRootScope());
3772 else
3773 // FIXME: This is wrong. We are essentially getting past a problem with
3774 // debug information not being able to handle unreachable blocks that have
3775 // debug information in them. In particular, those unreachable blocks that
3776 // have "region end" info in them. That situation results in the "root
3777 // scope" not being created. If that's the case, then emit a "default"
3778 // scope, i.e., one that encompasses the whole function. This isn't
3779 // desirable. And a better way of handling this (and all of the debugging
3780 // information) needs to be explored.
3781 ConstructDefaultScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003782
3783 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3784 MMI->getFrameMoves()));
Jim Laskeyef42a012006-11-02 20:12:39 +00003785 }
Jim Laskey65195462006-10-30 13:35:07 +00003786};
3787
Jim Laskey072200c2007-01-29 18:51:14 +00003788//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003789/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003790///
3791class DwarfException : public Dwarf {
3792
Jim Laskeyb82313f2007-02-01 16:31:34 +00003793private:
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003794 struct FunctionEHFrameInfo {
3795 std::string FnName;
3796 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003797 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003798 bool hasCalls;
3799 bool hasLandingPads;
3800 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003801 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003802
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003803 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3804 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003805 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003806 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003807 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003808 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003809 };
3810
3811 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003812
3813 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3814 /// be emitted.
3815 bool shouldEmitTable;
3816
3817 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3818 /// should be emitted.
3819 bool shouldEmitMoves;
3820
3821 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3822 /// should be emitted.
3823 bool shouldEmitTableModule;
3824
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003825 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003826 /// should be emitted.
3827 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003828
Jim Laskey3f09fc22007-02-28 18:38:31 +00003829 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3830 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003831 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003832 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003833 int stackGrowth =
3834 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3835 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003836 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003837
Jim Laskeybacd3042007-02-21 22:48:45 +00003838 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003839 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003840
3841 if (!TAI->doesRequireNonLocalEHFrameLabel())
3842 O << TAI->getEHGlobalPrefix();
3843 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003844 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003845
Jim Laskeybacd3042007-02-21 22:48:45 +00003846 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003847 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003848
Jim Laskeybacd3042007-02-21 22:48:45 +00003849 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003850 EmitDifference("eh_frame_common_end", Index,
3851 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003852 Asm->EOL("Length of Common Information Entry");
3853
Jim Laskeybacd3042007-02-21 22:48:45 +00003854 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003855 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003856 Asm->EmitInt32((int)0);
3857 Asm->EOL("CIE Identifier Tag");
3858 Asm->EmitInt8(DW_CIE_VERSION);
3859 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003860
Jim Laskeybacd3042007-02-21 22:48:45 +00003861 // The personality presence indicates that language specific information
3862 // will show up in the eh frame.
3863 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003864 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003865
Jim Laskeybacd3042007-02-21 22:48:45 +00003866 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003867 Asm->EmitULEB128Bytes(1);
3868 Asm->EOL("CIE Code Alignment Factor");
3869 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003870 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003871 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003872 Asm->EOL("CIE Return Address Column");
3873
Jim Laskeybacd3042007-02-21 22:48:45 +00003874 // If there is a personality, we need to indicate the functions location.
3875 if (Personality) {
3876 Asm->EmitULEB128Bytes(7);
3877 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003878
Duncan Sands671fa972008-05-07 19:11:09 +00003879 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003880 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003881 Asm->EOL("Personality (pcrel sdata4 indirect)");
3882 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003883 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003884 Asm->EOL("Personality (pcrel sdata4)");
3885 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003886
Duncan Sands671fa972008-05-07 19:11:09 +00003887 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003888 O << TAI->getPersonalityPrefix();
3889 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3890 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003891 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3892 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003893 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003894
Duncan Sands671fa972008-05-07 19:11:09 +00003895 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3896 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003897
Bill Wendlingd60de512009-01-05 22:53:45 +00003898 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3899 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003900 } else {
3901 Asm->EmitULEB128Bytes(1);
3902 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003903
Bill Wendlingd60de512009-01-05 22:53:45 +00003904 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3905 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003906 }
3907
3908 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003909 std::vector<MachineMove> Moves;
3910 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003911 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003912
Dale Johannesen21d972a2008-04-30 00:43:29 +00003913 // On Darwin the linker honors the alignment of eh_frame, which means it
3914 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3915 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003916 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003917 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003918 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003919
Jim Laskeybacd3042007-02-21 22:48:45 +00003920 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003921 }
Duncan Sands671fa972008-05-07 19:11:09 +00003922
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003923 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003924 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003925 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003926 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3927
Jim Laskeybacd3042007-02-21 22:48:45 +00003928 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3929
3930 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003931 // If the corresponding function is static, this should not be
3932 // externally visible.
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003933 if (linkage != Function::InternalLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003934 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3935 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3936 }
3937
Dale Johannesen038129d2008-01-10 02:03:30 +00003938 // If corresponding function is weak definition, this should be too.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003939 if ((linkage == Function::WeakLinkage ||
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003940 linkage == Function::LinkOnceLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003941 TAI->getWeakDefDirective())
3942 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3943
3944 // If there are no calls then you can't unwind. This may mean we can
3945 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003946 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003947 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003948 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003949 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003950 !UnwindTablesMandatory &&
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003951 ((linkage != Function::WeakLinkage &&
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003952 linkage != Function::LinkOnceLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003953 !TAI->getWeakDefDirective() ||
3954 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003955 {
Bill Wendling6e198962007-09-18 01:47:22 +00003956 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003957 // This name has no connection to the function, so it might get
3958 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003959 // dead-stripping unconditionally.
3960 if (const char *UsedDirective = TAI->getUsedDirective())
3961 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003962 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003963 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003964
Jim Laskeybacd3042007-02-21 22:48:45 +00003965 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003966 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3967 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003968 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003969
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003970 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003971
Bill Wendling722f5f12008-12-24 08:05:17 +00003972 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3973 PrintRelDirective(true, true);
3974 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3975
3976 if (!TAI->isAbsoluteEHSectionOffsets())
3977 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3978 } else {
3979 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3980 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3981 true, true, false);
3982 }
3983
Jim Laskeybacd3042007-02-21 22:48:45 +00003984 Asm->EOL("FDE CIE offset");
3985
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003986 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003987 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003988 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003989 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003990 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003991
Jim Laskey3f09fc22007-02-28 18:38:31 +00003992 // If there is a personality and landing pads then point to the language
3993 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003994 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003995 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003996 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003997
3998 if (EHFrameInfo.hasLandingPads)
3999 EmitReference("exception", EHFrameInfo.Number, true, true);
4000 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00004001 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00004002 Asm->EOL("Language Specific Data Area");
4003 } else {
4004 Asm->EmitULEB128Bytes(0);
4005 Asm->EOL("Augmentation size");
4006 }
Duncan Sands671fa972008-05-07 19:11:09 +00004007
Jim Laskeybacd3042007-02-21 22:48:45 +00004008 // Indicate locations of function specific callee saved registers in
4009 // frame.
Dale Johannesenb97aec62007-11-13 19:13:01 +00004010 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004011
Dale Johannesen21d972a2008-04-30 00:43:29 +00004012 // On Darwin the linker honors the alignment of eh_frame, which means it
4013 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
4014 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004015 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00004016 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004017 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004018
4019 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004020 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004021 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004022 // that depends on unused functions (calling undefined externals) being
4023 // dead-stripped to link correctly. Yes, there really is.
4024 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
4025 if (const char *UsedDirective = TAI->getUsedDirective())
4026 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
4027 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004028 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00004029
Duncan Sands57810cd2007-09-05 11:27:52 +00004030 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004031 ///
4032 /// The general organization of the table is complex, but the basic concepts
4033 /// are easy. First there is a header which describes the location and
4034 /// organization of the three components that follow.
4035 /// 1. The landing pad site information describes the range of code covered
4036 /// by the try. In our case it's an accumulation of the ranges covered
4037 /// by the invokes in the try. There is also a reference to the landing
4038 /// pad that handles the exception once processed. Finally an index into
4039 /// the actions table.
4040 /// 2. The action table, in our case, is composed of pairs of type ids
4041 /// and next action offset. Starting with the action index from the
4042 /// landing pad site, each type Id is checked for a match to the current
4043 /// exception. If it matches then the exception and type id are passed
4044 /// on to the landing pad. Otherwise the next action is looked up. This
4045 /// chain is terminated with a next action of zero. If no type id is
4046 /// found the the frame is unwound and handling continues.
4047 /// 3. Type id table contains references to all the C++ typeinfo for all
4048 /// catches in the function. This tables is reversed indexed base 1.
4049
Duncan Sandsb32edb42007-06-06 15:37:31 +00004050 /// SharedTypeIds - How many leading type ids two landing pads have in common.
4051 static unsigned SharedTypeIds(const LandingPadInfo *L,
4052 const LandingPadInfo *R) {
4053 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004054 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004055 unsigned MinSize = LSize < RSize ? LSize : RSize;
4056 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004057
Duncan Sandsb32edb42007-06-06 15:37:31 +00004058 for (; Count != MinSize; ++Count)
4059 if (LIds[Count] != RIds[Count])
4060 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004061
Duncan Sandsb32edb42007-06-06 15:37:31 +00004062 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004063 }
4064
Duncan Sandsb32edb42007-06-06 15:37:31 +00004065 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00004066 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004067 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004068 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004069 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004070
Duncan Sandsb32edb42007-06-06 15:37:31 +00004071 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00004072 if (LIds[i] != RIds[i])
4073 return LIds[i] < RIds[i];
4074
Duncan Sandsb32edb42007-06-06 15:37:31 +00004075 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004076 }
4077
Duncan Sands53c3a332007-05-16 12:12:23 +00004078 struct KeyInfo {
4079 static inline unsigned getEmptyKey() { return -1U; }
4080 static inline unsigned getTombstoneKey() { return -2U; }
4081 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00004082 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00004083 static bool isPod() { return true; }
4084 };
4085
Duncan Sands57810cd2007-09-05 11:27:52 +00004086 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004087 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004088 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004089 int NextAction;
4090 struct ActionEntry *Previous;
4091 };
4092
Duncan Sands57810cd2007-09-05 11:27:52 +00004093 /// PadRange - Structure holding a try-range and the associated landing pad.
4094 struct PadRange {
4095 // The index of the landing pad.
4096 unsigned PadIndex;
4097 // The index of the begin and end labels in the landing pad's label lists.
4098 unsigned RangeIndex;
4099 };
4100
4101 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
4102
4103 /// CallSiteEntry - Structure describing an entry in the call-site table.
4104 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00004105 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004106 unsigned BeginLabel; // zero indicates the start of the function.
4107 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00004108 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004109 unsigned PadLabel; // zero indicates that there is no landing pad.
4110 unsigned Action;
4111 };
4112
Jim Laskeybacd3042007-02-21 22:48:45 +00004113 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00004114 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00004115 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00004116 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
4117 if (PadInfos.empty()) return;
4118
4119 // Sort the landing pads in order of their type ids. This is used to fold
4120 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00004121 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00004122 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00004123 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00004124 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004125 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
4126
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004127 // Negative type ids index into FilterIds, positive type ids index into
4128 // TypeInfos. The value written for a positive type id is just the type
4129 // id itself. For a negative type id, however, the value written is the
4130 // (negative) byte offset of the corresponding FilterIds entry. The byte
4131 // offset is usually equal to the type id, because the FilterIds entries
4132 // are written using a variable width encoding which outputs one byte per
4133 // entry as long as the value written is not too large, but can differ.
4134 // This kind of complication does not occur for positive type ids because
4135 // type infos are output using a fixed width encoding.
4136 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
4137 SmallVector<int, 16> FilterOffsets;
4138 FilterOffsets.reserve(FilterIds.size());
4139 int Offset = -1;
4140 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
4141 E = FilterIds.end(); I != E; ++I) {
4142 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004143 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004144 }
4145
Duncan Sands57810cd2007-09-05 11:27:52 +00004146 // Compute the actions table and gather the first action index for each
4147 // landing pad site.
4148 SmallVector<ActionEntry, 32> Actions;
4149 SmallVector<unsigned, 64> FirstActions;
4150 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00004151
Duncan Sandsb32edb42007-06-06 15:37:31 +00004152 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00004153 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004154 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004155 const LandingPadInfo *LP = LandingPads[i];
4156 const std::vector<int> &TypeIds = LP->TypeIds;
4157 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004158 unsigned SizeSiteActions = 0;
4159
Duncan Sandsb32edb42007-06-06 15:37:31 +00004160 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00004161 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00004162 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004163
Duncan Sandsb32edb42007-06-06 15:37:31 +00004164 if (NumShared) {
4165 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
4166 assert(Actions.size());
4167 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004168 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
4169 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004170 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004171 SizeAction -=
4172 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004173 SizeAction += -PrevAction->NextAction;
4174 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004175 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00004176 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00004177
Duncan Sandsb32edb42007-06-06 15:37:31 +00004178 // Compute the actions.
4179 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
4180 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004181 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4182 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004183 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004184
4185 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004186 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004187 SizeSiteActions += SizeAction;
4188
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004189 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00004190 Actions.push_back(Action);
4191
4192 PrevAction = &Actions.back();
4193 }
4194
4195 // Record the first action of the landing pad site.
4196 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4197 } // else identical - re-use previous FirstAction
4198
4199 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004200
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00004201 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00004202 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00004203 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004204
Duncan Sands481dc722007-12-19 07:36:31 +00004205 // Compute the call-site table. The entry for an invoke has a try-range
4206 // containing the call, a non-zero landing pad and an appropriate action.
4207 // The entry for an ordinary call has a try-range containing the call and
4208 // zero for the landing pad and the action. Calls marked 'nounwind' have
4209 // no entry and must not be contained in the try-range of any entry - they
4210 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004211 SmallVector<CallSiteEntry, 64> CallSites;
4212
4213 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00004214 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4215 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4216 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00004217 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4218 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00004219 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004220 unsigned BeginLabel = LandingPad->BeginLabels[j];
4221 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4222 PadRange P = { i, j };
4223 PadMap[BeginLabel] = P;
4224 }
4225 }
4226
Duncan Sands481dc722007-12-19 07:36:31 +00004227 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00004228 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00004229
4230 // Whether there is a potentially throwing instruction (currently this means
4231 // an ordinary call) between the end of the previous try-range and now.
4232 bool SawPotentiallyThrowing = false;
4233
4234 // Whether the last callsite entry was for an invoke.
4235 bool PreviousIsInvoke = false;
4236
Duncan Sands481dc722007-12-19 07:36:31 +00004237 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004238 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4239 I != E; ++I) {
4240 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4241 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00004242 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00004243 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00004244 continue;
4245 }
4246
Chris Lattner9e330492007-12-30 20:50:28 +00004247 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00004248 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00004249
Duncan Sands481dc722007-12-19 07:36:31 +00004250 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00004251 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00004252 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004253
Duncan Sands481dc722007-12-19 07:36:31 +00004254 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00004255 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00004256 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00004257 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00004258 continue;
4259
4260 PadRange P = L->second;
4261 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4262
4263 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4264 "Inconsistent landing pad map!");
4265
4266 // If some instruction between the previous try-range and this one may
4267 // throw, create a call-site entry with no landing pad for the region
4268 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00004269 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004270 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4271 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00004272 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004273 }
4274
4275 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00004276 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00004277
Duncan Sands481dc722007-12-19 07:36:31 +00004278 if (LandingPad->LandingPadLabel) {
4279 // This try-range is for an invoke.
4280 CallSiteEntry Site = {BeginLabel, LastLabel,
4281 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00004282
Duncan Sands481dc722007-12-19 07:36:31 +00004283 // Try to merge with the previous call-site.
4284 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00004285 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00004286 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4287 // Extend the range of the previous entry.
4288 Prev.EndLabel = Site.EndLabel;
4289 continue;
4290 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004291 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004292
Duncan Sands481dc722007-12-19 07:36:31 +00004293 // Otherwise, create a new call-site.
4294 CallSites.push_back(Site);
4295 PreviousIsInvoke = true;
4296 } else {
4297 // Create a gap.
4298 PreviousIsInvoke = false;
4299 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004300 }
4301 }
4302 // If some instruction between the previous try-range and the end of the
4303 // function may throw, create a call-site entry with no landing pad for the
4304 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00004305 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004306 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4307 CallSites.push_back(Site);
4308 }
4309
Jim Laskeybacd3042007-02-21 22:48:45 +00004310 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00004311
4312 // Call sites.
4313 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4314 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4315 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4316 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4317 SiteLengthSize +
4318 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00004319 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004320 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00004321
Duncan Sands671fa972008-05-07 19:11:09 +00004322 // Type infos.
4323 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4324 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004325
4326 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004327 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004328 SizeSites + SizeActions + SizeTypes;
4329
4330 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4331 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004332 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004333 TypeOffset;
4334
4335 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004336
Jim Laskeybacd3042007-02-21 22:48:45 +00004337 // Begin the exception table.
4338 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00004339 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00004340 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004341 for (unsigned i = 0; i != SizeAlign; ++i) {
4342 Asm->EmitInt8(0);
4343 Asm->EOL("Padding");
4344 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004345 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004346
Jim Laskeybacd3042007-02-21 22:48:45 +00004347 // Emit the header.
4348 Asm->EmitInt8(DW_EH_PE_omit);
4349 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4350 Asm->EmitInt8(DW_EH_PE_absptr);
4351 Asm->EOL("TType format (DW_EH_PE_absptr)");
4352 Asm->EmitULEB128Bytes(TypeOffset);
4353 Asm->EOL("TType base offset");
4354 Asm->EmitInt8(DW_EH_PE_udata4);
4355 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4356 Asm->EmitULEB128Bytes(SizeSites);
4357 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004358
Duncan Sands57810cd2007-09-05 11:27:52 +00004359 // Emit the landing pad site information.
4360 for (unsigned i = 0; i < CallSites.size(); ++i) {
4361 CallSiteEntry &S = CallSites[i];
4362 const char *BeginTag;
4363 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00004364
Duncan Sands57810cd2007-09-05 11:27:52 +00004365 if (!S.BeginLabel) {
4366 BeginTag = "eh_func_begin";
4367 BeginNumber = SubprogramCount;
4368 } else {
4369 BeginTag = "label";
4370 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004371 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004372
Duncan Sands57810cd2007-09-05 11:27:52 +00004373 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004374 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004375 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004376
Duncan Sands57810cd2007-09-05 11:27:52 +00004377 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004378 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004379 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004380 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004381 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004382 }
4383 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004384
Duncan Sands671fa972008-05-07 19:11:09 +00004385 if (!S.PadLabel)
4386 Asm->EmitInt32(0);
4387 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004388 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004389 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004390 Asm->EOL("Landing pad");
4391
4392 Asm->EmitULEB128Bytes(S.Action);
4393 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004394 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004395
Jim Laskeybacd3042007-02-21 22:48:45 +00004396 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004397 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4398 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004399
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004400 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004401 Asm->EOL("TypeInfo index");
4402 Asm->EmitSLEB128Bytes(Action.NextAction);
4403 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004404 }
4405
4406 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004407 for (unsigned M = TypeInfos.size(); M; --M) {
4408 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004409
4410 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004411
4412 if (GV)
4413 O << Asm->getGlobalLinkName(GV);
4414 else
4415 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00004416
Jim Laskeybacd3042007-02-21 22:48:45 +00004417 Asm->EOL("TypeInfo");
4418 }
4419
Duncan Sands73ef58a2007-06-02 16:53:42 +00004420 // Emit the filter typeids.
4421 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4422 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004423 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004424 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004425 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004426
Evan Cheng05548eb2008-02-29 19:36:59 +00004427 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004428 }
4429
Jim Laskey072200c2007-01-29 18:51:14 +00004430public:
4431 //===--------------------------------------------------------------------===//
4432 // Main entry points.
4433 //
Owen Andersoncb371882008-08-21 00:14:44 +00004434 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00004435 : Dwarf(OS, A, T, "eh")
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004436 , shouldEmitTable(false)
4437 , shouldEmitMoves(false)
4438 , shouldEmitTableModule(false)
4439 , shouldEmitMovesModule(false)
Jim Laskey072200c2007-01-29 18:51:14 +00004440 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004441
Jim Laskey072200c2007-01-29 18:51:14 +00004442 virtual ~DwarfException() {}
4443
4444 /// SetModuleInfo - Set machine module information when it's known that pass
4445 /// manager has created it. Set by the target AsmPrinter.
4446 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004447 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004448 }
4449
4450 /// BeginModule - Emit all exception information that should come prior to the
4451 /// content.
4452 void BeginModule(Module *M) {
4453 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004454 }
4455
4456 /// EndModule - Emit all exception information that should come after the
4457 /// content.
4458 void EndModule() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004459 if (shouldEmitMovesModule || shouldEmitTableModule) {
4460 const std::vector<Function *> Personalities = MMI->getPersonalities();
4461 for (unsigned i =0; i < Personalities.size(); ++i)
4462 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004463
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004464 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4465 E = EHFrames.end(); I != E; ++I)
4466 EmitEHFrame(*I);
4467 }
Jim Laskey072200c2007-01-29 18:51:14 +00004468 }
4469
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004470 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004471 /// emitted immediately after the function entry point.
4472 void BeginFunction(MachineFunction *MF) {
4473 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004474 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesene0040622008-04-02 17:04:45 +00004475 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004476
4477 // Map all labels and get rid of any dead landing pads.
4478 MMI->TidyLandingPads();
4479 // If any landing pads survive, we need an EH table.
4480 if (MMI->getLandingPads().size())
4481 shouldEmitTable = true;
4482
4483 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004484 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004485 shouldEmitMoves = true;
4486
4487 if (shouldEmitMoves || shouldEmitTable)
4488 // Assumes in correct section after the entry point.
4489 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004490 }
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004491 shouldEmitTableModule |= shouldEmitTable;
4492 shouldEmitMovesModule |= shouldEmitMoves;
Jim Laskey072200c2007-01-29 18:51:14 +00004493 }
4494
4495 /// EndFunction - Gather and emit post-function exception information.
4496 ///
4497 void EndFunction() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004498 if (shouldEmitMoves || shouldEmitTable) {
4499 EmitLabel("eh_func_end", SubprogramCount);
4500 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004501
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004502 // Save EH frame information
4503 EHFrames.
4504 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling6e198962007-09-18 01:47:22 +00004505 SubprogramCount,
4506 MMI->getPersonalityIndex(),
4507 MF->getFrameInfo()->hasCalls(),
4508 !MMI->getLandingPads().empty(),
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00004509 MMI->getFrameMoves(),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004510 MF->getFunction()));
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004511 }
Jim Laskey072200c2007-01-29 18:51:14 +00004512 }
4513};
4514
Jim Laskey0d086af2006-02-27 12:43:29 +00004515} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004516
4517//===----------------------------------------------------------------------===//
4518
Jim Laskeyd18e2892006-01-20 20:34:06 +00004519/// Emit - Print the abbreviation using the specified Dwarf writer.
4520///
Jim Laskey072200c2007-01-29 18:51:14 +00004521void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004522 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004523 DD.getAsm()->EmitULEB128Bytes(Tag);
4524 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004525
Jim Laskeyd18e2892006-01-20 20:34:06 +00004526 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004527 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4528 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004529
Jim Laskeyd18e2892006-01-20 20:34:06 +00004530 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004531 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004532 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004533
Jim Laskeyd18e2892006-01-20 20:34:06 +00004534 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004535 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4536 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004537
Jim Laskeyd18e2892006-01-20 20:34:06 +00004538 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004539 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4540 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004541 }
4542
4543 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004544 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4545 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004546}
4547
4548#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004549void DIEAbbrev::print(std::ostream &O) {
4550 O << "Abbreviation @"
4551 << std::hex << (intptr_t)this << std::dec
4552 << " "
4553 << TagString(Tag)
4554 << " "
4555 << ChildrenString(ChildrenFlag)
4556 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004557
Jim Laskeya0f3d172006-09-07 22:06:40 +00004558 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4559 O << " "
4560 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004561 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004562 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004563 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004564 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004565}
Bill Wendlinge8156192006-12-07 01:30:32 +00004566void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004567#endif
4568
4569//===----------------------------------------------------------------------===//
4570
Jim Laskeyef42a012006-11-02 20:12:39 +00004571#ifndef NDEBUG
4572void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004573 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004574}
Jim Laskeyef42a012006-11-02 20:12:39 +00004575#endif
4576
4577//===----------------------------------------------------------------------===//
4578
Jim Laskey063e7652006-01-17 17:31:53 +00004579/// EmitValue - Emit integer of appropriate size.
4580///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004581void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004582 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004583 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004584 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004585 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004586 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004587 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004588 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004589 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004590 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004591 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4592 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4593 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004594 default: assert(0 && "DIE Value form not supported yet"); break;
4595 }
4596}
4597
4598/// SizeOf - Determine size of integer value in bytes.
4599///
Jim Laskey072200c2007-01-29 18:51:14 +00004600unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004601 switch (Form) {
4602 case DW_FORM_flag: // Fall thru
4603 case DW_FORM_ref1: // Fall thru
4604 case DW_FORM_data1: return sizeof(int8_t);
4605 case DW_FORM_ref2: // Fall thru
4606 case DW_FORM_data2: return sizeof(int16_t);
4607 case DW_FORM_ref4: // Fall thru
4608 case DW_FORM_data4: return sizeof(int32_t);
4609 case DW_FORM_ref8: // Fall thru
4610 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004611 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4612 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004613 default: assert(0 && "DIE Value form not supported yet"); break;
4614 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004615 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004616}
4617
Jim Laskey063e7652006-01-17 17:31:53 +00004618//===----------------------------------------------------------------------===//
4619
4620/// EmitValue - Emit string value.
4621///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004622void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004623 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00004624}
4625
Jim Laskey063e7652006-01-17 17:31:53 +00004626//===----------------------------------------------------------------------===//
4627
4628/// EmitValue - Emit label value.
4629///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004630void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004631 bool IsSmall = Form == DW_FORM_data4;
4632 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004633}
4634
4635/// SizeOf - Determine size of label value in bytes.
4636///
Jim Laskey072200c2007-01-29 18:51:14 +00004637unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004638 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004639 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004640}
Jim Laskeyef42a012006-11-02 20:12:39 +00004641
Jim Laskey063e7652006-01-17 17:31:53 +00004642//===----------------------------------------------------------------------===//
4643
Jim Laskeyd18e2892006-01-20 20:34:06 +00004644/// EmitValue - Emit label value.
4645///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004646void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004647 bool IsSmall = Form == DW_FORM_data4;
4648 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004649}
4650
4651/// SizeOf - Determine size of label value in bytes.
4652///
Jim Laskey072200c2007-01-29 18:51:14 +00004653unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004654 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004655 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004656}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004657
Jim Laskeyd18e2892006-01-20 20:34:06 +00004658//===----------------------------------------------------------------------===//
4659
Jim Laskey063e7652006-01-17 17:31:53 +00004660/// EmitValue - Emit delta value.
4661///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004662void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4663 bool IsSmall = Form == DW_FORM_data4;
4664 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4665 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4666}
4667
4668/// SizeOf - Determine size of delta value in bytes.
4669///
4670unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4671 if (Form == DW_FORM_data4) return 4;
4672 return DD.getTargetData()->getPointerSize();
4673}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004674
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004675//===----------------------------------------------------------------------===//
4676
4677/// EmitValue - Emit delta value.
4678///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004679void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004680 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004681 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004682}
4683
4684/// SizeOf - Determine size of delta value in bytes.
4685///
Jim Laskey072200c2007-01-29 18:51:14 +00004686unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004687 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004688 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004689}
4690
4691//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004692
Jim Laskeyb8509c52006-03-23 18:07:55 +00004693/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004694///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004695void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004696 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004697}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004698
Jim Laskeyd18e2892006-01-20 20:34:06 +00004699//===----------------------------------------------------------------------===//
4700
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004701/// ComputeSize - calculate the size of the block.
4702///
Jim Laskey072200c2007-01-29 18:51:14 +00004703unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004704 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004705 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004706
Jim Laskeyef42a012006-11-02 20:12:39 +00004707 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004708 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004709 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004710 }
4711 return Size;
4712}
4713
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004714/// EmitValue - Emit block data.
4715///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004716void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004717 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004718 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4719 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4720 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4721 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004722 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004723 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004724
Owen Anderson873e1b52008-06-24 21:44:59 +00004725 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004726
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004727 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004728 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004729 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004730 }
4731}
4732
4733/// SizeOf - Determine size of block data in bytes.
4734///
Jim Laskey072200c2007-01-29 18:51:14 +00004735unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004736 switch (Form) {
4737 case DW_FORM_block1: return Size + sizeof(int8_t);
4738 case DW_FORM_block2: return Size + sizeof(int16_t);
4739 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004740 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004741 default: assert(0 && "Improper form for block"); break;
4742 }
4743 return 0;
4744}
4745
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004746//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004747/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004748
4749DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004750 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004751 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004752}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004753
Jim Laskeyb8509c52006-03-23 18:07:55 +00004754/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4755///
4756void DIE::AddSiblingOffset() {
4757 DIEInteger *DI = new DIEInteger(0);
4758 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004759 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004760}
4761
Jim Laskeyef42a012006-11-02 20:12:39 +00004762/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004763///
Jim Laskeyef42a012006-11-02 20:12:39 +00004764void DIE::Profile(FoldingSetNodeID &ID) {
4765 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004766
Jim Laskeyef42a012006-11-02 20:12:39 +00004767 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4768 ID.AddPointer(Children[i]);
4769
4770 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4771 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004772}
Jim Laskeyef42a012006-11-02 20:12:39 +00004773
4774#ifndef NDEBUG
4775void DIE::print(std::ostream &O, unsigned IncIndent) {
4776 static unsigned IndentCount = 0;
4777 IndentCount += IncIndent;
4778 const std::string Indent(IndentCount, ' ');
4779 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004780
Jim Laskeyef42a012006-11-02 20:12:39 +00004781 if (!isBlock) {
4782 O << Indent
4783 << "Die: "
4784 << "0x" << std::hex << (intptr_t)this << std::dec
4785 << ", Offset: " << Offset
4786 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004787 << "\n";
4788
Jim Laskeyef42a012006-11-02 20:12:39 +00004789 O << Indent
4790 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004791 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004792 << ChildrenString(Abbrev.getChildrenFlag());
4793 } else {
4794 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004795 }
4796 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004797
Owen Anderson873e1b52008-06-24 21:44:59 +00004798 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004799
Jim Laskeyef42a012006-11-02 20:12:39 +00004800 IndentCount += 2;
4801 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4802 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004803
4804 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004805 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004806 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004807 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004808
Jim Laskeyef42a012006-11-02 20:12:39 +00004809 O << " "
4810 << FormEncodingString(Data[i].getForm())
4811 << " ";
4812 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004813 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004814 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004815 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004816
Jim Laskeyef42a012006-11-02 20:12:39 +00004817 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4818 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004819 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004820
Jim Laskeyef42a012006-11-02 20:12:39 +00004821 if (!isBlock) O << "\n";
4822 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004823}
4824
Jim Laskeyef42a012006-11-02 20:12:39 +00004825void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004826 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004827}
Jim Laskeybd761842006-02-27 17:27:12 +00004828#endif
Jim Laskey65195462006-10-30 13:35:07 +00004829
4830//===----------------------------------------------------------------------===//
4831/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004832///
Jim Laskey65195462006-10-30 13:35:07 +00004833
Owen Andersoncb371882008-08-21 00:14:44 +00004834DwarfWriter::DwarfWriter(raw_ostream &OS, AsmPrinter *A,
Jim Laskey65195462006-10-30 13:35:07 +00004835 const TargetAsmInfo *T) {
Jim Laskey072200c2007-01-29 18:51:14 +00004836 DE = new DwarfException(OS, A, T);
4837 DD = new DwarfDebug(OS, A, T);
Jim Laskey65195462006-10-30 13:35:07 +00004838}
4839
4840DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004841 delete DE;
4842 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004843}
4844
Jim Laskey44c3b9f2007-01-26 21:22:28 +00004845/// SetModuleInfo - Set machine module info when it's known that pass manager
4846/// has created it. Set by the target AsmPrinter.
4847void DwarfWriter::SetModuleInfo(MachineModuleInfo *MMI) {
Jim Laskey072200c2007-01-29 18:51:14 +00004848 DD->SetModuleInfo(MMI);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004849 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004850}
4851
4852/// BeginModule - Emit all Dwarf sections that should come prior to the
4853/// content.
4854void DwarfWriter::BeginModule(Module *M) {
Jim Laskey072200c2007-01-29 18:51:14 +00004855 DE->BeginModule(M);
4856 DD->BeginModule(M);
Jim Laskey65195462006-10-30 13:35:07 +00004857}
4858
4859/// EndModule - Emit all Dwarf sections that should come after the content.
4860///
4861void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004862 DE->EndModule();
4863 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004864}
4865
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004866/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004867/// emitted immediately after the function entry point.
4868void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004869 DE->BeginFunction(MF);
4870 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004871}
4872
4873/// EndFunction - Gather and emit post-function debug information.
4874///
Bill Wendlingd751c642008-09-26 00:28:12 +00004875void DwarfWriter::EndFunction(MachineFunction *MF) {
4876 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004877 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004878
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004879 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004880 // Clear function debug information.
4881 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004882}