blob: 25420473ad6913f40c5a30811969c6282ca7898b [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
Devang Pateleb3fc282009-01-08 23:40:34 +000046static RegisterPass<DwarfWriter>
47X("dwarfwriter", "DWARF Information Writer");
48char DwarfWriter::ID = 0;
49
Jim Laskey0d086af2006-02-27 12:43:29 +000050namespace llvm {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +000051
Jim Laskey65195462006-10-30 13:35:07 +000052//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +000053
54/// Configuration values for initial hash set sizes (log2).
55///
56static const unsigned InitDiesSetSize = 9; // 512
57static const unsigned InitAbbreviationsSetSize = 9; // 512
58static const unsigned InitValuesSetSize = 9; // 512
59
60//===----------------------------------------------------------------------===//
61/// Forward declarations.
62///
63class DIE;
64class DIEValue;
65
66//===----------------------------------------------------------------------===//
Devang Pateld1ca9252009-01-05 23:03:32 +000067/// Utility routines.
68///
69/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
70/// specified value in their initializer somewhere.
71static void
72getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
73 // Scan though value users.
74 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
75 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
76 // If the user is a GlobalVariable then add to result.
77 Result.push_back(GV);
78 } else if (Constant *C = dyn_cast<Constant>(*I)) {
79 // If the user is a constant variable then scan its users
80 getGlobalVariablesUsing(C, Result);
81 }
82 }
83}
84
85/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
86/// named GlobalVariable.
87static void
88getGlobalVariablesUsing(Module &M, const std::string &RootName,
89 std::vector<GlobalVariable*> &Result) {
90 std::vector<const Type*> FieldTypes;
91 FieldTypes.push_back(Type::Int32Ty);
92 FieldTypes.push_back(Type::Int32Ty);
93
94 // Get the GlobalVariable root.
95 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
96 StructType::get(FieldTypes));
97
98 // If present and linkonce then scan for users.
99 if (UseRoot && UseRoot->hasLinkOnceLinkage())
100 getGlobalVariablesUsing(UseRoot, Result);
101}
102
103//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000104/// DWLabel - Labels are used to track locations in the assembler file.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000105/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
106/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer181b6c92007-08-05 20:06:04 +0000107/// unique in that category.
Jim Laskey65195462006-10-30 13:35:07 +0000108class DWLabel {
109public:
Jim Laskeyef42a012006-11-02 20:12:39 +0000110 /// Tag - Label category tag. Should always be a staticly declared C string.
111 ///
112 const char *Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000113
Jim Laskeyef42a012006-11-02 20:12:39 +0000114 /// Number - Value to make label unique.
115 ///
116 unsigned Number;
Jim Laskey65195462006-10-30 13:35:07 +0000117
118 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000119
Jim Laskeyef42a012006-11-02 20:12:39 +0000120 void Profile(FoldingSetNodeID &ID) const {
121 ID.AddString(std::string(Tag));
122 ID.AddInteger(Number);
Jim Laskey90c79d72006-03-23 23:02:34 +0000123 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000124
Jim Laskeyef42a012006-11-02 20:12:39 +0000125#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000126 void print(std::ostream *O) const {
127 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000128 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000129 void print(std::ostream &O) const {
Jim Laskeybacd3042007-02-21 22:48:45 +0000130 O << "." << Tag;
Jim Laskeyef42a012006-11-02 20:12:39 +0000131 if (Number) O << Number;
132 }
133#endif
Jim Laskeybd761842006-02-27 17:27:12 +0000134};
135
136//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000137/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
138/// Dwarf abbreviation.
Jim Laskey0d086af2006-02-27 12:43:29 +0000139class DIEAbbrevData {
140private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000141 /// Attribute - Dwarf attribute code.
142 ///
143 unsigned Attribute;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000144
Jim Laskeyef42a012006-11-02 20:12:39 +0000145 /// Form - Dwarf form code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000146 ///
147 unsigned Form;
148
Jim Laskey0d086af2006-02-27 12:43:29 +0000149public:
150 DIEAbbrevData(unsigned A, unsigned F)
151 : Attribute(A)
152 , Form(F)
153 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000154
Jim Laskeybd761842006-02-27 17:27:12 +0000155 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000156 unsigned getAttribute() const { return Attribute; }
157 unsigned getForm() const { return Form; }
Jim Laskey063e7652006-01-17 17:31:53 +0000158
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000159 /// Profile - Used to gather unique data for the abbreviation folding set.
Jim Laskey0d086af2006-02-27 12:43:29 +0000160 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000161 void Profile(FoldingSetNodeID &ID)const {
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000162 ID.AddInteger(Attribute);
163 ID.AddInteger(Form);
Jim Laskey0d086af2006-02-27 12:43:29 +0000164 }
165};
Jim Laskey063e7652006-01-17 17:31:53 +0000166
Jim Laskey0d086af2006-02-27 12:43:29 +0000167//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000168/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
169/// information object.
170class DIEAbbrev : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000171private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000172 /// Tag - Dwarf tag code.
173 ///
174 unsigned Tag;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000175
Jim Laskeyef42a012006-11-02 20:12:39 +0000176 /// Unique number for node.
177 ///
178 unsigned Number;
179
180 /// ChildrenFlag - Dwarf children flag.
181 ///
182 unsigned ChildrenFlag;
183
184 /// Data - Raw data bytes for abbreviation.
185 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000186 SmallVector<DIEAbbrevData, 8> Data;
Jim Laskey063e7652006-01-17 17:31:53 +0000187
Jim Laskey0d086af2006-02-27 12:43:29 +0000188public:
Jim Laskey063e7652006-01-17 17:31:53 +0000189
Jim Laskey0d086af2006-02-27 12:43:29 +0000190 DIEAbbrev(unsigned T, unsigned C)
Jim Laskeyef42a012006-11-02 20:12:39 +0000191 : Tag(T)
Jim Laskey0d086af2006-02-27 12:43:29 +0000192 , ChildrenFlag(C)
193 , Data()
194 {}
195 ~DIEAbbrev() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000196
Jim Laskeybd761842006-02-27 17:27:12 +0000197 // Accessors.
Jim Laskey0d086af2006-02-27 12:43:29 +0000198 unsigned getTag() const { return Tag; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000199 unsigned getNumber() const { return Number; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000200 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000201 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000202 void setTag(unsigned T) { Tag = T; }
Jim Laskey0d086af2006-02-27 12:43:29 +0000203 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000204 void setNumber(unsigned N) { Number = N; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000205
Jim Laskey0d086af2006-02-27 12:43:29 +0000206 /// AddAttribute - Adds another set of attribute information to the
207 /// abbreviation.
208 void AddAttribute(unsigned Attribute, unsigned Form) {
209 Data.push_back(DIEAbbrevData(Attribute, Form));
Jim Laskey063e7652006-01-17 17:31:53 +0000210 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000211
Jim Laskeyb8509c52006-03-23 18:07:55 +0000212 /// AddFirstAttribute - Adds a set of attribute information to the front
213 /// of the abbreviation.
214 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
215 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
216 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000217
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000218 /// Profile - Used to gather unique data for the abbreviation folding set.
219 ///
220 void Profile(FoldingSetNodeID &ID) {
221 ID.AddInteger(Tag);
222 ID.AddInteger(ChildrenFlag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000223
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000224 // For each attribute description.
225 for (unsigned i = 0, N = Data.size(); i < N; ++i)
226 Data[i].Profile(ID);
227 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000228
Jim Laskey0d086af2006-02-27 12:43:29 +0000229 /// Emit - Print the abbreviation using the specified Dwarf writer.
230 ///
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000231 void Emit(const DwarfDebug &DD) const;
232
Jim Laskey0d086af2006-02-27 12:43:29 +0000233#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000234 void print(std::ostream *O) {
235 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000236 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000237 void print(std::ostream &O);
238 void dump();
239#endif
240};
Jim Laskey063e7652006-01-17 17:31:53 +0000241
Jim Laskey0d086af2006-02-27 12:43:29 +0000242//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000243/// DIE - A structured debug information entry. Has an abbreviation which
244/// describes it's organization.
245class DIE : public FoldingSetNode {
246protected:
247 /// Abbrev - Buffer for constructing abbreviation.
248 ///
249 DIEAbbrev Abbrev;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000250
Jim Laskeyef42a012006-11-02 20:12:39 +0000251 /// Offset - Offset in debug info section.
252 ///
253 unsigned Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000254
Jim Laskeyef42a012006-11-02 20:12:39 +0000255 /// Size - Size of instance + children.
256 ///
257 unsigned Size;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000258
Jim Laskeyef42a012006-11-02 20:12:39 +0000259 /// Children DIEs.
260 ///
261 std::vector<DIE *> Children;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000262
Jim Laskeyef42a012006-11-02 20:12:39 +0000263 /// Attributes values.
264 ///
Owen Anderson873e1b52008-06-24 21:44:59 +0000265 SmallVector<DIEValue*, 32> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000266
Jim Laskeyef42a012006-11-02 20:12:39 +0000267public:
Dan Gohman81975f62007-08-27 14:50:10 +0000268 explicit DIE(unsigned Tag)
Jim Laskeyef42a012006-11-02 20:12:39 +0000269 : Abbrev(Tag, DW_CHILDREN_no)
270 , Offset(0)
271 , Size(0)
272 , Children()
273 , Values()
274 {}
275 virtual ~DIE();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000276
Jim Laskeyef42a012006-11-02 20:12:39 +0000277 // Accessors.
278 DIEAbbrev &getAbbrev() { return Abbrev; }
279 unsigned getAbbrevNumber() const {
280 return Abbrev.getNumber();
281 }
Jim Laskey85f419b2006-11-09 16:32:26 +0000282 unsigned getTag() const { return Abbrev.getTag(); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000283 unsigned getOffset() const { return Offset; }
284 unsigned getSize() const { return Size; }
285 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson873e1b52008-06-24 21:44:59 +0000286 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Jim Laskeyef42a012006-11-02 20:12:39 +0000287 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
288 void setOffset(unsigned O) { Offset = O; }
289 void setSize(unsigned S) { Size = S; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000290
Jim Laskeyef42a012006-11-02 20:12:39 +0000291 /// AddValue - Add a value and attributes to a DIE.
292 ///
293 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
294 Abbrev.AddAttribute(Attribute, Form);
295 Values.push_back(Value);
296 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000297
Jim Laskeyef42a012006-11-02 20:12:39 +0000298 /// SiblingOffset - Return the offset of the debug information entry's
299 /// sibling.
300 unsigned SiblingOffset() const { return Offset + Size; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000301
Jim Laskeyef42a012006-11-02 20:12:39 +0000302 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
303 ///
304 void AddSiblingOffset();
305
306 /// AddChild - Add a child to the DIE.
307 ///
308 void AddChild(DIE *Child) {
309 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
310 Children.push_back(Child);
311 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000312
Jim Laskeyef42a012006-11-02 20:12:39 +0000313 /// Detach - Detaches objects connected to it after copying.
314 ///
315 void Detach() {
316 Children.clear();
317 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000318
Jim Laskeyef42a012006-11-02 20:12:39 +0000319 /// Profile - Used to gather unique data for the value folding set.
320 ///
321 void Profile(FoldingSetNodeID &ID) ;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000322
Jim Laskeyef42a012006-11-02 20:12:39 +0000323#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000324 void print(std::ostream *O, unsigned IncIndent = 0) {
325 if (O) print(*O, IncIndent);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000326 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000327 void print(std::ostream &O, unsigned IncIndent = 0);
328 void dump();
329#endif
330};
331
332//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000333/// DIEValue - A debug information entry value.
Jim Laskeyef42a012006-11-02 20:12:39 +0000334///
335class DIEValue : public FoldingSetNode {
Jim Laskey0d086af2006-02-27 12:43:29 +0000336public:
337 enum {
338 isInteger,
339 isString,
340 isLabel,
341 isAsIsLabel,
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000342 isSectionOffset,
Jim Laskey0d086af2006-02-27 12:43:29 +0000343 isDelta,
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000344 isEntry,
345 isBlock
Jim Laskey0d086af2006-02-27 12:43:29 +0000346 };
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000347
Jim Laskeyef42a012006-11-02 20:12:39 +0000348 /// Type - Type of data stored in the value.
349 ///
350 unsigned Type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000351
Dan Gohman81975f62007-08-27 14:50:10 +0000352 explicit DIEValue(unsigned T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000353 : Type(T)
Jim Laskeyef42a012006-11-02 20:12:39 +0000354 {}
Jim Laskey0d086af2006-02-27 12:43:29 +0000355 virtual ~DIEValue() {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000356
Jim Laskeyf6733882006-11-02 21:48:18 +0000357 // Accessors
Jim Laskeyef42a012006-11-02 20:12:39 +0000358 unsigned getType() const { return Type; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000359
Jim Laskey0d086af2006-02-27 12:43:29 +0000360 // Implement isa/cast/dyncast.
361 static bool classof(const DIEValue *) { return true; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000362
Jim Laskey0d086af2006-02-27 12:43:29 +0000363 /// EmitValue - Emit value via the Dwarf writer.
364 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000365 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000366
Jim Laskey0d086af2006-02-27 12:43:29 +0000367 /// SizeOf - Return the size of a value in bytes.
368 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000369 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000370
Jim Laskeyef42a012006-11-02 20:12:39 +0000371 /// Profile - Used to gather unique data for the value folding set.
372 ///
373 virtual void Profile(FoldingSetNodeID &ID) = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000374
Jim Laskeyef42a012006-11-02 20:12:39 +0000375#ifndef NDEBUG
Bill Wendling5c7e3262006-12-17 05:15:13 +0000376 void print(std::ostream *O) {
377 if (O) print(*O);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000378 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000379 virtual void print(std::ostream &O) = 0;
380 void dump();
381#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000382};
Jim Laskey063e7652006-01-17 17:31:53 +0000383
Jim Laskey0d086af2006-02-27 12:43:29 +0000384//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000385/// DWInteger - An integer value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000386///
Jim Laskey0d086af2006-02-27 12:43:29 +0000387class DIEInteger : public DIEValue {
388private:
389 uint64_t Integer;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000390
Jim Laskey0d086af2006-02-27 12:43:29 +0000391public:
Dan Gohman81975f62007-08-27 14:50:10 +0000392 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000393
Jim Laskey0d086af2006-02-27 12:43:29 +0000394 // Implement isa/cast/dyncast.
395 static bool classof(const DIEInteger *) { return true; }
396 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000397
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000398 /// BestForm - Choose the best form for integer.
399 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000400 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
401 if (IsSigned) {
402 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
403 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
404 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
405 } else {
406 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
407 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
408 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
409 }
410 return DW_FORM_data8;
411 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000412
Jim Laskey0d086af2006-02-27 12:43:29 +0000413 /// EmitValue - Emit integer of appropriate size.
414 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000415 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000416
Jim Laskey0d086af2006-02-27 12:43:29 +0000417 /// SizeOf - Determine size of integer value in bytes.
418 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000419 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000420
Jim Laskeyef42a012006-11-02 20:12:39 +0000421 /// Profile - Used to gather unique data for the value folding set.
422 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000423 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000424 ID.AddInteger(isInteger);
Jim Laskeyef42a012006-11-02 20:12:39 +0000425 ID.AddInteger(Integer);
426 }
Jim Laskey5496f012006-11-09 14:52:14 +0000427 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000428
Jim Laskeyef42a012006-11-02 20:12:39 +0000429#ifndef NDEBUG
430 virtual void print(std::ostream &O) {
431 O << "Int: " << (int64_t)Integer
432 << " 0x" << std::hex << Integer << std::dec;
433 }
434#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000435};
Jim Laskey063e7652006-01-17 17:31:53 +0000436
Jim Laskey0d086af2006-02-27 12:43:29 +0000437//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000438/// DIEString - A string value DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000439///
Jim Laskeyef42a012006-11-02 20:12:39 +0000440class DIEString : public DIEValue {
441public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000442 const std::string String;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000443
Dan Gohman81975f62007-08-27 14:50:10 +0000444 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000445
Jim Laskey0d086af2006-02-27 12:43:29 +0000446 // Implement isa/cast/dyncast.
447 static bool classof(const DIEString *) { return true; }
448 static bool classof(const DIEValue *S) { return S->Type == isString; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000449
Jim Laskey0d086af2006-02-27 12:43:29 +0000450 /// EmitValue - Emit string value.
451 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000452 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000453
Jim Laskey0d086af2006-02-27 12:43:29 +0000454 /// SizeOf - Determine size of string value in bytes.
455 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000456 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000457 return String.size() + sizeof(char); // sizeof('\0');
458 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000459
Jim Laskeyef42a012006-11-02 20:12:39 +0000460 /// Profile - Used to gather unique data for the value folding set.
461 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000462 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000463 ID.AddInteger(isString);
Jim Laskeyef42a012006-11-02 20:12:39 +0000464 ID.AddString(String);
465 }
Jim Laskey5496f012006-11-09 14:52:14 +0000466 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000467
Jim Laskeyef42a012006-11-02 20:12:39 +0000468#ifndef NDEBUG
469 virtual void print(std::ostream &O) {
470 O << "Str: \"" << String << "\"";
471 }
472#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000473};
Jim Laskey063e7652006-01-17 17:31:53 +0000474
Jim Laskey0d086af2006-02-27 12:43:29 +0000475//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000476/// DIEDwarfLabel - A Dwarf internal label expression DIE.
Jim Laskey0d086af2006-02-27 12:43:29 +0000477//
Jim Laskeyef42a012006-11-02 20:12:39 +0000478class DIEDwarfLabel : public DIEValue {
479public:
480
Jim Laskey0d086af2006-02-27 12:43:29 +0000481 const DWLabel Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000482
Dan Gohman81975f62007-08-27 14:50:10 +0000483 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000484
Jim Laskey0d086af2006-02-27 12:43:29 +0000485 // Implement isa/cast/dyncast.
486 static bool classof(const DIEDwarfLabel *) { return true; }
487 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000488
Jim Laskey0d086af2006-02-27 12:43:29 +0000489 /// EmitValue - Emit label value.
490 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000491 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000492
Jim Laskey0d086af2006-02-27 12:43:29 +0000493 /// SizeOf - Determine size of label value in bytes.
494 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000495 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000496
Jim Laskeyef42a012006-11-02 20:12:39 +0000497 /// Profile - Used to gather unique data for the value folding set.
498 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000499 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000500 ID.AddInteger(isLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000501 Label.Profile(ID);
502 }
Jim Laskey5496f012006-11-09 14:52:14 +0000503 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000504
Jim Laskeyef42a012006-11-02 20:12:39 +0000505#ifndef NDEBUG
506 virtual void print(std::ostream &O) {
507 O << "Lbl: ";
508 Label.print(O);
509 }
510#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000511};
Jim Laskey063e7652006-01-17 17:31:53 +0000512
Jim Laskey063e7652006-01-17 17:31:53 +0000513
Jim Laskey0d086af2006-02-27 12:43:29 +0000514//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000515/// DIEObjectLabel - A label to an object in code or data.
Jim Laskey0d086af2006-02-27 12:43:29 +0000516//
Jim Laskeyef42a012006-11-02 20:12:39 +0000517class DIEObjectLabel : public DIEValue {
518public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000519 const std::string Label;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000520
Dan Gohman81975f62007-08-27 14:50:10 +0000521 explicit DIEObjectLabel(const std::string &L)
522 : DIEValue(isAsIsLabel), Label(L) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000523
Jim Laskey0d086af2006-02-27 12:43:29 +0000524 // Implement isa/cast/dyncast.
525 static bool classof(const DIEObjectLabel *) { return true; }
526 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000527
Jim Laskey0d086af2006-02-27 12:43:29 +0000528 /// EmitValue - Emit label value.
529 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000530 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000531
Jim Laskey0d086af2006-02-27 12:43:29 +0000532 /// SizeOf - Determine size of label value in bytes.
533 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000534 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000535
Jim Laskeyef42a012006-11-02 20:12:39 +0000536 /// Profile - Used to gather unique data for the value folding set.
537 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000538 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000539 ID.AddInteger(isAsIsLabel);
Jim Laskeyef42a012006-11-02 20:12:39 +0000540 ID.AddString(Label);
541 }
Jim Laskey5496f012006-11-09 14:52:14 +0000542 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000543
544#ifndef NDEBUG
545 virtual void print(std::ostream &O) {
546 O << "Obj: " << Label;
547 }
548#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000549};
Jim Laskey063e7652006-01-17 17:31:53 +0000550
Jim Laskey0d086af2006-02-27 12:43:29 +0000551//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000552/// DIESectionOffset - A section offset DIE.
553//
554class DIESectionOffset : public DIEValue {
555public:
556 const DWLabel Label;
557 const DWLabel Section;
558 bool IsEH : 1;
559 bool UseSet : 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000560
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000561 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
562 bool isEH = false, bool useSet = true)
563 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
564 IsEH(isEH), UseSet(useSet) {}
565
566 // Implement isa/cast/dyncast.
567 static bool classof(const DIESectionOffset *) { return true; }
568 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000569
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000570 /// EmitValue - Emit section offset.
571 ///
572 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000573
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000574 /// SizeOf - Determine size of section offset value in bytes.
575 ///
576 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000577
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +0000578 /// Profile - Used to gather unique data for the value folding set.
579 ///
580 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
581 const DWLabel &Section) {
582 ID.AddInteger(isSectionOffset);
583 Label.Profile(ID);
584 Section.Profile(ID);
585 // IsEH and UseSet are specific to the Label/Section that we will emit
586 // the offset for; so Label/Section are enough for uniqueness.
587 }
588 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
589
590#ifndef NDEBUG
591 virtual void print(std::ostream &O) {
592 O << "Off: ";
593 Label.print(O);
594 O << "-";
595 Section.print(O);
596 O << "-" << IsEH << "-" << UseSet;
597 }
598#endif
599};
600
601//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000602/// DIEDelta - A simple label difference DIE.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000603///
Jim Laskeyef42a012006-11-02 20:12:39 +0000604class DIEDelta : public DIEValue {
605public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000606 const DWLabel LabelHi;
607 const DWLabel LabelLo;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000608
Jim Laskey0d086af2006-02-27 12:43:29 +0000609 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
610 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
Jim Laskey063e7652006-01-17 17:31:53 +0000611
Jim Laskey0d086af2006-02-27 12:43:29 +0000612 // Implement isa/cast/dyncast.
613 static bool classof(const DIEDelta *) { return true; }
614 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000615
Jim Laskey0d086af2006-02-27 12:43:29 +0000616 /// EmitValue - Emit delta value.
617 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000618 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000619
Jim Laskey0d086af2006-02-27 12:43:29 +0000620 /// SizeOf - Determine size of delta value in bytes.
621 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000622 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000623
Jim Laskeyef42a012006-11-02 20:12:39 +0000624 /// Profile - Used to gather unique data for the value folding set.
625 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000626 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
627 const DWLabel &LabelLo) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000628 ID.AddInteger(isDelta);
Jim Laskeyef42a012006-11-02 20:12:39 +0000629 LabelHi.Profile(ID);
630 LabelLo.Profile(ID);
631 }
Jim Laskey5496f012006-11-09 14:52:14 +0000632 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
Jim Laskeyef42a012006-11-02 20:12:39 +0000633
634#ifndef NDEBUG
635 virtual void print(std::ostream &O) {
636 O << "Del: ";
637 LabelHi.print(O);
638 O << "-";
639 LabelLo.print(O);
640 }
641#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000642};
Jim Laskey063e7652006-01-17 17:31:53 +0000643
Jim Laskey0d086af2006-02-27 12:43:29 +0000644//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000645/// DIEntry - A pointer to another debug information entry. An instance of this
646/// class can also be used as a proxy for a debug information entry not yet
647/// defined (ie. types.)
648class DIEntry : public DIEValue {
649public:
Jim Laskey0d086af2006-02-27 12:43:29 +0000650 DIE *Entry;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000651
Dan Gohman81975f62007-08-27 14:50:10 +0000652 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000653
Jim Laskey0d086af2006-02-27 12:43:29 +0000654 // Implement isa/cast/dyncast.
655 static bool classof(const DIEntry *) { return true; }
656 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000657
Jim Laskeyb8509c52006-03-23 18:07:55 +0000658 /// EmitValue - Emit debug information entry offset.
Jim Laskey0d086af2006-02-27 12:43:29 +0000659 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000660 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000661
Jim Laskeyb8509c52006-03-23 18:07:55 +0000662 /// SizeOf - Determine size of debug information entry in bytes.
Jim Laskey0d086af2006-02-27 12:43:29 +0000663 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000664 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyef42a012006-11-02 20:12:39 +0000665 return sizeof(int32_t);
666 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000667
Jim Laskeyef42a012006-11-02 20:12:39 +0000668 /// Profile - Used to gather unique data for the value folding set.
669 ///
Jim Laskey5496f012006-11-09 14:52:14 +0000670 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
671 ID.AddInteger(isEntry);
672 ID.AddPointer(Entry);
673 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000674 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000675 ID.AddInteger(isEntry);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000676
Jim Laskeyef42a012006-11-02 20:12:39 +0000677 if (Entry) {
678 ID.AddPointer(Entry);
679 } else {
680 ID.AddPointer(this);
681 }
682 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000683
Jim Laskeyef42a012006-11-02 20:12:39 +0000684#ifndef NDEBUG
685 virtual void print(std::ostream &O) {
686 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
687 }
688#endif
Jim Laskey0d086af2006-02-27 12:43:29 +0000689};
690
691//===----------------------------------------------------------------------===//
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000692/// DIEBlock - A block of values. Primarily used for location expressions.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000693//
Jim Laskeyef42a012006-11-02 20:12:39 +0000694class DIEBlock : public DIEValue, public DIE {
695public:
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000696 unsigned Size; // Size in bytes excluding size header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000697
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000698 DIEBlock()
699 : DIEValue(isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +0000700 , DIE(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000701 , Size(0)
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000702 {}
Jim Laskeyef42a012006-11-02 20:12:39 +0000703 ~DIEBlock() {
704 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000705
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000706 // Implement isa/cast/dyncast.
707 static bool classof(const DIEBlock *) { return true; }
708 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000709
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000710 /// ComputeSize - calculate the size of the block.
711 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000712 unsigned ComputeSize(DwarfDebug &DD);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000713
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000714 /// BestForm - Choose the best form for data.
715 ///
Jim Laskeyef42a012006-11-02 20:12:39 +0000716 unsigned BestForm() const {
717 if ((unsigned char)Size == Size) return DW_FORM_block1;
718 if ((unsigned short)Size == Size) return DW_FORM_block2;
719 if ((unsigned int)Size == Size) return DW_FORM_block4;
720 return DW_FORM_block;
721 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000722
723 /// EmitValue - Emit block data.
724 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000725 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000726
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000727 /// SizeOf - Determine size of block data in bytes.
728 ///
Jim Laskey072200c2007-01-29 18:51:14 +0000729 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000730
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000731
Jim Laskeyef42a012006-11-02 20:12:39 +0000732 /// Profile - Used to gather unique data for the value folding set.
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000733 ///
Reid Spencer97821312006-11-02 23:56:21 +0000734 virtual void Profile(FoldingSetNodeID &ID) {
Jim Laskeyf6733882006-11-02 21:48:18 +0000735 ID.AddInteger(isBlock);
Jim Laskeyef42a012006-11-02 20:12:39 +0000736 DIE::Profile(ID);
737 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000738
Jim Laskeyef42a012006-11-02 20:12:39 +0000739#ifndef NDEBUG
740 virtual void print(std::ostream &O) {
741 O << "Blk: ";
742 DIE::print(O, 5);
743 }
744#endif
Jim Laskeyb80af6f2006-03-03 21:00:14 +0000745};
746
747//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +0000748/// CompileUnit - This dwarf writer support class manages information associate
749/// with a source file.
750class CompileUnit {
Jim Laskey0d086af2006-02-27 12:43:29 +0000751private:
Jim Laskeyef42a012006-11-02 20:12:39 +0000752 /// Desc - Compile unit debug descriptor.
753 ///
754 CompileUnitDesc *Desc;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000755
Jim Laskeyef42a012006-11-02 20:12:39 +0000756 /// ID - File identifier for source.
757 ///
758 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000759
Jim Laskeyef42a012006-11-02 20:12:39 +0000760 /// Die - Compile unit debug information entry.
761 ///
762 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000763
Jim Laskeyef42a012006-11-02 20:12:39 +0000764 /// DescToDieMap - Tracks the mapping of unit level debug informaton
765 /// descriptors to debug information entries.
766 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
Devang Patel48d190f2009-01-05 21:47:57 +0000767 DenseMap<GlobalVariable *, DIE *> GVToDieMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000768
769 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
770 /// descriptors to debug information entries using a DIEntry proxy.
771 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
Devang Patel48d190f2009-01-05 21:47:57 +0000772 DenseMap<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000773
774 /// Globals - A map of globally visible named entities for this unit.
775 ///
776 std::map<std::string, DIE *> Globals;
777
778 /// DiesSet - Used to uniquely define dies within the compile unit.
779 ///
780 FoldingSet<DIE> DiesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000781
Jim Laskeyef42a012006-11-02 20:12:39 +0000782 /// Dies - List of all dies in the compile unit.
783 ///
784 std::vector<DIE *> Dies;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000785
Jim Laskey0d086af2006-02-27 12:43:29 +0000786public:
Devang Pateld1ca9252009-01-05 23:03:32 +0000787 CompileUnit(unsigned I, DIE *D)
788 : ID(I), Die(D), DescToDieMap(), GVToDieMap(), DescToDIEntryMap(),
789 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize), Dies()
790 {}
791
Jim Laskeyef42a012006-11-02 20:12:39 +0000792 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
793 : Desc(CUD)
794 , ID(I)
795 , Die(D)
796 , DescToDieMap()
Devang Patel48d190f2009-01-05 21:47:57 +0000797 , GVToDieMap()
Jim Laskeyef42a012006-11-02 20:12:39 +0000798 , DescToDIEntryMap()
Devang Patel48d190f2009-01-05 21:47:57 +0000799 , GVToDIEntryMap()
Jim Laskeyef42a012006-11-02 20:12:39 +0000800 , Globals()
801 , DiesSet(InitDiesSetSize)
802 , Dies()
803 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000804
Jim Laskeyef42a012006-11-02 20:12:39 +0000805 ~CompileUnit() {
806 delete Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000807
Jim Laskeyef42a012006-11-02 20:12:39 +0000808 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
809 delete Dies[i];
810 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000811
Jim Laskeybd761842006-02-27 17:27:12 +0000812 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000813 CompileUnitDesc *getDesc() const { return Desc; }
814 unsigned getID() const { return ID; }
815 DIE* getDie() const { return Die; }
816 std::map<std::string, DIE *> &getGlobals() { return Globals; }
817
818 /// hasContent - Return true if this compile unit has something to write out.
819 ///
820 bool hasContent() const {
821 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000822 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000823
824 /// AddGlobal - Add a new global entity to the compile unit.
825 ///
826 void AddGlobal(const std::string &Name, DIE *Die) {
827 Globals[Name] = Die;
828 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000829
Jim Laskeyef42a012006-11-02 20:12:39 +0000830 /// getDieMapSlotFor - Returns the debug information entry map slot for the
831 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000832 DIE *&getDieMapSlotFor(DebugInfoDesc *DID) {
833 return DescToDieMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000834 }
Devang Patel48d190f2009-01-05 21:47:57 +0000835 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
836 return GVToDieMap[GV];
837 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000838
Jim Laskeyef42a012006-11-02 20:12:39 +0000839 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
840 /// specified debug descriptor.
Jim Laskey072200c2007-01-29 18:51:14 +0000841 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DID) {
842 return DescToDIEntryMap[DID];
Jim Laskeyef42a012006-11-02 20:12:39 +0000843 }
Devang Patel48d190f2009-01-05 21:47:57 +0000844 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
845 return GVToDIEntryMap[GV];
846 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000847
Jim Laskeyef42a012006-11-02 20:12:39 +0000848 /// AddDie - Adds or interns the DIE to the compile unit.
849 ///
850 DIE *AddDie(DIE &Buffer) {
851 FoldingSetNodeID ID;
852 Buffer.Profile(ID);
853 void *Where;
854 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000855
Jim Laskeyef42a012006-11-02 20:12:39 +0000856 if (!Die) {
857 Die = new DIE(Buffer);
858 DiesSet.InsertNode(Die, Where);
859 this->Die->AddChild(Die);
860 Buffer.Detach();
861 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000862
Jim Laskeyef42a012006-11-02 20:12:39 +0000863 return Die;
864 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000865};
866
Jim Laskey65195462006-10-30 13:35:07 +0000867//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000868/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000869///
Jim Laskey65195462006-10-30 13:35:07 +0000870class Dwarf {
871
Jim Laskey072200c2007-01-29 18:51:14 +0000872protected:
Jim Laskey65195462006-10-30 13:35:07 +0000873
874 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000875 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000876 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000877
Jim Laskey65195462006-10-30 13:35:07 +0000878 //
879 /// O - Stream to .s file.
880 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000881 raw_ostream &O;
Jim Laskey65195462006-10-30 13:35:07 +0000882
883 /// Asm - Target of Dwarf emission.
884 ///
885 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000886
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000887 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000888 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000889
Jim Laskey65195462006-10-30 13:35:07 +0000890 /// TD - Target data.
891 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000892
Jim Laskey65195462006-10-30 13:35:07 +0000893 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000894 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000895
Jim Laskey65195462006-10-30 13:35:07 +0000896 /// M - Current module.
897 ///
898 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000899
Jim Laskey65195462006-10-30 13:35:07 +0000900 /// MF - Current machine function.
901 ///
902 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000903
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000904 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000905 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000906 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000907
Jim Laskey65195462006-10-30 13:35:07 +0000908 /// SubprogramCount - The running count of functions being compiled.
909 ///
910 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000911
Chris Lattner9251f132007-09-24 03:35:37 +0000912 /// Flavor - A unique string indicating what dwarf producer this is, used to
913 /// unique labels.
914 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000915
916 unsigned SetCounter;
Owen Andersoncb371882008-08-21 00:14:44 +0000917 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattner9251f132007-09-24 03:35:37 +0000918 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000919 : O(OS)
920 , Asm(A)
921 , TAI(T)
922 , TD(Asm->TM.getTargetData())
923 , RI(Asm->TM.getRegisterInfo())
924 , M(NULL)
925 , MF(NULL)
926 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000927 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000928 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000929 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000930 {
931 }
932
933public:
934
935 //===--------------------------------------------------------------------===//
936 // Accessors.
937 //
938 AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000939 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000940 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000941 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000942
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000943 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
944 const {
945 if (isInSection && TAI->getDwarfSectionOffsetDirective())
946 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000947 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000948 O << TAI->getData32bitsDirective();
949 else
950 O << TAI->getData64bitsDirective();
951 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000952
Jim Laskeyb82313f2007-02-01 16:31:34 +0000953 /// PrintLabelName - Print label name in form used by Dwarf writer.
954 ///
955 void PrintLabelName(DWLabel Label) const {
956 PrintLabelName(Label.Tag, Label.Number);
957 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000958 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000959 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000960 if (Number) O << Number;
961 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000962
Chris Lattner9251f132007-09-24 03:35:37 +0000963 void PrintLabelName(const char *Tag, unsigned Number,
964 const char *Suffix) const {
965 O << TAI->getPrivateGlobalPrefix() << Tag;
966 if (Number) O << Number;
967 O << Suffix;
968 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000969
Jim Laskeyb82313f2007-02-01 16:31:34 +0000970 /// EmitLabel - Emit location label for internal use by Dwarf.
971 ///
972 void EmitLabel(DWLabel Label) const {
973 EmitLabel(Label.Tag, Label.Number);
974 }
975 void EmitLabel(const char *Tag, unsigned Number) const {
976 PrintLabelName(Tag, Number);
977 O << ":\n";
978 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000979
Jim Laskeyb82313f2007-02-01 16:31:34 +0000980 /// EmitReference - Emit a reference to a label.
981 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000982 void EmitReference(DWLabel Label, bool IsPCRelative = false,
983 bool Force32Bit = false) const {
984 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000985 }
986 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000987 bool IsPCRelative = false, bool Force32Bit = false) const {
988 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000989 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000990
Jim Laskeyb82313f2007-02-01 16:31:34 +0000991 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
992 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000993 void EmitReference(const std::string &Name, bool IsPCRelative = false,
994 bool Force32Bit = false) const {
995 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000996
Jim Laskeyb82313f2007-02-01 16:31:34 +0000997 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000998
Jim Laskeyb82313f2007-02-01 16:31:34 +0000999 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
1000 }
1001
1002 /// EmitDifference - Emit the difference between two labels. Some
1003 /// assemblers do not behave with absolute expressions with data directives,
1004 /// so there is an option (needsSet) to use an intermediary set expression.
1005 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +00001006 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001007 EmitDifference(LabelHi.Tag, LabelHi.Number,
1008 LabelLo.Tag, LabelLo.Number,
1009 IsSmall);
1010 }
1011 void EmitDifference(const char *TagHi, unsigned NumberHi,
1012 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +00001013 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001014 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001015 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +00001016 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001017 O << ",";
1018 PrintLabelName(TagHi, NumberHi);
1019 O << "-";
1020 PrintLabelName(TagLo, NumberLo);
1021 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001022
1023 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +00001024 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001025 ++SetCounter;
1026 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001027 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001028
Jim Laskeyb82313f2007-02-01 16:31:34 +00001029 PrintLabelName(TagHi, NumberHi);
1030 O << "-";
1031 PrintLabelName(TagLo, NumberLo);
1032 }
1033 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001034
1035 void EmitSectionOffset(const char* Label, const char* Section,
1036 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +00001037 bool IsSmall = false, bool isEH = false,
1038 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001039 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +00001040 if (isEH)
1041 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
1042 else
1043 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
1044
1045 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001046 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +00001047 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001048 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001049 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001050
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001051 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001052 O << "-";
1053 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001054 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001055 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001056
1057 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001058
Chris Lattner9251f132007-09-24 03:35:37 +00001059 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001060 ++SetCounter;
1061 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001062 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001063
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001064 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001065
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001066 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001067 O << "-";
1068 PrintLabelName(Section, SectionNumber);
1069 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001070 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001071 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001072
Jim Laskeyb82313f2007-02-01 16:31:34 +00001073 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1074 /// frame.
1075 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +00001076 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001077 int stackGrowth =
1078 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1079 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001080 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +00001081 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +00001082
1083 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001084 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +00001085 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001086
Jim Laskeyb82313f2007-02-01 16:31:34 +00001087 if (LabelID) {
1088 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001089
Jim Laskeyb82313f2007-02-01 16:31:34 +00001090 // Throw out move if the label is invalid.
1091 if (!LabelID) continue;
1092 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001093
Jim Laskeyb82313f2007-02-01 16:31:34 +00001094 const MachineLocation &Dst = Move.getDestination();
1095 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001096
Jim Laskeyb82313f2007-02-01 16:31:34 +00001097 // Advance row if new location.
1098 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1099 Asm->EmitInt8(DW_CFA_advance_loc4);
1100 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001101 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1102 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001103
Jim Laskeyb82313f2007-02-01 16:31:34 +00001104 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001105 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001106 IsLocal = true;
1107 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001108
Jim Laskeyb82313f2007-02-01 16:31:34 +00001109 // If advancing cfa.
Dan Gohmand735b802008-10-03 15:45:36 +00001110 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1111 if (!Src.isReg()) {
1112 if (Src.getReg() == MachineLocation::VirtualFP) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001113 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1114 Asm->EOL("DW_CFA_def_cfa_offset");
1115 } else {
1116 Asm->EmitInt8(DW_CFA_def_cfa);
1117 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmand735b802008-10-03 15:45:36 +00001118 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001119 Asm->EOL("Register");
1120 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001121
Jim Laskeyb82313f2007-02-01 16:31:34 +00001122 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001123
Jim Laskeyb82313f2007-02-01 16:31:34 +00001124 Asm->EmitULEB128Bytes(Offset);
1125 Asm->EOL("Offset");
1126 } else {
1127 assert(0 && "Machine move no supported yet.");
1128 }
Dan Gohmand735b802008-10-03 15:45:36 +00001129 } else if (Src.isReg() &&
1130 Src.getReg() == MachineLocation::VirtualFP) {
1131 if (Dst.isReg()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001132 Asm->EmitInt8(DW_CFA_def_cfa_register);
1133 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmand735b802008-10-03 15:45:36 +00001134 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001135 Asm->EOL("Register");
1136 } else {
1137 assert(0 && "Machine move no supported yet.");
1138 }
1139 } else {
Dan Gohmand735b802008-10-03 15:45:36 +00001140 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001141 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001142
Jim Laskeyb82313f2007-02-01 16:31:34 +00001143 if (Offset < 0) {
1144 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1145 Asm->EOL("DW_CFA_offset_extended_sf");
1146 Asm->EmitULEB128Bytes(Reg);
1147 Asm->EOL("Reg");
1148 Asm->EmitSLEB128Bytes(Offset);
1149 Asm->EOL("Offset");
1150 } else if (Reg < 64) {
1151 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Chengd4114192008-07-09 21:53:02 +00001152 if (VerboseAsm)
1153 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1154 else
1155 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001156 Asm->EmitULEB128Bytes(Offset);
1157 Asm->EOL("Offset");
1158 } else {
1159 Asm->EmitInt8(DW_CFA_offset_extended);
1160 Asm->EOL("DW_CFA_offset_extended");
1161 Asm->EmitULEB128Bytes(Reg);
1162 Asm->EOL("Reg");
1163 Asm->EmitULEB128Bytes(Offset);
1164 Asm->EOL("Offset");
1165 }
1166 }
1167 }
1168 }
1169
Jim Laskey072200c2007-01-29 18:51:14 +00001170};
1171
1172//===----------------------------------------------------------------------===//
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001173/// SourceLineInfo - This class is used to record source line correspondence.
1174///
1175class SrcLineInfo {
1176 unsigned Line; // Source line number.
1177 unsigned Column; // Source column.
1178 unsigned SourceID; // Source ID number.
1179 unsigned LabelID; // Label in code ID number.
1180public:
1181 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
1182 : Line(L), Column(C), SourceID(S), LabelID(I) {}
1183
1184 // Accessors
1185 unsigned getLine() const { return Line; }
1186 unsigned getColumn() const { return Column; }
1187 unsigned getSourceID() const { return SourceID; }
1188 unsigned getLabelID() const { return LabelID; }
1189};
1190
1191
1192//===----------------------------------------------------------------------===//
Devang Patel8526cc02009-01-05 22:35:52 +00001193/// SrcFileInfo - This class is used to track source information.
1194///
1195class SrcFileInfo {
1196 unsigned DirectoryID; // Directory ID number.
1197 std::string Name; // File name (not including directory.)
1198public:
1199 SrcFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
1200
1201 // Accessors
1202 unsigned getDirectoryID() const { return DirectoryID; }
1203 const std::string &getName() const { return Name; }
1204
1205 /// operator== - Used by UniqueVector to locate entry.
1206 ///
1207 bool operator==(const SourceFileInfo &SI) const {
1208 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
1209 }
1210
1211 /// operator< - Used by UniqueVector to locate entry.
1212 ///
1213 bool operator<(const SrcFileInfo &SI) const {
1214 return getDirectoryID() < SI.getDirectoryID() ||
1215 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
1216 }
1217};
1218
1219//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +00001220/// DbgVariable - This class is used to track local variable information.
1221///
1222class DbgVariable {
1223private:
1224 DIVariable *Var; // Variable Descriptor.
1225 unsigned FrameIndex; // Variable frame index.
1226
1227public:
1228 DbgVariable(DIVariable *V, unsigned I) : Var(V), FrameIndex(I) {}
1229
1230 // Accessors.
1231 DIVariable *getVariable() const { return Var; }
1232 unsigned getFrameIndex() const { return FrameIndex; }
1233};
1234
1235//===----------------------------------------------------------------------===//
1236/// DbgScope - This class is used to track scope information.
1237///
1238class DbgScope {
1239private:
1240 DbgScope *Parent; // Parent to this scope.
1241 DIDescriptor *Desc; // Debug info descriptor for scope.
1242 // Either subprogram or block.
1243 unsigned StartLabelID; // Label ID of the beginning of scope.
1244 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel9795da52009-01-10 02:42:49 +00001245 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
1246 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001247
1248public:
1249 DbgScope(DbgScope *P, DIDescriptor *D)
1250 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1251 {}
1252 ~DbgScope();
1253
1254 // Accessors.
1255 DbgScope *getParent() const { return Parent; }
1256 DIDescriptor *getDesc() const { return Desc; }
1257 unsigned getStartLabelID() const { return StartLabelID; }
1258 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel9795da52009-01-10 02:42:49 +00001259 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1260 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001261 void setStartLabelID(unsigned S) { StartLabelID = S; }
1262 void setEndLabelID(unsigned E) { EndLabelID = E; }
1263
1264 /// AddScope - Add a scope to the scope.
1265 ///
1266 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1267
1268 /// AddVariable - Add a variable to the scope.
1269 ///
1270 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1271};
1272
1273//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001274/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001275///
1276class DwarfDebug : public Dwarf {
1277
1278private:
Jim Laskey65195462006-10-30 13:35:07 +00001279 //===--------------------------------------------------------------------===//
1280 // Attributes used to construct specific Dwarf sections.
1281 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001282
Jim Laskey65195462006-10-30 13:35:07 +00001283 /// CompileUnits - All the compile units involved in this build. The index
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001284 /// of each entry in this vector corresponds to the sources in MMI.
Jim Laskey65195462006-10-30 13:35:07 +00001285 std::vector<CompileUnit *> CompileUnits;
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001286 DenseMap<Value *, CompileUnit *> DW_CUs;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001287
Jim Laskeyef42a012006-11-02 20:12:39 +00001288 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001289 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001290 FoldingSet<DIEAbbrev> AbbreviationsSet;
1291
1292 /// Abbreviations - A list of all the unique abbreviations in use.
1293 ///
1294 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001295
Jim Laskeyef42a012006-11-02 20:12:39 +00001296 /// ValuesSet - Used to uniquely define values.
1297 ///
Devang Patel8526cc02009-01-05 22:35:52 +00001298 // Directories - Uniquing vector for directories.
1299 UniqueVector<std::string> Directories;
1300
1301 // SourceFiles - Uniquing vector for source files.
1302 UniqueVector<SrcFileInfo> SrcFiles;
1303
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001304 // Lines - List of of source line correspondence.
1305 std::vector<SrcLineInfo> Lines;
1306
Jim Laskeyef42a012006-11-02 20:12:39 +00001307 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001308
Jim Laskeyef42a012006-11-02 20:12:39 +00001309 /// Values - A list of all the unique values in use.
1310 ///
1311 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001312
Jim Laskey65195462006-10-30 13:35:07 +00001313 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001314 ///
Jim Laskey65195462006-10-30 13:35:07 +00001315 UniqueVector<std::string> StringPool;
1316
1317 /// UnitMap - Map debug information descriptor to compile unit.
1318 ///
1319 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001320
Jim Laskey65195462006-10-30 13:35:07 +00001321 /// SectionMap - Provides a unique id per text section.
1322 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001323 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001324
Jim Laskey65195462006-10-30 13:35:07 +00001325 /// SectionSourceLines - Tracks line numbers per text section.
1326 ///
1327 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1328
Jim Laskeybacd3042007-02-21 22:48:45 +00001329 /// didInitial - Flag to indicate if initial emission has been done.
1330 ///
1331 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001332
Jim Laskeybacd3042007-02-21 22:48:45 +00001333 /// shouldEmit - Flag to indicate if debug information should be emitted.
1334 ///
1335 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001336
Devang Patel7a6e5a32009-01-08 02:33:41 +00001337 // RootScope - Top level scope for the current function.
1338 //
1339 DbgScope *RootDbgScope;
1340
1341 // DbgScopeMap - Tracks the scopes in the current function.
1342 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
1343
1344 // DbgLabelIDList - One entry per assigned label. Normally the entry is equal to
1345 // the list index(+1). If the entry is zero then the label has been deleted.
1346 // Any other value indicates the label has been deleted by is mapped to
1347 // another label.
1348 SmallVector<unsigned, 32> DbgLabelIDList;
1349
1350 /// NextLabelID - Return the next unique label id.
1351 ///
1352 unsigned NextLabelID() {
1353 unsigned ID = (unsigned)DbgLabelIDList.size() + 1;
1354 DbgLabelIDList.push_back(ID);
1355 return ID;
1356 }
1357
1358 /// RemapLabel - Indicate that a label has been merged into another.
1359 ///
1360 void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) {
1361 assert(0 < OldLabelID && OldLabelID <= DbgLabelIDList.size() &&
1362 "Old label ID out of range.");
1363 assert(NewLabelID <= DbgLabelIDList.size() &&
1364 "New label ID out of range.");
1365 DbgLabelIDList[OldLabelID - 1] = NewLabelID;
1366 }
1367
1368 /// MappedLabel - Find out the label's final ID. Zero indicates deletion.
1369 /// ID != Mapped ID indicates that the label was folded into another label.
1370 unsigned MappedLabel(unsigned LabelID) const {
1371 assert(LabelID <= DbgLabelIDList.size() && "Debug label ID out of range.");
1372 return LabelID ? DbgLabelIDList[LabelID - 1] : 0;
1373 }
1374
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001375 struct FunctionDebugFrameInfo {
1376 unsigned Number;
1377 std::vector<MachineMove> Moves;
1378
1379 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001380 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001381 };
1382
1383 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001384
Jim Laskey65195462006-10-30 13:35:07 +00001385public:
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001386
Jim Laskeybacd3042007-02-21 22:48:45 +00001387 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1388 ///
1389 bool ShouldEmitDwarf() const { return shouldEmit; }
Jim Laskey65195462006-10-30 13:35:07 +00001390
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001391 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001392 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001393 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1394 // Profile the node so that we can make it unique.
1395 FoldingSetNodeID ID;
1396 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001397
Jim Laskeyef42a012006-11-02 20:12:39 +00001398 // Check the set for priors.
1399 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001400
Jim Laskeyef42a012006-11-02 20:12:39 +00001401 // If it's newly added.
1402 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001403 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001404 Abbreviations.push_back(&Abbrev);
1405 // Assign the vector position + 1 as its number.
1406 Abbrev.setNumber(Abbreviations.size());
1407 } else {
1408 // Assign existing abbreviation number.
1409 Abbrev.setNumber(InSet->getNumber());
1410 }
1411 }
1412
Jim Laskey65195462006-10-30 13:35:07 +00001413 /// NewString - Add a string to the constant pool and returns a label.
1414 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001415 DWLabel NewString(const std::string &String) {
1416 unsigned StringID = StringPool.insert(String);
1417 return DWLabel("string", StringID);
1418 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001419
Jim Laskeyef42a012006-11-02 20:12:39 +00001420 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1421 /// entry.
1422 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1423 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001424
Jim Laskeyef42a012006-11-02 20:12:39 +00001425 if (Entry) {
1426 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001427 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001428 void *Where;
1429 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001430
Jim Laskeyf6733882006-11-02 21:48:18 +00001431 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001432
Jim Laskeyef42a012006-11-02 20:12:39 +00001433 Value = new DIEntry(Entry);
1434 ValuesSet.InsertNode(Value, Where);
1435 } else {
1436 Value = new DIEntry(Entry);
1437 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001438
Jim Laskeyef42a012006-11-02 20:12:39 +00001439 Values.push_back(Value);
1440 return Value;
1441 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001442
Jim Laskeyef42a012006-11-02 20:12:39 +00001443 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1444 ///
1445 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1446 Value->Entry = Entry;
1447 // Add to values set if not already there. If it is, we merely have a
1448 // duplicate in the values list (no harm.)
1449 ValuesSet.GetOrInsertNode(Value);
1450 }
1451
1452 /// AddUInt - Add an unsigned integer attribute data and value.
1453 ///
1454 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1455 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1456
1457 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001458 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001459 void *Where;
1460 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1461 if (!Value) {
1462 Value = new DIEInteger(Integer);
1463 ValuesSet.InsertNode(Value, Where);
1464 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001465 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001466
Jim Laskeyef42a012006-11-02 20:12:39 +00001467 Die->AddValue(Attribute, Form, Value);
1468 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001469
Jim Laskeyef42a012006-11-02 20:12:39 +00001470 /// AddSInt - Add an signed integer attribute data and value.
1471 ///
1472 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1473 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1474
1475 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001476 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001477 void *Where;
1478 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1479 if (!Value) {
1480 Value = new DIEInteger(Integer);
1481 ValuesSet.InsertNode(Value, Where);
1482 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001483 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001484
Jim Laskeyef42a012006-11-02 20:12:39 +00001485 Die->AddValue(Attribute, Form, Value);
1486 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001487
Jim Laskeyef42a012006-11-02 20:12:39 +00001488 /// AddString - Add a std::string attribute data and value.
1489 ///
1490 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1491 const std::string &String) {
1492 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001493 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001494 void *Where;
1495 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1496 if (!Value) {
1497 Value = new DIEString(String);
1498 ValuesSet.InsertNode(Value, Where);
1499 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001500 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001501
Jim Laskeyef42a012006-11-02 20:12:39 +00001502 Die->AddValue(Attribute, Form, Value);
1503 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001504
Jim Laskeyef42a012006-11-02 20:12:39 +00001505 /// AddLabel - Add a Dwarf label attribute data and value.
1506 ///
1507 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1508 const DWLabel &Label) {
1509 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001510 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001511 void *Where;
1512 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1513 if (!Value) {
1514 Value = new DIEDwarfLabel(Label);
1515 ValuesSet.InsertNode(Value, Where);
1516 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001517 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001518
Jim Laskeyef42a012006-11-02 20:12:39 +00001519 Die->AddValue(Attribute, Form, Value);
1520 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001521
Jim Laskeyef42a012006-11-02 20:12:39 +00001522 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1523 ///
1524 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1525 const std::string &Label) {
1526 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001527 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001528 void *Where;
1529 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1530 if (!Value) {
1531 Value = new DIEObjectLabel(Label);
1532 ValuesSet.InsertNode(Value, Where);
1533 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001534 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001535
Jim Laskeyef42a012006-11-02 20:12:39 +00001536 Die->AddValue(Attribute, Form, Value);
1537 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001538
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001539 /// AddSectionOffset - Add a section offset label attribute data and value.
1540 ///
1541 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1542 const DWLabel &Label, const DWLabel &Section,
1543 bool isEH = false, bool useSet = true) {
1544 FoldingSetNodeID ID;
1545 DIESectionOffset::Profile(ID, Label, Section);
1546 void *Where;
1547 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1548 if (!Value) {
1549 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1550 ValuesSet.InsertNode(Value, Where);
1551 Values.push_back(Value);
1552 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001553
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001554 Die->AddValue(Attribute, Form, Value);
1555 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001556
Jim Laskeyef42a012006-11-02 20:12:39 +00001557 /// AddDelta - Add a label delta attribute data and value.
1558 ///
1559 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1560 const DWLabel &Hi, const DWLabel &Lo) {
1561 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001562 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001563 void *Where;
1564 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1565 if (!Value) {
1566 Value = new DIEDelta(Hi, Lo);
1567 ValuesSet.InsertNode(Value, Where);
1568 Values.push_back(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, Form, Value);
1572 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001573
Jim Laskeyef42a012006-11-02 20:12:39 +00001574 /// AddDIEntry - Add a DIE attribute data and value.
1575 ///
1576 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1577 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1578 }
1579
1580 /// AddBlock - Add block data.
1581 ///
1582 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1583 Block->ComputeSize(*this);
1584 FoldingSetNodeID ID;
1585 Block->Profile(ID);
1586 void *Where;
1587 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1588 if (!Value) {
1589 Value = Block;
1590 ValuesSet.InsertNode(Value, Where);
1591 Values.push_back(Value);
1592 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001593 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001594 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001595 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001596 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001597
Jim Laskeyef42a012006-11-02 20:12:39 +00001598 Die->AddValue(Attribute, Block->BestForm(), Value);
1599 }
1600
Jim Laskey65195462006-10-30 13:35:07 +00001601private:
1602
1603 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001604 /// entry.
1605 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1606 if (File && Line) {
1607 CompileUnit *FileUnit = FindCompileUnit(File);
1608 unsigned FileID = FileUnit->getID();
1609 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1610 AddUInt(Die, DW_AT_decl_line, 0, Line);
1611 }
1612 }
Jim Laskey65195462006-10-30 13:35:07 +00001613
Devang Patel8526cc02009-01-05 22:35:52 +00001614 /// AddSourceLine - Add location information to specified debug information
1615 /// entry.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001616 void AddSourceLine(DIE *Die, DIVariable *V) {
1617 unsigned FileID = 0;
1618 unsigned Line = V->getLineNumber();
1619 if (V->getVersion() < DIDescriptor::Version7) {
1620 // Version6 or earlier. Use compile unit info to get file id.
1621 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1622 FileID = Unit->getID();
1623 } else {
1624 // Version7 or newer, use filename and directory info from DIVariable
1625 // directly.
1626 unsigned DID = Directories.idFor(V->getDirectory());
1627 FileID = SrcFiles.idFor(SrcFileInfo(DID, V->getFilename()));
1628 }
1629 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1630 AddUInt(Die, DW_AT_decl_line, 0, Line);
1631 }
1632
1633 /// AddSourceLine - Add location information to specified debug information
1634 /// entry.
Devang Patel8526cc02009-01-05 22:35:52 +00001635 void AddSourceLine(DIE *Die, DIGlobal *G) {
1636 unsigned FileID = 0;
1637 unsigned Line = G->getLineNumber();
1638 if (G->getVersion() < DIDescriptor::Version7) {
1639 // Version6 or earlier. Use compile unit info to get file id.
1640 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1641 FileID = Unit->getID();
1642 } else {
1643 // Version7 or newer, use filename and directory info from DIGlobal
1644 // directly.
1645 unsigned DID = Directories.idFor(G->getDirectory());
1646 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1647 }
1648 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1649 AddUInt(Die, DW_AT_decl_line, 0, Line);
1650 }
1651
1652 void AddSourceLine(DIE *Die, DIType *G) {
1653 unsigned FileID = 0;
1654 unsigned Line = G->getLineNumber();
1655 if (G->getVersion() < DIDescriptor::Version7) {
1656 // Version6 or earlier. Use compile unit info to get file id.
1657 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1658 FileID = Unit->getID();
1659 } else {
1660 // Version7 or newer, use filename and directory info from DIGlobal
1661 // directly.
1662 unsigned DID = Directories.idFor(G->getDirectory());
1663 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1664 }
1665 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1666 AddUInt(Die, DW_AT_decl_line, 0, Line);
1667 }
1668
Jim Laskey65195462006-10-30 13:35:07 +00001669 /// AddAddress - Add an address attribute to a die based on the location
1670 /// provided.
1671 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001672 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001673 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001674 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001675
Dan Gohmand735b802008-10-03 15:45:36 +00001676 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001677 if (Reg < 32) {
1678 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1679 } else {
1680 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1681 AddUInt(Block, 0, DW_FORM_udata, Reg);
1682 }
1683 } else {
1684 if (Reg < 32) {
1685 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1686 } else {
1687 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1688 AddUInt(Block, 0, DW_FORM_udata, Reg);
1689 }
1690 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1691 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001692
Jim Laskeyef42a012006-11-02 20:12:39 +00001693 AddBlock(Die, Attribute, 0, Block);
1694 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001695
Jim Laskeyef42a012006-11-02 20:12:39 +00001696 /// AddBasicType - Add a new basic type attribute to the specified entity.
1697 ///
1698 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1699 const std::string &Name,
1700 unsigned Encoding, unsigned Size) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001701
Jim Laskeyef42a012006-11-02 20:12:39 +00001702 DIE Buffer(DW_TAG_base_type);
1703 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1704 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1705 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel9ede3f22009-01-05 17:44:11 +00001706 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1707 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001708 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001709
Jim Laskeyef42a012006-11-02 20:12:39 +00001710 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1711 ///
1712 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001713 DIE Buffer(DW_TAG_pointer_type);
Dan Gohman82482942007-09-27 23:12:31 +00001714 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Jim Laskeyef42a012006-11-02 20:12:39 +00001715 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelca03c272009-01-05 17:45:59 +00001716 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1717 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001718 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001719
Jim Laskeyef42a012006-11-02 20:12:39 +00001720 /// AddType - Add a new type attribute to the specified entity.
1721 ///
1722 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1723 if (!TyDesc) {
Jim Laskey2b935d52007-01-26 14:19:17 +00001724 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001725 } else {
1726 // Check for pre-existence.
1727 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001728
Jim Laskeyef42a012006-11-02 20:12:39 +00001729 // If it exists then use the existing value.
1730 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001731 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1732 return;
1733 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001734
Jim Laskeyef42a012006-11-02 20:12:39 +00001735 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1736 // FIXME - Not sure why programs and variables are coming through here.
1737 // Short cut for handling subprogram types (not really a TyDesc.)
1738 AddPointerType(Entity, Unit, SubprogramTy->getName());
1739 } else if (GlobalVariableDesc *GlobalTy =
1740 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1741 // FIXME - Not sure why programs and variables are coming through here.
1742 // Short cut for handling global variable types (not really a TyDesc.)
1743 AddPointerType(Entity, Unit, GlobalTy->getName());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001744 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001745 // Set up proxy.
1746 Slot = NewDIEntry();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001747
Jim Laskeyef42a012006-11-02 20:12:39 +00001748 // Construct type.
1749 DIE Buffer(DW_TAG_base_type);
1750 ConstructType(Buffer, TyDesc, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001751
Jim Laskeyef42a012006-11-02 20:12:39 +00001752 // Add debug information entry to entity and unit.
1753 DIE *Die = Unit->AddDie(Buffer);
1754 SetDIEntry(Slot, Die);
1755 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1756 }
1757 }
1758 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001759
Devang Patel48d190f2009-01-05 21:47:57 +00001760 /// AddType - Add a new type attribute to the specified entity.
1761 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
1762 if (Ty.isNull()) {
1763 AddBasicType(Entity, DW_Unit, "", DW_ATE_signed, sizeof(int32_t));
1764 return;
1765 }
1766
1767 // Check for pre-existence.
1768 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1769 // If it exists then use the existing value.
1770 if (Slot) {
1771 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1772 return;
1773 }
1774
1775 // Set up proxy.
1776 Slot = NewDIEntry();
1777
1778 // Construct type.
1779 DIE Buffer(DW_TAG_base_type);
1780 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1781 ConstructTypeDIE(DW_Unit, Buffer, BT);
1782 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1783 ConstructTypeDIE(DW_Unit, Buffer, DT);
1784 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1785 ConstructTypeDIE(DW_Unit, Buffer, CT);
1786
1787 // Add debug information entry to entity and unit.
1788 DIE *Die = DW_Unit->AddDie(Buffer);
1789 SetDIEntry(Slot, Die);
1790 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1791 }
1792
Devang Patele5202732009-01-05 19:07:53 +00001793 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1794 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1795 DIBasicType *BTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001796
1797 // Get core information.
1798 const std::string &Name = BTy->getName();
1799 Buffer.setTag(DW_TAG_base_type);
1800 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy->getEncoding());
1801 // Add name if not anonymous or intermediate type.
1802 if (!Name.empty())
1803 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1804 uint64_t Size = BTy->getSizeInBits() >> 3;
1805 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1806 }
1807
Devang Patele5202732009-01-05 19:07:53 +00001808 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1809 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1810 DIDerivedType *DTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001811
1812 // Get core information.
1813 const std::string &Name = DTy->getName();
1814 uint64_t Size = DTy->getSizeInBits() >> 3;
1815 unsigned Tag = DTy->getTag();
1816 // FIXME - Workaround for templates.
1817 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1818
1819 Buffer.setTag(Tag);
1820 // Map to main type, void will not have a type.
1821 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001822 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001823
1824 // Add name if not anonymous or intermediate type.
1825 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1826
1827 // Add size if non-zero (derived types might be zero-sized.)
1828 if (Size)
1829 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1830
1831 // Add source line info if available and TyDesc is not a forward
1832 // declaration.
1833 // FIXME - Enable this. if (!DTy->isForwardDecl())
1834 // FIXME - Enable this. AddSourceLine(&Buffer, *DTy);
1835 }
1836
Devang Patelf4215332009-01-05 19:55:51 +00001837 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1838 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1839 DICompositeType *CTy) {
1840
1841 // Get core information.
1842 const std::string &Name = CTy->getName();
1843 uint64_t Size = CTy->getSizeInBits() >> 3;
1844 unsigned Tag = CTy->getTag();
1845 switch (Tag) {
1846 case DW_TAG_vector_type:
1847 case DW_TAG_array_type:
1848 ConstructArrayTypeDIE(DW_Unit, Buffer, CTy);
1849 break;
1850 //FIXME - Enable this.
1851 // case DW_TAG_enumeration_type:
1852 // DIArray Elements = CTy->getTypeArray();
1853 // // Add enumerators to enumeration type.
1854 // for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i)
1855 // ConstructEnumTypeDIE(Buffer, &Elements.getElement(i));
1856 // break;
1857 case DW_TAG_subroutine_type:
1858 {
1859 // Add prototype flag.
1860 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1861 DIArray Elements = CTy->getTypeArray();
1862 // Add return type.
Devang Patel48d190f2009-01-05 21:47:57 +00001863 DIDescriptor RTy = Elements.getElement(0);
1864 if (DIBasicType *BT = dyn_cast<DIBasicType>(&RTy))
1865 AddType(DW_Unit, &Buffer, *BT);
1866 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&RTy))
1867 AddType(DW_Unit, &Buffer, *DT);
1868 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&RTy))
1869 AddType(DW_Unit, &Buffer, *CT);
1870
1871 //AddType(DW_Unit, &Buffer, Elements.getElement(0));
Devang Patelf4215332009-01-05 19:55:51 +00001872 // Add arguments.
1873 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1874 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001875 DIDescriptor Ty = Elements.getElement(i);
1876 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1877 AddType(DW_Unit, &Buffer, *BT);
1878 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1879 AddType(DW_Unit, &Buffer, *DT);
1880 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1881 AddType(DW_Unit, &Buffer, *CT);
Devang Patelf4215332009-01-05 19:55:51 +00001882 Buffer.AddChild(Arg);
1883 }
1884 }
1885 break;
1886 case DW_TAG_structure_type:
1887 case DW_TAG_union_type:
1888 {
1889 // Add elements to structure type.
1890 DIArray Elements = CTy->getTypeArray();
1891 // Add elements to structure type.
1892 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1893 DIDescriptor Element = Elements.getElement(i);
1894 if (DISubprogram *SP = dyn_cast<DISubprogram>(&Element))
1895 ConstructFieldTypeDIE(DW_Unit, Buffer, SP);
1896 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Element))
1897 ConstructFieldTypeDIE(DW_Unit, Buffer, DT);
1898 else if (DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(&Element))
1899 ConstructFieldTypeDIE(DW_Unit, Buffer, GV);
1900 }
1901 }
1902 break;
1903 default:
1904 break;
1905 }
1906
1907 // Add name if not anonymous or intermediate type.
1908 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1909
1910 // Add size if non-zero (derived types might be zero-sized.)
1911 if (Size)
1912 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1913 else {
1914 // Add zero size even if it is not a forward declaration.
1915 // FIXME - Enable this.
1916 // if (!CTy->isDefinition())
1917 // AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1918 // else
1919 // AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1920 }
1921
1922 // Add source line info if available and TyDesc is not a forward
1923 // declaration.
1924 // FIXME - Enable this.
1925 // if (CTy->isForwardDecl())
1926 // AddSourceLine(&Buffer, *CTy);
1927 }
1928
Devang Patel68afdc32009-01-05 18:33:01 +00001929 // ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1930 void ConstructSubrangeDIE (DIE &Buffer, DISubrange *SR, DIE *IndexTy) {
1931 int64_t L = SR->getLo();
1932 int64_t H = SR->getHi();
1933 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1934 if (L != H) {
1935 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1936 if (L)
1937 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1938 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
1939 }
1940 Buffer.AddChild(DW_Subrange);
1941 }
1942
1943 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1944 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1945 DICompositeType *CTy) {
1946 Buffer.setTag(DW_TAG_array_type);
1947 if (CTy->getTag() == DW_TAG_vector_type)
1948 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1949
1950 DIArray Elements = CTy->getTypeArray();
1951 // FIXME - Enable this.
Devang Patel48d190f2009-01-05 21:47:57 +00001952 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001953
1954 // Construct an anonymous type for index type.
1955 DIE IdxBuffer(DW_TAG_base_type);
1956 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1957 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1958 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1959
1960 // Add subranges to array type.
1961 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001962 DIDescriptor Element = Elements.getElement(i);
1963 if (DISubrange *SR = dyn_cast<DISubrange>(&Element))
1964 ConstructSubrangeDIE(Buffer, SR, IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001965 }
1966 }
1967
Devang Patelc69bf2c2009-01-05 18:38:38 +00001968 /// ConstructEnumTypeDIE - Construct enum type DIE from
1969 /// DIEnumerator.
Devang Patelf4215332009-01-05 19:55:51 +00001970 void ConstructEnumTypeDIE(CompileUnit *DW_Unit,
1971 DIE &Buffer, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001972
1973 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1974 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1975 int64_t Value = ETy->getEnumValue();
1976 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1977 Buffer.AddChild(Enumerator);
1978 }
Devang Patel68afdc32009-01-05 18:33:01 +00001979
Devang Patel86ae1422009-01-05 18:59:44 +00001980 /// ConstructFieldTypeDIE - Construct variable DIE for a struct field.
1981 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1982 DIE &Buffer, DIGlobalVariable *V) {
1983
1984 DIE *VariableDie = new DIE(DW_TAG_variable);
1985 const std::string &LinkageName = V->getLinkageName();
1986 if (!LinkageName.empty())
1987 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1988 LinkageName);
1989 // FIXME - Enable this. AddSourceLine(VariableDie, V);
Devang Patel48d190f2009-01-05 21:47:57 +00001990 AddType(DW_Unit, VariableDie, V->getType());
Devang Patel86ae1422009-01-05 18:59:44 +00001991 if (!V->isLocalToUnit())
1992 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1993 AddUInt(VariableDie, DW_AT_declaration, DW_FORM_flag, 1);
1994 Buffer.AddChild(VariableDie);
1995 }
1996
1997 /// ConstructFieldTypeDIE - Construct subprogram DIE for a struct field.
1998 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1999 DIE &Buffer, DISubprogram *SP,
2000 bool IsConstructor = false) {
2001 DIE *Method = new DIE(DW_TAG_subprogram);
2002 AddString(Method, DW_AT_name, DW_FORM_string, SP->getName());
2003 const std::string &LinkageName = SP->getLinkageName();
2004 if (!LinkageName.empty())
2005 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
2006 // FIXME - Enable this. AddSourceLine(Method, SP);
2007
2008 DICompositeType MTy = SP->getType();
2009 DIArray Args = MTy.getTypeArray();
2010
2011 // Add Return Type.
Devang Patel48d190f2009-01-05 21:47:57 +00002012 if (!IsConstructor) {
2013 DIDescriptor Ty = Args.getElement(0);
2014 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
2015 AddType(DW_Unit, Method, *BT);
2016 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
2017 AddType(DW_Unit, Method, *DT);
2018 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
2019 AddType(DW_Unit, Method, *CT);
2020 }
Devang Patel86ae1422009-01-05 18:59:44 +00002021
2022 // Add arguments.
2023 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
2024 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00002025 DIDescriptor Ty = Args.getElement(i);
2026 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
2027 AddType(DW_Unit, Method, *BT);
2028 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
2029 AddType(DW_Unit, Method, *DT);
2030 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
2031 AddType(DW_Unit, Method, *CT);
Devang Patel86ae1422009-01-05 18:59:44 +00002032 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
2033 Method->AddChild(Arg);
2034 }
2035
2036 if (!SP->isLocalToUnit())
2037 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
2038 Buffer.AddChild(Method);
2039 }
2040
2041 /// COnstructFieldTypeDIE - Construct derived type DIE for a struct field.
2042 void ConstructFieldTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
2043 DIDerivedType *DTy) {
2044 unsigned Tag = DTy->getTag();
2045 DIE *MemberDie = new DIE(Tag);
2046 if (!DTy->getName().empty())
2047 AddString(MemberDie, DW_AT_name, DW_FORM_string, DTy->getName());
2048 // FIXME - Enable this. AddSourceLine(MemberDie, DTy);
2049
2050 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00002051 AddType(DW_Unit, MemberDie, FromTy);
Devang Patel86ae1422009-01-05 18:59:44 +00002052
2053 uint64_t Size = DTy->getSizeInBits();
2054 uint64_t Offset = DTy->getOffsetInBits();
2055
2056 // FIXME Handle bitfields
2057
2058 // Add size.
2059 AddUInt(MemberDie, DW_AT_bit_size, 0, Size);
2060 // Add computation for offset.
2061 DIEBlock *Block = new DIEBlock();
2062 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
2063 AddUInt(Block, 0, DW_FORM_udata, Offset >> 3);
2064 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
2065
2066 // FIXME Handle DW_AT_accessibility.
2067
2068 Buffer.AddChild(MemberDie);
2069 }
2070
Jim Laskeyef42a012006-11-02 20:12:39 +00002071 /// ConstructType - Adds all the required attributes to the type.
2072 ///
2073 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
2074 // Get core information.
2075 const std::string &Name = TyDesc->getName();
2076 uint64_t Size = TyDesc->getSize() >> 3;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002077
Jim Laskeyef42a012006-11-02 20:12:39 +00002078 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
2079 // Fundamental types like int, float, bool
2080 Buffer.setTag(DW_TAG_base_type);
2081 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
2082 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00002083 // Fetch tag.
2084 unsigned Tag = DerivedTy->getTag();
2085 // FIXME - Workaround for templates.
2086 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002087 // Pointers, typedefs et al.
Jim Laskey85f419b2006-11-09 16:32:26 +00002088 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00002089 // Map to main type, void will not have a type.
2090 if (TypeDesc *FromTy = DerivedTy->getFromType())
2091 AddType(&Buffer, FromTy, Unit);
2092 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
2093 // Fetch tag.
2094 unsigned Tag = CompTy->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002095
Jim Laskeyef42a012006-11-02 20:12:39 +00002096 // Set tag accordingly.
2097 if (Tag == DW_TAG_vector_type)
2098 Buffer.setTag(DW_TAG_array_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002099 else
Jim Laskeyef42a012006-11-02 20:12:39 +00002100 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00002101
Jim Laskeyef42a012006-11-02 20:12:39 +00002102 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002103
Jim Laskeyef42a012006-11-02 20:12:39 +00002104 switch (Tag) {
2105 case DW_TAG_vector_type:
2106 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
2107 // Fall thru
2108 case DW_TAG_array_type: {
2109 // Add element type.
2110 if (TypeDesc *FromTy = CompTy->getFromType())
2111 AddType(&Buffer, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002112
Jim Laskeyef42a012006-11-02 20:12:39 +00002113 // Don't emit size attribute.
2114 Size = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002115
Jim Laskeyef42a012006-11-02 20:12:39 +00002116 // Construct an anonymous type for index type.
Devang Patel9ede3f22009-01-05 17:44:11 +00002117 DIE Buffer(DW_TAG_base_type);
2118 AddUInt(&Buffer, DW_AT_byte_size, 0, sizeof(int32_t));
2119 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
2120 DIE *IndexTy = Unit->AddDie(Buffer);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002121
Jim Laskeyef42a012006-11-02 20:12:39 +00002122 // Add subranges to array type.
Evan Chengf8480282008-12-09 17:56:30 +00002123 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002124 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
2125 int64_t Lo = SRD->getLo();
2126 int64_t Hi = SRD->getHi();
2127 DIE *Subrange = new DIE(DW_TAG_subrange_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002128
Jim Laskeyef42a012006-11-02 20:12:39 +00002129 // If a range is available.
2130 if (Lo != Hi) {
2131 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
2132 // Only add low if non-zero.
2133 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
2134 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
2135 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002136
Jim Laskeyef42a012006-11-02 20:12:39 +00002137 Buffer.AddChild(Subrange);
2138 }
2139 break;
2140 }
2141 case DW_TAG_structure_type:
2142 case DW_TAG_union_type: {
2143 // Add elements to structure type.
Evan Chengf8480282008-12-09 17:56:30 +00002144 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002145 DebugInfoDesc *Element = Elements[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002146
Jim Laskeyef42a012006-11-02 20:12:39 +00002147 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
2148 // Add field or base class.
Jim Laskeyef42a012006-11-02 20:12:39 +00002149 unsigned Tag = MemberDesc->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002150
Jim Laskeyef42a012006-11-02 20:12:39 +00002151 // Extract the basic information.
2152 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00002153 uint64_t Size = MemberDesc->getSize();
2154 uint64_t Align = MemberDesc->getAlign();
2155 uint64_t Offset = MemberDesc->getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002156
Jim Laskeyef42a012006-11-02 20:12:39 +00002157 // Construct member debug information entry.
2158 DIE *Member = new DIE(Tag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002159
Jim Laskeyef42a012006-11-02 20:12:39 +00002160 // Add name if not "".
2161 if (!Name.empty())
2162 AddString(Member, DW_AT_name, DW_FORM_string, Name);
Evan Chengf8480282008-12-09 17:56:30 +00002163
Jim Laskeyef42a012006-11-02 20:12:39 +00002164 // Add location if available.
2165 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002166
Jim Laskeyef42a012006-11-02 20:12:39 +00002167 // Most of the time the field info is the same as the members.
2168 uint64_t FieldSize = Size;
2169 uint64_t FieldAlign = Align;
2170 uint64_t FieldOffset = Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002171
Jim Laskeyee5f9272006-12-22 20:03:42 +00002172 // Set the member type.
2173 TypeDesc *FromTy = MemberDesc->getFromType();
2174 AddType(Member, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002175
Jim Laskeyee5f9272006-12-22 20:03:42 +00002176 // Walk up typedefs until a real size is found.
2177 while (FromTy) {
2178 if (FromTy->getTag() != DW_TAG_typedef) {
2179 FieldSize = FromTy->getSize();
Devang Pateld0935c32008-12-23 21:55:38 +00002180 FieldAlign = FromTy->getAlign();
Jim Laskeyee5f9272006-12-22 20:03:42 +00002181 break;
2182 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002183
Dan Gohman275769a2007-07-23 20:24:29 +00002184 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
Jim Laskeyef42a012006-11-02 20:12:39 +00002185 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002186
Jim Laskeyef42a012006-11-02 20:12:39 +00002187 // Unless we have a bit field.
2188 if (Tag == DW_TAG_member && FieldSize != Size) {
2189 // Construct the alignment mask.
2190 uint64_t AlignMask = ~(FieldAlign - 1);
2191 // Determine the high bit + 1 of the declared size.
2192 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
2193 // Work backwards to determine the base offset of the field.
2194 FieldOffset = HiMark - FieldSize;
2195 // Now normalize offset to the field.
2196 Offset -= FieldOffset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002197
Jim Laskeyef42a012006-11-02 20:12:39 +00002198 // Maybe we need to work from the other end.
2199 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002200
Jim Laskeyef42a012006-11-02 20:12:39 +00002201 // Add size and offset.
2202 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
2203 AddUInt(Member, DW_AT_bit_size, 0, Size);
2204 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
2205 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002206
Jim Laskeyef42a012006-11-02 20:12:39 +00002207 // Add computation for offset.
2208 DIEBlock *Block = new DIEBlock();
2209 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
2210 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
2211 AddBlock(Member, DW_AT_data_member_location, 0, Block);
2212
2213 // Add accessibility (public default unless is base class.
2214 if (MemberDesc->isProtected()) {
2215 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
2216 } else if (MemberDesc->isPrivate()) {
2217 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
2218 } else if (Tag == DW_TAG_inheritance) {
2219 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
2220 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002221
Jim Laskeyef42a012006-11-02 20:12:39 +00002222 Buffer.AddChild(Member);
2223 } else if (GlobalVariableDesc *StaticDesc =
2224 dyn_cast<GlobalVariableDesc>(Element)) {
2225 // Add static member.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002226
Jim Laskeyef42a012006-11-02 20:12:39 +00002227 // Construct member debug information entry.
2228 DIE *Static = new DIE(DW_TAG_variable);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002229
Jim Laskeyef42a012006-11-02 20:12:39 +00002230 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00002231 const std::string &Name = StaticDesc->getName();
2232 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00002233 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002234 if (!LinkageName.empty()) {
2235 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
2236 LinkageName);
2237 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002238
Jim Laskeyef42a012006-11-02 20:12:39 +00002239 // Add location.
2240 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002241
Jim Laskeyef42a012006-11-02 20:12:39 +00002242 // Add type.
2243 if (TypeDesc *StaticTy = StaticDesc->getType())
2244 AddType(Static, StaticTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002245
Jim Laskeyef42a012006-11-02 20:12:39 +00002246 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00002247 if (!StaticDesc->isStatic())
2248 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002249 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002250
Jim Laskeyef42a012006-11-02 20:12:39 +00002251 Buffer.AddChild(Static);
2252 } else if (SubprogramDesc *MethodDesc =
2253 dyn_cast<SubprogramDesc>(Element)) {
2254 // Add member function.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002255
Jim Laskeyef42a012006-11-02 20:12:39 +00002256 // Construct member debug information entry.
2257 DIE *Method = new DIE(DW_TAG_subprogram);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002258
Jim Laskeyef42a012006-11-02 20:12:39 +00002259 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00002260 const std::string &Name = MethodDesc->getName();
2261 const std::string &LinkageName = MethodDesc->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002262
2263 AddString(Method, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002264 bool IsCTor = TyDesc->getName() == Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002265
Jim Laskey2172f962006-11-30 14:35:45 +00002266 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002267 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00002268 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00002269 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002270
Jim Laskeyef42a012006-11-02 20:12:39 +00002271 // Add location.
2272 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002273
Jim Laskeyef42a012006-11-02 20:12:39 +00002274 // Add type.
2275 if (CompositeTypeDesc *MethodTy =
2276 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
2277 // Get argument information.
2278 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002279
Jim Laskeyef42a012006-11-02 20:12:39 +00002280 // If not a ctor.
2281 if (!IsCTor) {
2282 // Add return type.
2283 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
2284 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002285
Jim Laskeyef42a012006-11-02 20:12:39 +00002286 // Add arguments.
Evan Chengf8480282008-12-09 17:56:30 +00002287 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002288 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2289 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
2290 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
2291 Method->AddChild(Arg);
2292 }
2293 }
2294
2295 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00002296 if (!MethodDesc->isStatic())
2297 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002298 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002299
Jim Laskeyef42a012006-11-02 20:12:39 +00002300 Buffer.AddChild(Method);
2301 }
2302 }
2303 break;
2304 }
2305 case DW_TAG_enumeration_type: {
2306 // Add enumerators to enumeration type.
Evan Chengf8480282008-12-09 17:56:30 +00002307 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002308 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
2309 const std::string &Name = ED->getName();
2310 int64_t Value = ED->getValue();
2311 DIE *Enumerator = new DIE(DW_TAG_enumerator);
2312 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
2313 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
2314 Buffer.AddChild(Enumerator);
2315 }
2316
2317 break;
2318 }
2319 case DW_TAG_subroutine_type: {
2320 // Add prototype flag.
2321 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
2322 // Add return type.
2323 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002324
Jim Laskeyef42a012006-11-02 20:12:39 +00002325 // Add arguments.
Evan Chengf8480282008-12-09 17:56:30 +00002326 for (unsigned i = 1, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002327 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2328 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
2329 Buffer.AddChild(Arg);
2330 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002331
Jim Laskeyef42a012006-11-02 20:12:39 +00002332 break;
2333 }
2334 default: break;
2335 }
2336 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002337
Jim Laskeyef42a012006-11-02 20:12:39 +00002338 // Add name if not anonymous or intermediate type.
2339 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Evan Chengf8480282008-12-09 17:56:30 +00002340
Evan Cheng94ea5be2008-12-10 00:15:44 +00002341 // Add size if non-zero (derived types might be zero-sized.)
2342 if (Size)
2343 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
2344 else if (isa<CompositeTypeDesc>(TyDesc)) {
2345 // If TyDesc is a composite type, then add size even if it's zero unless
2346 // it's a forward declaration.
2347 if (TyDesc->isForwardDecl())
2348 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
2349 else
2350 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
2351 }
2352
2353 // Add source line info if available and TyDesc is not a forward
2354 // declaration.
2355 if (!TyDesc->isForwardDecl())
2356 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
Jim Laskeyef42a012006-11-02 20:12:39 +00002357 }
2358
2359 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00002360 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002361 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
2362 // Construct debug information entry.
2363 DIE *Die = new DIE(DW_TAG_compile_unit);
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00002364 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2365 DWLabel("section_line", 0), DWLabel("section_line", 0), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002366 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
2367 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
2368 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
Devang Patel83580992008-12-12 21:57:54 +00002369 if (!UnitDesc->getDirectory().empty())
2370 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002371
Jim Laskeyef42a012006-11-02 20:12:39 +00002372 // Construct compile unit.
2373 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002374
Jim Laskeyef42a012006-11-02 20:12:39 +00002375 // Add Unit to compile unit map.
2376 DescToUnitMap[UnitDesc] = Unit;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002377
Jim Laskeyef42a012006-11-02 20:12:39 +00002378 return Unit;
2379 }
2380
Jim Laskey9d4209f2006-11-07 19:33:46 +00002381 /// GetBaseCompileUnit - Get the main compile unit.
2382 ///
2383 CompileUnit *GetBaseCompileUnit() const {
2384 CompileUnit *Unit = CompileUnits[0];
2385 assert(Unit && "Missing compile unit.");
2386 return Unit;
2387 }
2388
Jim Laskey65195462006-10-30 13:35:07 +00002389 /// FindCompileUnit - Get the compile unit for the given descriptor.
2390 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002391 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002392 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00002393 assert(Unit && "Missing compile unit.");
2394 return Unit;
2395 }
2396
Devang Patel8526cc02009-01-05 22:35:52 +00002397 /// FindCompileUnit - Get the compile unit for the given descriptor.
2398 ///
2399 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
2400 CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
2401 assert(DW_Unit && "Missing compile unit.");
2402 return DW_Unit;
2403 }
2404
Jim Laskeyef42a012006-11-02 20:12:39 +00002405 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00002406 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002407 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
2408 // Get the compile unit context.
2409 CompileUnitDesc *UnitDesc =
2410 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00002411 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002412
2413 // Check for pre-existence.
2414 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
2415 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002416
Jim Laskeyef42a012006-11-02 20:12:39 +00002417 // Get the global variable itself.
2418 GlobalVariable *GV = GVD->getGlobalVariable();
2419
Jim Laskey2172f962006-11-30 14:35:45 +00002420 const std::string &Name = GVD->getName();
2421 const std::string &FullName = GVD->getFullName();
2422 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00002423 // Create the global's variable DIE.
2424 DIE *VariableDie = new DIE(DW_TAG_variable);
2425 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002426 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002427 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00002428 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00002429 }
Jim Laskey6488a072007-01-08 22:15:18 +00002430 AddType(VariableDie, GVD->getType(), Unit);
2431 if (!GVD->isStatic())
2432 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002433
Jim Laskeyef42a012006-11-02 20:12:39 +00002434 // Add source line info if available.
2435 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002436
Jim Laskeyef42a012006-11-02 20:12:39 +00002437 // Add address.
2438 DIEBlock *Block = new DIEBlock();
2439 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00002440 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
2441 AddBlock(VariableDie, DW_AT_location, 0, Block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002442
Jim Laskeyef42a012006-11-02 20:12:39 +00002443 // Add to map.
2444 Slot = VariableDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002445
Jim Laskeyef42a012006-11-02 20:12:39 +00002446 // Add to context owner.
2447 Unit->getDie()->AddChild(VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002448
Jim Laskeyef42a012006-11-02 20:12:39 +00002449 // Expose as global.
2450 // FIXME - need to check external flag.
Jim Laskey2172f962006-11-30 14:35:45 +00002451 Unit->AddGlobal(FullName, VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002452
Jim Laskeyef42a012006-11-02 20:12:39 +00002453 return VariableDie;
2454 }
Jim Laskey65195462006-10-30 13:35:07 +00002455
2456 /// NewSubprogram - Add a new subprogram DIE.
2457 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002458 DIE *NewSubprogram(SubprogramDesc *SPD) {
2459 // Get the compile unit context.
2460 CompileUnitDesc *UnitDesc =
2461 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00002462 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002463
2464 // Check for pre-existence.
2465 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
2466 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002467
Jim Laskeyef42a012006-11-02 20:12:39 +00002468 // Gather the details (simplify add attribute code.)
Jim Laskey2172f962006-11-30 14:35:45 +00002469 const std::string &Name = SPD->getName();
2470 const std::string &FullName = SPD->getFullName();
2471 const std::string &LinkageName = SPD->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002472
Jim Laskeyef42a012006-11-02 20:12:39 +00002473 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
2474 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002475 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002476 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00002477 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00002478 }
2479 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
Jim Laskey6488a072007-01-08 22:15:18 +00002480 if (!SPD->isStatic())
2481 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002482 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002483
Jim Laskeyef42a012006-11-02 20:12:39 +00002484 // Add source line info if available.
2485 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
2486
2487 // Add to map.
2488 Slot = SubprogramDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002489
Jim Laskeyef42a012006-11-02 20:12:39 +00002490 // Add to context owner.
2491 Unit->getDie()->AddChild(SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002492
Jim Laskeyef42a012006-11-02 20:12:39 +00002493 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00002494 Unit->AddGlobal(FullName, SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002495
Jim Laskeyef42a012006-11-02 20:12:39 +00002496 return SubprogramDie;
2497 }
Jim Laskey65195462006-10-30 13:35:07 +00002498
2499 /// NewScopeVariable - Create a new scope variable.
2500 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002501 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
2502 // Get the descriptor.
2503 VariableDesc *VD = DV->getDesc();
2504
2505 // Translate tag to proper Dwarf tag. The result variable is dropped for
2506 // now.
2507 unsigned Tag;
2508 switch (VD->getTag()) {
2509 case DW_TAG_return_variable: return NULL;
2510 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2511 case DW_TAG_auto_variable: // fall thru
2512 default: Tag = DW_TAG_variable; break;
2513 }
2514
2515 // Define variable debug information entry.
2516 DIE *VariableDie = new DIE(Tag);
2517 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2518
2519 // Add source line info if available.
2520 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002521
Jim Laskeyef42a012006-11-02 20:12:39 +00002522 // Add variable type.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002523 AddType(VariableDie, VD->getType(), Unit);
2524
Jim Laskeyef42a012006-11-02 20:12:39 +00002525 // Add variable address.
2526 MachineLocation Location;
Evan Cheng72bebb92008-01-31 03:37:28 +00002527 Location.set(RI->getFrameRegister(*MF),
2528 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Jim Laskeyef42a012006-11-02 20:12:39 +00002529 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002530
Jim Laskeyef42a012006-11-02 20:12:39 +00002531 return VariableDie;
2532 }
Jim Laskey65195462006-10-30 13:35:07 +00002533
Devang Patel7a6e5a32009-01-08 02:33:41 +00002534 /// NewScopeVariable - Create a new scope variable.
2535 ///
2536 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
2537 // Get the descriptor.
2538 DIVariable *VD = DV->getVariable();
2539
2540 // Translate tag to proper Dwarf tag. The result variable is dropped for
2541 // now.
2542 unsigned Tag;
2543 switch (VD->getTag()) {
2544 case DW_TAG_return_variable: return NULL;
2545 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2546 case DW_TAG_auto_variable: // fall thru
2547 default: Tag = DW_TAG_variable; break;
2548 }
2549
2550 // Define variable debug information entry.
2551 DIE *VariableDie = new DIE(Tag);
2552 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2553
2554 // Add source line info if available.
2555 AddSourceLine(VariableDie, VD);
2556
2557 // Add variable type.
2558 AddType(Unit, VariableDie, VD->getType());
2559
2560 // Add variable address.
2561 MachineLocation Location;
2562 Location.set(RI->getFrameRegister(*MF),
2563 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2564 AddAddress(VariableDie, DW_AT_location, Location);
2565
2566 return VariableDie;
2567 }
2568
Devang Patel9f8fcfc2009-01-08 17:19:22 +00002569 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
2570 CompileUnit *Unit = DW_CUs[V];
2571 assert (Unit && "Unable to find CompileUnit");
2572 unsigned ID = NextLabelID();
2573 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
2574 return ID;
2575 }
2576
2577 unsigned getRecordSourceLineCount() {
2578 return Lines.size();
2579 }
2580
2581 unsigned RecordSource(const std::string &Directory,
2582 const std::string &File) {
2583 unsigned DID = Directories.insert(Directory);
2584 return SrcFiles.insert(SrcFileInfo(DID,File));
2585 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002586
Devang Patel2ec78c42009-01-08 02:49:34 +00002587 /// RecordRegionStart - Indicate the start of a region.
2588 ///
2589 unsigned RecordRegionStart(GlobalVariable *V) {
2590 DbgScope *Scope = getOrCreateScope(V);
2591 unsigned ID = NextLabelID();
2592 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
2593 return ID;
2594 }
2595
2596 /// RecordRegionEnd - Indicate the end of a region.
2597 ///
2598 unsigned RecordRegionEnd(GlobalVariable *V) {
2599 DbgScope *Scope = getOrCreateScope(V);
2600 unsigned ID = NextLabelID();
2601 Scope->setEndLabelID(ID);
2602 return ID;
2603 }
2604
2605 /// RecordVariable - Indicate the declaration of a local variable.
2606 ///
2607 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
2608 DbgScope *Scope = getOrCreateScope(GV);
2609 DIVariable *VD = new DIVariable(GV);
2610 DbgVariable *DV = new DbgVariable(VD, FrameIndex);
2611 Scope->AddVariable(DV);
2612 }
2613
Devang Patel7a6e5a32009-01-08 02:33:41 +00002614 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2615 ///
2616 DbgScope *getOrCreateScope(GlobalVariable *V) {
2617 DbgScope *&Slot = DbgScopeMap[V];
2618 if (!Slot) {
2619 // FIXME - breaks down when the context is an inlined function.
2620 DIDescriptor ParentDesc;
Devang Patel0dc969e2009-01-10 02:34:18 +00002621 DIDescriptor *DB = new DIBlock(V);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002622 if (DIBlock *Block = dyn_cast<DIBlock>(DB)) {
2623 ParentDesc = Block->getContext();
2624 }
2625 DbgScope *Parent = ParentDesc.isNull() ?
Devang Patel0dc969e2009-01-10 02:34:18 +00002626 NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002627 Slot = new DbgScope(Parent, DB);
2628 if (Parent) {
2629 Parent->AddScope(Slot);
2630 } else if (RootDbgScope) {
2631 // FIXME - Add inlined function scopes to the root so we can delete
2632 // them later. Long term, handle inlined functions properly.
2633 RootDbgScope->AddScope(Slot);
2634 } else {
2635 // First function is top level function.
2636 RootDbgScope = Slot;
2637 }
2638 }
2639 return Slot;
2640 }
2641
2642 /// ConstructDbgScope - Construct the components of a scope.
2643 ///
2644 void ConstructDbgScope(DbgScope *ParentScope,
2645 unsigned ParentStartID, unsigned ParentEndID,
2646 DIE *ParentDie, CompileUnit *Unit) {
2647 // Add variables to scope.
Devang Patel9795da52009-01-10 02:42:49 +00002648 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002649 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2650 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2651 if (VariableDie) ParentDie->AddChild(VariableDie);
2652 }
2653
2654 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002655 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002656 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2657 // Define the Scope debug information entry.
2658 DbgScope *Scope = Scopes[j];
2659 // FIXME - Ignore inlined functions for the time being.
2660 if (!Scope->getParent()) continue;
2661
2662 unsigned StartID = MappedLabel(Scope->getStartLabelID());
2663 unsigned EndID = MappedLabel(Scope->getEndLabelID());
2664
2665 // Ignore empty scopes.
2666 if (StartID == EndID && StartID != 0) continue;
2667 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2668
2669 if (StartID == ParentStartID && EndID == ParentEndID) {
2670 // Just add stuff to the parent scope.
2671 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2672 } else {
2673 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2674
2675 // Add the scope bounds.
2676 if (StartID) {
2677 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2678 DWLabel("label", StartID));
2679 } else {
2680 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2681 DWLabel("func_begin", SubprogramCount));
2682 }
2683 if (EndID) {
2684 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2685 DWLabel("label", EndID));
2686 } else {
2687 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2688 DWLabel("func_end", SubprogramCount));
2689 }
2690
2691 // Add the scope contents.
2692 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2693 ParentDie->AddChild(ScopeDie);
2694 }
2695 }
2696 }
2697
2698 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2699 ///
2700 void ConstructRootDbgScope(DbgScope *RootScope) {
2701 // Exit if there is no root scope.
2702 if (!RootScope) return;
2703
2704 // Get the subprogram debug information entry.
2705 DISubprogram *SPD = cast<DISubprogram>(RootScope->getDesc());
2706
2707 // Get the compile unit context.
2708 CompileUnit *Unit = FindCompileUnit(SPD->getCompileUnit());
2709
2710 // Get the subprogram die.
2711 DIE *SPDie = Unit->getDieMapSlotFor(SPD->getGV());
2712 assert(SPDie && "Missing subprogram descriptor");
2713
2714 // Add the function bounds.
2715 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2716 DWLabel("func_begin", SubprogramCount));
2717 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2718 DWLabel("func_end", SubprogramCount));
2719 MachineLocation Location(RI->getFrameRegister(*MF));
2720 AddAddress(SPDie, DW_AT_frame_base, Location);
2721
2722 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2723 }
2724
2725 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2726 ///
2727 void ConstructDefaultDbgScope(MachineFunction *MF) {
2728 // Find the correct subprogram descriptor.
2729 std::string SPName = "llvm.dbg.subprograms";
2730 std::vector<GlobalVariable*> Result;
2731 getGlobalVariablesUsing(*M, SPName, Result);
2732 for (std::vector<GlobalVariable *>::iterator I = Result.begin(),
2733 E = Result.end(); I != E; ++I) {
2734
2735 DISubprogram *SPD = new DISubprogram(*I);
2736
2737 if (SPD->getName() == MF->getFunction()->getName()) {
2738 // Get the compile unit context.
2739 CompileUnit *Unit = FindCompileUnit(SPD->getCompileUnit());
2740
2741 // Get the subprogram die.
2742 DIE *SPDie = Unit->getDieMapSlotFor(SPD->getGV());
2743 assert(SPDie && "Missing subprogram descriptor");
2744
2745 // Add the function bounds.
2746 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2747 DWLabel("func_begin", SubprogramCount));
2748 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2749 DWLabel("func_end", SubprogramCount));
2750
2751 MachineLocation Location(RI->getFrameRegister(*MF));
2752 AddAddress(SPDie, DW_AT_frame_base, Location);
2753 return;
2754 }
2755 }
2756#if 0
2757 // FIXME: This is causing an abort because C++ mangled names are compared
2758 // with their unmangled counterparts. See PR2885. Don't do this assert.
2759 assert(0 && "Couldn't find DIE for machine function!");
2760#endif
2761 }
2762
Jim Laskey65195462006-10-30 13:35:07 +00002763 /// ConstructScope - Construct the components of a scope.
2764 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002765 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00002766 unsigned ParentStartID, unsigned ParentEndID,
2767 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002768 // Add variables to scope.
2769 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
2770 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2771 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
2772 if (VariableDie) ParentDie->AddChild(VariableDie);
2773 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002774
Jim Laskeyef42a012006-11-02 20:12:39 +00002775 // Add nested scopes.
2776 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
2777 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2778 // Define the Scope debug information entry.
2779 DebugScope *Scope = Scopes[j];
2780 // FIXME - Ignore inlined functions for the time being.
2781 if (!Scope->getParent()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002782
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002783 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2784 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00002785
Jim Laskey9d4209f2006-11-07 19:33:46 +00002786 // Ignore empty scopes.
2787 if (StartID == EndID && StartID != 0) continue;
2788 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002789
Jim Laskey36729dd2006-11-29 16:55:57 +00002790 if (StartID == ParentStartID && EndID == ParentEndID) {
2791 // Just add stuff to the parent scope.
2792 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002793 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00002794 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002795
Jim Laskey36729dd2006-11-29 16:55:57 +00002796 // Add the scope bounds.
2797 if (StartID) {
2798 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00002799 DWLabel("label", StartID));
Jim Laskey36729dd2006-11-29 16:55:57 +00002800 } else {
2801 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2802 DWLabel("func_begin", SubprogramCount));
2803 }
2804 if (EndID) {
2805 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00002806 DWLabel("label", EndID));
Jim Laskey36729dd2006-11-29 16:55:57 +00002807 } else {
2808 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2809 DWLabel("func_end", SubprogramCount));
2810 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002811
Jim Laskey36729dd2006-11-29 16:55:57 +00002812 // Add the scope contents.
2813 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
2814 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00002815 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002816 }
2817 }
Jim Laskey65195462006-10-30 13:35:07 +00002818
2819 /// ConstructRootScope - Construct the scope for the subprogram.
2820 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002821 void ConstructRootScope(DebugScope *RootScope) {
2822 // Exit if there is no root scope.
2823 if (!RootScope) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002824
2825 // Get the subprogram debug information entry.
Jim Laskeyef42a012006-11-02 20:12:39 +00002826 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002827
Jim Laskeyef42a012006-11-02 20:12:39 +00002828 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00002829 CompileUnit *Unit = GetBaseCompileUnit();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002830
Jim Laskeyef42a012006-11-02 20:12:39 +00002831 // Get the subprogram die.
2832 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2833 assert(SPDie && "Missing subprogram descriptor");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002834
Jim Laskeyef42a012006-11-02 20:12:39 +00002835 // Add the function bounds.
2836 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2837 DWLabel("func_begin", SubprogramCount));
2838 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2839 DWLabel("func_end", SubprogramCount));
2840 MachineLocation Location(RI->getFrameRegister(*MF));
2841 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002842
Jim Laskey36729dd2006-11-29 16:55:57 +00002843 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002844 }
Jim Laskey65195462006-10-30 13:35:07 +00002845
Bill Wendlingd751c642008-09-26 00:28:12 +00002846 /// ConstructDefaultScope - Construct a default scope for the subprogram.
2847 ///
2848 void ConstructDefaultScope(MachineFunction *MF) {
2849 // Find the correct subprogram descriptor.
2850 std::vector<SubprogramDesc *> Subprograms;
2851 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
2852
2853 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2854 SubprogramDesc *SPD = Subprograms[i];
2855
2856 if (SPD->getName() == MF->getFunction()->getName()) {
2857 // Get the compile unit context.
2858 CompileUnit *Unit = GetBaseCompileUnit();
2859
2860 // Get the subprogram die.
2861 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2862 assert(SPDie && "Missing subprogram descriptor");
2863
2864 // Add the function bounds.
2865 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2866 DWLabel("func_begin", SubprogramCount));
2867 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2868 DWLabel("func_end", SubprogramCount));
2869
2870 MachineLocation Location(RI->getFrameRegister(*MF));
2871 AddAddress(SPDie, DW_AT_frame_base, Location);
2872 return;
2873 }
2874 }
Bill Wendlinga9f0cc42008-10-17 18:48:57 +00002875#if 0
2876 // FIXME: This is causing an abort because C++ mangled names are compared
2877 // with their unmangled counterparts. See PR2885. Don't do this assert.
Bill Wendlingd751c642008-09-26 00:28:12 +00002878 assert(0 && "Couldn't find DIE for machine function!");
Bill Wendlinga9f0cc42008-10-17 18:48:57 +00002879#endif
Bill Wendlingd751c642008-09-26 00:28:12 +00002880 }
2881
Jim Laskeyef42a012006-11-02 20:12:39 +00002882 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2883 /// tools to recognize the object file contains Dwarf information.
2884 void EmitInitial() {
2885 // Check to see if we already emitted intial headers.
2886 if (didInitial) return;
2887 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002888
Jim Laskeyef42a012006-11-02 20:12:39 +00002889 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002890 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002891 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002892 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002893 }
2894 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2895 EmitLabel("section_info", 0);
2896 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2897 EmitLabel("section_abbrev", 0);
2898 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2899 EmitLabel("section_aranges", 0);
2900 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2901 EmitLabel("section_macinfo", 0);
2902 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2903 EmitLabel("section_line", 0);
2904 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2905 EmitLabel("section_loc", 0);
2906 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2907 EmitLabel("section_pubnames", 0);
2908 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2909 EmitLabel("section_str", 0);
2910 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2911 EmitLabel("section_ranges", 0);
2912
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002913 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002914 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002915 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002916 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002917 }
2918
Jim Laskey65195462006-10-30 13:35:07 +00002919 /// EmitDIE - Recusively Emits a debug information entry.
2920 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002921 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002922 // Get the abbreviation for this DIE.
2923 unsigned AbbrevNumber = Die->getAbbrevNumber();
2924 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002925
Jim Laskeybacd3042007-02-21 22:48:45 +00002926 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002927
2928 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002929 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002930
2931 if (VerboseAsm)
2932 Asm->EOL(std::string("Abbrev [" +
2933 utostr(AbbrevNumber) +
2934 "] 0x" + utohexstr(Die->getOffset()) +
2935 ":0x" + utohexstr(Die->getSize()) + " " +
2936 TagString(Abbrev->getTag())));
2937 else
2938 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002939
Owen Anderson873e1b52008-06-24 21:44:59 +00002940 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2941 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002942
Jim Laskeyef42a012006-11-02 20:12:39 +00002943 // Emit the DIE attribute values.
2944 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2945 unsigned Attr = AbbrevData[i].getAttribute();
2946 unsigned Form = AbbrevData[i].getForm();
2947 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002948
Jim Laskeyef42a012006-11-02 20:12:39 +00002949 switch (Attr) {
2950 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002951 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002952 break;
2953 }
2954 default: {
2955 // Emit an attribute using the defined form.
2956 Values[i]->EmitValue(*this, Form);
2957 break;
2958 }
2959 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002960
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002961 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002962 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002963
Jim Laskeyef42a012006-11-02 20:12:39 +00002964 // Emit the DIE children if any.
2965 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2966 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002967
Jim Laskeyef42a012006-11-02 20:12:39 +00002968 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2969 EmitDIE(Children[j]);
2970 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002971
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002972 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002973 }
2974 }
2975
Jim Laskey65195462006-10-30 13:35:07 +00002976 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2977 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002978 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2979 // Get the children.
2980 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002981
Jim Laskeyef42a012006-11-02 20:12:39 +00002982 // If not last sibling and has children then add sibling offset attribute.
2983 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2984
2985 // Record the abbreviation.
2986 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002987
Jim Laskeyef42a012006-11-02 20:12:39 +00002988 // Get the abbreviation for this DIE.
2989 unsigned AbbrevNumber = Die->getAbbrevNumber();
2990 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2991
2992 // Set DIE offset
2993 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002994
Jim Laskeyef42a012006-11-02 20:12:39 +00002995 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002996 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2997
Owen Anderson873e1b52008-06-24 21:44:59 +00002998 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2999 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00003000
3001 // Size the DIE attribute values.
3002 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3003 // Size attribute value.
3004 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
3005 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003006
Jim Laskeyef42a012006-11-02 20:12:39 +00003007 // Size the DIE children if any.
3008 if (!Children.empty()) {
3009 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
3010 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003011
Jim Laskeyef42a012006-11-02 20:12:39 +00003012 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3013 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
3014 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003015
Jim Laskeyef42a012006-11-02 20:12:39 +00003016 // End of children marker.
3017 Offset += sizeof(int8_t);
3018 }
3019
3020 Die->setSize(Offset - Die->getOffset());
3021 return Offset;
3022 }
Jim Laskey65195462006-10-30 13:35:07 +00003023
3024 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
3025 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003026 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00003027 // Process base compile unit.
3028 CompileUnit *Unit = GetBaseCompileUnit();
3029 // Compute size of compile unit header
3030 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
3031 sizeof(int16_t) + // DWARF version number
3032 sizeof(int32_t) + // Offset Into Abbrev. Section
3033 sizeof(int8_t); // Pointer Size (in bytes)
3034 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00003035 }
3036
Jim Laskey65195462006-10-30 13:35:07 +00003037 /// EmitDebugInfo - Emit the debug info section.
3038 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003039 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003040 // Start debug info section.
3041 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003042
Jim Laskey5496f012006-11-09 14:52:14 +00003043 CompileUnit *Unit = GetBaseCompileUnit();
3044 DIE *Die = Unit->getDie();
3045 // Emit the compile units header.
3046 EmitLabel("info_begin", Unit->getID());
3047 // Emit size of content not including length itself
3048 unsigned ContentSize = Die->getSize() +
3049 sizeof(int16_t) + // DWARF version number
3050 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00003051 sizeof(int8_t) + // Pointer Size (in bytes)
3052 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003053
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003054 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
3055 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00003056 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003057 Asm->EOL("Offset Into Abbrev. Section");
Dan Gohman82482942007-09-27 23:12:31 +00003058 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003059
Jim Laskey5496f012006-11-09 14:52:14 +00003060 EmitDIE(Die);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003061 // FIXME - extra padding for gdb bug.
3062 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3063 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3064 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3065 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Jim Laskey5496f012006-11-09 14:52:14 +00003066 EmitLabel("info_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003067
Jim Laskeybacd3042007-02-21 22:48:45 +00003068 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003069 }
3070
Jim Laskey65195462006-10-30 13:35:07 +00003071 /// EmitAbbreviations - Emit the abbreviation section.
3072 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003073 void EmitAbbreviations() const {
3074 // Check to see if it is worth the effort.
3075 if (!Abbreviations.empty()) {
3076 // Start the debug abbrev section.
3077 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003078
Jim Laskeyef42a012006-11-02 20:12:39 +00003079 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003080
Jim Laskeyef42a012006-11-02 20:12:39 +00003081 // For each abbrevation.
3082 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3083 // Get abbreviation data
3084 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003085
Jim Laskeyef42a012006-11-02 20:12:39 +00003086 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003087 Asm->EmitULEB128Bytes(Abbrev->getNumber());
3088 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003089
Jim Laskeyef42a012006-11-02 20:12:39 +00003090 // Emit the abbreviations data.
3091 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003092
Jim Laskeybacd3042007-02-21 22:48:45 +00003093 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003094 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003095
Jim Laskey7b1b39d2007-02-22 18:22:42 +00003096 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00003097 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00003098
Jim Laskeyef42a012006-11-02 20:12:39 +00003099 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003100
Jim Laskeybacd3042007-02-21 22:48:45 +00003101 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003102 }
3103 }
3104
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003105 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
3106 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003107 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003108 void EmitEndOfLineMatrix(unsigned SectionEnd) {
3109 // Define last address of section.
3110 Asm->EmitInt8(0); Asm->EOL("Extended Op");
3111 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
3112 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
3113 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
3114
3115 // Mark end of matrix.
3116 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
3117 Asm->EmitULEB128Bytes(1); Asm->EOL();
3118 Asm->EmitInt8(1); Asm->EOL();
3119 }
3120
Jim Laskey65195462006-10-30 13:35:07 +00003121 /// EmitDebugLines - Emit source line information.
3122 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003123 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003124 // If the target is using .loc/.file, the assembler will be emitting the
3125 // .debug_line table automatically.
3126 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00003127 return;
3128
Jim Laskeyef42a012006-11-02 20:12:39 +00003129 // Minimum line delta, thus ranging from -10..(255-10).
3130 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
3131 // Maximum line delta, thus ranging from -10..(255-10).
3132 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00003133
Jim Laskeyef42a012006-11-02 20:12:39 +00003134 // Start the dwarf line section.
3135 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003136
Jim Laskeyef42a012006-11-02 20:12:39 +00003137 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003138
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003139 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003140 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00003141 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003142
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003143 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003144
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003145 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003146 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00003147 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003148
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003149 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00003150
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003151 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00003152
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003153 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003154
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003155 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00003156
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003157 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003158
Jim Laskeyef42a012006-11-02 20:12:39 +00003159 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003160 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
3161 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
3162 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
3163 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
3164 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
3165 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
3166 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
3167 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
3168 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00003169
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003170 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Evan Cheng6547e402008-07-01 23:18:29 +00003171 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
Jim Laskeyef42a012006-11-02 20:12:39 +00003172
3173 // Emit directories.
3174 for (unsigned DirectoryID = 1, NDID = Directories.size();
3175 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003176 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00003177 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003178 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003179
Jim Laskeyef42a012006-11-02 20:12:39 +00003180 // Emit files.
3181 for (unsigned SourceID = 1, NSID = SourceFiles.size();
3182 SourceID <= NSID; ++SourceID) {
3183 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003184 Asm->EmitString(SourceFile.getName());
3185 Asm->EOL("Source");
3186 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
3187 Asm->EOL("Directory #");
3188 Asm->EmitULEB128Bytes(0);
3189 Asm->EOL("Mod date");
3190 Asm->EmitULEB128Bytes(0);
3191 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00003192 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003193 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003194
Jim Laskeyef42a012006-11-02 20:12:39 +00003195 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003196
Jim Laskeyef42a012006-11-02 20:12:39 +00003197 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003198 unsigned SecSrcLinesSize = SectionSourceLines.size();
3199
3200 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003201 // Isolate current sections line info.
3202 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00003203
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003204 if (VerboseAsm) {
3205 const Section* S = SectionMap[j + 1];
3206 Asm->EOL(std::string("Section ") + S->getName());
3207 } else
Evan Cheng6547e402008-07-01 23:18:29 +00003208 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003209
3210 // Dwarf assumes we start with first line of first source file.
3211 unsigned Source = 1;
3212 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003213
Jim Laskeyef42a012006-11-02 20:12:39 +00003214 // Construct rows of the address, source, line, column matrix.
3215 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3216 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003217 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00003218 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003219
Jim Laskey1a4a83c2007-01-25 15:45:58 +00003220 unsigned SourceID = LineInfo.getSourceID();
3221 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
3222 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng6547e402008-07-01 23:18:29 +00003223 if (VerboseAsm)
3224 Asm->EOL(Directories[DirectoryID]
3225 + SourceFile.getName()
3226 + ":"
3227 + utostr_32(LineInfo.getLine()));
3228 else
3229 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003230
3231 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003232 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00003233 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003234 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00003235 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003236
Jim Laskeyef42a012006-11-02 20:12:39 +00003237 // If change of source, then switch to the new source.
3238 if (Source != LineInfo.getSourceID()) {
3239 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003240 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
3241 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00003242 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003243
Jim Laskeyef42a012006-11-02 20:12:39 +00003244 // If change of line.
3245 if (Line != LineInfo.getLine()) {
3246 // Determine offset.
3247 int Offset = LineInfo.getLine() - Line;
3248 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003249
Jim Laskeyef42a012006-11-02 20:12:39 +00003250 // Update line.
3251 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003252
Jim Laskeyef42a012006-11-02 20:12:39 +00003253 // If delta is small enough and in range...
3254 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3255 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003256 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00003257 } else {
3258 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003259 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
3260 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
3261 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00003262 }
3263 } else {
3264 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003265 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00003266 }
3267 }
3268
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003269 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00003270 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003271
3272 if (SecSrcLinesSize == 0)
3273 // Because we're emitting a debug_line section, we still need a line
3274 // table. The linker and friends expect it to exist. If there's nothing to
3275 // put into it, emit an empty table.
3276 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003277
Jim Laskeyef42a012006-11-02 20:12:39 +00003278 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003279
Jim Laskeybacd3042007-02-21 22:48:45 +00003280 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003281 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003282
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003283 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00003284 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003285 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00003286 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00003287 return;
3288
3289 int stackGrowth =
3290 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3291 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003292 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00003293
3294 // Start the dwarf frame section.
3295 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
3296
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003297 EmitLabel("debug_frame_common", 0);
3298 EmitDifference("debug_frame_common_end", 0,
3299 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003300 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00003301
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003302 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003303 Asm->EmitInt32((int)DW_CIE_ID);
3304 Asm->EOL("CIE Identifier Tag");
3305 Asm->EmitInt8(DW_CIE_VERSION);
3306 Asm->EOL("CIE Version");
3307 Asm->EmitString("");
3308 Asm->EOL("CIE Augmentation");
3309 Asm->EmitULEB128Bytes(1);
3310 Asm->EOL("CIE Code Alignment Factor");
3311 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003312 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003313 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003314 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003315
Jim Laskey5e73d5b2007-01-24 18:45:13 +00003316 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00003317 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003318
Dale Johannesenb97aec62007-11-13 19:13:01 +00003319 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00003320
Evan Cheng05548eb2008-02-29 19:36:59 +00003321 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003322 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003323
Jim Laskeybacd3042007-02-21 22:48:45 +00003324 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003325 }
3326
Jim Laskey65195462006-10-30 13:35:07 +00003327 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
3328 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003329 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00003330 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00003331 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003332
Jim Laskeyef42a012006-11-02 20:12:39 +00003333 // Start the dwarf frame section.
3334 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003335
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003336 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
3337 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003338 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003339
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003340 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003341
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003342 EmitSectionOffset("debug_frame_common", "section_debug_frame",
3343 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003344 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00003345
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003346 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003347 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003348 EmitDifference("func_end", DebugFrameInfo.Number,
3349 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003350 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003351
Dale Johannesenb97aec62007-11-13 19:13:01 +00003352 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003353
Evan Cheng05548eb2008-02-29 19:36:59 +00003354 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003355 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00003356
Jim Laskeybacd3042007-02-21 22:48:45 +00003357 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003358 }
3359
3360 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00003361 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003362 void EmitDebugPubNames() {
3363 // Start the dwarf pubnames section.
3364 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003365
3366 CompileUnit *Unit = GetBaseCompileUnit();
3367
Jim Laskey5496f012006-11-09 14:52:14 +00003368 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003369 "pubnames_begin", Unit->getID(), true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003370 Asm->EOL("Length of Public Names Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003371
Jim Laskey5496f012006-11-09 14:52:14 +00003372 EmitLabel("pubnames_begin", Unit->getID());
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 Korobeynikova6199c82007-03-07 02:47:57 +00003375
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00003376 EmitSectionOffset("info_begin", "section_info",
3377 Unit->getID(), 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003378 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00003379
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003380 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003381 Asm->EOL("Compilation Unit Length");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003382
Jim Laskey5496f012006-11-09 14:52:14 +00003383 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003384
Jim Laskey5496f012006-11-09 14:52:14 +00003385 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
3386 GE = Globals.end();
3387 GI != GE; ++GI) {
3388 const std::string &Name = GI->first;
3389 DIE * Entity = GI->second;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003390
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003391 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
3392 Asm->EmitString(Name); Asm->EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00003393 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003394
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003395 Asm->EmitInt32(0); Asm->EOL("End Mark");
Jim Laskey5496f012006-11-09 14:52:14 +00003396 EmitLabel("pubnames_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003397
Jim Laskeybacd3042007-02-21 22:48:45 +00003398 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003399 }
3400
3401 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00003402 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003403 void EmitDebugStr() {
3404 // Check to see if it is worth the effort.
3405 if (!StringPool.empty()) {
3406 // Start the dwarf str section.
3407 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003408
Jim Laskeyef42a012006-11-02 20:12:39 +00003409 // For each of strings in the string pool.
3410 for (unsigned StringID = 1, N = StringPool.size();
3411 StringID <= N; ++StringID) {
3412 // Emit a label for reference from debug information entries.
3413 EmitLabel("string", StringID);
3414 // Emit the string itself.
3415 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00003416 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003417 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003418
Jim Laskeybacd3042007-02-21 22:48:45 +00003419 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003420 }
3421 }
3422
3423 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00003424 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003425 void EmitDebugLoc() {
3426 // Start the dwarf loc section.
3427 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003428
Jim Laskeybacd3042007-02-21 22:48:45 +00003429 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003430 }
3431
3432 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00003433 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003434 void EmitDebugARanges() {
3435 // Start the dwarf aranges section.
3436 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003437
Jim Laskeyef42a012006-11-02 20:12:39 +00003438 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00003439#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003440 CompileUnit *Unit = GetBaseCompileUnit();
3441
Jim Laskey5496f012006-11-09 14:52:14 +00003442 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003443 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003444
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003445 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003446
Jim Laskey5496f012006-11-09 14:52:14 +00003447 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003448 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00003449
Dan Gohman82482942007-09-27 23:12:31 +00003450 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00003451
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003452 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00003453
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003454 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
3455 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00003456
Jim Laskey5496f012006-11-09 14:52:14 +00003457 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003458 EmitReference("text_begin", 0); Asm->EOL("Address");
3459 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00003460
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003461 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
3462 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00003463#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003464
Jim Laskeybacd3042007-02-21 22:48:45 +00003465 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003466 }
3467
3468 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00003469 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003470 void EmitDebugRanges() {
3471 // Start the dwarf ranges section.
3472 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003473
Jim Laskeybacd3042007-02-21 22:48:45 +00003474 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003475 }
3476
3477 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00003478 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003479 void EmitDebugMacInfo() {
3480 // Start the dwarf macinfo section.
3481 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003482
Jim Laskeybacd3042007-02-21 22:48:45 +00003483 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003484 }
3485
Devang Patelc4523242009-01-05 23:11:11 +00003486 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00003487 void ConstructCompileUnits() {
3488 std::string CUName = "llvm.dbg.compile_units";
3489 std::vector<GlobalVariable*> Result;
3490 getGlobalVariablesUsing(*M, CUName, Result);
3491 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3492 RE = Result.end(); RI != RE; ++RI) {
3493 DICompileUnit *DIUnit = new DICompileUnit(*RI);
Devang Patel9f8fcfc2009-01-08 17:19:22 +00003494 unsigned ID = RecordSource(DIUnit->getDirectory(),
3495 DIUnit->getFilename());
Devang Pateld1ca9252009-01-05 23:03:32 +00003496
3497 DIE *Die = new DIE(DW_TAG_compile_unit);
3498 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
3499 DWLabel("section_line", 0), DWLabel("section_line", 0),
3500 false);
3501 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit->getProducer());
3502 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit->getLanguage());
3503 AddString(Die, DW_AT_name, DW_FORM_string, DIUnit->getFilename());
3504 if (!DIUnit->getDirectory().empty())
3505 AddString(Die, DW_AT_comp_dir, DW_FORM_string, DIUnit->getDirectory());
3506
3507 CompileUnit *Unit = new CompileUnit(ID, Die);
3508 DW_CUs[DIUnit->getGV()] = Unit;
3509 }
3510 }
3511
Jim Laskey65195462006-10-30 13:35:07 +00003512 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
3513 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00003514 void ConstructCompileUnitDIEs() {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003515 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003516
Jim Laskeyef42a012006-11-02 20:12:39 +00003517 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003518 unsigned ID = MMI->RecordSource(CUW[i]);
Jim Laskey9d4209f2006-11-07 19:33:46 +00003519 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00003520 CompileUnits.push_back(Unit);
3521 }
3522 }
3523
Devang Patelc4523242009-01-05 23:11:11 +00003524 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
3525 /// visible global variables.
3526 void ConstructGlobalVariableDIEs() {
3527 std::string GVName = "llvm.dbg.global_variables";
3528 std::vector<GlobalVariable*> Result;
3529 getGlobalVariablesUsing(*M, GVName, Result);
3530 for (std::vector<GlobalVariable *>::iterator GVI = Result.begin(),
3531 GVE = Result.end(); GVI != GVE; ++GVI) {
3532 DIGlobalVariable *DI_GV = new DIGlobalVariable(*GVI);
3533 CompileUnit *DW_Unit = FindCompileUnit(DI_GV->getCompileUnit());
3534
3535 // Check for pre-existence.
3536 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV->getGV());
3537 if (Slot) continue;
3538
3539 DIE *VariableDie = new DIE(DW_TAG_variable);
3540 AddString(VariableDie, DW_AT_name, DW_FORM_string, DI_GV->getName());
3541 const std::string &LinkageName = DI_GV->getLinkageName();
3542 if (!LinkageName.empty())
3543 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3544 LinkageName);
3545 AddType(DW_Unit, VariableDie, DI_GV->getType());
3546
3547 if (!DI_GV->isLocalToUnit())
3548 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
3549
3550 // Add source line info, if available.
3551 AddSourceLine(VariableDie, DI_GV);
3552
3553 // Add address.
3554 DIEBlock *Block = new DIEBlock();
3555 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
3556 AddObjectLabel(Block, 0, DW_FORM_udata,
3557 Asm->getGlobalLinkName(DI_GV->getGV()));
3558 AddBlock(VariableDie, DW_AT_location, 0, Block);
3559
3560 //Add to map.
3561 Slot = VariableDie;
3562
3563 //Add to context owner.
3564 DW_Unit->getDie()->AddChild(VariableDie);
3565
3566 //Expose as global. FIXME - need to check external flag.
3567 DW_Unit->AddGlobal(DI_GV->getName(), VariableDie);
3568 }
3569 }
3570
Jim Laskey65195462006-10-30 13:35:07 +00003571 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
3572 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00003573 void ConstructGlobalDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00003574 std::vector<GlobalVariableDesc *> GlobalVariables;
3575 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M, GlobalVariables);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003576
Bill Wendling10fff602008-07-03 22:53:42 +00003577 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
3578 GlobalVariableDesc *GVD = GlobalVariables[i];
3579 NewGlobalVariable(GVD);
3580 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003581 }
Jim Laskey65195462006-10-30 13:35:07 +00003582
Devang Patel78eb6ad2009-01-05 23:21:35 +00003583 /// ConstructSubprograms - Create DIEs for each of the externally visible
3584 /// subprograms.
3585 void ConstructSubprograms() {
3586
3587 std::string SPName = "llvm.dbg.subprograms";
3588 std::vector<GlobalVariable*> Result;
3589 getGlobalVariablesUsing(*M, SPName, Result);
3590 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3591 RE = Result.end(); RI != RE; ++RI) {
3592
3593 DISubprogram *SP = new DISubprogram(*RI);
3594 CompileUnit *Unit = FindCompileUnit(SP->getCompileUnit());
3595
3596 // Check for pre-existence.
3597 DIE *&Slot = Unit->getDieMapSlotFor(SP->getGV());
3598 if (Slot) continue;
3599
3600 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
3601 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, SP->getName());
3602 const std::string &LinkageName = SP->getLinkageName();
3603 if (!LinkageName.empty())
3604 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3605 LinkageName);
3606 DIType SPTy = SP->getType();
3607 AddType(Unit, SubprogramDie, SPTy);
3608 if (!SP->isLocalToUnit())
3609 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
3610 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
3611
3612 AddSourceLine(SubprogramDie, SP);
3613 //Add to map.
3614 Slot = SubprogramDie;
3615 //Add to context owner.
3616 Unit->getDie()->AddChild(SubprogramDie);
3617 //Expose as global.
3618 Unit->AddGlobal(SP->getName(), SubprogramDie);
3619 }
3620 }
3621
Jim Laskey65195462006-10-30 13:35:07 +00003622 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
3623 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00003624 void ConstructSubprogramDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00003625 std::vector<SubprogramDesc *> Subprograms;
3626 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003627
Bill Wendling10fff602008-07-03 22:53:42 +00003628 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
3629 SubprogramDesc *SPD = Subprograms[i];
3630 NewSubprogram(SPD);
3631 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003632 }
Jim Laskey65195462006-10-30 13:35:07 +00003633
Jim Laskey65195462006-10-30 13:35:07 +00003634public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003635 //===--------------------------------------------------------------------===//
3636 // Main entry points.
3637 //
Owen Andersoncb371882008-08-21 00:14:44 +00003638 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00003639 : Dwarf(OS, A, T, "dbg")
Jim Laskeyef42a012006-11-02 20:12:39 +00003640 , CompileUnits()
3641 , AbbreviationsSet(InitAbbreviationsSetSize)
3642 , Abbreviations()
3643 , ValuesSet(InitValuesSetSize)
3644 , Values()
3645 , StringPool()
3646 , DescToUnitMap()
3647 , SectionMap()
3648 , SectionSourceLines()
Jim Laskeybacd3042007-02-21 22:48:45 +00003649 , didInitial(false)
3650 , shouldEmit(false)
Devang Patel7a6e5a32009-01-08 02:33:41 +00003651 , RootDbgScope(NULL)
Jim Laskeyef42a012006-11-02 20:12:39 +00003652 {
3653 }
Jim Laskey072200c2007-01-29 18:51:14 +00003654 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003655 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
3656 delete CompileUnits[i];
3657 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3658 delete Values[j];
3659 }
3660
Devang Patel5d315982009-01-06 21:07:30 +00003661 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3662 /// This is inovked by the target AsmPrinter.
3663 void SetDebugInfo() {
3664 // FIXME - Check if the module has debug info or not.
3665 // Create all the compile unit DIEs.
3666 ConstructCompileUnits();
3667
3668 // Create DIEs for each of the externally visible global variables.
3669 ConstructGlobalVariableDIEs();
3670
3671 // Create DIEs for each of the externally visible subprograms.
3672 ConstructSubprograms();
3673
3674 // Prime section data.
3675 SectionMap.insert(TAI->getTextSection());
3676
3677 // Print out .file directives to specify files for .loc directives. These
3678 // are printed out early so that they precede any .loc directives.
3679 if (TAI->hasDotLocAndDotFile()) {
3680 for (unsigned i = 1, e = SrcFiles.size(); i <= e; ++i) {
3681 sys::Path FullPath(Directories[SrcFiles[i].getDirectoryID()]);
3682 bool AppendOk = FullPath.appendComponent(SrcFiles[i].getName());
3683 assert(AppendOk && "Could not append filename to directory!");
3684 AppendOk = false;
3685 Asm->EmitFile(i, FullPath.toString());
3686 Asm->EOL();
3687 }
3688 }
3689
3690 // Emit initial sections
3691 EmitInitial();
3692 }
3693
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003694 /// SetModuleInfo - Set machine module information when it's known that pass
3695 /// manager has created it. Set by the target AsmPrinter.
3696 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003697 // Make sure initial declarations are made.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003698 if (!MMI && mmi->hasDebugInfo()) {
3699 MMI = mmi;
Jim Laskeyef42a012006-11-02 20:12:39 +00003700 shouldEmit = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003701
Jim Laskeyef42a012006-11-02 20:12:39 +00003702 // Create all the compile unit DIEs.
3703 ConstructCompileUnitDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003704
Jim Laskeyef42a012006-11-02 20:12:39 +00003705 // Create DIEs for each of the externally visible global variables.
3706 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00003707
Jim Laskeyef42a012006-11-02 20:12:39 +00003708 // Create DIEs for each of the externally visible subprograms.
3709 ConstructSubprogramDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003710
Jim Laskeyef42a012006-11-02 20:12:39 +00003711 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00003712 SectionMap.insert(TAI->getTextSection());
Dan Gohmand57c3882007-10-01 22:40:20 +00003713
3714 // Print out .file directives to specify files for .loc directives. These
3715 // are printed out early so that they precede any .loc directives.
3716 if (TAI->hasDotLocAndDotFile()) {
3717 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
3718 const UniqueVector<std::string> &Directories = MMI->getDirectories();
3719 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
3720 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
3721 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
3722 assert(AppendOk && "Could not append filename to directory!");
Devang Pateld0935c32008-12-23 21:55:38 +00003723 AppendOk = false;
Dan Gohmand57c3882007-10-01 22:40:20 +00003724 Asm->EmitFile(i, FullPath.toString());
3725 Asm->EOL();
3726 }
3727 }
3728
3729 // Emit initial sections
3730 EmitInitial();
Jim Laskeyef42a012006-11-02 20:12:39 +00003731 }
3732 }
3733
Jim Laskey65195462006-10-30 13:35:07 +00003734 /// BeginModule - Emit all Dwarf sections that should come prior to the
3735 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003736 void BeginModule(Module *M) {
3737 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003738 }
3739
Jim Laskey65195462006-10-30 13:35:07 +00003740 /// EndModule - Emit all Dwarf sections that should come after the content.
3741 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003742 void EndModule() {
3743 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003744
Jim Laskeyef42a012006-11-02 20:12:39 +00003745 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003746 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003747 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003748 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003749 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003750
Jim Laskeyef42a012006-11-02 20:12:39 +00003751 // End text sections.
3752 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003753 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003754 EmitLabel("section_end", i);
3755 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003756
3757 // Emit common frame information.
3758 EmitCommonDebugFrame();
3759
3760 // Emit function debug frame information
3761 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3762 E = DebugFrames.end(); I != E; ++I)
3763 EmitFunctionDebugFrame(*I);
3764
Jim Laskeyef42a012006-11-02 20:12:39 +00003765 // Compute DIE offsets and sizes.
3766 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003767
Jim Laskeyef42a012006-11-02 20:12:39 +00003768 // Emit all the DIEs into a debug info section
3769 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003770
Jim Laskeyef42a012006-11-02 20:12:39 +00003771 // Corresponding abbreviations into a abbrev section.
3772 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003773
Jim Laskeyef42a012006-11-02 20:12:39 +00003774 // Emit source line correspondence into a debug line section.
3775 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003776
Jim Laskeyef42a012006-11-02 20:12:39 +00003777 // Emit info into a debug pubnames section.
3778 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003779
Jim Laskeyef42a012006-11-02 20:12:39 +00003780 // Emit info into a debug str section.
3781 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003782
Jim Laskeyef42a012006-11-02 20:12:39 +00003783 // Emit info into a debug loc section.
3784 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003785
Jim Laskeyef42a012006-11-02 20:12:39 +00003786 // Emit info into a debug aranges section.
3787 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003788
Jim Laskeyef42a012006-11-02 20:12:39 +00003789 // Emit info into a debug ranges section.
3790 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003791
Jim Laskeyef42a012006-11-02 20:12:39 +00003792 // Emit info into a debug macinfo section.
3793 EmitDebugMacInfo();
3794 }
3795
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003796 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003797 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003798 void BeginFunction(MachineFunction *MF) {
3799 this->MF = MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003800
Jim Laskeyef42a012006-11-02 20:12:39 +00003801 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003802
3803 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003804 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003805
Jim Laskeyef42a012006-11-02 20:12:39 +00003806 // Assumes in correct section after the entry point.
3807 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003808
3809 // Emit label for the implicitly defined dbg.stoppoint at the start of
3810 // the function.
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003811 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
3812 if (!LineInfos.empty()) {
3813 const SourceLineInfo &LineInfo = LineInfos[0];
3814 Asm->printLabel(LineInfo.getLabelID());
3815 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003816 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003817
Jim Laskey65195462006-10-30 13:35:07 +00003818 /// EndFunction - Gather and emit post-function debug information.
3819 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003820 void EndFunction(MachineFunction *MF) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003821 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003822
Jim Laskeyb82313f2007-02-01 16:31:34 +00003823 // Define end label for subprogram.
3824 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003825
Jim Laskeyef42a012006-11-02 20:12:39 +00003826 // Get function line info.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003827 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
Jim Laskeyef42a012006-11-02 20:12:39 +00003828
3829 if (!LineInfos.empty()) {
3830 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003831 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003832 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
3833 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
3834 // Append the function info to section info.
3835 SectionLineInfos.insert(SectionLineInfos.end(),
3836 LineInfos.begin(), LineInfos.end());
3837 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003838
Jim Laskeyef42a012006-11-02 20:12:39 +00003839 // Construct scopes for subprogram.
Bill Wendlingd751c642008-09-26 00:28:12 +00003840 if (MMI->getRootScope())
3841 ConstructRootScope(MMI->getRootScope());
3842 else
3843 // FIXME: This is wrong. We are essentially getting past a problem with
3844 // debug information not being able to handle unreachable blocks that have
3845 // debug information in them. In particular, those unreachable blocks that
3846 // have "region end" info in them. That situation results in the "root
3847 // scope" not being created. If that's the case, then emit a "default"
3848 // scope, i.e., one that encompasses the whole function. This isn't
3849 // desirable. And a better way of handling this (and all of the debugging
3850 // information) needs to be explored.
3851 ConstructDefaultScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003852
3853 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3854 MMI->getFrameMoves()));
Jim Laskeyef42a012006-11-02 20:12:39 +00003855 }
Jim Laskey65195462006-10-30 13:35:07 +00003856};
3857
Jim Laskey072200c2007-01-29 18:51:14 +00003858//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003859/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003860///
3861class DwarfException : public Dwarf {
3862
Jim Laskeyb82313f2007-02-01 16:31:34 +00003863private:
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003864 struct FunctionEHFrameInfo {
3865 std::string FnName;
3866 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003867 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003868 bool hasCalls;
3869 bool hasLandingPads;
3870 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003871 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003872
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003873 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3874 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003875 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003876 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003877 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003878 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003879 };
3880
3881 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003882
3883 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3884 /// be emitted.
3885 bool shouldEmitTable;
3886
3887 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3888 /// should be emitted.
3889 bool shouldEmitMoves;
3890
3891 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3892 /// should be emitted.
3893 bool shouldEmitTableModule;
3894
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003895 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003896 /// should be emitted.
3897 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003898
Jim Laskey3f09fc22007-02-28 18:38:31 +00003899 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3900 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003901 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003902 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003903 int stackGrowth =
3904 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3905 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003906 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003907
Jim Laskeybacd3042007-02-21 22:48:45 +00003908 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003909 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003910
3911 if (!TAI->doesRequireNonLocalEHFrameLabel())
3912 O << TAI->getEHGlobalPrefix();
3913 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003914 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003915
Jim Laskeybacd3042007-02-21 22:48:45 +00003916 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003917 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003918
Jim Laskeybacd3042007-02-21 22:48:45 +00003919 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003920 EmitDifference("eh_frame_common_end", Index,
3921 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003922 Asm->EOL("Length of Common Information Entry");
3923
Jim Laskeybacd3042007-02-21 22:48:45 +00003924 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003925 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003926 Asm->EmitInt32((int)0);
3927 Asm->EOL("CIE Identifier Tag");
3928 Asm->EmitInt8(DW_CIE_VERSION);
3929 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003930
Jim Laskeybacd3042007-02-21 22:48:45 +00003931 // The personality presence indicates that language specific information
3932 // will show up in the eh frame.
3933 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003934 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003935
Jim Laskeybacd3042007-02-21 22:48:45 +00003936 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003937 Asm->EmitULEB128Bytes(1);
3938 Asm->EOL("CIE Code Alignment Factor");
3939 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003940 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003941 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003942 Asm->EOL("CIE Return Address Column");
3943
Jim Laskeybacd3042007-02-21 22:48:45 +00003944 // If there is a personality, we need to indicate the functions location.
3945 if (Personality) {
3946 Asm->EmitULEB128Bytes(7);
3947 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003948
Duncan Sands671fa972008-05-07 19:11:09 +00003949 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003950 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003951 Asm->EOL("Personality (pcrel sdata4 indirect)");
3952 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003953 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003954 Asm->EOL("Personality (pcrel sdata4)");
3955 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003956
Duncan Sands671fa972008-05-07 19:11:09 +00003957 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003958 O << TAI->getPersonalityPrefix();
3959 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3960 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003961 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3962 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003963 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003964
Duncan Sands671fa972008-05-07 19:11:09 +00003965 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3966 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003967
Bill Wendlingd60de512009-01-05 22:53:45 +00003968 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3969 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003970 } else {
3971 Asm->EmitULEB128Bytes(1);
3972 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003973
Bill Wendlingd60de512009-01-05 22:53:45 +00003974 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3975 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003976 }
3977
3978 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003979 std::vector<MachineMove> Moves;
3980 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003981 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003982
Dale Johannesen21d972a2008-04-30 00:43:29 +00003983 // On Darwin the linker honors the alignment of eh_frame, which means it
3984 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3985 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003986 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003987 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003988 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003989
Jim Laskeybacd3042007-02-21 22:48:45 +00003990 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003991 }
Duncan Sands671fa972008-05-07 19:11:09 +00003992
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003993 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003994 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003995 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003996 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3997
Jim Laskeybacd3042007-02-21 22:48:45 +00003998 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3999
4000 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00004001 // If the corresponding function is static, this should not be
4002 // externally visible.
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004003 if (linkage != Function::InternalLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00004004 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
4005 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
4006 }
4007
Dale Johannesen038129d2008-01-10 02:03:30 +00004008 // If corresponding function is weak definition, this should be too.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004009 if ((linkage == Function::WeakLinkage ||
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004010 linkage == Function::LinkOnceLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00004011 TAI->getWeakDefDirective())
4012 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
4013
4014 // If there are no calls then you can't unwind. This may mean we can
4015 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004016 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00004017 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00004018 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00004019 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00004020 !UnwindTablesMandatory &&
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004021 ((linkage != Function::WeakLinkage &&
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004022 linkage != Function::LinkOnceLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00004023 !TAI->getWeakDefDirective() ||
4024 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004025 {
Bill Wendling6e198962007-09-18 01:47:22 +00004026 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004027 // This name has no connection to the function, so it might get
4028 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004029 // dead-stripping unconditionally.
4030 if (const char *UsedDirective = TAI->getUsedDirective())
4031 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00004032 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00004033 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00004034
Jim Laskeybacd3042007-02-21 22:48:45 +00004035 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004036 EmitDifference("eh_frame_end", EHFrameInfo.Number,
4037 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00004038 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004039
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004040 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00004041
Bill Wendling722f5f12008-12-24 08:05:17 +00004042 if (TAI->doesRequireNonLocalEHFrameLabel()) {
4043 PrintRelDirective(true, true);
4044 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
4045
4046 if (!TAI->isAbsoluteEHSectionOffsets())
4047 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
4048 } else {
4049 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
4050 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
4051 true, true, false);
4052 }
4053
Jim Laskeybacd3042007-02-21 22:48:45 +00004054 Asm->EOL("FDE CIE offset");
4055
Bill Wendling5fe1fac2009-01-06 19:13:55 +00004056 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00004057 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004058 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00004059 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00004060 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00004061
Jim Laskey3f09fc22007-02-28 18:38:31 +00004062 // If there is a personality and landing pads then point to the language
4063 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004064 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00004065 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00004066 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00004067
4068 if (EHFrameInfo.hasLandingPads)
4069 EmitReference("exception", EHFrameInfo.Number, true, true);
4070 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00004071 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00004072 Asm->EOL("Language Specific Data Area");
4073 } else {
4074 Asm->EmitULEB128Bytes(0);
4075 Asm->EOL("Augmentation size");
4076 }
Duncan Sands671fa972008-05-07 19:11:09 +00004077
Jim Laskeybacd3042007-02-21 22:48:45 +00004078 // Indicate locations of function specific callee saved registers in
4079 // frame.
Dale Johannesenb97aec62007-11-13 19:13:01 +00004080 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004081
Dale Johannesen21d972a2008-04-30 00:43:29 +00004082 // On Darwin the linker honors the alignment of eh_frame, which means it
4083 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
4084 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004085 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00004086 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004087 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004088
4089 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004090 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004091 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004092 // that depends on unused functions (calling undefined externals) being
4093 // dead-stripped to link correctly. Yes, there really is.
4094 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
4095 if (const char *UsedDirective = TAI->getUsedDirective())
4096 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
4097 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004098 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00004099
Duncan Sands57810cd2007-09-05 11:27:52 +00004100 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004101 ///
4102 /// The general organization of the table is complex, but the basic concepts
4103 /// are easy. First there is a header which describes the location and
4104 /// organization of the three components that follow.
4105 /// 1. The landing pad site information describes the range of code covered
4106 /// by the try. In our case it's an accumulation of the ranges covered
4107 /// by the invokes in the try. There is also a reference to the landing
4108 /// pad that handles the exception once processed. Finally an index into
4109 /// the actions table.
4110 /// 2. The action table, in our case, is composed of pairs of type ids
4111 /// and next action offset. Starting with the action index from the
4112 /// landing pad site, each type Id is checked for a match to the current
4113 /// exception. If it matches then the exception and type id are passed
4114 /// on to the landing pad. Otherwise the next action is looked up. This
4115 /// chain is terminated with a next action of zero. If no type id is
4116 /// found the the frame is unwound and handling continues.
4117 /// 3. Type id table contains references to all the C++ typeinfo for all
4118 /// catches in the function. This tables is reversed indexed base 1.
4119
Duncan Sandsb32edb42007-06-06 15:37:31 +00004120 /// SharedTypeIds - How many leading type ids two landing pads have in common.
4121 static unsigned SharedTypeIds(const LandingPadInfo *L,
4122 const LandingPadInfo *R) {
4123 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004124 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004125 unsigned MinSize = LSize < RSize ? LSize : RSize;
4126 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004127
Duncan Sandsb32edb42007-06-06 15:37:31 +00004128 for (; Count != MinSize; ++Count)
4129 if (LIds[Count] != RIds[Count])
4130 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004131
Duncan Sandsb32edb42007-06-06 15:37:31 +00004132 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004133 }
4134
Duncan Sandsb32edb42007-06-06 15:37:31 +00004135 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00004136 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004137 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004138 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004139 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004140
Duncan Sandsb32edb42007-06-06 15:37:31 +00004141 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00004142 if (LIds[i] != RIds[i])
4143 return LIds[i] < RIds[i];
4144
Duncan Sandsb32edb42007-06-06 15:37:31 +00004145 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004146 }
4147
Duncan Sands53c3a332007-05-16 12:12:23 +00004148 struct KeyInfo {
4149 static inline unsigned getEmptyKey() { return -1U; }
4150 static inline unsigned getTombstoneKey() { return -2U; }
4151 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00004152 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00004153 static bool isPod() { return true; }
4154 };
4155
Duncan Sands57810cd2007-09-05 11:27:52 +00004156 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004157 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004158 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004159 int NextAction;
4160 struct ActionEntry *Previous;
4161 };
4162
Duncan Sands57810cd2007-09-05 11:27:52 +00004163 /// PadRange - Structure holding a try-range and the associated landing pad.
4164 struct PadRange {
4165 // The index of the landing pad.
4166 unsigned PadIndex;
4167 // The index of the begin and end labels in the landing pad's label lists.
4168 unsigned RangeIndex;
4169 };
4170
4171 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
4172
4173 /// CallSiteEntry - Structure describing an entry in the call-site table.
4174 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00004175 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004176 unsigned BeginLabel; // zero indicates the start of the function.
4177 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00004178 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004179 unsigned PadLabel; // zero indicates that there is no landing pad.
4180 unsigned Action;
4181 };
4182
Jim Laskeybacd3042007-02-21 22:48:45 +00004183 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00004184 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00004185 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00004186 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
4187 if (PadInfos.empty()) return;
4188
4189 // Sort the landing pads in order of their type ids. This is used to fold
4190 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00004191 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00004192 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00004193 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00004194 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004195 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
4196
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004197 // Negative type ids index into FilterIds, positive type ids index into
4198 // TypeInfos. The value written for a positive type id is just the type
4199 // id itself. For a negative type id, however, the value written is the
4200 // (negative) byte offset of the corresponding FilterIds entry. The byte
4201 // offset is usually equal to the type id, because the FilterIds entries
4202 // are written using a variable width encoding which outputs one byte per
4203 // entry as long as the value written is not too large, but can differ.
4204 // This kind of complication does not occur for positive type ids because
4205 // type infos are output using a fixed width encoding.
4206 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
4207 SmallVector<int, 16> FilterOffsets;
4208 FilterOffsets.reserve(FilterIds.size());
4209 int Offset = -1;
4210 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
4211 E = FilterIds.end(); I != E; ++I) {
4212 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004213 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004214 }
4215
Duncan Sands57810cd2007-09-05 11:27:52 +00004216 // Compute the actions table and gather the first action index for each
4217 // landing pad site.
4218 SmallVector<ActionEntry, 32> Actions;
4219 SmallVector<unsigned, 64> FirstActions;
4220 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00004221
Duncan Sandsb32edb42007-06-06 15:37:31 +00004222 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00004223 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004224 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004225 const LandingPadInfo *LP = LandingPads[i];
4226 const std::vector<int> &TypeIds = LP->TypeIds;
4227 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004228 unsigned SizeSiteActions = 0;
4229
Duncan Sandsb32edb42007-06-06 15:37:31 +00004230 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00004231 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00004232 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004233
Duncan Sandsb32edb42007-06-06 15:37:31 +00004234 if (NumShared) {
4235 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
4236 assert(Actions.size());
4237 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004238 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
4239 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004240 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004241 SizeAction -=
4242 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004243 SizeAction += -PrevAction->NextAction;
4244 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004245 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00004246 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00004247
Duncan Sandsb32edb42007-06-06 15:37:31 +00004248 // Compute the actions.
4249 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
4250 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004251 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4252 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004253 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004254
4255 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004256 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004257 SizeSiteActions += SizeAction;
4258
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004259 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00004260 Actions.push_back(Action);
4261
4262 PrevAction = &Actions.back();
4263 }
4264
4265 // Record the first action of the landing pad site.
4266 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4267 } // else identical - re-use previous FirstAction
4268
4269 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004270
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00004271 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00004272 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00004273 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004274
Duncan Sands481dc722007-12-19 07:36:31 +00004275 // Compute the call-site table. The entry for an invoke has a try-range
4276 // containing the call, a non-zero landing pad and an appropriate action.
4277 // The entry for an ordinary call has a try-range containing the call and
4278 // zero for the landing pad and the action. Calls marked 'nounwind' have
4279 // no entry and must not be contained in the try-range of any entry - they
4280 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004281 SmallVector<CallSiteEntry, 64> CallSites;
4282
4283 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00004284 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4285 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4286 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00004287 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4288 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00004289 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004290 unsigned BeginLabel = LandingPad->BeginLabels[j];
4291 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4292 PadRange P = { i, j };
4293 PadMap[BeginLabel] = P;
4294 }
4295 }
4296
Duncan Sands481dc722007-12-19 07:36:31 +00004297 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00004298 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00004299
4300 // Whether there is a potentially throwing instruction (currently this means
4301 // an ordinary call) between the end of the previous try-range and now.
4302 bool SawPotentiallyThrowing = false;
4303
4304 // Whether the last callsite entry was for an invoke.
4305 bool PreviousIsInvoke = false;
4306
Duncan Sands481dc722007-12-19 07:36:31 +00004307 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004308 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4309 I != E; ++I) {
4310 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4311 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00004312 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00004313 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00004314 continue;
4315 }
4316
Chris Lattner9e330492007-12-30 20:50:28 +00004317 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00004318 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00004319
Duncan Sands481dc722007-12-19 07:36:31 +00004320 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00004321 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00004322 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004323
Duncan Sands481dc722007-12-19 07:36:31 +00004324 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00004325 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00004326 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00004327 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00004328 continue;
4329
4330 PadRange P = L->second;
4331 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4332
4333 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4334 "Inconsistent landing pad map!");
4335
4336 // If some instruction between the previous try-range and this one may
4337 // throw, create a call-site entry with no landing pad for the region
4338 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00004339 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004340 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4341 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00004342 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004343 }
4344
4345 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00004346 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00004347
Duncan Sands481dc722007-12-19 07:36:31 +00004348 if (LandingPad->LandingPadLabel) {
4349 // This try-range is for an invoke.
4350 CallSiteEntry Site = {BeginLabel, LastLabel,
4351 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00004352
Duncan Sands481dc722007-12-19 07:36:31 +00004353 // Try to merge with the previous call-site.
4354 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00004355 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00004356 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4357 // Extend the range of the previous entry.
4358 Prev.EndLabel = Site.EndLabel;
4359 continue;
4360 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004361 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004362
Duncan Sands481dc722007-12-19 07:36:31 +00004363 // Otherwise, create a new call-site.
4364 CallSites.push_back(Site);
4365 PreviousIsInvoke = true;
4366 } else {
4367 // Create a gap.
4368 PreviousIsInvoke = false;
4369 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004370 }
4371 }
4372 // If some instruction between the previous try-range and the end of the
4373 // function may throw, create a call-site entry with no landing pad for the
4374 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00004375 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004376 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4377 CallSites.push_back(Site);
4378 }
4379
Jim Laskeybacd3042007-02-21 22:48:45 +00004380 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00004381
4382 // Call sites.
4383 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4384 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4385 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4386 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4387 SiteLengthSize +
4388 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00004389 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004390 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00004391
Duncan Sands671fa972008-05-07 19:11:09 +00004392 // Type infos.
4393 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4394 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004395
4396 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004397 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004398 SizeSites + SizeActions + SizeTypes;
4399
4400 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4401 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004402 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004403 TypeOffset;
4404
4405 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004406
Jim Laskeybacd3042007-02-21 22:48:45 +00004407 // Begin the exception table.
4408 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00004409 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00004410 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004411 for (unsigned i = 0; i != SizeAlign; ++i) {
4412 Asm->EmitInt8(0);
4413 Asm->EOL("Padding");
4414 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004415 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004416
Jim Laskeybacd3042007-02-21 22:48:45 +00004417 // Emit the header.
4418 Asm->EmitInt8(DW_EH_PE_omit);
4419 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4420 Asm->EmitInt8(DW_EH_PE_absptr);
4421 Asm->EOL("TType format (DW_EH_PE_absptr)");
4422 Asm->EmitULEB128Bytes(TypeOffset);
4423 Asm->EOL("TType base offset");
4424 Asm->EmitInt8(DW_EH_PE_udata4);
4425 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4426 Asm->EmitULEB128Bytes(SizeSites);
4427 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004428
Duncan Sands57810cd2007-09-05 11:27:52 +00004429 // Emit the landing pad site information.
4430 for (unsigned i = 0; i < CallSites.size(); ++i) {
4431 CallSiteEntry &S = CallSites[i];
4432 const char *BeginTag;
4433 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00004434
Duncan Sands57810cd2007-09-05 11:27:52 +00004435 if (!S.BeginLabel) {
4436 BeginTag = "eh_func_begin";
4437 BeginNumber = SubprogramCount;
4438 } else {
4439 BeginTag = "label";
4440 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004441 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004442
Duncan Sands57810cd2007-09-05 11:27:52 +00004443 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004444 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004445 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004446
Duncan Sands57810cd2007-09-05 11:27:52 +00004447 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004448 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004449 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004450 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004451 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004452 }
4453 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004454
Duncan Sands671fa972008-05-07 19:11:09 +00004455 if (!S.PadLabel)
4456 Asm->EmitInt32(0);
4457 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004458 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004459 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004460 Asm->EOL("Landing pad");
4461
4462 Asm->EmitULEB128Bytes(S.Action);
4463 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004464 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004465
Jim Laskeybacd3042007-02-21 22:48:45 +00004466 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004467 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4468 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004469
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004470 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004471 Asm->EOL("TypeInfo index");
4472 Asm->EmitSLEB128Bytes(Action.NextAction);
4473 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004474 }
4475
4476 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004477 for (unsigned M = TypeInfos.size(); M; --M) {
4478 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004479
4480 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004481
4482 if (GV)
4483 O << Asm->getGlobalLinkName(GV);
4484 else
4485 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00004486
Jim Laskeybacd3042007-02-21 22:48:45 +00004487 Asm->EOL("TypeInfo");
4488 }
4489
Duncan Sands73ef58a2007-06-02 16:53:42 +00004490 // Emit the filter typeids.
4491 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4492 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004493 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004494 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004495 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004496
Evan Cheng05548eb2008-02-29 19:36:59 +00004497 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004498 }
4499
Jim Laskey072200c2007-01-29 18:51:14 +00004500public:
4501 //===--------------------------------------------------------------------===//
4502 // Main entry points.
4503 //
Owen Andersoncb371882008-08-21 00:14:44 +00004504 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00004505 : Dwarf(OS, A, T, "eh")
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004506 , shouldEmitTable(false)
4507 , shouldEmitMoves(false)
4508 , shouldEmitTableModule(false)
4509 , shouldEmitMovesModule(false)
Jim Laskey072200c2007-01-29 18:51:14 +00004510 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004511
Jim Laskey072200c2007-01-29 18:51:14 +00004512 virtual ~DwarfException() {}
4513
4514 /// SetModuleInfo - Set machine module information when it's known that pass
4515 /// manager has created it. Set by the target AsmPrinter.
4516 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004517 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004518 }
4519
4520 /// BeginModule - Emit all exception information that should come prior to the
4521 /// content.
4522 void BeginModule(Module *M) {
4523 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004524 }
4525
4526 /// EndModule - Emit all exception information that should come after the
4527 /// content.
4528 void EndModule() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004529 if (shouldEmitMovesModule || shouldEmitTableModule) {
4530 const std::vector<Function *> Personalities = MMI->getPersonalities();
4531 for (unsigned i =0; i < Personalities.size(); ++i)
4532 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004533
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004534 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4535 E = EHFrames.end(); I != E; ++I)
4536 EmitEHFrame(*I);
4537 }
Jim Laskey072200c2007-01-29 18:51:14 +00004538 }
4539
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004540 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004541 /// emitted immediately after the function entry point.
4542 void BeginFunction(MachineFunction *MF) {
4543 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004544 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesene0040622008-04-02 17:04:45 +00004545 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004546
4547 // Map all labels and get rid of any dead landing pads.
4548 MMI->TidyLandingPads();
4549 // If any landing pads survive, we need an EH table.
4550 if (MMI->getLandingPads().size())
4551 shouldEmitTable = true;
4552
4553 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004554 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004555 shouldEmitMoves = true;
4556
4557 if (shouldEmitMoves || shouldEmitTable)
4558 // Assumes in correct section after the entry point.
4559 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004560 }
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004561 shouldEmitTableModule |= shouldEmitTable;
4562 shouldEmitMovesModule |= shouldEmitMoves;
Jim Laskey072200c2007-01-29 18:51:14 +00004563 }
4564
4565 /// EndFunction - Gather and emit post-function exception information.
4566 ///
4567 void EndFunction() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004568 if (shouldEmitMoves || shouldEmitTable) {
4569 EmitLabel("eh_func_end", SubprogramCount);
4570 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004571
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004572 // Save EH frame information
4573 EHFrames.
4574 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling6e198962007-09-18 01:47:22 +00004575 SubprogramCount,
4576 MMI->getPersonalityIndex(),
4577 MF->getFrameInfo()->hasCalls(),
4578 !MMI->getLandingPads().empty(),
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00004579 MMI->getFrameMoves(),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004580 MF->getFunction()));
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004581 }
Jim Laskey072200c2007-01-29 18:51:14 +00004582 }
4583};
4584
Jim Laskey0d086af2006-02-27 12:43:29 +00004585} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004586
4587//===----------------------------------------------------------------------===//
4588
Jim Laskeyd18e2892006-01-20 20:34:06 +00004589/// Emit - Print the abbreviation using the specified Dwarf writer.
4590///
Jim Laskey072200c2007-01-29 18:51:14 +00004591void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004592 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004593 DD.getAsm()->EmitULEB128Bytes(Tag);
4594 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004595
Jim Laskeyd18e2892006-01-20 20:34:06 +00004596 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004597 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4598 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004599
Jim Laskeyd18e2892006-01-20 20:34:06 +00004600 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004601 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004602 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004603
Jim Laskeyd18e2892006-01-20 20:34:06 +00004604 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004605 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4606 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004607
Jim Laskeyd18e2892006-01-20 20:34:06 +00004608 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004609 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4610 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004611 }
4612
4613 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004614 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4615 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004616}
4617
4618#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004619void DIEAbbrev::print(std::ostream &O) {
4620 O << "Abbreviation @"
4621 << std::hex << (intptr_t)this << std::dec
4622 << " "
4623 << TagString(Tag)
4624 << " "
4625 << ChildrenString(ChildrenFlag)
4626 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004627
Jim Laskeya0f3d172006-09-07 22:06:40 +00004628 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4629 O << " "
4630 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004631 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004632 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004633 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004634 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004635}
Bill Wendlinge8156192006-12-07 01:30:32 +00004636void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004637#endif
4638
4639//===----------------------------------------------------------------------===//
4640
Jim Laskeyef42a012006-11-02 20:12:39 +00004641#ifndef NDEBUG
4642void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004643 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004644}
Jim Laskeyef42a012006-11-02 20:12:39 +00004645#endif
4646
4647//===----------------------------------------------------------------------===//
4648
Jim Laskey063e7652006-01-17 17:31:53 +00004649/// EmitValue - Emit integer of appropriate size.
4650///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004651void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004652 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004653 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004654 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004655 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004656 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004657 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004658 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004659 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004660 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004661 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4662 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4663 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004664 default: assert(0 && "DIE Value form not supported yet"); break;
4665 }
4666}
4667
4668/// SizeOf - Determine size of integer value in bytes.
4669///
Jim Laskey072200c2007-01-29 18:51:14 +00004670unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004671 switch (Form) {
4672 case DW_FORM_flag: // Fall thru
4673 case DW_FORM_ref1: // Fall thru
4674 case DW_FORM_data1: return sizeof(int8_t);
4675 case DW_FORM_ref2: // Fall thru
4676 case DW_FORM_data2: return sizeof(int16_t);
4677 case DW_FORM_ref4: // Fall thru
4678 case DW_FORM_data4: return sizeof(int32_t);
4679 case DW_FORM_ref8: // Fall thru
4680 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004681 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4682 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004683 default: assert(0 && "DIE Value form not supported yet"); break;
4684 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004685 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004686}
4687
Jim Laskey063e7652006-01-17 17:31:53 +00004688//===----------------------------------------------------------------------===//
4689
4690/// EmitValue - Emit string value.
4691///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004692void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004693 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00004694}
4695
Jim Laskey063e7652006-01-17 17:31:53 +00004696//===----------------------------------------------------------------------===//
4697
4698/// EmitValue - Emit label value.
4699///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004700void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004701 bool IsSmall = Form == DW_FORM_data4;
4702 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004703}
4704
4705/// SizeOf - Determine size of label value in bytes.
4706///
Jim Laskey072200c2007-01-29 18:51:14 +00004707unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004708 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004709 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004710}
Jim Laskeyef42a012006-11-02 20:12:39 +00004711
Jim Laskey063e7652006-01-17 17:31:53 +00004712//===----------------------------------------------------------------------===//
4713
Jim Laskeyd18e2892006-01-20 20:34:06 +00004714/// EmitValue - Emit label value.
4715///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004716void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004717 bool IsSmall = Form == DW_FORM_data4;
4718 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004719}
4720
4721/// SizeOf - Determine size of label value in bytes.
4722///
Jim Laskey072200c2007-01-29 18:51:14 +00004723unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004724 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004725 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004726}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004727
Jim Laskeyd18e2892006-01-20 20:34:06 +00004728//===----------------------------------------------------------------------===//
4729
Jim Laskey063e7652006-01-17 17:31:53 +00004730/// EmitValue - Emit delta value.
4731///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004732void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4733 bool IsSmall = Form == DW_FORM_data4;
4734 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4735 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4736}
4737
4738/// SizeOf - Determine size of delta value in bytes.
4739///
4740unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4741 if (Form == DW_FORM_data4) return 4;
4742 return DD.getTargetData()->getPointerSize();
4743}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004744
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004745//===----------------------------------------------------------------------===//
4746
4747/// EmitValue - Emit delta value.
4748///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004749void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004750 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004751 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004752}
4753
4754/// SizeOf - Determine size of delta value in bytes.
4755///
Jim Laskey072200c2007-01-29 18:51:14 +00004756unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004757 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004758 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004759}
4760
4761//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004762
Jim Laskeyb8509c52006-03-23 18:07:55 +00004763/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004764///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004765void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004766 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004767}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004768
Jim Laskeyd18e2892006-01-20 20:34:06 +00004769//===----------------------------------------------------------------------===//
4770
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004771/// ComputeSize - calculate the size of the block.
4772///
Jim Laskey072200c2007-01-29 18:51:14 +00004773unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004774 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004775 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004776
Jim Laskeyef42a012006-11-02 20:12:39 +00004777 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004778 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004779 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004780 }
4781 return Size;
4782}
4783
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004784/// EmitValue - Emit block data.
4785///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004786void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004787 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004788 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4789 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4790 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4791 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004792 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004793 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004794
Owen Anderson873e1b52008-06-24 21:44:59 +00004795 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004796
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004797 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004798 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004799 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004800 }
4801}
4802
4803/// SizeOf - Determine size of block data in bytes.
4804///
Jim Laskey072200c2007-01-29 18:51:14 +00004805unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004806 switch (Form) {
4807 case DW_FORM_block1: return Size + sizeof(int8_t);
4808 case DW_FORM_block2: return Size + sizeof(int16_t);
4809 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004810 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004811 default: assert(0 && "Improper form for block"); break;
4812 }
4813 return 0;
4814}
4815
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004816//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004817/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004818
4819DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004820 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004821 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004822}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004823
Jim Laskeyb8509c52006-03-23 18:07:55 +00004824/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4825///
4826void DIE::AddSiblingOffset() {
4827 DIEInteger *DI = new DIEInteger(0);
4828 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004829 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004830}
4831
Jim Laskeyef42a012006-11-02 20:12:39 +00004832/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004833///
Jim Laskeyef42a012006-11-02 20:12:39 +00004834void DIE::Profile(FoldingSetNodeID &ID) {
4835 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004836
Jim Laskeyef42a012006-11-02 20:12:39 +00004837 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4838 ID.AddPointer(Children[i]);
4839
4840 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4841 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004842}
Jim Laskeyef42a012006-11-02 20:12:39 +00004843
4844#ifndef NDEBUG
4845void DIE::print(std::ostream &O, unsigned IncIndent) {
4846 static unsigned IndentCount = 0;
4847 IndentCount += IncIndent;
4848 const std::string Indent(IndentCount, ' ');
4849 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004850
Jim Laskeyef42a012006-11-02 20:12:39 +00004851 if (!isBlock) {
4852 O << Indent
4853 << "Die: "
4854 << "0x" << std::hex << (intptr_t)this << std::dec
4855 << ", Offset: " << Offset
4856 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004857 << "\n";
4858
Jim Laskeyef42a012006-11-02 20:12:39 +00004859 O << Indent
4860 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004861 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004862 << ChildrenString(Abbrev.getChildrenFlag());
4863 } else {
4864 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004865 }
4866 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004867
Owen Anderson873e1b52008-06-24 21:44:59 +00004868 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004869
Jim Laskeyef42a012006-11-02 20:12:39 +00004870 IndentCount += 2;
4871 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4872 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004873
4874 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004875 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004876 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004877 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004878
Jim Laskeyef42a012006-11-02 20:12:39 +00004879 O << " "
4880 << FormEncodingString(Data[i].getForm())
4881 << " ";
4882 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004883 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004884 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004885 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004886
Jim Laskeyef42a012006-11-02 20:12:39 +00004887 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4888 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004889 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004890
Jim Laskeyef42a012006-11-02 20:12:39 +00004891 if (!isBlock) O << "\n";
4892 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004893}
4894
Jim Laskeyef42a012006-11-02 20:12:39 +00004895void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004896 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004897}
Jim Laskeybd761842006-02-27 17:27:12 +00004898#endif
Jim Laskey65195462006-10-30 13:35:07 +00004899
4900//===----------------------------------------------------------------------===//
4901/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004902///
Jim Laskey65195462006-10-30 13:35:07 +00004903
Devang Pateleb3fc282009-01-08 23:40:34 +00004904DwarfWriter::DwarfWriter() : ImmutablePass(&ID), DD(NULL), DE(NULL) {
Jim Laskey65195462006-10-30 13:35:07 +00004905}
4906
4907DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004908 delete DE;
4909 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004910}
4911
Jim Laskey65195462006-10-30 13:35:07 +00004912/// BeginModule - Emit all Dwarf sections that should come prior to the
4913/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004914void DwarfWriter::BeginModule(Module *M,
4915 MachineModuleInfo *MMI,
4916 raw_ostream &OS, AsmPrinter *A,
4917 const TargetAsmInfo *T) {
4918 DE = new DwarfException(OS, A, T);
4919 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004920 DE->BeginModule(M);
4921 DD->BeginModule(M);
Devang Pateleb3fc282009-01-08 23:40:34 +00004922 DD->SetModuleInfo(MMI);
4923 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004924}
4925
4926/// EndModule - Emit all Dwarf sections that should come after the content.
4927///
4928void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004929 DE->EndModule();
4930 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004931}
4932
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004933/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004934/// emitted immediately after the function entry point.
4935void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004936 DE->BeginFunction(MF);
4937 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004938}
4939
4940/// EndFunction - Gather and emit post-function debug information.
4941///
Bill Wendlingd751c642008-09-26 00:28:12 +00004942void DwarfWriter::EndFunction(MachineFunction *MF) {
4943 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004944 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004945
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004946 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004947 // Clear function debug information.
4948 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004949}