blob: 09d169b6f338b04731c6ec45a36c0d6f8b5e537c [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 /// ID - File identifier for source.
753 ///
754 unsigned ID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000755
Jim Laskeyef42a012006-11-02 20:12:39 +0000756 /// Die - Compile unit debug information entry.
757 ///
758 DIE *Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000759
Devang Patelbbdc8202009-01-13 23:54:55 +0000760 /// GVToDieMap - Tracks the mapping of unit level debug informaton
761 /// variables to debug information entries.
Devang Patel48d190f2009-01-05 21:47:57 +0000762 DenseMap<GlobalVariable *, DIE *> GVToDieMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000763
Devang Patelbbdc8202009-01-13 23:54:55 +0000764 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Jim Laskeyef42a012006-11-02 20:12:39 +0000765 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patel48d190f2009-01-05 21:47:57 +0000766 DenseMap<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Jim Laskeyef42a012006-11-02 20:12:39 +0000767
768 /// Globals - A map of globally visible named entities for this unit.
769 ///
770 std::map<std::string, DIE *> Globals;
771
772 /// DiesSet - Used to uniquely define dies within the compile unit.
773 ///
774 FoldingSet<DIE> DiesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000775
Jim Laskeyef42a012006-11-02 20:12:39 +0000776 /// Dies - List of all dies in the compile unit.
777 ///
778 std::vector<DIE *> Dies;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000779
Jim Laskey0d086af2006-02-27 12:43:29 +0000780public:
Devang Pateld1ca9252009-01-05 23:03:32 +0000781 CompileUnit(unsigned I, DIE *D)
Devang Patelbbdc8202009-01-13 23:54:55 +0000782 : ID(I), Die(D), GVToDieMap(),
Devang Pateld1ca9252009-01-05 23:03:32 +0000783 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize), Dies()
784 {}
785
Jim Laskeyef42a012006-11-02 20:12:39 +0000786 ~CompileUnit() {
787 delete Die;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000788
Jim Laskeyef42a012006-11-02 20:12:39 +0000789 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
790 delete Dies[i];
791 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000792
Jim Laskeybd761842006-02-27 17:27:12 +0000793 // Accessors.
Jim Laskeyef42a012006-11-02 20:12:39 +0000794 unsigned getID() const { return ID; }
795 DIE* getDie() const { return Die; }
796 std::map<std::string, DIE *> &getGlobals() { return Globals; }
797
798 /// hasContent - Return true if this compile unit has something to write out.
799 ///
800 bool hasContent() const {
801 return !Die->getChildren().empty();
Jim Laskeya9c83fe2006-10-30 15:59:54 +0000802 }
Jim Laskeyef42a012006-11-02 20:12:39 +0000803
804 /// AddGlobal - Add a new global entity to the compile unit.
805 ///
806 void AddGlobal(const std::string &Name, DIE *Die) {
807 Globals[Name] = Die;
808 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000809
Jim Laskeyef42a012006-11-02 20:12:39 +0000810 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000811 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000812 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
813 return GVToDieMap[GV];
814 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000815
Jim Laskeyef42a012006-11-02 20:12:39 +0000816 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patelbbdc8202009-01-13 23:54:55 +0000817 /// specified debug variable.
Devang Patel48d190f2009-01-05 21:47:57 +0000818 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
819 return GVToDIEntryMap[GV];
820 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000821
Jim Laskeyef42a012006-11-02 20:12:39 +0000822 /// AddDie - Adds or interns the DIE to the compile unit.
823 ///
824 DIE *AddDie(DIE &Buffer) {
825 FoldingSetNodeID ID;
826 Buffer.Profile(ID);
827 void *Where;
828 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000829
Jim Laskeyef42a012006-11-02 20:12:39 +0000830 if (!Die) {
831 Die = new DIE(Buffer);
832 DiesSet.InsertNode(Die, Where);
833 this->Die->AddChild(Die);
834 Buffer.Detach();
835 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000836
Jim Laskeyef42a012006-11-02 20:12:39 +0000837 return Die;
838 }
Jim Laskey0d086af2006-02-27 12:43:29 +0000839};
840
Jim Laskey65195462006-10-30 13:35:07 +0000841//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000842/// Dwarf - Emits general Dwarf directives.
Jim Laskeyef42a012006-11-02 20:12:39 +0000843///
Jim Laskey65195462006-10-30 13:35:07 +0000844class Dwarf {
845
Jim Laskey072200c2007-01-29 18:51:14 +0000846protected:
Jim Laskey65195462006-10-30 13:35:07 +0000847
848 //===--------------------------------------------------------------------===//
Jim Laskey072200c2007-01-29 18:51:14 +0000849 // Core attributes used by the Dwarf writer.
Jim Laskey65195462006-10-30 13:35:07 +0000850 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000851
Jim Laskey65195462006-10-30 13:35:07 +0000852 //
853 /// O - Stream to .s file.
854 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000855 raw_ostream &O;
Jim Laskey65195462006-10-30 13:35:07 +0000856
857 /// Asm - Target of Dwarf emission.
858 ///
859 AsmPrinter *Asm;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000860
Bill Wendlingaa8f8882008-07-01 23:34:48 +0000861 /// TAI - Target asm information.
Jim Laskey65195462006-10-30 13:35:07 +0000862 const TargetAsmInfo *TAI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000863
Jim Laskey65195462006-10-30 13:35:07 +0000864 /// TD - Target data.
865 const TargetData *TD;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000866
Jim Laskey65195462006-10-30 13:35:07 +0000867 /// RI - Register Information.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000868 const TargetRegisterInfo *RI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000869
Jim Laskey65195462006-10-30 13:35:07 +0000870 /// M - Current module.
871 ///
872 Module *M;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000873
Jim Laskey65195462006-10-30 13:35:07 +0000874 /// MF - Current machine function.
875 ///
876 MachineFunction *MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000877
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000878 /// MMI - Collected machine module information.
Jim Laskey65195462006-10-30 13:35:07 +0000879 ///
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000880 MachineModuleInfo *MMI;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000881
Jim Laskey65195462006-10-30 13:35:07 +0000882 /// SubprogramCount - The running count of functions being compiled.
883 ///
884 unsigned SubprogramCount;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000885
Chris Lattner9251f132007-09-24 03:35:37 +0000886 /// Flavor - A unique string indicating what dwarf producer this is, used to
887 /// unique labels.
888 const char * const Flavor;
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000889
890 unsigned SetCounter;
Owen Andersoncb371882008-08-21 00:14:44 +0000891 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattner9251f132007-09-24 03:35:37 +0000892 const char *flavor)
Jim Laskey072200c2007-01-29 18:51:14 +0000893 : O(OS)
894 , Asm(A)
895 , TAI(T)
896 , TD(Asm->TM.getTargetData())
897 , RI(Asm->TM.getRegisterInfo())
898 , M(NULL)
899 , MF(NULL)
900 , MMI(NULL)
Jim Laskey072200c2007-01-29 18:51:14 +0000901 , SubprogramCount(0)
Chris Lattner9251f132007-09-24 03:35:37 +0000902 , Flavor(flavor)
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000903 , SetCounter(1)
Jim Laskey072200c2007-01-29 18:51:14 +0000904 {
905 }
906
907public:
908
909 //===--------------------------------------------------------------------===//
910 // Accessors.
911 //
912 AsmPrinter *getAsm() const { return Asm; }
Jim Laskeyb82313f2007-02-01 16:31:34 +0000913 MachineModuleInfo *getMMI() const { return MMI; }
Jim Laskey072200c2007-01-29 18:51:14 +0000914 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohman82482942007-09-27 23:12:31 +0000915 const TargetData *getTargetData() const { return TD; }
Jim Laskey072200c2007-01-29 18:51:14 +0000916
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000917 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
918 const {
919 if (isInSection && TAI->getDwarfSectionOffsetDirective())
920 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohman82482942007-09-27 23:12:31 +0000921 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000922 O << TAI->getData32bitsDirective();
923 else
924 O << TAI->getData64bitsDirective();
925 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000926
Jim Laskeyb82313f2007-02-01 16:31:34 +0000927 /// PrintLabelName - Print label name in form used by Dwarf writer.
928 ///
929 void PrintLabelName(DWLabel Label) const {
930 PrintLabelName(Label.Tag, Label.Number);
931 }
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000932 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000933 O << TAI->getPrivateGlobalPrefix() << Tag;
Jim Laskeyb82313f2007-02-01 16:31:34 +0000934 if (Number) O << Number;
935 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000936
Chris Lattner9251f132007-09-24 03:35:37 +0000937 void PrintLabelName(const char *Tag, unsigned Number,
938 const char *Suffix) const {
939 O << TAI->getPrivateGlobalPrefix() << Tag;
940 if (Number) O << Number;
941 O << Suffix;
942 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000943
Jim Laskeyb82313f2007-02-01 16:31:34 +0000944 /// EmitLabel - Emit location label for internal use by Dwarf.
945 ///
946 void EmitLabel(DWLabel Label) const {
947 EmitLabel(Label.Tag, Label.Number);
948 }
949 void EmitLabel(const char *Tag, unsigned Number) const {
950 PrintLabelName(Tag, Number);
951 O << ":\n";
952 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000953
Jim Laskeyb82313f2007-02-01 16:31:34 +0000954 /// EmitReference - Emit a reference to a label.
955 ///
Dan Gohman06ff4e62007-09-28 15:43:33 +0000956 void EmitReference(DWLabel Label, bool IsPCRelative = false,
957 bool Force32Bit = false) const {
958 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000959 }
960 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman06ff4e62007-09-28 15:43:33 +0000961 bool IsPCRelative = false, bool Force32Bit = false) const {
962 PrintRelDirective(Force32Bit);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000963 PrintLabelName(Tag, Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000964
Jim Laskeyb82313f2007-02-01 16:31:34 +0000965 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
966 }
Dan Gohman06ff4e62007-09-28 15:43:33 +0000967 void EmitReference(const std::string &Name, bool IsPCRelative = false,
968 bool Force32Bit = false) const {
969 PrintRelDirective(Force32Bit);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000970
Jim Laskeyb82313f2007-02-01 16:31:34 +0000971 O << Name;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000972
Jim Laskeyb82313f2007-02-01 16:31:34 +0000973 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
974 }
975
976 /// EmitDifference - Emit the difference between two labels. Some
977 /// assemblers do not behave with absolute expressions with data directives,
978 /// so there is an option (needsSet) to use an intermediary set expression.
979 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000980 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000981 EmitDifference(LabelHi.Tag, LabelHi.Number,
982 LabelLo.Tag, LabelLo.Number,
983 IsSmall);
984 }
985 void EmitDifference(const char *TagHi, unsigned NumberHi,
986 const char *TagLo, unsigned NumberLo,
Anton Korobeynikov6a143592007-03-07 08:25:02 +0000987 bool IsSmall = false) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000988 if (TAI->needsSet()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +0000989 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +0000990 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000991 O << ",";
992 PrintLabelName(TagHi, NumberHi);
993 O << "-";
994 PrintLabelName(TagLo, NumberLo);
995 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +0000996
997 PrintRelDirective(IsSmall);
Chris Lattner9251f132007-09-24 03:35:37 +0000998 PrintLabelName("set", SetCounter, Flavor);
Jim Laskeyb82313f2007-02-01 16:31:34 +0000999 ++SetCounter;
1000 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001001 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001002
Jim Laskeyb82313f2007-02-01 16:31:34 +00001003 PrintLabelName(TagHi, NumberHi);
1004 O << "-";
1005 PrintLabelName(TagLo, NumberLo);
1006 }
1007 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001008
1009 void EmitSectionOffset(const char* Label, const char* Section,
1010 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesend9ffd4c2008-03-26 23:31:39 +00001011 bool IsSmall = false, bool isEH = false,
1012 bool useSet = true) {
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001013 bool printAbsolute = false;
Dale Johannesend9ffd4c2008-03-26 23:31:39 +00001014 if (isEH)
1015 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
1016 else
1017 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
1018
1019 if (TAI->needsSet() && useSet) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001020 O << "\t.set\t";
Chris Lattner9251f132007-09-24 03:35:37 +00001021 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001022 O << ",";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001023 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001024
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001025 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001026 O << "-";
1027 PrintLabelName(Section, SectionNumber);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001028 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001029 O << "\n";
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001030
1031 PrintRelDirective(IsSmall);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001032
Chris Lattner9251f132007-09-24 03:35:37 +00001033 PrintLabelName("set", SetCounter, Flavor);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001034 ++SetCounter;
1035 } else {
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001036 PrintRelDirective(IsSmall, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001037
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00001038 PrintLabelName(Label, LabelNumber);
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001039
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +00001040 if (!printAbsolute) {
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001041 O << "-";
1042 PrintLabelName(Section, SectionNumber);
1043 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001044 }
Anton Korobeynikova6199c82007-03-07 02:47:57 +00001045 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001046
Jim Laskeyb82313f2007-02-01 16:31:34 +00001047 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1048 /// frame.
1049 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenb97aec62007-11-13 19:13:01 +00001050 const std::vector<MachineMove> &Moves, bool isEH) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001051 int stackGrowth =
1052 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1053 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00001054 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeybacd3042007-02-21 22:48:45 +00001055 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
Jim Laskeyb82313f2007-02-01 16:31:34 +00001056
1057 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00001058 const MachineMove &Move = Moves[i];
Jim Laskeyb82313f2007-02-01 16:31:34 +00001059 unsigned LabelID = Move.getLabelID();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001060
Jim Laskeyb82313f2007-02-01 16:31:34 +00001061 if (LabelID) {
1062 LabelID = MMI->MappedLabel(LabelID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001063
Jim Laskeyb82313f2007-02-01 16:31:34 +00001064 // Throw out move if the label is invalid.
1065 if (!LabelID) continue;
1066 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001067
Jim Laskeyb82313f2007-02-01 16:31:34 +00001068 const MachineLocation &Dst = Move.getDestination();
1069 const MachineLocation &Src = Move.getSource();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001070
Jim Laskeyb82313f2007-02-01 16:31:34 +00001071 // Advance row if new location.
1072 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1073 Asm->EmitInt8(DW_CFA_advance_loc4);
1074 Asm->EOL("DW_CFA_advance_loc4");
Jim Laskeybacd3042007-02-21 22:48:45 +00001075 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1076 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001077
Jim Laskeyb82313f2007-02-01 16:31:34 +00001078 BaseLabelID = LabelID;
Jim Laskeybacd3042007-02-21 22:48:45 +00001079 BaseLabel = "label";
Jim Laskeyb82313f2007-02-01 16:31:34 +00001080 IsLocal = true;
1081 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001082
Jim Laskeyb82313f2007-02-01 16:31:34 +00001083 // If advancing cfa.
Dan Gohmand735b802008-10-03 15:45:36 +00001084 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1085 if (!Src.isReg()) {
1086 if (Src.getReg() == MachineLocation::VirtualFP) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001087 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1088 Asm->EOL("DW_CFA_def_cfa_offset");
1089 } else {
1090 Asm->EmitInt8(DW_CFA_def_cfa);
1091 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmand735b802008-10-03 15:45:36 +00001092 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001093 Asm->EOL("Register");
1094 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001095
Jim Laskeyb82313f2007-02-01 16:31:34 +00001096 int Offset = -Src.getOffset();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001097
Jim Laskeyb82313f2007-02-01 16:31:34 +00001098 Asm->EmitULEB128Bytes(Offset);
1099 Asm->EOL("Offset");
1100 } else {
1101 assert(0 && "Machine move no supported yet.");
1102 }
Dan Gohmand735b802008-10-03 15:45:36 +00001103 } else if (Src.isReg() &&
1104 Src.getReg() == MachineLocation::VirtualFP) {
1105 if (Dst.isReg()) {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001106 Asm->EmitInt8(DW_CFA_def_cfa_register);
1107 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmand735b802008-10-03 15:45:36 +00001108 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Jim Laskeyb82313f2007-02-01 16:31:34 +00001109 Asm->EOL("Register");
1110 } else {
1111 assert(0 && "Machine move no supported yet.");
1112 }
1113 } else {
Dan Gohmand735b802008-10-03 15:45:36 +00001114 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Jim Laskeyb82313f2007-02-01 16:31:34 +00001115 int Offset = Dst.getOffset() / stackGrowth;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001116
Jim Laskeyb82313f2007-02-01 16:31:34 +00001117 if (Offset < 0) {
1118 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1119 Asm->EOL("DW_CFA_offset_extended_sf");
1120 Asm->EmitULEB128Bytes(Reg);
1121 Asm->EOL("Reg");
1122 Asm->EmitSLEB128Bytes(Offset);
1123 Asm->EOL("Offset");
1124 } else if (Reg < 64) {
1125 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Chengd4114192008-07-09 21:53:02 +00001126 if (VerboseAsm)
1127 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1128 else
1129 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00001130 Asm->EmitULEB128Bytes(Offset);
1131 Asm->EOL("Offset");
1132 } else {
1133 Asm->EmitInt8(DW_CFA_offset_extended);
1134 Asm->EOL("DW_CFA_offset_extended");
1135 Asm->EmitULEB128Bytes(Reg);
1136 Asm->EOL("Reg");
1137 Asm->EmitULEB128Bytes(Offset);
1138 Asm->EOL("Offset");
1139 }
1140 }
1141 }
1142 }
1143
Jim Laskey072200c2007-01-29 18:51:14 +00001144};
1145
1146//===----------------------------------------------------------------------===//
Devang Patelf3ee5142009-01-12 22:54:42 +00001147/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001148///
1149class SrcLineInfo {
1150 unsigned Line; // Source line number.
1151 unsigned Column; // Source column.
1152 unsigned SourceID; // Source ID number.
1153 unsigned LabelID; // Label in code ID number.
1154public:
1155 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
1156 : Line(L), Column(C), SourceID(S), LabelID(I) {}
1157
1158 // Accessors
1159 unsigned getLine() const { return Line; }
1160 unsigned getColumn() const { return Column; }
1161 unsigned getSourceID() const { return SourceID; }
1162 unsigned getLabelID() const { return LabelID; }
1163};
1164
1165
1166//===----------------------------------------------------------------------===//
Devang Patel8526cc02009-01-05 22:35:52 +00001167/// SrcFileInfo - This class is used to track source information.
1168///
1169class SrcFileInfo {
1170 unsigned DirectoryID; // Directory ID number.
1171 std::string Name; // File name (not including directory.)
1172public:
1173 SrcFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
1174
1175 // Accessors
1176 unsigned getDirectoryID() const { return DirectoryID; }
1177 const std::string &getName() const { return Name; }
1178
1179 /// operator== - Used by UniqueVector to locate entry.
1180 ///
Devang Patelbbdc8202009-01-13 23:54:55 +00001181 bool operator==(const SrcFileInfo &SI) const {
Devang Patel8526cc02009-01-05 22:35:52 +00001182 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
1183 }
1184
1185 /// operator< - Used by UniqueVector to locate entry.
1186 ///
1187 bool operator<(const SrcFileInfo &SI) const {
1188 return getDirectoryID() < SI.getDirectoryID() ||
1189 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
1190 }
1191};
1192
1193//===----------------------------------------------------------------------===//
Devang Patel7a6e5a32009-01-08 02:33:41 +00001194/// DbgVariable - This class is used to track local variable information.
1195///
1196class DbgVariable {
1197private:
1198 DIVariable *Var; // Variable Descriptor.
1199 unsigned FrameIndex; // Variable frame index.
1200
1201public:
1202 DbgVariable(DIVariable *V, unsigned I) : Var(V), FrameIndex(I) {}
1203
1204 // Accessors.
1205 DIVariable *getVariable() const { return Var; }
1206 unsigned getFrameIndex() const { return FrameIndex; }
1207};
1208
1209//===----------------------------------------------------------------------===//
1210/// DbgScope - This class is used to track scope information.
1211///
1212class DbgScope {
1213private:
1214 DbgScope *Parent; // Parent to this scope.
Devang Patel0e5200f2009-01-15 18:25:17 +00001215 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001216 // Either subprogram or block.
1217 unsigned StartLabelID; // Label ID of the beginning of scope.
1218 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel9795da52009-01-10 02:42:49 +00001219 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
1220 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001221
1222public:
Devang Patel0e5200f2009-01-15 18:25:17 +00001223 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel7a6e5a32009-01-08 02:33:41 +00001224 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1225 {}
Devang Patel481ff5b2009-01-12 18:48:36 +00001226 ~DbgScope() {
1227 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1228 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1229 }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001230
1231 // Accessors.
1232 DbgScope *getParent() const { return Parent; }
Devang Patel0e5200f2009-01-15 18:25:17 +00001233 DIDescriptor getDesc() const { return Desc; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001234 unsigned getStartLabelID() const { return StartLabelID; }
1235 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel9795da52009-01-10 02:42:49 +00001236 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1237 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel7a6e5a32009-01-08 02:33:41 +00001238 void setStartLabelID(unsigned S) { StartLabelID = S; }
1239 void setEndLabelID(unsigned E) { EndLabelID = E; }
1240
1241 /// AddScope - Add a scope to the scope.
1242 ///
1243 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1244
1245 /// AddVariable - Add a variable to the scope.
1246 ///
1247 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1248};
1249
1250//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001251/// DwarfDebug - Emits Dwarf debug directives.
Jim Laskey072200c2007-01-29 18:51:14 +00001252///
1253class DwarfDebug : public Dwarf {
1254
1255private:
Jim Laskey65195462006-10-30 13:35:07 +00001256 //===--------------------------------------------------------------------===//
1257 // Attributes used to construct specific Dwarf sections.
1258 //
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001259
Jim Laskey65195462006-10-30 13:35:07 +00001260 /// CompileUnits - All the compile units involved in this build. The index
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001261 /// of each entry in this vector corresponds to the sources in MMI.
Jim Laskey65195462006-10-30 13:35:07 +00001262 std::vector<CompileUnit *> CompileUnits;
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001263 DenseMap<Value *, CompileUnit *> DW_CUs;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001264
Jim Laskeyef42a012006-11-02 20:12:39 +00001265 /// AbbreviationsSet - Used to uniquely define abbreviations.
Jim Laskey65195462006-10-30 13:35:07 +00001266 ///
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001267 FoldingSet<DIEAbbrev> AbbreviationsSet;
1268
1269 /// Abbreviations - A list of all the unique abbreviations in use.
1270 ///
1271 std::vector<DIEAbbrev *> Abbreviations;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001272
Jim Laskeyef42a012006-11-02 20:12:39 +00001273 /// ValuesSet - Used to uniquely define values.
1274 ///
Devang Patel8526cc02009-01-05 22:35:52 +00001275 // Directories - Uniquing vector for directories.
1276 UniqueVector<std::string> Directories;
1277
1278 // SourceFiles - Uniquing vector for source files.
1279 UniqueVector<SrcFileInfo> SrcFiles;
1280
Devang Patel9f8fcfc2009-01-08 17:19:22 +00001281 // Lines - List of of source line correspondence.
1282 std::vector<SrcLineInfo> Lines;
1283
Jim Laskeyef42a012006-11-02 20:12:39 +00001284 FoldingSet<DIEValue> ValuesSet;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001285
Jim Laskeyef42a012006-11-02 20:12:39 +00001286 /// Values - A list of all the unique values in use.
1287 ///
1288 std::vector<DIEValue *> Values;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001289
Jim Laskey65195462006-10-30 13:35:07 +00001290 /// StringPool - A UniqueVector of strings used by indirect references.
Jim Laskeyef42a012006-11-02 20:12:39 +00001291 ///
Jim Laskey65195462006-10-30 13:35:07 +00001292 UniqueVector<std::string> StringPool;
1293
Jim Laskey65195462006-10-30 13:35:07 +00001294 /// SectionMap - Provides a unique id per text section.
1295 ///
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00001296 UniqueVector<const Section*> SectionMap;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001297
Jim Laskey65195462006-10-30 13:35:07 +00001298 /// SectionSourceLines - Tracks line numbers per text section.
1299 ///
Devang Patelf3ee5142009-01-12 22:54:42 +00001300 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Jim Laskey65195462006-10-30 13:35:07 +00001301
Jim Laskeybacd3042007-02-21 22:48:45 +00001302 /// didInitial - Flag to indicate if initial emission has been done.
1303 ///
1304 bool didInitial;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001305
Jim Laskeybacd3042007-02-21 22:48:45 +00001306 /// shouldEmit - Flag to indicate if debug information should be emitted.
1307 ///
1308 bool shouldEmit;
Jim Laskey65195462006-10-30 13:35:07 +00001309
Devang Patel0e5200f2009-01-15 18:25:17 +00001310 // RootDbgScope - Top level scope for the current function.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001311 //
1312 DbgScope *RootDbgScope;
1313
1314 // DbgScopeMap - Tracks the scopes in the current function.
1315 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
1316
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001317 struct FunctionDebugFrameInfo {
1318 unsigned Number;
1319 std::vector<MachineMove> Moves;
1320
1321 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman81975f62007-08-27 14:50:10 +00001322 Number(Num), Moves(M) { }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00001323 };
1324
1325 std::vector<FunctionDebugFrameInfo> DebugFrames;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001326
Jim Laskey65195462006-10-30 13:35:07 +00001327public:
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001328
Jim Laskeybacd3042007-02-21 22:48:45 +00001329 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1330 ///
1331 bool ShouldEmitDwarf() const { return shouldEmit; }
Jim Laskey65195462006-10-30 13:35:07 +00001332
Jim Laskeya9c83fe2006-10-30 15:59:54 +00001333 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001334 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001335 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1336 // Profile the node so that we can make it unique.
1337 FoldingSetNodeID ID;
1338 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001339
Jim Laskeyef42a012006-11-02 20:12:39 +00001340 // Check the set for priors.
1341 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001342
Jim Laskeyef42a012006-11-02 20:12:39 +00001343 // If it's newly added.
1344 if (InSet == &Abbrev) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001345 // Add to abbreviation list.
Jim Laskeyef42a012006-11-02 20:12:39 +00001346 Abbreviations.push_back(&Abbrev);
1347 // Assign the vector position + 1 as its number.
1348 Abbrev.setNumber(Abbreviations.size());
1349 } else {
1350 // Assign existing abbreviation number.
1351 Abbrev.setNumber(InSet->getNumber());
1352 }
1353 }
1354
Jim Laskey65195462006-10-30 13:35:07 +00001355 /// NewString - Add a string to the constant pool and returns a label.
1356 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00001357 DWLabel NewString(const std::string &String) {
1358 unsigned StringID = StringPool.insert(String);
1359 return DWLabel("string", StringID);
1360 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001361
Jim Laskeyef42a012006-11-02 20:12:39 +00001362 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1363 /// entry.
1364 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1365 DIEntry *Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001366
Jim Laskeyef42a012006-11-02 20:12:39 +00001367 if (Entry) {
1368 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001369 DIEntry::Profile(ID, Entry);
Jim Laskeyef42a012006-11-02 20:12:39 +00001370 void *Where;
1371 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001372
Jim Laskeyf6733882006-11-02 21:48:18 +00001373 if (Value) return Value;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001374
Jim Laskeyef42a012006-11-02 20:12:39 +00001375 Value = new DIEntry(Entry);
1376 ValuesSet.InsertNode(Value, Where);
1377 } else {
1378 Value = new DIEntry(Entry);
1379 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001380
Jim Laskeyef42a012006-11-02 20:12:39 +00001381 Values.push_back(Value);
1382 return Value;
1383 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001384
Jim Laskeyef42a012006-11-02 20:12:39 +00001385 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1386 ///
1387 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1388 Value->Entry = Entry;
1389 // Add to values set if not already there. If it is, we merely have a
1390 // duplicate in the values list (no harm.)
1391 ValuesSet.GetOrInsertNode(Value);
1392 }
1393
1394 /// AddUInt - Add an unsigned integer attribute data and value.
1395 ///
1396 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1397 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1398
1399 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001400 DIEInteger::Profile(ID, Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001401 void *Where;
1402 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1403 if (!Value) {
1404 Value = new DIEInteger(Integer);
1405 ValuesSet.InsertNode(Value, Where);
1406 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001407 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001408
Jim Laskeyef42a012006-11-02 20:12:39 +00001409 Die->AddValue(Attribute, Form, Value);
1410 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001411
Jim Laskeyef42a012006-11-02 20:12:39 +00001412 /// AddSInt - Add an signed integer attribute data and value.
1413 ///
1414 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1415 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1416
1417 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001418 DIEInteger::Profile(ID, (uint64_t)Integer);
Jim Laskeyef42a012006-11-02 20:12:39 +00001419 void *Where;
1420 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1421 if (!Value) {
1422 Value = new DIEInteger(Integer);
1423 ValuesSet.InsertNode(Value, Where);
1424 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001425 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001426
Jim Laskeyef42a012006-11-02 20:12:39 +00001427 Die->AddValue(Attribute, Form, Value);
1428 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001429
Jim Laskeyef42a012006-11-02 20:12:39 +00001430 /// AddString - Add a std::string attribute data and value.
1431 ///
1432 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1433 const std::string &String) {
1434 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001435 DIEString::Profile(ID, String);
Jim Laskeyef42a012006-11-02 20:12:39 +00001436 void *Where;
1437 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1438 if (!Value) {
1439 Value = new DIEString(String);
1440 ValuesSet.InsertNode(Value, Where);
1441 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001442 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001443
Jim Laskeyef42a012006-11-02 20:12:39 +00001444 Die->AddValue(Attribute, Form, Value);
1445 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001446
Jim Laskeyef42a012006-11-02 20:12:39 +00001447 /// AddLabel - Add a Dwarf label attribute data and value.
1448 ///
1449 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1450 const DWLabel &Label) {
1451 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001452 DIEDwarfLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001453 void *Where;
1454 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1455 if (!Value) {
1456 Value = new DIEDwarfLabel(Label);
1457 ValuesSet.InsertNode(Value, Where);
1458 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001459 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001460
Jim Laskeyef42a012006-11-02 20:12:39 +00001461 Die->AddValue(Attribute, Form, Value);
1462 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001463
Jim Laskeyef42a012006-11-02 20:12:39 +00001464 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1465 ///
1466 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1467 const std::string &Label) {
1468 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001469 DIEObjectLabel::Profile(ID, Label);
Jim Laskeyef42a012006-11-02 20:12:39 +00001470 void *Where;
1471 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1472 if (!Value) {
1473 Value = new DIEObjectLabel(Label);
1474 ValuesSet.InsertNode(Value, Where);
1475 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001476 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001477
Jim Laskeyef42a012006-11-02 20:12:39 +00001478 Die->AddValue(Attribute, Form, Value);
1479 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001480
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001481 /// AddSectionOffset - Add a section offset label attribute data and value.
1482 ///
1483 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1484 const DWLabel &Label, const DWLabel &Section,
1485 bool isEH = false, bool useSet = true) {
1486 FoldingSetNodeID ID;
1487 DIESectionOffset::Profile(ID, Label, Section);
1488 void *Where;
1489 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1490 if (!Value) {
1491 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1492 ValuesSet.InsertNode(Value, Where);
1493 Values.push_back(Value);
1494 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001495
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00001496 Die->AddValue(Attribute, Form, Value);
1497 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001498
Jim Laskeyef42a012006-11-02 20:12:39 +00001499 /// AddDelta - Add a label delta attribute data and value.
1500 ///
1501 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1502 const DWLabel &Hi, const DWLabel &Lo) {
1503 FoldingSetNodeID ID;
Jim Laskey5496f012006-11-09 14:52:14 +00001504 DIEDelta::Profile(ID, Hi, Lo);
Jim Laskeyef42a012006-11-02 20:12:39 +00001505 void *Where;
1506 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1507 if (!Value) {
1508 Value = new DIEDelta(Hi, Lo);
1509 ValuesSet.InsertNode(Value, Where);
1510 Values.push_back(Value);
Jim Laskeyef42a012006-11-02 20:12:39 +00001511 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001512
Jim Laskeyef42a012006-11-02 20:12:39 +00001513 Die->AddValue(Attribute, Form, Value);
1514 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001515
Jim Laskeyef42a012006-11-02 20:12:39 +00001516 /// AddDIEntry - Add a DIE attribute data and value.
1517 ///
1518 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1519 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1520 }
1521
1522 /// AddBlock - Add block data.
1523 ///
1524 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1525 Block->ComputeSize(*this);
1526 FoldingSetNodeID ID;
1527 Block->Profile(ID);
1528 void *Where;
1529 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1530 if (!Value) {
1531 Value = Block;
1532 ValuesSet.InsertNode(Value, Where);
1533 Values.push_back(Value);
1534 } else {
Chris Lattnerc369bd72007-09-21 18:25:53 +00001535 // Already exists, reuse the previous one.
Jim Laskeyef42a012006-11-02 20:12:39 +00001536 delete Block;
Chris Lattnerc369bd72007-09-21 18:25:53 +00001537 Block = cast<DIEBlock>(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, Block->BestForm(), Value);
1541 }
1542
Jim Laskey65195462006-10-30 13:35:07 +00001543private:
1544
1545 /// AddSourceLine - Add location information to specified debug information
Jim Laskeyef42a012006-11-02 20:12:39 +00001546 /// entry.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001547 void AddSourceLine(DIE *Die, DIVariable *V) {
1548 unsigned FileID = 0;
1549 unsigned Line = V->getLineNumber();
1550 if (V->getVersion() < DIDescriptor::Version7) {
1551 // Version6 or earlier. Use compile unit info to get file id.
1552 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1553 FileID = Unit->getID();
1554 } else {
1555 // Version7 or newer, use filename and directory info from DIVariable
1556 // directly.
1557 unsigned DID = Directories.idFor(V->getDirectory());
1558 FileID = SrcFiles.idFor(SrcFileInfo(DID, V->getFilename()));
1559 }
1560 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1561 AddUInt(Die, DW_AT_decl_line, 0, Line);
1562 }
1563
1564 /// AddSourceLine - Add location information to specified debug information
1565 /// entry.
Devang Patel8526cc02009-01-05 22:35:52 +00001566 void AddSourceLine(DIE *Die, DIGlobal *G) {
1567 unsigned FileID = 0;
1568 unsigned Line = G->getLineNumber();
1569 if (G->getVersion() < DIDescriptor::Version7) {
1570 // Version6 or earlier. Use compile unit info to get file id.
1571 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1572 FileID = Unit->getID();
1573 } else {
1574 // Version7 or newer, use filename and directory info from DIGlobal
1575 // directly.
1576 unsigned DID = Directories.idFor(G->getDirectory());
1577 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1578 }
1579 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1580 AddUInt(Die, DW_AT_decl_line, 0, Line);
1581 }
1582
1583 void AddSourceLine(DIE *Die, DIType *G) {
1584 unsigned FileID = 0;
1585 unsigned Line = G->getLineNumber();
1586 if (G->getVersion() < DIDescriptor::Version7) {
1587 // Version6 or earlier. Use compile unit info to get file id.
1588 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1589 FileID = Unit->getID();
1590 } else {
1591 // Version7 or newer, use filename and directory info from DIGlobal
1592 // directly.
1593 unsigned DID = Directories.idFor(G->getDirectory());
1594 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1595 }
1596 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1597 AddUInt(Die, DW_AT_decl_line, 0, Line);
1598 }
1599
Jim Laskey65195462006-10-30 13:35:07 +00001600 /// AddAddress - Add an address attribute to a die based on the location
1601 /// provided.
1602 void AddAddress(DIE *Die, unsigned Attribute,
Jim Laskeyef42a012006-11-02 20:12:39 +00001603 const MachineLocation &Location) {
Dan Gohmand735b802008-10-03 15:45:36 +00001604 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Jim Laskeyef42a012006-11-02 20:12:39 +00001605 DIEBlock *Block = new DIEBlock();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001606
Dan Gohmand735b802008-10-03 15:45:36 +00001607 if (Location.isReg()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001608 if (Reg < 32) {
1609 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1610 } else {
1611 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1612 AddUInt(Block, 0, DW_FORM_udata, Reg);
1613 }
1614 } else {
1615 if (Reg < 32) {
1616 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1617 } else {
1618 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1619 AddUInt(Block, 0, DW_FORM_udata, Reg);
1620 }
1621 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1622 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001623
Jim Laskeyef42a012006-11-02 20:12:39 +00001624 AddBlock(Die, Attribute, 0, Block);
1625 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001626
Jim Laskeyef42a012006-11-02 20:12:39 +00001627 /// AddBasicType - Add a new basic type attribute to the specified entity.
1628 ///
1629 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1630 const std::string &Name,
1631 unsigned Encoding, unsigned Size) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001632
Jim Laskeyef42a012006-11-02 20:12:39 +00001633 DIE Buffer(DW_TAG_base_type);
1634 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1635 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1636 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patel9ede3f22009-01-05 17:44:11 +00001637 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1638 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001639 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001640
Jim Laskeyef42a012006-11-02 20:12:39 +00001641 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1642 ///
1643 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
Jim Laskeyef42a012006-11-02 20:12:39 +00001644 DIE Buffer(DW_TAG_pointer_type);
Dan Gohman82482942007-09-27 23:12:31 +00001645 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Jim Laskeyef42a012006-11-02 20:12:39 +00001646 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelca03c272009-01-05 17:45:59 +00001647 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1648 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
Jim Laskeyef42a012006-11-02 20:12:39 +00001649 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00001650
Jim Laskeyef42a012006-11-02 20:12:39 +00001651 /// AddType - Add a new type attribute to the specified entity.
Devang Patel48d190f2009-01-05 21:47:57 +00001652 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
1653 if (Ty.isNull()) {
1654 AddBasicType(Entity, DW_Unit, "", DW_ATE_signed, sizeof(int32_t));
1655 return;
1656 }
1657
1658 // Check for pre-existence.
1659 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1660 // If it exists then use the existing value.
1661 if (Slot) {
1662 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1663 return;
1664 }
1665
1666 // Set up proxy.
1667 Slot = NewDIEntry();
1668
1669 // Construct type.
1670 DIE Buffer(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001671 if (Ty.isBasicType(Ty.getTag()))
1672 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1673 else if (Ty.isDerivedType(Ty.getTag()))
1674 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1675 else {
1676 assert (Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
1677 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1678 }
1679
Devang Patel48d190f2009-01-05 21:47:57 +00001680 // Add debug information entry to entity and unit.
1681 DIE *Die = DW_Unit->AddDie(Buffer);
1682 SetDIEntry(Slot, Die);
1683 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1684 }
1685
Devang Patele5202732009-01-05 19:07:53 +00001686 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1687 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001688 DIBasicType BTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001689
1690 // Get core information.
Devang Patelf193ff02009-01-15 19:26:23 +00001691 const std::string &Name = BTy.getName();
Devang Patel08f053f2009-01-05 17:57:47 +00001692 Buffer.setTag(DW_TAG_base_type);
Devang Patelf193ff02009-01-15 19:26:23 +00001693 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patel08f053f2009-01-05 17:57:47 +00001694 // Add name if not anonymous or intermediate type.
1695 if (!Name.empty())
1696 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf193ff02009-01-15 19:26:23 +00001697 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel08f053f2009-01-05 17:57:47 +00001698 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1699 }
1700
Devang Patele5202732009-01-05 19:07:53 +00001701 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1702 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001703 DIDerivedType DTy) {
Devang Patel08f053f2009-01-05 17:57:47 +00001704
1705 // Get core information.
Devang Patelf193ff02009-01-15 19:26:23 +00001706 const std::string &Name = DTy.getName();
1707 uint64_t Size = DTy.getSizeInBits() >> 3;
1708 unsigned Tag = DTy.getTag();
Devang Patel08f053f2009-01-05 17:57:47 +00001709 // FIXME - Workaround for templates.
1710 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1711
1712 Buffer.setTag(Tag);
1713 // Map to main type, void will not have a type.
Devang Patelf193ff02009-01-15 19:26:23 +00001714 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001715 AddType(DW_Unit, &Buffer, FromTy);
Devang Patel08f053f2009-01-05 17:57:47 +00001716
1717 // Add name if not anonymous or intermediate type.
1718 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1719
1720 // Add size if non-zero (derived types might be zero-sized.)
1721 if (Size)
1722 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1723
1724 // Add source line info if available and TyDesc is not a forward
1725 // declaration.
Devang Patelf193ff02009-01-15 19:26:23 +00001726 // FIXME - Enable this. if (!DTy.isForwardDecl())
Devang Patel08f053f2009-01-05 17:57:47 +00001727 // FIXME - Enable this. AddSourceLine(&Buffer, *DTy);
1728 }
1729
Devang Patelf4215332009-01-05 19:55:51 +00001730 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1731 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001732 DICompositeType CTy) {
Devang Patelf4215332009-01-05 19:55:51 +00001733
1734 // Get core information.
Devang Patelf193ff02009-01-15 19:26:23 +00001735 const std::string &Name = CTy.getName();
1736 uint64_t Size = CTy.getSizeInBits() >> 3;
1737 unsigned Tag = CTy.getTag();
Devang Patelf4215332009-01-05 19:55:51 +00001738 switch (Tag) {
1739 case DW_TAG_vector_type:
1740 case DW_TAG_array_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001741 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patelf4215332009-01-05 19:55:51 +00001742 break;
1743 //FIXME - Enable this.
1744 // case DW_TAG_enumeration_type:
Devang Patelf193ff02009-01-15 19:26:23 +00001745 // DIArray Elements = CTy.getTypeArray();
Devang Patelf4215332009-01-05 19:55:51 +00001746 // // Add enumerators to enumeration type.
1747 // for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i)
1748 // ConstructEnumTypeDIE(Buffer, &Elements.getElement(i));
1749 // break;
1750 case DW_TAG_subroutine_type:
1751 {
1752 // Add prototype flag.
1753 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelf193ff02009-01-15 19:26:23 +00001754 DIArray Elements = CTy.getTypeArray();
Devang Patelf4215332009-01-05 19:55:51 +00001755 // Add return type.
Devang Patel48d190f2009-01-05 21:47:57 +00001756 DIDescriptor RTy = Elements.getElement(0);
Devang Patelf193ff02009-01-15 19:26:23 +00001757 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel48d190f2009-01-05 21:47:57 +00001758
1759 //AddType(DW_Unit, &Buffer, Elements.getElement(0));
Devang Patelf4215332009-01-05 19:55:51 +00001760 // Add arguments.
1761 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1762 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel48d190f2009-01-05 21:47:57 +00001763 DIDescriptor Ty = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001764 AddType(DW_Unit, &Buffer, DIType(Ty.getGV()));
Devang Patelf4215332009-01-05 19:55:51 +00001765 Buffer.AddChild(Arg);
1766 }
1767 }
1768 break;
1769 case DW_TAG_structure_type:
1770 case DW_TAG_union_type:
1771 {
1772 // Add elements to structure type.
Devang Patelf193ff02009-01-15 19:26:23 +00001773 DIArray Elements = CTy.getTypeArray();
Devang Patelf4215332009-01-05 19:55:51 +00001774 // Add elements to structure type.
1775 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1776 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001777 if (Element.getTag() == dwarf::DW_TAG_subprogram)
1778 ConstructFieldTypeDIE(DW_Unit, Buffer, DISubprogram(Element.getGV()));
1779 else if (Element.getTag() == dwarf::DW_TAG_variable)
1780 ConstructFieldTypeDIE(DW_Unit, Buffer,
1781 DIGlobalVariable(Element.getGV()));
1782 else {
1783 DIDerivedType DT = DIDerivedType(Element.getGV());
1784 assert (DT.isDerivedType(DT.getTag()) && "Unexpected strcut element");
Devang Patelf4215332009-01-05 19:55:51 +00001785 ConstructFieldTypeDIE(DW_Unit, Buffer, DT);
Devang Patelf193ff02009-01-15 19:26:23 +00001786 }
Devang Patelf4215332009-01-05 19:55:51 +00001787 }
1788 }
1789 break;
1790 default:
1791 break;
1792 }
1793
1794 // Add name if not anonymous or intermediate type.
1795 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1796
1797 // Add size if non-zero (derived types might be zero-sized.)
1798 if (Size)
1799 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1800 else {
1801 // Add zero size even if it is not a forward declaration.
1802 // FIXME - Enable this.
Devang Patelf193ff02009-01-15 19:26:23 +00001803 // if (!CTy.isDefinition())
Devang Patelf4215332009-01-05 19:55:51 +00001804 // AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1805 // else
1806 // AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1807 }
1808
1809 // Add source line info if available and TyDesc is not a forward
1810 // declaration.
1811 // FIXME - Enable this.
Devang Patelf193ff02009-01-15 19:26:23 +00001812 // if (CTy.isForwardDecl())
Devang Patelf4215332009-01-05 19:55:51 +00001813 // AddSourceLine(&Buffer, *CTy);
1814 }
1815
Devang Patel68afdc32009-01-05 18:33:01 +00001816 // ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
Devang Patelf193ff02009-01-15 19:26:23 +00001817 void ConstructSubrangeDIE (DIE &Buffer, DISubrange SR, DIE *IndexTy) {
1818 int64_t L = SR.getLo();
1819 int64_t H = SR.getHi();
Devang Patel68afdc32009-01-05 18:33:01 +00001820 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1821 if (L != H) {
1822 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1823 if (L)
1824 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1825 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
1826 }
1827 Buffer.AddChild(DW_Subrange);
1828 }
1829
1830 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1831 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1832 DICompositeType *CTy) {
1833 Buffer.setTag(DW_TAG_array_type);
1834 if (CTy->getTag() == DW_TAG_vector_type)
1835 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1836
1837 DIArray Elements = CTy->getTypeArray();
1838 // FIXME - Enable this.
Devang Patel48d190f2009-01-05 21:47:57 +00001839 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel68afdc32009-01-05 18:33:01 +00001840
1841 // Construct an anonymous type for index type.
1842 DIE IdxBuffer(DW_TAG_base_type);
1843 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1844 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1845 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1846
1847 // Add subranges to array type.
1848 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patelf4215332009-01-05 19:55:51 +00001849 DIDescriptor Element = Elements.getElement(i);
Devang Patelf193ff02009-01-15 19:26:23 +00001850 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1851 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel68afdc32009-01-05 18:33:01 +00001852 }
1853 }
1854
Devang Patelc69bf2c2009-01-05 18:38:38 +00001855 /// ConstructEnumTypeDIE - Construct enum type DIE from
1856 /// DIEnumerator.
Devang Patelf4215332009-01-05 19:55:51 +00001857 void ConstructEnumTypeDIE(CompileUnit *DW_Unit,
1858 DIE &Buffer, DIEnumerator *ETy) {
Devang Patelc69bf2c2009-01-05 18:38:38 +00001859
1860 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1861 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1862 int64_t Value = ETy->getEnumValue();
1863 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1864 Buffer.AddChild(Enumerator);
1865 }
Devang Patel68afdc32009-01-05 18:33:01 +00001866
Devang Patel86ae1422009-01-05 18:59:44 +00001867 /// ConstructFieldTypeDIE - Construct variable DIE for a struct field.
1868 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
Devang Patelf193ff02009-01-15 19:26:23 +00001869 DIE &Buffer, DIGlobalVariable V) {
Devang Patel86ae1422009-01-05 18:59:44 +00001870
1871 DIE *VariableDie = new DIE(DW_TAG_variable);
Devang Patelf193ff02009-01-15 19:26:23 +00001872 const std::string &LinkageName = V.getLinkageName();
Devang Patel86ae1422009-01-05 18:59:44 +00001873 if (!LinkageName.empty())
1874 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1875 LinkageName);
1876 // FIXME - Enable this. AddSourceLine(VariableDie, V);
Devang Patelf193ff02009-01-15 19:26:23 +00001877 AddType(DW_Unit, VariableDie, V.getType());
1878 if (!V.isLocalToUnit())
Devang Patel86ae1422009-01-05 18:59:44 +00001879 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1880 AddUInt(VariableDie, DW_AT_declaration, DW_FORM_flag, 1);
1881 Buffer.AddChild(VariableDie);
1882 }
1883
1884 /// ConstructFieldTypeDIE - Construct subprogram DIE for a struct field.
1885 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
Devang Patelf193ff02009-01-15 19:26:23 +00001886 DIE &Buffer, DISubprogram SP,
Devang Patel86ae1422009-01-05 18:59:44 +00001887 bool IsConstructor = false) {
1888 DIE *Method = new DIE(DW_TAG_subprogram);
Devang Patelf193ff02009-01-15 19:26:23 +00001889 AddString(Method, DW_AT_name, DW_FORM_string, SP.getName());
1890 const std::string &LinkageName = SP.getLinkageName();
Devang Patel86ae1422009-01-05 18:59:44 +00001891 if (!LinkageName.empty())
1892 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1893 // FIXME - Enable this. AddSourceLine(Method, SP);
1894
Devang Patelf193ff02009-01-15 19:26:23 +00001895 DICompositeType MTy = SP.getType();
Devang Patel86ae1422009-01-05 18:59:44 +00001896 DIArray Args = MTy.getTypeArray();
1897
1898 // Add Return Type.
Devang Patelf193ff02009-01-15 19:26:23 +00001899 if (!IsConstructor)
1900 AddType(DW_Unit, Method, DIType(Args.getElement(0).getGV()));
Devang Patel86ae1422009-01-05 18:59:44 +00001901
1902 // Add arguments.
1903 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1904 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patelf193ff02009-01-15 19:26:23 +00001905 AddType(DW_Unit, Method, DIType(Args.getElement(i).getGV()));
Devang Patel86ae1422009-01-05 18:59:44 +00001906 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1907 Method->AddChild(Arg);
1908 }
1909
Devang Patelf193ff02009-01-15 19:26:23 +00001910 if (!SP.isLocalToUnit())
Devang Patel86ae1422009-01-05 18:59:44 +00001911 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
1912 Buffer.AddChild(Method);
1913 }
1914
Devang Patelf193ff02009-01-15 19:26:23 +00001915 /// ConstructFieldTypeDIE - Construct derived type DIE for a struct field.
Devang Patel86ae1422009-01-05 18:59:44 +00001916 void ConstructFieldTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelf193ff02009-01-15 19:26:23 +00001917 DIDerivedType DTy) {
1918 unsigned Tag = DTy.getTag();
Devang Patel86ae1422009-01-05 18:59:44 +00001919 DIE *MemberDie = new DIE(Tag);
Devang Patelf193ff02009-01-15 19:26:23 +00001920 if (!DTy.getName().empty())
1921 AddString(MemberDie, DW_AT_name, DW_FORM_string, DTy.getName());
Devang Patel86ae1422009-01-05 18:59:44 +00001922 // FIXME - Enable this. AddSourceLine(MemberDie, DTy);
1923
Devang Patelf193ff02009-01-15 19:26:23 +00001924 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel48d190f2009-01-05 21:47:57 +00001925 AddType(DW_Unit, MemberDie, FromTy);
Devang Patel86ae1422009-01-05 18:59:44 +00001926
Devang Patelf193ff02009-01-15 19:26:23 +00001927 uint64_t Size = DTy.getSizeInBits();
1928 uint64_t Offset = DTy.getOffsetInBits();
Devang Patel86ae1422009-01-05 18:59:44 +00001929
1930 // FIXME Handle bitfields
1931
1932 // Add size.
1933 AddUInt(MemberDie, DW_AT_bit_size, 0, Size);
1934 // Add computation for offset.
1935 DIEBlock *Block = new DIEBlock();
1936 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1937 AddUInt(Block, 0, DW_FORM_udata, Offset >> 3);
1938 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1939
1940 // FIXME Handle DW_AT_accessibility.
1941
1942 Buffer.AddChild(MemberDie);
1943 }
1944
Devang Patel8526cc02009-01-05 22:35:52 +00001945 /// FindCompileUnit - Get the compile unit for the given descriptor.
1946 ///
1947 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
1948 CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
1949 assert(DW_Unit && "Missing compile unit.");
1950 return DW_Unit;
1951 }
1952
Devang Patelbbdc8202009-01-13 23:54:55 +00001953 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel7a6e5a32009-01-08 02:33:41 +00001954 ///
1955 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1956 // Get the descriptor.
1957 DIVariable *VD = DV->getVariable();
1958
1959 // Translate tag to proper Dwarf tag. The result variable is dropped for
1960 // now.
1961 unsigned Tag;
1962 switch (VD->getTag()) {
1963 case DW_TAG_return_variable: return NULL;
1964 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1965 case DW_TAG_auto_variable: // fall thru
1966 default: Tag = DW_TAG_variable; break;
1967 }
1968
1969 // Define variable debug information entry.
1970 DIE *VariableDie = new DIE(Tag);
1971 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1972
1973 // Add source line info if available.
1974 AddSourceLine(VariableDie, VD);
1975
1976 // Add variable type.
1977 AddType(Unit, VariableDie, VD->getType());
1978
1979 // Add variable address.
1980 MachineLocation Location;
1981 Location.set(RI->getFrameRegister(*MF),
1982 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1983 AddAddress(VariableDie, DW_AT_location, Location);
1984
1985 return VariableDie;
1986 }
1987
Devang Patel7a6e5a32009-01-08 02:33:41 +00001988 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1989 ///
1990 DbgScope *getOrCreateScope(GlobalVariable *V) {
1991 DbgScope *&Slot = DbgScopeMap[V];
1992 if (!Slot) {
1993 // FIXME - breaks down when the context is an inlined function.
1994 DIDescriptor ParentDesc;
Devang Patel0e5200f2009-01-15 18:25:17 +00001995 DIDescriptor Desc(V);
1996 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
1997 DIBlock Block(V);
1998 ParentDesc = Block.getContext();
Devang Patel7a6e5a32009-01-08 02:33:41 +00001999 }
2000 DbgScope *Parent = ParentDesc.isNull() ?
Devang Patel0dc969e2009-01-10 02:34:18 +00002001 NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel0e5200f2009-01-15 18:25:17 +00002002 Slot = new DbgScope(Parent, Desc);
Devang Patel7a6e5a32009-01-08 02:33:41 +00002003 if (Parent) {
2004 Parent->AddScope(Slot);
2005 } else if (RootDbgScope) {
2006 // FIXME - Add inlined function scopes to the root so we can delete
2007 // them later. Long term, handle inlined functions properly.
2008 RootDbgScope->AddScope(Slot);
2009 } else {
2010 // First function is top level function.
2011 RootDbgScope = Slot;
2012 }
2013 }
2014 return Slot;
2015 }
2016
2017 /// ConstructDbgScope - Construct the components of a scope.
2018 ///
2019 void ConstructDbgScope(DbgScope *ParentScope,
2020 unsigned ParentStartID, unsigned ParentEndID,
2021 DIE *ParentDie, CompileUnit *Unit) {
2022 // Add variables to scope.
Devang Patel9795da52009-01-10 02:42:49 +00002023 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002024 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2025 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2026 if (VariableDie) ParentDie->AddChild(VariableDie);
2027 }
2028
2029 // Add nested scopes.
Devang Patel9795da52009-01-10 02:42:49 +00002030 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel7a6e5a32009-01-08 02:33:41 +00002031 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2032 // Define the Scope debug information entry.
2033 DbgScope *Scope = Scopes[j];
2034 // FIXME - Ignore inlined functions for the time being.
2035 if (!Scope->getParent()) continue;
2036
Devang Patele9bfb0f2009-01-12 18:41:00 +00002037 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2038 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002039
2040 // Ignore empty scopes.
2041 if (StartID == EndID && StartID != 0) continue;
2042 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2043
2044 if (StartID == ParentStartID && EndID == ParentEndID) {
2045 // Just add stuff to the parent scope.
2046 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2047 } else {
2048 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2049
2050 // Add the scope bounds.
2051 if (StartID) {
2052 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2053 DWLabel("label", StartID));
2054 } else {
2055 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2056 DWLabel("func_begin", SubprogramCount));
2057 }
2058 if (EndID) {
2059 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2060 DWLabel("label", EndID));
2061 } else {
2062 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2063 DWLabel("func_end", SubprogramCount));
2064 }
2065
2066 // Add the scope contents.
2067 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2068 ParentDie->AddChild(ScopeDie);
2069 }
2070 }
2071 }
2072
2073 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2074 ///
2075 void ConstructRootDbgScope(DbgScope *RootScope) {
2076 // Exit if there is no root scope.
2077 if (!RootScope) return;
Devang Patel0e5200f2009-01-15 18:25:17 +00002078 DIDescriptor Desc = RootScope->getDesc();
2079 if (Desc.isNull())
2080 return;
Devang Patel7a6e5a32009-01-08 02:33:41 +00002081
2082 // Get the subprogram debug information entry.
Devang Patel0e5200f2009-01-15 18:25:17 +00002083 DISubprogram SPD(Desc.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002084
2085 // Get the compile unit context.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002086 CompileUnit *Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002087
2088 // Get the subprogram die.
Devang Patelf6bac3e2009-01-12 22:58:14 +00002089 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7a6e5a32009-01-08 02:33:41 +00002090 assert(SPDie && "Missing subprogram descriptor");
2091
2092 // Add the function bounds.
2093 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2094 DWLabel("func_begin", SubprogramCount));
2095 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2096 DWLabel("func_end", SubprogramCount));
2097 MachineLocation Location(RI->getFrameRegister(*MF));
2098 AddAddress(SPDie, DW_AT_frame_base, Location);
2099
2100 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2101 }
2102
2103 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2104 ///
2105 void ConstructDefaultDbgScope(MachineFunction *MF) {
2106 // Find the correct subprogram descriptor.
2107 std::string SPName = "llvm.dbg.subprograms";
2108 std::vector<GlobalVariable*> Result;
2109 getGlobalVariablesUsing(*M, SPName, Result);
2110 for (std::vector<GlobalVariable *>::iterator I = Result.begin(),
2111 E = Result.end(); I != E; ++I) {
2112
2113 DISubprogram *SPD = new DISubprogram(*I);
2114
2115 if (SPD->getName() == MF->getFunction()->getName()) {
2116 // Get the compile unit context.
2117 CompileUnit *Unit = FindCompileUnit(SPD->getCompileUnit());
2118
2119 // Get the subprogram die.
2120 DIE *SPDie = Unit->getDieMapSlotFor(SPD->getGV());
2121 assert(SPDie && "Missing subprogram descriptor");
2122
2123 // Add the function bounds.
2124 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2125 DWLabel("func_begin", SubprogramCount));
2126 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2127 DWLabel("func_end", SubprogramCount));
2128
2129 MachineLocation Location(RI->getFrameRegister(*MF));
2130 AddAddress(SPDie, DW_AT_frame_base, Location);
2131 return;
2132 }
2133 }
2134#if 0
2135 // FIXME: This is causing an abort because C++ mangled names are compared
2136 // with their unmangled counterparts. See PR2885. Don't do this assert.
2137 assert(0 && "Couldn't find DIE for machine function!");
2138#endif
2139 }
2140
Jim Laskeyef42a012006-11-02 20:12:39 +00002141 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2142 /// tools to recognize the object file contains Dwarf information.
2143 void EmitInitial() {
2144 // Check to see if we already emitted intial headers.
2145 if (didInitial) return;
2146 didInitial = true;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002147
Jim Laskeyef42a012006-11-02 20:12:39 +00002148 // Dwarf sections base addresses.
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002149 if (TAI->doesDwarfRequireFrameSection()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002150 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002151 EmitLabel("section_debug_frame", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002152 }
2153 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2154 EmitLabel("section_info", 0);
2155 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2156 EmitLabel("section_abbrev", 0);
2157 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2158 EmitLabel("section_aranges", 0);
2159 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2160 EmitLabel("section_macinfo", 0);
2161 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2162 EmitLabel("section_line", 0);
2163 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2164 EmitLabel("section_loc", 0);
2165 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2166 EmitLabel("section_pubnames", 0);
2167 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2168 EmitLabel("section_str", 0);
2169 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2170 EmitLabel("section_ranges", 0);
2171
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002172 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002173 EmitLabel("text_begin", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002174 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002175 EmitLabel("data_begin", 0);
Jim Laskeyef42a012006-11-02 20:12:39 +00002176 }
2177
Jim Laskey65195462006-10-30 13:35:07 +00002178 /// EmitDIE - Recusively Emits a debug information entry.
2179 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002180 void EmitDIE(DIE *Die) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002181 // Get the abbreviation for this DIE.
2182 unsigned AbbrevNumber = Die->getAbbrevNumber();
2183 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002184
Jim Laskeybacd3042007-02-21 22:48:45 +00002185 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002186
2187 // Emit the code (index) for the abbreviation.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002188 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng6547e402008-07-01 23:18:29 +00002189
2190 if (VerboseAsm)
2191 Asm->EOL(std::string("Abbrev [" +
2192 utostr(AbbrevNumber) +
2193 "] 0x" + utohexstr(Die->getOffset()) +
2194 ":0x" + utohexstr(Die->getSize()) + " " +
2195 TagString(Abbrev->getTag())));
2196 else
2197 Asm->EOL();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002198
Owen Anderson873e1b52008-06-24 21:44:59 +00002199 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2200 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002201
Jim Laskeyef42a012006-11-02 20:12:39 +00002202 // Emit the DIE attribute values.
2203 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2204 unsigned Attr = AbbrevData[i].getAttribute();
2205 unsigned Form = AbbrevData[i].getForm();
2206 assert(Form && "Too many attributes for DIE (check abbreviation)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002207
Jim Laskeyef42a012006-11-02 20:12:39 +00002208 switch (Attr) {
2209 case DW_AT_sibling: {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002210 Asm->EmitInt32(Die->SiblingOffset());
Jim Laskeyef42a012006-11-02 20:12:39 +00002211 break;
2212 }
2213 default: {
2214 // Emit an attribute using the defined form.
2215 Values[i]->EmitValue(*this, Form);
2216 break;
2217 }
2218 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002219
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002220 Asm->EOL(AttributeString(Attr));
Jim Laskeyef42a012006-11-02 20:12:39 +00002221 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002222
Jim Laskeyef42a012006-11-02 20:12:39 +00002223 // Emit the DIE children if any.
2224 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2225 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002226
Jim Laskeyef42a012006-11-02 20:12:39 +00002227 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2228 EmitDIE(Children[j]);
2229 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002230
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002231 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
Jim Laskeyef42a012006-11-02 20:12:39 +00002232 }
2233 }
2234
Jim Laskey65195462006-10-30 13:35:07 +00002235 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2236 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002237 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2238 // Get the children.
2239 const std::vector<DIE *> &Children = Die->getChildren();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002240
Jim Laskeyef42a012006-11-02 20:12:39 +00002241 // If not last sibling and has children then add sibling offset attribute.
2242 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2243
2244 // Record the abbreviation.
2245 AssignAbbrevNumber(Die->getAbbrev());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002246
Jim Laskeyef42a012006-11-02 20:12:39 +00002247 // Get the abbreviation for this DIE.
2248 unsigned AbbrevNumber = Die->getAbbrevNumber();
2249 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2250
2251 // Set DIE offset
2252 Die->setOffset(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002253
Jim Laskeyef42a012006-11-02 20:12:39 +00002254 // Start the size with the size of abbreviation code.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002255 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2256
Owen Anderson873e1b52008-06-24 21:44:59 +00002257 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2258 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00002259
2260 // Size the DIE attribute values.
2261 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2262 // Size attribute value.
2263 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2264 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002265
Jim Laskeyef42a012006-11-02 20:12:39 +00002266 // Size the DIE children if any.
2267 if (!Children.empty()) {
2268 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2269 "Children flag not set");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002270
Jim Laskeyef42a012006-11-02 20:12:39 +00002271 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2272 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2273 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002274
Jim Laskeyef42a012006-11-02 20:12:39 +00002275 // End of children marker.
2276 Offset += sizeof(int8_t);
2277 }
2278
2279 Die->setSize(Offset - Die->getOffset());
2280 return Offset;
2281 }
Jim Laskey65195462006-10-30 13:35:07 +00002282
2283 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2284 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002285 void SizeAndOffsets() {
Jim Laskey5496f012006-11-09 14:52:14 +00002286 // Process base compile unit.
Devang Patel72b66352009-01-12 23:05:55 +00002287 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2288 CE = DW_CUs.end(); CI != CE; ++CI) {
2289 CompileUnit *Unit = CI->second;
2290 // Compute size of compile unit header
2291 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2292 sizeof(int16_t) + // DWARF version number
2293 sizeof(int32_t) + // Offset Into Abbrev. Section
2294 sizeof(int8_t); // Pointer Size (in bytes)
2295 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2296 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002297 }
2298
Jim Laskey65195462006-10-30 13:35:07 +00002299 /// EmitDebugInfo - Emit the debug info section.
2300 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002301 void EmitDebugInfo() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002302 // Start debug info section.
2303 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002304
Devang Patel72b66352009-01-12 23:05:55 +00002305 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2306 CE = DW_CUs.end(); CI != CE; ++CI) {
2307 CompileUnit *Unit = CI->second;
2308 DIE *Die = Unit->getDie();
2309 // Emit the compile units header.
2310 EmitLabel("info_begin", Unit->getID());
2311 // Emit size of content not including length itself
2312 unsigned ContentSize = Die->getSize() +
2313 sizeof(int16_t) + // DWARF version number
2314 sizeof(int32_t) + // Offset Into Abbrev. Section
2315 sizeof(int8_t) + // Pointer Size (in bytes)
2316 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2317
2318 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2319 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2320 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2321 Asm->EOL("Offset Into Abbrev. Section");
2322 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2323
2324 EmitDIE(Die);
2325 // FIXME - extra padding for gdb bug.
2326 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2327 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2328 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2329 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2330 EmitLabel("info_end", Unit->getID());
2331
2332 Asm->EOL();
2333 }
Jim Laskeyef42a012006-11-02 20:12:39 +00002334 }
2335
Jim Laskey65195462006-10-30 13:35:07 +00002336 /// EmitAbbreviations - Emit the abbreviation section.
2337 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002338 void EmitAbbreviations() const {
2339 // Check to see if it is worth the effort.
2340 if (!Abbreviations.empty()) {
2341 // Start the debug abbrev section.
2342 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002343
Jim Laskeyef42a012006-11-02 20:12:39 +00002344 EmitLabel("abbrev_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002345
Jim Laskeyef42a012006-11-02 20:12:39 +00002346 // For each abbrevation.
2347 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2348 // Get abbreviation data
2349 const DIEAbbrev *Abbrev = Abbreviations[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002350
Jim Laskeyef42a012006-11-02 20:12:39 +00002351 // Emit the abbrevations code (base 1 index.)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002352 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2353 Asm->EOL("Abbreviation Code");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002354
Jim Laskeyef42a012006-11-02 20:12:39 +00002355 // Emit the abbreviations data.
2356 Abbrev->Emit(*this);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002357
Jim Laskeybacd3042007-02-21 22:48:45 +00002358 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002359 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002360
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002361 // Mark end of abbreviations.
Jim Laskey5df3ad82007-02-22 18:48:52 +00002362 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
Jim Laskey7b1b39d2007-02-22 18:22:42 +00002363
Jim Laskeyef42a012006-11-02 20:12:39 +00002364 EmitLabel("abbrev_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002365
Jim Laskeybacd3042007-02-21 22:48:45 +00002366 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002367 }
2368 }
2369
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002370 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2371 /// the line matrix.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002372 ///
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002373 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2374 // Define last address of section.
2375 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2376 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2377 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2378 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2379
2380 // Mark end of matrix.
2381 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2382 Asm->EmitULEB128Bytes(1); Asm->EOL();
2383 Asm->EmitInt8(1); Asm->EOL();
2384 }
2385
Jim Laskey65195462006-10-30 13:35:07 +00002386 /// EmitDebugLines - Emit source line information.
2387 ///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00002388 void EmitDebugLines() {
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002389 // If the target is using .loc/.file, the assembler will be emitting the
2390 // .debug_line table automatically.
2391 if (TAI->hasDotLocAndDotFile())
Dan Gohman81a148b2007-09-24 21:43:52 +00002392 return;
2393
Jim Laskeyef42a012006-11-02 20:12:39 +00002394 // Minimum line delta, thus ranging from -10..(255-10).
2395 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2396 // Maximum line delta, thus ranging from -10..(255-10).
2397 const int MaxLineDelta = 255 + MinLineDelta;
Jim Laskey65195462006-10-30 13:35:07 +00002398
Jim Laskeyef42a012006-11-02 20:12:39 +00002399 // Start the dwarf line section.
2400 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002401
Jim Laskeyef42a012006-11-02 20:12:39 +00002402 // Construct the section header.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002403
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002404 EmitDifference("line_end", 0, "line_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002405 Asm->EOL("Length of Source Line Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002406 EmitLabel("line_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002407
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002408 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002409
Jim Laskey2b4e98c2006-12-06 17:43:18 +00002410 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002411 Asm->EOL("Prolog Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002412 EmitLabel("line_prolog_begin", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002413
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002414 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002415
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002416 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
Jim Laskeyef42a012006-11-02 20:12:39 +00002417
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002418 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002419
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002420 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002421
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002422 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002423
Jim Laskeyef42a012006-11-02 20:12:39 +00002424 // Line number standard opcode encodings argument count
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002425 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2426 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2427 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2428 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2429 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2430 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2431 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2432 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2433 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskeyef42a012006-11-02 20:12:39 +00002434
Jim Laskeyef42a012006-11-02 20:12:39 +00002435 // Emit directories.
2436 for (unsigned DirectoryID = 1, NDID = Directories.size();
2437 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002438 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
Jim Laskeyef42a012006-11-02 20:12:39 +00002439 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002440 Asm->EmitInt8(0); Asm->EOL("End of directories");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002441
Jim Laskeyef42a012006-11-02 20:12:39 +00002442 // Emit files.
Devang Patel7bb89ed2009-01-13 00:20:51 +00002443 for (unsigned SourceID = 1, NSID = SrcFiles.size();
Jim Laskeyef42a012006-11-02 20:12:39 +00002444 SourceID <= NSID; ++SourceID) {
Devang Patel7bb89ed2009-01-13 00:20:51 +00002445 const SrcFileInfo &SourceFile = SrcFiles[SourceID];
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002446 Asm->EmitString(SourceFile.getName());
2447 Asm->EOL("Source");
2448 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2449 Asm->EOL("Directory #");
2450 Asm->EmitULEB128Bytes(0);
2451 Asm->EOL("Mod date");
2452 Asm->EmitULEB128Bytes(0);
2453 Asm->EOL("File size");
Jim Laskeyef42a012006-11-02 20:12:39 +00002454 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002455 Asm->EmitInt8(0); Asm->EOL("End of files");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002456
Jim Laskeyef42a012006-11-02 20:12:39 +00002457 EmitLabel("line_prolog_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002458
Jim Laskeyef42a012006-11-02 20:12:39 +00002459 // A sequence for each text section.
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002460 unsigned SecSrcLinesSize = SectionSourceLines.size();
2461
2462 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Jim Laskeyef42a012006-11-02 20:12:39 +00002463 // Isolate current sections line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00002464 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng6547e402008-07-01 23:18:29 +00002465
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002466 if (VerboseAsm) {
2467 const Section* S = SectionMap[j + 1];
2468 Asm->EOL(std::string("Section ") + S->getName());
2469 } else
Evan Cheng6547e402008-07-01 23:18:29 +00002470 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002471
2472 // Dwarf assumes we start with first line of first source file.
2473 unsigned Source = 1;
2474 unsigned Line = 1;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002475
Jim Laskeyef42a012006-11-02 20:12:39 +00002476 // Construct rows of the address, source, line, column matrix.
2477 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patelf3ee5142009-01-12 22:54:42 +00002478 const SrcLineInfo &LineInfo = LineInfos[i];
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002479 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
Jim Laskey9d4209f2006-11-07 19:33:46 +00002480 if (!LabelID) continue;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002481
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002482 unsigned SourceID = LineInfo.getSourceID();
Devang Patel7bb89ed2009-01-13 00:20:51 +00002483 const SrcFileInfo &SourceFile = SrcFiles[SourceID];
Jim Laskey1a4a83c2007-01-25 15:45:58 +00002484 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng6547e402008-07-01 23:18:29 +00002485 if (VerboseAsm)
2486 Asm->EOL(Directories[DirectoryID]
2487 + SourceFile.getName()
2488 + ":"
2489 + utostr_32(LineInfo.getLine()));
2490 else
2491 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002492
2493 // Define the line address.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002494 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohman82482942007-09-27 23:12:31 +00002495 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002496 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
Jim Laskeybacd3042007-02-21 22:48:45 +00002497 EmitReference("label", LabelID); Asm->EOL("Location label");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002498
Jim Laskeyef42a012006-11-02 20:12:39 +00002499 // If change of source, then switch to the new source.
2500 if (Source != LineInfo.getSourceID()) {
2501 Source = LineInfo.getSourceID();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002502 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2503 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
Jim Laskeyef42a012006-11-02 20:12:39 +00002504 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002505
Jim Laskeyef42a012006-11-02 20:12:39 +00002506 // If change of line.
2507 if (Line != LineInfo.getLine()) {
2508 // Determine offset.
2509 int Offset = LineInfo.getLine() - Line;
2510 int Delta = Offset - MinLineDelta;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002511
Jim Laskeyef42a012006-11-02 20:12:39 +00002512 // Update line.
2513 Line = LineInfo.getLine();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002514
Jim Laskeyef42a012006-11-02 20:12:39 +00002515 // If delta is small enough and in range...
2516 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2517 // ... then use fast opcode.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002518 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
Jim Laskeyef42a012006-11-02 20:12:39 +00002519 } else {
2520 // ... otherwise use long hand.
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002521 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2522 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2523 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002524 }
2525 } else {
2526 // Copy the previous row (different address or source)
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002527 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
Jim Laskeyef42a012006-11-02 20:12:39 +00002528 }
2529 }
2530
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002531 EmitEndOfLineMatrix(j + 1);
Jim Laskeyef42a012006-11-02 20:12:39 +00002532 }
Bill Wendlingfbbd7012008-07-20 00:11:19 +00002533
2534 if (SecSrcLinesSize == 0)
2535 // Because we're emitting a debug_line section, we still need a line
2536 // table. The linker and friends expect it to exist. If there's nothing to
2537 // put into it, emit an empty table.
2538 EmitEndOfLineMatrix(1);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002539
Jim Laskeyef42a012006-11-02 20:12:39 +00002540 EmitLabel("line_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002541
Jim Laskeybacd3042007-02-21 22:48:45 +00002542 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002543 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002544
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002545 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
Jim Laskey65195462006-10-30 13:35:07 +00002546 ///
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002547 void EmitCommonDebugFrame() {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002548 if (!TAI->doesDwarfRequireFrameSection())
Jim Laskeyef42a012006-11-02 20:12:39 +00002549 return;
2550
2551 int stackGrowth =
2552 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2553 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00002554 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyef42a012006-11-02 20:12:39 +00002555
2556 // Start the dwarf frame section.
2557 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2558
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002559 EmitLabel("debug_frame_common", 0);
2560 EmitDifference("debug_frame_common_end", 0,
2561 "debug_frame_common_begin", 0, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002562 Asm->EOL("Length of Common Information Entry");
Jim Laskeyef42a012006-11-02 20:12:39 +00002563
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002564 EmitLabel("debug_frame_common_begin", 0);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002565 Asm->EmitInt32((int)DW_CIE_ID);
2566 Asm->EOL("CIE Identifier Tag");
2567 Asm->EmitInt8(DW_CIE_VERSION);
2568 Asm->EOL("CIE Version");
2569 Asm->EmitString("");
2570 Asm->EOL("CIE Augmentation");
2571 Asm->EmitULEB128Bytes(1);
2572 Asm->EOL("CIE Code Alignment Factor");
2573 Asm->EmitSLEB128Bytes(stackGrowth);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002574 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00002575 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002576 Asm->EOL("CIE RA Column");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002577
Jim Laskey5e73d5b2007-01-24 18:45:13 +00002578 std::vector<MachineMove> Moves;
Jim Laskeyef42a012006-11-02 20:12:39 +00002579 RI->getInitialFrameState(Moves);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00002580
Dale Johannesenb97aec62007-11-13 19:13:01 +00002581 EmitFrameMoves(NULL, 0, Moves, false);
Jim Laskeyef42a012006-11-02 20:12:39 +00002582
Evan Cheng05548eb2008-02-29 19:36:59 +00002583 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002584 EmitLabel("debug_frame_common_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002585
Jim Laskeybacd3042007-02-21 22:48:45 +00002586 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002587 }
2588
Jim Laskey65195462006-10-30 13:35:07 +00002589 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2590 /// section.
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002591 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +00002592 if (!TAI->doesDwarfRequireFrameSection())
Reid Spencer5a4951e2006-11-07 06:36:36 +00002593 return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002594
Jim Laskeyef42a012006-11-02 20:12:39 +00002595 // Start the dwarf frame section.
2596 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002597
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002598 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2599 "debug_frame_begin", DebugFrameInfo.Number, true);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002600 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002601
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002602 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00002603
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002604 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2605 0, 0, true, false);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002606 Asm->EOL("FDE CIE offset");
Jim Laskey65195462006-10-30 13:35:07 +00002607
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002608 EmitReference("func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002609 Asm->EOL("FDE initial location");
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002610 EmitDifference("func_end", DebugFrameInfo.Number,
2611 "func_begin", DebugFrameInfo.Number);
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002612 Asm->EOL("FDE address range");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002613
Dale Johannesenb97aec62007-11-13 19:13:01 +00002614 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002615
Evan Cheng05548eb2008-02-29 19:36:59 +00002616 Asm->EmitAlignment(2, 0, 0, false);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002617 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Jim Laskeyef42a012006-11-02 20:12:39 +00002618
Jim Laskeybacd3042007-02-21 22:48:45 +00002619 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002620 }
2621
2622 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
Jim Laskey65195462006-10-30 13:35:07 +00002623 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002624 void EmitDebugPubNames() {
2625 // Start the dwarf pubnames section.
2626 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002627
Devang Patel72b66352009-01-12 23:05:55 +00002628 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2629 CE = DW_CUs.end(); CI != CE; ++CI) {
2630 CompileUnit *Unit = CI->second;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002631
Devang Patel72b66352009-01-12 23:05:55 +00002632 EmitDifference("pubnames_end", Unit->getID(),
2633 "pubnames_begin", Unit->getID(), true);
2634 Asm->EOL("Length of Public Names Info");
2635
2636 EmitLabel("pubnames_begin", Unit->getID());
2637
2638 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2639
2640 EmitSectionOffset("info_begin", "section_info",
2641 Unit->getID(), 0, true, false);
2642 Asm->EOL("Offset of Compilation Unit Info");
2643
2644 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
2645 Asm->EOL("Compilation Unit Length");
2646
2647 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2648
2649 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2650 GE = Globals.end();
2651 GI != GE; ++GI) {
2652 const std::string &Name = GI->first;
2653 DIE * Entity = GI->second;
2654
2655 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2656 Asm->EmitString(Name); Asm->EOL("External Name");
2657 }
2658
2659 Asm->EmitInt32(0); Asm->EOL("End Mark");
2660 EmitLabel("pubnames_end", Unit->getID());
2661
2662 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002663 }
2664 }
2665
2666 /// EmitDebugStr - Emit visible names into a debug str section.
Jim Laskey65195462006-10-30 13:35:07 +00002667 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002668 void EmitDebugStr() {
2669 // Check to see if it is worth the effort.
2670 if (!StringPool.empty()) {
2671 // Start the dwarf str section.
2672 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002673
Jim Laskeyef42a012006-11-02 20:12:39 +00002674 // For each of strings in the string pool.
2675 for (unsigned StringID = 1, N = StringPool.size();
2676 StringID <= N; ++StringID) {
2677 // Emit a label for reference from debug information entries.
2678 EmitLabel("string", StringID);
2679 // Emit the string itself.
2680 const std::string &String = StringPool[StringID];
Jim Laskeybacd3042007-02-21 22:48:45 +00002681 Asm->EmitString(String); Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002682 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002683
Jim Laskeybacd3042007-02-21 22:48:45 +00002684 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002685 }
2686 }
2687
2688 /// EmitDebugLoc - Emit visible names into a debug loc section.
Jim Laskey65195462006-10-30 13:35:07 +00002689 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002690 void EmitDebugLoc() {
2691 // Start the dwarf loc section.
2692 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002693
Jim Laskeybacd3042007-02-21 22:48:45 +00002694 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002695 }
2696
2697 /// EmitDebugARanges - Emit visible names into a debug aranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002698 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002699 void EmitDebugARanges() {
2700 // Start the dwarf aranges section.
2701 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002702
Jim Laskeyef42a012006-11-02 20:12:39 +00002703 // FIXME - Mock up
Bill Wendlingd751c642008-09-26 00:28:12 +00002704#if 0
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002705 CompileUnit *Unit = GetBaseCompileUnit();
2706
Jim Laskey5496f012006-11-09 14:52:14 +00002707 // Don't include size of length
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002708 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002709
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002710 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002711
Jim Laskey5496f012006-11-09 14:52:14 +00002712 EmitReference("info_begin", Unit->getID());
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002713 Asm->EOL("Offset of Compilation Unit Info");
Jim Laskeyef42a012006-11-02 20:12:39 +00002714
Dan Gohman82482942007-09-27 23:12:31 +00002715 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Jim Laskeyef42a012006-11-02 20:12:39 +00002716
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002717 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
Jim Laskeyef42a012006-11-02 20:12:39 +00002718
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002719 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2720 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
Jim Laskeyef42a012006-11-02 20:12:39 +00002721
Jim Laskey5496f012006-11-09 14:52:14 +00002722 // Range 1
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002723 EmitReference("text_begin", 0); Asm->EOL("Address");
2724 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
Jim Laskeyef42a012006-11-02 20:12:39 +00002725
Jim Laskeyf1cdea12007-01-25 15:12:02 +00002726 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2727 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingd751c642008-09-26 00:28:12 +00002728#endif
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002729
Jim Laskeybacd3042007-02-21 22:48:45 +00002730 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002731 }
2732
2733 /// EmitDebugRanges - Emit visible names into a debug ranges section.
Jim Laskey65195462006-10-30 13:35:07 +00002734 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002735 void EmitDebugRanges() {
2736 // Start the dwarf ranges section.
2737 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002738
Jim Laskeybacd3042007-02-21 22:48:45 +00002739 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002740 }
2741
2742 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
Jim Laskey65195462006-10-30 13:35:07 +00002743 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002744 void EmitDebugMacInfo() {
2745 // Start the dwarf macinfo section.
2746 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002747
Jim Laskeybacd3042007-02-21 22:48:45 +00002748 Asm->EOL();
Jim Laskeyef42a012006-11-02 20:12:39 +00002749 }
2750
Devang Patelc4523242009-01-05 23:11:11 +00002751 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Pateld1ca9252009-01-05 23:03:32 +00002752 void ConstructCompileUnits() {
2753 std::string CUName = "llvm.dbg.compile_units";
2754 std::vector<GlobalVariable*> Result;
2755 getGlobalVariablesUsing(*M, CUName, Result);
2756 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
2757 RE = Result.end(); RI != RE; ++RI) {
2758 DICompileUnit *DIUnit = new DICompileUnit(*RI);
Devang Patel9f8fcfc2009-01-08 17:19:22 +00002759 unsigned ID = RecordSource(DIUnit->getDirectory(),
2760 DIUnit->getFilename());
Devang Pateld1ca9252009-01-05 23:03:32 +00002761
2762 DIE *Die = new DIE(DW_TAG_compile_unit);
2763 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2764 DWLabel("section_line", 0), DWLabel("section_line", 0),
2765 false);
2766 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit->getProducer());
2767 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit->getLanguage());
2768 AddString(Die, DW_AT_name, DW_FORM_string, DIUnit->getFilename());
2769 if (!DIUnit->getDirectory().empty())
2770 AddString(Die, DW_AT_comp_dir, DW_FORM_string, DIUnit->getDirectory());
2771
2772 CompileUnit *Unit = new CompileUnit(ID, Die);
2773 DW_CUs[DIUnit->getGV()] = Unit;
2774 }
2775 }
2776
Devang Patelc4523242009-01-05 23:11:11 +00002777 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
2778 /// visible global variables.
2779 void ConstructGlobalVariableDIEs() {
2780 std::string GVName = "llvm.dbg.global_variables";
2781 std::vector<GlobalVariable*> Result;
2782 getGlobalVariablesUsing(*M, GVName, Result);
2783 for (std::vector<GlobalVariable *>::iterator GVI = Result.begin(),
2784 GVE = Result.end(); GVI != GVE; ++GVI) {
2785 DIGlobalVariable *DI_GV = new DIGlobalVariable(*GVI);
2786 CompileUnit *DW_Unit = FindCompileUnit(DI_GV->getCompileUnit());
2787
2788 // Check for pre-existence.
2789 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV->getGV());
2790 if (Slot) continue;
2791
2792 DIE *VariableDie = new DIE(DW_TAG_variable);
2793 AddString(VariableDie, DW_AT_name, DW_FORM_string, DI_GV->getName());
2794 const std::string &LinkageName = DI_GV->getLinkageName();
2795 if (!LinkageName.empty())
2796 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
2797 LinkageName);
2798 AddType(DW_Unit, VariableDie, DI_GV->getType());
2799
2800 if (!DI_GV->isLocalToUnit())
2801 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
2802
2803 // Add source line info, if available.
2804 AddSourceLine(VariableDie, DI_GV);
2805
2806 // Add address.
2807 DIEBlock *Block = new DIEBlock();
2808 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2809 AddObjectLabel(Block, 0, DW_FORM_udata,
2810 Asm->getGlobalLinkName(DI_GV->getGV()));
2811 AddBlock(VariableDie, DW_AT_location, 0, Block);
2812
2813 //Add to map.
2814 Slot = VariableDie;
2815
2816 //Add to context owner.
2817 DW_Unit->getDie()->AddChild(VariableDie);
2818
2819 //Expose as global. FIXME - need to check external flag.
2820 DW_Unit->AddGlobal(DI_GV->getName(), VariableDie);
2821 }
2822 }
2823
Devang Patel78eb6ad2009-01-05 23:21:35 +00002824 /// ConstructSubprograms - Create DIEs for each of the externally visible
2825 /// subprograms.
2826 void ConstructSubprograms() {
2827
2828 std::string SPName = "llvm.dbg.subprograms";
2829 std::vector<GlobalVariable*> Result;
2830 getGlobalVariablesUsing(*M, SPName, Result);
2831 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
2832 RE = Result.end(); RI != RE; ++RI) {
2833
2834 DISubprogram *SP = new DISubprogram(*RI);
2835 CompileUnit *Unit = FindCompileUnit(SP->getCompileUnit());
2836
2837 // Check for pre-existence.
2838 DIE *&Slot = Unit->getDieMapSlotFor(SP->getGV());
2839 if (Slot) continue;
2840
2841 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
2842 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, SP->getName());
2843 const std::string &LinkageName = SP->getLinkageName();
2844 if (!LinkageName.empty())
2845 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
2846 LinkageName);
2847 DIType SPTy = SP->getType();
2848 AddType(Unit, SubprogramDie, SPTy);
2849 if (!SP->isLocalToUnit())
2850 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
2851 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
2852
2853 AddSourceLine(SubprogramDie, SP);
2854 //Add to map.
2855 Slot = SubprogramDie;
2856 //Add to context owner.
2857 Unit->getDie()->AddChild(SubprogramDie);
2858 //Expose as global.
2859 Unit->AddGlobal(SP->getName(), SubprogramDie);
2860 }
2861 }
2862
Jim Laskey65195462006-10-30 13:35:07 +00002863public:
Jim Laskeyef42a012006-11-02 20:12:39 +00002864 //===--------------------------------------------------------------------===//
2865 // Main entry points.
2866 //
Owen Andersoncb371882008-08-21 00:14:44 +00002867 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00002868 : Dwarf(OS, A, T, "dbg")
Jim Laskeyef42a012006-11-02 20:12:39 +00002869 , CompileUnits()
2870 , AbbreviationsSet(InitAbbreviationsSetSize)
2871 , Abbreviations()
2872 , ValuesSet(InitValuesSetSize)
2873 , Values()
2874 , StringPool()
Jim Laskeyef42a012006-11-02 20:12:39 +00002875 , SectionMap()
2876 , SectionSourceLines()
Jim Laskeybacd3042007-02-21 22:48:45 +00002877 , didInitial(false)
2878 , shouldEmit(false)
Devang Patel7a6e5a32009-01-08 02:33:41 +00002879 , RootDbgScope(NULL)
Jim Laskeyef42a012006-11-02 20:12:39 +00002880 {
2881 }
Jim Laskey072200c2007-01-29 18:51:14 +00002882 virtual ~DwarfDebug() {
Jim Laskeyef42a012006-11-02 20:12:39 +00002883 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2884 delete CompileUnits[i];
2885 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2886 delete Values[j];
2887 }
2888
Devang Patel5d315982009-01-06 21:07:30 +00002889 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
2890 /// This is inovked by the target AsmPrinter.
Devang Patel3f7833a2009-01-12 23:09:42 +00002891 void SetDebugInfo(MachineModuleInfo *mmi) {
2892
Devang Patel5d315982009-01-06 21:07:30 +00002893 // Create all the compile unit DIEs.
2894 ConstructCompileUnits();
Devang Patel3f7833a2009-01-12 23:09:42 +00002895
2896 if (DW_CUs.empty())
2897 return;
2898
2899 MMI = mmi;
2900 shouldEmit = true;
Devang Patele2051622009-01-13 23:02:17 +00002901 MMI->setDebugInfoAvailability(true);
Devang Patel5d315982009-01-06 21:07:30 +00002902
2903 // Create DIEs for each of the externally visible global variables.
2904 ConstructGlobalVariableDIEs();
2905
2906 // Create DIEs for each of the externally visible subprograms.
2907 ConstructSubprograms();
2908
2909 // Prime section data.
2910 SectionMap.insert(TAI->getTextSection());
2911
2912 // Print out .file directives to specify files for .loc directives. These
2913 // are printed out early so that they precede any .loc directives.
2914 if (TAI->hasDotLocAndDotFile()) {
2915 for (unsigned i = 1, e = SrcFiles.size(); i <= e; ++i) {
2916 sys::Path FullPath(Directories[SrcFiles[i].getDirectoryID()]);
2917 bool AppendOk = FullPath.appendComponent(SrcFiles[i].getName());
2918 assert(AppendOk && "Could not append filename to directory!");
2919 AppendOk = false;
2920 Asm->EmitFile(i, FullPath.toString());
2921 Asm->EOL();
2922 }
2923 }
2924
2925 // Emit initial sections
2926 EmitInitial();
2927 }
2928
Jim Laskey65195462006-10-30 13:35:07 +00002929 /// BeginModule - Emit all Dwarf sections that should come prior to the
2930 /// content.
Jim Laskeyef42a012006-11-02 20:12:39 +00002931 void BeginModule(Module *M) {
2932 this->M = M;
Jim Laskeyef42a012006-11-02 20:12:39 +00002933 }
2934
Jim Laskey65195462006-10-30 13:35:07 +00002935 /// EndModule - Emit all Dwarf sections that should come after the content.
2936 ///
Jim Laskeyef42a012006-11-02 20:12:39 +00002937 void EndModule() {
2938 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002939
Jim Laskeyef42a012006-11-02 20:12:39 +00002940 // Standard sections final addresses.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002941 Asm->SwitchToSection(TAI->getTextSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002942 EmitLabel("text_end", 0);
Anton Korobeynikov315690e2008-09-24 22:16:16 +00002943 Asm->SwitchToSection(TAI->getDataSection());
Jim Laskeyef42a012006-11-02 20:12:39 +00002944 EmitLabel("data_end", 0);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002945
Jim Laskeyef42a012006-11-02 20:12:39 +00002946 // End text sections.
2947 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00002948 Asm->SwitchToSection(SectionMap[i]);
Jim Laskeyef42a012006-11-02 20:12:39 +00002949 EmitLabel("section_end", i);
2950 }
Anton Korobeynikov185bc892007-05-13 17:30:11 +00002951
2952 // Emit common frame information.
2953 EmitCommonDebugFrame();
2954
2955 // Emit function debug frame information
2956 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2957 E = DebugFrames.end(); I != E; ++I)
2958 EmitFunctionDebugFrame(*I);
2959
Jim Laskeyef42a012006-11-02 20:12:39 +00002960 // Compute DIE offsets and sizes.
2961 SizeAndOffsets();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002962
Jim Laskeyef42a012006-11-02 20:12:39 +00002963 // Emit all the DIEs into a debug info section
2964 EmitDebugInfo();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002965
Jim Laskeyef42a012006-11-02 20:12:39 +00002966 // Corresponding abbreviations into a abbrev section.
2967 EmitAbbreviations();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002968
Jim Laskeyef42a012006-11-02 20:12:39 +00002969 // Emit source line correspondence into a debug line section.
2970 EmitDebugLines();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002971
Jim Laskeyef42a012006-11-02 20:12:39 +00002972 // Emit info into a debug pubnames section.
2973 EmitDebugPubNames();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002974
Jim Laskeyef42a012006-11-02 20:12:39 +00002975 // Emit info into a debug str section.
2976 EmitDebugStr();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002977
Jim Laskeyef42a012006-11-02 20:12:39 +00002978 // Emit info into a debug loc section.
2979 EmitDebugLoc();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002980
Jim Laskeyef42a012006-11-02 20:12:39 +00002981 // Emit info into a debug aranges section.
2982 EmitDebugARanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002983
Jim Laskeyef42a012006-11-02 20:12:39 +00002984 // Emit info into a debug ranges section.
2985 EmitDebugRanges();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002986
Jim Laskeyef42a012006-11-02 20:12:39 +00002987 // Emit info into a debug macinfo section.
2988 EmitDebugMacInfo();
2989 }
2990
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002991 /// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00002992 /// emitted immediately after the function entry point.
Jim Laskeyef42a012006-11-02 20:12:39 +00002993 void BeginFunction(MachineFunction *MF) {
2994 this->MF = MF;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00002995
Jim Laskeyef42a012006-11-02 20:12:39 +00002996 if (!ShouldEmitDwarf()) return;
Jim Laskeyef42a012006-11-02 20:12:39 +00002997
2998 // Begin accumulating function debug information.
Jim Laskey44c3b9f2007-01-26 21:22:28 +00002999 MMI->BeginFunction(MF);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003000
Jim Laskeyef42a012006-11-02 20:12:39 +00003001 // Assumes in correct section after the entry point.
3002 EmitLabel("func_begin", ++SubprogramCount);
Evan Cheng1b08bbc2008-02-01 09:10:45 +00003003
3004 // Emit label for the implicitly defined dbg.stoppoint at the start of
3005 // the function.
Devang Patelf3ee5142009-01-12 22:54:42 +00003006 if (!Lines.empty()) {
3007 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth51dd8c92008-04-03 17:37:43 +00003008 Asm->printLabel(LineInfo.getLabelID());
3009 }
Jim Laskeyef42a012006-11-02 20:12:39 +00003010 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003011
Jim Laskey65195462006-10-30 13:35:07 +00003012 /// EndFunction - Gather and emit post-function debug information.
3013 ///
Bill Wendlingd751c642008-09-26 00:28:12 +00003014 void EndFunction(MachineFunction *MF) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003015 if (!ShouldEmitDwarf()) return;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003016
Jim Laskeyb82313f2007-02-01 16:31:34 +00003017 // Define end label for subprogram.
3018 EmitLabel("func_end", SubprogramCount);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003019
Jim Laskeyef42a012006-11-02 20:12:39 +00003020 // Get function line info.
Devang Patelf3ee5142009-01-12 22:54:42 +00003021 if (!Lines.empty()) {
Jim Laskeyef42a012006-11-02 20:12:39 +00003022 // Get section line info.
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +00003023 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Jim Laskeyef42a012006-11-02 20:12:39 +00003024 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patelf3ee5142009-01-12 22:54:42 +00003025 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Jim Laskeyef42a012006-11-02 20:12:39 +00003026 // Append the function info to section info.
3027 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patelf3ee5142009-01-12 22:54:42 +00003028 Lines.begin(), Lines.end());
Jim Laskeyef42a012006-11-02 20:12:39 +00003029 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003030
Jim Laskeyef42a012006-11-02 20:12:39 +00003031 // Construct scopes for subprogram.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003032 if (RootDbgScope)
3033 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingd751c642008-09-26 00:28:12 +00003034 else
3035 // FIXME: This is wrong. We are essentially getting past a problem with
3036 // debug information not being able to handle unreachable blocks that have
3037 // debug information in them. In particular, those unreachable blocks that
3038 // have "region end" info in them. That situation results in the "root
3039 // scope" not being created. If that's the case, then emit a "default"
3040 // scope, i.e., one that encompasses the whole function. This isn't
3041 // desirable. And a better way of handling this (and all of the debugging
3042 // information) needs to be explored.
Devang Patel7bb89ed2009-01-13 00:20:51 +00003043 ConstructDefaultDbgScope(MF);
Anton Korobeynikov185bc892007-05-13 17:30:11 +00003044
3045 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3046 MMI->getFrameMoves()));
Devang Patel481ff5b2009-01-12 18:48:36 +00003047
3048 // Clear debug info
3049 if (RootDbgScope) {
3050 delete RootDbgScope;
3051 DbgScopeMap.clear();
3052 RootDbgScope = NULL;
3053 }
3054 Lines.clear();
Jim Laskeyef42a012006-11-02 20:12:39 +00003055 }
Devang Patelccca7fe2009-01-12 19:17:34 +00003056
3057public:
3058
3059 /// RecordSourceLine - Records location information and associates it with a
3060 /// label. Returns a unique label ID used to generate a label and provide
3061 /// correspondence to the source line list.
3062 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
3063 CompileUnit *Unit = DW_CUs[V];
3064 assert (Unit && "Unable to find CompileUnit");
3065 unsigned ID = MMI->NextLabelID();
3066 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
3067 return ID;
3068 }
3069
3070 /// RecordSourceLine - Records location information and associates it with a
3071 /// label. Returns a unique label ID used to generate a label and provide
3072 /// correspondence to the source line list.
3073 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
3074 unsigned ID = MMI->NextLabelID();
3075 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
3076 return ID;
3077 }
3078
3079 unsigned getRecordSourceLineCount() {
3080 return Lines.size();
3081 }
3082
3083 /// RecordSource - Register a source file with debug info. Returns an source
3084 /// ID.
3085 unsigned RecordSource(const std::string &Directory,
3086 const std::string &File) {
3087 unsigned DID = Directories.insert(Directory);
3088 return SrcFiles.insert(SrcFileInfo(DID,File));
3089 }
3090
3091 /// RecordRegionStart - Indicate the start of a region.
3092 ///
3093 unsigned RecordRegionStart(GlobalVariable *V) {
3094 DbgScope *Scope = getOrCreateScope(V);
3095 unsigned ID = MMI->NextLabelID();
3096 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
3097 return ID;
3098 }
3099
3100 /// RecordRegionEnd - Indicate the end of a region.
3101 ///
3102 unsigned RecordRegionEnd(GlobalVariable *V) {
3103 DbgScope *Scope = getOrCreateScope(V);
3104 unsigned ID = MMI->NextLabelID();
3105 Scope->setEndLabelID(ID);
3106 return ID;
3107 }
3108
3109 /// RecordVariable - Indicate the declaration of a local variable.
3110 ///
3111 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Devang Patel0e5200f2009-01-15 18:25:17 +00003112 DIDescriptor Desc(GV);
3113 DbgScope *Scope = NULL;
3114 if (Desc.getTag() == DW_TAG_variable) {
3115 // GV is a global variable.
3116 DIGlobalVariable DG(GV);
3117 Scope = getOrCreateScope(DG.getContext().getGV());
3118 } else {
3119 // or GV is a local variable.
3120 DIVariable DV(GV);
3121 Scope = getOrCreateScope(DV.getContext().getGV());
3122 }
3123 assert (Scope && "Unable to find variable' scope");
Devang Patelccca7fe2009-01-12 19:17:34 +00003124 DIVariable *VD = new DIVariable(GV);
3125 DbgVariable *DV = new DbgVariable(VD, FrameIndex);
3126 Scope->AddVariable(DV);
3127 }
Jim Laskey65195462006-10-30 13:35:07 +00003128};
3129
Jim Laskey072200c2007-01-29 18:51:14 +00003130//===----------------------------------------------------------------------===//
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003131/// DwarfException - Emits Dwarf exception handling directives.
Jim Laskey072200c2007-01-29 18:51:14 +00003132///
3133class DwarfException : public Dwarf {
3134
Jim Laskeyb82313f2007-02-01 16:31:34 +00003135private:
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003136 struct FunctionEHFrameInfo {
3137 std::string FnName;
3138 unsigned Number;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003139 unsigned PersonalityIndex;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003140 bool hasCalls;
3141 bool hasLandingPads;
3142 std::vector<MachineMove> Moves;
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003143 const Function * function;
Jim Laskeyb82313f2007-02-01 16:31:34 +00003144
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003145 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3146 bool hC, bool hL,
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003147 const std::vector<MachineMove> &M,
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003148 const Function *f):
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003149 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003150 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003151 };
3152
3153 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003154
3155 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3156 /// be emitted.
3157 bool shouldEmitTable;
3158
3159 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3160 /// should be emitted.
3161 bool shouldEmitMoves;
3162
3163 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3164 /// should be emitted.
3165 bool shouldEmitTableModule;
3166
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003167 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003168 /// should be emitted.
3169 bool shouldEmitMovesModule;
Duncan Sands671fa972008-05-07 19:11:09 +00003170
Jim Laskey3f09fc22007-02-28 18:38:31 +00003171 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3172 ///
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003173 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
Jim Laskeybacd3042007-02-21 22:48:45 +00003174 // Size and sign of stack growth.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003175 int stackGrowth =
3176 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3177 TargetFrameInfo::StackGrowsUp ?
Dan Gohman82482942007-09-27 23:12:31 +00003178 TD->getPointerSize() : -TD->getPointerSize();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003179
Jim Laskeybacd3042007-02-21 22:48:45 +00003180 // Begin eh frame section.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003181 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling722f5f12008-12-24 08:05:17 +00003182
3183 if (!TAI->doesRequireNonLocalEHFrameLabel())
3184 O << TAI->getEHGlobalPrefix();
3185 O << "EH_frame" << Index << ":\n";
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003186 EmitLabel("section_eh_frame", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003187
Jim Laskeybacd3042007-02-21 22:48:45 +00003188 // Define base labels.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003189 EmitLabel("eh_frame_common", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003190
Jim Laskeybacd3042007-02-21 22:48:45 +00003191 // Define the eh frame length.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003192 EmitDifference("eh_frame_common_end", Index,
3193 "eh_frame_common_begin", Index, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003194 Asm->EOL("Length of Common Information Entry");
3195
Jim Laskeybacd3042007-02-21 22:48:45 +00003196 // EH frame header.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003197 EmitLabel("eh_frame_common_begin", Index);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003198 Asm->EmitInt32((int)0);
3199 Asm->EOL("CIE Identifier Tag");
3200 Asm->EmitInt8(DW_CIE_VERSION);
3201 Asm->EOL("CIE Version");
Duncan Sands671fa972008-05-07 19:11:09 +00003202
Jim Laskeybacd3042007-02-21 22:48:45 +00003203 // The personality presence indicates that language specific information
3204 // will show up in the eh frame.
3205 Asm->EmitString(Personality ? "zPLR" : "zR");
Jim Laskeyb82313f2007-02-01 16:31:34 +00003206 Asm->EOL("CIE Augmentation");
Duncan Sands671fa972008-05-07 19:11:09 +00003207
Jim Laskeybacd3042007-02-21 22:48:45 +00003208 // Round out reader.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003209 Asm->EmitULEB128Bytes(1);
3210 Asm->EOL("CIE Code Alignment Factor");
3211 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands671fa972008-05-07 19:11:09 +00003212 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenb97aec62007-11-13 19:13:01 +00003213 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands671fa972008-05-07 19:11:09 +00003214 Asm->EOL("CIE Return Address Column");
3215
Jim Laskeybacd3042007-02-21 22:48:45 +00003216 // If there is a personality, we need to indicate the functions location.
3217 if (Personality) {
3218 Asm->EmitULEB128Bytes(7);
3219 Asm->EOL("Augmentation Size");
Bill Wendlingef4a6612007-09-11 17:20:55 +00003220
Duncan Sands671fa972008-05-07 19:11:09 +00003221 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003222 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands671fa972008-05-07 19:11:09 +00003223 Asm->EOL("Personality (pcrel sdata4 indirect)");
3224 } else {
Bill Wendlingef4a6612007-09-11 17:20:55 +00003225 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands671fa972008-05-07 19:11:09 +00003226 Asm->EOL("Personality (pcrel sdata4)");
3227 }
Bill Wendlingef4a6612007-09-11 17:20:55 +00003228
Duncan Sands671fa972008-05-07 19:11:09 +00003229 PrintRelDirective(true);
Bill Wendlingd60da492007-09-11 08:27:17 +00003230 O << TAI->getPersonalityPrefix();
3231 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3232 O << TAI->getPersonalitySuffix();
Duncan Sands43b30a82008-05-08 12:33:11 +00003233 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3234 O << "-" << TAI->getPCSymbol();
Bill Wendlingd60da492007-09-11 08:27:17 +00003235 Asm->EOL("Personality");
Bill Wendlingcf4bb312007-08-25 00:51:55 +00003236
Duncan Sands671fa972008-05-07 19:11:09 +00003237 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3238 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003239
Bill Wendlingd60de512009-01-05 22:53:45 +00003240 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3241 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003242 } else {
3243 Asm->EmitULEB128Bytes(1);
3244 Asm->EOL("Augmentation Size");
Bill Wendlingd4121be2008-12-24 05:25:49 +00003245
Bill Wendlingd60de512009-01-05 22:53:45 +00003246 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3247 Asm->EOL("FDE Encoding (pcrel sdata4)");
Jim Laskeybacd3042007-02-21 22:48:45 +00003248 }
3249
3250 // Indicate locations of general callee saved registers in frame.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003251 std::vector<MachineMove> Moves;
3252 RI->getInitialFrameState(Moves);
Dale Johannesenb97aec62007-11-13 19:13:01 +00003253 EmitFrameMoves(NULL, 0, Moves, true);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003254
Dale Johannesen21d972a2008-04-30 00:43:29 +00003255 // On Darwin the linker honors the alignment of eh_frame, which means it
3256 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3257 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003258 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003259 0, 0, false);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003260 EmitLabel("eh_frame_common_end", Index);
Duncan Sands671fa972008-05-07 19:11:09 +00003261
Jim Laskeybacd3042007-02-21 22:48:45 +00003262 Asm->EOL();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003263 }
Duncan Sands671fa972008-05-07 19:11:09 +00003264
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003265 /// EmitEHFrame - Emit function exception frame information.
Jim Laskeyb82313f2007-02-01 16:31:34 +00003266 ///
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003267 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003268 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3269
Jim Laskeybacd3042007-02-21 22:48:45 +00003270 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3271
3272 // Externally visible entry into the functions eh frame info.
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003273 // If the corresponding function is static, this should not be
3274 // externally visible.
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003275 if (linkage != Function::InternalLinkage) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003276 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3277 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3278 }
3279
Dale Johannesen038129d2008-01-10 02:03:30 +00003280 // If corresponding function is weak definition, this should be too.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003281 if ((linkage == Function::WeakLinkage ||
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003282 linkage == Function::LinkOnceLinkage) &&
Dale Johannesen038129d2008-01-10 02:03:30 +00003283 TAI->getWeakDefDirective())
3284 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3285
3286 // If there are no calls then you can't unwind. This may mean we can
3287 // omit the EH Frame, but some environments do not handle weak absolute
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003288 // symbols.
Dale Johannesen3541af72008-04-14 17:54:17 +00003289 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesen4e1b7942008-04-08 00:10:24 +00003290 // unwind info is to be available for non-EH uses.
Dale Johannesen038129d2008-01-10 02:03:30 +00003291 if (!EHFrameInfo.hasCalls &&
Dale Johannesen3541af72008-04-14 17:54:17 +00003292 !UnwindTablesMandatory &&
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003293 ((linkage != Function::WeakLinkage &&
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003294 linkage != Function::LinkOnceLinkage) ||
Dale Johannesen038129d2008-01-10 02:03:30 +00003295 !TAI->getWeakDefDirective() ||
3296 TAI->getSupportsWeakOmittedEHFrame()))
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003297 {
Bill Wendling6e198962007-09-18 01:47:22 +00003298 O << EHFrameInfo.FnName << " = 0\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003299 // This name has no connection to the function, so it might get
3300 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003301 // dead-stripping unconditionally.
3302 if (const char *UsedDirective = TAI->getUsedDirective())
3303 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Jim Laskeybacd3042007-02-21 22:48:45 +00003304 } else {
Bill Wendling6e198962007-09-18 01:47:22 +00003305 O << EHFrameInfo.FnName << ":\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003306
Jim Laskeybacd3042007-02-21 22:48:45 +00003307 // EH frame header.
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003308 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3309 "eh_frame_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003310 Asm->EOL("Length of Frame Information Entry");
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003311
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003312 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
Anton Korobeynikova6199c82007-03-07 02:47:57 +00003313
Bill Wendling722f5f12008-12-24 08:05:17 +00003314 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3315 PrintRelDirective(true, true);
3316 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3317
3318 if (!TAI->isAbsoluteEHSectionOffsets())
3319 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3320 } else {
3321 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3322 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3323 true, true, false);
3324 }
3325
Jim Laskeybacd3042007-02-21 22:48:45 +00003326 Asm->EOL("FDE CIE offset");
3327
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003328 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003329 Asm->EOL("FDE initial location");
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003330 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendling5fe1fac2009-01-06 19:13:55 +00003331 "eh_func_begin", EHFrameInfo.Number, true);
Jim Laskeybacd3042007-02-21 22:48:45 +00003332 Asm->EOL("FDE address range");
Duncan Sands671fa972008-05-07 19:11:09 +00003333
Jim Laskey3f09fc22007-02-28 18:38:31 +00003334 // If there is a personality and landing pads then point to the language
3335 // specific data area in the exception table.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003336 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands671fa972008-05-07 19:11:09 +00003337 Asm->EmitULEB128Bytes(4);
Jim Laskeybacd3042007-02-21 22:48:45 +00003338 Asm->EOL("Augmentation size");
Duncan Sands671fa972008-05-07 19:11:09 +00003339
3340 if (EHFrameInfo.hasLandingPads)
3341 EmitReference("exception", EHFrameInfo.Number, true, true);
3342 else
Jim Laskey3f09fc22007-02-28 18:38:31 +00003343 Asm->EmitInt32((int)0);
Jim Laskeybacd3042007-02-21 22:48:45 +00003344 Asm->EOL("Language Specific Data Area");
3345 } else {
3346 Asm->EmitULEB128Bytes(0);
3347 Asm->EOL("Augmentation size");
3348 }
Duncan Sands671fa972008-05-07 19:11:09 +00003349
Jim Laskeybacd3042007-02-21 22:48:45 +00003350 // Indicate locations of function specific callee saved registers in
3351 // frame.
Dale Johannesenb97aec62007-11-13 19:13:01 +00003352 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003353
Dale Johannesen21d972a2008-04-30 00:43:29 +00003354 // On Darwin the linker honors the alignment of eh_frame, which means it
3355 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3356 // you get holes which confuse readers of eh_frame.
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003357 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen7b251e02008-04-29 22:58:20 +00003358 0, 0, false);
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003359 EmitLabel("eh_frame_end", EHFrameInfo.Number);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003360
3361 // If the function is marked used, this table should be also. We cannot
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003362 // make the mark unconditional in this case, since retaining the table
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003363 // also retains the function in this case, and there is code around
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003364 // that depends on unused functions (calling undefined externals) being
3365 // dead-stripped to link correctly. Yes, there really is.
3366 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3367 if (const char *UsedDirective = TAI->getUsedDirective())
3368 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3369 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003370 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003371
Duncan Sands57810cd2007-09-05 11:27:52 +00003372 /// EmitExceptionTable - Emit landing pads and actions.
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003373 ///
3374 /// The general organization of the table is complex, but the basic concepts
3375 /// are easy. First there is a header which describes the location and
3376 /// organization of the three components that follow.
3377 /// 1. The landing pad site information describes the range of code covered
3378 /// by the try. In our case it's an accumulation of the ranges covered
3379 /// by the invokes in the try. There is also a reference to the landing
3380 /// pad that handles the exception once processed. Finally an index into
3381 /// the actions table.
3382 /// 2. The action table, in our case, is composed of pairs of type ids
3383 /// and next action offset. Starting with the action index from the
3384 /// landing pad site, each type Id is checked for a match to the current
3385 /// exception. If it matches then the exception and type id are passed
3386 /// on to the landing pad. Otherwise the next action is looked up. This
3387 /// chain is terminated with a next action of zero. If no type id is
3388 /// found the the frame is unwound and handling continues.
3389 /// 3. Type id table contains references to all the C++ typeinfo for all
3390 /// catches in the function. This tables is reversed indexed base 1.
3391
Duncan Sandsb32edb42007-06-06 15:37:31 +00003392 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3393 static unsigned SharedTypeIds(const LandingPadInfo *L,
3394 const LandingPadInfo *R) {
3395 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003396 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003397 unsigned MinSize = LSize < RSize ? LSize : RSize;
3398 unsigned Count = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003399
Duncan Sandsb32edb42007-06-06 15:37:31 +00003400 for (; Count != MinSize; ++Count)
3401 if (LIds[Count] != RIds[Count])
3402 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003403
Duncan Sandsb32edb42007-06-06 15:37:31 +00003404 return Count;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003405 }
3406
Duncan Sandsb32edb42007-06-06 15:37:31 +00003407 /// PadLT - Order landing pads lexicographically by type id.
Duncan Sands7bf7a442007-05-21 18:50:28 +00003408 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003409 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003410 unsigned LSize = LIds.size(), RSize = RIds.size();
Duncan Sandsb32edb42007-06-06 15:37:31 +00003411 unsigned MinSize = LSize < RSize ? LSize : RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003412
Duncan Sandsb32edb42007-06-06 15:37:31 +00003413 for (unsigned i = 0; i != MinSize; ++i)
Duncan Sands7bf7a442007-05-21 18:50:28 +00003414 if (LIds[i] != RIds[i])
3415 return LIds[i] < RIds[i];
3416
Duncan Sandsb32edb42007-06-06 15:37:31 +00003417 return LSize < RSize;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003418 }
3419
Duncan Sands53c3a332007-05-16 12:12:23 +00003420 struct KeyInfo {
3421 static inline unsigned getEmptyKey() { return -1U; }
3422 static inline unsigned getTombstoneKey() { return -2U; }
3423 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner76c1b972007-09-17 18:34:04 +00003424 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Duncan Sands53c3a332007-05-16 12:12:23 +00003425 static bool isPod() { return true; }
3426 };
3427
Duncan Sands57810cd2007-09-05 11:27:52 +00003428 /// ActionEntry - Structure describing an entry in the actions table.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003429 struct ActionEntry {
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003430 int ValueForTypeID; // The value to write - may not be equal to the type id.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003431 int NextAction;
3432 struct ActionEntry *Previous;
3433 };
3434
Duncan Sands57810cd2007-09-05 11:27:52 +00003435 /// PadRange - Structure holding a try-range and the associated landing pad.
3436 struct PadRange {
3437 // The index of the landing pad.
3438 unsigned PadIndex;
3439 // The index of the begin and end labels in the landing pad's label lists.
3440 unsigned RangeIndex;
3441 };
3442
3443 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3444
3445 /// CallSiteEntry - Structure describing an entry in the call-site table.
3446 struct CallSiteEntry {
Duncan Sands481dc722007-12-19 07:36:31 +00003447 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003448 unsigned BeginLabel; // zero indicates the start of the function.
3449 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands481dc722007-12-19 07:36:31 +00003450 // The landing pad starts at PadLabel.
Duncan Sands57810cd2007-09-05 11:27:52 +00003451 unsigned PadLabel; // zero indicates that there is no landing pad.
3452 unsigned Action;
3453 };
3454
Jim Laskeybacd3042007-02-21 22:48:45 +00003455 void EmitExceptionTable() {
Jim Laskeybacd3042007-02-21 22:48:45 +00003456 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Duncan Sands73ef58a2007-06-02 16:53:42 +00003457 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Duncan Sands7bf7a442007-05-21 18:50:28 +00003458 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3459 if (PadInfos.empty()) return;
3460
3461 // Sort the landing pads in order of their type ids. This is used to fold
3462 // duplicate actions.
Duncan Sands6cc76082007-06-08 08:59:11 +00003463 SmallVector<const LandingPadInfo *, 64> LandingPads;
Duncan Sands6cc76082007-06-08 08:59:11 +00003464 LandingPads.reserve(PadInfos.size());
Duncan Sands7bf7a442007-05-21 18:50:28 +00003465 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
Duncan Sands6cc76082007-06-08 08:59:11 +00003466 LandingPads.push_back(&PadInfos[i]);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003467 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3468
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003469 // Negative type ids index into FilterIds, positive type ids index into
3470 // TypeInfos. The value written for a positive type id is just the type
3471 // id itself. For a negative type id, however, the value written is the
3472 // (negative) byte offset of the corresponding FilterIds entry. The byte
3473 // offset is usually equal to the type id, because the FilterIds entries
3474 // are written using a variable width encoding which outputs one byte per
3475 // entry as long as the value written is not too large, but can differ.
3476 // This kind of complication does not occur for positive type ids because
3477 // type infos are output using a fixed width encoding.
3478 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3479 SmallVector<int, 16> FilterOffsets;
3480 FilterOffsets.reserve(FilterIds.size());
3481 int Offset = -1;
3482 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3483 E = FilterIds.end(); I != E; ++I) {
3484 FilterOffsets.push_back(Offset);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003485 Offset -= TargetAsmInfo::getULEB128Size(*I);
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003486 }
3487
Duncan Sands57810cd2007-09-05 11:27:52 +00003488 // Compute the actions table and gather the first action index for each
3489 // landing pad site.
3490 SmallVector<ActionEntry, 32> Actions;
3491 SmallVector<unsigned, 64> FirstActions;
3492 FirstActions.reserve(LandingPads.size());
Jim Laskeybacd3042007-02-21 22:48:45 +00003493
Duncan Sandsb32edb42007-06-06 15:37:31 +00003494 int FirstAction = 0;
Duncan Sands57810cd2007-09-05 11:27:52 +00003495 unsigned SizeActions = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003496 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
Duncan Sandsb32edb42007-06-06 15:37:31 +00003497 const LandingPadInfo *LP = LandingPads[i];
3498 const std::vector<int> &TypeIds = LP->TypeIds;
3499 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003500 unsigned SizeSiteActions = 0;
3501
Duncan Sandsb32edb42007-06-06 15:37:31 +00003502 if (NumShared < TypeIds.size()) {
Duncan Sands7bf7a442007-05-21 18:50:28 +00003503 unsigned SizeAction = 0;
Duncan Sandsb32edb42007-06-06 15:37:31 +00003504 ActionEntry *PrevAction = 0;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003505
Duncan Sandsb32edb42007-06-06 15:37:31 +00003506 if (NumShared) {
3507 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3508 assert(Actions.size());
3509 PrevAction = &Actions.back();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003510 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3511 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003512 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003513 SizeAction -=
3514 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003515 SizeAction += -PrevAction->NextAction;
3516 PrevAction = PrevAction->Previous;
Duncan Sands7bf7a442007-05-21 18:50:28 +00003517 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00003518 }
Duncan Sands7bf7a442007-05-21 18:50:28 +00003519
Duncan Sandsb32edb42007-06-06 15:37:31 +00003520 // Compute the actions.
3521 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3522 int TypeID = TypeIds[I];
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003523 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3524 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003525 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003526
3527 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003528 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003529 SizeSiteActions += SizeAction;
3530
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003531 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Duncan Sandsb32edb42007-06-06 15:37:31 +00003532 Actions.push_back(Action);
3533
3534 PrevAction = &Actions.back();
3535 }
3536
3537 // Record the first action of the landing pad site.
3538 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3539 } // else identical - re-use previous FirstAction
3540
3541 FirstActions.push_back(FirstAction);
Duncan Sands7bf7a442007-05-21 18:50:28 +00003542
Anton Korobeynikov29c9caf2007-05-11 08:23:57 +00003543 // Compute this sites contribution to size.
Anton Korobeynikov22d5c372007-05-11 08:47:35 +00003544 SizeActions += SizeSiteActions;
Jim Laskeybacd3042007-02-21 22:48:45 +00003545 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003546
Duncan Sands481dc722007-12-19 07:36:31 +00003547 // Compute the call-site table. The entry for an invoke has a try-range
3548 // containing the call, a non-zero landing pad and an appropriate action.
3549 // The entry for an ordinary call has a try-range containing the call and
3550 // zero for the landing pad and the action. Calls marked 'nounwind' have
3551 // no entry and must not be contained in the try-range of any entry - they
3552 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003553 SmallVector<CallSiteEntry, 64> CallSites;
3554
3555 RangeMapType PadMap;
Duncan Sands481dc722007-12-19 07:36:31 +00003556 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3557 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3558 // try-ranges for them need be deduced.
Duncan Sands57810cd2007-09-05 11:27:52 +00003559 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3560 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands481dc722007-12-19 07:36:31 +00003561 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003562 unsigned BeginLabel = LandingPad->BeginLabels[j];
3563 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3564 PadRange P = { i, j };
3565 PadMap[BeginLabel] = P;
3566 }
3567 }
3568
Duncan Sands481dc722007-12-19 07:36:31 +00003569 // The end label of the previous invoke or nounwind try-range.
Duncan Sands57810cd2007-09-05 11:27:52 +00003570 unsigned LastLabel = 0;
Duncan Sands481dc722007-12-19 07:36:31 +00003571
3572 // Whether there is a potentially throwing instruction (currently this means
3573 // an ordinary call) between the end of the previous try-range and now.
3574 bool SawPotentiallyThrowing = false;
3575
3576 // Whether the last callsite entry was for an invoke.
3577 bool PreviousIsInvoke = false;
3578
Duncan Sands481dc722007-12-19 07:36:31 +00003579 // Visit all instructions in order of address.
Duncan Sands57810cd2007-09-05 11:27:52 +00003580 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3581 I != E; ++I) {
3582 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3583 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +00003584 if (!MI->isLabel()) {
Chris Lattner749c6f62008-01-07 07:27:27 +00003585 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands57810cd2007-09-05 11:27:52 +00003586 continue;
3587 }
3588
Chris Lattner9e330492007-12-30 20:50:28 +00003589 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands57810cd2007-09-05 11:27:52 +00003590 assert(BeginLabel && "Invalid label!");
Duncan Sands98865042007-09-05 14:12:46 +00003591
Duncan Sands481dc722007-12-19 07:36:31 +00003592 // End of the previous try-range?
Duncan Sands98865042007-09-05 14:12:46 +00003593 if (BeginLabel == LastLabel)
Duncan Sands481dc722007-12-19 07:36:31 +00003594 SawPotentiallyThrowing = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003595
Duncan Sands481dc722007-12-19 07:36:31 +00003596 // Beginning of a new try-range?
Duncan Sands57810cd2007-09-05 11:27:52 +00003597 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands57810cd2007-09-05 11:27:52 +00003598 if (L == PadMap.end())
Duncan Sands481dc722007-12-19 07:36:31 +00003599 // Nope, it was just some random label.
Duncan Sands57810cd2007-09-05 11:27:52 +00003600 continue;
3601
3602 PadRange P = L->second;
3603 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3604
3605 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3606 "Inconsistent landing pad map!");
3607
3608 // If some instruction between the previous try-range and this one may
3609 // throw, create a call-site entry with no landing pad for the region
3610 // between the try-ranges.
Duncan Sands481dc722007-12-19 07:36:31 +00003611 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003612 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3613 CallSites.push_back(Site);
Duncan Sands481dc722007-12-19 07:36:31 +00003614 PreviousIsInvoke = false;
Duncan Sands57810cd2007-09-05 11:27:52 +00003615 }
3616
3617 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands481dc722007-12-19 07:36:31 +00003618 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands57810cd2007-09-05 11:27:52 +00003619
Duncan Sands481dc722007-12-19 07:36:31 +00003620 if (LandingPad->LandingPadLabel) {
3621 // This try-range is for an invoke.
3622 CallSiteEntry Site = {BeginLabel, LastLabel,
3623 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands57810cd2007-09-05 11:27:52 +00003624
Duncan Sands481dc722007-12-19 07:36:31 +00003625 // Try to merge with the previous call-site.
3626 if (PreviousIsInvoke) {
Dan Gohman719de532008-06-21 22:00:54 +00003627 CallSiteEntry &Prev = CallSites.back();
Duncan Sands481dc722007-12-19 07:36:31 +00003628 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3629 // Extend the range of the previous entry.
3630 Prev.EndLabel = Site.EndLabel;
3631 continue;
3632 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003633 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003634
Duncan Sands481dc722007-12-19 07:36:31 +00003635 // Otherwise, create a new call-site.
3636 CallSites.push_back(Site);
3637 PreviousIsInvoke = true;
3638 } else {
3639 // Create a gap.
3640 PreviousIsInvoke = false;
3641 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003642 }
3643 }
3644 // If some instruction between the previous try-range and the end of the
3645 // function may throw, create a call-site entry with no landing pad for the
3646 // region following the try-range.
Duncan Sands481dc722007-12-19 07:36:31 +00003647 if (SawPotentiallyThrowing) {
Duncan Sands57810cd2007-09-05 11:27:52 +00003648 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3649 CallSites.push_back(Site);
3650 }
3651
Jim Laskeybacd3042007-02-21 22:48:45 +00003652 // Final tallies.
Duncan Sands671fa972008-05-07 19:11:09 +00003653
3654 // Call sites.
3655 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3656 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3657 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3658 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3659 SiteLengthSize +
3660 LandingPadSize);
Duncan Sands57810cd2007-09-05 11:27:52 +00003661 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003662 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands57810cd2007-09-05 11:27:52 +00003663
Duncan Sands671fa972008-05-07 19:11:09 +00003664 // Type infos.
3665 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3666 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003667
3668 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003669 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003670 SizeSites + SizeActions + SizeTypes;
3671
3672 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3673 sizeof(int8_t) + // TType format
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003674 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003675 TypeOffset;
3676
3677 unsigned SizeAlign = (4 - TotalSize) & 3;
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003678
Jim Laskeybacd3042007-02-21 22:48:45 +00003679 // Begin the exception table.
3680 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng05548eb2008-02-29 19:36:59 +00003681 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesend65b2642008-10-08 21:50:21 +00003682 O << "GCC_except_table" << SubprogramCount << ":\n";
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +00003683 for (unsigned i = 0; i != SizeAlign; ++i) {
3684 Asm->EmitInt8(0);
3685 Asm->EOL("Padding");
3686 }
Jim Laskeybacd3042007-02-21 22:48:45 +00003687 EmitLabel("exception", SubprogramCount);
Duncan Sandsc1fe1662007-05-10 18:40:24 +00003688
Jim Laskeybacd3042007-02-21 22:48:45 +00003689 // Emit the header.
3690 Asm->EmitInt8(DW_EH_PE_omit);
3691 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3692 Asm->EmitInt8(DW_EH_PE_absptr);
3693 Asm->EOL("TType format (DW_EH_PE_absptr)");
3694 Asm->EmitULEB128Bytes(TypeOffset);
3695 Asm->EOL("TType base offset");
3696 Asm->EmitInt8(DW_EH_PE_udata4);
3697 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3698 Asm->EmitULEB128Bytes(SizeSites);
3699 Asm->EOL("Call-site table length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003700
Duncan Sands57810cd2007-09-05 11:27:52 +00003701 // Emit the landing pad site information.
3702 for (unsigned i = 0; i < CallSites.size(); ++i) {
3703 CallSiteEntry &S = CallSites[i];
3704 const char *BeginTag;
3705 unsigned BeginNumber;
Duncan Sands53c3a332007-05-16 12:12:23 +00003706
Duncan Sands57810cd2007-09-05 11:27:52 +00003707 if (!S.BeginLabel) {
3708 BeginTag = "eh_func_begin";
3709 BeginNumber = SubprogramCount;
3710 } else {
3711 BeginTag = "label";
3712 BeginNumber = S.BeginLabel;
Duncan Sands53c3a332007-05-16 12:12:23 +00003713 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003714
Duncan Sands57810cd2007-09-05 11:27:52 +00003715 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003716 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003717 Asm->EOL("Region start");
Duncan Sands53c3a332007-05-16 12:12:23 +00003718
Duncan Sands57810cd2007-09-05 11:27:52 +00003719 if (!S.EndLabel) {
Dale Johannesen4af34942008-01-15 23:24:56 +00003720 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands671fa972008-05-07 19:11:09 +00003721 true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003722 } else {
Duncan Sands671fa972008-05-07 19:11:09 +00003723 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003724 }
3725 Asm->EOL("Region length");
Duncan Sands53c3a332007-05-16 12:12:23 +00003726
Duncan Sands671fa972008-05-07 19:11:09 +00003727 if (!S.PadLabel)
3728 Asm->EmitInt32(0);
3729 else
Duncan Sands57810cd2007-09-05 11:27:52 +00003730 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands671fa972008-05-07 19:11:09 +00003731 true, true);
Duncan Sands57810cd2007-09-05 11:27:52 +00003732 Asm->EOL("Landing pad");
3733
3734 Asm->EmitULEB128Bytes(S.Action);
3735 Asm->EOL("Action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003736 }
Duncan Sands53c3a332007-05-16 12:12:23 +00003737
Jim Laskeybacd3042007-02-21 22:48:45 +00003738 // Emit the actions.
Duncan Sandsb32edb42007-06-06 15:37:31 +00003739 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3740 ActionEntry &Action = Actions[I];
Duncan Sands7bf7a442007-05-21 18:50:28 +00003741
Duncan Sandsfccf0a22007-07-06 12:46:24 +00003742 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
Duncan Sandsb32edb42007-06-06 15:37:31 +00003743 Asm->EOL("TypeInfo index");
3744 Asm->EmitSLEB128Bytes(Action.NextAction);
3745 Asm->EOL("Next action");
Jim Laskeybacd3042007-02-21 22:48:45 +00003746 }
3747
3748 // Emit the type ids.
Jim Laskeybacd3042007-02-21 22:48:45 +00003749 for (unsigned M = TypeInfos.size(); M; --M) {
3750 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov9cc54f52007-09-02 22:07:21 +00003751
3752 PrintRelDirective();
Jim Laskeybacd3042007-02-21 22:48:45 +00003753
3754 if (GV)
3755 O << Asm->getGlobalLinkName(GV);
3756 else
3757 O << "0";
Duncan Sands57810cd2007-09-05 11:27:52 +00003758
Jim Laskeybacd3042007-02-21 22:48:45 +00003759 Asm->EOL("TypeInfo");
3760 }
3761
Duncan Sands73ef58a2007-06-02 16:53:42 +00003762 // Emit the filter typeids.
3763 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3764 unsigned TypeID = FilterIds[j];
Duncan Sandsbb821dd2007-07-12 13:51:39 +00003765 Asm->EmitULEB128Bytes(TypeID);
Duncan Sands73ef58a2007-06-02 16:53:42 +00003766 Asm->EOL("Filter TypeInfo index");
Jim Laskey0102ca82007-03-01 20:26:43 +00003767 }
Duncan Sands57810cd2007-09-05 11:27:52 +00003768
Evan Cheng05548eb2008-02-29 19:36:59 +00003769 Asm->EmitAlignment(2, 0, 0, false);
Jim Laskeyb82313f2007-02-01 16:31:34 +00003770 }
3771
Jim Laskey072200c2007-01-29 18:51:14 +00003772public:
3773 //===--------------------------------------------------------------------===//
3774 // Main entry points.
3775 //
Owen Andersoncb371882008-08-21 00:14:44 +00003776 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattner9251f132007-09-24 03:35:37 +00003777 : Dwarf(OS, A, T, "eh")
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003778 , shouldEmitTable(false)
3779 , shouldEmitMoves(false)
3780 , shouldEmitTableModule(false)
3781 , shouldEmitMovesModule(false)
Jim Laskey072200c2007-01-29 18:51:14 +00003782 {}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003783
Jim Laskey072200c2007-01-29 18:51:14 +00003784 virtual ~DwarfException() {}
3785
3786 /// SetModuleInfo - Set machine module information when it's known that pass
3787 /// manager has created it. Set by the target AsmPrinter.
3788 void SetModuleInfo(MachineModuleInfo *mmi) {
Jim Laskey3f09fc22007-02-28 18:38:31 +00003789 MMI = mmi;
Jim Laskey072200c2007-01-29 18:51:14 +00003790 }
3791
3792 /// BeginModule - Emit all exception information that should come prior to the
3793 /// content.
3794 void BeginModule(Module *M) {
3795 this->M = M;
Jim Laskey072200c2007-01-29 18:51:14 +00003796 }
3797
3798 /// EndModule - Emit all exception information that should come after the
3799 /// content.
3800 void EndModule() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003801 if (shouldEmitMovesModule || shouldEmitTableModule) {
3802 const std::vector<Function *> Personalities = MMI->getPersonalities();
3803 for (unsigned i =0; i < Personalities.size(); ++i)
3804 EmitCommonEHFrame(Personalities[i], i);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +00003805
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003806 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3807 E = EHFrames.end(); I != E; ++I)
3808 EmitEHFrame(*I);
3809 }
Jim Laskey072200c2007-01-29 18:51:14 +00003810 }
3811
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003812 /// BeginFunction - Gather pre-function exception information. Assumes being
Jim Laskey072200c2007-01-29 18:51:14 +00003813 /// emitted immediately after the function entry point.
3814 void BeginFunction(MachineFunction *MF) {
3815 this->MF = MF;
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003816 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesene0040622008-04-02 17:04:45 +00003817 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003818
3819 // Map all labels and get rid of any dead landing pads.
3820 MMI->TidyLandingPads();
3821 // If any landing pads survive, we need an EH table.
3822 if (MMI->getLandingPads().size())
3823 shouldEmitTable = true;
3824
3825 // See if we need frame move info.
Duncan Sandsececf992008-07-04 09:55:48 +00003826 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003827 shouldEmitMoves = true;
3828
3829 if (shouldEmitMoves || shouldEmitTable)
3830 // Assumes in correct section after the entry point.
3831 EmitLabel("eh_func_begin", ++SubprogramCount);
Jim Laskey3f09fc22007-02-28 18:38:31 +00003832 }
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003833 shouldEmitTableModule |= shouldEmitTable;
3834 shouldEmitMovesModule |= shouldEmitMoves;
Jim Laskey072200c2007-01-29 18:51:14 +00003835 }
3836
3837 /// EndFunction - Gather and emit post-function exception information.
3838 ///
3839 void EndFunction() {
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003840 if (shouldEmitMoves || shouldEmitTable) {
3841 EmitLabel("eh_func_end", SubprogramCount);
3842 EmitExceptionTable();
Jim Laskeyb82313f2007-02-01 16:31:34 +00003843
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003844 // Save EH frame information
3845 EHFrames.
3846 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendling6e198962007-09-18 01:47:22 +00003847 SubprogramCount,
3848 MMI->getPersonalityIndex(),
3849 MF->getFrameInfo()->hasCalls(),
3850 !MMI->getLandingPads().empty(),
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00003851 MMI->getFrameMoves(),
Dale Johannesen48ae02f2008-01-16 19:59:28 +00003852 MF->getFunction()));
Dale Johannesen1532f3d2008-04-02 00:25:04 +00003853 }
Jim Laskey072200c2007-01-29 18:51:14 +00003854 }
3855};
3856
Jim Laskey0d086af2006-02-27 12:43:29 +00003857} // End of namespace llvm
Jim Laskey063e7652006-01-17 17:31:53 +00003858
3859//===----------------------------------------------------------------------===//
3860
Jim Laskeyd18e2892006-01-20 20:34:06 +00003861/// Emit - Print the abbreviation using the specified Dwarf writer.
3862///
Jim Laskey072200c2007-01-29 18:51:14 +00003863void DIEAbbrev::Emit(const DwarfDebug &DD) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003864 // Emit its Dwarf tag type.
Jim Laskey072200c2007-01-29 18:51:14 +00003865 DD.getAsm()->EmitULEB128Bytes(Tag);
3866 DD.getAsm()->EOL(TagString(Tag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003867
Jim Laskeyd18e2892006-01-20 20:34:06 +00003868 // Emit whether it has children DIEs.
Jim Laskey072200c2007-01-29 18:51:14 +00003869 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3870 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003871
Jim Laskeyd18e2892006-01-20 20:34:06 +00003872 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +00003873 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00003874 const DIEAbbrevData &AttrData = Data[i];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003875
Jim Laskeyd18e2892006-01-20 20:34:06 +00003876 // Emit attribute type.
Jim Laskey072200c2007-01-29 18:51:14 +00003877 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3878 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003879
Jim Laskeyd18e2892006-01-20 20:34:06 +00003880 // Emit form type.
Jim Laskey072200c2007-01-29 18:51:14 +00003881 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3882 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
Jim Laskeyd18e2892006-01-20 20:34:06 +00003883 }
3884
3885 // Mark end of abbreviation.
Jim Laskey072200c2007-01-29 18:51:14 +00003886 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3887 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
Jim Laskeyd18e2892006-01-20 20:34:06 +00003888}
3889
3890#ifndef NDEBUG
Jim Laskeya0f3d172006-09-07 22:06:40 +00003891void DIEAbbrev::print(std::ostream &O) {
3892 O << "Abbreviation @"
3893 << std::hex << (intptr_t)this << std::dec
3894 << " "
3895 << TagString(Tag)
3896 << " "
3897 << ChildrenString(ChildrenFlag)
3898 << "\n";
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003899
Jim Laskeya0f3d172006-09-07 22:06:40 +00003900 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3901 O << " "
3902 << AttributeString(Data[i].getAttribute())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003903 << " "
Jim Laskeya0f3d172006-09-07 22:06:40 +00003904 << FormEncodingString(Data[i].getForm())
Jim Laskeyd18e2892006-01-20 20:34:06 +00003905 << "\n";
Jim Laskeyd18e2892006-01-20 20:34:06 +00003906 }
Jim Laskeya0f3d172006-09-07 22:06:40 +00003907}
Bill Wendlinge8156192006-12-07 01:30:32 +00003908void DIEAbbrev::dump() { print(cerr); }
Jim Laskeyd18e2892006-01-20 20:34:06 +00003909#endif
3910
3911//===----------------------------------------------------------------------===//
3912
Jim Laskeyef42a012006-11-02 20:12:39 +00003913#ifndef NDEBUG
3914void DIEValue::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00003915 print(cerr);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00003916}
Jim Laskeyef42a012006-11-02 20:12:39 +00003917#endif
3918
3919//===----------------------------------------------------------------------===//
3920
Jim Laskey063e7652006-01-17 17:31:53 +00003921/// EmitValue - Emit integer of appropriate size.
3922///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003923void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey063e7652006-01-17 17:31:53 +00003924 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +00003925 case DW_FORM_flag: // Fall thru
Jim Laskeyb8509c52006-03-23 18:07:55 +00003926 case DW_FORM_ref1: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003927 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003928 case DW_FORM_ref2: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003929 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003930 case DW_FORM_ref4: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003931 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
Jim Laskeyb8509c52006-03-23 18:07:55 +00003932 case DW_FORM_ref8: // Fall thru
Jim Laskey072200c2007-01-29 18:51:14 +00003933 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
3934 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
3935 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003936 default: assert(0 && "DIE Value form not supported yet"); break;
3937 }
3938}
3939
3940/// SizeOf - Determine size of integer value in bytes.
3941///
Jim Laskey072200c2007-01-29 18:51:14 +00003942unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003943 switch (Form) {
3944 case DW_FORM_flag: // Fall thru
3945 case DW_FORM_ref1: // Fall thru
3946 case DW_FORM_data1: return sizeof(int8_t);
3947 case DW_FORM_ref2: // Fall thru
3948 case DW_FORM_data2: return sizeof(int16_t);
3949 case DW_FORM_ref4: // Fall thru
3950 case DW_FORM_data4: return sizeof(int32_t);
3951 case DW_FORM_ref8: // Fall thru
3952 case DW_FORM_data8: return sizeof(int64_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003953 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
3954 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +00003955 default: assert(0 && "DIE Value form not supported yet"); break;
3956 }
Jim Laskeyf1cdea12007-01-25 15:12:02 +00003957 return 0;
Jim Laskey063e7652006-01-17 17:31:53 +00003958}
3959
Jim Laskey063e7652006-01-17 17:31:53 +00003960//===----------------------------------------------------------------------===//
3961
3962/// EmitValue - Emit string value.
3963///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003964void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00003965 DD.getAsm()->EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +00003966}
3967
Jim Laskey063e7652006-01-17 17:31:53 +00003968//===----------------------------------------------------------------------===//
3969
3970/// EmitValue - Emit label value.
3971///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003972void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003973 bool IsSmall = Form == DW_FORM_data4;
3974 DD.EmitReference(Label, false, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00003975}
3976
3977/// SizeOf - Determine size of label value in bytes.
3978///
Jim Laskey072200c2007-01-29 18:51:14 +00003979unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003980 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003981 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00003982}
Jim Laskeyef42a012006-11-02 20:12:39 +00003983
Jim Laskey063e7652006-01-17 17:31:53 +00003984//===----------------------------------------------------------------------===//
3985
Jim Laskeyd18e2892006-01-20 20:34:06 +00003986/// EmitValue - Emit label value.
3987///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00003988void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003989 bool IsSmall = Form == DW_FORM_data4;
3990 DD.EmitReference(Label, false, IsSmall);
Jim Laskeyd18e2892006-01-20 20:34:06 +00003991}
3992
3993/// SizeOf - Determine size of label value in bytes.
3994///
Jim Laskey072200c2007-01-29 18:51:14 +00003995unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman9fda5be2007-09-28 16:50:28 +00003996 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00003997 return DD.getTargetData()->getPointerSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +00003998}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00003999
Jim Laskeyd18e2892006-01-20 20:34:06 +00004000//===----------------------------------------------------------------------===//
4001
Jim Laskey063e7652006-01-17 17:31:53 +00004002/// EmitValue - Emit delta value.
4003///
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004004void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4005 bool IsSmall = Form == DW_FORM_data4;
4006 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4007 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4008}
4009
4010/// SizeOf - Determine size of delta value in bytes.
4011///
4012unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4013 if (Form == DW_FORM_data4) return 4;
4014 return DD.getTargetData()->getPointerSize();
4015}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004016
Argyrios Kyrtzidisf7acf8f2008-06-18 19:27:37 +00004017//===----------------------------------------------------------------------===//
4018
4019/// EmitValue - Emit delta value.
4020///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004021void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004022 bool IsSmall = Form == DW_FORM_data4;
Jim Laskey072200c2007-01-29 18:51:14 +00004023 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
Jim Laskey063e7652006-01-17 17:31:53 +00004024}
4025
4026/// SizeOf - Determine size of delta value in bytes.
4027///
Jim Laskey072200c2007-01-29 18:51:14 +00004028unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskey2b4e98c2006-12-06 17:43:18 +00004029 if (Form == DW_FORM_data4) return 4;
Dan Gohman82482942007-09-27 23:12:31 +00004030 return DD.getTargetData()->getPointerSize();
Jim Laskey063e7652006-01-17 17:31:53 +00004031}
4032
4033//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004034
Jim Laskeyb8509c52006-03-23 18:07:55 +00004035/// EmitValue - Emit debug information entry offset.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004036///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004037void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004038 DD.getAsm()->EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +00004039}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004040
Jim Laskeyd18e2892006-01-20 20:34:06 +00004041//===----------------------------------------------------------------------===//
4042
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004043/// ComputeSize - calculate the size of the block.
4044///
Jim Laskey072200c2007-01-29 18:51:14 +00004045unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
Jim Laskeyef42a012006-11-02 20:12:39 +00004046 if (!Size) {
Owen Anderson873e1b52008-06-24 21:44:59 +00004047 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004048
Jim Laskeyef42a012006-11-02 20:12:39 +00004049 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey072200c2007-01-29 18:51:14 +00004050 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
Jim Laskeyef42a012006-11-02 20:12:39 +00004051 }
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004052 }
4053 return Size;
4054}
4055
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004056/// EmitValue - Emit block data.
4057///
Anton Korobeynikov6a143592007-03-07 08:25:02 +00004058void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004059 switch (Form) {
Jim Laskey072200c2007-01-29 18:51:14 +00004060 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4061 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4062 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4063 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
Jim Laskeyf1cdea12007-01-25 15:12:02 +00004064 default: assert(0 && "Improper form for block"); break;
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004065 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004066
Owen Anderson873e1b52008-06-24 21:44:59 +00004067 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Jim Laskeyef42a012006-11-02 20:12:39 +00004068
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004069 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeybacd3042007-02-21 22:48:45 +00004070 DD.getAsm()->EOL();
Jim Laskey072200c2007-01-29 18:51:14 +00004071 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004072 }
4073}
4074
4075/// SizeOf - Determine size of block data in bytes.
4076///
Jim Laskey072200c2007-01-29 18:51:14 +00004077unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004078 switch (Form) {
4079 case DW_FORM_block1: return Size + sizeof(int8_t);
4080 case DW_FORM_block2: return Size + sizeof(int16_t);
4081 case DW_FORM_block4: return Size + sizeof(int32_t);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004082 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004083 default: assert(0 && "Improper form for block"); break;
4084 }
4085 return 0;
4086}
4087
Jim Laskeyb80af6f2006-03-03 21:00:14 +00004088//===----------------------------------------------------------------------===//
Jim Laskeyef42a012006-11-02 20:12:39 +00004089/// DIE Implementation
Jim Laskeyd18e2892006-01-20 20:34:06 +00004090
4091DIE::~DIE() {
Jim Laskeyef42a012006-11-02 20:12:39 +00004092 for (unsigned i = 0, N = Children.size(); i < N; ++i)
Jim Laskeyd18e2892006-01-20 20:34:06 +00004093 delete Children[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +00004094}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004095
Jim Laskeyb8509c52006-03-23 18:07:55 +00004096/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4097///
4098void DIE::AddSiblingOffset() {
4099 DIEInteger *DI = new DIEInteger(0);
4100 Values.insert(Values.begin(), DI);
Jim Laskeya9c83fe2006-10-30 15:59:54 +00004101 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
Jim Laskeyb8509c52006-03-23 18:07:55 +00004102}
4103
Jim Laskeyef42a012006-11-02 20:12:39 +00004104/// Profile - Used to gather unique data for the value folding set.
Jim Laskeyd18e2892006-01-20 20:34:06 +00004105///
Jim Laskeyef42a012006-11-02 20:12:39 +00004106void DIE::Profile(FoldingSetNodeID &ID) {
4107 Abbrev.Profile(ID);
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004108
Jim Laskeyef42a012006-11-02 20:12:39 +00004109 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4110 ID.AddPointer(Children[i]);
4111
4112 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4113 ID.AddPointer(Values[j]);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00004114}
Jim Laskeyef42a012006-11-02 20:12:39 +00004115
4116#ifndef NDEBUG
4117void DIE::print(std::ostream &O, unsigned IncIndent) {
4118 static unsigned IndentCount = 0;
4119 IndentCount += IncIndent;
4120 const std::string Indent(IndentCount, ' ');
4121 bool isBlock = Abbrev.getTag() == 0;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004122
Jim Laskeyef42a012006-11-02 20:12:39 +00004123 if (!isBlock) {
4124 O << Indent
4125 << "Die: "
4126 << "0x" << std::hex << (intptr_t)this << std::dec
4127 << ", Offset: " << Offset
4128 << ", Size: " << Size
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004129 << "\n";
4130
Jim Laskeyef42a012006-11-02 20:12:39 +00004131 O << Indent
4132 << TagString(Abbrev.getTag())
Jim Laskey063e7652006-01-17 17:31:53 +00004133 << " "
Jim Laskeyef42a012006-11-02 20:12:39 +00004134 << ChildrenString(Abbrev.getChildrenFlag());
4135 } else {
4136 O << "Size: " << Size;
Jim Laskey063e7652006-01-17 17:31:53 +00004137 }
4138 O << "\n";
Jim Laskeya7cea6f2006-01-04 13:52:30 +00004139
Owen Anderson873e1b52008-06-24 21:44:59 +00004140 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004141
Jim Laskeyef42a012006-11-02 20:12:39 +00004142 IndentCount += 2;
4143 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4144 O << Indent;
Bill Wendling4e974012008-07-22 00:28:47 +00004145
4146 if (!isBlock)
Jim Laskeyef42a012006-11-02 20:12:39 +00004147 O << AttributeString(Data[i].getAttribute());
Bill Wendling4e974012008-07-22 00:28:47 +00004148 else
Jim Laskeyef42a012006-11-02 20:12:39 +00004149 O << "Blk[" << i << "]";
Bill Wendling4e974012008-07-22 00:28:47 +00004150
Jim Laskeyef42a012006-11-02 20:12:39 +00004151 O << " "
4152 << FormEncodingString(Data[i].getForm())
4153 << " ";
4154 Values[i]->print(O);
Jim Laskey0d086af2006-02-27 12:43:29 +00004155 O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00004156 }
Jim Laskeyef42a012006-11-02 20:12:39 +00004157 IndentCount -= 2;
Jim Laskey063e7652006-01-17 17:31:53 +00004158
Jim Laskeyef42a012006-11-02 20:12:39 +00004159 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4160 Children[j]->print(O, 4);
Jim Laskey063e7652006-01-17 17:31:53 +00004161 }
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004162
Jim Laskeyef42a012006-11-02 20:12:39 +00004163 if (!isBlock) O << "\n";
4164 IndentCount -= IncIndent;
Jim Laskey19ef4ef2006-01-17 20:41:40 +00004165}
4166
Jim Laskeyef42a012006-11-02 20:12:39 +00004167void DIE::dump() {
Bill Wendlinge8156192006-12-07 01:30:32 +00004168 print(cerr);
Jim Laskey41886992006-04-07 16:34:46 +00004169}
Jim Laskeybd761842006-02-27 17:27:12 +00004170#endif
Jim Laskey65195462006-10-30 13:35:07 +00004171
4172//===----------------------------------------------------------------------===//
4173/// DwarfWriter Implementation
Jim Laskeyef42a012006-11-02 20:12:39 +00004174///
Jim Laskey65195462006-10-30 13:35:07 +00004175
Devang Pateleb3fc282009-01-08 23:40:34 +00004176DwarfWriter::DwarfWriter() : ImmutablePass(&ID), DD(NULL), DE(NULL) {
Jim Laskey65195462006-10-30 13:35:07 +00004177}
4178
4179DwarfWriter::~DwarfWriter() {
Jim Laskey072200c2007-01-29 18:51:14 +00004180 delete DE;
4181 delete DD;
Jim Laskey65195462006-10-30 13:35:07 +00004182}
4183
Jim Laskey65195462006-10-30 13:35:07 +00004184/// BeginModule - Emit all Dwarf sections that should come prior to the
4185/// content.
Devang Pateleb3fc282009-01-08 23:40:34 +00004186void DwarfWriter::BeginModule(Module *M,
4187 MachineModuleInfo *MMI,
4188 raw_ostream &OS, AsmPrinter *A,
4189 const TargetAsmInfo *T) {
4190 DE = new DwarfException(OS, A, T);
4191 DD = new DwarfDebug(OS, A, T);
Jim Laskey072200c2007-01-29 18:51:14 +00004192 DE->BeginModule(M);
4193 DD->BeginModule(M);
Devang Patel7bb89ed2009-01-13 00:20:51 +00004194 DD->SetDebugInfo(MMI);
Devang Pateleb3fc282009-01-08 23:40:34 +00004195 DE->SetModuleInfo(MMI);
Jim Laskey65195462006-10-30 13:35:07 +00004196}
4197
4198/// EndModule - Emit all Dwarf sections that should come after the content.
4199///
4200void DwarfWriter::EndModule() {
Jim Laskey072200c2007-01-29 18:51:14 +00004201 DE->EndModule();
4202 DD->EndModule();
Jim Laskey65195462006-10-30 13:35:07 +00004203}
4204
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004205/// BeginFunction - Gather pre-function debug information. Assumes being
Jim Laskey65195462006-10-30 13:35:07 +00004206/// emitted immediately after the function entry point.
4207void DwarfWriter::BeginFunction(MachineFunction *MF) {
Jim Laskey072200c2007-01-29 18:51:14 +00004208 DE->BeginFunction(MF);
4209 DD->BeginFunction(MF);
Jim Laskey65195462006-10-30 13:35:07 +00004210}
4211
4212/// EndFunction - Gather and emit post-function debug information.
4213///
Bill Wendlingd751c642008-09-26 00:28:12 +00004214void DwarfWriter::EndFunction(MachineFunction *MF) {
4215 DD->EndFunction(MF);
Jim Laskeyb82313f2007-02-01 16:31:34 +00004216 DE->EndFunction();
Anton Korobeynikovffe31d72008-08-16 12:57:46 +00004217
Bill Wendlingc91d0b92008-07-22 00:53:37 +00004218 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Jim Laskeyb82313f2007-02-01 16:31:34 +00004219 // Clear function debug information.
4220 MMI->EndFunction();
Jim Laskey65195462006-10-30 13:35:07 +00004221}
Devang Patelccca7fe2009-01-12 19:17:34 +00004222
4223/// RecordSourceLine - Records location information and associates it with a
4224/// label. Returns a unique label ID used to generate a label and provide
4225/// correspondence to the source line list.
4226unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4227 unsigned Src) {
4228 return DD->RecordSourceLine(Line, Col, Src);
4229}
4230
4231/// RecordSource - Register a source file with debug info. Returns an source
4232/// ID.
4233unsigned DwarfWriter::RecordSource(const std::string &Dir,
4234 const std::string &File) {
4235 return DD->RecordSource(Dir, File);
4236}
4237
4238/// RecordRegionStart - Indicate the start of a region.
4239unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
4240 return DD->RecordRegionStart(V);
4241}
4242
4243/// RecordRegionEnd - Indicate the end of a region.
4244unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
4245 return DD->RecordRegionEnd(V);
4246}
4247
4248/// getRecordSourceLineCount - Count source lines.
4249unsigned DwarfWriter::getRecordSourceLineCount() {
4250 return DD->getRecordSourceLineCount();
4251}
Devang Patelbb8c5952009-01-13 21:25:00 +00004252
Devang Patelc48c5502009-01-13 21:44:10 +00004253/// RecordVariable - Indicate the declaration of a local variable.
4254///
4255void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
4256 DD->RecordVariable(GV, FrameIndex);
4257}
Devang Patelbbdc8202009-01-13 23:54:55 +00004258