blob: d73f2fe5bfb140e8c4e6e0c9aedd1572c0f1c7e2 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf info into asm files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/DwarfWriter.h"
15
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/FoldingSet.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/ADT/UniqueVector.h"
20#include "llvm/Module.h"
Devang Patelb3907da2009-01-05 23:03:32 +000021#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/CodeGen/AsmPrinter.h"
23#include "llvm/CodeGen/MachineModuleInfo.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineLocation.h"
Devang Patelfc187162009-01-05 17:57:47 +000026#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/Dwarf.h"
29#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/DataTypes.h"
31#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000032#include "llvm/Support/raw_ostream.h"
Dan Gohman80bbde72007-09-24 21:32:18 +000033#include "llvm/System/Path.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000035#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Target/TargetData.h"
37#include "llvm/Target/TargetFrameInfo.h"
38#include "llvm/Target/TargetInstrInfo.h"
39#include "llvm/Target/TargetMachine.h"
40#include "llvm/Target/TargetOptions.h"
41#include <ostream>
42#include <string>
43using namespace llvm;
44using namespace llvm::dwarf;
45
Devang Patelaa1e8432009-01-08 23:40:34 +000046static RegisterPass<DwarfWriter>
47X("dwarfwriter", "DWARF Information Writer");
48char DwarfWriter::ID = 0;
49
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050namespace llvm {
aslc200b112008-08-16 12:57:46 +000051
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052//===----------------------------------------------------------------------===//
53
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 Patelb3907da2009-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//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104/// DWLabel - Labels are used to track locations in the assembler file.
aslc200b112008-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 Spencer37c7cea2007-08-05 20:06:04 +0000107/// unique in that category.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108class DWLabel {
109public:
110 /// Tag - Label category tag. Should always be a staticly declared C string.
111 ///
112 const char *Tag;
aslc200b112008-08-16 12:57:46 +0000113
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 /// Number - Value to make label unique.
115 ///
116 unsigned Number;
117
118 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
aslc200b112008-08-16 12:57:46 +0000119
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 void Profile(FoldingSetNodeID &ID) const {
121 ID.AddString(std::string(Tag));
122 ID.AddInteger(Number);
123 }
aslc200b112008-08-16 12:57:46 +0000124
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125#ifndef NDEBUG
126 void print(std::ostream *O) const {
127 if (O) print(*O);
128 }
129 void print(std::ostream &O) const {
130 O << "." << Tag;
131 if (Number) O << Number;
132 }
133#endif
134};
135
136//===----------------------------------------------------------------------===//
137/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
138/// Dwarf abbreviation.
139class DIEAbbrevData {
140private:
141 /// Attribute - Dwarf attribute code.
142 ///
143 unsigned Attribute;
aslc200b112008-08-16 12:57:46 +0000144
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 /// Form - Dwarf form code.
aslc200b112008-08-16 12:57:46 +0000146 ///
147 unsigned Form;
148
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149public:
150 DIEAbbrevData(unsigned A, unsigned F)
151 : Attribute(A)
152 , Form(F)
153 {}
aslc200b112008-08-16 12:57:46 +0000154
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 // Accessors.
156 unsigned getAttribute() const { return Attribute; }
157 unsigned getForm() const { return Form; }
158
159 /// Profile - Used to gather unique data for the abbreviation folding set.
160 ///
161 void Profile(FoldingSetNodeID &ID)const {
162 ID.AddInteger(Attribute);
163 ID.AddInteger(Form);
164 }
165};
166
167//===----------------------------------------------------------------------===//
168/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
169/// information object.
170class DIEAbbrev : public FoldingSetNode {
171private:
172 /// Tag - Dwarf tag code.
173 ///
174 unsigned Tag;
aslc200b112008-08-16 12:57:46 +0000175
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Anderson88dd6232008-06-24 21:44:59 +0000186 SmallVector<DIEAbbrevData, 8> Data;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187
188public:
189
190 DIEAbbrev(unsigned T, unsigned C)
191 : Tag(T)
192 , ChildrenFlag(C)
193 , Data()
194 {}
195 ~DIEAbbrev() {}
aslc200b112008-08-16 12:57:46 +0000196
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 // Accessors.
198 unsigned getTag() const { return Tag; }
199 unsigned getNumber() const { return Number; }
200 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000201 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 void setTag(unsigned T) { Tag = T; }
203 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
204 void setNumber(unsigned N) { Number = N; }
aslc200b112008-08-16 12:57:46 +0000205
Dan Gohmanf17a25c2007-07-18 16:29:46 +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));
210 }
aslc200b112008-08-16 12:57:46 +0000211
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 }
aslc200b112008-08-16 12:57:46 +0000217
Dan Gohmanf17a25c2007-07-18 16:29:46 +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);
aslc200b112008-08-16 12:57:46 +0000223
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 // For each attribute description.
225 for (unsigned i = 0, N = Data.size(); i < N; ++i)
226 Data[i].Profile(ID);
227 }
aslc200b112008-08-16 12:57:46 +0000228
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 /// Emit - Print the abbreviation using the specified Dwarf writer.
230 ///
aslc200b112008-08-16 12:57:46 +0000231 void Emit(const DwarfDebug &DD) const;
232
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233#ifndef NDEBUG
234 void print(std::ostream *O) {
235 if (O) print(*O);
236 }
237 void print(std::ostream &O);
238 void dump();
239#endif
240};
241
242//===----------------------------------------------------------------------===//
243/// 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;
aslc200b112008-08-16 12:57:46 +0000250
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 /// Offset - Offset in debug info section.
252 ///
253 unsigned Offset;
aslc200b112008-08-16 12:57:46 +0000254
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 /// Size - Size of instance + children.
256 ///
257 unsigned Size;
aslc200b112008-08-16 12:57:46 +0000258
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 /// Children DIEs.
260 ///
261 std::vector<DIE *> Children;
aslc200b112008-08-16 12:57:46 +0000262
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 /// Attributes values.
264 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000265 SmallVector<DIEValue*, 32> Values;
aslc200b112008-08-16 12:57:46 +0000266
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000268 explicit DIE(unsigned Tag)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 : Abbrev(Tag, DW_CHILDREN_no)
270 , Offset(0)
271 , Size(0)
272 , Children()
273 , Values()
274 {}
275 virtual ~DIE();
aslc200b112008-08-16 12:57:46 +0000276
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 // Accessors.
278 DIEAbbrev &getAbbrev() { return Abbrev; }
279 unsigned getAbbrevNumber() const {
280 return Abbrev.getNumber();
281 }
282 unsigned getTag() const { return Abbrev.getTag(); }
283 unsigned getOffset() const { return Offset; }
284 unsigned getSize() const { return Size; }
285 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000286 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
288 void setOffset(unsigned O) { Offset = O; }
289 void setSize(unsigned S) { Size = S; }
aslc200b112008-08-16 12:57:46 +0000290
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 }
aslc200b112008-08-16 12:57:46 +0000297
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 /// SiblingOffset - Return the offset of the debug information entry's
299 /// sibling.
300 unsigned SiblingOffset() const { return Offset + Size; }
aslc200b112008-08-16 12:57:46 +0000301
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 }
aslc200b112008-08-16 12:57:46 +0000312
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 /// Detach - Detaches objects connected to it after copying.
314 ///
315 void Detach() {
316 Children.clear();
317 }
aslc200b112008-08-16 12:57:46 +0000318
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 /// Profile - Used to gather unique data for the value folding set.
320 ///
321 void Profile(FoldingSetNodeID &ID) ;
aslc200b112008-08-16 12:57:46 +0000322
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323#ifndef NDEBUG
324 void print(std::ostream *O, unsigned IncIndent = 0) {
325 if (O) print(*O, IncIndent);
326 }
327 void print(std::ostream &O, unsigned IncIndent = 0);
328 void dump();
329#endif
330};
331
332//===----------------------------------------------------------------------===//
333/// DIEValue - A debug information entry value.
334///
335class DIEValue : public FoldingSetNode {
336public:
337 enum {
338 isInteger,
339 isString,
340 isLabel,
341 isAsIsLabel,
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000342 isSectionOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 isDelta,
344 isEntry,
345 isBlock
346 };
aslc200b112008-08-16 12:57:46 +0000347
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 /// Type - Type of data stored in the value.
349 ///
350 unsigned Type;
aslc200b112008-08-16 12:57:46 +0000351
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000352 explicit DIEValue(unsigned T)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 : Type(T)
354 {}
355 virtual ~DIEValue() {}
aslc200b112008-08-16 12:57:46 +0000356
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 // Accessors
358 unsigned getType() const { return Type; }
aslc200b112008-08-16 12:57:46 +0000359
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 // Implement isa/cast/dyncast.
361 static bool classof(const DIEValue *) { return true; }
aslc200b112008-08-16 12:57:46 +0000362
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 /// EmitValue - Emit value via the Dwarf writer.
364 ///
365 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
aslc200b112008-08-16 12:57:46 +0000366
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 /// SizeOf - Return the size of a value in bytes.
368 ///
369 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
aslc200b112008-08-16 12:57:46 +0000370
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371 /// Profile - Used to gather unique data for the value folding set.
372 ///
373 virtual void Profile(FoldingSetNodeID &ID) = 0;
aslc200b112008-08-16 12:57:46 +0000374
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375#ifndef NDEBUG
376 void print(std::ostream *O) {
377 if (O) print(*O);
378 }
379 virtual void print(std::ostream &O) = 0;
380 void dump();
381#endif
382};
383
384//===----------------------------------------------------------------------===//
385/// DWInteger - An integer value DIE.
aslc200b112008-08-16 12:57:46 +0000386///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387class DIEInteger : public DIEValue {
388private:
389 uint64_t Integer;
aslc200b112008-08-16 12:57:46 +0000390
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000392 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393
394 // Implement isa/cast/dyncast.
395 static bool classof(const DIEInteger *) { return true; }
396 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
aslc200b112008-08-16 12:57:46 +0000397
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 /// BestForm - Choose the best form for integer.
399 ///
400 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 }
aslc200b112008-08-16 12:57:46 +0000412
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413 /// EmitValue - Emit integer of appropriate size.
414 ///
415 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000416
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 /// SizeOf - Determine size of integer value in bytes.
418 ///
419 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000420
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 /// Profile - Used to gather unique data for the value folding set.
422 ///
423 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
424 ID.AddInteger(isInteger);
425 ID.AddInteger(Integer);
426 }
427 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
aslc200b112008-08-16 12:57:46 +0000428
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
435};
436
437//===----------------------------------------------------------------------===//
438/// DIEString - A string value DIE.
aslc200b112008-08-16 12:57:46 +0000439///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440class DIEString : public DIEValue {
441public:
442 const std::string String;
aslc200b112008-08-16 12:57:46 +0000443
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000444 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445
446 // Implement isa/cast/dyncast.
447 static bool classof(const DIEString *) { return true; }
448 static bool classof(const DIEValue *S) { return S->Type == isString; }
aslc200b112008-08-16 12:57:46 +0000449
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 /// EmitValue - Emit string value.
451 ///
452 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000453
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 /// SizeOf - Determine size of string value in bytes.
455 ///
456 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
457 return String.size() + sizeof(char); // sizeof('\0');
458 }
aslc200b112008-08-16 12:57:46 +0000459
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 /// Profile - Used to gather unique data for the value folding set.
461 ///
462 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
463 ID.AddInteger(isString);
464 ID.AddString(String);
465 }
466 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
aslc200b112008-08-16 12:57:46 +0000467
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468#ifndef NDEBUG
469 virtual void print(std::ostream &O) {
470 O << "Str: \"" << String << "\"";
471 }
472#endif
473};
474
475//===----------------------------------------------------------------------===//
476/// DIEDwarfLabel - A Dwarf internal label expression DIE.
477//
478class DIEDwarfLabel : public DIEValue {
479public:
480
481 const DWLabel Label;
aslc200b112008-08-16 12:57:46 +0000482
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000483 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484
485 // Implement isa/cast/dyncast.
486 static bool classof(const DIEDwarfLabel *) { return true; }
487 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
aslc200b112008-08-16 12:57:46 +0000488
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489 /// EmitValue - Emit label value.
490 ///
491 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000492
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 /// SizeOf - Determine size of label value in bytes.
494 ///
495 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000496
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 /// Profile - Used to gather unique data for the value folding set.
498 ///
499 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
500 ID.AddInteger(isLabel);
501 Label.Profile(ID);
502 }
503 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
aslc200b112008-08-16 12:57:46 +0000504
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505#ifndef NDEBUG
506 virtual void print(std::ostream &O) {
507 O << "Lbl: ";
508 Label.print(O);
509 }
510#endif
511};
512
513
514//===----------------------------------------------------------------------===//
515/// DIEObjectLabel - A label to an object in code or data.
516//
517class DIEObjectLabel : public DIEValue {
518public:
519 const std::string Label;
aslc200b112008-08-16 12:57:46 +0000520
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000521 explicit DIEObjectLabel(const std::string &L)
522 : DIEValue(isAsIsLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523
524 // Implement isa/cast/dyncast.
525 static bool classof(const DIEObjectLabel *) { return true; }
526 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
aslc200b112008-08-16 12:57:46 +0000527
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 /// EmitValue - Emit label value.
529 ///
530 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000531
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532 /// SizeOf - Determine size of label value in bytes.
533 ///
534 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000535
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 /// Profile - Used to gather unique data for the value folding set.
537 ///
538 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
539 ID.AddInteger(isAsIsLabel);
540 ID.AddString(Label);
541 }
542 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
543
544#ifndef NDEBUG
545 virtual void print(std::ostream &O) {
546 O << "Obj: " << Label;
547 }
548#endif
549};
550
551//===----------------------------------------------------------------------===//
Argiris Kirtzidis03449652008-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;
aslc200b112008-08-16 12:57:46 +0000560
Argiris Kirtzidis03449652008-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; }
aslc200b112008-08-16 12:57:46 +0000569
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000570 /// EmitValue - Emit section offset.
571 ///
572 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000573
Argiris Kirtzidis03449652008-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;
aslc200b112008-08-16 12:57:46 +0000577
Argiris Kirtzidis03449652008-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//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602/// DIEDelta - A simple label difference DIE.
aslc200b112008-08-16 12:57:46 +0000603///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604class DIEDelta : public DIEValue {
605public:
606 const DWLabel LabelHi;
607 const DWLabel LabelLo;
aslc200b112008-08-16 12:57:46 +0000608
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
610 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
611
612 // Implement isa/cast/dyncast.
613 static bool classof(const DIEDelta *) { return true; }
614 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
aslc200b112008-08-16 12:57:46 +0000615
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 /// EmitValue - Emit delta value.
617 ///
618 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000619
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 /// SizeOf - Determine size of delta value in bytes.
621 ///
622 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000623
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624 /// Profile - Used to gather unique data for the value folding set.
625 ///
626 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
627 const DWLabel &LabelLo) {
628 ID.AddInteger(isDelta);
629 LabelHi.Profile(ID);
630 LabelLo.Profile(ID);
631 }
632 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
633
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
642};
643
644//===----------------------------------------------------------------------===//
645/// 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:
650 DIE *Entry;
aslc200b112008-08-16 12:57:46 +0000651
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000652 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
aslc200b112008-08-16 12:57:46 +0000653
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654 // Implement isa/cast/dyncast.
655 static bool classof(const DIEntry *) { return true; }
656 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
aslc200b112008-08-16 12:57:46 +0000657
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 /// EmitValue - Emit debug information entry offset.
659 ///
660 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000661
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 /// SizeOf - Determine size of debug information entry in bytes.
663 ///
664 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
665 return sizeof(int32_t);
666 }
aslc200b112008-08-16 12:57:46 +0000667
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668 /// Profile - Used to gather unique data for the value folding set.
669 ///
670 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
671 ID.AddInteger(isEntry);
672 ID.AddPointer(Entry);
673 }
674 virtual void Profile(FoldingSetNodeID &ID) {
675 ID.AddInteger(isEntry);
aslc200b112008-08-16 12:57:46 +0000676
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 if (Entry) {
678 ID.AddPointer(Entry);
679 } else {
680 ID.AddPointer(this);
681 }
682 }
aslc200b112008-08-16 12:57:46 +0000683
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000684#ifndef NDEBUG
685 virtual void print(std::ostream &O) {
686 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
687 }
688#endif
689};
690
691//===----------------------------------------------------------------------===//
692/// DIEBlock - A block of values. Primarily used for location expressions.
693//
694class DIEBlock : public DIEValue, public DIE {
695public:
696 unsigned Size; // Size in bytes excluding size header.
aslc200b112008-08-16 12:57:46 +0000697
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 DIEBlock()
699 : DIEValue(isBlock)
700 , DIE(0)
701 , Size(0)
702 {}
703 ~DIEBlock() {
704 }
aslc200b112008-08-16 12:57:46 +0000705
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 // Implement isa/cast/dyncast.
707 static bool classof(const DIEBlock *) { return true; }
708 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
aslc200b112008-08-16 12:57:46 +0000709
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 /// ComputeSize - calculate the size of the block.
711 ///
712 unsigned ComputeSize(DwarfDebug &DD);
aslc200b112008-08-16 12:57:46 +0000713
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714 /// BestForm - Choose the best form for data.
715 ///
716 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 }
722
723 /// EmitValue - Emit block data.
724 ///
725 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000726
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727 /// SizeOf - Determine size of block data in bytes.
728 ///
729 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000730
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731
732 /// Profile - Used to gather unique data for the value folding set.
733 ///
734 virtual void Profile(FoldingSetNodeID &ID) {
735 ID.AddInteger(isBlock);
736 DIE::Profile(ID);
737 }
aslc200b112008-08-16 12:57:46 +0000738
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739#ifndef NDEBUG
740 virtual void print(std::ostream &O) {
741 O << "Blk: ";
742 DIE::print(O, 5);
743 }
744#endif
745};
746
747//===----------------------------------------------------------------------===//
748/// CompileUnit - This dwarf writer support class manages information associate
749/// with a source file.
750class CompileUnit {
751private:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 /// ID - File identifier for source.
753 ///
754 unsigned ID;
aslc200b112008-08-16 12:57:46 +0000755
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756 /// Die - Compile unit debug information entry.
757 ///
758 DIE *Die;
aslc200b112008-08-16 12:57:46 +0000759
Devang Patel42f6bed2009-01-13 23:54:55 +0000760 /// GVToDieMap - Tracks the mapping of unit level debug informaton
761 /// variables to debug information entries.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000762 DenseMap<GlobalVariable *, DIE *> GVToDieMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763
Devang Patel42f6bed2009-01-13 23:54:55 +0000764 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000766 DenseMap<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
aslc200b112008-08-16 12:57:46 +0000775
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776 /// Dies - List of all dies in the compile unit.
777 ///
778 std::vector<DIE *> Dies;
aslc200b112008-08-16 12:57:46 +0000779
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000780public:
Devang Patelb3907da2009-01-05 23:03:32 +0000781 CompileUnit(unsigned I, DIE *D)
Devang Patel42f6bed2009-01-13 23:54:55 +0000782 : ID(I), Die(D), GVToDieMap(),
Devang Patelb3907da2009-01-05 23:03:32 +0000783 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize), Dies()
784 {}
785
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786 ~CompileUnit() {
787 delete Die;
aslc200b112008-08-16 12:57:46 +0000788
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000789 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
790 delete Dies[i];
791 }
aslc200b112008-08-16 12:57:46 +0000792
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000793 // Accessors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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();
802 }
803
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 }
aslc200b112008-08-16 12:57:46 +0000809
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000811 /// specified debug variable.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000812 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
813 return GVToDieMap[GV];
814 }
aslc200b112008-08-16 12:57:46 +0000815
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000817 /// specified debug variable.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000818 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
819 return GVToDIEntryMap[GV];
820 }
aslc200b112008-08-16 12:57:46 +0000821
Dan Gohmanf17a25c2007-07-18 16:29:46 +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);
aslc200b112008-08-16 12:57:46 +0000829
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830 if (!Die) {
831 Die = new DIE(Buffer);
832 DiesSet.InsertNode(Die, Where);
833 this->Die->AddChild(Die);
834 Buffer.Detach();
835 }
aslc200b112008-08-16 12:57:46 +0000836
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000837 return Die;
838 }
839};
840
841//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +0000842/// Dwarf - Emits general Dwarf directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843///
844class Dwarf {
845
846protected:
847
848 //===--------------------------------------------------------------------===//
849 // Core attributes used by the Dwarf writer.
850 //
aslc200b112008-08-16 12:57:46 +0000851
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 //
853 /// O - Stream to .s file.
854 ///
Owen Anderson847b99b2008-08-21 00:14:44 +0000855 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856
857 /// Asm - Target of Dwarf emission.
858 ///
859 AsmPrinter *Asm;
aslc200b112008-08-16 12:57:46 +0000860
Bill Wendlingac9639d2008-07-01 23:34:48 +0000861 /// TAI - Target asm information.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862 const TargetAsmInfo *TAI;
aslc200b112008-08-16 12:57:46 +0000863
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864 /// TD - Target data.
865 const TargetData *TD;
aslc200b112008-08-16 12:57:46 +0000866
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867 /// RI - Register Information.
Dan Gohman1e57df32008-02-10 18:45:23 +0000868 const TargetRegisterInfo *RI;
aslc200b112008-08-16 12:57:46 +0000869
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 /// M - Current module.
871 ///
872 Module *M;
aslc200b112008-08-16 12:57:46 +0000873
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000874 /// MF - Current machine function.
875 ///
876 MachineFunction *MF;
aslc200b112008-08-16 12:57:46 +0000877
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 /// MMI - Collected machine module information.
879 ///
880 MachineModuleInfo *MMI;
aslc200b112008-08-16 12:57:46 +0000881
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000882 /// SubprogramCount - The running count of functions being compiled.
883 ///
884 unsigned SubprogramCount;
aslc200b112008-08-16 12:57:46 +0000885
Chris Lattnerb3876c72007-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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889
890 unsigned SetCounter;
Owen Anderson847b99b2008-08-21 00:14:44 +0000891 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattnerb3876c72007-09-24 03:35:37 +0000892 const char *flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +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)
901 , SubprogramCount(0)
Chris Lattnerb3876c72007-09-24 03:35:37 +0000902 , Flavor(flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 , SetCounter(1)
904 {
905 }
906
907public:
908
909 //===--------------------------------------------------------------------===//
910 // Accessors.
911 //
912 AsmPrinter *getAsm() const { return Asm; }
913 MachineModuleInfo *getMMI() const { return MMI; }
914 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohmancfb72b22007-09-27 23:12:31 +0000915 const TargetData *getTargetData() const { return TD; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916
Anton Korobeynikov5ef86702007-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 Gohmancfb72b22007-09-27 23:12:31 +0000921 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000922 O << TAI->getData32bitsDirective();
923 else
924 O << TAI->getData64bitsDirective();
925 }
aslc200b112008-08-16 12:57:46 +0000926
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Korobeynikov5ef86702007-09-02 22:07:21 +0000932 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000933 O << TAI->getPrivateGlobalPrefix() << Tag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000934 if (Number) O << Number;
935 }
aslc200b112008-08-16 12:57:46 +0000936
Chris Lattnerb3876c72007-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 }
aslc200b112008-08-16 12:57:46 +0000943
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 }
aslc200b112008-08-16 12:57:46 +0000953
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000954 /// EmitReference - Emit a reference to a label.
955 ///
Dan Gohman4fd77742007-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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000959 }
960 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman4fd77742007-09-28 15:43:33 +0000961 bool IsPCRelative = false, bool Force32Bit = false) const {
962 PrintRelDirective(Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000963 PrintLabelName(Tag, Number);
aslc200b112008-08-16 12:57:46 +0000964
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000965 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
966 }
Dan Gohman4fd77742007-09-28 15:43:33 +0000967 void EmitReference(const std::string &Name, bool IsPCRelative = false,
968 bool Force32Bit = false) const {
969 PrintRelDirective(Force32Bit);
aslc200b112008-08-16 12:57:46 +0000970
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971 O << Name;
aslc200b112008-08-16 12:57:46 +0000972
Dan Gohmanf17a25c2007-07-18 16:29:46 +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,
980 bool IsSmall = false) {
981 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,
987 bool IsSmall = false) {
988 if (TAI->needsSet()) {
989 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +0000990 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 O << ",";
992 PrintLabelName(TagHi, NumberHi);
993 O << "-";
994 PrintLabelName(TagLo, NumberLo);
995 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000996
997 PrintRelDirective(IsSmall);
Chris Lattnerb3876c72007-09-24 03:35:37 +0000998 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999 ++SetCounter;
1000 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001001 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001002
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001003 PrintLabelName(TagHi, NumberHi);
1004 O << "-";
1005 PrintLabelName(TagLo, NumberLo);
1006 }
1007 }
1008
1009 void EmitSectionOffset(const char* Label, const char* Section,
1010 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesen0ebb2432008-03-26 23:31:39 +00001011 bool IsSmall = false, bool isEH = false,
1012 bool useSet = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013 bool printAbsolute = false;
Dale Johannesen0ebb2432008-03-26 23:31:39 +00001014 if (isEH)
1015 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
1016 else
1017 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
1018
1019 if (TAI->needsSet() && useSet) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +00001021 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001022 O << ",";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001023 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001024
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 if (!printAbsolute) {
1026 O << "-";
1027 PrintLabelName(Section, SectionNumber);
aslc200b112008-08-16 12:57:46 +00001028 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001030
1031 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001032
Chris Lattnerb3876c72007-09-24 03:35:37 +00001033 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001034 ++SetCounter;
1035 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001036 PrintRelDirective(IsSmall, true);
aslc200b112008-08-16 12:57:46 +00001037
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001038 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001040 if (!printAbsolute) {
1041 O << "-";
1042 PrintLabelName(Section, SectionNumber);
1043 }
aslc200b112008-08-16 12:57:46 +00001044 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001045 }
aslc200b112008-08-16 12:57:46 +00001046
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1048 /// frame.
1049 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenf5a11532007-11-13 19:13:01 +00001050 const std::vector<MachineMove> &Moves, bool isEH) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001051 int stackGrowth =
1052 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1053 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00001054 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
1056
1057 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1058 const MachineMove &Move = Moves[i];
1059 unsigned LabelID = Move.getLabelID();
aslc200b112008-08-16 12:57:46 +00001060
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061 if (LabelID) {
1062 LabelID = MMI->MappedLabel(LabelID);
aslc200b112008-08-16 12:57:46 +00001063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 // Throw out move if the label is invalid.
1065 if (!LabelID) continue;
1066 }
aslc200b112008-08-16 12:57:46 +00001067
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068 const MachineLocation &Dst = Move.getDestination();
1069 const MachineLocation &Src = Move.getSource();
aslc200b112008-08-16 12:57:46 +00001070
Dan Gohmanf17a25c2007-07-18 16:29:46 +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");
1075 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1076 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00001077
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078 BaseLabelID = LabelID;
1079 BaseLabel = "label";
1080 IsLocal = true;
1081 }
aslc200b112008-08-16 12:57:46 +00001082
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001083 // If advancing cfa.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001084 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1085 if (!Src.isReg()) {
1086 if (Src.getReg() == MachineLocation::VirtualFP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Gohmanb9f4fa72008-10-03 15:45:36 +00001092 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093 Asm->EOL("Register");
1094 }
aslc200b112008-08-16 12:57:46 +00001095
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001096 int Offset = -Src.getOffset();
aslc200b112008-08-16 12:57:46 +00001097
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001098 Asm->EmitULEB128Bytes(Offset);
1099 Asm->EOL("Offset");
1100 } else {
1101 assert(0 && "Machine move no supported yet.");
1102 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001103 } else if (Src.isReg() &&
1104 Src.getReg() == MachineLocation::VirtualFP) {
1105 if (Dst.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001106 Asm->EmitInt8(DW_CFA_def_cfa_register);
1107 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001108 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001109 Asm->EOL("Register");
1110 } else {
1111 assert(0 && "Machine move no supported yet.");
1112 }
1113 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001114 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001115 int Offset = Dst.getOffset() / stackGrowth;
aslc200b112008-08-16 12:57:46 +00001116
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Cheng6181e062008-07-09 21:53:02 +00001126 if (VerboseAsm)
1127 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1128 else
1129 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
1144};
1145
1146//===----------------------------------------------------------------------===//
Devang Patel35a078f2009-01-12 22:54:42 +00001147/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel7dd15a92009-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 Patel5f244e32009-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 Patel42f6bed2009-01-13 23:54:55 +00001181 bool operator==(const SrcFileInfo &SI) const {
Devang Patel5f244e32009-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 Patel4d1709e2009-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 Patel2560d922009-01-15 18:25:17 +00001215 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel4d1709e2009-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 Patel63c22f42009-01-10 02:42:49 +00001219 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
1220 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001221
1222public:
Devang Patel2560d922009-01-15 18:25:17 +00001223 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel4d1709e2009-01-08 02:33:41 +00001224 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1225 {}
Devang Patela4162952009-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 Patel4d1709e2009-01-08 02:33:41 +00001230
1231 // Accessors.
1232 DbgScope *getParent() const { return Parent; }
Devang Patel2560d922009-01-15 18:25:17 +00001233 DIDescriptor getDesc() const { return Desc; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001234 unsigned getStartLabelID() const { return StartLabelID; }
1235 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel63c22f42009-01-10 02:42:49 +00001236 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1237 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel4d1709e2009-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//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00001251/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001252///
1253class DwarfDebug : public Dwarf {
1254
1255private:
1256 //===--------------------------------------------------------------------===//
1257 // Attributes used to construct specific Dwarf sections.
1258 //
aslc200b112008-08-16 12:57:46 +00001259
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001260 /// CompileUnits - All the compile units involved in this build. The index
1261 /// of each entry in this vector corresponds to the sources in MMI.
1262 std::vector<CompileUnit *> CompileUnits;
Devang Patel7dd15a92009-01-08 17:19:22 +00001263 DenseMap<Value *, CompileUnit *> DW_CUs;
aslc200b112008-08-16 12:57:46 +00001264
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265 /// AbbreviationsSet - Used to uniquely define abbreviations.
1266 ///
1267 FoldingSet<DIEAbbrev> AbbreviationsSet;
1268
1269 /// Abbreviations - A list of all the unique abbreviations in use.
1270 ///
1271 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +00001272
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001273 /// ValuesSet - Used to uniquely define values.
1274 ///
Devang Patel5f244e32009-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 Patel7dd15a92009-01-08 17:19:22 +00001281 // Lines - List of of source line correspondence.
1282 std::vector<SrcLineInfo> Lines;
1283
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001284 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +00001285
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001286 /// Values - A list of all the unique values in use.
1287 ///
1288 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +00001289
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 /// StringPool - A UniqueVector of strings used by indirect references.
1291 ///
1292 UniqueVector<std::string> StringPool;
1293
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001294 /// SectionMap - Provides a unique id per text section.
1295 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001296 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +00001297
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 /// SectionSourceLines - Tracks line numbers per text section.
1299 ///
Devang Patel35a078f2009-01-12 22:54:42 +00001300 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001301
1302 /// didInitial - Flag to indicate if initial emission has been done.
1303 ///
1304 bool didInitial;
aslc200b112008-08-16 12:57:46 +00001305
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306 /// shouldEmit - Flag to indicate if debug information should be emitted.
1307 ///
1308 bool shouldEmit;
1309
Devang Patel2560d922009-01-15 18:25:17 +00001310 // RootDbgScope - Top level scope for the current function.
Devang Patel4d1709e2009-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001317 struct FunctionDebugFrameInfo {
1318 unsigned Number;
1319 std::vector<MachineMove> Moves;
1320
1321 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001322 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001323 };
1324
1325 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001326
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001327public:
aslc200b112008-08-16 12:57:46 +00001328
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001329 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1330 ///
1331 bool ShouldEmitDwarf() const { return shouldEmit; }
1332
1333 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001334 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001335 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1336 // Profile the node so that we can make it unique.
1337 FoldingSetNodeID ID;
1338 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001339
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001340 // Check the set for priors.
1341 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001342
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001343 // If it's newly added.
1344 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001345 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
1355 /// NewString - Add a string to the constant pool and returns a label.
1356 ///
1357 DWLabel NewString(const std::string &String) {
1358 unsigned StringID = StringPool.insert(String);
1359 return DWLabel("string", StringID);
1360 }
aslc200b112008-08-16 12:57:46 +00001361
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
aslc200b112008-08-16 12:57:46 +00001366
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001367 if (Entry) {
1368 FoldingSetNodeID ID;
1369 DIEntry::Profile(ID, Entry);
1370 void *Where;
1371 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001372
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001374
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001375 Value = new DIEntry(Entry);
1376 ValuesSet.InsertNode(Value, Where);
1377 } else {
1378 Value = new DIEntry(Entry);
1379 }
aslc200b112008-08-16 12:57:46 +00001380
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001381 Values.push_back(Value);
1382 return Value;
1383 }
aslc200b112008-08-16 12:57:46 +00001384
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
1400 DIEInteger::Profile(ID, Integer);
1401 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);
1407 }
aslc200b112008-08-16 12:57:46 +00001408
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001409 Die->AddValue(Attribute, Form, Value);
1410 }
aslc200b112008-08-16 12:57:46 +00001411
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
1418 DIEInteger::Profile(ID, (uint64_t)Integer);
1419 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);
1425 }
aslc200b112008-08-16 12:57:46 +00001426
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001427 Die->AddValue(Attribute, Form, Value);
1428 }
aslc200b112008-08-16 12:57:46 +00001429
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
1435 DIEString::Profile(ID, String);
1436 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);
1442 }
aslc200b112008-08-16 12:57:46 +00001443
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001444 Die->AddValue(Attribute, Form, Value);
1445 }
aslc200b112008-08-16 12:57:46 +00001446
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
1452 DIEDwarfLabel::Profile(ID, Label);
1453 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);
1459 }
aslc200b112008-08-16 12:57:46 +00001460
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001461 Die->AddValue(Attribute, Form, Value);
1462 }
aslc200b112008-08-16 12:57:46 +00001463
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
1469 DIEObjectLabel::Profile(ID, Label);
1470 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);
1476 }
aslc200b112008-08-16 12:57:46 +00001477
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001478 Die->AddValue(Attribute, Form, Value);
1479 }
aslc200b112008-08-16 12:57:46 +00001480
Argiris Kirtzidis03449652008-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 }
aslc200b112008-08-16 12:57:46 +00001495
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001496 Die->AddValue(Attribute, Form, Value);
1497 }
aslc200b112008-08-16 12:57:46 +00001498
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
1504 DIEDelta::Profile(ID, Hi, Lo);
1505 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);
1511 }
aslc200b112008-08-16 12:57:46 +00001512
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001513 Die->AddValue(Attribute, Form, Value);
1514 }
aslc200b112008-08-16 12:57:46 +00001515
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Lattner3de66892007-09-21 18:25:53 +00001535 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001536 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001537 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001538 }
aslc200b112008-08-16 12:57:46 +00001539
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001540 Die->AddValue(Attribute, Block->BestForm(), Value);
1541 }
1542
1543private:
1544
1545 /// AddSourceLine - Add location information to specified debug information
1546 /// entry.
Devang Patel4d1709e2009-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 Patel5f244e32009-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001600 /// AddAddress - Add an address attribute to a die based on the location
1601 /// provided.
1602 void AddAddress(DIE *Die, unsigned Attribute,
1603 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001604 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001605 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001606
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001607 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 }
aslc200b112008-08-16 12:57:46 +00001623
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001624 AddBlock(Die, Attribute, 0, Block);
1625 }
aslc200b112008-08-16 12:57:46 +00001626
Dan Gohmanf17a25c2007-07-18 16:29:46 +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) {
aslc200b112008-08-16 12:57:46 +00001632
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Patelf49e13d2009-01-05 17:44:11 +00001637 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1638 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001639 }
aslc200b112008-08-16 12:57:46 +00001640
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001641 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1642 ///
1643 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001644 DIE Buffer(DW_TAG_pointer_type);
Dan Gohmancfb72b22007-09-27 23:12:31 +00001645 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001646 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelbbca50b2009-01-05 17:45:59 +00001647 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1648 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001649 }
aslc200b112008-08-16 12:57:46 +00001650
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001651 /// AddType - Add a new type attribute to the specified entity.
Devang Patel4a4cbe72009-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);
1671 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1672 ConstructTypeDIE(DW_Unit, Buffer, BT);
1673 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1674 ConstructTypeDIE(DW_Unit, Buffer, DT);
1675 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1676 ConstructTypeDIE(DW_Unit, Buffer, CT);
1677
1678 // Add debug information entry to entity and unit.
1679 DIE *Die = DW_Unit->AddDie(Buffer);
1680 SetDIEntry(Slot, Die);
1681 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1682 }
1683
Devang Patel46d13752009-01-05 19:07:53 +00001684 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1685 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1686 DIBasicType *BTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001687
1688 // Get core information.
1689 const std::string &Name = BTy->getName();
1690 Buffer.setTag(DW_TAG_base_type);
1691 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy->getEncoding());
1692 // Add name if not anonymous or intermediate type.
1693 if (!Name.empty())
1694 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1695 uint64_t Size = BTy->getSizeInBits() >> 3;
1696 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1697 }
1698
Devang Patel46d13752009-01-05 19:07:53 +00001699 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1700 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1701 DIDerivedType *DTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001702
1703 // Get core information.
1704 const std::string &Name = DTy->getName();
1705 uint64_t Size = DTy->getSizeInBits() >> 3;
1706 unsigned Tag = DTy->getTag();
1707 // FIXME - Workaround for templates.
1708 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1709
1710 Buffer.setTag(Tag);
1711 // Map to main type, void will not have a type.
1712 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001713 AddType(DW_Unit, &Buffer, FromTy);
Devang Patelfc187162009-01-05 17:57:47 +00001714
1715 // Add name if not anonymous or intermediate type.
1716 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1717
1718 // Add size if non-zero (derived types might be zero-sized.)
1719 if (Size)
1720 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1721
1722 // Add source line info if available and TyDesc is not a forward
1723 // declaration.
1724 // FIXME - Enable this. if (!DTy->isForwardDecl())
1725 // FIXME - Enable this. AddSourceLine(&Buffer, *DTy);
1726 }
1727
Devang Patel30c01372009-01-05 19:55:51 +00001728 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1729 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1730 DICompositeType *CTy) {
1731
1732 // Get core information.
1733 const std::string &Name = CTy->getName();
1734 uint64_t Size = CTy->getSizeInBits() >> 3;
1735 unsigned Tag = CTy->getTag();
1736 switch (Tag) {
1737 case DW_TAG_vector_type:
1738 case DW_TAG_array_type:
1739 ConstructArrayTypeDIE(DW_Unit, Buffer, CTy);
1740 break;
1741 //FIXME - Enable this.
1742 // case DW_TAG_enumeration_type:
1743 // DIArray Elements = CTy->getTypeArray();
1744 // // Add enumerators to enumeration type.
1745 // for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i)
1746 // ConstructEnumTypeDIE(Buffer, &Elements.getElement(i));
1747 // break;
1748 case DW_TAG_subroutine_type:
1749 {
1750 // Add prototype flag.
1751 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1752 DIArray Elements = CTy->getTypeArray();
1753 // Add return type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001754 DIDescriptor RTy = Elements.getElement(0);
1755 if (DIBasicType *BT = dyn_cast<DIBasicType>(&RTy))
1756 AddType(DW_Unit, &Buffer, *BT);
1757 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&RTy))
1758 AddType(DW_Unit, &Buffer, *DT);
1759 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&RTy))
1760 AddType(DW_Unit, &Buffer, *CT);
1761
1762 //AddType(DW_Unit, &Buffer, Elements.getElement(0));
Devang Patel30c01372009-01-05 19:55:51 +00001763 // Add arguments.
1764 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1765 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001766 DIDescriptor Ty = Elements.getElement(i);
1767 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1768 AddType(DW_Unit, &Buffer, *BT);
1769 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1770 AddType(DW_Unit, &Buffer, *DT);
1771 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1772 AddType(DW_Unit, &Buffer, *CT);
Devang Patel30c01372009-01-05 19:55:51 +00001773 Buffer.AddChild(Arg);
1774 }
1775 }
1776 break;
1777 case DW_TAG_structure_type:
1778 case DW_TAG_union_type:
1779 {
1780 // Add elements to structure type.
1781 DIArray Elements = CTy->getTypeArray();
1782 // Add elements to structure type.
1783 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1784 DIDescriptor Element = Elements.getElement(i);
1785 if (DISubprogram *SP = dyn_cast<DISubprogram>(&Element))
1786 ConstructFieldTypeDIE(DW_Unit, Buffer, SP);
1787 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Element))
1788 ConstructFieldTypeDIE(DW_Unit, Buffer, DT);
1789 else if (DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(&Element))
1790 ConstructFieldTypeDIE(DW_Unit, Buffer, GV);
1791 }
1792 }
1793 break;
1794 default:
1795 break;
1796 }
1797
1798 // Add name if not anonymous or intermediate type.
1799 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1800
1801 // Add size if non-zero (derived types might be zero-sized.)
1802 if (Size)
1803 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1804 else {
1805 // Add zero size even if it is not a forward declaration.
1806 // FIXME - Enable this.
1807 // if (!CTy->isDefinition())
1808 // AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1809 // else
1810 // AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1811 }
1812
1813 // Add source line info if available and TyDesc is not a forward
1814 // declaration.
1815 // FIXME - Enable this.
1816 // if (CTy->isForwardDecl())
1817 // AddSourceLine(&Buffer, *CTy);
1818 }
1819
Devang Patel6fb54132009-01-05 18:33:01 +00001820 // ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1821 void ConstructSubrangeDIE (DIE &Buffer, DISubrange *SR, DIE *IndexTy) {
1822 int64_t L = SR->getLo();
1823 int64_t H = SR->getHi();
1824 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1825 if (L != H) {
1826 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1827 if (L)
1828 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1829 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
1830 }
1831 Buffer.AddChild(DW_Subrange);
1832 }
1833
1834 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1835 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1836 DICompositeType *CTy) {
1837 Buffer.setTag(DW_TAG_array_type);
1838 if (CTy->getTag() == DW_TAG_vector_type)
1839 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1840
1841 DIArray Elements = CTy->getTypeArray();
1842 // FIXME - Enable this.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001843 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel6fb54132009-01-05 18:33:01 +00001844
1845 // Construct an anonymous type for index type.
1846 DIE IdxBuffer(DW_TAG_base_type);
1847 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1848 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1849 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1850
1851 // Add subranges to array type.
1852 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel30c01372009-01-05 19:55:51 +00001853 DIDescriptor Element = Elements.getElement(i);
1854 if (DISubrange *SR = dyn_cast<DISubrange>(&Element))
1855 ConstructSubrangeDIE(Buffer, SR, IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +00001856 }
1857 }
1858
Devang Patela566e812009-01-05 18:38:38 +00001859 /// ConstructEnumTypeDIE - Construct enum type DIE from
1860 /// DIEnumerator.
Devang Patel30c01372009-01-05 19:55:51 +00001861 void ConstructEnumTypeDIE(CompileUnit *DW_Unit,
1862 DIE &Buffer, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +00001863
1864 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1865 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1866 int64_t Value = ETy->getEnumValue();
1867 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1868 Buffer.AddChild(Enumerator);
1869 }
Devang Patel6fb54132009-01-05 18:33:01 +00001870
Devang Patel526b01d2009-01-05 18:59:44 +00001871 /// ConstructFieldTypeDIE - Construct variable DIE for a struct field.
1872 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1873 DIE &Buffer, DIGlobalVariable *V) {
1874
1875 DIE *VariableDie = new DIE(DW_TAG_variable);
1876 const std::string &LinkageName = V->getLinkageName();
1877 if (!LinkageName.empty())
1878 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1879 LinkageName);
1880 // FIXME - Enable this. AddSourceLine(VariableDie, V);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001881 AddType(DW_Unit, VariableDie, V->getType());
Devang Patel526b01d2009-01-05 18:59:44 +00001882 if (!V->isLocalToUnit())
1883 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1884 AddUInt(VariableDie, DW_AT_declaration, DW_FORM_flag, 1);
1885 Buffer.AddChild(VariableDie);
1886 }
1887
1888 /// ConstructFieldTypeDIE - Construct subprogram DIE for a struct field.
1889 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1890 DIE &Buffer, DISubprogram *SP,
1891 bool IsConstructor = false) {
1892 DIE *Method = new DIE(DW_TAG_subprogram);
1893 AddString(Method, DW_AT_name, DW_FORM_string, SP->getName());
1894 const std::string &LinkageName = SP->getLinkageName();
1895 if (!LinkageName.empty())
1896 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1897 // FIXME - Enable this. AddSourceLine(Method, SP);
1898
1899 DICompositeType MTy = SP->getType();
1900 DIArray Args = MTy.getTypeArray();
1901
1902 // Add Return Type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001903 if (!IsConstructor) {
1904 DIDescriptor Ty = Args.getElement(0);
1905 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1906 AddType(DW_Unit, Method, *BT);
1907 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1908 AddType(DW_Unit, Method, *DT);
1909 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1910 AddType(DW_Unit, Method, *CT);
1911 }
Devang Patel526b01d2009-01-05 18:59:44 +00001912
1913 // Add arguments.
1914 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1915 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001916 DIDescriptor Ty = Args.getElement(i);
1917 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1918 AddType(DW_Unit, Method, *BT);
1919 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1920 AddType(DW_Unit, Method, *DT);
1921 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1922 AddType(DW_Unit, Method, *CT);
Devang Patel526b01d2009-01-05 18:59:44 +00001923 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1924 Method->AddChild(Arg);
1925 }
1926
1927 if (!SP->isLocalToUnit())
1928 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
1929 Buffer.AddChild(Method);
1930 }
1931
1932 /// COnstructFieldTypeDIE - Construct derived type DIE for a struct field.
1933 void ConstructFieldTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1934 DIDerivedType *DTy) {
1935 unsigned Tag = DTy->getTag();
1936 DIE *MemberDie = new DIE(Tag);
1937 if (!DTy->getName().empty())
1938 AddString(MemberDie, DW_AT_name, DW_FORM_string, DTy->getName());
1939 // FIXME - Enable this. AddSourceLine(MemberDie, DTy);
1940
1941 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001942 AddType(DW_Unit, MemberDie, FromTy);
Devang Patel526b01d2009-01-05 18:59:44 +00001943
1944 uint64_t Size = DTy->getSizeInBits();
1945 uint64_t Offset = DTy->getOffsetInBits();
1946
1947 // FIXME Handle bitfields
1948
1949 // Add size.
1950 AddUInt(MemberDie, DW_AT_bit_size, 0, Size);
1951 // Add computation for offset.
1952 DIEBlock *Block = new DIEBlock();
1953 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1954 AddUInt(Block, 0, DW_FORM_udata, Offset >> 3);
1955 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1956
1957 // FIXME Handle DW_AT_accessibility.
1958
1959 Buffer.AddChild(MemberDie);
1960 }
1961
Devang Patel5f244e32009-01-05 22:35:52 +00001962 /// FindCompileUnit - Get the compile unit for the given descriptor.
1963 ///
1964 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
1965 CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
1966 assert(DW_Unit && "Missing compile unit.");
1967 return DW_Unit;
1968 }
1969
Devang Patel42f6bed2009-01-13 23:54:55 +00001970 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel4d1709e2009-01-08 02:33:41 +00001971 ///
1972 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1973 // Get the descriptor.
1974 DIVariable *VD = DV->getVariable();
1975
1976 // Translate tag to proper Dwarf tag. The result variable is dropped for
1977 // now.
1978 unsigned Tag;
1979 switch (VD->getTag()) {
1980 case DW_TAG_return_variable: return NULL;
1981 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1982 case DW_TAG_auto_variable: // fall thru
1983 default: Tag = DW_TAG_variable; break;
1984 }
1985
1986 // Define variable debug information entry.
1987 DIE *VariableDie = new DIE(Tag);
1988 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1989
1990 // Add source line info if available.
1991 AddSourceLine(VariableDie, VD);
1992
1993 // Add variable type.
1994 AddType(Unit, VariableDie, VD->getType());
1995
1996 // Add variable address.
1997 MachineLocation Location;
1998 Location.set(RI->getFrameRegister(*MF),
1999 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2000 AddAddress(VariableDie, DW_AT_location, Location);
2001
2002 return VariableDie;
2003 }
2004
Devang Patel4d1709e2009-01-08 02:33:41 +00002005 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2006 ///
2007 DbgScope *getOrCreateScope(GlobalVariable *V) {
2008 DbgScope *&Slot = DbgScopeMap[V];
2009 if (!Slot) {
2010 // FIXME - breaks down when the context is an inlined function.
2011 DIDescriptor ParentDesc;
Devang Patel2560d922009-01-15 18:25:17 +00002012 DIDescriptor Desc(V);
2013 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
2014 DIBlock Block(V);
2015 ParentDesc = Block.getContext();
Devang Patel4d1709e2009-01-08 02:33:41 +00002016 }
2017 DbgScope *Parent = ParentDesc.isNull() ?
Devang Pateldd49fbb2009-01-10 02:34:18 +00002018 NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel2560d922009-01-15 18:25:17 +00002019 Slot = new DbgScope(Parent, Desc);
Devang Patel4d1709e2009-01-08 02:33:41 +00002020 if (Parent) {
2021 Parent->AddScope(Slot);
2022 } else if (RootDbgScope) {
2023 // FIXME - Add inlined function scopes to the root so we can delete
2024 // them later. Long term, handle inlined functions properly.
2025 RootDbgScope->AddScope(Slot);
2026 } else {
2027 // First function is top level function.
2028 RootDbgScope = Slot;
2029 }
2030 }
2031 return Slot;
2032 }
2033
2034 /// ConstructDbgScope - Construct the components of a scope.
2035 ///
2036 void ConstructDbgScope(DbgScope *ParentScope,
2037 unsigned ParentStartID, unsigned ParentEndID,
2038 DIE *ParentDie, CompileUnit *Unit) {
2039 // Add variables to scope.
Devang Patel63c22f42009-01-10 02:42:49 +00002040 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel4d1709e2009-01-08 02:33:41 +00002041 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2042 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2043 if (VariableDie) ParentDie->AddChild(VariableDie);
2044 }
2045
2046 // Add nested scopes.
Devang Patel63c22f42009-01-10 02:42:49 +00002047 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel4d1709e2009-01-08 02:33:41 +00002048 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2049 // Define the Scope debug information entry.
2050 DbgScope *Scope = Scopes[j];
2051 // FIXME - Ignore inlined functions for the time being.
2052 if (!Scope->getParent()) continue;
2053
Devang Patelb9224922009-01-12 18:41:00 +00002054 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2055 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel4d1709e2009-01-08 02:33:41 +00002056
2057 // Ignore empty scopes.
2058 if (StartID == EndID && StartID != 0) continue;
2059 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2060
2061 if (StartID == ParentStartID && EndID == ParentEndID) {
2062 // Just add stuff to the parent scope.
2063 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2064 } else {
2065 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2066
2067 // Add the scope bounds.
2068 if (StartID) {
2069 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2070 DWLabel("label", StartID));
2071 } else {
2072 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2073 DWLabel("func_begin", SubprogramCount));
2074 }
2075 if (EndID) {
2076 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2077 DWLabel("label", EndID));
2078 } else {
2079 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2080 DWLabel("func_end", SubprogramCount));
2081 }
2082
2083 // Add the scope contents.
2084 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2085 ParentDie->AddChild(ScopeDie);
2086 }
2087 }
2088 }
2089
2090 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2091 ///
2092 void ConstructRootDbgScope(DbgScope *RootScope) {
2093 // Exit if there is no root scope.
2094 if (!RootScope) return;
Devang Patel2560d922009-01-15 18:25:17 +00002095 DIDescriptor Desc = RootScope->getDesc();
2096 if (Desc.isNull())
2097 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002098
2099 // Get the subprogram debug information entry.
Devang Patel2560d922009-01-15 18:25:17 +00002100 DISubprogram SPD(Desc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002101
2102 // Get the compile unit context.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002103 CompileUnit *Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00002104
2105 // Get the subprogram die.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002106 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002107 assert(SPDie && "Missing subprogram descriptor");
2108
2109 // Add the function bounds.
2110 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2111 DWLabel("func_begin", SubprogramCount));
2112 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2113 DWLabel("func_end", SubprogramCount));
2114 MachineLocation Location(RI->getFrameRegister(*MF));
2115 AddAddress(SPDie, DW_AT_frame_base, Location);
2116
2117 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2118 }
2119
2120 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2121 ///
2122 void ConstructDefaultDbgScope(MachineFunction *MF) {
2123 // Find the correct subprogram descriptor.
2124 std::string SPName = "llvm.dbg.subprograms";
2125 std::vector<GlobalVariable*> Result;
2126 getGlobalVariablesUsing(*M, SPName, Result);
2127 for (std::vector<GlobalVariable *>::iterator I = Result.begin(),
2128 E = Result.end(); I != E; ++I) {
2129
2130 DISubprogram *SPD = new DISubprogram(*I);
2131
2132 if (SPD->getName() == MF->getFunction()->getName()) {
2133 // Get the compile unit context.
2134 CompileUnit *Unit = FindCompileUnit(SPD->getCompileUnit());
2135
2136 // Get the subprogram die.
2137 DIE *SPDie = Unit->getDieMapSlotFor(SPD->getGV());
2138 assert(SPDie && "Missing subprogram descriptor");
2139
2140 // Add the function bounds.
2141 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2142 DWLabel("func_begin", SubprogramCount));
2143 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2144 DWLabel("func_end", SubprogramCount));
2145
2146 MachineLocation Location(RI->getFrameRegister(*MF));
2147 AddAddress(SPDie, DW_AT_frame_base, Location);
2148 return;
2149 }
2150 }
2151#if 0
2152 // FIXME: This is causing an abort because C++ mangled names are compared
2153 // with their unmangled counterparts. See PR2885. Don't do this assert.
2154 assert(0 && "Couldn't find DIE for machine function!");
2155#endif
2156 }
2157
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002158 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2159 /// tools to recognize the object file contains Dwarf information.
2160 void EmitInitial() {
2161 // Check to see if we already emitted intial headers.
2162 if (didInitial) return;
2163 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002164
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002165 // Dwarf sections base addresses.
2166 if (TAI->doesDwarfRequireFrameSection()) {
2167 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2168 EmitLabel("section_debug_frame", 0);
2169 }
2170 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2171 EmitLabel("section_info", 0);
2172 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2173 EmitLabel("section_abbrev", 0);
2174 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2175 EmitLabel("section_aranges", 0);
2176 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2177 EmitLabel("section_macinfo", 0);
2178 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2179 EmitLabel("section_line", 0);
2180 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2181 EmitLabel("section_loc", 0);
2182 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2183 EmitLabel("section_pubnames", 0);
2184 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2185 EmitLabel("section_str", 0);
2186 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2187 EmitLabel("section_ranges", 0);
2188
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002189 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002190 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002191 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002192 EmitLabel("data_begin", 0);
2193 }
2194
2195 /// EmitDIE - Recusively Emits a debug information entry.
2196 ///
2197 void EmitDIE(DIE *Die) {
2198 // Get the abbreviation for this DIE.
2199 unsigned AbbrevNumber = Die->getAbbrevNumber();
2200 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002201
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002202 Asm->EOL();
2203
2204 // Emit the code (index) for the abbreviation.
2205 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002206
2207 if (VerboseAsm)
2208 Asm->EOL(std::string("Abbrev [" +
2209 utostr(AbbrevNumber) +
2210 "] 0x" + utohexstr(Die->getOffset()) +
2211 ":0x" + utohexstr(Die->getSize()) + " " +
2212 TagString(Abbrev->getTag())));
2213 else
2214 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002215
Owen Anderson88dd6232008-06-24 21:44:59 +00002216 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2217 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002218
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002219 // Emit the DIE attribute values.
2220 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2221 unsigned Attr = AbbrevData[i].getAttribute();
2222 unsigned Form = AbbrevData[i].getForm();
2223 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002224
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002225 switch (Attr) {
2226 case DW_AT_sibling: {
2227 Asm->EmitInt32(Die->SiblingOffset());
2228 break;
2229 }
2230 default: {
2231 // Emit an attribute using the defined form.
2232 Values[i]->EmitValue(*this, Form);
2233 break;
2234 }
2235 }
aslc200b112008-08-16 12:57:46 +00002236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002237 Asm->EOL(AttributeString(Attr));
2238 }
aslc200b112008-08-16 12:57:46 +00002239
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002240 // Emit the DIE children if any.
2241 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2242 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002244 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2245 EmitDIE(Children[j]);
2246 }
aslc200b112008-08-16 12:57:46 +00002247
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002248 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2249 }
2250 }
2251
2252 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2253 ///
2254 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2255 // Get the children.
2256 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002257
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002258 // If not last sibling and has children then add sibling offset attribute.
2259 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2260
2261 // Record the abbreviation.
2262 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002263
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002264 // Get the abbreviation for this DIE.
2265 unsigned AbbrevNumber = Die->getAbbrevNumber();
2266 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2267
2268 // Set DIE offset
2269 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002270
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002271 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002272 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2273
Owen Anderson88dd6232008-06-24 21:44:59 +00002274 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2275 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002276
2277 // Size the DIE attribute values.
2278 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2279 // Size attribute value.
2280 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2281 }
aslc200b112008-08-16 12:57:46 +00002282
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002283 // Size the DIE children if any.
2284 if (!Children.empty()) {
2285 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2286 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002287
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002288 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2289 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2290 }
aslc200b112008-08-16 12:57:46 +00002291
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002292 // End of children marker.
2293 Offset += sizeof(int8_t);
2294 }
2295
2296 Die->setSize(Offset - Die->getOffset());
2297 return Offset;
2298 }
2299
2300 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2301 ///
2302 void SizeAndOffsets() {
2303 // Process base compile unit.
Devang Patel6eae2832009-01-12 23:05:55 +00002304 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2305 CE = DW_CUs.end(); CI != CE; ++CI) {
2306 CompileUnit *Unit = CI->second;
2307 // Compute size of compile unit header
2308 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2309 sizeof(int16_t) + // DWARF version number
2310 sizeof(int32_t) + // Offset Into Abbrev. Section
2311 sizeof(int8_t); // Pointer Size (in bytes)
2312 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2313 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002314 }
2315
2316 /// EmitDebugInfo - Emit the debug info section.
2317 ///
2318 void EmitDebugInfo() {
2319 // Start debug info section.
2320 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002321
Devang Patel6eae2832009-01-12 23:05:55 +00002322 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2323 CE = DW_CUs.end(); CI != CE; ++CI) {
2324 CompileUnit *Unit = CI->second;
2325 DIE *Die = Unit->getDie();
2326 // Emit the compile units header.
2327 EmitLabel("info_begin", Unit->getID());
2328 // Emit size of content not including length itself
2329 unsigned ContentSize = Die->getSize() +
2330 sizeof(int16_t) + // DWARF version number
2331 sizeof(int32_t) + // Offset Into Abbrev. Section
2332 sizeof(int8_t) + // Pointer Size (in bytes)
2333 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2334
2335 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2336 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2337 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2338 Asm->EOL("Offset Into Abbrev. Section");
2339 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2340
2341 EmitDIE(Die);
2342 // FIXME - extra padding for gdb bug.
2343 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2344 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2345 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2346 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2347 EmitLabel("info_end", Unit->getID());
2348
2349 Asm->EOL();
2350 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002351 }
2352
2353 /// EmitAbbreviations - Emit the abbreviation section.
2354 ///
2355 void EmitAbbreviations() const {
2356 // Check to see if it is worth the effort.
2357 if (!Abbreviations.empty()) {
2358 // Start the debug abbrev section.
2359 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00002360
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002361 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00002362
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002363 // For each abbrevation.
2364 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2365 // Get abbreviation data
2366 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00002367
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002368 // Emit the abbrevations code (base 1 index.)
2369 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2370 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00002371
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002372 // Emit the abbreviations data.
2373 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00002374
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002375 Asm->EOL();
2376 }
aslc200b112008-08-16 12:57:46 +00002377
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002378 // Mark end of abbreviations.
2379 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2380
2381 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00002382
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002383 Asm->EOL();
2384 }
2385 }
2386
Bill Wendling1983a2a2008-07-20 00:11:19 +00002387 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2388 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00002389 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00002390 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2391 // Define last address of section.
2392 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2393 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2394 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2395 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2396
2397 // Mark end of matrix.
2398 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2399 Asm->EmitULEB128Bytes(1); Asm->EOL();
2400 Asm->EmitInt8(1); Asm->EOL();
2401 }
2402
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002403 /// EmitDebugLines - Emit source line information.
2404 ///
2405 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00002406 // If the target is using .loc/.file, the assembler will be emitting the
2407 // .debug_line table automatically.
2408 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00002409 return;
2410
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002411 // Minimum line delta, thus ranging from -10..(255-10).
2412 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2413 // Maximum line delta, thus ranging from -10..(255-10).
2414 const int MaxLineDelta = 255 + MinLineDelta;
2415
2416 // Start the dwarf line section.
2417 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00002418
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002419 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00002420
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002421 EmitDifference("line_end", 0, "line_begin", 0, true);
2422 Asm->EOL("Length of Source Line Info");
2423 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00002424
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002425 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00002426
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002427 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2428 Asm->EOL("Prolog Length");
2429 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00002430
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002431 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2432
2433 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2434
2435 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00002436
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002437 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2438
2439 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00002440
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002441 // Line number standard opcode encodings argument count
2442 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2443 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2444 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2445 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2446 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2447 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2448 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2449 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2450 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2451
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002452 // Emit directories.
2453 for (unsigned DirectoryID = 1, NDID = Directories.size();
2454 DirectoryID <= NDID; ++DirectoryID) {
2455 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
2456 }
2457 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00002458
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002459 // Emit files.
Devang Patel6ccd57e2009-01-13 00:20:51 +00002460 for (unsigned SourceID = 1, NSID = SrcFiles.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002461 SourceID <= NSID; ++SourceID) {
Devang Patel6ccd57e2009-01-13 00:20:51 +00002462 const SrcFileInfo &SourceFile = SrcFiles[SourceID];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002463 Asm->EmitString(SourceFile.getName());
2464 Asm->EOL("Source");
2465 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2466 Asm->EOL("Directory #");
2467 Asm->EmitULEB128Bytes(0);
2468 Asm->EOL("Mod date");
2469 Asm->EmitULEB128Bytes(0);
2470 Asm->EOL("File size");
2471 }
2472 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00002473
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002474 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00002475
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002476 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00002477 unsigned SecSrcLinesSize = SectionSourceLines.size();
2478
2479 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002480 // Isolate current sections line info.
Devang Patel35a078f2009-01-12 22:54:42 +00002481 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00002482
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002483 if (VerboseAsm) {
2484 const Section* S = SectionMap[j + 1];
2485 Asm->EOL(std::string("Section ") + S->getName());
2486 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00002487 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002488
2489 // Dwarf assumes we start with first line of first source file.
2490 unsigned Source = 1;
2491 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00002492
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002493 // Construct rows of the address, source, line, column matrix.
2494 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patel35a078f2009-01-12 22:54:42 +00002495 const SrcLineInfo &LineInfo = LineInfos[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002496 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2497 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00002498
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002499 unsigned SourceID = LineInfo.getSourceID();
Devang Patel6ccd57e2009-01-13 00:20:51 +00002500 const SrcFileInfo &SourceFile = SrcFiles[SourceID];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002501 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng0eeed442008-07-01 23:18:29 +00002502 if (VerboseAsm)
2503 Asm->EOL(Directories[DirectoryID]
2504 + SourceFile.getName()
2505 + ":"
2506 + utostr_32(LineInfo.getLine()));
2507 else
2508 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002509
2510 // Define the line address.
2511 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002512 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002513 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2514 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00002515
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002516 // If change of source, then switch to the new source.
2517 if (Source != LineInfo.getSourceID()) {
2518 Source = LineInfo.getSourceID();
2519 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2520 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2521 }
aslc200b112008-08-16 12:57:46 +00002522
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002523 // If change of line.
2524 if (Line != LineInfo.getLine()) {
2525 // Determine offset.
2526 int Offset = LineInfo.getLine() - Line;
2527 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00002528
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002529 // Update line.
2530 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00002531
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002532 // If delta is small enough and in range...
2533 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2534 // ... then use fast opcode.
2535 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2536 } else {
2537 // ... otherwise use long hand.
2538 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2539 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2540 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2541 }
2542 } else {
2543 // Copy the previous row (different address or source)
2544 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2545 }
2546 }
2547
Bill Wendling1983a2a2008-07-20 00:11:19 +00002548 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002549 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00002550
2551 if (SecSrcLinesSize == 0)
2552 // Because we're emitting a debug_line section, we still need a line
2553 // table. The linker and friends expect it to exist. If there's nothing to
2554 // put into it, emit an empty table.
2555 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00002556
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002557 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00002558
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002559 Asm->EOL();
2560 }
aslc200b112008-08-16 12:57:46 +00002561
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002562 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2563 ///
2564 void EmitCommonDebugFrame() {
2565 if (!TAI->doesDwarfRequireFrameSection())
2566 return;
2567
2568 int stackGrowth =
2569 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2570 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002571 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002572
2573 // Start the dwarf frame section.
2574 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2575
2576 EmitLabel("debug_frame_common", 0);
2577 EmitDifference("debug_frame_common_end", 0,
2578 "debug_frame_common_begin", 0, true);
2579 Asm->EOL("Length of Common Information Entry");
2580
2581 EmitLabel("debug_frame_common_begin", 0);
2582 Asm->EmitInt32((int)DW_CIE_ID);
2583 Asm->EOL("CIE Identifier Tag");
2584 Asm->EmitInt8(DW_CIE_VERSION);
2585 Asm->EOL("CIE Version");
2586 Asm->EmitString("");
2587 Asm->EOL("CIE Augmentation");
2588 Asm->EmitULEB128Bytes(1);
2589 Asm->EOL("CIE Code Alignment Factor");
2590 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00002591 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002592 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002593 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00002594
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002595 std::vector<MachineMove> Moves;
2596 RI->getInitialFrameState(Moves);
2597
Dale Johannesenf5a11532007-11-13 19:13:01 +00002598 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002599
Evan Cheng7e7d1942008-02-29 19:36:59 +00002600 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002601 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00002602
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002603 Asm->EOL();
2604 }
2605
2606 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2607 /// section.
2608 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2609 if (!TAI->doesDwarfRequireFrameSection())
2610 return;
aslc200b112008-08-16 12:57:46 +00002611
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002612 // Start the dwarf frame section.
2613 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00002614
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002615 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2616 "debug_frame_begin", DebugFrameInfo.Number, true);
2617 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00002618
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002619 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2620
2621 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2622 0, 0, true, false);
2623 Asm->EOL("FDE CIE offset");
2624
2625 EmitReference("func_begin", DebugFrameInfo.Number);
2626 Asm->EOL("FDE initial location");
2627 EmitDifference("func_end", DebugFrameInfo.Number,
2628 "func_begin", DebugFrameInfo.Number);
2629 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00002630
Dale Johannesenf5a11532007-11-13 19:13:01 +00002631 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
aslc200b112008-08-16 12:57:46 +00002632
Evan Cheng7e7d1942008-02-29 19:36:59 +00002633 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002634 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2635
2636 Asm->EOL();
2637 }
2638
2639 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2640 ///
2641 void EmitDebugPubNames() {
2642 // Start the dwarf pubnames section.
2643 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00002644
Devang Patel6eae2832009-01-12 23:05:55 +00002645 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2646 CE = DW_CUs.end(); CI != CE; ++CI) {
2647 CompileUnit *Unit = CI->second;
aslc200b112008-08-16 12:57:46 +00002648
Devang Patel6eae2832009-01-12 23:05:55 +00002649 EmitDifference("pubnames_end", Unit->getID(),
2650 "pubnames_begin", Unit->getID(), true);
2651 Asm->EOL("Length of Public Names Info");
2652
2653 EmitLabel("pubnames_begin", Unit->getID());
2654
2655 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2656
2657 EmitSectionOffset("info_begin", "section_info",
2658 Unit->getID(), 0, true, false);
2659 Asm->EOL("Offset of Compilation Unit Info");
2660
2661 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
2662 Asm->EOL("Compilation Unit Length");
2663
2664 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2665
2666 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2667 GE = Globals.end();
2668 GI != GE; ++GI) {
2669 const std::string &Name = GI->first;
2670 DIE * Entity = GI->second;
2671
2672 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2673 Asm->EmitString(Name); Asm->EOL("External Name");
2674 }
2675
2676 Asm->EmitInt32(0); Asm->EOL("End Mark");
2677 EmitLabel("pubnames_end", Unit->getID());
2678
2679 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002680 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002681 }
2682
2683 /// EmitDebugStr - Emit visible names into a debug str section.
2684 ///
2685 void EmitDebugStr() {
2686 // Check to see if it is worth the effort.
2687 if (!StringPool.empty()) {
2688 // Start the dwarf str section.
2689 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00002690
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002691 // For each of strings in the string pool.
2692 for (unsigned StringID = 1, N = StringPool.size();
2693 StringID <= N; ++StringID) {
2694 // Emit a label for reference from debug information entries.
2695 EmitLabel("string", StringID);
2696 // Emit the string itself.
2697 const std::string &String = StringPool[StringID];
2698 Asm->EmitString(String); Asm->EOL();
2699 }
aslc200b112008-08-16 12:57:46 +00002700
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002701 Asm->EOL();
2702 }
2703 }
2704
2705 /// EmitDebugLoc - Emit visible names into a debug loc section.
2706 ///
2707 void EmitDebugLoc() {
2708 // Start the dwarf loc section.
2709 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00002710
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002711 Asm->EOL();
2712 }
2713
2714 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2715 ///
2716 void EmitDebugARanges() {
2717 // Start the dwarf aranges section.
2718 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00002719
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002720 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002721#if 0
aslc200b112008-08-16 12:57:46 +00002722 CompileUnit *Unit = GetBaseCompileUnit();
2723
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002724 // Don't include size of length
2725 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00002726
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002727 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00002728
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002729 EmitReference("info_begin", Unit->getID());
2730 Asm->EOL("Offset of Compilation Unit Info");
2731
Dan Gohmancfb72b22007-09-27 23:12:31 +00002732 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002733
2734 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2735
2736 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2737 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2738
2739 // Range 1
2740 EmitReference("text_begin", 0); Asm->EOL("Address");
2741 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2742
2743 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2744 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002745#endif
aslc200b112008-08-16 12:57:46 +00002746
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002747 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002748 }
2749
2750 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2751 ///
2752 void EmitDebugRanges() {
2753 // Start the dwarf ranges section.
2754 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00002755
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002756 Asm->EOL();
2757 }
2758
2759 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2760 ///
2761 void EmitDebugMacInfo() {
2762 // Start the dwarf macinfo section.
2763 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00002764
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002765 Asm->EOL();
2766 }
2767
Devang Patel289f2362009-01-05 23:11:11 +00002768 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00002769 void ConstructCompileUnits() {
2770 std::string CUName = "llvm.dbg.compile_units";
2771 std::vector<GlobalVariable*> Result;
2772 getGlobalVariablesUsing(*M, CUName, Result);
2773 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
2774 RE = Result.end(); RI != RE; ++RI) {
2775 DICompileUnit *DIUnit = new DICompileUnit(*RI);
Devang Patel7dd15a92009-01-08 17:19:22 +00002776 unsigned ID = RecordSource(DIUnit->getDirectory(),
2777 DIUnit->getFilename());
Devang Patelb3907da2009-01-05 23:03:32 +00002778
2779 DIE *Die = new DIE(DW_TAG_compile_unit);
2780 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2781 DWLabel("section_line", 0), DWLabel("section_line", 0),
2782 false);
2783 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit->getProducer());
2784 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit->getLanguage());
2785 AddString(Die, DW_AT_name, DW_FORM_string, DIUnit->getFilename());
2786 if (!DIUnit->getDirectory().empty())
2787 AddString(Die, DW_AT_comp_dir, DW_FORM_string, DIUnit->getDirectory());
2788
2789 CompileUnit *Unit = new CompileUnit(ID, Die);
2790 DW_CUs[DIUnit->getGV()] = Unit;
2791 }
2792 }
2793
Devang Patel289f2362009-01-05 23:11:11 +00002794 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
2795 /// visible global variables.
2796 void ConstructGlobalVariableDIEs() {
2797 std::string GVName = "llvm.dbg.global_variables";
2798 std::vector<GlobalVariable*> Result;
2799 getGlobalVariablesUsing(*M, GVName, Result);
2800 for (std::vector<GlobalVariable *>::iterator GVI = Result.begin(),
2801 GVE = Result.end(); GVI != GVE; ++GVI) {
2802 DIGlobalVariable *DI_GV = new DIGlobalVariable(*GVI);
2803 CompileUnit *DW_Unit = FindCompileUnit(DI_GV->getCompileUnit());
2804
2805 // Check for pre-existence.
2806 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV->getGV());
2807 if (Slot) continue;
2808
2809 DIE *VariableDie = new DIE(DW_TAG_variable);
2810 AddString(VariableDie, DW_AT_name, DW_FORM_string, DI_GV->getName());
2811 const std::string &LinkageName = DI_GV->getLinkageName();
2812 if (!LinkageName.empty())
2813 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
2814 LinkageName);
2815 AddType(DW_Unit, VariableDie, DI_GV->getType());
2816
2817 if (!DI_GV->isLocalToUnit())
2818 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
2819
2820 // Add source line info, if available.
2821 AddSourceLine(VariableDie, DI_GV);
2822
2823 // Add address.
2824 DIEBlock *Block = new DIEBlock();
2825 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2826 AddObjectLabel(Block, 0, DW_FORM_udata,
2827 Asm->getGlobalLinkName(DI_GV->getGV()));
2828 AddBlock(VariableDie, DW_AT_location, 0, Block);
2829
2830 //Add to map.
2831 Slot = VariableDie;
2832
2833 //Add to context owner.
2834 DW_Unit->getDie()->AddChild(VariableDie);
2835
2836 //Expose as global. FIXME - need to check external flag.
2837 DW_Unit->AddGlobal(DI_GV->getName(), VariableDie);
2838 }
2839 }
2840
Devang Patele6caf012009-01-05 23:21:35 +00002841 /// ConstructSubprograms - Create DIEs for each of the externally visible
2842 /// subprograms.
2843 void ConstructSubprograms() {
2844
2845 std::string SPName = "llvm.dbg.subprograms";
2846 std::vector<GlobalVariable*> Result;
2847 getGlobalVariablesUsing(*M, SPName, Result);
2848 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
2849 RE = Result.end(); RI != RE; ++RI) {
2850
2851 DISubprogram *SP = new DISubprogram(*RI);
2852 CompileUnit *Unit = FindCompileUnit(SP->getCompileUnit());
2853
2854 // Check for pre-existence.
2855 DIE *&Slot = Unit->getDieMapSlotFor(SP->getGV());
2856 if (Slot) continue;
2857
2858 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
2859 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, SP->getName());
2860 const std::string &LinkageName = SP->getLinkageName();
2861 if (!LinkageName.empty())
2862 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
2863 LinkageName);
2864 DIType SPTy = SP->getType();
2865 AddType(Unit, SubprogramDie, SPTy);
2866 if (!SP->isLocalToUnit())
2867 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
2868 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
2869
2870 AddSourceLine(SubprogramDie, SP);
2871 //Add to map.
2872 Slot = SubprogramDie;
2873 //Add to context owner.
2874 Unit->getDie()->AddChild(SubprogramDie);
2875 //Expose as global.
2876 Unit->AddGlobal(SP->getName(), SubprogramDie);
2877 }
2878 }
2879
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002880public:
2881 //===--------------------------------------------------------------------===//
2882 // Main entry points.
2883 //
Owen Anderson847b99b2008-08-21 00:14:44 +00002884 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00002885 : Dwarf(OS, A, T, "dbg")
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002886 , CompileUnits()
2887 , AbbreviationsSet(InitAbbreviationsSetSize)
2888 , Abbreviations()
2889 , ValuesSet(InitValuesSetSize)
2890 , Values()
2891 , StringPool()
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002892 , SectionMap()
2893 , SectionSourceLines()
2894 , didInitial(false)
2895 , shouldEmit(false)
Devang Patel4d1709e2009-01-08 02:33:41 +00002896 , RootDbgScope(NULL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002897 {
2898 }
2899 virtual ~DwarfDebug() {
2900 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2901 delete CompileUnits[i];
2902 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2903 delete Values[j];
2904 }
2905
Devang Patel9304b382009-01-06 21:07:30 +00002906 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
2907 /// This is inovked by the target AsmPrinter.
Devang Patel91d27b02009-01-12 23:09:42 +00002908 void SetDebugInfo(MachineModuleInfo *mmi) {
2909
Devang Patel9304b382009-01-06 21:07:30 +00002910 // Create all the compile unit DIEs.
2911 ConstructCompileUnits();
Devang Patel91d27b02009-01-12 23:09:42 +00002912
2913 if (DW_CUs.empty())
2914 return;
2915
2916 MMI = mmi;
2917 shouldEmit = true;
Devang Patel86cfa402009-01-13 23:02:17 +00002918 MMI->setDebugInfoAvailability(true);
Devang Patel9304b382009-01-06 21:07:30 +00002919
2920 // Create DIEs for each of the externally visible global variables.
2921 ConstructGlobalVariableDIEs();
2922
2923 // Create DIEs for each of the externally visible subprograms.
2924 ConstructSubprograms();
2925
2926 // Prime section data.
2927 SectionMap.insert(TAI->getTextSection());
2928
2929 // Print out .file directives to specify files for .loc directives. These
2930 // are printed out early so that they precede any .loc directives.
2931 if (TAI->hasDotLocAndDotFile()) {
2932 for (unsigned i = 1, e = SrcFiles.size(); i <= e; ++i) {
2933 sys::Path FullPath(Directories[SrcFiles[i].getDirectoryID()]);
2934 bool AppendOk = FullPath.appendComponent(SrcFiles[i].getName());
2935 assert(AppendOk && "Could not append filename to directory!");
2936 AppendOk = false;
2937 Asm->EmitFile(i, FullPath.toString());
2938 Asm->EOL();
2939 }
2940 }
2941
2942 // Emit initial sections
2943 EmitInitial();
2944 }
2945
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002946 /// BeginModule - Emit all Dwarf sections that should come prior to the
2947 /// content.
2948 void BeginModule(Module *M) {
2949 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002950 }
2951
2952 /// EndModule - Emit all Dwarf sections that should come after the content.
2953 ///
2954 void EndModule() {
2955 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00002956
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002957 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002958 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002959 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002960 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002961 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00002962
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002963 // End text sections.
2964 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002965 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002966 EmitLabel("section_end", i);
2967 }
2968
2969 // Emit common frame information.
2970 EmitCommonDebugFrame();
2971
2972 // Emit function debug frame information
2973 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2974 E = DebugFrames.end(); I != E; ++I)
2975 EmitFunctionDebugFrame(*I);
2976
2977 // Compute DIE offsets and sizes.
2978 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00002979
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002980 // Emit all the DIEs into a debug info section
2981 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00002982
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002983 // Corresponding abbreviations into a abbrev section.
2984 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00002985
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002986 // Emit source line correspondence into a debug line section.
2987 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00002988
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002989 // Emit info into a debug pubnames section.
2990 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00002991
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002992 // Emit info into a debug str section.
2993 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00002994
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002995 // Emit info into a debug loc section.
2996 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00002997
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002998 // Emit info into a debug aranges section.
2999 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00003000
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003001 // Emit info into a debug ranges section.
3002 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00003003
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003004 // Emit info into a debug macinfo section.
3005 EmitDebugMacInfo();
3006 }
3007
aslc200b112008-08-16 12:57:46 +00003008 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003009 /// emitted immediately after the function entry point.
3010 void BeginFunction(MachineFunction *MF) {
3011 this->MF = MF;
aslc200b112008-08-16 12:57:46 +00003012
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003013 if (!ShouldEmitDwarf()) return;
3014
3015 // Begin accumulating function debug information.
3016 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00003017
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003018 // Assumes in correct section after the entry point.
3019 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00003020
3021 // Emit label for the implicitly defined dbg.stoppoint at the start of
3022 // the function.
Devang Patel35a078f2009-01-12 22:54:42 +00003023 if (!Lines.empty()) {
3024 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth42f91402008-04-03 17:37:43 +00003025 Asm->printLabel(LineInfo.getLabelID());
3026 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003027 }
aslc200b112008-08-16 12:57:46 +00003028
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003029 /// EndFunction - Gather and emit post-function debug information.
3030 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003031 void EndFunction(MachineFunction *MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003032 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00003033
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003034 // Define end label for subprogram.
3035 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003036
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003037 // Get function line info.
Devang Patel35a078f2009-01-12 22:54:42 +00003038 if (!Lines.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003039 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003040 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003041 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patel35a078f2009-01-12 22:54:42 +00003042 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003043 // Append the function info to section info.
3044 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patel35a078f2009-01-12 22:54:42 +00003045 Lines.begin(), Lines.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003046 }
aslc200b112008-08-16 12:57:46 +00003047
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003048 // Construct scopes for subprogram.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003049 if (RootDbgScope)
3050 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003051 else
3052 // FIXME: This is wrong. We are essentially getting past a problem with
3053 // debug information not being able to handle unreachable blocks that have
3054 // debug information in them. In particular, those unreachable blocks that
3055 // have "region end" info in them. That situation results in the "root
3056 // scope" not being created. If that's the case, then emit a "default"
3057 // scope, i.e., one that encompasses the whole function. This isn't
3058 // desirable. And a better way of handling this (and all of the debugging
3059 // information) needs to be explored.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003060 ConstructDefaultDbgScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003061
3062 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3063 MMI->getFrameMoves()));
Devang Patela4162952009-01-12 18:48:36 +00003064
3065 // Clear debug info
3066 if (RootDbgScope) {
3067 delete RootDbgScope;
3068 DbgScopeMap.clear();
3069 RootDbgScope = NULL;
3070 }
3071 Lines.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003072 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003073
3074public:
3075
3076 /// RecordSourceLine - Records location information and associates it with a
3077 /// label. Returns a unique label ID used to generate a label and provide
3078 /// correspondence to the source line list.
3079 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
3080 CompileUnit *Unit = DW_CUs[V];
3081 assert (Unit && "Unable to find CompileUnit");
3082 unsigned ID = MMI->NextLabelID();
3083 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
3084 return ID;
3085 }
3086
3087 /// RecordSourceLine - Records location information and associates it with a
3088 /// label. Returns a unique label ID used to generate a label and provide
3089 /// correspondence to the source line list.
3090 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
3091 unsigned ID = MMI->NextLabelID();
3092 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
3093 return ID;
3094 }
3095
3096 unsigned getRecordSourceLineCount() {
3097 return Lines.size();
3098 }
3099
3100 /// RecordSource - Register a source file with debug info. Returns an source
3101 /// ID.
3102 unsigned RecordSource(const std::string &Directory,
3103 const std::string &File) {
3104 unsigned DID = Directories.insert(Directory);
3105 return SrcFiles.insert(SrcFileInfo(DID,File));
3106 }
3107
3108 /// RecordRegionStart - Indicate the start of a region.
3109 ///
3110 unsigned RecordRegionStart(GlobalVariable *V) {
3111 DbgScope *Scope = getOrCreateScope(V);
3112 unsigned ID = MMI->NextLabelID();
3113 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
3114 return ID;
3115 }
3116
3117 /// RecordRegionEnd - Indicate the end of a region.
3118 ///
3119 unsigned RecordRegionEnd(GlobalVariable *V) {
3120 DbgScope *Scope = getOrCreateScope(V);
3121 unsigned ID = MMI->NextLabelID();
3122 Scope->setEndLabelID(ID);
3123 return ID;
3124 }
3125
3126 /// RecordVariable - Indicate the declaration of a local variable.
3127 ///
3128 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Devang Patel2560d922009-01-15 18:25:17 +00003129 DIDescriptor Desc(GV);
3130 DbgScope *Scope = NULL;
3131 if (Desc.getTag() == DW_TAG_variable) {
3132 // GV is a global variable.
3133 DIGlobalVariable DG(GV);
3134 Scope = getOrCreateScope(DG.getContext().getGV());
3135 } else {
3136 // or GV is a local variable.
3137 DIVariable DV(GV);
3138 Scope = getOrCreateScope(DV.getContext().getGV());
3139 }
3140 assert (Scope && "Unable to find variable' scope");
Devang Patelcb59fd42009-01-12 19:17:34 +00003141 DIVariable *VD = new DIVariable(GV);
3142 DbgVariable *DV = new DbgVariable(VD, FrameIndex);
3143 Scope->AddVariable(DV);
3144 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003145};
3146
3147//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003148/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003149///
3150class DwarfException : public Dwarf {
3151
3152private:
3153 struct FunctionEHFrameInfo {
3154 std::string FnName;
3155 unsigned Number;
3156 unsigned PersonalityIndex;
3157 bool hasCalls;
3158 bool hasLandingPads;
3159 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003160 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003161
3162 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3163 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003164 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003165 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003166 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003167 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003168 };
3169
3170 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003171
3172 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3173 /// be emitted.
3174 bool shouldEmitTable;
3175
3176 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3177 /// should be emitted.
3178 bool shouldEmitMoves;
3179
3180 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3181 /// should be emitted.
3182 bool shouldEmitTableModule;
3183
aslc200b112008-08-16 12:57:46 +00003184 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003185 /// should be emitted.
3186 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003187
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003188 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3189 ///
3190 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3191 // Size and sign of stack growth.
3192 int stackGrowth =
3193 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3194 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003195 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003196
3197 // Begin eh frame section.
3198 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003199
3200 if (!TAI->doesRequireNonLocalEHFrameLabel())
3201 O << TAI->getEHGlobalPrefix();
3202 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003203 EmitLabel("section_eh_frame", Index);
3204
3205 // Define base labels.
3206 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003207
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003208 // Define the eh frame length.
3209 EmitDifference("eh_frame_common_end", Index,
3210 "eh_frame_common_begin", Index, true);
3211 Asm->EOL("Length of Common Information Entry");
3212
3213 // EH frame header.
3214 EmitLabel("eh_frame_common_begin", Index);
3215 Asm->EmitInt32((int)0);
3216 Asm->EOL("CIE Identifier Tag");
3217 Asm->EmitInt8(DW_CIE_VERSION);
3218 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003219
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003220 // The personality presence indicates that language specific information
3221 // will show up in the eh frame.
3222 Asm->EmitString(Personality ? "zPLR" : "zR");
3223 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003224
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003225 // Round out reader.
3226 Asm->EmitULEB128Bytes(1);
3227 Asm->EOL("CIE Code Alignment Factor");
3228 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003229 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003230 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003231 Asm->EOL("CIE Return Address Column");
3232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003233 // If there is a personality, we need to indicate the functions location.
3234 if (Personality) {
3235 Asm->EmitULEB128Bytes(7);
3236 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003237
Duncan Sands96144f92008-05-07 19:11:09 +00003238 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003239 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003240 Asm->EOL("Personality (pcrel sdata4 indirect)");
3241 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003242 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003243 Asm->EOL("Personality (pcrel sdata4)");
3244 }
Bill Wendling2d369922007-09-11 17:20:55 +00003245
Duncan Sands96144f92008-05-07 19:11:09 +00003246 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003247 O << TAI->getPersonalityPrefix();
3248 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3249 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003250 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3251 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003252 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003253
Duncan Sands96144f92008-05-07 19:11:09 +00003254 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3255 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003256
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003257 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3258 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003259 } else {
3260 Asm->EmitULEB128Bytes(1);
3261 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003262
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003263 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3264 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003265 }
3266
3267 // Indicate locations of general callee saved registers in frame.
3268 std::vector<MachineMove> Moves;
3269 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003270 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003271
Dale Johannesen388f20f2008-04-30 00:43:29 +00003272 // On Darwin the linker honors the alignment of eh_frame, which means it
3273 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3274 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003275 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003276 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003277 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003278
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003279 Asm->EOL();
3280 }
Duncan Sands96144f92008-05-07 19:11:09 +00003281
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003282 /// EmitEHFrame - Emit function exception frame information.
3283 ///
3284 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003285 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3286
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003287 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3288
3289 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003290 // If the corresponding function is static, this should not be
3291 // externally visible.
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003292 if (linkage != Function::InternalLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003293 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3294 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3295 }
3296
Dale Johannesenf09b5992008-01-10 02:03:30 +00003297 // If corresponding function is weak definition, this should be too.
aslc200b112008-08-16 12:57:46 +00003298 if ((linkage == Function::WeakLinkage ||
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003299 linkage == Function::LinkOnceLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003300 TAI->getWeakDefDirective())
3301 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3302
3303 // If there are no calls then you can't unwind. This may mean we can
3304 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003305 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003306 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003307 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003308 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003309 !UnwindTablesMandatory &&
aslc200b112008-08-16 12:57:46 +00003310 ((linkage != Function::WeakLinkage &&
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003311 linkage != Function::LinkOnceLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003312 !TAI->getWeakDefDirective() ||
3313 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003314 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003315 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003316 // This name has no connection to the function, so it might get
3317 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003318 // dead-stripping unconditionally.
3319 if (const char *UsedDirective = TAI->getUsedDirective())
3320 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003321 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003322 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003323
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003324 // EH frame header.
3325 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3326 "eh_frame_begin", EHFrameInfo.Number, true);
3327 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003328
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003329 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3330
Bill Wendling189bde72008-12-24 08:05:17 +00003331 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3332 PrintRelDirective(true, true);
3333 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3334
3335 if (!TAI->isAbsoluteEHSectionOffsets())
3336 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3337 } else {
3338 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3339 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3340 true, true, false);
3341 }
3342
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003343 Asm->EOL("FDE CIE offset");
3344
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003345 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003346 Asm->EOL("FDE initial location");
3347 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003348 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003349 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003350
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003351 // If there is a personality and landing pads then point to the language
3352 // specific data area in the exception table.
3353 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003354 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003355 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003356
3357 if (EHFrameInfo.hasLandingPads)
3358 EmitReference("exception", EHFrameInfo.Number, true, true);
3359 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003360 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003361 Asm->EOL("Language Specific Data Area");
3362 } else {
3363 Asm->EmitULEB128Bytes(0);
3364 Asm->EOL("Augmentation size");
3365 }
Duncan Sands96144f92008-05-07 19:11:09 +00003366
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003367 // Indicate locations of function specific callee saved registers in
3368 // frame.
Dale Johannesenf5a11532007-11-13 19:13:01 +00003369 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
aslc200b112008-08-16 12:57:46 +00003370
Dale Johannesen388f20f2008-04-30 00:43:29 +00003371 // On Darwin the linker honors the alignment of eh_frame, which means it
3372 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3373 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003374 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003375 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003376 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003377
3378 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003379 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003380 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003381 // that depends on unused functions (calling undefined externals) being
3382 // dead-stripped to link correctly. Yes, there really is.
3383 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3384 if (const char *UsedDirective = TAI->getUsedDirective())
3385 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3386 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003387 }
3388
Duncan Sands241a0c92007-09-05 11:27:52 +00003389 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003390 ///
3391 /// The general organization of the table is complex, but the basic concepts
3392 /// are easy. First there is a header which describes the location and
3393 /// organization of the three components that follow.
3394 /// 1. The landing pad site information describes the range of code covered
3395 /// by the try. In our case it's an accumulation of the ranges covered
3396 /// by the invokes in the try. There is also a reference to the landing
3397 /// pad that handles the exception once processed. Finally an index into
3398 /// the actions table.
3399 /// 2. The action table, in our case, is composed of pairs of type ids
3400 /// and next action offset. Starting with the action index from the
3401 /// landing pad site, each type Id is checked for a match to the current
3402 /// exception. If it matches then the exception and type id are passed
3403 /// on to the landing pad. Otherwise the next action is looked up. This
3404 /// chain is terminated with a next action of zero. If no type id is
3405 /// found the the frame is unwound and handling continues.
3406 /// 3. Type id table contains references to all the C++ typeinfo for all
3407 /// catches in the function. This tables is reversed indexed base 1.
3408
3409 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3410 static unsigned SharedTypeIds(const LandingPadInfo *L,
3411 const LandingPadInfo *R) {
3412 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3413 unsigned LSize = LIds.size(), RSize = RIds.size();
3414 unsigned MinSize = LSize < RSize ? LSize : RSize;
3415 unsigned Count = 0;
3416
3417 for (; Count != MinSize; ++Count)
3418 if (LIds[Count] != RIds[Count])
3419 return Count;
3420
3421 return Count;
3422 }
3423
3424 /// PadLT - Order landing pads lexicographically by type id.
3425 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3426 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3427 unsigned LSize = LIds.size(), RSize = RIds.size();
3428 unsigned MinSize = LSize < RSize ? LSize : RSize;
3429
3430 for (unsigned i = 0; i != MinSize; ++i)
3431 if (LIds[i] != RIds[i])
3432 return LIds[i] < RIds[i];
3433
3434 return LSize < RSize;
3435 }
3436
3437 struct KeyInfo {
3438 static inline unsigned getEmptyKey() { return -1U; }
3439 static inline unsigned getTombstoneKey() { return -2U; }
3440 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00003441 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003442 static bool isPod() { return true; }
3443 };
3444
Duncan Sands241a0c92007-09-05 11:27:52 +00003445 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003446 struct ActionEntry {
3447 int ValueForTypeID; // The value to write - may not be equal to the type id.
3448 int NextAction;
3449 struct ActionEntry *Previous;
3450 };
3451
Duncan Sands241a0c92007-09-05 11:27:52 +00003452 /// PadRange - Structure holding a try-range and the associated landing pad.
3453 struct PadRange {
3454 // The index of the landing pad.
3455 unsigned PadIndex;
3456 // The index of the begin and end labels in the landing pad's label lists.
3457 unsigned RangeIndex;
3458 };
3459
3460 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3461
3462 /// CallSiteEntry - Structure describing an entry in the call-site table.
3463 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00003464 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003465 unsigned BeginLabel; // zero indicates the start of the function.
3466 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003467 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003468 unsigned PadLabel; // zero indicates that there is no landing pad.
3469 unsigned Action;
3470 };
3471
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003472 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003473 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3474 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3475 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3476 if (PadInfos.empty()) return;
3477
3478 // Sort the landing pads in order of their type ids. This is used to fold
3479 // duplicate actions.
3480 SmallVector<const LandingPadInfo *, 64> LandingPads;
3481 LandingPads.reserve(PadInfos.size());
3482 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3483 LandingPads.push_back(&PadInfos[i]);
3484 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3485
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003486 // Negative type ids index into FilterIds, positive type ids index into
3487 // TypeInfos. The value written for a positive type id is just the type
3488 // id itself. For a negative type id, however, the value written is the
3489 // (negative) byte offset of the corresponding FilterIds entry. The byte
3490 // offset is usually equal to the type id, because the FilterIds entries
3491 // are written using a variable width encoding which outputs one byte per
3492 // entry as long as the value written is not too large, but can differ.
3493 // This kind of complication does not occur for positive type ids because
3494 // type infos are output using a fixed width encoding.
3495 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3496 SmallVector<int, 16> FilterOffsets;
3497 FilterOffsets.reserve(FilterIds.size());
3498 int Offset = -1;
3499 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3500 E = FilterIds.end(); I != E; ++I) {
3501 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00003502 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003503 }
3504
Duncan Sands241a0c92007-09-05 11:27:52 +00003505 // Compute the actions table and gather the first action index for each
3506 // landing pad site.
3507 SmallVector<ActionEntry, 32> Actions;
3508 SmallVector<unsigned, 64> FirstActions;
3509 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003510
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003511 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00003512 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003513 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3514 const LandingPadInfo *LP = LandingPads[i];
3515 const std::vector<int> &TypeIds = LP->TypeIds;
3516 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3517 unsigned SizeSiteActions = 0;
3518
3519 if (NumShared < TypeIds.size()) {
3520 unsigned SizeAction = 0;
3521 ActionEntry *PrevAction = 0;
3522
3523 if (NumShared) {
3524 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3525 assert(Actions.size());
3526 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00003527 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3528 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003529 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00003530 SizeAction -=
3531 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003532 SizeAction += -PrevAction->NextAction;
3533 PrevAction = PrevAction->Previous;
3534 }
3535 }
3536
3537 // Compute the actions.
3538 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3539 int TypeID = TypeIds[I];
3540 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3541 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00003542 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003543
3544 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00003545 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003546 SizeSiteActions += SizeAction;
3547
3548 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
3549 Actions.push_back(Action);
3550
3551 PrevAction = &Actions.back();
3552 }
3553
3554 // Record the first action of the landing pad site.
3555 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3556 } // else identical - re-use previous FirstAction
3557
3558 FirstActions.push_back(FirstAction);
3559
3560 // Compute this sites contribution to size.
3561 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003562 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003563
Duncan Sands4ff179f2007-12-19 07:36:31 +00003564 // Compute the call-site table. The entry for an invoke has a try-range
3565 // containing the call, a non-zero landing pad and an appropriate action.
3566 // The entry for an ordinary call has a try-range containing the call and
3567 // zero for the landing pad and the action. Calls marked 'nounwind' have
3568 // no entry and must not be contained in the try-range of any entry - they
3569 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003570 SmallVector<CallSiteEntry, 64> CallSites;
3571
3572 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003573 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3574 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3575 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00003576 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3577 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003578 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003579 unsigned BeginLabel = LandingPad->BeginLabels[j];
3580 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3581 PadRange P = { i, j };
3582 PadMap[BeginLabel] = P;
3583 }
3584 }
3585
Duncan Sands4ff179f2007-12-19 07:36:31 +00003586 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00003587 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003588
3589 // Whether there is a potentially throwing instruction (currently this means
3590 // an ordinary call) between the end of the previous try-range and now.
3591 bool SawPotentiallyThrowing = false;
3592
3593 // Whether the last callsite entry was for an invoke.
3594 bool PreviousIsInvoke = false;
3595
Duncan Sands4ff179f2007-12-19 07:36:31 +00003596 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003597 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3598 I != E; ++I) {
3599 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3600 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00003601 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00003602 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00003603 continue;
3604 }
3605
Chris Lattnerda4cff12007-12-30 20:50:28 +00003606 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00003607 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00003608
Duncan Sands4ff179f2007-12-19 07:36:31 +00003609 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00003610 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00003611 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003612
Duncan Sands4ff179f2007-12-19 07:36:31 +00003613 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00003614 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00003615 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00003616 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00003617 continue;
3618
3619 PadRange P = L->second;
3620 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3621
3622 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3623 "Inconsistent landing pad map!");
3624
3625 // If some instruction between the previous try-range and this one may
3626 // throw, create a call-site entry with no landing pad for the region
3627 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003628 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003629 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3630 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00003631 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003632 }
3633
3634 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003635 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00003636
Duncan Sands4ff179f2007-12-19 07:36:31 +00003637 if (LandingPad->LandingPadLabel) {
3638 // This try-range is for an invoke.
3639 CallSiteEntry Site = {BeginLabel, LastLabel,
3640 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00003641
Duncan Sands4ff179f2007-12-19 07:36:31 +00003642 // Try to merge with the previous call-site.
3643 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00003644 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00003645 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3646 // Extend the range of the previous entry.
3647 Prev.EndLabel = Site.EndLabel;
3648 continue;
3649 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003650 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003651
Duncan Sands4ff179f2007-12-19 07:36:31 +00003652 // Otherwise, create a new call-site.
3653 CallSites.push_back(Site);
3654 PreviousIsInvoke = true;
3655 } else {
3656 // Create a gap.
3657 PreviousIsInvoke = false;
3658 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003659 }
3660 }
3661 // If some instruction between the previous try-range and the end of the
3662 // function may throw, create a call-site entry with no landing pad for the
3663 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003664 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003665 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3666 CallSites.push_back(Site);
3667 }
3668
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003669 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00003670
3671 // Call sites.
3672 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3673 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3674 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3675 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3676 SiteLengthSize +
3677 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00003678 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00003679 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00003680
Duncan Sands96144f92008-05-07 19:11:09 +00003681 // Type infos.
3682 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3683 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003684
3685 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00003686 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003687 SizeSites + SizeActions + SizeTypes;
3688
3689 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3690 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00003691 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003692 TypeOffset;
3693
3694 unsigned SizeAlign = (4 - TotalSize) & 3;
3695
3696 // Begin the exception table.
3697 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00003698 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00003699 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003700 for (unsigned i = 0; i != SizeAlign; ++i) {
3701 Asm->EmitInt8(0);
3702 Asm->EOL("Padding");
3703 }
3704 EmitLabel("exception", SubprogramCount);
3705
3706 // Emit the header.
3707 Asm->EmitInt8(DW_EH_PE_omit);
3708 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3709 Asm->EmitInt8(DW_EH_PE_absptr);
3710 Asm->EOL("TType format (DW_EH_PE_absptr)");
3711 Asm->EmitULEB128Bytes(TypeOffset);
3712 Asm->EOL("TType base offset");
3713 Asm->EmitInt8(DW_EH_PE_udata4);
3714 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3715 Asm->EmitULEB128Bytes(SizeSites);
3716 Asm->EOL("Call-site table length");
3717
Duncan Sands241a0c92007-09-05 11:27:52 +00003718 // Emit the landing pad site information.
3719 for (unsigned i = 0; i < CallSites.size(); ++i) {
3720 CallSiteEntry &S = CallSites[i];
3721 const char *BeginTag;
3722 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003723
Duncan Sands241a0c92007-09-05 11:27:52 +00003724 if (!S.BeginLabel) {
3725 BeginTag = "eh_func_begin";
3726 BeginNumber = SubprogramCount;
3727 } else {
3728 BeginTag = "label";
3729 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003730 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003731
Duncan Sands241a0c92007-09-05 11:27:52 +00003732 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003733 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003734 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003735
Duncan Sands241a0c92007-09-05 11:27:52 +00003736 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00003737 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00003738 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003739 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00003740 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003741 }
3742 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003743
Duncan Sands96144f92008-05-07 19:11:09 +00003744 if (!S.PadLabel)
3745 Asm->EmitInt32(0);
3746 else
Duncan Sands241a0c92007-09-05 11:27:52 +00003747 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003748 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003749 Asm->EOL("Landing pad");
3750
3751 Asm->EmitULEB128Bytes(S.Action);
3752 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003753 }
3754
3755 // Emit the actions.
3756 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3757 ActionEntry &Action = Actions[I];
3758
3759 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
3760 Asm->EOL("TypeInfo index");
3761 Asm->EmitSLEB128Bytes(Action.NextAction);
3762 Asm->EOL("Next action");
3763 }
3764
3765 // Emit the type ids.
3766 for (unsigned M = TypeInfos.size(); M; --M) {
3767 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00003768
3769 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003770
3771 if (GV)
3772 O << Asm->getGlobalLinkName(GV);
3773 else
3774 O << "0";
Duncan Sands241a0c92007-09-05 11:27:52 +00003775
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003776 Asm->EOL("TypeInfo");
3777 }
3778
3779 // Emit the filter typeids.
3780 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3781 unsigned TypeID = FilterIds[j];
3782 Asm->EmitULEB128Bytes(TypeID);
3783 Asm->EOL("Filter TypeInfo index");
3784 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003785
Evan Cheng7e7d1942008-02-29 19:36:59 +00003786 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003787 }
3788
3789public:
3790 //===--------------------------------------------------------------------===//
3791 // Main entry points.
3792 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003793 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00003794 : Dwarf(OS, A, T, "eh")
Dale Johannesen85535762008-04-02 00:25:04 +00003795 , shouldEmitTable(false)
3796 , shouldEmitMoves(false)
3797 , shouldEmitTableModule(false)
3798 , shouldEmitMovesModule(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003799 {}
aslc200b112008-08-16 12:57:46 +00003800
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003801 virtual ~DwarfException() {}
3802
3803 /// SetModuleInfo - Set machine module information when it's known that pass
3804 /// manager has created it. Set by the target AsmPrinter.
3805 void SetModuleInfo(MachineModuleInfo *mmi) {
3806 MMI = mmi;
3807 }
3808
3809 /// BeginModule - Emit all exception information that should come prior to the
3810 /// content.
3811 void BeginModule(Module *M) {
3812 this->M = M;
3813 }
3814
3815 /// EndModule - Emit all exception information that should come after the
3816 /// content.
3817 void EndModule() {
Dale Johannesen85535762008-04-02 00:25:04 +00003818 if (shouldEmitMovesModule || shouldEmitTableModule) {
3819 const std::vector<Function *> Personalities = MMI->getPersonalities();
3820 for (unsigned i =0; i < Personalities.size(); ++i)
3821 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003822
Dale Johannesen85535762008-04-02 00:25:04 +00003823 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3824 E = EHFrames.end(); I != E; ++I)
3825 EmitEHFrame(*I);
3826 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003827 }
3828
aslc200b112008-08-16 12:57:46 +00003829 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003830 /// emitted immediately after the function entry point.
3831 void BeginFunction(MachineFunction *MF) {
3832 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00003833 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen62f0a6d2008-04-02 17:04:45 +00003834 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00003835
3836 // Map all labels and get rid of any dead landing pads.
3837 MMI->TidyLandingPads();
3838 // If any landing pads survive, we need an EH table.
3839 if (MMI->getLandingPads().size())
3840 shouldEmitTable = true;
3841
3842 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00003843 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00003844 shouldEmitMoves = true;
3845
3846 if (shouldEmitMoves || shouldEmitTable)
3847 // Assumes in correct section after the entry point.
3848 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003849 }
Dale Johannesen85535762008-04-02 00:25:04 +00003850 shouldEmitTableModule |= shouldEmitTable;
3851 shouldEmitMovesModule |= shouldEmitMoves;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003852 }
3853
3854 /// EndFunction - Gather and emit post-function exception information.
3855 ///
3856 void EndFunction() {
Dale Johannesen85535762008-04-02 00:25:04 +00003857 if (shouldEmitMoves || shouldEmitTable) {
3858 EmitLabel("eh_func_end", SubprogramCount);
3859 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003860
Dale Johannesen85535762008-04-02 00:25:04 +00003861 // Save EH frame information
3862 EHFrames.
3863 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingef9211a2007-09-18 01:47:22 +00003864 SubprogramCount,
3865 MMI->getPersonalityIndex(),
3866 MF->getFrameInfo()->hasCalls(),
3867 !MMI->getLandingPads().empty(),
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003868 MMI->getFrameMoves(),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003869 MF->getFunction()));
Dale Johannesen85535762008-04-02 00:25:04 +00003870 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003871 }
3872};
3873
3874} // End of namespace llvm
3875
3876//===----------------------------------------------------------------------===//
3877
3878/// Emit - Print the abbreviation using the specified Dwarf writer.
3879///
3880void DIEAbbrev::Emit(const DwarfDebug &DD) const {
3881 // Emit its Dwarf tag type.
3882 DD.getAsm()->EmitULEB128Bytes(Tag);
3883 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00003884
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003885 // Emit whether it has children DIEs.
3886 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3887 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00003888
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003889 // For each attribute description.
3890 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3891 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00003892
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003893 // Emit attribute type.
3894 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3895 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00003896
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003897 // Emit form type.
3898 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3899 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
3900 }
3901
3902 // Mark end of abbreviation.
3903 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3904 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
3905}
3906
3907#ifndef NDEBUG
3908void DIEAbbrev::print(std::ostream &O) {
3909 O << "Abbreviation @"
3910 << std::hex << (intptr_t)this << std::dec
3911 << " "
3912 << TagString(Tag)
3913 << " "
3914 << ChildrenString(ChildrenFlag)
3915 << "\n";
aslc200b112008-08-16 12:57:46 +00003916
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003917 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3918 O << " "
3919 << AttributeString(Data[i].getAttribute())
3920 << " "
3921 << FormEncodingString(Data[i].getForm())
3922 << "\n";
3923 }
3924}
3925void DIEAbbrev::dump() { print(cerr); }
3926#endif
3927
3928//===----------------------------------------------------------------------===//
3929
3930#ifndef NDEBUG
3931void DIEValue::dump() {
3932 print(cerr);
3933}
3934#endif
3935
3936//===----------------------------------------------------------------------===//
3937
3938/// EmitValue - Emit integer of appropriate size.
3939///
3940void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
3941 switch (Form) {
3942 case DW_FORM_flag: // Fall thru
3943 case DW_FORM_ref1: // Fall thru
3944 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
3945 case DW_FORM_ref2: // Fall thru
3946 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
3947 case DW_FORM_ref4: // Fall thru
3948 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
3949 case DW_FORM_ref8: // Fall thru
3950 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
3951 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
3952 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
3953 default: assert(0 && "DIE Value form not supported yet"); break;
3954 }
3955}
3956
3957/// SizeOf - Determine size of integer value in bytes.
3958///
3959unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3960 switch (Form) {
3961 case DW_FORM_flag: // Fall thru
3962 case DW_FORM_ref1: // Fall thru
3963 case DW_FORM_data1: return sizeof(int8_t);
3964 case DW_FORM_ref2: // Fall thru
3965 case DW_FORM_data2: return sizeof(int16_t);
3966 case DW_FORM_ref4: // Fall thru
3967 case DW_FORM_data4: return sizeof(int32_t);
3968 case DW_FORM_ref8: // Fall thru
3969 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00003970 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
3971 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003972 default: assert(0 && "DIE Value form not supported yet"); break;
3973 }
3974 return 0;
3975}
3976
3977//===----------------------------------------------------------------------===//
3978
3979/// EmitValue - Emit string value.
3980///
3981void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
3982 DD.getAsm()->EmitString(String);
3983}
3984
3985//===----------------------------------------------------------------------===//
3986
3987/// EmitValue - Emit label value.
3988///
3989void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00003990 bool IsSmall = Form == DW_FORM_data4;
3991 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003992}
3993
3994/// SizeOf - Determine size of label value in bytes.
3995///
3996unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00003997 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00003998 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003999}
4000
4001//===----------------------------------------------------------------------===//
4002
4003/// EmitValue - Emit label value.
4004///
4005void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004006 bool IsSmall = Form == DW_FORM_data4;
4007 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004008}
4009
4010/// SizeOf - Determine size of label value in bytes.
4011///
4012unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004013 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004014 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004015}
aslc200b112008-08-16 12:57:46 +00004016
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004017//===----------------------------------------------------------------------===//
4018
4019/// EmitValue - Emit delta value.
4020///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004021void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4022 bool IsSmall = Form == DW_FORM_data4;
4023 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4024 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4025}
4026
4027/// SizeOf - Determine size of delta value in bytes.
4028///
4029unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4030 if (Form == DW_FORM_data4) return 4;
4031 return DD.getTargetData()->getPointerSize();
4032}
aslc200b112008-08-16 12:57:46 +00004033
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004034//===----------------------------------------------------------------------===//
4035
4036/// EmitValue - Emit delta value.
4037///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004038void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4039 bool IsSmall = Form == DW_FORM_data4;
4040 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4041}
4042
4043/// SizeOf - Determine size of delta value in bytes.
4044///
4045unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4046 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004047 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004048}
4049
4050//===----------------------------------------------------------------------===//
4051
4052/// EmitValue - Emit debug information entry offset.
4053///
4054void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4055 DD.getAsm()->EmitInt32(Entry->getOffset());
4056}
aslc200b112008-08-16 12:57:46 +00004057
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004058//===----------------------------------------------------------------------===//
4059
4060/// ComputeSize - calculate the size of the block.
4061///
4062unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4063 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004064 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004065
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004066 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4067 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4068 }
4069 }
4070 return Size;
4071}
4072
4073/// EmitValue - Emit block data.
4074///
4075void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4076 switch (Form) {
4077 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4078 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4079 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4080 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4081 default: assert(0 && "Improper form for block"); break;
4082 }
aslc200b112008-08-16 12:57:46 +00004083
Owen Anderson88dd6232008-06-24 21:44:59 +00004084 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004085
4086 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4087 DD.getAsm()->EOL();
4088 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4089 }
4090}
4091
4092/// SizeOf - Determine size of block data in bytes.
4093///
4094unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4095 switch (Form) {
4096 case DW_FORM_block1: return Size + sizeof(int8_t);
4097 case DW_FORM_block2: return Size + sizeof(int16_t);
4098 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004099 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004100 default: assert(0 && "Improper form for block"); break;
4101 }
4102 return 0;
4103}
4104
4105//===----------------------------------------------------------------------===//
4106/// DIE Implementation
4107
4108DIE::~DIE() {
4109 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4110 delete Children[i];
4111}
aslc200b112008-08-16 12:57:46 +00004112
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004113/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4114///
4115void DIE::AddSiblingOffset() {
4116 DIEInteger *DI = new DIEInteger(0);
4117 Values.insert(Values.begin(), DI);
4118 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4119}
4120
4121/// Profile - Used to gather unique data for the value folding set.
4122///
4123void DIE::Profile(FoldingSetNodeID &ID) {
4124 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004125
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004126 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4127 ID.AddPointer(Children[i]);
4128
4129 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4130 ID.AddPointer(Values[j]);
4131}
4132
4133#ifndef NDEBUG
4134void DIE::print(std::ostream &O, unsigned IncIndent) {
4135 static unsigned IndentCount = 0;
4136 IndentCount += IncIndent;
4137 const std::string Indent(IndentCount, ' ');
4138 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004139
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004140 if (!isBlock) {
4141 O << Indent
4142 << "Die: "
4143 << "0x" << std::hex << (intptr_t)this << std::dec
4144 << ", Offset: " << Offset
4145 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004146 << "\n";
4147
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004148 O << Indent
4149 << TagString(Abbrev.getTag())
4150 << " "
4151 << ChildrenString(Abbrev.getChildrenFlag());
4152 } else {
4153 O << "Size: " << Size;
4154 }
4155 O << "\n";
4156
Owen Anderson88dd6232008-06-24 21:44:59 +00004157 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004158
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004159 IndentCount += 2;
4160 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4161 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004162
4163 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004164 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004165 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004166 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004167
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004168 O << " "
4169 << FormEncodingString(Data[i].getForm())
4170 << " ";
4171 Values[i]->print(O);
4172 O << "\n";
4173 }
4174 IndentCount -= 2;
4175
4176 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4177 Children[j]->print(O, 4);
4178 }
aslc200b112008-08-16 12:57:46 +00004179
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004180 if (!isBlock) O << "\n";
4181 IndentCount -= IncIndent;
4182}
4183
4184void DIE::dump() {
4185 print(cerr);
4186}
4187#endif
4188
4189//===----------------------------------------------------------------------===//
4190/// DwarfWriter Implementation
4191///
4192
Devang Patelaa1e8432009-01-08 23:40:34 +00004193DwarfWriter::DwarfWriter() : ImmutablePass(&ID), DD(NULL), DE(NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004194}
4195
4196DwarfWriter::~DwarfWriter() {
4197 delete DE;
4198 delete DD;
4199}
4200
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004201/// BeginModule - Emit all Dwarf sections that should come prior to the
4202/// content.
Devang Patelaa1e8432009-01-08 23:40:34 +00004203void DwarfWriter::BeginModule(Module *M,
4204 MachineModuleInfo *MMI,
4205 raw_ostream &OS, AsmPrinter *A,
4206 const TargetAsmInfo *T) {
4207 DE = new DwarfException(OS, A, T);
4208 DD = new DwarfDebug(OS, A, T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004209 DE->BeginModule(M);
4210 DD->BeginModule(M);
Devang Patel6ccd57e2009-01-13 00:20:51 +00004211 DD->SetDebugInfo(MMI);
Devang Patelaa1e8432009-01-08 23:40:34 +00004212 DE->SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004213}
4214
4215/// EndModule - Emit all Dwarf sections that should come after the content.
4216///
4217void DwarfWriter::EndModule() {
4218 DE->EndModule();
4219 DD->EndModule();
4220}
4221
aslc200b112008-08-16 12:57:46 +00004222/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004223/// emitted immediately after the function entry point.
4224void DwarfWriter::BeginFunction(MachineFunction *MF) {
4225 DE->BeginFunction(MF);
4226 DD->BeginFunction(MF);
4227}
4228
4229/// EndFunction - Gather and emit post-function debug information.
4230///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004231void DwarfWriter::EndFunction(MachineFunction *MF) {
4232 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004233 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004234
Bill Wendling5b4796a2008-07-22 00:53:37 +00004235 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004236 // Clear function debug information.
4237 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004238}
Devang Patelcb59fd42009-01-12 19:17:34 +00004239
4240/// RecordSourceLine - Records location information and associates it with a
4241/// label. Returns a unique label ID used to generate a label and provide
4242/// correspondence to the source line list.
4243unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4244 unsigned Src) {
4245 return DD->RecordSourceLine(Line, Col, Src);
4246}
4247
4248/// RecordSource - Register a source file with debug info. Returns an source
4249/// ID.
4250unsigned DwarfWriter::RecordSource(const std::string &Dir,
4251 const std::string &File) {
4252 return DD->RecordSource(Dir, File);
4253}
4254
4255/// RecordRegionStart - Indicate the start of a region.
4256unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
4257 return DD->RecordRegionStart(V);
4258}
4259
4260/// RecordRegionEnd - Indicate the end of a region.
4261unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
4262 return DD->RecordRegionEnd(V);
4263}
4264
4265/// getRecordSourceLineCount - Count source lines.
4266unsigned DwarfWriter::getRecordSourceLineCount() {
4267 return DD->getRecordSourceLineCount();
4268}
Devang Patel70190872009-01-13 21:25:00 +00004269
Devang Patelfe359e72009-01-13 21:44:10 +00004270/// RecordVariable - Indicate the declaration of a local variable.
4271///
4272void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
4273 DD->RecordVariable(GV, FrameIndex);
4274}
Devang Patel42f6bed2009-01-13 23:54:55 +00004275