blob: 0194dd4e490c58d454484e34892919f55cc5fe66 [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
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001344 struct FunctionDebugFrameInfo {
1345 unsigned Number;
1346 std::vector<MachineMove> Moves;
1347
1348 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001349 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001350 };
1351
1352 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001353
Jim Laskey65195462006-10-30 13:35:07 +00001354public:
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001355
Jim Laskeybacd3042007-02-21 22:48:45 +00001356 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1357 ///
1358 bool ShouldEmitDwarf() const { return shouldEmit; }
Jim Laskey65195462006-10-30 13:35:07 +00001359
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001360 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001361 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001362 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1363 // Profile the node so that we can make it unique.
1364 FoldingSetNodeID ID;
1365 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001366
Jim Laskeyef42a012006-11-02 20:12:39 +00001367 // Check the set for priors.
1368 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001369
Jim Laskeyef42a012006-11-02 20:12:39 +00001370 // If it's newly added.
1371 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001372 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001373 Abbreviations.push_back(&Abbrev);
1374 // Assign the vector position + 1 as its number.
1375 Abbrev.setNumber(Abbreviations.size());
1376 } else {
1377 // Assign existing abbreviation number.
1378 Abbrev.setNumber(InSet->getNumber());
1379 }
1380 }
1381
Jim Laskey65195462006-10-30 13:35:07 +00001382 /// NewString - Add a string to the constant pool and returns a label.
1383 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001384 DWLabel NewString(const std::string &String) {
1385 unsigned StringID = StringPool.insert(String);
1386 return DWLabel("string", StringID);
1387 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001388
Jim Laskeyef42a012006-11-02 20:12:39 +00001389 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1390 /// entry.
1391 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1392 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001393
Jim Laskeyef42a012006-11-02 20:12:39 +00001394 if (Entry) {
1395 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001396 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001397 void *Where;
1398 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001399
Jim Laskeyf6733882006-11-02 21:48:18 +00001400 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001401
Jim Laskeyef42a012006-11-02 20:12:39 +00001402 Value = new DIEntry(Entry);
1403 ValuesSet.InsertNode(Value, Where);
1404 } else {
1405 Value = new DIEntry(Entry);
1406 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001407
Jim Laskeyef42a012006-11-02 20:12:39 +00001408 Values.push_back(Value);
1409 return Value;
1410 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001411
Jim Laskeyef42a012006-11-02 20:12:39 +00001412 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1413 ///
1414 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1415 Value->Entry = Entry;
1416 // Add to values set if not already there. If it is, we merely have a
1417 // duplicate in the values list (no harm.)
1418 ValuesSet.GetOrInsertNode(Value);
1419 }
1420
1421 /// AddUInt - Add an unsigned integer attribute data and value.
1422 ///
1423 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1424 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1425
1426 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001427 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001428 void *Where;
1429 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1430 if (!Value) {
1431 Value = new DIEInteger(Integer);
1432 ValuesSet.InsertNode(Value, Where);
1433 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001434 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001435
Jim Laskeyef42a012006-11-02 20:12:39 +00001436 Die->AddValue(Attribute, Form, Value);
1437 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001438
Jim Laskeyef42a012006-11-02 20:12:39 +00001439 /// AddSInt - Add an signed integer attribute data and value.
1440 ///
1441 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1442 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1443
1444 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001445 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001446 void *Where;
1447 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1448 if (!Value) {
1449 Value = new DIEInteger(Integer);
1450 ValuesSet.InsertNode(Value, Where);
1451 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001452 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001453
Jim Laskeyef42a012006-11-02 20:12:39 +00001454 Die->AddValue(Attribute, Form, Value);
1455 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001456
Jim Laskeyef42a012006-11-02 20:12:39 +00001457 /// AddString - Add a std::string attribute data and value.
1458 ///
1459 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1460 const std::string &String) {
1461 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001462 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001463 void *Where;
1464 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1465 if (!Value) {
1466 Value = new DIEString(String);
1467 ValuesSet.InsertNode(Value, Where);
1468 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001469 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001470
Jim Laskeyef42a012006-11-02 20:12:39 +00001471 Die->AddValue(Attribute, Form, Value);
1472 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001473
Jim Laskeyef42a012006-11-02 20:12:39 +00001474 /// AddLabel - Add a Dwarf label attribute data and value.
1475 ///
1476 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1477 const DWLabel &Label) {
1478 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001479 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001480 void *Where;
1481 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1482 if (!Value) {
1483 Value = new DIEDwarfLabel(Label);
1484 ValuesSet.InsertNode(Value, Where);
1485 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001486 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001487
Jim Laskeyef42a012006-11-02 20:12:39 +00001488 Die->AddValue(Attribute, Form, Value);
1489 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001490
Jim Laskeyef42a012006-11-02 20:12:39 +00001491 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1492 ///
1493 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1494 const std::string &Label) {
1495 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001496 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001497 void *Where;
1498 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1499 if (!Value) {
1500 Value = new DIEObjectLabel(Label);
1501 ValuesSet.InsertNode(Value, Where);
1502 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001503 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001504
Jim Laskeyef42a012006-11-02 20:12:39 +00001505 Die->AddValue(Attribute, Form, Value);
1506 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001507
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001508 /// AddSectionOffset - Add a section offset label attribute data and value.
1509 ///
1510 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1511 const DWLabel &Label, const DWLabel &Section,
1512 bool isEH = false, bool useSet = true) {
1513 FoldingSetNodeID ID;
1514 DIESectionOffset::Profile(ID, Label, Section);
1515 void *Where;
1516 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1517 if (!Value) {
1518 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1519 ValuesSet.InsertNode(Value, Where);
1520 Values.push_back(Value);
1521 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001522
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001523 Die->AddValue(Attribute, Form, Value);
1524 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001525
Jim Laskeyef42a012006-11-02 20:12:39 +00001526 /// AddDelta - Add a label delta attribute data and value.
1527 ///
1528 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1529 const DWLabel &Hi, const DWLabel &Lo) {
1530 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001531 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001532 void *Where;
1533 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1534 if (!Value) {
1535 Value = new DIEDelta(Hi, Lo);
1536 ValuesSet.InsertNode(Value, Where);
1537 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001538 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001539
Jim Laskeyef42a012006-11-02 20:12:39 +00001540 Die->AddValue(Attribute, Form, Value);
1541 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001542
Jim Laskeyef42a012006-11-02 20:12:39 +00001543 /// AddDIEntry - Add a DIE attribute data and value.
1544 ///
1545 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1546 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1547 }
1548
1549 /// AddBlock - Add block data.
1550 ///
1551 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1552 Block->ComputeSize(*this);
1553 FoldingSetNodeID ID;
1554 Block->Profile(ID);
1555 void *Where;
1556 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1557 if (!Value) {
1558 Value = Block;
1559 ValuesSet.InsertNode(Value, Where);
1560 Values.push_back(Value);
1561 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001562 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001563 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001564 Block = cast<DIEBlock>(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001565 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001566
Jim Laskeyef42a012006-11-02 20:12:39 +00001567 Die->AddValue(Attribute, Block->BestForm(), Value);
1568 }
1569
Jim Laskey65195462006-10-30 13:35:07 +00001570private:
1571
1572 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001573 /// entry.
1574 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1575 if (File && Line) {
1576 CompileUnit *FileUnit = FindCompileUnit(File);
1577 unsigned FileID = FileUnit->getID();
1578 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1579 AddUInt(Die, DW_AT_decl_line, 0, Line);
1580 }
1581 }
Jim Laskey65195462006-10-30 13:35:07 +00001582
Devang Patel8526cc02009-01-05 22:35:52 +00001583 /// AddSourceLine - Add location information to specified debug information
1584 /// entry.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001585 void AddSourceLine(DIE *Die, DIVariable *V) {
1586 unsigned FileID = 0;
1587 unsigned Line = V->getLineNumber();
1588 if (V->getVersion() < DIDescriptor::Version7) {
1589 // Version6 or earlier. Use compile unit info to get file id.
1590 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1591 FileID = Unit->getID();
1592 } else {
1593 // Version7 or newer, use filename and directory info from DIVariable
1594 // directly.
1595 unsigned DID = Directories.idFor(V->getDirectory());
1596 FileID = SrcFiles.idFor(SrcFileInfo(DID, V->getFilename()));
1597 }
1598 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1599 AddUInt(Die, DW_AT_decl_line, 0, Line);
1600 }
1601
1602 /// AddSourceLine - Add location information to specified debug information
1603 /// entry.
Devang Patel8526cc02009-01-05 22:35:52 +00001604 void AddSourceLine(DIE *Die, DIGlobal *G) {
1605 unsigned FileID = 0;
1606 unsigned Line = G->getLineNumber();
1607 if (G->getVersion() < DIDescriptor::Version7) {
1608 // Version6 or earlier. Use compile unit info to get file id.
1609 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1610 FileID = Unit->getID();
1611 } else {
1612 // Version7 or newer, use filename and directory info from DIGlobal
1613 // directly.
1614 unsigned DID = Directories.idFor(G->getDirectory());
1615 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1616 }
1617 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1618 AddUInt(Die, DW_AT_decl_line, 0, Line);
1619 }
1620
1621 void AddSourceLine(DIE *Die, DIType *G) {
1622 unsigned FileID = 0;
1623 unsigned Line = G->getLineNumber();
1624 if (G->getVersion() < DIDescriptor::Version7) {
1625 // Version6 or earlier. Use compile unit info to get file id.
1626 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1627 FileID = Unit->getID();
1628 } else {
1629 // Version7 or newer, use filename and directory info from DIGlobal
1630 // directly.
1631 unsigned DID = Directories.idFor(G->getDirectory());
1632 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1633 }
1634 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1635 AddUInt(Die, DW_AT_decl_line, 0, Line);
1636 }
1637
Jim Laskey65195462006-10-30 13:35:07 +00001638 /// AddAddress - Add an address attribute to a die based on the location
1639 /// provided.
1640 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001641 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001642 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001643 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001644
Dan Gohmand735b802008-10-03 15:45:36 +00001645 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001646 if (Reg < 32) {
1647 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1648 } else {
1649 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1650 AddUInt(Block, 0, DW_FORM_udata, Reg);
1651 }
1652 } else {
1653 if (Reg < 32) {
1654 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1655 } else {
1656 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1657 AddUInt(Block, 0, DW_FORM_udata, Reg);
1658 }
1659 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1660 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001661
Jim Laskeyef42a012006-11-02 20:12:39 +00001662 AddBlock(Die, Attribute, 0, Block);
1663 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001664
Jim Laskeyef42a012006-11-02 20:12:39 +00001665 /// AddBasicType - Add a new basic type attribute to the specified entity.
1666 ///
1667 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1668 const std::string &Name,
1669 unsigned Encoding, unsigned Size) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001670
Jim Laskeyef42a012006-11-02 20:12:39 +00001671 DIE Buffer(DW_TAG_base_type);
1672 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1673 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1674 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel9ede3f22009-01-05 17:44:11 +00001675 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1676 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001677 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001678
Jim Laskeyef42a012006-11-02 20:12:39 +00001679 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1680 ///
1681 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001682 DIE Buffer(DW_TAG_pointer_type);
Dan Gohman82482942007-09-27 23:12:31 +00001683 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Jim Laskeyef42a012006-11-02 20:12:39 +00001684 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelca03c272009-01-05 17:45:59 +00001685 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1686 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001687 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001688
Jim Laskeyef42a012006-11-02 20:12:39 +00001689 /// AddType - Add a new type attribute to the specified entity.
1690 ///
1691 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1692 if (!TyDesc) {
Jim Laskey2b935d52007-01-26 14:19:17 +00001693 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
Jim Laskeyef42a012006-11-02 20:12:39 +00001694 } else {
1695 // Check for pre-existence.
1696 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001697
Jim Laskeyef42a012006-11-02 20:12:39 +00001698 // If it exists then use the existing value.
1699 if (Slot) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001700 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1701 return;
1702 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001703
Jim Laskeyef42a012006-11-02 20:12:39 +00001704 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1705 // FIXME - Not sure why programs and variables are coming through here.
1706 // Short cut for handling subprogram types (not really a TyDesc.)
1707 AddPointerType(Entity, Unit, SubprogramTy->getName());
1708 } else if (GlobalVariableDesc *GlobalTy =
1709 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1710 // FIXME - Not sure why programs and variables are coming through here.
1711 // Short cut for handling global variable types (not really a TyDesc.)
1712 AddPointerType(Entity, Unit, GlobalTy->getName());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001713 } else {
Jim Laskeyef42a012006-11-02 20:12:39 +00001714 // Set up proxy.
1715 Slot = NewDIEntry();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001716
Jim Laskeyef42a012006-11-02 20:12:39 +00001717 // Construct type.
1718 DIE Buffer(DW_TAG_base_type);
1719 ConstructType(Buffer, TyDesc, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001720
Jim Laskeyef42a012006-11-02 20:12:39 +00001721 // Add debug information entry to entity and unit.
1722 DIE *Die = Unit->AddDie(Buffer);
1723 SetDIEntry(Slot, Die);
1724 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1725 }
1726 }
1727 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001728
Devang Patel48d190f2009-01-05 21:47:57 +00001729 /// AddType - Add a new type attribute to the specified entity.
1730 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
1731 if (Ty.isNull()) {
1732 AddBasicType(Entity, DW_Unit, "", DW_ATE_signed, sizeof(int32_t));
1733 return;
1734 }
1735
1736 // Check for pre-existence.
1737 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1738 // If it exists then use the existing value.
1739 if (Slot) {
1740 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1741 return;
1742 }
1743
1744 // Set up proxy.
1745 Slot = NewDIEntry();
1746
1747 // Construct type.
1748 DIE Buffer(DW_TAG_base_type);
1749 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1750 ConstructTypeDIE(DW_Unit, Buffer, BT);
1751 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1752 ConstructTypeDIE(DW_Unit, Buffer, DT);
1753 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1754 ConstructTypeDIE(DW_Unit, Buffer, CT);
1755
1756 // Add debug information entry to entity and unit.
1757 DIE *Die = DW_Unit->AddDie(Buffer);
1758 SetDIEntry(Slot, Die);
1759 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1760 }
1761
Devang Patele5202732009-01-05 19:07:53 +00001762 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1763 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1764 DIBasicType *BTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001765
1766 // Get core information.
1767 const std::string &Name = BTy->getName();
1768 Buffer.setTag(DW_TAG_base_type);
1769 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy->getEncoding());
1770 // Add name if not anonymous or intermediate type.
1771 if (!Name.empty())
1772 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1773 uint64_t Size = BTy->getSizeInBits() >> 3;
1774 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1775 }
1776
Devang Patele5202732009-01-05 19:07:53 +00001777 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1778 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1779 DIDerivedType *DTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001780
1781 // Get core information.
1782 const std::string &Name = DTy->getName();
1783 uint64_t Size = DTy->getSizeInBits() >> 3;
1784 unsigned Tag = DTy->getTag();
1785 // FIXME - Workaround for templates.
1786 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1787
1788 Buffer.setTag(Tag);
1789 // Map to main type, void will not have a type.
1790 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001791 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001792
1793 // Add name if not anonymous or intermediate type.
1794 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1795
1796 // Add size if non-zero (derived types might be zero-sized.)
1797 if (Size)
1798 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1799
1800 // Add source line info if available and TyDesc is not a forward
1801 // declaration.
1802 // FIXME - Enable this. if (!DTy->isForwardDecl())
1803 // FIXME - Enable this. AddSourceLine(&Buffer, *DTy);
1804 }
1805
Devang Patelf4215332009-01-05 19:55:51 +00001806 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1807 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1808 DICompositeType *CTy) {
1809
1810 // Get core information.
1811 const std::string &Name = CTy->getName();
1812 uint64_t Size = CTy->getSizeInBits() >> 3;
1813 unsigned Tag = CTy->getTag();
1814 switch (Tag) {
1815 case DW_TAG_vector_type:
1816 case DW_TAG_array_type:
1817 ConstructArrayTypeDIE(DW_Unit, Buffer, CTy);
1818 break;
1819 //FIXME - Enable this.
1820 // case DW_TAG_enumeration_type:
1821 // DIArray Elements = CTy->getTypeArray();
1822 // // Add enumerators to enumeration type.
1823 // for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i)
1824 // ConstructEnumTypeDIE(Buffer, &Elements.getElement(i));
1825 // break;
1826 case DW_TAG_subroutine_type:
1827 {
1828 // Add prototype flag.
1829 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1830 DIArray Elements = CTy->getTypeArray();
1831 // Add return type.
Devang Patel48d190f2009-01-05 21:47:57 +00001832 DIDescriptor RTy = Elements.getElement(0);
1833 if (DIBasicType *BT = dyn_cast<DIBasicType>(&RTy))
1834 AddType(DW_Unit, &Buffer, *BT);
1835 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&RTy))
1836 AddType(DW_Unit, &Buffer, *DT);
1837 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&RTy))
1838 AddType(DW_Unit, &Buffer, *CT);
1839
1840 //AddType(DW_Unit, &Buffer, Elements.getElement(0));
Devang Patelf4215332009-01-05 19:55:51 +00001841 // Add arguments.
1842 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1843 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001844 DIDescriptor Ty = Elements.getElement(i);
1845 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1846 AddType(DW_Unit, &Buffer, *BT);
1847 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1848 AddType(DW_Unit, &Buffer, *DT);
1849 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1850 AddType(DW_Unit, &Buffer, *CT);
Devang Patelf4215332009-01-05 19:55:51 +00001851 Buffer.AddChild(Arg);
1852 }
1853 }
1854 break;
1855 case DW_TAG_structure_type:
1856 case DW_TAG_union_type:
1857 {
1858 // Add elements to structure type.
1859 DIArray Elements = CTy->getTypeArray();
1860 // Add elements to structure type.
1861 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1862 DIDescriptor Element = Elements.getElement(i);
1863 if (DISubprogram *SP = dyn_cast<DISubprogram>(&Element))
1864 ConstructFieldTypeDIE(DW_Unit, Buffer, SP);
1865 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Element))
1866 ConstructFieldTypeDIE(DW_Unit, Buffer, DT);
1867 else if (DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(&Element))
1868 ConstructFieldTypeDIE(DW_Unit, Buffer, GV);
1869 }
1870 }
1871 break;
1872 default:
1873 break;
1874 }
1875
1876 // Add name if not anonymous or intermediate type.
1877 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1878
1879 // Add size if non-zero (derived types might be zero-sized.)
1880 if (Size)
1881 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1882 else {
1883 // Add zero size even if it is not a forward declaration.
1884 // FIXME - Enable this.
1885 // if (!CTy->isDefinition())
1886 // AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1887 // else
1888 // AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1889 }
1890
1891 // Add source line info if available and TyDesc is not a forward
1892 // declaration.
1893 // FIXME - Enable this.
1894 // if (CTy->isForwardDecl())
1895 // AddSourceLine(&Buffer, *CTy);
1896 }
1897
Devang Patel68afdc32009-01-05 18:33:01 +00001898 // ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1899 void ConstructSubrangeDIE (DIE &Buffer, DISubrange *SR, DIE *IndexTy) {
1900 int64_t L = SR->getLo();
1901 int64_t H = SR->getHi();
1902 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1903 if (L != H) {
1904 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1905 if (L)
1906 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1907 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
1908 }
1909 Buffer.AddChild(DW_Subrange);
1910 }
1911
1912 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1913 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1914 DICompositeType *CTy) {
1915 Buffer.setTag(DW_TAG_array_type);
1916 if (CTy->getTag() == DW_TAG_vector_type)
1917 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1918
1919 DIArray Elements = CTy->getTypeArray();
1920 // FIXME - Enable this.
Devang Patel48d190f2009-01-05 21:47:57 +00001921 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001922
1923 // Construct an anonymous type for index type.
1924 DIE IdxBuffer(DW_TAG_base_type);
1925 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1926 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1927 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1928
1929 // Add subranges to array type.
1930 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001931 DIDescriptor Element = Elements.getElement(i);
1932 if (DISubrange *SR = dyn_cast<DISubrange>(&Element))
1933 ConstructSubrangeDIE(Buffer, SR, IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001934 }
1935 }
1936
Devang Patelc69bf2c2009-01-05 18:38:38 +00001937 /// ConstructEnumTypeDIE - Construct enum type DIE from
1938 /// DIEnumerator.
Devang Patelf4215332009-01-05 19:55:51 +00001939 void ConstructEnumTypeDIE(CompileUnit *DW_Unit,
1940 DIE &Buffer, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001941
1942 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1943 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1944 int64_t Value = ETy->getEnumValue();
1945 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1946 Buffer.AddChild(Enumerator);
1947 }
Devang Patel68afdc32009-01-05 18:33:01 +00001948
Devang Patel86ae1422009-01-05 18:59:44 +00001949 /// ConstructFieldTypeDIE - Construct variable DIE for a struct field.
1950 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1951 DIE &Buffer, DIGlobalVariable *V) {
1952
1953 DIE *VariableDie = new DIE(DW_TAG_variable);
1954 const std::string &LinkageName = V->getLinkageName();
1955 if (!LinkageName.empty())
1956 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1957 LinkageName);
1958 // FIXME - Enable this. AddSourceLine(VariableDie, V);
Devang Patel48d190f2009-01-05 21:47:57 +00001959 AddType(DW_Unit, VariableDie, V->getType());
Devang Patel86ae1422009-01-05 18:59:44 +00001960 if (!V->isLocalToUnit())
1961 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1962 AddUInt(VariableDie, DW_AT_declaration, DW_FORM_flag, 1);
1963 Buffer.AddChild(VariableDie);
1964 }
1965
1966 /// ConstructFieldTypeDIE - Construct subprogram DIE for a struct field.
1967 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1968 DIE &Buffer, DISubprogram *SP,
1969 bool IsConstructor = false) {
1970 DIE *Method = new DIE(DW_TAG_subprogram);
1971 AddString(Method, DW_AT_name, DW_FORM_string, SP->getName());
1972 const std::string &LinkageName = SP->getLinkageName();
1973 if (!LinkageName.empty())
1974 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1975 // FIXME - Enable this. AddSourceLine(Method, SP);
1976
1977 DICompositeType MTy = SP->getType();
1978 DIArray Args = MTy.getTypeArray();
1979
1980 // Add Return Type.
Devang Patel48d190f2009-01-05 21:47:57 +00001981 if (!IsConstructor) {
1982 DIDescriptor Ty = Args.getElement(0);
1983 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1984 AddType(DW_Unit, Method, *BT);
1985 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1986 AddType(DW_Unit, Method, *DT);
1987 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1988 AddType(DW_Unit, Method, *CT);
1989 }
Devang Patel86ae1422009-01-05 18:59:44 +00001990
1991 // Add arguments.
1992 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1993 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001994 DIDescriptor Ty = Args.getElement(i);
1995 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1996 AddType(DW_Unit, Method, *BT);
1997 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1998 AddType(DW_Unit, Method, *DT);
1999 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
2000 AddType(DW_Unit, Method, *CT);
Devang Patel86ae1422009-01-05 18:59:44 +00002001 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
2002 Method->AddChild(Arg);
2003 }
2004
2005 if (!SP->isLocalToUnit())
2006 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
2007 Buffer.AddChild(Method);
2008 }
2009
2010 /// COnstructFieldTypeDIE - Construct derived type DIE for a struct field.
2011 void ConstructFieldTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
2012 DIDerivedType *DTy) {
2013 unsigned Tag = DTy->getTag();
2014 DIE *MemberDie = new DIE(Tag);
2015 if (!DTy->getName().empty())
2016 AddString(MemberDie, DW_AT_name, DW_FORM_string, DTy->getName());
2017 // FIXME - Enable this. AddSourceLine(MemberDie, DTy);
2018
2019 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00002020 AddType(DW_Unit, MemberDie, FromTy);
Devang Patel86ae1422009-01-05 18:59:44 +00002021
2022 uint64_t Size = DTy->getSizeInBits();
2023 uint64_t Offset = DTy->getOffsetInBits();
2024
2025 // FIXME Handle bitfields
2026
2027 // Add size.
2028 AddUInt(MemberDie, DW_AT_bit_size, 0, Size);
2029 // Add computation for offset.
2030 DIEBlock *Block = new DIEBlock();
2031 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
2032 AddUInt(Block, 0, DW_FORM_udata, Offset >> 3);
2033 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
2034
2035 // FIXME Handle DW_AT_accessibility.
2036
2037 Buffer.AddChild(MemberDie);
2038 }
2039
Jim Laskeyef42a012006-11-02 20:12:39 +00002040 /// ConstructType - Adds all the required attributes to the type.
2041 ///
2042 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
2043 // Get core information.
2044 const std::string &Name = TyDesc->getName();
2045 uint64_t Size = TyDesc->getSize() >> 3;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002046
Jim Laskeyef42a012006-11-02 20:12:39 +00002047 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
2048 // Fundamental types like int, float, bool
2049 Buffer.setTag(DW_TAG_base_type);
2050 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
2051 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
Jim Laskey85f419b2006-11-09 16:32:26 +00002052 // Fetch tag.
2053 unsigned Tag = DerivedTy->getTag();
2054 // FIXME - Workaround for templates.
2055 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002056 // Pointers, typedefs et al.
Jim Laskey85f419b2006-11-09 16:32:26 +00002057 Buffer.setTag(Tag);
Jim Laskeyef42a012006-11-02 20:12:39 +00002058 // Map to main type, void will not have a type.
2059 if (TypeDesc *FromTy = DerivedTy->getFromType())
2060 AddType(&Buffer, FromTy, Unit);
2061 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
2062 // Fetch tag.
2063 unsigned Tag = CompTy->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002064
Jim Laskeyef42a012006-11-02 20:12:39 +00002065 // Set tag accordingly.
2066 if (Tag == DW_TAG_vector_type)
2067 Buffer.setTag(DW_TAG_array_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002068 else
Jim Laskeyef42a012006-11-02 20:12:39 +00002069 Buffer.setTag(Tag);
Jim Laskey65195462006-10-30 13:35:07 +00002070
Jim Laskeyef42a012006-11-02 20:12:39 +00002071 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002072
Jim Laskeyef42a012006-11-02 20:12:39 +00002073 switch (Tag) {
2074 case DW_TAG_vector_type:
2075 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
2076 // Fall thru
2077 case DW_TAG_array_type: {
2078 // Add element type.
2079 if (TypeDesc *FromTy = CompTy->getFromType())
2080 AddType(&Buffer, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002081
Jim Laskeyef42a012006-11-02 20:12:39 +00002082 // Don't emit size attribute.
2083 Size = 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002084
Jim Laskeyef42a012006-11-02 20:12:39 +00002085 // Construct an anonymous type for index type.
Devang Patel9ede3f22009-01-05 17:44:11 +00002086 DIE Buffer(DW_TAG_base_type);
2087 AddUInt(&Buffer, DW_AT_byte_size, 0, sizeof(int32_t));
2088 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
2089 DIE *IndexTy = Unit->AddDie(Buffer);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002090
Jim Laskeyef42a012006-11-02 20:12:39 +00002091 // Add subranges to array type.
Evan Chengf8480282008-12-09 17:56:30 +00002092 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002093 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
2094 int64_t Lo = SRD->getLo();
2095 int64_t Hi = SRD->getHi();
2096 DIE *Subrange = new DIE(DW_TAG_subrange_type);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002097
Jim Laskeyef42a012006-11-02 20:12:39 +00002098 // If a range is available.
2099 if (Lo != Hi) {
2100 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
2101 // Only add low if non-zero.
2102 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
2103 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
2104 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002105
Jim Laskeyef42a012006-11-02 20:12:39 +00002106 Buffer.AddChild(Subrange);
2107 }
2108 break;
2109 }
2110 case DW_TAG_structure_type:
2111 case DW_TAG_union_type: {
2112 // Add elements to structure type.
Evan Chengf8480282008-12-09 17:56:30 +00002113 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002114 DebugInfoDesc *Element = Elements[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002115
Jim Laskeyef42a012006-11-02 20:12:39 +00002116 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
2117 // Add field or base class.
Jim Laskeyef42a012006-11-02 20:12:39 +00002118 unsigned Tag = MemberDesc->getTag();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002119
Jim Laskeyef42a012006-11-02 20:12:39 +00002120 // Extract the basic information.
2121 const std::string &Name = MemberDesc->getName();
Jim Laskeyef42a012006-11-02 20:12:39 +00002122 uint64_t Size = MemberDesc->getSize();
2123 uint64_t Align = MemberDesc->getAlign();
2124 uint64_t Offset = MemberDesc->getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002125
Jim Laskeyef42a012006-11-02 20:12:39 +00002126 // Construct member debug information entry.
2127 DIE *Member = new DIE(Tag);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002128
Jim Laskeyef42a012006-11-02 20:12:39 +00002129 // Add name if not "".
2130 if (!Name.empty())
2131 AddString(Member, DW_AT_name, DW_FORM_string, Name);
Evan Chengf8480282008-12-09 17:56:30 +00002132
Jim Laskeyef42a012006-11-02 20:12:39 +00002133 // Add location if available.
2134 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002135
Jim Laskeyef42a012006-11-02 20:12:39 +00002136 // Most of the time the field info is the same as the members.
2137 uint64_t FieldSize = Size;
2138 uint64_t FieldAlign = Align;
2139 uint64_t FieldOffset = Offset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002140
Jim Laskeyee5f9272006-12-22 20:03:42 +00002141 // Set the member type.
2142 TypeDesc *FromTy = MemberDesc->getFromType();
2143 AddType(Member, FromTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002144
Jim Laskeyee5f9272006-12-22 20:03:42 +00002145 // Walk up typedefs until a real size is found.
2146 while (FromTy) {
2147 if (FromTy->getTag() != DW_TAG_typedef) {
2148 FieldSize = FromTy->getSize();
Devang Pateld0935c32008-12-23 21:55:38 +00002149 FieldAlign = FromTy->getAlign();
Jim Laskeyee5f9272006-12-22 20:03:42 +00002150 break;
2151 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002152
Dan Gohman275769a2007-07-23 20:24:29 +00002153 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
Jim Laskeyef42a012006-11-02 20:12:39 +00002154 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002155
Jim Laskeyef42a012006-11-02 20:12:39 +00002156 // Unless we have a bit field.
2157 if (Tag == DW_TAG_member && FieldSize != Size) {
2158 // Construct the alignment mask.
2159 uint64_t AlignMask = ~(FieldAlign - 1);
2160 // Determine the high bit + 1 of the declared size.
2161 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
2162 // Work backwards to determine the base offset of the field.
2163 FieldOffset = HiMark - FieldSize;
2164 // Now normalize offset to the field.
2165 Offset -= FieldOffset;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002166
Jim Laskeyef42a012006-11-02 20:12:39 +00002167 // Maybe we need to work from the other end.
2168 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002169
Jim Laskeyef42a012006-11-02 20:12:39 +00002170 // Add size and offset.
2171 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
2172 AddUInt(Member, DW_AT_bit_size, 0, Size);
2173 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
2174 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002175
Jim Laskeyef42a012006-11-02 20:12:39 +00002176 // Add computation for offset.
2177 DIEBlock *Block = new DIEBlock();
2178 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
2179 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
2180 AddBlock(Member, DW_AT_data_member_location, 0, Block);
2181
2182 // Add accessibility (public default unless is base class.
2183 if (MemberDesc->isProtected()) {
2184 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
2185 } else if (MemberDesc->isPrivate()) {
2186 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
2187 } else if (Tag == DW_TAG_inheritance) {
2188 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
2189 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002190
Jim Laskeyef42a012006-11-02 20:12:39 +00002191 Buffer.AddChild(Member);
2192 } else if (GlobalVariableDesc *StaticDesc =
2193 dyn_cast<GlobalVariableDesc>(Element)) {
2194 // Add static member.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002195
Jim Laskeyef42a012006-11-02 20:12:39 +00002196 // Construct member debug information entry.
2197 DIE *Static = new DIE(DW_TAG_variable);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002198
Jim Laskeyef42a012006-11-02 20:12:39 +00002199 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00002200 const std::string &Name = StaticDesc->getName();
2201 const std::string &LinkageName = StaticDesc->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00002202 AddString(Static, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002203 if (!LinkageName.empty()) {
2204 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
2205 LinkageName);
2206 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002207
Jim Laskeyef42a012006-11-02 20:12:39 +00002208 // Add location.
2209 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002210
Jim Laskeyef42a012006-11-02 20:12:39 +00002211 // Add type.
2212 if (TypeDesc *StaticTy = StaticDesc->getType())
2213 AddType(Static, StaticTy, Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002214
Jim Laskeyef42a012006-11-02 20:12:39 +00002215 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00002216 if (!StaticDesc->isStatic())
2217 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002218 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002219
Jim Laskeyef42a012006-11-02 20:12:39 +00002220 Buffer.AddChild(Static);
2221 } else if (SubprogramDesc *MethodDesc =
2222 dyn_cast<SubprogramDesc>(Element)) {
2223 // Add member function.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002224
Jim Laskeyef42a012006-11-02 20:12:39 +00002225 // Construct member debug information entry.
2226 DIE *Method = new DIE(DW_TAG_subprogram);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002227
Jim Laskeyef42a012006-11-02 20:12:39 +00002228 // Add name and mangled name.
Jim Laskey2172f962006-11-30 14:35:45 +00002229 const std::string &Name = MethodDesc->getName();
2230 const std::string &LinkageName = MethodDesc->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002231
2232 AddString(Method, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002233 bool IsCTor = TyDesc->getName() == Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002234
Jim Laskey2172f962006-11-30 14:35:45 +00002235 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002236 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00002237 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00002238 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002239
Jim Laskeyef42a012006-11-02 20:12:39 +00002240 // Add location.
2241 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002242
Jim Laskeyef42a012006-11-02 20:12:39 +00002243 // Add type.
2244 if (CompositeTypeDesc *MethodTy =
2245 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
2246 // Get argument information.
2247 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002248
Jim Laskeyef42a012006-11-02 20:12:39 +00002249 // If not a ctor.
2250 if (!IsCTor) {
2251 // Add return type.
2252 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
2253 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002254
Jim Laskeyef42a012006-11-02 20:12:39 +00002255 // Add arguments.
Evan Chengf8480282008-12-09 17:56:30 +00002256 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002257 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2258 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
2259 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
2260 Method->AddChild(Arg);
2261 }
2262 }
2263
2264 // Add flags.
Jim Laskey6488a072007-01-08 22:15:18 +00002265 if (!MethodDesc->isStatic())
2266 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002267 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002268
Jim Laskeyef42a012006-11-02 20:12:39 +00002269 Buffer.AddChild(Method);
2270 }
2271 }
2272 break;
2273 }
2274 case DW_TAG_enumeration_type: {
2275 // Add enumerators to enumeration type.
Evan Chengf8480282008-12-09 17:56:30 +00002276 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002277 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
2278 const std::string &Name = ED->getName();
2279 int64_t Value = ED->getValue();
2280 DIE *Enumerator = new DIE(DW_TAG_enumerator);
2281 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
2282 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
2283 Buffer.AddChild(Enumerator);
2284 }
2285
2286 break;
2287 }
2288 case DW_TAG_subroutine_type: {
2289 // Add prototype flag.
2290 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
2291 // Add return type.
2292 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002293
Jim Laskeyef42a012006-11-02 20:12:39 +00002294 // Add arguments.
Evan Chengf8480282008-12-09 17:56:30 +00002295 for (unsigned i = 1, N = Elements.size(); i < N; ++i) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002296 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2297 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
2298 Buffer.AddChild(Arg);
2299 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002300
Jim Laskeyef42a012006-11-02 20:12:39 +00002301 break;
2302 }
2303 default: break;
2304 }
2305 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002306
Jim Laskeyef42a012006-11-02 20:12:39 +00002307 // Add name if not anonymous or intermediate type.
2308 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Evan Chengf8480282008-12-09 17:56:30 +00002309
Evan Cheng94ea5be2008-12-10 00:15:44 +00002310 // Add size if non-zero (derived types might be zero-sized.)
2311 if (Size)
2312 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
2313 else if (isa<CompositeTypeDesc>(TyDesc)) {
2314 // If TyDesc is a composite type, then add size even if it's zero unless
2315 // it's a forward declaration.
2316 if (TyDesc->isForwardDecl())
2317 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
2318 else
2319 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
2320 }
2321
2322 // Add source line info if available and TyDesc is not a forward
2323 // declaration.
2324 if (!TyDesc->isForwardDecl())
2325 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
Jim Laskeyef42a012006-11-02 20:12:39 +00002326 }
2327
2328 /// NewCompileUnit - Create new compile unit and it's debug information entry.
Jim Laskey65195462006-10-30 13:35:07 +00002329 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002330 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
2331 // Construct debug information entry.
2332 DIE *Die = new DIE(DW_TAG_compile_unit);
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00002333 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2334 DWLabel("section_line", 0), DWLabel("section_line", 0), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002335 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
2336 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
2337 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
Devang Patel83580992008-12-12 21:57:54 +00002338 if (!UnitDesc->getDirectory().empty())
2339 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002340
Jim Laskeyef42a012006-11-02 20:12:39 +00002341 // Construct compile unit.
2342 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002343
Jim Laskeyef42a012006-11-02 20:12:39 +00002344 // Add Unit to compile unit map.
2345 DescToUnitMap[UnitDesc] = Unit;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002346
Jim Laskeyef42a012006-11-02 20:12:39 +00002347 return Unit;
2348 }
2349
Jim Laskey9d4209f2006-11-07 19:33:46 +00002350 /// GetBaseCompileUnit - Get the main compile unit.
2351 ///
2352 CompileUnit *GetBaseCompileUnit() const {
2353 CompileUnit *Unit = CompileUnits[0];
2354 assert(Unit && "Missing compile unit.");
2355 return Unit;
2356 }
2357
Jim Laskey65195462006-10-30 13:35:07 +00002358 /// FindCompileUnit - Get the compile unit for the given descriptor.
2359 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002360 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002361 CompileUnit *Unit = DescToUnitMap[UnitDesc];
Jim Laskeyef42a012006-11-02 20:12:39 +00002362 assert(Unit && "Missing compile unit.");
2363 return Unit;
2364 }
2365
Devang Patel8526cc02009-01-05 22:35:52 +00002366 /// FindCompileUnit - Get the compile unit for the given descriptor.
2367 ///
2368 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
2369 CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
2370 assert(DW_Unit && "Missing compile unit.");
2371 return DW_Unit;
2372 }
2373
Jim Laskeyef42a012006-11-02 20:12:39 +00002374 /// NewGlobalVariable - Add a new global variable DIE.
Jim Laskey65195462006-10-30 13:35:07 +00002375 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002376 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
2377 // Get the compile unit context.
2378 CompileUnitDesc *UnitDesc =
2379 static_cast<CompileUnitDesc *>(GVD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00002380 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002381
2382 // Check for pre-existence.
2383 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
2384 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002385
Jim Laskeyef42a012006-11-02 20:12:39 +00002386 // Get the global variable itself.
2387 GlobalVariable *GV = GVD->getGlobalVariable();
2388
Jim Laskey2172f962006-11-30 14:35:45 +00002389 const std::string &Name = GVD->getName();
2390 const std::string &FullName = GVD->getFullName();
2391 const std::string &LinkageName = GVD->getLinkageName();
Jim Laskeyef42a012006-11-02 20:12:39 +00002392 // Create the global's variable DIE.
2393 DIE *VariableDie = new DIE(DW_TAG_variable);
2394 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002395 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002396 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00002397 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00002398 }
Jim Laskey6488a072007-01-08 22:15:18 +00002399 AddType(VariableDie, GVD->getType(), Unit);
2400 if (!GVD->isStatic())
2401 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002402
Jim Laskeyef42a012006-11-02 20:12:39 +00002403 // Add source line info if available.
2404 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002405
Jim Laskeyef42a012006-11-02 20:12:39 +00002406 // Add address.
2407 DIEBlock *Block = new DIEBlock();
2408 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
Jim Laskey2172f962006-11-30 14:35:45 +00002409 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
2410 AddBlock(VariableDie, DW_AT_location, 0, Block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002411
Jim Laskeyef42a012006-11-02 20:12:39 +00002412 // Add to map.
2413 Slot = VariableDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002414
Jim Laskeyef42a012006-11-02 20:12:39 +00002415 // Add to context owner.
2416 Unit->getDie()->AddChild(VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002417
Jim Laskeyef42a012006-11-02 20:12:39 +00002418 // Expose as global.
2419 // FIXME - need to check external flag.
Jim Laskey2172f962006-11-30 14:35:45 +00002420 Unit->AddGlobal(FullName, VariableDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002421
Jim Laskeyef42a012006-11-02 20:12:39 +00002422 return VariableDie;
2423 }
Jim Laskey65195462006-10-30 13:35:07 +00002424
2425 /// NewSubprogram - Add a new subprogram DIE.
2426 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002427 DIE *NewSubprogram(SubprogramDesc *SPD) {
2428 // Get the compile unit context.
2429 CompileUnitDesc *UnitDesc =
2430 static_cast<CompileUnitDesc *>(SPD->getContext());
Jim Laskey5496f012006-11-09 14:52:14 +00002431 CompileUnit *Unit = GetBaseCompileUnit();
Jim Laskeyef42a012006-11-02 20:12:39 +00002432
2433 // Check for pre-existence.
2434 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
2435 if (Slot) return Slot;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002436
Jim Laskeyef42a012006-11-02 20:12:39 +00002437 // Gather the details (simplify add attribute code.)
Jim Laskey2172f962006-11-30 14:35:45 +00002438 const std::string &Name = SPD->getName();
2439 const std::string &FullName = SPD->getFullName();
2440 const std::string &LinkageName = SPD->getLinkageName();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002441
Jim Laskeyef42a012006-11-02 20:12:39 +00002442 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
2443 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
Jim Laskey2172f962006-11-30 14:35:45 +00002444 if (!LinkageName.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002445 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Jim Laskey2172f962006-11-30 14:35:45 +00002446 LinkageName);
Jim Laskeyef42a012006-11-02 20:12:39 +00002447 }
2448 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
Jim Laskey6488a072007-01-08 22:15:18 +00002449 if (!SPD->isStatic())
2450 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002451 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002452
Jim Laskeyef42a012006-11-02 20:12:39 +00002453 // Add source line info if available.
2454 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
2455
2456 // Add to map.
2457 Slot = SubprogramDie;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002458
Jim Laskeyef42a012006-11-02 20:12:39 +00002459 // Add to context owner.
2460 Unit->getDie()->AddChild(SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002461
Jim Laskeyef42a012006-11-02 20:12:39 +00002462 // Expose as global.
Jim Laskey2172f962006-11-30 14:35:45 +00002463 Unit->AddGlobal(FullName, SubprogramDie);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002464
Jim Laskeyef42a012006-11-02 20:12:39 +00002465 return SubprogramDie;
2466 }
Jim Laskey65195462006-10-30 13:35:07 +00002467
2468 /// NewScopeVariable - Create a new scope variable.
2469 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002470 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
2471 // Get the descriptor.
2472 VariableDesc *VD = DV->getDesc();
2473
2474 // Translate tag to proper Dwarf tag. The result variable is dropped for
2475 // now.
2476 unsigned Tag;
2477 switch (VD->getTag()) {
2478 case DW_TAG_return_variable: return NULL;
2479 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2480 case DW_TAG_auto_variable: // fall thru
2481 default: Tag = DW_TAG_variable; break;
2482 }
2483
2484 // Define variable debug information entry.
2485 DIE *VariableDie = new DIE(Tag);
2486 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2487
2488 // Add source line info if available.
2489 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002490
Jim Laskeyef42a012006-11-02 20:12:39 +00002491 // Add variable type.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002492 AddType(VariableDie, VD->getType(), Unit);
2493
Jim Laskeyef42a012006-11-02 20:12:39 +00002494 // Add variable address.
2495 MachineLocation Location;
Evan Cheng72bebb92008-01-31 03:37:28 +00002496 Location.set(RI->getFrameRegister(*MF),
2497 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Jim Laskeyef42a012006-11-02 20:12:39 +00002498 AddAddress(VariableDie, DW_AT_location, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002499
Jim Laskeyef42a012006-11-02 20:12:39 +00002500 return VariableDie;
2501 }
Jim Laskey65195462006-10-30 13:35:07 +00002502
Devang Patel7a6e5a32009-01-08 02:33:41 +00002503 /// NewScopeVariable - Create a new scope variable.
2504 ///
2505 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
2506 // Get the descriptor.
2507 DIVariable *VD = DV->getVariable();
2508
2509 // Translate tag to proper Dwarf tag. The result variable is dropped for
2510 // now.
2511 unsigned Tag;
2512 switch (VD->getTag()) {
2513 case DW_TAG_return_variable: return NULL;
2514 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2515 case DW_TAG_auto_variable: // fall thru
2516 default: Tag = DW_TAG_variable; break;
2517 }
2518
2519 // Define variable debug information entry.
2520 DIE *VariableDie = new DIE(Tag);
2521 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2522
2523 // Add source line info if available.
2524 AddSourceLine(VariableDie, VD);
2525
2526 // Add variable type.
2527 AddType(Unit, VariableDie, VD->getType());
2528
2529 // Add variable address.
2530 MachineLocation Location;
2531 Location.set(RI->getFrameRegister(*MF),
2532 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2533 AddAddress(VariableDie, DW_AT_location, Location);
2534
2535 return VariableDie;
2536 }
2537
Devang Patel9f8fcfc2009-01-08 17:19:22 +00002538 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
2539 CompileUnit *Unit = DW_CUs[V];
2540 assert (Unit && "Unable to find CompileUnit");
Devang Patele9bfb0f2009-01-12 18:41:00 +00002541 unsigned ID = MMI->NextLabelID();
Devang Patel9f8fcfc2009-01-08 17:19:22 +00002542 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
2543 return ID;
2544 }
2545
2546 unsigned getRecordSourceLineCount() {
2547 return Lines.size();
2548 }
2549
2550 unsigned RecordSource(const std::string &Directory,
2551 const std::string &File) {
2552 unsigned DID = Directories.insert(Directory);
2553 return SrcFiles.insert(SrcFileInfo(DID,File));
2554 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00002555
Devang Patel2ec78c42009-01-08 02:49:34 +00002556 /// RecordRegionStart - Indicate the start of a region.
2557 ///
2558 unsigned RecordRegionStart(GlobalVariable *V) {
2559 DbgScope *Scope = getOrCreateScope(V);
Devang Patele9bfb0f2009-01-12 18:41:00 +00002560 unsigned ID = MMI->NextLabelID();
Devang Patel2ec78c42009-01-08 02:49:34 +00002561 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
2562 return ID;
2563 }
2564
2565 /// RecordRegionEnd - Indicate the end of a region.
2566 ///
2567 unsigned RecordRegionEnd(GlobalVariable *V) {
2568 DbgScope *Scope = getOrCreateScope(V);
Devang Patele9bfb0f2009-01-12 18:41:00 +00002569 unsigned ID = MMI->NextLabelID();
Devang Patel2ec78c42009-01-08 02:49:34 +00002570 Scope->setEndLabelID(ID);
2571 return ID;
2572 }
2573
2574 /// RecordVariable - Indicate the declaration of a local variable.
2575 ///
2576 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
2577 DbgScope *Scope = getOrCreateScope(GV);
2578 DIVariable *VD = new DIVariable(GV);
2579 DbgVariable *DV = new DbgVariable(VD, FrameIndex);
2580 Scope->AddVariable(DV);
2581 }
2582
Devang Patel7a6e5a32009-01-08 02:33:41 +00002583 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2584 ///
2585 DbgScope *getOrCreateScope(GlobalVariable *V) {
2586 DbgScope *&Slot = DbgScopeMap[V];
2587 if (!Slot) {
2588 // FIXME - breaks down when the context is an inlined function.
2589 DIDescriptor ParentDesc;
Devang Patel0dc969e2009-01-10 02:34:18 +00002590 DIDescriptor *DB = new DIBlock(V);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002591 if (DIBlock *Block = dyn_cast<DIBlock>(DB)) {
2592 ParentDesc = Block->getContext();
2593 }
2594 DbgScope *Parent = ParentDesc.isNull() ?
Devang Patel0dc969e2009-01-10 02:34:18 +00002595 NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002596 Slot = new DbgScope(Parent, DB);
2597 if (Parent) {
2598 Parent->AddScope(Slot);
2599 } else if (RootDbgScope) {
2600 // FIXME - Add inlined function scopes to the root so we can delete
2601 // them later. Long term, handle inlined functions properly.
2602 RootDbgScope->AddScope(Slot);
2603 } else {
2604 // First function is top level function.
2605 RootDbgScope = Slot;
2606 }
2607 }
2608 return Slot;
2609 }
2610
2611 /// ConstructDbgScope - Construct the components of a scope.
2612 ///
2613 void ConstructDbgScope(DbgScope *ParentScope,
2614 unsigned ParentStartID, unsigned ParentEndID,
2615 DIE *ParentDie, CompileUnit *Unit) {
2616 // Add variables to scope.
Devang Patel9795da52009-01-10 02:42:49 +00002617 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002618 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2619 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2620 if (VariableDie) ParentDie->AddChild(VariableDie);
2621 }
2622
2623 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002624 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002625 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2626 // Define the Scope debug information entry.
2627 DbgScope *Scope = Scopes[j];
2628 // FIXME - Ignore inlined functions for the time being.
2629 if (!Scope->getParent()) continue;
2630
Devang Patele9bfb0f2009-01-12 18:41:00 +00002631 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2632 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002633
2634 // Ignore empty scopes.
2635 if (StartID == EndID && StartID != 0) continue;
2636 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2637
2638 if (StartID == ParentStartID && EndID == ParentEndID) {
2639 // Just add stuff to the parent scope.
2640 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2641 } else {
2642 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2643
2644 // Add the scope bounds.
2645 if (StartID) {
2646 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2647 DWLabel("label", StartID));
2648 } else {
2649 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2650 DWLabel("func_begin", SubprogramCount));
2651 }
2652 if (EndID) {
2653 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2654 DWLabel("label", EndID));
2655 } else {
2656 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2657 DWLabel("func_end", SubprogramCount));
2658 }
2659
2660 // Add the scope contents.
2661 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2662 ParentDie->AddChild(ScopeDie);
2663 }
2664 }
2665 }
2666
2667 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2668 ///
2669 void ConstructRootDbgScope(DbgScope *RootScope) {
2670 // Exit if there is no root scope.
2671 if (!RootScope) return;
2672
2673 // Get the subprogram debug information entry.
2674 DISubprogram *SPD = cast<DISubprogram>(RootScope->getDesc());
2675
2676 // Get the compile unit context.
2677 CompileUnit *Unit = FindCompileUnit(SPD->getCompileUnit());
2678
2679 // Get the subprogram die.
2680 DIE *SPDie = Unit->getDieMapSlotFor(SPD->getGV());
2681 assert(SPDie && "Missing subprogram descriptor");
2682
2683 // Add the function bounds.
2684 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2685 DWLabel("func_begin", SubprogramCount));
2686 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2687 DWLabel("func_end", SubprogramCount));
2688 MachineLocation Location(RI->getFrameRegister(*MF));
2689 AddAddress(SPDie, DW_AT_frame_base, Location);
2690
2691 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2692 }
2693
2694 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2695 ///
2696 void ConstructDefaultDbgScope(MachineFunction *MF) {
2697 // Find the correct subprogram descriptor.
2698 std::string SPName = "llvm.dbg.subprograms";
2699 std::vector<GlobalVariable*> Result;
2700 getGlobalVariablesUsing(*M, SPName, Result);
2701 for (std::vector<GlobalVariable *>::iterator I = Result.begin(),
2702 E = Result.end(); I != E; ++I) {
2703
2704 DISubprogram *SPD = new DISubprogram(*I);
2705
2706 if (SPD->getName() == MF->getFunction()->getName()) {
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
2720 MachineLocation Location(RI->getFrameRegister(*MF));
2721 AddAddress(SPDie, DW_AT_frame_base, Location);
2722 return;
2723 }
2724 }
2725#if 0
2726 // FIXME: This is causing an abort because C++ mangled names are compared
2727 // with their unmangled counterparts. See PR2885. Don't do this assert.
2728 assert(0 && "Couldn't find DIE for machine function!");
2729#endif
2730 }
2731
Jim Laskey65195462006-10-30 13:35:07 +00002732 /// ConstructScope - Construct the components of a scope.
2733 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002734 void ConstructScope(DebugScope *ParentScope,
Jim Laskey36729dd2006-11-29 16:55:57 +00002735 unsigned ParentStartID, unsigned ParentEndID,
2736 DIE *ParentDie, CompileUnit *Unit) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002737 // Add variables to scope.
2738 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
2739 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2740 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
2741 if (VariableDie) ParentDie->AddChild(VariableDie);
2742 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002743
Jim Laskeyef42a012006-11-02 20:12:39 +00002744 // Add nested scopes.
2745 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
2746 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2747 // Define the Scope debug information entry.
2748 DebugScope *Scope = Scopes[j];
2749 // FIXME - Ignore inlined functions for the time being.
2750 if (!Scope->getParent()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002751
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002752 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2753 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Jim Laskey5496f012006-11-09 14:52:14 +00002754
Jim Laskey9d4209f2006-11-07 19:33:46 +00002755 // Ignore empty scopes.
2756 if (StartID == EndID && StartID != 0) continue;
2757 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002758
Jim Laskey36729dd2006-11-29 16:55:57 +00002759 if (StartID == ParentStartID && EndID == ParentEndID) {
2760 // Just add stuff to the parent scope.
2761 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002762 } else {
Jim Laskey36729dd2006-11-29 16:55:57 +00002763 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002764
Jim Laskey36729dd2006-11-29 16:55:57 +00002765 // Add the scope bounds.
2766 if (StartID) {
2767 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00002768 DWLabel("label", StartID));
Jim Laskey36729dd2006-11-29 16:55:57 +00002769 } else {
2770 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2771 DWLabel("func_begin", SubprogramCount));
2772 }
2773 if (EndID) {
2774 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
Jim Laskeybacd3042007-02-21 22:48:45 +00002775 DWLabel("label", EndID));
Jim Laskey36729dd2006-11-29 16:55:57 +00002776 } else {
2777 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2778 DWLabel("func_end", SubprogramCount));
2779 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002780
Jim Laskey36729dd2006-11-29 16:55:57 +00002781 // Add the scope contents.
2782 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
2783 ParentDie->AddChild(ScopeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00002784 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002785 }
2786 }
Jim Laskey65195462006-10-30 13:35:07 +00002787
2788 /// ConstructRootScope - Construct the scope for the subprogram.
2789 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002790 void ConstructRootScope(DebugScope *RootScope) {
2791 // Exit if there is no root scope.
2792 if (!RootScope) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002793
2794 // Get the subprogram debug information entry.
Jim Laskeyef42a012006-11-02 20:12:39 +00002795 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002796
Jim Laskeyef42a012006-11-02 20:12:39 +00002797 // Get the compile unit context.
Jim Laskey5496f012006-11-09 14:52:14 +00002798 CompileUnit *Unit = GetBaseCompileUnit();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002799
Jim Laskeyef42a012006-11-02 20:12:39 +00002800 // Get the subprogram die.
2801 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2802 assert(SPDie && "Missing subprogram descriptor");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002803
Jim Laskeyef42a012006-11-02 20:12:39 +00002804 // Add the function bounds.
2805 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2806 DWLabel("func_begin", SubprogramCount));
2807 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2808 DWLabel("func_end", SubprogramCount));
2809 MachineLocation Location(RI->getFrameRegister(*MF));
2810 AddAddress(SPDie, DW_AT_frame_base, Location);
Jim Laskey5496f012006-11-09 14:52:14 +00002811
Jim Laskey36729dd2006-11-29 16:55:57 +00002812 ConstructScope(RootScope, 0, 0, SPDie, Unit);
Jim Laskeyef42a012006-11-02 20:12:39 +00002813 }
Jim Laskey65195462006-10-30 13:35:07 +00002814
Bill Wendlingd751c642008-09-26 00:28:12 +00002815 /// ConstructDefaultScope - Construct a default scope for the subprogram.
2816 ///
2817 void ConstructDefaultScope(MachineFunction *MF) {
2818 // Find the correct subprogram descriptor.
2819 std::vector<SubprogramDesc *> Subprograms;
2820 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
2821
2822 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2823 SubprogramDesc *SPD = Subprograms[i];
2824
2825 if (SPD->getName() == MF->getFunction()->getName()) {
2826 // Get the compile unit context.
2827 CompileUnit *Unit = GetBaseCompileUnit();
2828
2829 // Get the subprogram die.
2830 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2831 assert(SPDie && "Missing subprogram descriptor");
2832
2833 // Add the function bounds.
2834 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2835 DWLabel("func_begin", SubprogramCount));
2836 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2837 DWLabel("func_end", SubprogramCount));
2838
2839 MachineLocation Location(RI->getFrameRegister(*MF));
2840 AddAddress(SPDie, DW_AT_frame_base, Location);
2841 return;
2842 }
2843 }
Bill Wendlinga9f0cc42008-10-17 18:48:57 +00002844#if 0
2845 // FIXME: This is causing an abort because C++ mangled names are compared
2846 // with their unmangled counterparts. See PR2885. Don't do this assert.
Bill Wendlingd751c642008-09-26 00:28:12 +00002847 assert(0 && "Couldn't find DIE for machine function!");
Bill Wendlinga9f0cc42008-10-17 18:48:57 +00002848#endif
Bill Wendlingd751c642008-09-26 00:28:12 +00002849 }
2850
Jim Laskeyef42a012006-11-02 20:12:39 +00002851 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2852 /// tools to recognize the object file contains Dwarf information.
2853 void EmitInitial() {
2854 // Check to see if we already emitted intial headers.
2855 if (didInitial) return;
2856 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002857
Jim Laskeyef42a012006-11-02 20:12:39 +00002858 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002859 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002860 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002861 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002862 }
2863 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2864 EmitLabel("section_info", 0);
2865 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2866 EmitLabel("section_abbrev", 0);
2867 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2868 EmitLabel("section_aranges", 0);
2869 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2870 EmitLabel("section_macinfo", 0);
2871 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2872 EmitLabel("section_line", 0);
2873 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2874 EmitLabel("section_loc", 0);
2875 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2876 EmitLabel("section_pubnames", 0);
2877 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2878 EmitLabel("section_str", 0);
2879 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2880 EmitLabel("section_ranges", 0);
2881
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002882 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002883 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002884 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002885 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002886 }
2887
Jim Laskey65195462006-10-30 13:35:07 +00002888 /// EmitDIE - Recusively Emits a debug information entry.
2889 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002890 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002891 // Get the abbreviation for this DIE.
2892 unsigned AbbrevNumber = Die->getAbbrevNumber();
2893 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002894
Jim Laskeybacd3042007-02-21 22:48:45 +00002895 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002896
2897 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002898 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002899
2900 if (VerboseAsm)
2901 Asm->EOL(std::string("Abbrev [" +
2902 utostr(AbbrevNumber) +
2903 "] 0x" + utohexstr(Die->getOffset()) +
2904 ":0x" + utohexstr(Die->getSize()) + " " +
2905 TagString(Abbrev->getTag())));
2906 else
2907 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002908
Owen Anderson873e1b52008-06-24 21:44:59 +00002909 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2910 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002911
Jim Laskeyef42a012006-11-02 20:12:39 +00002912 // Emit the DIE attribute values.
2913 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2914 unsigned Attr = AbbrevData[i].getAttribute();
2915 unsigned Form = AbbrevData[i].getForm();
2916 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002917
Jim Laskeyef42a012006-11-02 20:12:39 +00002918 switch (Attr) {
2919 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002920 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002921 break;
2922 }
2923 default: {
2924 // Emit an attribute using the defined form.
2925 Values[i]->EmitValue(*this, Form);
2926 break;
2927 }
2928 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002929
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002930 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002931 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002932
Jim Laskeyef42a012006-11-02 20:12:39 +00002933 // Emit the DIE children if any.
2934 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2935 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002936
Jim Laskeyef42a012006-11-02 20:12:39 +00002937 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2938 EmitDIE(Children[j]);
2939 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002940
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002941 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002942 }
2943 }
2944
Jim Laskey65195462006-10-30 13:35:07 +00002945 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2946 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002947 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2948 // Get the children.
2949 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002950
Jim Laskeyef42a012006-11-02 20:12:39 +00002951 // If not last sibling and has children then add sibling offset attribute.
2952 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2953
2954 // Record the abbreviation.
2955 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002956
Jim Laskeyef42a012006-11-02 20:12:39 +00002957 // Get the abbreviation for this DIE.
2958 unsigned AbbrevNumber = Die->getAbbrevNumber();
2959 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2960
2961 // Set DIE offset
2962 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002963
Jim Laskeyef42a012006-11-02 20:12:39 +00002964 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002965 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2966
Owen Anderson873e1b52008-06-24 21:44:59 +00002967 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2968 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002969
2970 // Size the DIE attribute values.
2971 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2972 // Size attribute value.
2973 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2974 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002975
Jim Laskeyef42a012006-11-02 20:12:39 +00002976 // Size the DIE children if any.
2977 if (!Children.empty()) {
2978 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2979 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002980
Jim Laskeyef42a012006-11-02 20:12:39 +00002981 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2982 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2983 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002984
Jim Laskeyef42a012006-11-02 20:12:39 +00002985 // End of children marker.
2986 Offset += sizeof(int8_t);
2987 }
2988
2989 Die->setSize(Offset - Die->getOffset());
2990 return Offset;
2991 }
Jim Laskey65195462006-10-30 13:35:07 +00002992
2993 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2994 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002995 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002996 // Process base compile unit.
2997 CompileUnit *Unit = GetBaseCompileUnit();
2998 // Compute size of compile unit header
2999 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
3000 sizeof(int16_t) + // DWARF version number
3001 sizeof(int32_t) + // Offset Into Abbrev. Section
3002 sizeof(int8_t); // Pointer Size (in bytes)
3003 SizeAndOffsetDie(Unit->getDie(), Offset, true);
Jim Laskeyef42a012006-11-02 20:12:39 +00003004 }
3005
Jim Laskey65195462006-10-30 13:35:07 +00003006 /// EmitDebugInfo - Emit the debug info section.
3007 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003008 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003009 // Start debug info section.
3010 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003011
Jim Laskey5496f012006-11-09 14:52:14 +00003012 CompileUnit *Unit = GetBaseCompileUnit();
3013 DIE *Die = Unit->getDie();
3014 // Emit the compile units header.
3015 EmitLabel("info_begin", Unit->getID());
3016 // Emit size of content not including length itself
3017 unsigned ContentSize = Die->getSize() +
3018 sizeof(int16_t) + // DWARF version number
3019 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskey749b01d2006-11-30 11:09:42 +00003020 sizeof(int8_t) + // Pointer Size (in bytes)
3021 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003022
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003023 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
3024 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00003025 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003026 Asm->EOL("Offset Into Abbrev. Section");
Dan Gohman82482942007-09-27 23:12:31 +00003027 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003028
Jim Laskey5496f012006-11-09 14:52:14 +00003029 EmitDIE(Die);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003030 // FIXME - extra padding for gdb bug.
3031 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3032 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3033 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
3034 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Jim Laskey5496f012006-11-09 14:52:14 +00003035 EmitLabel("info_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003036
Jim Laskeybacd3042007-02-21 22:48:45 +00003037 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003038 }
3039
Jim Laskey65195462006-10-30 13:35:07 +00003040 /// EmitAbbreviations - Emit the abbreviation section.
3041 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003042 void EmitAbbreviations() const {
3043 // Check to see if it is worth the effort.
3044 if (!Abbreviations.empty()) {
3045 // Start the debug abbrev section.
3046 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003047
Jim Laskeyef42a012006-11-02 20:12:39 +00003048 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003049
Jim Laskeyef42a012006-11-02 20:12:39 +00003050 // For each abbrevation.
3051 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3052 // Get abbreviation data
3053 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003054
Jim Laskeyef42a012006-11-02 20:12:39 +00003055 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003056 Asm->EmitULEB128Bytes(Abbrev->getNumber());
3057 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003058
Jim Laskeyef42a012006-11-02 20:12:39 +00003059 // Emit the abbreviations data.
3060 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003061
Jim Laskeybacd3042007-02-21 22:48:45 +00003062 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003063 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003064
Jim Laskey7b1b39d2007-02-22 18:22:42 +00003065 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00003066 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00003067
Jim Laskeyef42a012006-11-02 20:12:39 +00003068 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003069
Jim Laskeybacd3042007-02-21 22:48:45 +00003070 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003071 }
3072 }
3073
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003074 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
3075 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003076 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003077 void EmitEndOfLineMatrix(unsigned SectionEnd) {
3078 // Define last address of section.
3079 Asm->EmitInt8(0); Asm->EOL("Extended Op");
3080 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
3081 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
3082 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
3083
3084 // Mark end of matrix.
3085 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
3086 Asm->EmitULEB128Bytes(1); Asm->EOL();
3087 Asm->EmitInt8(1); Asm->EOL();
3088 }
3089
Jim Laskey65195462006-10-30 13:35:07 +00003090 /// EmitDebugLines - Emit source line information.
3091 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003092 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003093 // If the target is using .loc/.file, the assembler will be emitting the
3094 // .debug_line table automatically.
3095 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00003096 return;
3097
Jim Laskeyef42a012006-11-02 20:12:39 +00003098 // Minimum line delta, thus ranging from -10..(255-10).
3099 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
3100 // Maximum line delta, thus ranging from -10..(255-10).
3101 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00003102
Jim Laskeyef42a012006-11-02 20:12:39 +00003103 // Start the dwarf line section.
3104 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003105
Jim Laskeyef42a012006-11-02 20:12:39 +00003106 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003107
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003108 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003109 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00003110 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003111
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003112 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003113
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003114 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003115 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00003116 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003117
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003118 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00003119
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003120 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00003121
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003122 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003123
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003124 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00003125
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003126 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003127
Jim Laskeyef42a012006-11-02 20:12:39 +00003128 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003129 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
3130 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
3131 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
3132 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
3133 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
3134 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
3135 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
3136 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
3137 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00003138
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003139 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Evan Cheng6547e402008-07-01 23:18:29 +00003140 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
Jim Laskeyef42a012006-11-02 20:12:39 +00003141
3142 // Emit directories.
3143 for (unsigned DirectoryID = 1, NDID = Directories.size();
3144 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003145 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00003146 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003147 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003148
Jim Laskeyef42a012006-11-02 20:12:39 +00003149 // Emit files.
3150 for (unsigned SourceID = 1, NSID = SourceFiles.size();
3151 SourceID <= NSID; ++SourceID) {
3152 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003153 Asm->EmitString(SourceFile.getName());
3154 Asm->EOL("Source");
3155 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
3156 Asm->EOL("Directory #");
3157 Asm->EmitULEB128Bytes(0);
3158 Asm->EOL("Mod date");
3159 Asm->EmitULEB128Bytes(0);
3160 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00003161 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003162 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003163
Jim Laskeyef42a012006-11-02 20:12:39 +00003164 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003165
Jim Laskeyef42a012006-11-02 20:12:39 +00003166 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003167 unsigned SecSrcLinesSize = SectionSourceLines.size();
3168
3169 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003170 // Isolate current sections line info.
3171 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00003172
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003173 if (VerboseAsm) {
3174 const Section* S = SectionMap[j + 1];
3175 Asm->EOL(std::string("Section ") + S->getName());
3176 } else
Evan Cheng6547e402008-07-01 23:18:29 +00003177 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003178
3179 // Dwarf assumes we start with first line of first source file.
3180 unsigned Source = 1;
3181 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003182
Jim Laskeyef42a012006-11-02 20:12:39 +00003183 // Construct rows of the address, source, line, column matrix.
3184 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3185 const SourceLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003186 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00003187 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003188
Jim Laskey1a4a83c2007-01-25 15:45:58 +00003189 unsigned SourceID = LineInfo.getSourceID();
3190 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
3191 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng6547e402008-07-01 23:18:29 +00003192 if (VerboseAsm)
3193 Asm->EOL(Directories[DirectoryID]
3194 + SourceFile.getName()
3195 + ":"
3196 + utostr_32(LineInfo.getLine()));
3197 else
3198 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003199
3200 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003201 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00003202 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003203 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00003204 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003205
Jim Laskeyef42a012006-11-02 20:12:39 +00003206 // If change of source, then switch to the new source.
3207 if (Source != LineInfo.getSourceID()) {
3208 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003209 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
3210 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00003211 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003212
Jim Laskeyef42a012006-11-02 20:12:39 +00003213 // If change of line.
3214 if (Line != LineInfo.getLine()) {
3215 // Determine offset.
3216 int Offset = LineInfo.getLine() - Line;
3217 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003218
Jim Laskeyef42a012006-11-02 20:12:39 +00003219 // Update line.
3220 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003221
Jim Laskeyef42a012006-11-02 20:12:39 +00003222 // If delta is small enough and in range...
3223 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3224 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003225 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00003226 } else {
3227 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003228 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
3229 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
3230 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00003231 }
3232 } else {
3233 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003234 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00003235 }
3236 }
3237
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003238 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00003239 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00003240
3241 if (SecSrcLinesSize == 0)
3242 // Because we're emitting a debug_line section, we still need a line
3243 // table. The linker and friends expect it to exist. If there's nothing to
3244 // put into it, emit an empty table.
3245 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003246
Jim Laskeyef42a012006-11-02 20:12:39 +00003247 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003248
Jim Laskeybacd3042007-02-21 22:48:45 +00003249 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003250 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003251
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003252 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00003253 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003254 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00003255 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00003256 return;
3257
3258 int stackGrowth =
3259 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3260 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003261 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00003262
3263 // Start the dwarf frame section.
3264 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
3265
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003266 EmitLabel("debug_frame_common", 0);
3267 EmitDifference("debug_frame_common_end", 0,
3268 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003269 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00003270
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003271 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003272 Asm->EmitInt32((int)DW_CIE_ID);
3273 Asm->EOL("CIE Identifier Tag");
3274 Asm->EmitInt8(DW_CIE_VERSION);
3275 Asm->EOL("CIE Version");
3276 Asm->EmitString("");
3277 Asm->EOL("CIE Augmentation");
3278 Asm->EmitULEB128Bytes(1);
3279 Asm->EOL("CIE Code Alignment Factor");
3280 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003281 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003282 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003283 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003284
Jim Laskey5e73d5b2007-01-24 18:45:13 +00003285 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00003286 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003287
Dale Johannesenb97aec62007-11-13 19:13:01 +00003288 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00003289
Evan Cheng05548eb2008-02-29 19:36:59 +00003290 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003291 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003292
Jim Laskeybacd3042007-02-21 22:48:45 +00003293 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003294 }
3295
Jim Laskey65195462006-10-30 13:35:07 +00003296 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
3297 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003298 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00003299 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00003300 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003301
Jim Laskeyef42a012006-11-02 20:12:39 +00003302 // Start the dwarf frame section.
3303 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003304
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003305 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
3306 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003307 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003308
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003309 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003310
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003311 EmitSectionOffset("debug_frame_common", "section_debug_frame",
3312 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003313 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00003314
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003315 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003316 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003317 EmitDifference("func_end", DebugFrameInfo.Number,
3318 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003319 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003320
Dale Johannesenb97aec62007-11-13 19:13:01 +00003321 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003322
Evan Cheng05548eb2008-02-29 19:36:59 +00003323 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003324 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00003325
Jim Laskeybacd3042007-02-21 22:48:45 +00003326 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003327 }
3328
3329 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00003330 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003331 void EmitDebugPubNames() {
3332 // Start the dwarf pubnames section.
3333 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003334
3335 CompileUnit *Unit = GetBaseCompileUnit();
3336
Jim Laskey5496f012006-11-09 14:52:14 +00003337 EmitDifference("pubnames_end", Unit->getID(),
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003338 "pubnames_begin", Unit->getID(), true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003339 Asm->EOL("Length of Public Names Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003340
Jim Laskey5496f012006-11-09 14:52:14 +00003341 EmitLabel("pubnames_begin", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003342
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003343 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003344
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00003345 EmitSectionOffset("info_begin", "section_info",
3346 Unit->getID(), 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003347 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00003348
Jim Laskey2b4e98c2006-12-06 17:43:18 +00003349 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003350 Asm->EOL("Compilation Unit Length");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003351
Jim Laskey5496f012006-11-09 14:52:14 +00003352 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003353
Jim Laskey5496f012006-11-09 14:52:14 +00003354 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
3355 GE = Globals.end();
3356 GI != GE; ++GI) {
3357 const std::string &Name = GI->first;
3358 DIE * Entity = GI->second;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003359
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003360 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
3361 Asm->EmitString(Name); Asm->EOL("External Name");
Jim Laskeyef42a012006-11-02 20:12:39 +00003362 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003363
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003364 Asm->EmitInt32(0); Asm->EOL("End Mark");
Jim Laskey5496f012006-11-09 14:52:14 +00003365 EmitLabel("pubnames_end", Unit->getID());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003366
Jim Laskeybacd3042007-02-21 22:48:45 +00003367 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003368 }
3369
3370 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00003371 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003372 void EmitDebugStr() {
3373 // Check to see if it is worth the effort.
3374 if (!StringPool.empty()) {
3375 // Start the dwarf str section.
3376 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003377
Jim Laskeyef42a012006-11-02 20:12:39 +00003378 // For each of strings in the string pool.
3379 for (unsigned StringID = 1, N = StringPool.size();
3380 StringID <= N; ++StringID) {
3381 // Emit a label for reference from debug information entries.
3382 EmitLabel("string", StringID);
3383 // Emit the string itself.
3384 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00003385 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003386 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003387
Jim Laskeybacd3042007-02-21 22:48:45 +00003388 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003389 }
3390 }
3391
3392 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00003393 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003394 void EmitDebugLoc() {
3395 // Start the dwarf loc section.
3396 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
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 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00003402 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003403 void EmitDebugARanges() {
3404 // Start the dwarf aranges section.
3405 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003406
Jim Laskeyef42a012006-11-02 20:12:39 +00003407 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00003408#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003409 CompileUnit *Unit = GetBaseCompileUnit();
3410
Jim Laskey5496f012006-11-09 14:52:14 +00003411 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003412 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003413
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003414 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003415
Jim Laskey5496f012006-11-09 14:52:14 +00003416 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003417 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00003418
Dan Gohman82482942007-09-27 23:12:31 +00003419 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00003420
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003421 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00003422
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003423 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
3424 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00003425
Jim Laskey5496f012006-11-09 14:52:14 +00003426 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003427 EmitReference("text_begin", 0); Asm->EOL("Address");
3428 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00003429
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003430 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
3431 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00003432#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003433
Jim Laskeybacd3042007-02-21 22:48:45 +00003434 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003435 }
3436
3437 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00003438 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003439 void EmitDebugRanges() {
3440 // Start the dwarf ranges section.
3441 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003442
Jim Laskeybacd3042007-02-21 22:48:45 +00003443 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003444 }
3445
3446 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00003447 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003448 void EmitDebugMacInfo() {
3449 // Start the dwarf macinfo section.
3450 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003451
Jim Laskeybacd3042007-02-21 22:48:45 +00003452 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00003453 }
3454
Devang Patelc4523242009-01-05 23:11:11 +00003455 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00003456 void ConstructCompileUnits() {
3457 std::string CUName = "llvm.dbg.compile_units";
3458 std::vector<GlobalVariable*> Result;
3459 getGlobalVariablesUsing(*M, CUName, Result);
3460 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3461 RE = Result.end(); RI != RE; ++RI) {
3462 DICompileUnit *DIUnit = new DICompileUnit(*RI);
Devang Patel9f8fcfc2009-01-08 17:19:22 +00003463 unsigned ID = RecordSource(DIUnit->getDirectory(),
3464 DIUnit->getFilename());
Devang Pateld1ca9252009-01-05 23:03:32 +00003465
3466 DIE *Die = new DIE(DW_TAG_compile_unit);
3467 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
3468 DWLabel("section_line", 0), DWLabel("section_line", 0),
3469 false);
3470 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit->getProducer());
3471 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit->getLanguage());
3472 AddString(Die, DW_AT_name, DW_FORM_string, DIUnit->getFilename());
3473 if (!DIUnit->getDirectory().empty())
3474 AddString(Die, DW_AT_comp_dir, DW_FORM_string, DIUnit->getDirectory());
3475
3476 CompileUnit *Unit = new CompileUnit(ID, Die);
3477 DW_CUs[DIUnit->getGV()] = Unit;
3478 }
3479 }
3480
Jim Laskey65195462006-10-30 13:35:07 +00003481 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
3482 /// header file.
Jim Laskeyef42a012006-11-02 20:12:39 +00003483 void ConstructCompileUnitDIEs() {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003484 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003485
Jim Laskeyef42a012006-11-02 20:12:39 +00003486 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003487 unsigned ID = MMI->RecordSource(CUW[i]);
Jim Laskey9d4209f2006-11-07 19:33:46 +00003488 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
Jim Laskeyef42a012006-11-02 20:12:39 +00003489 CompileUnits.push_back(Unit);
3490 }
3491 }
3492
Devang Patelc4523242009-01-05 23:11:11 +00003493 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
3494 /// visible global variables.
3495 void ConstructGlobalVariableDIEs() {
3496 std::string GVName = "llvm.dbg.global_variables";
3497 std::vector<GlobalVariable*> Result;
3498 getGlobalVariablesUsing(*M, GVName, Result);
3499 for (std::vector<GlobalVariable *>::iterator GVI = Result.begin(),
3500 GVE = Result.end(); GVI != GVE; ++GVI) {
3501 DIGlobalVariable *DI_GV = new DIGlobalVariable(*GVI);
3502 CompileUnit *DW_Unit = FindCompileUnit(DI_GV->getCompileUnit());
3503
3504 // Check for pre-existence.
3505 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV->getGV());
3506 if (Slot) continue;
3507
3508 DIE *VariableDie = new DIE(DW_TAG_variable);
3509 AddString(VariableDie, DW_AT_name, DW_FORM_string, DI_GV->getName());
3510 const std::string &LinkageName = DI_GV->getLinkageName();
3511 if (!LinkageName.empty())
3512 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3513 LinkageName);
3514 AddType(DW_Unit, VariableDie, DI_GV->getType());
3515
3516 if (!DI_GV->isLocalToUnit())
3517 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
3518
3519 // Add source line info, if available.
3520 AddSourceLine(VariableDie, DI_GV);
3521
3522 // Add address.
3523 DIEBlock *Block = new DIEBlock();
3524 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
3525 AddObjectLabel(Block, 0, DW_FORM_udata,
3526 Asm->getGlobalLinkName(DI_GV->getGV()));
3527 AddBlock(VariableDie, DW_AT_location, 0, Block);
3528
3529 //Add to map.
3530 Slot = VariableDie;
3531
3532 //Add to context owner.
3533 DW_Unit->getDie()->AddChild(VariableDie);
3534
3535 //Expose as global. FIXME - need to check external flag.
3536 DW_Unit->AddGlobal(DI_GV->getName(), VariableDie);
3537 }
3538 }
3539
Jim Laskey65195462006-10-30 13:35:07 +00003540 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
3541 /// global variables.
Jim Laskeyef42a012006-11-02 20:12:39 +00003542 void ConstructGlobalDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00003543 std::vector<GlobalVariableDesc *> GlobalVariables;
3544 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M, GlobalVariables);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003545
Bill Wendling10fff602008-07-03 22:53:42 +00003546 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
3547 GlobalVariableDesc *GVD = GlobalVariables[i];
3548 NewGlobalVariable(GVD);
3549 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003550 }
Jim Laskey65195462006-10-30 13:35:07 +00003551
Devang Patel78eb6ad2009-01-05 23:21:35 +00003552 /// ConstructSubprograms - Create DIEs for each of the externally visible
3553 /// subprograms.
3554 void ConstructSubprograms() {
3555
3556 std::string SPName = "llvm.dbg.subprograms";
3557 std::vector<GlobalVariable*> Result;
3558 getGlobalVariablesUsing(*M, SPName, Result);
3559 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3560 RE = Result.end(); RI != RE; ++RI) {
3561
3562 DISubprogram *SP = new DISubprogram(*RI);
3563 CompileUnit *Unit = FindCompileUnit(SP->getCompileUnit());
3564
3565 // Check for pre-existence.
3566 DIE *&Slot = Unit->getDieMapSlotFor(SP->getGV());
3567 if (Slot) continue;
3568
3569 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
3570 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, SP->getName());
3571 const std::string &LinkageName = SP->getLinkageName();
3572 if (!LinkageName.empty())
3573 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3574 LinkageName);
3575 DIType SPTy = SP->getType();
3576 AddType(Unit, SubprogramDie, SPTy);
3577 if (!SP->isLocalToUnit())
3578 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
3579 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
3580
3581 AddSourceLine(SubprogramDie, SP);
3582 //Add to map.
3583 Slot = SubprogramDie;
3584 //Add to context owner.
3585 Unit->getDie()->AddChild(SubprogramDie);
3586 //Expose as global.
3587 Unit->AddGlobal(SP->getName(), SubprogramDie);
3588 }
3589 }
3590
Jim Laskey65195462006-10-30 13:35:07 +00003591 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
3592 /// subprograms.
Jim Laskeyef42a012006-11-02 20:12:39 +00003593 void ConstructSubprogramDIEs() {
Bill Wendlingc04f4652008-07-03 23:13:02 +00003594 std::vector<SubprogramDesc *> Subprograms;
3595 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003596
Bill Wendling10fff602008-07-03 22:53:42 +00003597 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
3598 SubprogramDesc *SPD = Subprograms[i];
3599 NewSubprogram(SPD);
3600 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003601 }
Jim Laskey65195462006-10-30 13:35:07 +00003602
Jim Laskey65195462006-10-30 13:35:07 +00003603public:
Jim Laskeyef42a012006-11-02 20:12:39 +00003604 //===--------------------------------------------------------------------===//
3605 // Main entry points.
3606 //
Owen Andersoncb371882008-08-21 00:14:44 +00003607 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00003608 : Dwarf(OS, A, T, "dbg")
Jim Laskeyef42a012006-11-02 20:12:39 +00003609 , CompileUnits()
3610 , AbbreviationsSet(InitAbbreviationsSetSize)
3611 , Abbreviations()
3612 , ValuesSet(InitValuesSetSize)
3613 , Values()
3614 , StringPool()
3615 , DescToUnitMap()
3616 , SectionMap()
3617 , SectionSourceLines()
Jim Laskeybacd3042007-02-21 22:48:45 +00003618 , didInitial(false)
3619 , shouldEmit(false)
Devang Patel7a6e5a32009-01-08 02:33:41 +00003620 , RootDbgScope(NULL)
Jim Laskeyef42a012006-11-02 20:12:39 +00003621 {
3622 }
Jim Laskey072200c2007-01-29 18:51:14 +00003623 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00003624 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
3625 delete CompileUnits[i];
3626 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3627 delete Values[j];
3628 }
3629
Devang Patel5d315982009-01-06 21:07:30 +00003630 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3631 /// This is inovked by the target AsmPrinter.
3632 void SetDebugInfo() {
3633 // FIXME - Check if the module has debug info or not.
3634 // Create all the compile unit DIEs.
3635 ConstructCompileUnits();
3636
3637 // Create DIEs for each of the externally visible global variables.
3638 ConstructGlobalVariableDIEs();
3639
3640 // Create DIEs for each of the externally visible subprograms.
3641 ConstructSubprograms();
3642
3643 // Prime section data.
3644 SectionMap.insert(TAI->getTextSection());
3645
3646 // Print out .file directives to specify files for .loc directives. These
3647 // are printed out early so that they precede any .loc directives.
3648 if (TAI->hasDotLocAndDotFile()) {
3649 for (unsigned i = 1, e = SrcFiles.size(); i <= e; ++i) {
3650 sys::Path FullPath(Directories[SrcFiles[i].getDirectoryID()]);
3651 bool AppendOk = FullPath.appendComponent(SrcFiles[i].getName());
3652 assert(AppendOk && "Could not append filename to directory!");
3653 AppendOk = false;
3654 Asm->EmitFile(i, FullPath.toString());
3655 Asm->EOL();
3656 }
3657 }
3658
3659 // Emit initial sections
3660 EmitInitial();
3661 }
3662
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003663 /// SetModuleInfo - Set machine module information when it's known that pass
3664 /// manager has created it. Set by the target AsmPrinter.
3665 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003666 // Make sure initial declarations are made.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003667 if (!MMI && mmi->hasDebugInfo()) {
3668 MMI = mmi;
Jim Laskeyef42a012006-11-02 20:12:39 +00003669 shouldEmit = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003670
Jim Laskeyef42a012006-11-02 20:12:39 +00003671 // Create all the compile unit DIEs.
3672 ConstructCompileUnitDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003673
Jim Laskeyef42a012006-11-02 20:12:39 +00003674 // Create DIEs for each of the externally visible global variables.
3675 ConstructGlobalDIEs();
Jim Laskey65195462006-10-30 13:35:07 +00003676
Jim Laskeyef42a012006-11-02 20:12:39 +00003677 // Create DIEs for each of the externally visible subprograms.
3678 ConstructSubprogramDIEs();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003679
Jim Laskeyef42a012006-11-02 20:12:39 +00003680 // Prime section data.
Jim Laskeyf910a3f2006-11-06 16:23:59 +00003681 SectionMap.insert(TAI->getTextSection());
Dan Gohmand57c3882007-10-01 22:40:20 +00003682
3683 // Print out .file directives to specify files for .loc directives. These
3684 // are printed out early so that they precede any .loc directives.
3685 if (TAI->hasDotLocAndDotFile()) {
3686 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
3687 const UniqueVector<std::string> &Directories = MMI->getDirectories();
3688 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
3689 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
3690 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
3691 assert(AppendOk && "Could not append filename to directory!");
Devang Pateld0935c32008-12-23 21:55:38 +00003692 AppendOk = false;
Dan Gohmand57c3882007-10-01 22:40:20 +00003693 Asm->EmitFile(i, FullPath.toString());
3694 Asm->EOL();
3695 }
3696 }
3697
3698 // Emit initial sections
3699 EmitInitial();
Jim Laskeyef42a012006-11-02 20:12:39 +00003700 }
3701 }
3702
Jim Laskey65195462006-10-30 13:35:07 +00003703 /// BeginModule - Emit all Dwarf sections that should come prior to the
3704 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00003705 void BeginModule(Module *M) {
3706 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00003707 }
3708
Jim Laskey65195462006-10-30 13:35:07 +00003709 /// EndModule - Emit all Dwarf sections that should come after the content.
3710 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00003711 void EndModule() {
3712 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003713
Jim Laskeyef42a012006-11-02 20:12:39 +00003714 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003715 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003716 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00003717 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00003718 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003719
Jim Laskeyef42a012006-11-02 20:12:39 +00003720 // End text sections.
3721 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003722 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00003723 EmitLabel("section_end", i);
3724 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003725
3726 // Emit common frame information.
3727 EmitCommonDebugFrame();
3728
3729 // Emit function debug frame information
3730 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3731 E = DebugFrames.end(); I != E; ++I)
3732 EmitFunctionDebugFrame(*I);
3733
Jim Laskeyef42a012006-11-02 20:12:39 +00003734 // Compute DIE offsets and sizes.
3735 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003736
Jim Laskeyef42a012006-11-02 20:12:39 +00003737 // Emit all the DIEs into a debug info section
3738 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003739
Jim Laskeyef42a012006-11-02 20:12:39 +00003740 // Corresponding abbreviations into a abbrev section.
3741 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003742
Jim Laskeyef42a012006-11-02 20:12:39 +00003743 // Emit source line correspondence into a debug line section.
3744 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003745
Jim Laskeyef42a012006-11-02 20:12:39 +00003746 // Emit info into a debug pubnames section.
3747 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003748
Jim Laskeyef42a012006-11-02 20:12:39 +00003749 // Emit info into a debug str section.
3750 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003751
Jim Laskeyef42a012006-11-02 20:12:39 +00003752 // Emit info into a debug loc section.
3753 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003754
Jim Laskeyef42a012006-11-02 20:12:39 +00003755 // Emit info into a debug aranges section.
3756 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003757
Jim Laskeyef42a012006-11-02 20:12:39 +00003758 // Emit info into a debug ranges section.
3759 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003760
Jim Laskeyef42a012006-11-02 20:12:39 +00003761 // Emit info into a debug macinfo section.
3762 EmitDebugMacInfo();
3763 }
3764
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003765 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00003766 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00003767 void BeginFunction(MachineFunction *MF) {
3768 this->MF = MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003769
Jim Laskeyef42a012006-11-02 20:12:39 +00003770 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00003771
3772 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003773 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003774
Jim Laskeyef42a012006-11-02 20:12:39 +00003775 // Assumes in correct section after the entry point.
3776 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003777
3778 // Emit label for the implicitly defined dbg.stoppoint at the start of
3779 // the function.
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003780 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
3781 if (!LineInfos.empty()) {
3782 const SourceLineInfo &LineInfo = LineInfos[0];
3783 Asm->printLabel(LineInfo.getLabelID());
3784 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003785 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003786
Jim Laskey65195462006-10-30 13:35:07 +00003787 /// EndFunction - Gather and emit post-function debug information.
3788 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003789 void EndFunction(MachineFunction *MF) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003790 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003791
Jim Laskeyb82313f2007-02-01 16:31:34 +00003792 // Define end label for subprogram.
3793 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003794
Jim Laskeyef42a012006-11-02 20:12:39 +00003795 // Get function line info.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00003796 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
Jim Laskeyef42a012006-11-02 20:12:39 +00003797
3798 if (!LineInfos.empty()) {
3799 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003800 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003801 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
3802 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
3803 // Append the function info to section info.
3804 SectionLineInfos.insert(SectionLineInfos.end(),
3805 LineInfos.begin(), LineInfos.end());
3806 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003807
Jim Laskeyef42a012006-11-02 20:12:39 +00003808 // Construct scopes for subprogram.
Bill Wendlingd751c642008-09-26 00:28:12 +00003809 if (MMI->getRootScope())
3810 ConstructRootScope(MMI->getRootScope());
3811 else
3812 // FIXME: This is wrong. We are essentially getting past a problem with
3813 // debug information not being able to handle unreachable blocks that have
3814 // debug information in them. In particular, those unreachable blocks that
3815 // have "region end" info in them. That situation results in the "root
3816 // scope" not being created. If that's the case, then emit a "default"
3817 // scope, i.e., one that encompasses the whole function. This isn't
3818 // desirable. And a better way of handling this (and all of the debugging
3819 // information) needs to be explored.
3820 ConstructDefaultScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003821
3822 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3823 MMI->getFrameMoves()));
Jim Laskeyef42a012006-11-02 20:12:39 +00003824 }
Jim Laskey65195462006-10-30 13:35:07 +00003825};
3826
Jim Laskey072200c2007-01-29 18:51:14 +00003827//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003828/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003829///
3830class DwarfException : public Dwarf {
3831
Jim Laskeyb82313f2007-02-01 16:31:34 +00003832private:
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003833 struct FunctionEHFrameInfo {
3834 std::string FnName;
3835 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003836 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003837 bool hasCalls;
3838 bool hasLandingPads;
3839 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003840 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003841
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003842 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3843 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003844 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003845 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003846 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003847 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003848 };
3849
3850 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003851
3852 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3853 /// be emitted.
3854 bool shouldEmitTable;
3855
3856 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3857 /// should be emitted.
3858 bool shouldEmitMoves;
3859
3860 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3861 /// should be emitted.
3862 bool shouldEmitTableModule;
3863
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003864 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003865 /// should be emitted.
3866 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003867
Jim Laskey3f09fc22007-02-28 18:38:31 +00003868 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3869 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003870 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003871 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003872 int stackGrowth =
3873 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3874 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003875 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003876
Jim Laskeybacd3042007-02-21 22:48:45 +00003877 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003878 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003879
3880 if (!TAI->doesRequireNonLocalEHFrameLabel())
3881 O << TAI->getEHGlobalPrefix();
3882 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003883 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003884
Jim Laskeybacd3042007-02-21 22:48:45 +00003885 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003886 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003887
Jim Laskeybacd3042007-02-21 22:48:45 +00003888 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003889 EmitDifference("eh_frame_common_end", Index,
3890 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003891 Asm->EOL("Length of Common Information Entry");
3892
Jim Laskeybacd3042007-02-21 22:48:45 +00003893 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003894 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003895 Asm->EmitInt32((int)0);
3896 Asm->EOL("CIE Identifier Tag");
3897 Asm->EmitInt8(DW_CIE_VERSION);
3898 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003899
Jim Laskeybacd3042007-02-21 22:48:45 +00003900 // The personality presence indicates that language specific information
3901 // will show up in the eh frame.
3902 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003903 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003904
Jim Laskeybacd3042007-02-21 22:48:45 +00003905 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003906 Asm->EmitULEB128Bytes(1);
3907 Asm->EOL("CIE Code Alignment Factor");
3908 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003909 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003910 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003911 Asm->EOL("CIE Return Address Column");
3912
Jim Laskeybacd3042007-02-21 22:48:45 +00003913 // If there is a personality, we need to indicate the functions location.
3914 if (Personality) {
3915 Asm->EmitULEB128Bytes(7);
3916 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003917
Duncan Sands671fa972008-05-07 19:11:09 +00003918 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003919 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003920 Asm->EOL("Personality (pcrel sdata4 indirect)");
3921 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003922 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003923 Asm->EOL("Personality (pcrel sdata4)");
3924 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003925
Duncan Sands671fa972008-05-07 19:11:09 +00003926 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003927 O << TAI->getPersonalityPrefix();
3928 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3929 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003930 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3931 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003932 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003933
Duncan Sands671fa972008-05-07 19:11:09 +00003934 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3935 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003936
Bill Wendlingd60de512009-01-05 22:53:45 +00003937 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3938 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003939 } else {
3940 Asm->EmitULEB128Bytes(1);
3941 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003942
Bill Wendlingd60de512009-01-05 22:53:45 +00003943 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3944 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003945 }
3946
3947 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003948 std::vector<MachineMove> Moves;
3949 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003950 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003951
Dale Johannesen21d972a2008-04-30 00:43:29 +00003952 // On Darwin the linker honors the alignment of eh_frame, which means it
3953 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3954 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003955 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003956 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003957 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003958
Jim Laskeybacd3042007-02-21 22:48:45 +00003959 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003960 }
Duncan Sands671fa972008-05-07 19:11:09 +00003961
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003962 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003963 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003964 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003965 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3966
Jim Laskeybacd3042007-02-21 22:48:45 +00003967 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3968
3969 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003970 // If the corresponding function is static, this should not be
3971 // externally visible.
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003972 if (linkage != Function::InternalLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003973 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3974 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3975 }
3976
Dale Johannesen038129d2008-01-10 02:03:30 +00003977 // If corresponding function is weak definition, this should be too.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003978 if ((linkage == Function::WeakLinkage ||
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003979 linkage == Function::LinkOnceLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003980 TAI->getWeakDefDirective())
3981 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3982
3983 // If there are no calls then you can't unwind. This may mean we can
3984 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003985 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003986 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003987 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003988 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003989 !UnwindTablesMandatory &&
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003990 ((linkage != Function::WeakLinkage &&
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003991 linkage != Function::LinkOnceLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003992 !TAI->getWeakDefDirective() ||
3993 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003994 {
Bill Wendling6e198962007-09-18 01:47:22 +00003995 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003996 // This name has no connection to the function, so it might get
3997 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003998 // dead-stripping unconditionally.
3999 if (const char *UsedDirective = TAI->getUsedDirective())
4000 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00004001 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00004002 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00004003
Jim Laskeybacd3042007-02-21 22:48:45 +00004004 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004005 EmitDifference("eh_frame_end", EHFrameInfo.Number,
4006 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00004007 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004008
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004009 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00004010
Bill Wendling722f5f12008-12-24 08:05:17 +00004011 if (TAI->doesRequireNonLocalEHFrameLabel()) {
4012 PrintRelDirective(true, true);
4013 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
4014
4015 if (!TAI->isAbsoluteEHSectionOffsets())
4016 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
4017 } else {
4018 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
4019 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
4020 true, true, false);
4021 }
4022
Jim Laskeybacd3042007-02-21 22:48:45 +00004023 Asm->EOL("FDE CIE offset");
4024
Bill Wendling5fe1fac2009-01-06 19:13:55 +00004025 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00004026 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004027 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00004028 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00004029 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00004030
Jim Laskey3f09fc22007-02-28 18:38:31 +00004031 // If there is a personality and landing pads then point to the language
4032 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004033 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00004034 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00004035 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00004036
4037 if (EHFrameInfo.hasLandingPads)
4038 EmitReference("exception", EHFrameInfo.Number, true, true);
4039 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00004040 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00004041 Asm->EOL("Language Specific Data Area");
4042 } else {
4043 Asm->EmitULEB128Bytes(0);
4044 Asm->EOL("Augmentation size");
4045 }
Duncan Sands671fa972008-05-07 19:11:09 +00004046
Jim Laskeybacd3042007-02-21 22:48:45 +00004047 // Indicate locations of function specific callee saved registers in
4048 // frame.
Dale Johannesenb97aec62007-11-13 19:13:01 +00004049 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004050
Dale Johannesen21d972a2008-04-30 00:43:29 +00004051 // On Darwin the linker honors the alignment of eh_frame, which means it
4052 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
4053 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004054 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00004055 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004056 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004057
4058 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004059 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004060 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004061 // that depends on unused functions (calling undefined externals) being
4062 // dead-stripped to link correctly. Yes, there really is.
4063 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
4064 if (const char *UsedDirective = TAI->getUsedDirective())
4065 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
4066 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004067 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00004068
Duncan Sands57810cd2007-09-05 11:27:52 +00004069 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004070 ///
4071 /// The general organization of the table is complex, but the basic concepts
4072 /// are easy. First there is a header which describes the location and
4073 /// organization of the three components that follow.
4074 /// 1. The landing pad site information describes the range of code covered
4075 /// by the try. In our case it's an accumulation of the ranges covered
4076 /// by the invokes in the try. There is also a reference to the landing
4077 /// pad that handles the exception once processed. Finally an index into
4078 /// the actions table.
4079 /// 2. The action table, in our case, is composed of pairs of type ids
4080 /// and next action offset. Starting with the action index from the
4081 /// landing pad site, each type Id is checked for a match to the current
4082 /// exception. If it matches then the exception and type id are passed
4083 /// on to the landing pad. Otherwise the next action is looked up. This
4084 /// chain is terminated with a next action of zero. If no type id is
4085 /// found the the frame is unwound and handling continues.
4086 /// 3. Type id table contains references to all the C++ typeinfo for all
4087 /// catches in the function. This tables is reversed indexed base 1.
4088
Duncan Sandsb32edb42007-06-06 15:37:31 +00004089 /// SharedTypeIds - How many leading type ids two landing pads have in common.
4090 static unsigned SharedTypeIds(const LandingPadInfo *L,
4091 const LandingPadInfo *R) {
4092 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004093 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004094 unsigned MinSize = LSize < RSize ? LSize : RSize;
4095 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004096
Duncan Sandsb32edb42007-06-06 15:37:31 +00004097 for (; Count != MinSize; ++Count)
4098 if (LIds[Count] != RIds[Count])
4099 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004100
Duncan Sandsb32edb42007-06-06 15:37:31 +00004101 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004102 }
4103
Duncan Sandsb32edb42007-06-06 15:37:31 +00004104 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00004105 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004106 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004107 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00004108 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004109
Duncan Sandsb32edb42007-06-06 15:37:31 +00004110 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00004111 if (LIds[i] != RIds[i])
4112 return LIds[i] < RIds[i];
4113
Duncan Sandsb32edb42007-06-06 15:37:31 +00004114 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004115 }
4116
Duncan Sands53c3a332007-05-16 12:12:23 +00004117 struct KeyInfo {
4118 static inline unsigned getEmptyKey() { return -1U; }
4119 static inline unsigned getTombstoneKey() { return -2U; }
4120 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00004121 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00004122 static bool isPod() { return true; }
4123 };
4124
Duncan Sands57810cd2007-09-05 11:27:52 +00004125 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004126 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004127 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004128 int NextAction;
4129 struct ActionEntry *Previous;
4130 };
4131
Duncan Sands57810cd2007-09-05 11:27:52 +00004132 /// PadRange - Structure holding a try-range and the associated landing pad.
4133 struct PadRange {
4134 // The index of the landing pad.
4135 unsigned PadIndex;
4136 // The index of the begin and end labels in the landing pad's label lists.
4137 unsigned RangeIndex;
4138 };
4139
4140 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
4141
4142 /// CallSiteEntry - Structure describing an entry in the call-site table.
4143 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00004144 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004145 unsigned BeginLabel; // zero indicates the start of the function.
4146 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00004147 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00004148 unsigned PadLabel; // zero indicates that there is no landing pad.
4149 unsigned Action;
4150 };
4151
Jim Laskeybacd3042007-02-21 22:48:45 +00004152 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00004153 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00004154 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00004155 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
4156 if (PadInfos.empty()) return;
4157
4158 // Sort the landing pads in order of their type ids. This is used to fold
4159 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00004160 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00004161 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00004162 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00004163 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004164 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
4165
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004166 // Negative type ids index into FilterIds, positive type ids index into
4167 // TypeInfos. The value written for a positive type id is just the type
4168 // id itself. For a negative type id, however, the value written is the
4169 // (negative) byte offset of the corresponding FilterIds entry. The byte
4170 // offset is usually equal to the type id, because the FilterIds entries
4171 // are written using a variable width encoding which outputs one byte per
4172 // entry as long as the value written is not too large, but can differ.
4173 // This kind of complication does not occur for positive type ids because
4174 // type infos are output using a fixed width encoding.
4175 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
4176 SmallVector<int, 16> FilterOffsets;
4177 FilterOffsets.reserve(FilterIds.size());
4178 int Offset = -1;
4179 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
4180 E = FilterIds.end(); I != E; ++I) {
4181 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004182 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004183 }
4184
Duncan Sands57810cd2007-09-05 11:27:52 +00004185 // Compute the actions table and gather the first action index for each
4186 // landing pad site.
4187 SmallVector<ActionEntry, 32> Actions;
4188 SmallVector<unsigned, 64> FirstActions;
4189 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00004190
Duncan Sandsb32edb42007-06-06 15:37:31 +00004191 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00004192 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004193 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00004194 const LandingPadInfo *LP = LandingPads[i];
4195 const std::vector<int> &TypeIds = LP->TypeIds;
4196 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004197 unsigned SizeSiteActions = 0;
4198
Duncan Sandsb32edb42007-06-06 15:37:31 +00004199 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00004200 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00004201 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004202
Duncan Sandsb32edb42007-06-06 15:37:31 +00004203 if (NumShared) {
4204 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
4205 assert(Actions.size());
4206 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004207 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
4208 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004209 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004210 SizeAction -=
4211 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004212 SizeAction += -PrevAction->NextAction;
4213 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00004214 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00004215 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00004216
Duncan Sandsb32edb42007-06-06 15:37:31 +00004217 // Compute the actions.
4218 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
4219 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004220 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4221 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004222 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004223
4224 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004225 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004226 SizeSiteActions += SizeAction;
4227
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004228 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00004229 Actions.push_back(Action);
4230
4231 PrevAction = &Actions.back();
4232 }
4233
4234 // Record the first action of the landing pad site.
4235 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4236 } // else identical - re-use previous FirstAction
4237
4238 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00004239
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00004240 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00004241 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00004242 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004243
Duncan Sands481dc722007-12-19 07:36:31 +00004244 // Compute the call-site table. The entry for an invoke has a try-range
4245 // containing the call, a non-zero landing pad and an appropriate action.
4246 // The entry for an ordinary call has a try-range containing the call and
4247 // zero for the landing pad and the action. Calls marked 'nounwind' have
4248 // no entry and must not be contained in the try-range of any entry - they
4249 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004250 SmallVector<CallSiteEntry, 64> CallSites;
4251
4252 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00004253 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4254 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4255 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00004256 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4257 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00004258 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004259 unsigned BeginLabel = LandingPad->BeginLabels[j];
4260 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4261 PadRange P = { i, j };
4262 PadMap[BeginLabel] = P;
4263 }
4264 }
4265
Duncan Sands481dc722007-12-19 07:36:31 +00004266 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00004267 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00004268
4269 // Whether there is a potentially throwing instruction (currently this means
4270 // an ordinary call) between the end of the previous try-range and now.
4271 bool SawPotentiallyThrowing = false;
4272
4273 // Whether the last callsite entry was for an invoke.
4274 bool PreviousIsInvoke = false;
4275
Duncan Sands481dc722007-12-19 07:36:31 +00004276 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00004277 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4278 I != E; ++I) {
4279 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4280 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00004281 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00004282 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00004283 continue;
4284 }
4285
Chris Lattner9e330492007-12-30 20:50:28 +00004286 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00004287 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00004288
Duncan Sands481dc722007-12-19 07:36:31 +00004289 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00004290 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00004291 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004292
Duncan Sands481dc722007-12-19 07:36:31 +00004293 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00004294 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00004295 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00004296 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00004297 continue;
4298
4299 PadRange P = L->second;
4300 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4301
4302 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4303 "Inconsistent landing pad map!");
4304
4305 // If some instruction between the previous try-range and this one may
4306 // throw, create a call-site entry with no landing pad for the region
4307 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00004308 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004309 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4310 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00004311 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00004312 }
4313
4314 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00004315 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00004316
Duncan Sands481dc722007-12-19 07:36:31 +00004317 if (LandingPad->LandingPadLabel) {
4318 // This try-range is for an invoke.
4319 CallSiteEntry Site = {BeginLabel, LastLabel,
4320 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00004321
Duncan Sands481dc722007-12-19 07:36:31 +00004322 // Try to merge with the previous call-site.
4323 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00004324 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00004325 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4326 // Extend the range of the previous entry.
4327 Prev.EndLabel = Site.EndLabel;
4328 continue;
4329 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004330 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004331
Duncan Sands481dc722007-12-19 07:36:31 +00004332 // Otherwise, create a new call-site.
4333 CallSites.push_back(Site);
4334 PreviousIsInvoke = true;
4335 } else {
4336 // Create a gap.
4337 PreviousIsInvoke = false;
4338 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004339 }
4340 }
4341 // If some instruction between the previous try-range and the end of the
4342 // function may throw, create a call-site entry with no landing pad for the
4343 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00004344 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00004345 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4346 CallSites.push_back(Site);
4347 }
4348
Jim Laskeybacd3042007-02-21 22:48:45 +00004349 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00004350
4351 // Call sites.
4352 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4353 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4354 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4355 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4356 SiteLengthSize +
4357 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00004358 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004359 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00004360
Duncan Sands671fa972008-05-07 19:11:09 +00004361 // Type infos.
4362 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4363 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004364
4365 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004366 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004367 SizeSites + SizeActions + SizeTypes;
4368
4369 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4370 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004371 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004372 TypeOffset;
4373
4374 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004375
Jim Laskeybacd3042007-02-21 22:48:45 +00004376 // Begin the exception table.
4377 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00004378 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00004379 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00004380 for (unsigned i = 0; i != SizeAlign; ++i) {
4381 Asm->EmitInt8(0);
4382 Asm->EOL("Padding");
4383 }
Jim Laskeybacd3042007-02-21 22:48:45 +00004384 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00004385
Jim Laskeybacd3042007-02-21 22:48:45 +00004386 // Emit the header.
4387 Asm->EmitInt8(DW_EH_PE_omit);
4388 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4389 Asm->EmitInt8(DW_EH_PE_absptr);
4390 Asm->EOL("TType format (DW_EH_PE_absptr)");
4391 Asm->EmitULEB128Bytes(TypeOffset);
4392 Asm->EOL("TType base offset");
4393 Asm->EmitInt8(DW_EH_PE_udata4);
4394 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4395 Asm->EmitULEB128Bytes(SizeSites);
4396 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004397
Duncan Sands57810cd2007-09-05 11:27:52 +00004398 // Emit the landing pad site information.
4399 for (unsigned i = 0; i < CallSites.size(); ++i) {
4400 CallSiteEntry &S = CallSites[i];
4401 const char *BeginTag;
4402 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00004403
Duncan Sands57810cd2007-09-05 11:27:52 +00004404 if (!S.BeginLabel) {
4405 BeginTag = "eh_func_begin";
4406 BeginNumber = SubprogramCount;
4407 } else {
4408 BeginTag = "label";
4409 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00004410 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004411
Duncan Sands57810cd2007-09-05 11:27:52 +00004412 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004413 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004414 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00004415
Duncan Sands57810cd2007-09-05 11:27:52 +00004416 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00004417 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00004418 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004419 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00004420 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004421 }
4422 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00004423
Duncan Sands671fa972008-05-07 19:11:09 +00004424 if (!S.PadLabel)
4425 Asm->EmitInt32(0);
4426 else
Duncan Sands57810cd2007-09-05 11:27:52 +00004427 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00004428 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00004429 Asm->EOL("Landing pad");
4430
4431 Asm->EmitULEB128Bytes(S.Action);
4432 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004433 }
Duncan Sands53c3a332007-05-16 12:12:23 +00004434
Jim Laskeybacd3042007-02-21 22:48:45 +00004435 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00004436 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4437 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00004438
Duncan Sandsfccf0a22007-07-06 12:46:24 +00004439 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00004440 Asm->EOL("TypeInfo index");
4441 Asm->EmitSLEB128Bytes(Action.NextAction);
4442 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00004443 }
4444
4445 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00004446 for (unsigned M = TypeInfos.size(); M; --M) {
4447 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00004448
4449 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00004450
4451 if (GV)
4452 O << Asm->getGlobalLinkName(GV);
4453 else
4454 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00004455
Jim Laskeybacd3042007-02-21 22:48:45 +00004456 Asm->EOL("TypeInfo");
4457 }
4458
Duncan Sands73ef58a2007-06-02 16:53:42 +00004459 // Emit the filter typeids.
4460 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4461 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00004462 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00004463 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00004464 }
Duncan Sands57810cd2007-09-05 11:27:52 +00004465
Evan Cheng05548eb2008-02-29 19:36:59 +00004466 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004467 }
4468
Jim Laskey072200c2007-01-29 18:51:14 +00004469public:
4470 //===--------------------------------------------------------------------===//
4471 // Main entry points.
4472 //
Owen Andersoncb371882008-08-21 00:14:44 +00004473 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00004474 : Dwarf(OS, A, T, "eh")
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004475 , shouldEmitTable(false)
4476 , shouldEmitMoves(false)
4477 , shouldEmitTableModule(false)
4478 , shouldEmitMovesModule(false)
Jim Laskey072200c2007-01-29 18:51:14 +00004479 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004480
Jim Laskey072200c2007-01-29 18:51:14 +00004481 virtual ~DwarfException() {}
4482
4483 /// SetModuleInfo - Set machine module information when it's known that pass
4484 /// manager has created it. Set by the target AsmPrinter.
4485 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00004486 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00004487 }
4488
4489 /// BeginModule - Emit all exception information that should come prior to the
4490 /// content.
4491 void BeginModule(Module *M) {
4492 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00004493 }
4494
4495 /// EndModule - Emit all exception information that should come after the
4496 /// content.
4497 void EndModule() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004498 if (shouldEmitMovesModule || shouldEmitTableModule) {
4499 const std::vector<Function *> Personalities = MMI->getPersonalities();
4500 for (unsigned i =0; i < Personalities.size(); ++i)
4501 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00004502
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004503 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4504 E = EHFrames.end(); I != E; ++I)
4505 EmitEHFrame(*I);
4506 }
Jim Laskey072200c2007-01-29 18:51:14 +00004507 }
4508
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004509 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00004510 /// emitted immediately after the function entry point.
4511 void BeginFunction(MachineFunction *MF) {
4512 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004513 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesene0040622008-04-02 17:04:45 +00004514 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004515
4516 // Map all labels and get rid of any dead landing pads.
4517 MMI->TidyLandingPads();
4518 // If any landing pads survive, we need an EH table.
4519 if (MMI->getLandingPads().size())
4520 shouldEmitTable = true;
4521
4522 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00004523 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004524 shouldEmitMoves = true;
4525
4526 if (shouldEmitMoves || shouldEmitTable)
4527 // Assumes in correct section after the entry point.
4528 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00004529 }
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004530 shouldEmitTableModule |= shouldEmitTable;
4531 shouldEmitMovesModule |= shouldEmitMoves;
Jim Laskey072200c2007-01-29 18:51:14 +00004532 }
4533
4534 /// EndFunction - Gather and emit post-function exception information.
4535 ///
4536 void EndFunction() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004537 if (shouldEmitMoves || shouldEmitTable) {
4538 EmitLabel("eh_func_end", SubprogramCount);
4539 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00004540
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004541 // Save EH frame information
4542 EHFrames.
4543 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling6e198962007-09-18 01:47:22 +00004544 SubprogramCount,
4545 MMI->getPersonalityIndex(),
4546 MF->getFrameInfo()->hasCalls(),
4547 !MMI->getLandingPads().empty(),
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00004548 MMI->getFrameMoves(),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00004549 MF->getFunction()));
Dale Johannesen1532f3d2008-04-02 00:25:04 +00004550 }
Jim Laskey072200c2007-01-29 18:51:14 +00004551 }
4552};
4553
Jim Laskey0d086af2006-02-27 12:43:29 +00004554} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00004555
4556//===----------------------------------------------------------------------===//
4557
Jim Laskeyd18e2892006-01-20 20:34:06 +00004558/// Emit - Print the abbreviation using the specified Dwarf writer.
4559///
Jim Laskey072200c2007-01-29 18:51:14 +00004560void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004561 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00004562 DD.getAsm()->EmitULEB128Bytes(Tag);
4563 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004564
Jim Laskeyd18e2892006-01-20 20:34:06 +00004565 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00004566 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4567 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004568
Jim Laskeyd18e2892006-01-20 20:34:06 +00004569 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00004570 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00004571 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004572
Jim Laskeyd18e2892006-01-20 20:34:06 +00004573 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00004574 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4575 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004576
Jim Laskeyd18e2892006-01-20 20:34:06 +00004577 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00004578 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4579 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00004580 }
4581
4582 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00004583 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4584 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00004585}
4586
4587#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00004588void DIEAbbrev::print(std::ostream &O) {
4589 O << "Abbreviation @"
4590 << std::hex << (intptr_t)this << std::dec
4591 << " "
4592 << TagString(Tag)
4593 << " "
4594 << ChildrenString(ChildrenFlag)
4595 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004596
Jim Laskeya0f3d172006-09-07 22:06:40 +00004597 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4598 O << " "
4599 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004600 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00004601 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00004602 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00004603 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00004604}
Bill Wendlinge8156192006-12-07 01:30:32 +00004605void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00004606#endif
4607
4608//===----------------------------------------------------------------------===//
4609
Jim Laskeyef42a012006-11-02 20:12:39 +00004610#ifndef NDEBUG
4611void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004612 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004613}
Jim Laskeyef42a012006-11-02 20:12:39 +00004614#endif
4615
4616//===----------------------------------------------------------------------===//
4617
Jim Laskey063e7652006-01-17 17:31:53 +00004618/// EmitValue - Emit integer of appropriate size.
4619///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004620void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00004621 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00004622 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00004623 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004624 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004625 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004626 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004627 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004628 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00004629 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00004630 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4631 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4632 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004633 default: assert(0 && "DIE Value form not supported yet"); break;
4634 }
4635}
4636
4637/// SizeOf - Determine size of integer value in bytes.
4638///
Jim Laskey072200c2007-01-29 18:51:14 +00004639unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004640 switch (Form) {
4641 case DW_FORM_flag: // Fall thru
4642 case DW_FORM_ref1: // Fall thru
4643 case DW_FORM_data1: return sizeof(int8_t);
4644 case DW_FORM_ref2: // Fall thru
4645 case DW_FORM_data2: return sizeof(int16_t);
4646 case DW_FORM_ref4: // Fall thru
4647 case DW_FORM_data4: return sizeof(int32_t);
4648 case DW_FORM_ref8: // Fall thru
4649 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004650 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4651 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00004652 default: assert(0 && "DIE Value form not supported yet"); break;
4653 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004654 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00004655}
4656
Jim Laskey063e7652006-01-17 17:31:53 +00004657//===----------------------------------------------------------------------===//
4658
4659/// EmitValue - Emit string value.
4660///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004661void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004662 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00004663}
4664
Jim Laskey063e7652006-01-17 17:31:53 +00004665//===----------------------------------------------------------------------===//
4666
4667/// EmitValue - Emit label value.
4668///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004669void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004670 bool IsSmall = Form == DW_FORM_data4;
4671 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004672}
4673
4674/// SizeOf - Determine size of label value in bytes.
4675///
Jim Laskey072200c2007-01-29 18:51:14 +00004676unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004677 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004678 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004679}
Jim Laskeyef42a012006-11-02 20:12:39 +00004680
Jim Laskey063e7652006-01-17 17:31:53 +00004681//===----------------------------------------------------------------------===//
4682
Jim Laskeyd18e2892006-01-20 20:34:06 +00004683/// EmitValue - Emit label value.
4684///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004685void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004686 bool IsSmall = Form == DW_FORM_data4;
4687 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00004688}
4689
4690/// SizeOf - Determine size of label value in bytes.
4691///
Jim Laskey072200c2007-01-29 18:51:14 +00004692unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00004693 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004694 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00004695}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004696
Jim Laskeyd18e2892006-01-20 20:34:06 +00004697//===----------------------------------------------------------------------===//
4698
Jim Laskey063e7652006-01-17 17:31:53 +00004699/// EmitValue - Emit delta value.
4700///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004701void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4702 bool IsSmall = Form == DW_FORM_data4;
4703 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4704 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4705}
4706
4707/// SizeOf - Determine size of delta value in bytes.
4708///
4709unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4710 if (Form == DW_FORM_data4) return 4;
4711 return DD.getTargetData()->getPointerSize();
4712}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004713
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004714//===----------------------------------------------------------------------===//
4715
4716/// EmitValue - Emit delta value.
4717///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004718void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004719 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004720 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004721}
4722
4723/// SizeOf - Determine size of delta value in bytes.
4724///
Jim Laskey072200c2007-01-29 18:51:14 +00004725unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004726 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004727 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004728}
4729
4730//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004731
Jim Laskeyb8509c52006-03-23 18:07:55 +00004732/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004733///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004734void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004735 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004736}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004737
Jim Laskeyd18e2892006-01-20 20:34:06 +00004738//===----------------------------------------------------------------------===//
4739
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004740/// ComputeSize - calculate the size of the block.
4741///
Jim Laskey072200c2007-01-29 18:51:14 +00004742unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004743 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004744 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004745
Jim Laskeyef42a012006-11-02 20:12:39 +00004746 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004747 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004748 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004749 }
4750 return Size;
4751}
4752
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004753/// EmitValue - Emit block data.
4754///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004755void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004756 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004757 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4758 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4759 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4760 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004761 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004762 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004763
Owen Anderson873e1b52008-06-24 21:44:59 +00004764 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004765
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004766 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004767 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004768 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004769 }
4770}
4771
4772/// SizeOf - Determine size of block data in bytes.
4773///
Jim Laskey072200c2007-01-29 18:51:14 +00004774unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004775 switch (Form) {
4776 case DW_FORM_block1: return Size + sizeof(int8_t);
4777 case DW_FORM_block2: return Size + sizeof(int16_t);
4778 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004779 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004780 default: assert(0 && "Improper form for block"); break;
4781 }
4782 return 0;
4783}
4784
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004785//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004786/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004787
4788DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004789 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004790 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004791}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004792
Jim Laskeyb8509c52006-03-23 18:07:55 +00004793/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4794///
4795void DIE::AddSiblingOffset() {
4796 DIEInteger *DI = new DIEInteger(0);
4797 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004798 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004799}
4800
Jim Laskeyef42a012006-11-02 20:12:39 +00004801/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004802///
Jim Laskeyef42a012006-11-02 20:12:39 +00004803void DIE::Profile(FoldingSetNodeID &ID) {
4804 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004805
Jim Laskeyef42a012006-11-02 20:12:39 +00004806 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4807 ID.AddPointer(Children[i]);
4808
4809 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4810 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004811}
Jim Laskeyef42a012006-11-02 20:12:39 +00004812
4813#ifndef NDEBUG
4814void DIE::print(std::ostream &O, unsigned IncIndent) {
4815 static unsigned IndentCount = 0;
4816 IndentCount += IncIndent;
4817 const std::string Indent(IndentCount, ' ');
4818 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004819
Jim Laskeyef42a012006-11-02 20:12:39 +00004820 if (!isBlock) {
4821 O << Indent
4822 << "Die: "
4823 << "0x" << std::hex << (intptr_t)this << std::dec
4824 << ", Offset: " << Offset
4825 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004826 << "\n";
4827
Jim Laskeyef42a012006-11-02 20:12:39 +00004828 O << Indent
4829 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004830 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004831 << ChildrenString(Abbrev.getChildrenFlag());
4832 } else {
4833 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004834 }
4835 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004836
Owen Anderson873e1b52008-06-24 21:44:59 +00004837 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004838
Jim Laskeyef42a012006-11-02 20:12:39 +00004839 IndentCount += 2;
4840 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4841 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004842
4843 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004844 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004845 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004846 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004847
Jim Laskeyef42a012006-11-02 20:12:39 +00004848 O << " "
4849 << FormEncodingString(Data[i].getForm())
4850 << " ";
4851 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004852 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004853 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004854 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004855
Jim Laskeyef42a012006-11-02 20:12:39 +00004856 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4857 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004858 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004859
Jim Laskeyef42a012006-11-02 20:12:39 +00004860 if (!isBlock) O << "\n";
4861 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004862}
4863
Jim Laskeyef42a012006-11-02 20:12:39 +00004864void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004865 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004866}
Jim Laskeybd761842006-02-27 17:27:12 +00004867#endif
Jim Laskey65195462006-10-30 13:35:07 +00004868
4869//===----------------------------------------------------------------------===//
4870/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004871///
Jim Laskey65195462006-10-30 13:35:07 +00004872
Devang Pateleb3fc282009-01-08 23:40:34 +00004873DwarfWriter::DwarfWriter() : ImmutablePass(&ID), DD(NULL), DE(NULL) {
Jim Laskey65195462006-10-30 13:35:07 +00004874}
4875
4876DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004877 delete DE;
4878 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004879}
4880
Jim Laskey65195462006-10-30 13:35:07 +00004881/// BeginModule - Emit all Dwarf sections that should come prior to the
4882/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004883void DwarfWriter::BeginModule(Module *M,
4884 MachineModuleInfo *MMI,
4885 raw_ostream &OS, AsmPrinter *A,
4886 const TargetAsmInfo *T) {
4887 DE = new DwarfException(OS, A, T);
4888 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004889 DE->BeginModule(M);
4890 DD->BeginModule(M);
Devang Pateleb3fc282009-01-08 23:40:34 +00004891 DD->SetModuleInfo(MMI);
4892 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004893}
4894
4895/// EndModule - Emit all Dwarf sections that should come after the content.
4896///
4897void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004898 DE->EndModule();
4899 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004900}
4901
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004902/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004903/// emitted immediately after the function entry point.
4904void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004905 DE->BeginFunction(MF);
4906 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004907}
4908
4909/// EndFunction - Gather and emit post-function debug information.
4910///
Bill Wendlingd751c642008-09-26 00:28:12 +00004911void DwarfWriter::EndFunction(MachineFunction *MF) {
4912 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004913 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004914
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004915 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004916 // Clear function debug information.
4917 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004918}