blob: e7d8a0a305b0c6e8e8b9b682b1d371154d9f8092 [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:
752 /// Desc - Compile unit debug descriptor.
753 ///
754 CompileUnitDesc *Desc;
aslc200b112008-08-16 12:57:46 +0000755
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756 /// ID - File identifier for source.
757 ///
758 unsigned ID;
aslc200b112008-08-16 12:57:46 +0000759
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760 /// Die - Compile unit debug information entry.
761 ///
762 DIE *Die;
aslc200b112008-08-16 12:57:46 +0000763
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764 /// DescToDieMap - Tracks the mapping of unit level debug informaton
765 /// descriptors to debug information entries.
766 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
Devang Patel4a4cbe72009-01-05 21:47:57 +0000767 DenseMap<GlobalVariable *, DIE *> GVToDieMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768
769 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
770 /// descriptors to debug information entries using a DIEntry proxy.
771 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
Devang Patel4a4cbe72009-01-05 21:47:57 +0000772 DenseMap<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773
774 /// Globals - A map of globally visible named entities for this unit.
775 ///
776 std::map<std::string, DIE *> Globals;
777
778 /// DiesSet - Used to uniquely define dies within the compile unit.
779 ///
780 FoldingSet<DIE> DiesSet;
aslc200b112008-08-16 12:57:46 +0000781
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 /// Dies - List of all dies in the compile unit.
783 ///
784 std::vector<DIE *> Dies;
aslc200b112008-08-16 12:57:46 +0000785
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786public:
Devang Patelb3907da2009-01-05 23:03:32 +0000787 CompileUnit(unsigned I, DIE *D)
788 : ID(I), Die(D), DescToDieMap(), GVToDieMap(), DescToDIEntryMap(),
789 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize), Dies()
790 {}
791
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
793 : Desc(CUD)
794 , ID(I)
795 , Die(D)
796 , DescToDieMap()
Devang Patel4a4cbe72009-01-05 21:47:57 +0000797 , GVToDieMap()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 , DescToDIEntryMap()
Devang Patel4a4cbe72009-01-05 21:47:57 +0000799 , GVToDIEntryMap()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 , Globals()
801 , DiesSet(InitDiesSetSize)
802 , Dies()
803 {}
aslc200b112008-08-16 12:57:46 +0000804
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 ~CompileUnit() {
806 delete Die;
aslc200b112008-08-16 12:57:46 +0000807
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
809 delete Dies[i];
810 }
aslc200b112008-08-16 12:57:46 +0000811
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 // Accessors.
813 CompileUnitDesc *getDesc() const { return Desc; }
814 unsigned getID() const { return ID; }
815 DIE* getDie() const { return Die; }
816 std::map<std::string, DIE *> &getGlobals() { return Globals; }
817
818 /// hasContent - Return true if this compile unit has something to write out.
819 ///
820 bool hasContent() const {
821 return !Die->getChildren().empty();
822 }
823
824 /// AddGlobal - Add a new global entity to the compile unit.
825 ///
826 void AddGlobal(const std::string &Name, DIE *Die) {
827 Globals[Name] = Die;
828 }
aslc200b112008-08-16 12:57:46 +0000829
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830 /// getDieMapSlotFor - Returns the debug information entry map slot for the
831 /// specified debug descriptor.
832 DIE *&getDieMapSlotFor(DebugInfoDesc *DID) {
833 return DescToDieMap[DID];
834 }
Devang Patel4a4cbe72009-01-05 21:47:57 +0000835 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
836 return GVToDieMap[GV];
837 }
aslc200b112008-08-16 12:57:46 +0000838
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
840 /// specified debug descriptor.
841 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DID) {
842 return DescToDIEntryMap[DID];
843 }
Devang Patel4a4cbe72009-01-05 21:47:57 +0000844 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
845 return GVToDIEntryMap[GV];
846 }
aslc200b112008-08-16 12:57:46 +0000847
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000848 /// AddDie - Adds or interns the DIE to the compile unit.
849 ///
850 DIE *AddDie(DIE &Buffer) {
851 FoldingSetNodeID ID;
852 Buffer.Profile(ID);
853 void *Where;
854 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
aslc200b112008-08-16 12:57:46 +0000855
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 if (!Die) {
857 Die = new DIE(Buffer);
858 DiesSet.InsertNode(Die, Where);
859 this->Die->AddChild(Die);
860 Buffer.Detach();
861 }
aslc200b112008-08-16 12:57:46 +0000862
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863 return Die;
864 }
865};
866
867//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +0000868/// Dwarf - Emits general Dwarf directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869///
870class Dwarf {
871
872protected:
873
874 //===--------------------------------------------------------------------===//
875 // Core attributes used by the Dwarf writer.
876 //
aslc200b112008-08-16 12:57:46 +0000877
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 //
879 /// O - Stream to .s file.
880 ///
Owen Anderson847b99b2008-08-21 00:14:44 +0000881 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000882
883 /// Asm - Target of Dwarf emission.
884 ///
885 AsmPrinter *Asm;
aslc200b112008-08-16 12:57:46 +0000886
Bill Wendlingac9639d2008-07-01 23:34:48 +0000887 /// TAI - Target asm information.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 const TargetAsmInfo *TAI;
aslc200b112008-08-16 12:57:46 +0000889
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890 /// TD - Target data.
891 const TargetData *TD;
aslc200b112008-08-16 12:57:46 +0000892
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000893 /// RI - Register Information.
Dan Gohman1e57df32008-02-10 18:45:23 +0000894 const TargetRegisterInfo *RI;
aslc200b112008-08-16 12:57:46 +0000895
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896 /// M - Current module.
897 ///
898 Module *M;
aslc200b112008-08-16 12:57:46 +0000899
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000900 /// MF - Current machine function.
901 ///
902 MachineFunction *MF;
aslc200b112008-08-16 12:57:46 +0000903
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904 /// MMI - Collected machine module information.
905 ///
906 MachineModuleInfo *MMI;
aslc200b112008-08-16 12:57:46 +0000907
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908 /// SubprogramCount - The running count of functions being compiled.
909 ///
910 unsigned SubprogramCount;
aslc200b112008-08-16 12:57:46 +0000911
Chris Lattnerb3876c72007-09-24 03:35:37 +0000912 /// Flavor - A unique string indicating what dwarf producer this is, used to
913 /// unique labels.
914 const char * const Flavor;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915
916 unsigned SetCounter;
Owen Anderson847b99b2008-08-21 00:14:44 +0000917 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattnerb3876c72007-09-24 03:35:37 +0000918 const char *flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 : O(OS)
920 , Asm(A)
921 , TAI(T)
922 , TD(Asm->TM.getTargetData())
923 , RI(Asm->TM.getRegisterInfo())
924 , M(NULL)
925 , MF(NULL)
926 , MMI(NULL)
927 , SubprogramCount(0)
Chris Lattnerb3876c72007-09-24 03:35:37 +0000928 , Flavor(flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 , SetCounter(1)
930 {
931 }
932
933public:
934
935 //===--------------------------------------------------------------------===//
936 // Accessors.
937 //
938 AsmPrinter *getAsm() const { return Asm; }
939 MachineModuleInfo *getMMI() const { return MMI; }
940 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohmancfb72b22007-09-27 23:12:31 +0000941 const TargetData *getTargetData() const { return TD; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000942
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000943 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
944 const {
945 if (isInSection && TAI->getDwarfSectionOffsetDirective())
946 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohmancfb72b22007-09-27 23:12:31 +0000947 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000948 O << TAI->getData32bitsDirective();
949 else
950 O << TAI->getData64bitsDirective();
951 }
aslc200b112008-08-16 12:57:46 +0000952
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000953 /// PrintLabelName - Print label name in form used by Dwarf writer.
954 ///
955 void PrintLabelName(DWLabel Label) const {
956 PrintLabelName(Label.Tag, Label.Number);
957 }
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000958 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000959 O << TAI->getPrivateGlobalPrefix() << Tag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000960 if (Number) O << Number;
961 }
aslc200b112008-08-16 12:57:46 +0000962
Chris Lattnerb3876c72007-09-24 03:35:37 +0000963 void PrintLabelName(const char *Tag, unsigned Number,
964 const char *Suffix) const {
965 O << TAI->getPrivateGlobalPrefix() << Tag;
966 if (Number) O << Number;
967 O << Suffix;
968 }
aslc200b112008-08-16 12:57:46 +0000969
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000970 /// EmitLabel - Emit location label for internal use by Dwarf.
971 ///
972 void EmitLabel(DWLabel Label) const {
973 EmitLabel(Label.Tag, Label.Number);
974 }
975 void EmitLabel(const char *Tag, unsigned Number) const {
976 PrintLabelName(Tag, Number);
977 O << ":\n";
978 }
aslc200b112008-08-16 12:57:46 +0000979
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000980 /// EmitReference - Emit a reference to a label.
981 ///
Dan Gohman4fd77742007-09-28 15:43:33 +0000982 void EmitReference(DWLabel Label, bool IsPCRelative = false,
983 bool Force32Bit = false) const {
984 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985 }
986 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman4fd77742007-09-28 15:43:33 +0000987 bool IsPCRelative = false, bool Force32Bit = false) const {
988 PrintRelDirective(Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 PrintLabelName(Tag, Number);
aslc200b112008-08-16 12:57:46 +0000990
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
992 }
Dan Gohman4fd77742007-09-28 15:43:33 +0000993 void EmitReference(const std::string &Name, bool IsPCRelative = false,
994 bool Force32Bit = false) const {
995 PrintRelDirective(Force32Bit);
aslc200b112008-08-16 12:57:46 +0000996
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997 O << Name;
aslc200b112008-08-16 12:57:46 +0000998
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
1000 }
1001
1002 /// EmitDifference - Emit the difference between two labels. Some
1003 /// assemblers do not behave with absolute expressions with data directives,
1004 /// so there is an option (needsSet) to use an intermediary set expression.
1005 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
1006 bool IsSmall = false) {
1007 EmitDifference(LabelHi.Tag, LabelHi.Number,
1008 LabelLo.Tag, LabelLo.Number,
1009 IsSmall);
1010 }
1011 void EmitDifference(const char *TagHi, unsigned NumberHi,
1012 const char *TagLo, unsigned NumberLo,
1013 bool IsSmall = false) {
1014 if (TAI->needsSet()) {
1015 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +00001016 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001017 O << ",";
1018 PrintLabelName(TagHi, NumberHi);
1019 O << "-";
1020 PrintLabelName(TagLo, NumberLo);
1021 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001022
1023 PrintRelDirective(IsSmall);
Chris Lattnerb3876c72007-09-24 03:35:37 +00001024 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 ++SetCounter;
1026 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001027 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001028
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029 PrintLabelName(TagHi, NumberHi);
1030 O << "-";
1031 PrintLabelName(TagLo, NumberLo);
1032 }
1033 }
1034
1035 void EmitSectionOffset(const char* Label, const char* Section,
1036 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesen0ebb2432008-03-26 23:31:39 +00001037 bool IsSmall = false, bool isEH = false,
1038 bool useSet = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039 bool printAbsolute = false;
Dale Johannesen0ebb2432008-03-26 23:31:39 +00001040 if (isEH)
1041 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
1042 else
1043 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
1044
1045 if (TAI->needsSet() && useSet) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001046 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +00001047 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001048 O << ",";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001049 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001050
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001051 if (!printAbsolute) {
1052 O << "-";
1053 PrintLabelName(Section, SectionNumber);
aslc200b112008-08-16 12:57:46 +00001054 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001056
1057 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001058
Chris Lattnerb3876c72007-09-24 03:35:37 +00001059 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001060 ++SetCounter;
1061 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001062 PrintRelDirective(IsSmall, true);
aslc200b112008-08-16 12:57:46 +00001063
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001064 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001065
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001066 if (!printAbsolute) {
1067 O << "-";
1068 PrintLabelName(Section, SectionNumber);
1069 }
aslc200b112008-08-16 12:57:46 +00001070 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001071 }
aslc200b112008-08-16 12:57:46 +00001072
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1074 /// frame.
1075 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenf5a11532007-11-13 19:13:01 +00001076 const std::vector<MachineMove> &Moves, bool isEH) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001077 int stackGrowth =
1078 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1079 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00001080 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001081 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
1082
1083 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1084 const MachineMove &Move = Moves[i];
1085 unsigned LabelID = Move.getLabelID();
aslc200b112008-08-16 12:57:46 +00001086
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001087 if (LabelID) {
1088 LabelID = MMI->MappedLabel(LabelID);
aslc200b112008-08-16 12:57:46 +00001089
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001090 // Throw out move if the label is invalid.
1091 if (!LabelID) continue;
1092 }
aslc200b112008-08-16 12:57:46 +00001093
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001094 const MachineLocation &Dst = Move.getDestination();
1095 const MachineLocation &Src = Move.getSource();
aslc200b112008-08-16 12:57:46 +00001096
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001097 // Advance row if new location.
1098 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1099 Asm->EmitInt8(DW_CFA_advance_loc4);
1100 Asm->EOL("DW_CFA_advance_loc4");
1101 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1102 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00001103
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001104 BaseLabelID = LabelID;
1105 BaseLabel = "label";
1106 IsLocal = true;
1107 }
aslc200b112008-08-16 12:57:46 +00001108
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001109 // If advancing cfa.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001110 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1111 if (!Src.isReg()) {
1112 if (Src.getReg() == MachineLocation::VirtualFP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001113 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1114 Asm->EOL("DW_CFA_def_cfa_offset");
1115 } else {
1116 Asm->EmitInt8(DW_CFA_def_cfa);
1117 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001118 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001119 Asm->EOL("Register");
1120 }
aslc200b112008-08-16 12:57:46 +00001121
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001122 int Offset = -Src.getOffset();
aslc200b112008-08-16 12:57:46 +00001123
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001124 Asm->EmitULEB128Bytes(Offset);
1125 Asm->EOL("Offset");
1126 } else {
1127 assert(0 && "Machine move no supported yet.");
1128 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001129 } else if (Src.isReg() &&
1130 Src.getReg() == MachineLocation::VirtualFP) {
1131 if (Dst.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001132 Asm->EmitInt8(DW_CFA_def_cfa_register);
1133 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001134 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001135 Asm->EOL("Register");
1136 } else {
1137 assert(0 && "Machine move no supported yet.");
1138 }
1139 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001140 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001141 int Offset = Dst.getOffset() / stackGrowth;
aslc200b112008-08-16 12:57:46 +00001142
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143 if (Offset < 0) {
1144 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1145 Asm->EOL("DW_CFA_offset_extended_sf");
1146 Asm->EmitULEB128Bytes(Reg);
1147 Asm->EOL("Reg");
1148 Asm->EmitSLEB128Bytes(Offset);
1149 Asm->EOL("Offset");
1150 } else if (Reg < 64) {
1151 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng6181e062008-07-09 21:53:02 +00001152 if (VerboseAsm)
1153 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1154 else
1155 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001156 Asm->EmitULEB128Bytes(Offset);
1157 Asm->EOL("Offset");
1158 } else {
1159 Asm->EmitInt8(DW_CFA_offset_extended);
1160 Asm->EOL("DW_CFA_offset_extended");
1161 Asm->EmitULEB128Bytes(Reg);
1162 Asm->EOL("Reg");
1163 Asm->EmitULEB128Bytes(Offset);
1164 Asm->EOL("Offset");
1165 }
1166 }
1167 }
1168 }
1169
1170};
1171
1172//===----------------------------------------------------------------------===//
Devang Patel35a078f2009-01-12 22:54:42 +00001173/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001174///
1175class SrcLineInfo {
1176 unsigned Line; // Source line number.
1177 unsigned Column; // Source column.
1178 unsigned SourceID; // Source ID number.
1179 unsigned LabelID; // Label in code ID number.
1180public:
1181 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
1182 : Line(L), Column(C), SourceID(S), LabelID(I) {}
1183
1184 // Accessors
1185 unsigned getLine() const { return Line; }
1186 unsigned getColumn() const { return Column; }
1187 unsigned getSourceID() const { return SourceID; }
1188 unsigned getLabelID() const { return LabelID; }
1189};
1190
1191
1192//===----------------------------------------------------------------------===//
Devang Patel5f244e32009-01-05 22:35:52 +00001193/// SrcFileInfo - This class is used to track source information.
1194///
1195class SrcFileInfo {
1196 unsigned DirectoryID; // Directory ID number.
1197 std::string Name; // File name (not including directory.)
1198public:
1199 SrcFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
1200
1201 // Accessors
1202 unsigned getDirectoryID() const { return DirectoryID; }
1203 const std::string &getName() const { return Name; }
1204
1205 /// operator== - Used by UniqueVector to locate entry.
1206 ///
1207 bool operator==(const SourceFileInfo &SI) const {
1208 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
1209 }
1210
1211 /// operator< - Used by UniqueVector to locate entry.
1212 ///
1213 bool operator<(const SrcFileInfo &SI) const {
1214 return getDirectoryID() < SI.getDirectoryID() ||
1215 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
1216 }
1217};
1218
1219//===----------------------------------------------------------------------===//
Devang Patel4d1709e2009-01-08 02:33:41 +00001220/// DbgVariable - This class is used to track local variable information.
1221///
1222class DbgVariable {
1223private:
1224 DIVariable *Var; // Variable Descriptor.
1225 unsigned FrameIndex; // Variable frame index.
1226
1227public:
1228 DbgVariable(DIVariable *V, unsigned I) : Var(V), FrameIndex(I) {}
1229
1230 // Accessors.
1231 DIVariable *getVariable() const { return Var; }
1232 unsigned getFrameIndex() const { return FrameIndex; }
1233};
1234
1235//===----------------------------------------------------------------------===//
1236/// DbgScope - This class is used to track scope information.
1237///
1238class DbgScope {
1239private:
1240 DbgScope *Parent; // Parent to this scope.
1241 DIDescriptor *Desc; // Debug info descriptor for scope.
1242 // Either subprogram or block.
1243 unsigned StartLabelID; // Label ID of the beginning of scope.
1244 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel63c22f42009-01-10 02:42:49 +00001245 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
1246 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001247
1248public:
1249 DbgScope(DbgScope *P, DIDescriptor *D)
1250 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1251 {}
Devang Patela4162952009-01-12 18:48:36 +00001252 ~DbgScope() {
1253 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1254 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1255 }
Devang Patel4d1709e2009-01-08 02:33:41 +00001256
1257 // Accessors.
1258 DbgScope *getParent() const { return Parent; }
1259 DIDescriptor *getDesc() const { return Desc; }
1260 unsigned getStartLabelID() const { return StartLabelID; }
1261 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel63c22f42009-01-10 02:42:49 +00001262 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1263 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001264 void setStartLabelID(unsigned S) { StartLabelID = S; }
1265 void setEndLabelID(unsigned E) { EndLabelID = E; }
1266
1267 /// AddScope - Add a scope to the scope.
1268 ///
1269 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1270
1271 /// AddVariable - Add a variable to the scope.
1272 ///
1273 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1274};
1275
1276//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00001277/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001278///
1279class DwarfDebug : public Dwarf {
1280
1281private:
1282 //===--------------------------------------------------------------------===//
1283 // Attributes used to construct specific Dwarf sections.
1284 //
aslc200b112008-08-16 12:57:46 +00001285
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001286 /// CompileUnits - All the compile units involved in this build. The index
1287 /// of each entry in this vector corresponds to the sources in MMI.
1288 std::vector<CompileUnit *> CompileUnits;
Devang Patel7dd15a92009-01-08 17:19:22 +00001289 DenseMap<Value *, CompileUnit *> DW_CUs;
aslc200b112008-08-16 12:57:46 +00001290
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001291 /// AbbreviationsSet - Used to uniquely define abbreviations.
1292 ///
1293 FoldingSet<DIEAbbrev> AbbreviationsSet;
1294
1295 /// Abbreviations - A list of all the unique abbreviations in use.
1296 ///
1297 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +00001298
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001299 /// ValuesSet - Used to uniquely define values.
1300 ///
Devang Patel5f244e32009-01-05 22:35:52 +00001301 // Directories - Uniquing vector for directories.
1302 UniqueVector<std::string> Directories;
1303
1304 // SourceFiles - Uniquing vector for source files.
1305 UniqueVector<SrcFileInfo> SrcFiles;
1306
Devang Patel7dd15a92009-01-08 17:19:22 +00001307 // Lines - List of of source line correspondence.
1308 std::vector<SrcLineInfo> Lines;
1309
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001310 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +00001311
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312 /// Values - A list of all the unique values in use.
1313 ///
1314 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +00001315
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316 /// StringPool - A UniqueVector of strings used by indirect references.
1317 ///
1318 UniqueVector<std::string> StringPool;
1319
1320 /// UnitMap - Map debug information descriptor to compile unit.
1321 ///
1322 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
aslc200b112008-08-16 12:57:46 +00001323
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001324 /// SectionMap - Provides a unique id per text section.
1325 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001326 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +00001327
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001328 /// SectionSourceLines - Tracks line numbers per text section.
1329 ///
Devang Patel35a078f2009-01-12 22:54:42 +00001330 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001331
1332 /// didInitial - Flag to indicate if initial emission has been done.
1333 ///
1334 bool didInitial;
aslc200b112008-08-16 12:57:46 +00001335
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001336 /// shouldEmit - Flag to indicate if debug information should be emitted.
1337 ///
1338 bool shouldEmit;
1339
Devang Patel4d1709e2009-01-08 02:33:41 +00001340 // RootScope - Top level scope for the current function.
1341 //
1342 DbgScope *RootDbgScope;
1343
1344 // DbgScopeMap - Tracks the scopes in the current function.
1345 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
1346
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001347 struct FunctionDebugFrameInfo {
1348 unsigned Number;
1349 std::vector<MachineMove> Moves;
1350
1351 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001352 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353 };
1354
1355 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001356
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001357public:
aslc200b112008-08-16 12:57:46 +00001358
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1360 ///
1361 bool ShouldEmitDwarf() const { return shouldEmit; }
1362
1363 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001364 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001365 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1366 // Profile the node so that we can make it unique.
1367 FoldingSetNodeID ID;
1368 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001369
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 // Check the set for priors.
1371 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001372
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373 // If it's newly added.
1374 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001375 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001376 Abbreviations.push_back(&Abbrev);
1377 // Assign the vector position + 1 as its number.
1378 Abbrev.setNumber(Abbreviations.size());
1379 } else {
1380 // Assign existing abbreviation number.
1381 Abbrev.setNumber(InSet->getNumber());
1382 }
1383 }
1384
1385 /// NewString - Add a string to the constant pool and returns a label.
1386 ///
1387 DWLabel NewString(const std::string &String) {
1388 unsigned StringID = StringPool.insert(String);
1389 return DWLabel("string", StringID);
1390 }
aslc200b112008-08-16 12:57:46 +00001391
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001392 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1393 /// entry.
1394 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1395 DIEntry *Value;
aslc200b112008-08-16 12:57:46 +00001396
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001397 if (Entry) {
1398 FoldingSetNodeID ID;
1399 DIEntry::Profile(ID, Entry);
1400 void *Where;
1401 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001402
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001403 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001404
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001405 Value = new DIEntry(Entry);
1406 ValuesSet.InsertNode(Value, Where);
1407 } else {
1408 Value = new DIEntry(Entry);
1409 }
aslc200b112008-08-16 12:57:46 +00001410
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001411 Values.push_back(Value);
1412 return Value;
1413 }
aslc200b112008-08-16 12:57:46 +00001414
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001415 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1416 ///
1417 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1418 Value->Entry = Entry;
1419 // Add to values set if not already there. If it is, we merely have a
1420 // duplicate in the values list (no harm.)
1421 ValuesSet.GetOrInsertNode(Value);
1422 }
1423
1424 /// AddUInt - Add an unsigned integer attribute data and value.
1425 ///
1426 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1427 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1428
1429 FoldingSetNodeID ID;
1430 DIEInteger::Profile(ID, Integer);
1431 void *Where;
1432 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1433 if (!Value) {
1434 Value = new DIEInteger(Integer);
1435 ValuesSet.InsertNode(Value, Where);
1436 Values.push_back(Value);
1437 }
aslc200b112008-08-16 12:57:46 +00001438
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001439 Die->AddValue(Attribute, Form, Value);
1440 }
aslc200b112008-08-16 12:57:46 +00001441
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001442 /// AddSInt - Add an signed integer attribute data and value.
1443 ///
1444 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1445 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1446
1447 FoldingSetNodeID ID;
1448 DIEInteger::Profile(ID, (uint64_t)Integer);
1449 void *Where;
1450 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1451 if (!Value) {
1452 Value = new DIEInteger(Integer);
1453 ValuesSet.InsertNode(Value, Where);
1454 Values.push_back(Value);
1455 }
aslc200b112008-08-16 12:57:46 +00001456
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001457 Die->AddValue(Attribute, Form, Value);
1458 }
aslc200b112008-08-16 12:57:46 +00001459
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001460 /// AddString - Add a std::string attribute data and value.
1461 ///
1462 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1463 const std::string &String) {
1464 FoldingSetNodeID ID;
1465 DIEString::Profile(ID, String);
1466 void *Where;
1467 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1468 if (!Value) {
1469 Value = new DIEString(String);
1470 ValuesSet.InsertNode(Value, Where);
1471 Values.push_back(Value);
1472 }
aslc200b112008-08-16 12:57:46 +00001473
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001474 Die->AddValue(Attribute, Form, Value);
1475 }
aslc200b112008-08-16 12:57:46 +00001476
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001477 /// AddLabel - Add a Dwarf label attribute data and value.
1478 ///
1479 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1480 const DWLabel &Label) {
1481 FoldingSetNodeID ID;
1482 DIEDwarfLabel::Profile(ID, Label);
1483 void *Where;
1484 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1485 if (!Value) {
1486 Value = new DIEDwarfLabel(Label);
1487 ValuesSet.InsertNode(Value, Where);
1488 Values.push_back(Value);
1489 }
aslc200b112008-08-16 12:57:46 +00001490
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491 Die->AddValue(Attribute, Form, Value);
1492 }
aslc200b112008-08-16 12:57:46 +00001493
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001494 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1495 ///
1496 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1497 const std::string &Label) {
1498 FoldingSetNodeID ID;
1499 DIEObjectLabel::Profile(ID, Label);
1500 void *Where;
1501 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1502 if (!Value) {
1503 Value = new DIEObjectLabel(Label);
1504 ValuesSet.InsertNode(Value, Where);
1505 Values.push_back(Value);
1506 }
aslc200b112008-08-16 12:57:46 +00001507
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001508 Die->AddValue(Attribute, Form, Value);
1509 }
aslc200b112008-08-16 12:57:46 +00001510
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001511 /// AddSectionOffset - Add a section offset label attribute data and value.
1512 ///
1513 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1514 const DWLabel &Label, const DWLabel &Section,
1515 bool isEH = false, bool useSet = true) {
1516 FoldingSetNodeID ID;
1517 DIESectionOffset::Profile(ID, Label, Section);
1518 void *Where;
1519 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1520 if (!Value) {
1521 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1522 ValuesSet.InsertNode(Value, Where);
1523 Values.push_back(Value);
1524 }
aslc200b112008-08-16 12:57:46 +00001525
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001526 Die->AddValue(Attribute, Form, Value);
1527 }
aslc200b112008-08-16 12:57:46 +00001528
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001529 /// AddDelta - Add a label delta attribute data and value.
1530 ///
1531 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1532 const DWLabel &Hi, const DWLabel &Lo) {
1533 FoldingSetNodeID ID;
1534 DIEDelta::Profile(ID, Hi, Lo);
1535 void *Where;
1536 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1537 if (!Value) {
1538 Value = new DIEDelta(Hi, Lo);
1539 ValuesSet.InsertNode(Value, Where);
1540 Values.push_back(Value);
1541 }
aslc200b112008-08-16 12:57:46 +00001542
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001543 Die->AddValue(Attribute, Form, Value);
1544 }
aslc200b112008-08-16 12:57:46 +00001545
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001546 /// AddDIEntry - Add a DIE attribute data and value.
1547 ///
1548 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1549 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1550 }
1551
1552 /// AddBlock - Add block data.
1553 ///
1554 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1555 Block->ComputeSize(*this);
1556 FoldingSetNodeID ID;
1557 Block->Profile(ID);
1558 void *Where;
1559 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1560 if (!Value) {
1561 Value = Block;
1562 ValuesSet.InsertNode(Value, Where);
1563 Values.push_back(Value);
1564 } else {
Chris Lattner3de66892007-09-21 18:25:53 +00001565 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001566 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001567 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001568 }
aslc200b112008-08-16 12:57:46 +00001569
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001570 Die->AddValue(Attribute, Block->BestForm(), Value);
1571 }
1572
1573private:
1574
1575 /// AddSourceLine - Add location information to specified debug information
1576 /// entry.
1577 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1578 if (File && Line) {
1579 CompileUnit *FileUnit = FindCompileUnit(File);
1580 unsigned FileID = FileUnit->getID();
1581 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1582 AddUInt(Die, DW_AT_decl_line, 0, Line);
1583 }
1584 }
1585
Devang Patel5f244e32009-01-05 22:35:52 +00001586 /// AddSourceLine - Add location information to specified debug information
1587 /// entry.
Devang Patel4d1709e2009-01-08 02:33:41 +00001588 void AddSourceLine(DIE *Die, DIVariable *V) {
1589 unsigned FileID = 0;
1590 unsigned Line = V->getLineNumber();
1591 if (V->getVersion() < DIDescriptor::Version7) {
1592 // Version6 or earlier. Use compile unit info to get file id.
1593 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1594 FileID = Unit->getID();
1595 } else {
1596 // Version7 or newer, use filename and directory info from DIVariable
1597 // directly.
1598 unsigned DID = Directories.idFor(V->getDirectory());
1599 FileID = SrcFiles.idFor(SrcFileInfo(DID, V->getFilename()));
1600 }
1601 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1602 AddUInt(Die, DW_AT_decl_line, 0, Line);
1603 }
1604
1605 /// AddSourceLine - Add location information to specified debug information
1606 /// entry.
Devang Patel5f244e32009-01-05 22:35:52 +00001607 void AddSourceLine(DIE *Die, DIGlobal *G) {
1608 unsigned FileID = 0;
1609 unsigned Line = G->getLineNumber();
1610 if (G->getVersion() < DIDescriptor::Version7) {
1611 // Version6 or earlier. Use compile unit info to get file id.
1612 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1613 FileID = Unit->getID();
1614 } else {
1615 // Version7 or newer, use filename and directory info from DIGlobal
1616 // directly.
1617 unsigned DID = Directories.idFor(G->getDirectory());
1618 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1619 }
1620 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1621 AddUInt(Die, DW_AT_decl_line, 0, Line);
1622 }
1623
1624 void AddSourceLine(DIE *Die, DIType *G) {
1625 unsigned FileID = 0;
1626 unsigned Line = G->getLineNumber();
1627 if (G->getVersion() < DIDescriptor::Version7) {
1628 // Version6 or earlier. Use compile unit info to get file id.
1629 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1630 FileID = Unit->getID();
1631 } else {
1632 // Version7 or newer, use filename and directory info from DIGlobal
1633 // directly.
1634 unsigned DID = Directories.idFor(G->getDirectory());
1635 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1636 }
1637 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1638 AddUInt(Die, DW_AT_decl_line, 0, Line);
1639 }
1640
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001641 /// AddAddress - Add an address attribute to a die based on the location
1642 /// provided.
1643 void AddAddress(DIE *Die, unsigned Attribute,
1644 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001645 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001646 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001647
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001648 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001649 if (Reg < 32) {
1650 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1651 } else {
1652 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1653 AddUInt(Block, 0, DW_FORM_udata, Reg);
1654 }
1655 } else {
1656 if (Reg < 32) {
1657 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1658 } else {
1659 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1660 AddUInt(Block, 0, DW_FORM_udata, Reg);
1661 }
1662 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1663 }
aslc200b112008-08-16 12:57:46 +00001664
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001665 AddBlock(Die, Attribute, 0, Block);
1666 }
aslc200b112008-08-16 12:57:46 +00001667
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001668 /// AddBasicType - Add a new basic type attribute to the specified entity.
1669 ///
1670 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1671 const std::string &Name,
1672 unsigned Encoding, unsigned Size) {
aslc200b112008-08-16 12:57:46 +00001673
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001674 DIE Buffer(DW_TAG_base_type);
1675 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1676 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1677 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf49e13d2009-01-05 17:44:11 +00001678 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1679 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001680 }
aslc200b112008-08-16 12:57:46 +00001681
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001682 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1683 ///
1684 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001685 DIE Buffer(DW_TAG_pointer_type);
Dan Gohmancfb72b22007-09-27 23:12:31 +00001686 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001687 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelbbca50b2009-01-05 17:45:59 +00001688 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1689 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001690 }
aslc200b112008-08-16 12:57:46 +00001691
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001692 /// AddType - Add a new type attribute to the specified entity.
1693 ///
1694 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1695 if (!TyDesc) {
1696 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
1697 } else {
1698 // Check for pre-existence.
1699 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
aslc200b112008-08-16 12:57:46 +00001700
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001701 // If it exists then use the existing value.
1702 if (Slot) {
1703 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1704 return;
1705 }
aslc200b112008-08-16 12:57:46 +00001706
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001707 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1708 // FIXME - Not sure why programs and variables are coming through here.
1709 // Short cut for handling subprogram types (not really a TyDesc.)
1710 AddPointerType(Entity, Unit, SubprogramTy->getName());
1711 } else if (GlobalVariableDesc *GlobalTy =
1712 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1713 // FIXME - Not sure why programs and variables are coming through here.
1714 // Short cut for handling global variable types (not really a TyDesc.)
1715 AddPointerType(Entity, Unit, GlobalTy->getName());
aslc200b112008-08-16 12:57:46 +00001716 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001717 // Set up proxy.
1718 Slot = NewDIEntry();
aslc200b112008-08-16 12:57:46 +00001719
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001720 // Construct type.
1721 DIE Buffer(DW_TAG_base_type);
1722 ConstructType(Buffer, TyDesc, Unit);
aslc200b112008-08-16 12:57:46 +00001723
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001724 // Add debug information entry to entity and unit.
1725 DIE *Die = Unit->AddDie(Buffer);
1726 SetDIEntry(Slot, Die);
1727 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1728 }
1729 }
1730 }
aslc200b112008-08-16 12:57:46 +00001731
Devang Patel4a4cbe72009-01-05 21:47:57 +00001732 /// AddType - Add a new type attribute to the specified entity.
1733 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
1734 if (Ty.isNull()) {
1735 AddBasicType(Entity, DW_Unit, "", DW_ATE_signed, sizeof(int32_t));
1736 return;
1737 }
1738
1739 // Check for pre-existence.
1740 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1741 // If it exists then use the existing value.
1742 if (Slot) {
1743 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1744 return;
1745 }
1746
1747 // Set up proxy.
1748 Slot = NewDIEntry();
1749
1750 // Construct type.
1751 DIE Buffer(DW_TAG_base_type);
1752 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1753 ConstructTypeDIE(DW_Unit, Buffer, BT);
1754 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1755 ConstructTypeDIE(DW_Unit, Buffer, DT);
1756 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1757 ConstructTypeDIE(DW_Unit, Buffer, CT);
1758
1759 // Add debug information entry to entity and unit.
1760 DIE *Die = DW_Unit->AddDie(Buffer);
1761 SetDIEntry(Slot, Die);
1762 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1763 }
1764
Devang Patel46d13752009-01-05 19:07:53 +00001765 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1766 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1767 DIBasicType *BTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001768
1769 // Get core information.
1770 const std::string &Name = BTy->getName();
1771 Buffer.setTag(DW_TAG_base_type);
1772 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy->getEncoding());
1773 // Add name if not anonymous or intermediate type.
1774 if (!Name.empty())
1775 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1776 uint64_t Size = BTy->getSizeInBits() >> 3;
1777 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1778 }
1779
Devang Patel46d13752009-01-05 19:07:53 +00001780 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1781 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1782 DIDerivedType *DTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001783
1784 // Get core information.
1785 const std::string &Name = DTy->getName();
1786 uint64_t Size = DTy->getSizeInBits() >> 3;
1787 unsigned Tag = DTy->getTag();
1788 // FIXME - Workaround for templates.
1789 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1790
1791 Buffer.setTag(Tag);
1792 // Map to main type, void will not have a type.
1793 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001794 AddType(DW_Unit, &Buffer, FromTy);
Devang Patelfc187162009-01-05 17:57:47 +00001795
1796 // Add name if not anonymous or intermediate type.
1797 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1798
1799 // Add size if non-zero (derived types might be zero-sized.)
1800 if (Size)
1801 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1802
1803 // Add source line info if available and TyDesc is not a forward
1804 // declaration.
1805 // FIXME - Enable this. if (!DTy->isForwardDecl())
1806 // FIXME - Enable this. AddSourceLine(&Buffer, *DTy);
1807 }
1808
Devang Patel30c01372009-01-05 19:55:51 +00001809 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1810 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1811 DICompositeType *CTy) {
1812
1813 // Get core information.
1814 const std::string &Name = CTy->getName();
1815 uint64_t Size = CTy->getSizeInBits() >> 3;
1816 unsigned Tag = CTy->getTag();
1817 switch (Tag) {
1818 case DW_TAG_vector_type:
1819 case DW_TAG_array_type:
1820 ConstructArrayTypeDIE(DW_Unit, Buffer, CTy);
1821 break;
1822 //FIXME - Enable this.
1823 // case DW_TAG_enumeration_type:
1824 // DIArray Elements = CTy->getTypeArray();
1825 // // Add enumerators to enumeration type.
1826 // for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i)
1827 // ConstructEnumTypeDIE(Buffer, &Elements.getElement(i));
1828 // break;
1829 case DW_TAG_subroutine_type:
1830 {
1831 // Add prototype flag.
1832 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1833 DIArray Elements = CTy->getTypeArray();
1834 // Add return type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001835 DIDescriptor RTy = Elements.getElement(0);
1836 if (DIBasicType *BT = dyn_cast<DIBasicType>(&RTy))
1837 AddType(DW_Unit, &Buffer, *BT);
1838 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&RTy))
1839 AddType(DW_Unit, &Buffer, *DT);
1840 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&RTy))
1841 AddType(DW_Unit, &Buffer, *CT);
1842
1843 //AddType(DW_Unit, &Buffer, Elements.getElement(0));
Devang Patel30c01372009-01-05 19:55:51 +00001844 // Add arguments.
1845 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1846 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001847 DIDescriptor Ty = Elements.getElement(i);
1848 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1849 AddType(DW_Unit, &Buffer, *BT);
1850 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1851 AddType(DW_Unit, &Buffer, *DT);
1852 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1853 AddType(DW_Unit, &Buffer, *CT);
Devang Patel30c01372009-01-05 19:55:51 +00001854 Buffer.AddChild(Arg);
1855 }
1856 }
1857 break;
1858 case DW_TAG_structure_type:
1859 case DW_TAG_union_type:
1860 {
1861 // Add elements to structure type.
1862 DIArray Elements = CTy->getTypeArray();
1863 // Add elements to structure type.
1864 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1865 DIDescriptor Element = Elements.getElement(i);
1866 if (DISubprogram *SP = dyn_cast<DISubprogram>(&Element))
1867 ConstructFieldTypeDIE(DW_Unit, Buffer, SP);
1868 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Element))
1869 ConstructFieldTypeDIE(DW_Unit, Buffer, DT);
1870 else if (DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(&Element))
1871 ConstructFieldTypeDIE(DW_Unit, Buffer, GV);
1872 }
1873 }
1874 break;
1875 default:
1876 break;
1877 }
1878
1879 // Add name if not anonymous or intermediate type.
1880 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1881
1882 // Add size if non-zero (derived types might be zero-sized.)
1883 if (Size)
1884 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1885 else {
1886 // Add zero size even if it is not a forward declaration.
1887 // FIXME - Enable this.
1888 // if (!CTy->isDefinition())
1889 // AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1890 // else
1891 // AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1892 }
1893
1894 // Add source line info if available and TyDesc is not a forward
1895 // declaration.
1896 // FIXME - Enable this.
1897 // if (CTy->isForwardDecl())
1898 // AddSourceLine(&Buffer, *CTy);
1899 }
1900
Devang Patel6fb54132009-01-05 18:33:01 +00001901 // ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1902 void ConstructSubrangeDIE (DIE &Buffer, DISubrange *SR, DIE *IndexTy) {
1903 int64_t L = SR->getLo();
1904 int64_t H = SR->getHi();
1905 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1906 if (L != H) {
1907 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1908 if (L)
1909 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1910 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
1911 }
1912 Buffer.AddChild(DW_Subrange);
1913 }
1914
1915 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1916 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1917 DICompositeType *CTy) {
1918 Buffer.setTag(DW_TAG_array_type);
1919 if (CTy->getTag() == DW_TAG_vector_type)
1920 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1921
1922 DIArray Elements = CTy->getTypeArray();
1923 // FIXME - Enable this.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001924 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel6fb54132009-01-05 18:33:01 +00001925
1926 // Construct an anonymous type for index type.
1927 DIE IdxBuffer(DW_TAG_base_type);
1928 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1929 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1930 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1931
1932 // Add subranges to array type.
1933 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel30c01372009-01-05 19:55:51 +00001934 DIDescriptor Element = Elements.getElement(i);
1935 if (DISubrange *SR = dyn_cast<DISubrange>(&Element))
1936 ConstructSubrangeDIE(Buffer, SR, IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +00001937 }
1938 }
1939
Devang Patela566e812009-01-05 18:38:38 +00001940 /// ConstructEnumTypeDIE - Construct enum type DIE from
1941 /// DIEnumerator.
Devang Patel30c01372009-01-05 19:55:51 +00001942 void ConstructEnumTypeDIE(CompileUnit *DW_Unit,
1943 DIE &Buffer, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +00001944
1945 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1946 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1947 int64_t Value = ETy->getEnumValue();
1948 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1949 Buffer.AddChild(Enumerator);
1950 }
Devang Patel6fb54132009-01-05 18:33:01 +00001951
Devang Patel526b01d2009-01-05 18:59:44 +00001952 /// ConstructFieldTypeDIE - Construct variable DIE for a struct field.
1953 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1954 DIE &Buffer, DIGlobalVariable *V) {
1955
1956 DIE *VariableDie = new DIE(DW_TAG_variable);
1957 const std::string &LinkageName = V->getLinkageName();
1958 if (!LinkageName.empty())
1959 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1960 LinkageName);
1961 // FIXME - Enable this. AddSourceLine(VariableDie, V);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001962 AddType(DW_Unit, VariableDie, V->getType());
Devang Patel526b01d2009-01-05 18:59:44 +00001963 if (!V->isLocalToUnit())
1964 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1965 AddUInt(VariableDie, DW_AT_declaration, DW_FORM_flag, 1);
1966 Buffer.AddChild(VariableDie);
1967 }
1968
1969 /// ConstructFieldTypeDIE - Construct subprogram DIE for a struct field.
1970 void ConstructFieldTypeDIE(CompileUnit *DW_Unit,
1971 DIE &Buffer, DISubprogram *SP,
1972 bool IsConstructor = false) {
1973 DIE *Method = new DIE(DW_TAG_subprogram);
1974 AddString(Method, DW_AT_name, DW_FORM_string, SP->getName());
1975 const std::string &LinkageName = SP->getLinkageName();
1976 if (!LinkageName.empty())
1977 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1978 // FIXME - Enable this. AddSourceLine(Method, SP);
1979
1980 DICompositeType MTy = SP->getType();
1981 DIArray Args = MTy.getTypeArray();
1982
1983 // Add Return Type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001984 if (!IsConstructor) {
1985 DIDescriptor Ty = Args.getElement(0);
1986 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1987 AddType(DW_Unit, Method, *BT);
1988 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
1989 AddType(DW_Unit, Method, *DT);
1990 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
1991 AddType(DW_Unit, Method, *CT);
1992 }
Devang Patel526b01d2009-01-05 18:59:44 +00001993
1994 // Add arguments.
1995 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1996 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001997 DIDescriptor Ty = Args.getElement(i);
1998 if (DIBasicType *BT = dyn_cast<DIBasicType>(&Ty))
1999 AddType(DW_Unit, Method, *BT);
2000 else if (DIDerivedType *DT = dyn_cast<DIDerivedType>(&Ty))
2001 AddType(DW_Unit, Method, *DT);
2002 else if (DICompositeType *CT = dyn_cast<DICompositeType>(&Ty))
2003 AddType(DW_Unit, Method, *CT);
Devang Patel526b01d2009-01-05 18:59:44 +00002004 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
2005 Method->AddChild(Arg);
2006 }
2007
2008 if (!SP->isLocalToUnit())
2009 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
2010 Buffer.AddChild(Method);
2011 }
2012
2013 /// COnstructFieldTypeDIE - Construct derived type DIE for a struct field.
2014 void ConstructFieldTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
2015 DIDerivedType *DTy) {
2016 unsigned Tag = DTy->getTag();
2017 DIE *MemberDie = new DIE(Tag);
2018 if (!DTy->getName().empty())
2019 AddString(MemberDie, DW_AT_name, DW_FORM_string, DTy->getName());
2020 // FIXME - Enable this. AddSourceLine(MemberDie, DTy);
2021
2022 DIType FromTy = DTy->getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00002023 AddType(DW_Unit, MemberDie, FromTy);
Devang Patel526b01d2009-01-05 18:59:44 +00002024
2025 uint64_t Size = DTy->getSizeInBits();
2026 uint64_t Offset = DTy->getOffsetInBits();
2027
2028 // FIXME Handle bitfields
2029
2030 // Add size.
2031 AddUInt(MemberDie, DW_AT_bit_size, 0, Size);
2032 // Add computation for offset.
2033 DIEBlock *Block = new DIEBlock();
2034 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
2035 AddUInt(Block, 0, DW_FORM_udata, Offset >> 3);
2036 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
2037
2038 // FIXME Handle DW_AT_accessibility.
2039
2040 Buffer.AddChild(MemberDie);
2041 }
2042
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002043 /// ConstructType - Adds all the required attributes to the type.
2044 ///
2045 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
2046 // Get core information.
2047 const std::string &Name = TyDesc->getName();
2048 uint64_t Size = TyDesc->getSize() >> 3;
aslc200b112008-08-16 12:57:46 +00002049
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002050 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
2051 // Fundamental types like int, float, bool
2052 Buffer.setTag(DW_TAG_base_type);
2053 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
2054 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
2055 // Fetch tag.
2056 unsigned Tag = DerivedTy->getTag();
2057 // FIXME - Workaround for templates.
2058 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
aslc200b112008-08-16 12:57:46 +00002059 // Pointers, typedefs et al.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002060 Buffer.setTag(Tag);
2061 // Map to main type, void will not have a type.
2062 if (TypeDesc *FromTy = DerivedTy->getFromType())
2063 AddType(&Buffer, FromTy, Unit);
2064 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
2065 // Fetch tag.
2066 unsigned Tag = CompTy->getTag();
aslc200b112008-08-16 12:57:46 +00002067
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002068 // Set tag accordingly.
2069 if (Tag == DW_TAG_vector_type)
2070 Buffer.setTag(DW_TAG_array_type);
aslc200b112008-08-16 12:57:46 +00002071 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002072 Buffer.setTag(Tag);
2073
2074 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
aslc200b112008-08-16 12:57:46 +00002075
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002076 switch (Tag) {
2077 case DW_TAG_vector_type:
2078 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
2079 // Fall thru
2080 case DW_TAG_array_type: {
2081 // Add element type.
2082 if (TypeDesc *FromTy = CompTy->getFromType())
2083 AddType(&Buffer, FromTy, Unit);
aslc200b112008-08-16 12:57:46 +00002084
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002085 // Don't emit size attribute.
2086 Size = 0;
aslc200b112008-08-16 12:57:46 +00002087
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002088 // Construct an anonymous type for index type.
Devang Patelf49e13d2009-01-05 17:44:11 +00002089 DIE Buffer(DW_TAG_base_type);
2090 AddUInt(&Buffer, DW_AT_byte_size, 0, sizeof(int32_t));
2091 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
2092 DIE *IndexTy = Unit->AddDie(Buffer);
aslc200b112008-08-16 12:57:46 +00002093
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002094 // Add subranges to array type.
Evan Chengc7efea32008-12-09 17:56:30 +00002095 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002096 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
2097 int64_t Lo = SRD->getLo();
2098 int64_t Hi = SRD->getHi();
2099 DIE *Subrange = new DIE(DW_TAG_subrange_type);
aslc200b112008-08-16 12:57:46 +00002100
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002101 // If a range is available.
2102 if (Lo != Hi) {
2103 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
2104 // Only add low if non-zero.
2105 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
2106 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
2107 }
aslc200b112008-08-16 12:57:46 +00002108
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002109 Buffer.AddChild(Subrange);
2110 }
2111 break;
2112 }
2113 case DW_TAG_structure_type:
2114 case DW_TAG_union_type: {
2115 // Add elements to structure type.
Evan Chengc7efea32008-12-09 17:56:30 +00002116 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002117 DebugInfoDesc *Element = Elements[i];
aslc200b112008-08-16 12:57:46 +00002118
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002119 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
2120 // Add field or base class.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002121 unsigned Tag = MemberDesc->getTag();
aslc200b112008-08-16 12:57:46 +00002122
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002123 // Extract the basic information.
2124 const std::string &Name = MemberDesc->getName();
2125 uint64_t Size = MemberDesc->getSize();
2126 uint64_t Align = MemberDesc->getAlign();
2127 uint64_t Offset = MemberDesc->getOffset();
aslc200b112008-08-16 12:57:46 +00002128
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002129 // Construct member debug information entry.
2130 DIE *Member = new DIE(Tag);
aslc200b112008-08-16 12:57:46 +00002131
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002132 // Add name if not "".
2133 if (!Name.empty())
2134 AddString(Member, DW_AT_name, DW_FORM_string, Name);
Evan Chengc7efea32008-12-09 17:56:30 +00002135
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002136 // Add location if available.
2137 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
aslc200b112008-08-16 12:57:46 +00002138
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002139 // Most of the time the field info is the same as the members.
2140 uint64_t FieldSize = Size;
2141 uint64_t FieldAlign = Align;
2142 uint64_t FieldOffset = Offset;
aslc200b112008-08-16 12:57:46 +00002143
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002144 // Set the member type.
2145 TypeDesc *FromTy = MemberDesc->getFromType();
2146 AddType(Member, FromTy, Unit);
aslc200b112008-08-16 12:57:46 +00002147
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002148 // Walk up typedefs until a real size is found.
2149 while (FromTy) {
2150 if (FromTy->getTag() != DW_TAG_typedef) {
2151 FieldSize = FromTy->getSize();
Devang Patel105a08a2008-12-23 21:55:38 +00002152 FieldAlign = FromTy->getAlign();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002153 break;
2154 }
aslc200b112008-08-16 12:57:46 +00002155
Dan Gohman53491e92007-07-23 20:24:29 +00002156 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002157 }
aslc200b112008-08-16 12:57:46 +00002158
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002159 // Unless we have a bit field.
2160 if (Tag == DW_TAG_member && FieldSize != Size) {
2161 // Construct the alignment mask.
2162 uint64_t AlignMask = ~(FieldAlign - 1);
2163 // Determine the high bit + 1 of the declared size.
2164 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
2165 // Work backwards to determine the base offset of the field.
2166 FieldOffset = HiMark - FieldSize;
2167 // Now normalize offset to the field.
2168 Offset -= FieldOffset;
aslc200b112008-08-16 12:57:46 +00002169
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002170 // Maybe we need to work from the other end.
2171 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
aslc200b112008-08-16 12:57:46 +00002172
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002173 // Add size and offset.
2174 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
2175 AddUInt(Member, DW_AT_bit_size, 0, Size);
2176 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
2177 }
aslc200b112008-08-16 12:57:46 +00002178
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002179 // Add computation for offset.
2180 DIEBlock *Block = new DIEBlock();
2181 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
2182 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
2183 AddBlock(Member, DW_AT_data_member_location, 0, Block);
2184
2185 // Add accessibility (public default unless is base class.
2186 if (MemberDesc->isProtected()) {
2187 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
2188 } else if (MemberDesc->isPrivate()) {
2189 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
2190 } else if (Tag == DW_TAG_inheritance) {
2191 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
2192 }
aslc200b112008-08-16 12:57:46 +00002193
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002194 Buffer.AddChild(Member);
2195 } else if (GlobalVariableDesc *StaticDesc =
2196 dyn_cast<GlobalVariableDesc>(Element)) {
2197 // Add static member.
aslc200b112008-08-16 12:57:46 +00002198
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002199 // Construct member debug information entry.
2200 DIE *Static = new DIE(DW_TAG_variable);
aslc200b112008-08-16 12:57:46 +00002201
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002202 // Add name and mangled name.
2203 const std::string &Name = StaticDesc->getName();
2204 const std::string &LinkageName = StaticDesc->getLinkageName();
2205 AddString(Static, DW_AT_name, DW_FORM_string, Name);
2206 if (!LinkageName.empty()) {
2207 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
2208 LinkageName);
2209 }
aslc200b112008-08-16 12:57:46 +00002210
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002211 // Add location.
2212 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
aslc200b112008-08-16 12:57:46 +00002213
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002214 // Add type.
2215 if (TypeDesc *StaticTy = StaticDesc->getType())
2216 AddType(Static, StaticTy, Unit);
aslc200b112008-08-16 12:57:46 +00002217
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002218 // Add flags.
2219 if (!StaticDesc->isStatic())
2220 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
2221 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00002222
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002223 Buffer.AddChild(Static);
2224 } else if (SubprogramDesc *MethodDesc =
2225 dyn_cast<SubprogramDesc>(Element)) {
2226 // Add member function.
aslc200b112008-08-16 12:57:46 +00002227
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002228 // Construct member debug information entry.
2229 DIE *Method = new DIE(DW_TAG_subprogram);
aslc200b112008-08-16 12:57:46 +00002230
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002231 // Add name and mangled name.
2232 const std::string &Name = MethodDesc->getName();
2233 const std::string &LinkageName = MethodDesc->getLinkageName();
aslc200b112008-08-16 12:57:46 +00002234
2235 AddString(Method, DW_AT_name, DW_FORM_string, Name);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002236 bool IsCTor = TyDesc->getName() == Name;
aslc200b112008-08-16 12:57:46 +00002237
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002238 if (!LinkageName.empty()) {
2239 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
2240 LinkageName);
2241 }
aslc200b112008-08-16 12:57:46 +00002242
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002243 // Add location.
2244 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
aslc200b112008-08-16 12:57:46 +00002245
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002246 // Add type.
2247 if (CompositeTypeDesc *MethodTy =
2248 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
2249 // Get argument information.
2250 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
aslc200b112008-08-16 12:57:46 +00002251
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002252 // If not a ctor.
2253 if (!IsCTor) {
2254 // Add return type.
2255 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
2256 }
aslc200b112008-08-16 12:57:46 +00002257
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002258 // Add arguments.
Evan Chengc7efea32008-12-09 17:56:30 +00002259 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002260 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2261 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
2262 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
2263 Method->AddChild(Arg);
2264 }
2265 }
2266
2267 // Add flags.
2268 if (!MethodDesc->isStatic())
2269 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
2270 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00002271
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002272 Buffer.AddChild(Method);
2273 }
2274 }
2275 break;
2276 }
2277 case DW_TAG_enumeration_type: {
2278 // Add enumerators to enumeration type.
Evan Chengc7efea32008-12-09 17:56:30 +00002279 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002280 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
2281 const std::string &Name = ED->getName();
2282 int64_t Value = ED->getValue();
2283 DIE *Enumerator = new DIE(DW_TAG_enumerator);
2284 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
2285 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
2286 Buffer.AddChild(Enumerator);
2287 }
2288
2289 break;
2290 }
2291 case DW_TAG_subroutine_type: {
2292 // Add prototype flag.
2293 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
2294 // Add return type.
2295 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
aslc200b112008-08-16 12:57:46 +00002296
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002297 // Add arguments.
Evan Chengc7efea32008-12-09 17:56:30 +00002298 for (unsigned i = 1, N = Elements.size(); i < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002299 DIE *Arg = new DIE(DW_TAG_formal_parameter);
2300 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
2301 Buffer.AddChild(Arg);
2302 }
aslc200b112008-08-16 12:57:46 +00002303
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002304 break;
2305 }
2306 default: break;
2307 }
2308 }
aslc200b112008-08-16 12:57:46 +00002309
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002310 // Add name if not anonymous or intermediate type.
2311 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Evan Chengc7efea32008-12-09 17:56:30 +00002312
Evan Chengb2fc7112008-12-10 00:15:44 +00002313 // Add size if non-zero (derived types might be zero-sized.)
2314 if (Size)
2315 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
2316 else if (isa<CompositeTypeDesc>(TyDesc)) {
2317 // If TyDesc is a composite type, then add size even if it's zero unless
2318 // it's a forward declaration.
2319 if (TyDesc->isForwardDecl())
2320 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
2321 else
2322 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
2323 }
2324
2325 // Add source line info if available and TyDesc is not a forward
2326 // declaration.
2327 if (!TyDesc->isForwardDecl())
2328 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002329 }
2330
2331 /// NewCompileUnit - Create new compile unit and it's debug information entry.
2332 ///
2333 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
2334 // Construct debug information entry.
2335 DIE *Die = new DIE(DW_TAG_compile_unit);
Argiris Kirtzidis03449652008-06-18 19:27:37 +00002336 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2337 DWLabel("section_line", 0), DWLabel("section_line", 0), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002338 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
2339 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
2340 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
Devang Patel6bcf9822008-12-12 21:57:54 +00002341 if (!UnitDesc->getDirectory().empty())
2342 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
aslc200b112008-08-16 12:57:46 +00002343
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002344 // Construct compile unit.
2345 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
aslc200b112008-08-16 12:57:46 +00002346
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002347 // Add Unit to compile unit map.
2348 DescToUnitMap[UnitDesc] = Unit;
aslc200b112008-08-16 12:57:46 +00002349
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002350 return Unit;
2351 }
2352
2353 /// GetBaseCompileUnit - Get the main compile unit.
2354 ///
2355 CompileUnit *GetBaseCompileUnit() const {
2356 CompileUnit *Unit = CompileUnits[0];
2357 assert(Unit && "Missing compile unit.");
2358 return Unit;
2359 }
2360
2361 /// FindCompileUnit - Get the compile unit for the given descriptor.
2362 ///
2363 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
2364 CompileUnit *Unit = DescToUnitMap[UnitDesc];
2365 assert(Unit && "Missing compile unit.");
2366 return Unit;
2367 }
2368
Devang Patel5f244e32009-01-05 22:35:52 +00002369 /// FindCompileUnit - Get the compile unit for the given descriptor.
2370 ///
2371 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
2372 CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
2373 assert(DW_Unit && "Missing compile unit.");
2374 return DW_Unit;
2375 }
2376
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002377 /// NewGlobalVariable - Add a new global variable DIE.
2378 ///
2379 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
2380 // Get the compile unit context.
2381 CompileUnitDesc *UnitDesc =
2382 static_cast<CompileUnitDesc *>(GVD->getContext());
2383 CompileUnit *Unit = GetBaseCompileUnit();
2384
2385 // Check for pre-existence.
2386 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
2387 if (Slot) return Slot;
aslc200b112008-08-16 12:57:46 +00002388
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002389 // Get the global variable itself.
2390 GlobalVariable *GV = GVD->getGlobalVariable();
2391
2392 const std::string &Name = GVD->getName();
2393 const std::string &FullName = GVD->getFullName();
2394 const std::string &LinkageName = GVD->getLinkageName();
2395 // Create the global's variable DIE.
2396 DIE *VariableDie = new DIE(DW_TAG_variable);
2397 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
2398 if (!LinkageName.empty()) {
2399 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
2400 LinkageName);
2401 }
2402 AddType(VariableDie, GVD->getType(), Unit);
2403 if (!GVD->isStatic())
2404 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00002405
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002406 // Add source line info if available.
2407 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
aslc200b112008-08-16 12:57:46 +00002408
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002409 // Add address.
2410 DIEBlock *Block = new DIEBlock();
2411 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2412 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
2413 AddBlock(VariableDie, DW_AT_location, 0, Block);
aslc200b112008-08-16 12:57:46 +00002414
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002415 // Add to map.
2416 Slot = VariableDie;
aslc200b112008-08-16 12:57:46 +00002417
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002418 // Add to context owner.
2419 Unit->getDie()->AddChild(VariableDie);
aslc200b112008-08-16 12:57:46 +00002420
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002421 // Expose as global.
2422 // FIXME - need to check external flag.
2423 Unit->AddGlobal(FullName, VariableDie);
aslc200b112008-08-16 12:57:46 +00002424
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002425 return VariableDie;
2426 }
2427
2428 /// NewSubprogram - Add a new subprogram DIE.
2429 ///
2430 DIE *NewSubprogram(SubprogramDesc *SPD) {
2431 // Get the compile unit context.
2432 CompileUnitDesc *UnitDesc =
2433 static_cast<CompileUnitDesc *>(SPD->getContext());
2434 CompileUnit *Unit = GetBaseCompileUnit();
2435
2436 // Check for pre-existence.
2437 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
2438 if (Slot) return Slot;
aslc200b112008-08-16 12:57:46 +00002439
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002440 // Gather the details (simplify add attribute code.)
2441 const std::string &Name = SPD->getName();
2442 const std::string &FullName = SPD->getFullName();
2443 const std::string &LinkageName = SPD->getLinkageName();
aslc200b112008-08-16 12:57:46 +00002444
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002445 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
2446 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
2447 if (!LinkageName.empty()) {
2448 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
2449 LinkageName);
2450 }
2451 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
2452 if (!SPD->isStatic())
2453 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
2454 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
aslc200b112008-08-16 12:57:46 +00002455
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002456 // Add source line info if available.
2457 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
2458
2459 // Add to map.
2460 Slot = SubprogramDie;
aslc200b112008-08-16 12:57:46 +00002461
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002462 // Add to context owner.
2463 Unit->getDie()->AddChild(SubprogramDie);
aslc200b112008-08-16 12:57:46 +00002464
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002465 // Expose as global.
2466 Unit->AddGlobal(FullName, SubprogramDie);
aslc200b112008-08-16 12:57:46 +00002467
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002468 return SubprogramDie;
2469 }
2470
2471 /// NewScopeVariable - Create a new scope variable.
2472 ///
2473 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
2474 // Get the descriptor.
2475 VariableDesc *VD = DV->getDesc();
2476
2477 // Translate tag to proper Dwarf tag. The result variable is dropped for
2478 // now.
2479 unsigned Tag;
2480 switch (VD->getTag()) {
2481 case DW_TAG_return_variable: return NULL;
2482 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2483 case DW_TAG_auto_variable: // fall thru
2484 default: Tag = DW_TAG_variable; break;
2485 }
2486
2487 // Define variable debug information entry.
2488 DIE *VariableDie = new DIE(Tag);
2489 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2490
2491 // Add source line info if available.
2492 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
aslc200b112008-08-16 12:57:46 +00002493
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002494 // Add variable type.
aslc200b112008-08-16 12:57:46 +00002495 AddType(VariableDie, VD->getType(), Unit);
2496
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002497 // Add variable address.
2498 MachineLocation Location;
Evan Cheng38948832008-01-31 03:37:28 +00002499 Location.set(RI->getFrameRegister(*MF),
2500 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002501 AddAddress(VariableDie, DW_AT_location, Location);
2502
2503 return VariableDie;
2504 }
2505
Devang Patel4d1709e2009-01-08 02:33:41 +00002506 /// NewScopeVariable - Create a new scope variable.
2507 ///
2508 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
2509 // Get the descriptor.
2510 DIVariable *VD = DV->getVariable();
2511
2512 // Translate tag to proper Dwarf tag. The result variable is dropped for
2513 // now.
2514 unsigned Tag;
2515 switch (VD->getTag()) {
2516 case DW_TAG_return_variable: return NULL;
2517 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2518 case DW_TAG_auto_variable: // fall thru
2519 default: Tag = DW_TAG_variable; break;
2520 }
2521
2522 // Define variable debug information entry.
2523 DIE *VariableDie = new DIE(Tag);
2524 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
2525
2526 // Add source line info if available.
2527 AddSourceLine(VariableDie, VD);
2528
2529 // Add variable type.
2530 AddType(Unit, VariableDie, VD->getType());
2531
2532 // Add variable address.
2533 MachineLocation Location;
2534 Location.set(RI->getFrameRegister(*MF),
2535 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
2536 AddAddress(VariableDie, DW_AT_location, Location);
2537
2538 return VariableDie;
2539 }
2540
Devang Patel4d1709e2009-01-08 02:33:41 +00002541 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2542 ///
2543 DbgScope *getOrCreateScope(GlobalVariable *V) {
2544 DbgScope *&Slot = DbgScopeMap[V];
2545 if (!Slot) {
2546 // FIXME - breaks down when the context is an inlined function.
2547 DIDescriptor ParentDesc;
Devang Pateldd49fbb2009-01-10 02:34:18 +00002548 DIDescriptor *DB = new DIBlock(V);
Devang Patel4d1709e2009-01-08 02:33:41 +00002549 if (DIBlock *Block = dyn_cast<DIBlock>(DB)) {
2550 ParentDesc = Block->getContext();
2551 }
2552 DbgScope *Parent = ParentDesc.isNull() ?
Devang Pateldd49fbb2009-01-10 02:34:18 +00002553 NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002554 Slot = new DbgScope(Parent, DB);
2555 if (Parent) {
2556 Parent->AddScope(Slot);
2557 } else if (RootDbgScope) {
2558 // FIXME - Add inlined function scopes to the root so we can delete
2559 // them later. Long term, handle inlined functions properly.
2560 RootDbgScope->AddScope(Slot);
2561 } else {
2562 // First function is top level function.
2563 RootDbgScope = Slot;
2564 }
2565 }
2566 return Slot;
2567 }
2568
2569 /// ConstructDbgScope - Construct the components of a scope.
2570 ///
2571 void ConstructDbgScope(DbgScope *ParentScope,
2572 unsigned ParentStartID, unsigned ParentEndID,
2573 DIE *ParentDie, CompileUnit *Unit) {
2574 // Add variables to scope.
Devang Patel63c22f42009-01-10 02:42:49 +00002575 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel4d1709e2009-01-08 02:33:41 +00002576 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2577 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2578 if (VariableDie) ParentDie->AddChild(VariableDie);
2579 }
2580
2581 // Add nested scopes.
Devang Patel63c22f42009-01-10 02:42:49 +00002582 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel4d1709e2009-01-08 02:33:41 +00002583 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2584 // Define the Scope debug information entry.
2585 DbgScope *Scope = Scopes[j];
2586 // FIXME - Ignore inlined functions for the time being.
2587 if (!Scope->getParent()) continue;
2588
Devang Patelb9224922009-01-12 18:41:00 +00002589 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2590 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel4d1709e2009-01-08 02:33:41 +00002591
2592 // Ignore empty scopes.
2593 if (StartID == EndID && StartID != 0) continue;
2594 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2595
2596 if (StartID == ParentStartID && EndID == ParentEndID) {
2597 // Just add stuff to the parent scope.
2598 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2599 } else {
2600 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2601
2602 // Add the scope bounds.
2603 if (StartID) {
2604 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2605 DWLabel("label", StartID));
2606 } else {
2607 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2608 DWLabel("func_begin", SubprogramCount));
2609 }
2610 if (EndID) {
2611 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2612 DWLabel("label", EndID));
2613 } else {
2614 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2615 DWLabel("func_end", SubprogramCount));
2616 }
2617
2618 // Add the scope contents.
2619 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2620 ParentDie->AddChild(ScopeDie);
2621 }
2622 }
2623 }
2624
2625 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2626 ///
2627 void ConstructRootDbgScope(DbgScope *RootScope) {
2628 // Exit if there is no root scope.
2629 if (!RootScope) return;
2630
2631 // Get the subprogram debug information entry.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002632 DISubprogram SPD(RootScope->getDesc()->getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002633
2634 // Get the compile unit context.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002635 CompileUnit *Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00002636
2637 // Get the subprogram die.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002638 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002639 assert(SPDie && "Missing subprogram descriptor");
2640
2641 // Add the function bounds.
2642 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2643 DWLabel("func_begin", SubprogramCount));
2644 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2645 DWLabel("func_end", SubprogramCount));
2646 MachineLocation Location(RI->getFrameRegister(*MF));
2647 AddAddress(SPDie, DW_AT_frame_base, Location);
2648
2649 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2650 }
2651
2652 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2653 ///
2654 void ConstructDefaultDbgScope(MachineFunction *MF) {
2655 // Find the correct subprogram descriptor.
2656 std::string SPName = "llvm.dbg.subprograms";
2657 std::vector<GlobalVariable*> Result;
2658 getGlobalVariablesUsing(*M, SPName, Result);
2659 for (std::vector<GlobalVariable *>::iterator I = Result.begin(),
2660 E = Result.end(); I != E; ++I) {
2661
2662 DISubprogram *SPD = new DISubprogram(*I);
2663
2664 if (SPD->getName() == MF->getFunction()->getName()) {
2665 // Get the compile unit context.
2666 CompileUnit *Unit = FindCompileUnit(SPD->getCompileUnit());
2667
2668 // Get the subprogram die.
2669 DIE *SPDie = Unit->getDieMapSlotFor(SPD->getGV());
2670 assert(SPDie && "Missing subprogram descriptor");
2671
2672 // Add the function bounds.
2673 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2674 DWLabel("func_begin", SubprogramCount));
2675 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2676 DWLabel("func_end", SubprogramCount));
2677
2678 MachineLocation Location(RI->getFrameRegister(*MF));
2679 AddAddress(SPDie, DW_AT_frame_base, Location);
2680 return;
2681 }
2682 }
2683#if 0
2684 // FIXME: This is causing an abort because C++ mangled names are compared
2685 // with their unmangled counterparts. See PR2885. Don't do this assert.
2686 assert(0 && "Couldn't find DIE for machine function!");
2687#endif
2688 }
2689
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002690 /// ConstructScope - Construct the components of a scope.
2691 ///
2692 void ConstructScope(DebugScope *ParentScope,
2693 unsigned ParentStartID, unsigned ParentEndID,
2694 DIE *ParentDie, CompileUnit *Unit) {
2695 // Add variables to scope.
2696 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
2697 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2698 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
2699 if (VariableDie) ParentDie->AddChild(VariableDie);
2700 }
aslc200b112008-08-16 12:57:46 +00002701
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002702 // Add nested scopes.
2703 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
2704 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2705 // Define the Scope debug information entry.
2706 DebugScope *Scope = Scopes[j];
2707 // FIXME - Ignore inlined functions for the time being.
2708 if (!Scope->getParent()) continue;
aslc200b112008-08-16 12:57:46 +00002709
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002710 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2711 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
2712
2713 // Ignore empty scopes.
2714 if (StartID == EndID && StartID != 0) continue;
2715 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
aslc200b112008-08-16 12:57:46 +00002716
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002717 if (StartID == ParentStartID && EndID == ParentEndID) {
2718 // Just add stuff to the parent scope.
2719 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2720 } else {
2721 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
aslc200b112008-08-16 12:57:46 +00002722
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002723 // Add the scope bounds.
2724 if (StartID) {
2725 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2726 DWLabel("label", StartID));
2727 } else {
2728 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2729 DWLabel("func_begin", SubprogramCount));
2730 }
2731 if (EndID) {
2732 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2733 DWLabel("label", EndID));
2734 } else {
2735 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2736 DWLabel("func_end", SubprogramCount));
2737 }
aslc200b112008-08-16 12:57:46 +00002738
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002739 // Add the scope contents.
2740 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
2741 ParentDie->AddChild(ScopeDie);
2742 }
2743 }
2744 }
2745
2746 /// ConstructRootScope - Construct the scope for the subprogram.
2747 ///
2748 void ConstructRootScope(DebugScope *RootScope) {
2749 // Exit if there is no root scope.
2750 if (!RootScope) return;
aslc200b112008-08-16 12:57:46 +00002751
2752 // Get the subprogram debug information entry.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002753 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
aslc200b112008-08-16 12:57:46 +00002754
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002755 // Get the compile unit context.
2756 CompileUnit *Unit = GetBaseCompileUnit();
aslc200b112008-08-16 12:57:46 +00002757
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002758 // Get the subprogram die.
2759 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2760 assert(SPDie && "Missing subprogram descriptor");
aslc200b112008-08-16 12:57:46 +00002761
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002762 // Add the function bounds.
2763 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2764 DWLabel("func_begin", SubprogramCount));
2765 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2766 DWLabel("func_end", SubprogramCount));
2767 MachineLocation Location(RI->getFrameRegister(*MF));
2768 AddAddress(SPDie, DW_AT_frame_base, Location);
2769
2770 ConstructScope(RootScope, 0, 0, SPDie, Unit);
2771 }
2772
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002773 /// ConstructDefaultScope - Construct a default scope for the subprogram.
2774 ///
2775 void ConstructDefaultScope(MachineFunction *MF) {
2776 // Find the correct subprogram descriptor.
2777 std::vector<SubprogramDesc *> Subprograms;
2778 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
2779
2780 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2781 SubprogramDesc *SPD = Subprograms[i];
2782
2783 if (SPD->getName() == MF->getFunction()->getName()) {
2784 // Get the compile unit context.
2785 CompileUnit *Unit = GetBaseCompileUnit();
2786
2787 // Get the subprogram die.
2788 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2789 assert(SPDie && "Missing subprogram descriptor");
2790
2791 // Add the function bounds.
2792 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2793 DWLabel("func_begin", SubprogramCount));
2794 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2795 DWLabel("func_end", SubprogramCount));
2796
2797 MachineLocation Location(RI->getFrameRegister(*MF));
2798 AddAddress(SPDie, DW_AT_frame_base, Location);
2799 return;
2800 }
2801 }
Bill Wendlingae6b98c2008-10-17 18:48:57 +00002802#if 0
2803 // FIXME: This is causing an abort because C++ mangled names are compared
2804 // with their unmangled counterparts. See PR2885. Don't do this assert.
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002805 assert(0 && "Couldn't find DIE for machine function!");
Bill Wendlingae6b98c2008-10-17 18:48:57 +00002806#endif
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002807 }
2808
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002809 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2810 /// tools to recognize the object file contains Dwarf information.
2811 void EmitInitial() {
2812 // Check to see if we already emitted intial headers.
2813 if (didInitial) return;
2814 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002815
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002816 // Dwarf sections base addresses.
2817 if (TAI->doesDwarfRequireFrameSection()) {
2818 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2819 EmitLabel("section_debug_frame", 0);
2820 }
2821 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2822 EmitLabel("section_info", 0);
2823 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2824 EmitLabel("section_abbrev", 0);
2825 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2826 EmitLabel("section_aranges", 0);
2827 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2828 EmitLabel("section_macinfo", 0);
2829 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2830 EmitLabel("section_line", 0);
2831 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2832 EmitLabel("section_loc", 0);
2833 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2834 EmitLabel("section_pubnames", 0);
2835 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2836 EmitLabel("section_str", 0);
2837 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2838 EmitLabel("section_ranges", 0);
2839
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002840 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002841 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002842 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002843 EmitLabel("data_begin", 0);
2844 }
2845
2846 /// EmitDIE - Recusively Emits a debug information entry.
2847 ///
2848 void EmitDIE(DIE *Die) {
2849 // Get the abbreviation for this DIE.
2850 unsigned AbbrevNumber = Die->getAbbrevNumber();
2851 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002852
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002853 Asm->EOL();
2854
2855 // Emit the code (index) for the abbreviation.
2856 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002857
2858 if (VerboseAsm)
2859 Asm->EOL(std::string("Abbrev [" +
2860 utostr(AbbrevNumber) +
2861 "] 0x" + utohexstr(Die->getOffset()) +
2862 ":0x" + utohexstr(Die->getSize()) + " " +
2863 TagString(Abbrev->getTag())));
2864 else
2865 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002866
Owen Anderson88dd6232008-06-24 21:44:59 +00002867 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2868 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002869
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002870 // Emit the DIE attribute values.
2871 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2872 unsigned Attr = AbbrevData[i].getAttribute();
2873 unsigned Form = AbbrevData[i].getForm();
2874 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002875
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002876 switch (Attr) {
2877 case DW_AT_sibling: {
2878 Asm->EmitInt32(Die->SiblingOffset());
2879 break;
2880 }
2881 default: {
2882 // Emit an attribute using the defined form.
2883 Values[i]->EmitValue(*this, Form);
2884 break;
2885 }
2886 }
aslc200b112008-08-16 12:57:46 +00002887
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002888 Asm->EOL(AttributeString(Attr));
2889 }
aslc200b112008-08-16 12:57:46 +00002890
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002891 // Emit the DIE children if any.
2892 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2893 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002894
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002895 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2896 EmitDIE(Children[j]);
2897 }
aslc200b112008-08-16 12:57:46 +00002898
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002899 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2900 }
2901 }
2902
2903 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2904 ///
2905 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2906 // Get the children.
2907 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002908
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002909 // If not last sibling and has children then add sibling offset attribute.
2910 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2911
2912 // Record the abbreviation.
2913 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002914
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002915 // Get the abbreviation for this DIE.
2916 unsigned AbbrevNumber = Die->getAbbrevNumber();
2917 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2918
2919 // Set DIE offset
2920 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002921
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002922 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002923 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2924
Owen Anderson88dd6232008-06-24 21:44:59 +00002925 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2926 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002927
2928 // Size the DIE attribute values.
2929 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2930 // Size attribute value.
2931 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2932 }
aslc200b112008-08-16 12:57:46 +00002933
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002934 // Size the DIE children if any.
2935 if (!Children.empty()) {
2936 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2937 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002938
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002939 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2940 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2941 }
aslc200b112008-08-16 12:57:46 +00002942
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943 // End of children marker.
2944 Offset += sizeof(int8_t);
2945 }
2946
2947 Die->setSize(Offset - Die->getOffset());
2948 return Offset;
2949 }
2950
2951 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2952 ///
2953 void SizeAndOffsets() {
2954 // Process base compile unit.
2955 CompileUnit *Unit = GetBaseCompileUnit();
2956 // Compute size of compile unit header
2957 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2958 sizeof(int16_t) + // DWARF version number
2959 sizeof(int32_t) + // Offset Into Abbrev. Section
2960 sizeof(int8_t); // Pointer Size (in bytes)
2961 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2962 }
2963
2964 /// EmitDebugInfo - Emit the debug info section.
2965 ///
2966 void EmitDebugInfo() {
2967 // Start debug info section.
2968 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002969
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002970 CompileUnit *Unit = GetBaseCompileUnit();
2971 DIE *Die = Unit->getDie();
2972 // Emit the compile units header.
2973 EmitLabel("info_begin", Unit->getID());
2974 // Emit size of content not including length itself
2975 unsigned ContentSize = Die->getSize() +
2976 sizeof(int16_t) + // DWARF version number
2977 sizeof(int32_t) + // Offset Into Abbrev. Section
2978 sizeof(int8_t) + // Pointer Size (in bytes)
2979 sizeof(int32_t); // FIXME - extra pad for gdb bug.
aslc200b112008-08-16 12:57:46 +00002980
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002981 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2982 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2983 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2984 Asm->EOL("Offset Into Abbrev. Section");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002985 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
aslc200b112008-08-16 12:57:46 +00002986
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002987 EmitDIE(Die);
2988 // FIXME - extra padding for gdb bug.
2989 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2990 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2991 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2992 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2993 EmitLabel("info_end", Unit->getID());
aslc200b112008-08-16 12:57:46 +00002994
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002995 Asm->EOL();
2996 }
2997
2998 /// EmitAbbreviations - Emit the abbreviation section.
2999 ///
3000 void EmitAbbreviations() const {
3001 // Check to see if it is worth the effort.
3002 if (!Abbreviations.empty()) {
3003 // Start the debug abbrev section.
3004 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00003005
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003006 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00003007
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003008 // For each abbrevation.
3009 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3010 // Get abbreviation data
3011 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00003012
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003013 // Emit the abbrevations code (base 1 index.)
3014 Asm->EmitULEB128Bytes(Abbrev->getNumber());
3015 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00003016
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003017 // Emit the abbreviations data.
3018 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00003019
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003020 Asm->EOL();
3021 }
aslc200b112008-08-16 12:57:46 +00003022
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003023 // Mark end of abbreviations.
3024 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
3025
3026 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00003027
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003028 Asm->EOL();
3029 }
3030 }
3031
Bill Wendling1983a2a2008-07-20 00:11:19 +00003032 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
3033 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00003034 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00003035 void EmitEndOfLineMatrix(unsigned SectionEnd) {
3036 // Define last address of section.
3037 Asm->EmitInt8(0); Asm->EOL("Extended Op");
3038 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
3039 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
3040 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
3041
3042 // Mark end of matrix.
3043 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
3044 Asm->EmitULEB128Bytes(1); Asm->EOL();
3045 Asm->EmitInt8(1); Asm->EOL();
3046 }
3047
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003048 /// EmitDebugLines - Emit source line information.
3049 ///
3050 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00003051 // If the target is using .loc/.file, the assembler will be emitting the
3052 // .debug_line table automatically.
3053 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00003054 return;
3055
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003056 // Minimum line delta, thus ranging from -10..(255-10).
3057 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
3058 // Maximum line delta, thus ranging from -10..(255-10).
3059 const int MaxLineDelta = 255 + MinLineDelta;
3060
3061 // Start the dwarf line section.
3062 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00003063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003064 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00003065
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003066 EmitDifference("line_end", 0, "line_begin", 0, true);
3067 Asm->EOL("Length of Source Line Info");
3068 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00003069
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003070 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00003071
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003072 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
3073 Asm->EOL("Prolog Length");
3074 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00003075
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003076 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
3077
3078 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
3079
3080 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00003081
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003082 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
3083
3084 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00003085
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003086 // Line number standard opcode encodings argument count
3087 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
3088 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
3089 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
3090 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
3091 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
3092 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
3093 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
3094 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
3095 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
3096
3097 const UniqueVector<std::string> &Directories = MMI->getDirectories();
Evan Cheng0eeed442008-07-01 23:18:29 +00003098 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003099
3100 // Emit directories.
3101 for (unsigned DirectoryID = 1, NDID = Directories.size();
3102 DirectoryID <= NDID; ++DirectoryID) {
3103 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
3104 }
3105 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00003106
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003107 // Emit files.
3108 for (unsigned SourceID = 1, NSID = SourceFiles.size();
3109 SourceID <= NSID; ++SourceID) {
3110 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
3111 Asm->EmitString(SourceFile.getName());
3112 Asm->EOL("Source");
3113 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
3114 Asm->EOL("Directory #");
3115 Asm->EmitULEB128Bytes(0);
3116 Asm->EOL("Mod date");
3117 Asm->EmitULEB128Bytes(0);
3118 Asm->EOL("File size");
3119 }
3120 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00003121
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003122 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00003123
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003124 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00003125 unsigned SecSrcLinesSize = SectionSourceLines.size();
3126
3127 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003128 // Isolate current sections line info.
Devang Patel35a078f2009-01-12 22:54:42 +00003129 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00003130
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003131 if (VerboseAsm) {
3132 const Section* S = SectionMap[j + 1];
3133 Asm->EOL(std::string("Section ") + S->getName());
3134 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00003135 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003136
3137 // Dwarf assumes we start with first line of first source file.
3138 unsigned Source = 1;
3139 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00003140
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003141 // Construct rows of the address, source, line, column matrix.
3142 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patel35a078f2009-01-12 22:54:42 +00003143 const SrcLineInfo &LineInfo = LineInfos[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003144 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
3145 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00003146
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003147 unsigned SourceID = LineInfo.getSourceID();
3148 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
3149 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng0eeed442008-07-01 23:18:29 +00003150 if (VerboseAsm)
3151 Asm->EOL(Directories[DirectoryID]
3152 + SourceFile.getName()
3153 + ":"
3154 + utostr_32(LineInfo.getLine()));
3155 else
3156 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003157
3158 // Define the line address.
3159 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00003160 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003161 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
3162 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00003163
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003164 // If change of source, then switch to the new source.
3165 if (Source != LineInfo.getSourceID()) {
3166 Source = LineInfo.getSourceID();
3167 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
3168 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
3169 }
aslc200b112008-08-16 12:57:46 +00003170
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003171 // If change of line.
3172 if (Line != LineInfo.getLine()) {
3173 // Determine offset.
3174 int Offset = LineInfo.getLine() - Line;
3175 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00003176
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003177 // Update line.
3178 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00003179
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003180 // If delta is small enough and in range...
3181 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3182 // ... then use fast opcode.
3183 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
3184 } else {
3185 // ... otherwise use long hand.
3186 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
3187 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
3188 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
3189 }
3190 } else {
3191 // Copy the previous row (different address or source)
3192 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
3193 }
3194 }
3195
Bill Wendling1983a2a2008-07-20 00:11:19 +00003196 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003197 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00003198
3199 if (SecSrcLinesSize == 0)
3200 // Because we're emitting a debug_line section, we still need a line
3201 // table. The linker and friends expect it to exist. If there's nothing to
3202 // put into it, emit an empty table.
3203 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00003204
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003205 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00003206
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003207 Asm->EOL();
3208 }
aslc200b112008-08-16 12:57:46 +00003209
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003210 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
3211 ///
3212 void EmitCommonDebugFrame() {
3213 if (!TAI->doesDwarfRequireFrameSection())
3214 return;
3215
3216 int stackGrowth =
3217 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3218 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003219 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003220
3221 // Start the dwarf frame section.
3222 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
3223
3224 EmitLabel("debug_frame_common", 0);
3225 EmitDifference("debug_frame_common_end", 0,
3226 "debug_frame_common_begin", 0, true);
3227 Asm->EOL("Length of Common Information Entry");
3228
3229 EmitLabel("debug_frame_common_begin", 0);
3230 Asm->EmitInt32((int)DW_CIE_ID);
3231 Asm->EOL("CIE Identifier Tag");
3232 Asm->EmitInt8(DW_CIE_VERSION);
3233 Asm->EOL("CIE Version");
3234 Asm->EmitString("");
3235 Asm->EOL("CIE Augmentation");
3236 Asm->EmitULEB128Bytes(1);
3237 Asm->EOL("CIE Code Alignment Factor");
3238 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00003239 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003240 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003241 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00003242
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003243 std::vector<MachineMove> Moves;
3244 RI->getInitialFrameState(Moves);
3245
Dale Johannesenf5a11532007-11-13 19:13:01 +00003246 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003247
Evan Cheng7e7d1942008-02-29 19:36:59 +00003248 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003249 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00003250
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003251 Asm->EOL();
3252 }
3253
3254 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
3255 /// section.
3256 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
3257 if (!TAI->doesDwarfRequireFrameSection())
3258 return;
aslc200b112008-08-16 12:57:46 +00003259
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003260 // Start the dwarf frame section.
3261 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00003262
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003263 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
3264 "debug_frame_begin", DebugFrameInfo.Number, true);
3265 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003266
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003267 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
3268
3269 EmitSectionOffset("debug_frame_common", "section_debug_frame",
3270 0, 0, true, false);
3271 Asm->EOL("FDE CIE offset");
3272
3273 EmitReference("func_begin", DebugFrameInfo.Number);
3274 Asm->EOL("FDE initial location");
3275 EmitDifference("func_end", DebugFrameInfo.Number,
3276 "func_begin", DebugFrameInfo.Number);
3277 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00003278
Dale Johannesenf5a11532007-11-13 19:13:01 +00003279 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, false);
aslc200b112008-08-16 12:57:46 +00003280
Evan Cheng7e7d1942008-02-29 19:36:59 +00003281 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003282 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
3283
3284 Asm->EOL();
3285 }
3286
3287 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
3288 ///
3289 void EmitDebugPubNames() {
3290 // Start the dwarf pubnames section.
3291 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00003292
3293 CompileUnit *Unit = GetBaseCompileUnit();
3294
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003295 EmitDifference("pubnames_end", Unit->getID(),
3296 "pubnames_begin", Unit->getID(), true);
3297 Asm->EOL("Length of Public Names Info");
aslc200b112008-08-16 12:57:46 +00003298
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003299 EmitLabel("pubnames_begin", Unit->getID());
aslc200b112008-08-16 12:57:46 +00003300
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003301 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
3302
3303 EmitSectionOffset("info_begin", "section_info",
3304 Unit->getID(), 0, true, false);
3305 Asm->EOL("Offset of Compilation Unit Info");
3306
3307 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
3308 Asm->EOL("Compilation Unit Length");
aslc200b112008-08-16 12:57:46 +00003309
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003310 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
aslc200b112008-08-16 12:57:46 +00003311
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003312 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
3313 GE = Globals.end();
3314 GI != GE; ++GI) {
3315 const std::string &Name = GI->first;
3316 DIE * Entity = GI->second;
aslc200b112008-08-16 12:57:46 +00003317
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003318 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
3319 Asm->EmitString(Name); Asm->EOL("External Name");
3320 }
aslc200b112008-08-16 12:57:46 +00003321
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003322 Asm->EmitInt32(0); Asm->EOL("End Mark");
3323 EmitLabel("pubnames_end", Unit->getID());
aslc200b112008-08-16 12:57:46 +00003324
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003325 Asm->EOL();
3326 }
3327
3328 /// EmitDebugStr - Emit visible names into a debug str section.
3329 ///
3330 void EmitDebugStr() {
3331 // Check to see if it is worth the effort.
3332 if (!StringPool.empty()) {
3333 // Start the dwarf str section.
3334 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00003335
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003336 // For each of strings in the string pool.
3337 for (unsigned StringID = 1, N = StringPool.size();
3338 StringID <= N; ++StringID) {
3339 // Emit a label for reference from debug information entries.
3340 EmitLabel("string", StringID);
3341 // Emit the string itself.
3342 const std::string &String = StringPool[StringID];
3343 Asm->EmitString(String); Asm->EOL();
3344 }
aslc200b112008-08-16 12:57:46 +00003345
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003346 Asm->EOL();
3347 }
3348 }
3349
3350 /// EmitDebugLoc - Emit visible names into a debug loc section.
3351 ///
3352 void EmitDebugLoc() {
3353 // Start the dwarf loc section.
3354 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00003355
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003356 Asm->EOL();
3357 }
3358
3359 /// EmitDebugARanges - Emit visible names into a debug aranges section.
3360 ///
3361 void EmitDebugARanges() {
3362 // Start the dwarf aranges section.
3363 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00003364
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003365 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003366#if 0
aslc200b112008-08-16 12:57:46 +00003367 CompileUnit *Unit = GetBaseCompileUnit();
3368
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003369 // Don't include size of length
3370 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00003371
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003372 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00003373
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003374 EmitReference("info_begin", Unit->getID());
3375 Asm->EOL("Offset of Compilation Unit Info");
3376
Dan Gohmancfb72b22007-09-27 23:12:31 +00003377 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003378
3379 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
3380
3381 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
3382 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
3383
3384 // Range 1
3385 EmitReference("text_begin", 0); Asm->EOL("Address");
3386 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
3387
3388 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
3389 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003390#endif
aslc200b112008-08-16 12:57:46 +00003391
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003392 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003393 }
3394
3395 /// EmitDebugRanges - Emit visible names into a debug ranges section.
3396 ///
3397 void EmitDebugRanges() {
3398 // Start the dwarf ranges section.
3399 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00003400
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003401 Asm->EOL();
3402 }
3403
3404 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
3405 ///
3406 void EmitDebugMacInfo() {
3407 // Start the dwarf macinfo section.
3408 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00003409
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003410 Asm->EOL();
3411 }
3412
Devang Patel289f2362009-01-05 23:11:11 +00003413 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00003414 void ConstructCompileUnits() {
3415 std::string CUName = "llvm.dbg.compile_units";
3416 std::vector<GlobalVariable*> Result;
3417 getGlobalVariablesUsing(*M, CUName, Result);
3418 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3419 RE = Result.end(); RI != RE; ++RI) {
3420 DICompileUnit *DIUnit = new DICompileUnit(*RI);
Devang Patel7dd15a92009-01-08 17:19:22 +00003421 unsigned ID = RecordSource(DIUnit->getDirectory(),
3422 DIUnit->getFilename());
Devang Patelb3907da2009-01-05 23:03:32 +00003423
3424 DIE *Die = new DIE(DW_TAG_compile_unit);
3425 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
3426 DWLabel("section_line", 0), DWLabel("section_line", 0),
3427 false);
3428 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit->getProducer());
3429 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit->getLanguage());
3430 AddString(Die, DW_AT_name, DW_FORM_string, DIUnit->getFilename());
3431 if (!DIUnit->getDirectory().empty())
3432 AddString(Die, DW_AT_comp_dir, DW_FORM_string, DIUnit->getDirectory());
3433
3434 CompileUnit *Unit = new CompileUnit(ID, Die);
3435 DW_CUs[DIUnit->getGV()] = Unit;
3436 }
3437 }
3438
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003439 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
3440 /// header file.
3441 void ConstructCompileUnitDIEs() {
3442 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
aslc200b112008-08-16 12:57:46 +00003443
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003444 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
3445 unsigned ID = MMI->RecordSource(CUW[i]);
3446 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
3447 CompileUnits.push_back(Unit);
3448 }
3449 }
3450
Devang Patel289f2362009-01-05 23:11:11 +00003451 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
3452 /// visible global variables.
3453 void ConstructGlobalVariableDIEs() {
3454 std::string GVName = "llvm.dbg.global_variables";
3455 std::vector<GlobalVariable*> Result;
3456 getGlobalVariablesUsing(*M, GVName, Result);
3457 for (std::vector<GlobalVariable *>::iterator GVI = Result.begin(),
3458 GVE = Result.end(); GVI != GVE; ++GVI) {
3459 DIGlobalVariable *DI_GV = new DIGlobalVariable(*GVI);
3460 CompileUnit *DW_Unit = FindCompileUnit(DI_GV->getCompileUnit());
3461
3462 // Check for pre-existence.
3463 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV->getGV());
3464 if (Slot) continue;
3465
3466 DIE *VariableDie = new DIE(DW_TAG_variable);
3467 AddString(VariableDie, DW_AT_name, DW_FORM_string, DI_GV->getName());
3468 const std::string &LinkageName = DI_GV->getLinkageName();
3469 if (!LinkageName.empty())
3470 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3471 LinkageName);
3472 AddType(DW_Unit, VariableDie, DI_GV->getType());
3473
3474 if (!DI_GV->isLocalToUnit())
3475 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
3476
3477 // Add source line info, if available.
3478 AddSourceLine(VariableDie, DI_GV);
3479
3480 // Add address.
3481 DIEBlock *Block = new DIEBlock();
3482 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
3483 AddObjectLabel(Block, 0, DW_FORM_udata,
3484 Asm->getGlobalLinkName(DI_GV->getGV()));
3485 AddBlock(VariableDie, DW_AT_location, 0, Block);
3486
3487 //Add to map.
3488 Slot = VariableDie;
3489
3490 //Add to context owner.
3491 DW_Unit->getDie()->AddChild(VariableDie);
3492
3493 //Expose as global. FIXME - need to check external flag.
3494 DW_Unit->AddGlobal(DI_GV->getName(), VariableDie);
3495 }
3496 }
3497
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003498 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
3499 /// global variables.
3500 void ConstructGlobalDIEs() {
Bill Wendling75091f92008-07-03 23:13:02 +00003501 std::vector<GlobalVariableDesc *> GlobalVariables;
3502 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M, GlobalVariables);
aslc200b112008-08-16 12:57:46 +00003503
Bill Wendling4de8de52008-07-03 22:53:42 +00003504 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
3505 GlobalVariableDesc *GVD = GlobalVariables[i];
3506 NewGlobalVariable(GVD);
3507 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003508 }
3509
Devang Patele6caf012009-01-05 23:21:35 +00003510 /// ConstructSubprograms - Create DIEs for each of the externally visible
3511 /// subprograms.
3512 void ConstructSubprograms() {
3513
3514 std::string SPName = "llvm.dbg.subprograms";
3515 std::vector<GlobalVariable*> Result;
3516 getGlobalVariablesUsing(*M, SPName, Result);
3517 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
3518 RE = Result.end(); RI != RE; ++RI) {
3519
3520 DISubprogram *SP = new DISubprogram(*RI);
3521 CompileUnit *Unit = FindCompileUnit(SP->getCompileUnit());
3522
3523 // Check for pre-existence.
3524 DIE *&Slot = Unit->getDieMapSlotFor(SP->getGV());
3525 if (Slot) continue;
3526
3527 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
3528 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, SP->getName());
3529 const std::string &LinkageName = SP->getLinkageName();
3530 if (!LinkageName.empty())
3531 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
3532 LinkageName);
3533 DIType SPTy = SP->getType();
3534 AddType(Unit, SubprogramDie, SPTy);
3535 if (!SP->isLocalToUnit())
3536 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
3537 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
3538
3539 AddSourceLine(SubprogramDie, SP);
3540 //Add to map.
3541 Slot = SubprogramDie;
3542 //Add to context owner.
3543 Unit->getDie()->AddChild(SubprogramDie);
3544 //Expose as global.
3545 Unit->AddGlobal(SP->getName(), SubprogramDie);
3546 }
3547 }
3548
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003549 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
3550 /// subprograms.
3551 void ConstructSubprogramDIEs() {
Bill Wendling75091f92008-07-03 23:13:02 +00003552 std::vector<SubprogramDesc *> Subprograms;
3553 MMI->getAnchoredDescriptors<SubprogramDesc>(*M, Subprograms);
aslc200b112008-08-16 12:57:46 +00003554
Bill Wendling4de8de52008-07-03 22:53:42 +00003555 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
3556 SubprogramDesc *SPD = Subprograms[i];
3557 NewSubprogram(SPD);
3558 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003559 }
3560
3561public:
3562 //===--------------------------------------------------------------------===//
3563 // Main entry points.
3564 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003565 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00003566 : Dwarf(OS, A, T, "dbg")
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003567 , CompileUnits()
3568 , AbbreviationsSet(InitAbbreviationsSetSize)
3569 , Abbreviations()
3570 , ValuesSet(InitValuesSetSize)
3571 , Values()
3572 , StringPool()
3573 , DescToUnitMap()
3574 , SectionMap()
3575 , SectionSourceLines()
3576 , didInitial(false)
3577 , shouldEmit(false)
Devang Patel4d1709e2009-01-08 02:33:41 +00003578 , RootDbgScope(NULL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003579 {
3580 }
3581 virtual ~DwarfDebug() {
3582 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
3583 delete CompileUnits[i];
3584 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3585 delete Values[j];
3586 }
3587
Devang Patel9304b382009-01-06 21:07:30 +00003588 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
3589 /// This is inovked by the target AsmPrinter.
3590 void SetDebugInfo() {
3591 // FIXME - Check if the module has debug info or not.
3592 // Create all the compile unit DIEs.
3593 ConstructCompileUnits();
3594
3595 // Create DIEs for each of the externally visible global variables.
3596 ConstructGlobalVariableDIEs();
3597
3598 // Create DIEs for each of the externally visible subprograms.
3599 ConstructSubprograms();
3600
3601 // Prime section data.
3602 SectionMap.insert(TAI->getTextSection());
3603
3604 // Print out .file directives to specify files for .loc directives. These
3605 // are printed out early so that they precede any .loc directives.
3606 if (TAI->hasDotLocAndDotFile()) {
3607 for (unsigned i = 1, e = SrcFiles.size(); i <= e; ++i) {
3608 sys::Path FullPath(Directories[SrcFiles[i].getDirectoryID()]);
3609 bool AppendOk = FullPath.appendComponent(SrcFiles[i].getName());
3610 assert(AppendOk && "Could not append filename to directory!");
3611 AppendOk = false;
3612 Asm->EmitFile(i, FullPath.toString());
3613 Asm->EOL();
3614 }
3615 }
3616
3617 // Emit initial sections
3618 EmitInitial();
3619 }
3620
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003621 /// SetModuleInfo - Set machine module information when it's known that pass
3622 /// manager has created it. Set by the target AsmPrinter.
3623 void SetModuleInfo(MachineModuleInfo *mmi) {
3624 // Make sure initial declarations are made.
3625 if (!MMI && mmi->hasDebugInfo()) {
3626 MMI = mmi;
3627 shouldEmit = true;
aslc200b112008-08-16 12:57:46 +00003628
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003629 // Create all the compile unit DIEs.
3630 ConstructCompileUnitDIEs();
aslc200b112008-08-16 12:57:46 +00003631
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003632 // Create DIEs for each of the externally visible global variables.
3633 ConstructGlobalDIEs();
3634
3635 // Create DIEs for each of the externally visible subprograms.
3636 ConstructSubprogramDIEs();
aslc200b112008-08-16 12:57:46 +00003637
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003638 // Prime section data.
3639 SectionMap.insert(TAI->getTextSection());
Dan Gohman6d6c2402007-10-01 22:40:20 +00003640
3641 // Print out .file directives to specify files for .loc directives. These
3642 // are printed out early so that they precede any .loc directives.
3643 if (TAI->hasDotLocAndDotFile()) {
3644 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
3645 const UniqueVector<std::string> &Directories = MMI->getDirectories();
3646 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
3647 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
3648 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
3649 assert(AppendOk && "Could not append filename to directory!");
Devang Patel105a08a2008-12-23 21:55:38 +00003650 AppendOk = false;
Dan Gohman6d6c2402007-10-01 22:40:20 +00003651 Asm->EmitFile(i, FullPath.toString());
3652 Asm->EOL();
3653 }
3654 }
3655
3656 // Emit initial sections
3657 EmitInitial();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003658 }
3659 }
3660
3661 /// BeginModule - Emit all Dwarf sections that should come prior to the
3662 /// content.
3663 void BeginModule(Module *M) {
3664 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003665 }
3666
3667 /// EndModule - Emit all Dwarf sections that should come after the content.
3668 ///
3669 void EndModule() {
3670 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00003671
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003672 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003673 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003674 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00003675 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003676 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00003677
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003678 // End text sections.
3679 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003680 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003681 EmitLabel("section_end", i);
3682 }
3683
3684 // Emit common frame information.
3685 EmitCommonDebugFrame();
3686
3687 // Emit function debug frame information
3688 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3689 E = DebugFrames.end(); I != E; ++I)
3690 EmitFunctionDebugFrame(*I);
3691
3692 // Compute DIE offsets and sizes.
3693 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00003694
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003695 // Emit all the DIEs into a debug info section
3696 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00003697
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003698 // Corresponding abbreviations into a abbrev section.
3699 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00003700
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003701 // Emit source line correspondence into a debug line section.
3702 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00003703
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003704 // Emit info into a debug pubnames section.
3705 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00003706
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003707 // Emit info into a debug str section.
3708 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00003709
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003710 // Emit info into a debug loc section.
3711 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00003712
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003713 // Emit info into a debug aranges section.
3714 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00003715
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003716 // Emit info into a debug ranges section.
3717 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00003718
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003719 // Emit info into a debug macinfo section.
3720 EmitDebugMacInfo();
3721 }
3722
aslc200b112008-08-16 12:57:46 +00003723 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003724 /// emitted immediately after the function entry point.
3725 void BeginFunction(MachineFunction *MF) {
3726 this->MF = MF;
aslc200b112008-08-16 12:57:46 +00003727
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003728 if (!ShouldEmitDwarf()) return;
3729
3730 // Begin accumulating function debug information.
3731 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00003732
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003733 // Assumes in correct section after the entry point.
3734 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00003735
3736 // Emit label for the implicitly defined dbg.stoppoint at the start of
3737 // the function.
Devang Patel35a078f2009-01-12 22:54:42 +00003738 if (!Lines.empty()) {
3739 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth42f91402008-04-03 17:37:43 +00003740 Asm->printLabel(LineInfo.getLabelID());
3741 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003742 }
aslc200b112008-08-16 12:57:46 +00003743
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003744 /// EndFunction - Gather and emit post-function debug information.
3745 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003746 void EndFunction(MachineFunction *MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003747 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00003748
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003749 // Define end label for subprogram.
3750 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003751
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003752 // Get function line info.
Devang Patel35a078f2009-01-12 22:54:42 +00003753 if (!Lines.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003754 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003755 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003756 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patel35a078f2009-01-12 22:54:42 +00003757 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003758 // Append the function info to section info.
3759 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patel35a078f2009-01-12 22:54:42 +00003760 Lines.begin(), Lines.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003761 }
aslc200b112008-08-16 12:57:46 +00003762
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003763 // Construct scopes for subprogram.
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003764 if (MMI->getRootScope())
3765 ConstructRootScope(MMI->getRootScope());
3766 else
3767 // FIXME: This is wrong. We are essentially getting past a problem with
3768 // debug information not being able to handle unreachable blocks that have
3769 // debug information in them. In particular, those unreachable blocks that
3770 // have "region end" info in them. That situation results in the "root
3771 // scope" not being created. If that's the case, then emit a "default"
3772 // scope, i.e., one that encompasses the whole function. This isn't
3773 // desirable. And a better way of handling this (and all of the debugging
3774 // information) needs to be explored.
3775 ConstructDefaultScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003776
3777 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3778 MMI->getFrameMoves()));
Devang Patela4162952009-01-12 18:48:36 +00003779
3780 // Clear debug info
3781 if (RootDbgScope) {
3782 delete RootDbgScope;
3783 DbgScopeMap.clear();
3784 RootDbgScope = NULL;
3785 }
3786 Lines.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003787 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003788
3789public:
3790
3791 /// RecordSourceLine - Records location information and associates it with a
3792 /// label. Returns a unique label ID used to generate a label and provide
3793 /// correspondence to the source line list.
3794 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
3795 CompileUnit *Unit = DW_CUs[V];
3796 assert (Unit && "Unable to find CompileUnit");
3797 unsigned ID = MMI->NextLabelID();
3798 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
3799 return ID;
3800 }
3801
3802 /// RecordSourceLine - Records location information and associates it with a
3803 /// label. Returns a unique label ID used to generate a label and provide
3804 /// correspondence to the source line list.
3805 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
3806 unsigned ID = MMI->NextLabelID();
3807 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
3808 return ID;
3809 }
3810
3811 unsigned getRecordSourceLineCount() {
3812 return Lines.size();
3813 }
3814
3815 /// RecordSource - Register a source file with debug info. Returns an source
3816 /// ID.
3817 unsigned RecordSource(const std::string &Directory,
3818 const std::string &File) {
3819 unsigned DID = Directories.insert(Directory);
3820 return SrcFiles.insert(SrcFileInfo(DID,File));
3821 }
3822
3823 /// RecordRegionStart - Indicate the start of a region.
3824 ///
3825 unsigned RecordRegionStart(GlobalVariable *V) {
3826 DbgScope *Scope = getOrCreateScope(V);
3827 unsigned ID = MMI->NextLabelID();
3828 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
3829 return ID;
3830 }
3831
3832 /// RecordRegionEnd - Indicate the end of a region.
3833 ///
3834 unsigned RecordRegionEnd(GlobalVariable *V) {
3835 DbgScope *Scope = getOrCreateScope(V);
3836 unsigned ID = MMI->NextLabelID();
3837 Scope->setEndLabelID(ID);
3838 return ID;
3839 }
3840
3841 /// RecordVariable - Indicate the declaration of a local variable.
3842 ///
3843 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
3844 DbgScope *Scope = getOrCreateScope(GV);
3845 DIVariable *VD = new DIVariable(GV);
3846 DbgVariable *DV = new DbgVariable(VD, FrameIndex);
3847 Scope->AddVariable(DV);
3848 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003849};
3850
3851//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003852/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003853///
3854class DwarfException : public Dwarf {
3855
3856private:
3857 struct FunctionEHFrameInfo {
3858 std::string FnName;
3859 unsigned Number;
3860 unsigned PersonalityIndex;
3861 bool hasCalls;
3862 bool hasLandingPads;
3863 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003864 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003865
3866 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3867 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003868 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003869 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003870 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003871 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003872 };
3873
3874 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003875
3876 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3877 /// be emitted.
3878 bool shouldEmitTable;
3879
3880 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3881 /// should be emitted.
3882 bool shouldEmitMoves;
3883
3884 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3885 /// should be emitted.
3886 bool shouldEmitTableModule;
3887
aslc200b112008-08-16 12:57:46 +00003888 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003889 /// should be emitted.
3890 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003891
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003892 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3893 ///
3894 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3895 // Size and sign of stack growth.
3896 int stackGrowth =
3897 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3898 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003899 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003900
3901 // Begin eh frame section.
3902 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003903
3904 if (!TAI->doesRequireNonLocalEHFrameLabel())
3905 O << TAI->getEHGlobalPrefix();
3906 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003907 EmitLabel("section_eh_frame", Index);
3908
3909 // Define base labels.
3910 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003911
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003912 // Define the eh frame length.
3913 EmitDifference("eh_frame_common_end", Index,
3914 "eh_frame_common_begin", Index, true);
3915 Asm->EOL("Length of Common Information Entry");
3916
3917 // EH frame header.
3918 EmitLabel("eh_frame_common_begin", Index);
3919 Asm->EmitInt32((int)0);
3920 Asm->EOL("CIE Identifier Tag");
3921 Asm->EmitInt8(DW_CIE_VERSION);
3922 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003923
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003924 // The personality presence indicates that language specific information
3925 // will show up in the eh frame.
3926 Asm->EmitString(Personality ? "zPLR" : "zR");
3927 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003928
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003929 // Round out reader.
3930 Asm->EmitULEB128Bytes(1);
3931 Asm->EOL("CIE Code Alignment Factor");
3932 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003933 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003934 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003935 Asm->EOL("CIE Return Address Column");
3936
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003937 // If there is a personality, we need to indicate the functions location.
3938 if (Personality) {
3939 Asm->EmitULEB128Bytes(7);
3940 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003941
Duncan Sands96144f92008-05-07 19:11:09 +00003942 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003943 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003944 Asm->EOL("Personality (pcrel sdata4 indirect)");
3945 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003946 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003947 Asm->EOL("Personality (pcrel sdata4)");
3948 }
Bill Wendling2d369922007-09-11 17:20:55 +00003949
Duncan Sands96144f92008-05-07 19:11:09 +00003950 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003951 O << TAI->getPersonalityPrefix();
3952 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3953 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003954 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3955 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003956 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003957
Duncan Sands96144f92008-05-07 19:11:09 +00003958 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3959 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003960
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003961 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3962 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003963 } else {
3964 Asm->EmitULEB128Bytes(1);
3965 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003966
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003967 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3968 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003969 }
3970
3971 // Indicate locations of general callee saved registers in frame.
3972 std::vector<MachineMove> Moves;
3973 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003974 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003975
Dale Johannesen388f20f2008-04-30 00:43:29 +00003976 // On Darwin the linker honors the alignment of eh_frame, which means it
3977 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3978 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003979 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003980 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003981 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003982
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003983 Asm->EOL();
3984 }
Duncan Sands96144f92008-05-07 19:11:09 +00003985
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003986 /// EmitEHFrame - Emit function exception frame information.
3987 ///
3988 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003989 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3990
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003991 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3992
3993 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003994 // If the corresponding function is static, this should not be
3995 // externally visible.
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003996 if (linkage != Function::InternalLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003997 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3998 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3999 }
4000
Dale Johannesenf09b5992008-01-10 02:03:30 +00004001 // If corresponding function is weak definition, this should be too.
aslc200b112008-08-16 12:57:46 +00004002 if ((linkage == Function::WeakLinkage ||
Dale Johannesen3dadeb32008-01-16 19:59:28 +00004003 linkage == Function::LinkOnceLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00004004 TAI->getWeakDefDirective())
4005 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
4006
4007 // If there are no calls then you can't unwind. This may mean we can
4008 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00004009 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00004010 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00004011 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00004012 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00004013 !UnwindTablesMandatory &&
aslc200b112008-08-16 12:57:46 +00004014 ((linkage != Function::WeakLinkage &&
Dale Johannesen3dadeb32008-01-16 19:59:28 +00004015 linkage != Function::LinkOnceLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00004016 !TAI->getWeakDefDirective() ||
4017 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00004018 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00004019 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00004020 // This name has no connection to the function, so it might get
4021 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00004022 // dead-stripping unconditionally.
4023 if (const char *UsedDirective = TAI->getUsedDirective())
4024 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004025 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00004026 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00004027
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004028 // EH frame header.
4029 EmitDifference("eh_frame_end", EHFrameInfo.Number,
4030 "eh_frame_begin", EHFrameInfo.Number, true);
4031 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00004032
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004033 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
4034
Bill Wendling189bde72008-12-24 08:05:17 +00004035 if (TAI->doesRequireNonLocalEHFrameLabel()) {
4036 PrintRelDirective(true, true);
4037 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
4038
4039 if (!TAI->isAbsoluteEHSectionOffsets())
4040 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
4041 } else {
4042 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
4043 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
4044 true, true, false);
4045 }
4046
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004047 Asm->EOL("FDE CIE offset");
4048
Bill Wendlingdd9127d2009-01-06 19:13:55 +00004049 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004050 Asm->EOL("FDE initial location");
4051 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00004052 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004053 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00004054
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004055 // If there is a personality and landing pads then point to the language
4056 // specific data area in the exception table.
4057 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00004058 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004059 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00004060
4061 if (EHFrameInfo.hasLandingPads)
4062 EmitReference("exception", EHFrameInfo.Number, true, true);
4063 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004064 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004065 Asm->EOL("Language Specific Data Area");
4066 } else {
4067 Asm->EmitULEB128Bytes(0);
4068 Asm->EOL("Augmentation size");
4069 }
Duncan Sands96144f92008-05-07 19:11:09 +00004070
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004071 // Indicate locations of function specific callee saved registers in
4072 // frame.
Dale Johannesenf5a11532007-11-13 19:13:01 +00004073 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, true);
aslc200b112008-08-16 12:57:46 +00004074
Dale Johannesen388f20f2008-04-30 00:43:29 +00004075 // On Darwin the linker honors the alignment of eh_frame, which means it
4076 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
4077 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00004078 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00004079 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004080 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00004081
4082 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00004083 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00004084 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00004085 // that depends on unused functions (calling undefined externals) being
4086 // dead-stripped to link correctly. Yes, there really is.
4087 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
4088 if (const char *UsedDirective = TAI->getUsedDirective())
4089 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
4090 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004091 }
4092
Duncan Sands241a0c92007-09-05 11:27:52 +00004093 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004094 ///
4095 /// The general organization of the table is complex, but the basic concepts
4096 /// are easy. First there is a header which describes the location and
4097 /// organization of the three components that follow.
4098 /// 1. The landing pad site information describes the range of code covered
4099 /// by the try. In our case it's an accumulation of the ranges covered
4100 /// by the invokes in the try. There is also a reference to the landing
4101 /// pad that handles the exception once processed. Finally an index into
4102 /// the actions table.
4103 /// 2. The action table, in our case, is composed of pairs of type ids
4104 /// and next action offset. Starting with the action index from the
4105 /// landing pad site, each type Id is checked for a match to the current
4106 /// exception. If it matches then the exception and type id are passed
4107 /// on to the landing pad. Otherwise the next action is looked up. This
4108 /// chain is terminated with a next action of zero. If no type id is
4109 /// found the the frame is unwound and handling continues.
4110 /// 3. Type id table contains references to all the C++ typeinfo for all
4111 /// catches in the function. This tables is reversed indexed base 1.
4112
4113 /// SharedTypeIds - How many leading type ids two landing pads have in common.
4114 static unsigned SharedTypeIds(const LandingPadInfo *L,
4115 const LandingPadInfo *R) {
4116 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
4117 unsigned LSize = LIds.size(), RSize = RIds.size();
4118 unsigned MinSize = LSize < RSize ? LSize : RSize;
4119 unsigned Count = 0;
4120
4121 for (; Count != MinSize; ++Count)
4122 if (LIds[Count] != RIds[Count])
4123 return Count;
4124
4125 return Count;
4126 }
4127
4128 /// PadLT - Order landing pads lexicographically by type id.
4129 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
4130 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
4131 unsigned LSize = LIds.size(), RSize = RIds.size();
4132 unsigned MinSize = LSize < RSize ? LSize : RSize;
4133
4134 for (unsigned i = 0; i != MinSize; ++i)
4135 if (LIds[i] != RIds[i])
4136 return LIds[i] < RIds[i];
4137
4138 return LSize < RSize;
4139 }
4140
4141 struct KeyInfo {
4142 static inline unsigned getEmptyKey() { return -1U; }
4143 static inline unsigned getTombstoneKey() { return -2U; }
4144 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00004145 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004146 static bool isPod() { return true; }
4147 };
4148
Duncan Sands241a0c92007-09-05 11:27:52 +00004149 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004150 struct ActionEntry {
4151 int ValueForTypeID; // The value to write - may not be equal to the type id.
4152 int NextAction;
4153 struct ActionEntry *Previous;
4154 };
4155
Duncan Sands241a0c92007-09-05 11:27:52 +00004156 /// PadRange - Structure holding a try-range and the associated landing pad.
4157 struct PadRange {
4158 // The index of the landing pad.
4159 unsigned PadIndex;
4160 // The index of the begin and end labels in the landing pad's label lists.
4161 unsigned RangeIndex;
4162 };
4163
4164 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
4165
4166 /// CallSiteEntry - Structure describing an entry in the call-site table.
4167 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00004168 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00004169 unsigned BeginLabel; // zero indicates the start of the function.
4170 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004171 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00004172 unsigned PadLabel; // zero indicates that there is no landing pad.
4173 unsigned Action;
4174 };
4175
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004176 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004177 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
4178 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
4179 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
4180 if (PadInfos.empty()) return;
4181
4182 // Sort the landing pads in order of their type ids. This is used to fold
4183 // duplicate actions.
4184 SmallVector<const LandingPadInfo *, 64> LandingPads;
4185 LandingPads.reserve(PadInfos.size());
4186 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
4187 LandingPads.push_back(&PadInfos[i]);
4188 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
4189
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004190 // Negative type ids index into FilterIds, positive type ids index into
4191 // TypeInfos. The value written for a positive type id is just the type
4192 // id itself. For a negative type id, however, the value written is the
4193 // (negative) byte offset of the corresponding FilterIds entry. The byte
4194 // offset is usually equal to the type id, because the FilterIds entries
4195 // are written using a variable width encoding which outputs one byte per
4196 // entry as long as the value written is not too large, but can differ.
4197 // This kind of complication does not occur for positive type ids because
4198 // type infos are output using a fixed width encoding.
4199 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
4200 SmallVector<int, 16> FilterOffsets;
4201 FilterOffsets.reserve(FilterIds.size());
4202 int Offset = -1;
4203 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
4204 E = FilterIds.end(); I != E; ++I) {
4205 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00004206 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004207 }
4208
Duncan Sands241a0c92007-09-05 11:27:52 +00004209 // Compute the actions table and gather the first action index for each
4210 // landing pad site.
4211 SmallVector<ActionEntry, 32> Actions;
4212 SmallVector<unsigned, 64> FirstActions;
4213 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004214
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004215 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00004216 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004217 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4218 const LandingPadInfo *LP = LandingPads[i];
4219 const std::vector<int> &TypeIds = LP->TypeIds;
4220 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
4221 unsigned SizeSiteActions = 0;
4222
4223 if (NumShared < TypeIds.size()) {
4224 unsigned SizeAction = 0;
4225 ActionEntry *PrevAction = 0;
4226
4227 if (NumShared) {
4228 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
4229 assert(Actions.size());
4230 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00004231 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
4232 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004233 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00004234 SizeAction -=
4235 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004236 SizeAction += -PrevAction->NextAction;
4237 PrevAction = PrevAction->Previous;
4238 }
4239 }
4240
4241 // Compute the actions.
4242 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
4243 int TypeID = TypeIds[I];
4244 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
4245 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00004246 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004247
4248 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00004249 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004250 SizeSiteActions += SizeAction;
4251
4252 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
4253 Actions.push_back(Action);
4254
4255 PrevAction = &Actions.back();
4256 }
4257
4258 // Record the first action of the landing pad site.
4259 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
4260 } // else identical - re-use previous FirstAction
4261
4262 FirstActions.push_back(FirstAction);
4263
4264 // Compute this sites contribution to size.
4265 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004266 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004267
Duncan Sands4ff179f2007-12-19 07:36:31 +00004268 // Compute the call-site table. The entry for an invoke has a try-range
4269 // containing the call, a non-zero landing pad and an appropriate action.
4270 // The entry for an ordinary call has a try-range containing the call and
4271 // zero for the landing pad and the action. Calls marked 'nounwind' have
4272 // no entry and must not be contained in the try-range of any entry - they
4273 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00004274 SmallVector<CallSiteEntry, 64> CallSites;
4275
4276 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00004277 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
4278 // by try-range labels when lowered). Ordinary calls do not, so appropriate
4279 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00004280 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
4281 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00004282 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004283 unsigned BeginLabel = LandingPad->BeginLabels[j];
4284 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
4285 PadRange P = { i, j };
4286 PadMap[BeginLabel] = P;
4287 }
4288 }
4289
Duncan Sands4ff179f2007-12-19 07:36:31 +00004290 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00004291 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00004292
4293 // Whether there is a potentially throwing instruction (currently this means
4294 // an ordinary call) between the end of the previous try-range and now.
4295 bool SawPotentiallyThrowing = false;
4296
4297 // Whether the last callsite entry was for an invoke.
4298 bool PreviousIsInvoke = false;
4299
Duncan Sands4ff179f2007-12-19 07:36:31 +00004300 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00004301 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
4302 I != E; ++I) {
4303 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
4304 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00004305 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00004306 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00004307 continue;
4308 }
4309
Chris Lattnerda4cff12007-12-30 20:50:28 +00004310 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00004311 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00004312
Duncan Sands4ff179f2007-12-19 07:36:31 +00004313 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00004314 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00004315 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00004316
Duncan Sands4ff179f2007-12-19 07:36:31 +00004317 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00004318 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00004319 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00004320 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00004321 continue;
4322
4323 PadRange P = L->second;
4324 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
4325
4326 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
4327 "Inconsistent landing pad map!");
4328
4329 // If some instruction between the previous try-range and this one may
4330 // throw, create a call-site entry with no landing pad for the region
4331 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004332 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004333 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
4334 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00004335 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00004336 }
4337
4338 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00004339 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00004340
Duncan Sands4ff179f2007-12-19 07:36:31 +00004341 if (LandingPad->LandingPadLabel) {
4342 // This try-range is for an invoke.
4343 CallSiteEntry Site = {BeginLabel, LastLabel,
4344 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00004345
Duncan Sands4ff179f2007-12-19 07:36:31 +00004346 // Try to merge with the previous call-site.
4347 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00004348 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00004349 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
4350 // Extend the range of the previous entry.
4351 Prev.EndLabel = Site.EndLabel;
4352 continue;
4353 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004354 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004355
Duncan Sands4ff179f2007-12-19 07:36:31 +00004356 // Otherwise, create a new call-site.
4357 CallSites.push_back(Site);
4358 PreviousIsInvoke = true;
4359 } else {
4360 // Create a gap.
4361 PreviousIsInvoke = false;
4362 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004363 }
4364 }
4365 // If some instruction between the previous try-range and the end of the
4366 // function may throw, create a call-site entry with no landing pad for the
4367 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00004368 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00004369 CallSiteEntry Site = {LastLabel, 0, 0, 0};
4370 CallSites.push_back(Site);
4371 }
4372
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004373 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00004374
4375 // Call sites.
4376 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
4377 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
4378 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
4379 unsigned SizeSites = CallSites.size() * (SiteStartSize +
4380 SiteLengthSize +
4381 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00004382 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00004383 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00004384
Duncan Sands96144f92008-05-07 19:11:09 +00004385 // Type infos.
4386 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
4387 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004388
4389 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00004390 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004391 SizeSites + SizeActions + SizeTypes;
4392
4393 unsigned TotalSize = sizeof(int8_t) + // LPStart format
4394 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00004395 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004396 TypeOffset;
4397
4398 unsigned SizeAlign = (4 - TotalSize) & 3;
4399
4400 // Begin the exception table.
4401 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00004402 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00004403 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004404 for (unsigned i = 0; i != SizeAlign; ++i) {
4405 Asm->EmitInt8(0);
4406 Asm->EOL("Padding");
4407 }
4408 EmitLabel("exception", SubprogramCount);
4409
4410 // Emit the header.
4411 Asm->EmitInt8(DW_EH_PE_omit);
4412 Asm->EOL("LPStart format (DW_EH_PE_omit)");
4413 Asm->EmitInt8(DW_EH_PE_absptr);
4414 Asm->EOL("TType format (DW_EH_PE_absptr)");
4415 Asm->EmitULEB128Bytes(TypeOffset);
4416 Asm->EOL("TType base offset");
4417 Asm->EmitInt8(DW_EH_PE_udata4);
4418 Asm->EOL("Call site format (DW_EH_PE_udata4)");
4419 Asm->EmitULEB128Bytes(SizeSites);
4420 Asm->EOL("Call-site table length");
4421
Duncan Sands241a0c92007-09-05 11:27:52 +00004422 // Emit the landing pad site information.
4423 for (unsigned i = 0; i < CallSites.size(); ++i) {
4424 CallSiteEntry &S = CallSites[i];
4425 const char *BeginTag;
4426 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004427
Duncan Sands241a0c92007-09-05 11:27:52 +00004428 if (!S.BeginLabel) {
4429 BeginTag = "eh_func_begin";
4430 BeginNumber = SubprogramCount;
4431 } else {
4432 BeginTag = "label";
4433 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004434 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004435
Duncan Sands241a0c92007-09-05 11:27:52 +00004436 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004437 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004438 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004439
Duncan Sands241a0c92007-09-05 11:27:52 +00004440 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00004441 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00004442 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004443 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00004444 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004445 }
4446 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004447
Duncan Sands96144f92008-05-07 19:11:09 +00004448 if (!S.PadLabel)
4449 Asm->EmitInt32(0);
4450 else
Duncan Sands241a0c92007-09-05 11:27:52 +00004451 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00004452 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00004453 Asm->EOL("Landing pad");
4454
4455 Asm->EmitULEB128Bytes(S.Action);
4456 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004457 }
4458
4459 // Emit the actions.
4460 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
4461 ActionEntry &Action = Actions[I];
4462
4463 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
4464 Asm->EOL("TypeInfo index");
4465 Asm->EmitSLEB128Bytes(Action.NextAction);
4466 Asm->EOL("Next action");
4467 }
4468
4469 // Emit the type ids.
4470 for (unsigned M = TypeInfos.size(); M; --M) {
4471 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00004472
4473 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004474
4475 if (GV)
4476 O << Asm->getGlobalLinkName(GV);
4477 else
4478 O << "0";
Duncan Sands241a0c92007-09-05 11:27:52 +00004479
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004480 Asm->EOL("TypeInfo");
4481 }
4482
4483 // Emit the filter typeids.
4484 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
4485 unsigned TypeID = FilterIds[j];
4486 Asm->EmitULEB128Bytes(TypeID);
4487 Asm->EOL("Filter TypeInfo index");
4488 }
Duncan Sands241a0c92007-09-05 11:27:52 +00004489
Evan Cheng7e7d1942008-02-29 19:36:59 +00004490 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004491 }
4492
4493public:
4494 //===--------------------------------------------------------------------===//
4495 // Main entry points.
4496 //
Owen Anderson847b99b2008-08-21 00:14:44 +00004497 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00004498 : Dwarf(OS, A, T, "eh")
Dale Johannesen85535762008-04-02 00:25:04 +00004499 , shouldEmitTable(false)
4500 , shouldEmitMoves(false)
4501 , shouldEmitTableModule(false)
4502 , shouldEmitMovesModule(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004503 {}
aslc200b112008-08-16 12:57:46 +00004504
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004505 virtual ~DwarfException() {}
4506
4507 /// SetModuleInfo - Set machine module information when it's known that pass
4508 /// manager has created it. Set by the target AsmPrinter.
4509 void SetModuleInfo(MachineModuleInfo *mmi) {
4510 MMI = mmi;
4511 }
4512
4513 /// BeginModule - Emit all exception information that should come prior to the
4514 /// content.
4515 void BeginModule(Module *M) {
4516 this->M = M;
4517 }
4518
4519 /// EndModule - Emit all exception information that should come after the
4520 /// content.
4521 void EndModule() {
Dale Johannesen85535762008-04-02 00:25:04 +00004522 if (shouldEmitMovesModule || shouldEmitTableModule) {
4523 const std::vector<Function *> Personalities = MMI->getPersonalities();
4524 for (unsigned i =0; i < Personalities.size(); ++i)
4525 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004526
Dale Johannesen85535762008-04-02 00:25:04 +00004527 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
4528 E = EHFrames.end(); I != E; ++I)
4529 EmitEHFrame(*I);
4530 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004531 }
4532
aslc200b112008-08-16 12:57:46 +00004533 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004534 /// emitted immediately after the function entry point.
4535 void BeginFunction(MachineFunction *MF) {
4536 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00004537 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen62f0a6d2008-04-02 17:04:45 +00004538 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00004539
4540 // Map all labels and get rid of any dead landing pads.
4541 MMI->TidyLandingPads();
4542 // If any landing pads survive, we need an EH table.
4543 if (MMI->getLandingPads().size())
4544 shouldEmitTable = true;
4545
4546 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00004547 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00004548 shouldEmitMoves = true;
4549
4550 if (shouldEmitMoves || shouldEmitTable)
4551 // Assumes in correct section after the entry point.
4552 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004553 }
Dale Johannesen85535762008-04-02 00:25:04 +00004554 shouldEmitTableModule |= shouldEmitTable;
4555 shouldEmitMovesModule |= shouldEmitMoves;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004556 }
4557
4558 /// EndFunction - Gather and emit post-function exception information.
4559 ///
4560 void EndFunction() {
Dale Johannesen85535762008-04-02 00:25:04 +00004561 if (shouldEmitMoves || shouldEmitTable) {
4562 EmitLabel("eh_func_end", SubprogramCount);
4563 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004564
Dale Johannesen85535762008-04-02 00:25:04 +00004565 // Save EH frame information
4566 EHFrames.
4567 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingef9211a2007-09-18 01:47:22 +00004568 SubprogramCount,
4569 MMI->getPersonalityIndex(),
4570 MF->getFrameInfo()->hasCalls(),
4571 !MMI->getLandingPads().empty(),
Dale Johannesenfb3ac732007-11-20 23:24:42 +00004572 MMI->getFrameMoves(),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00004573 MF->getFunction()));
Dale Johannesen85535762008-04-02 00:25:04 +00004574 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004575 }
4576};
4577
4578} // End of namespace llvm
4579
4580//===----------------------------------------------------------------------===//
4581
4582/// Emit - Print the abbreviation using the specified Dwarf writer.
4583///
4584void DIEAbbrev::Emit(const DwarfDebug &DD) const {
4585 // Emit its Dwarf tag type.
4586 DD.getAsm()->EmitULEB128Bytes(Tag);
4587 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00004588
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004589 // Emit whether it has children DIEs.
4590 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
4591 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00004592
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004593 // For each attribute description.
4594 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4595 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00004596
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004597 // Emit attribute type.
4598 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
4599 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00004600
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004601 // Emit form type.
4602 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
4603 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
4604 }
4605
4606 // Mark end of abbreviation.
4607 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
4608 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
4609}
4610
4611#ifndef NDEBUG
4612void DIEAbbrev::print(std::ostream &O) {
4613 O << "Abbreviation @"
4614 << std::hex << (intptr_t)this << std::dec
4615 << " "
4616 << TagString(Tag)
4617 << " "
4618 << ChildrenString(ChildrenFlag)
4619 << "\n";
aslc200b112008-08-16 12:57:46 +00004620
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004621 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4622 O << " "
4623 << AttributeString(Data[i].getAttribute())
4624 << " "
4625 << FormEncodingString(Data[i].getForm())
4626 << "\n";
4627 }
4628}
4629void DIEAbbrev::dump() { print(cerr); }
4630#endif
4631
4632//===----------------------------------------------------------------------===//
4633
4634#ifndef NDEBUG
4635void DIEValue::dump() {
4636 print(cerr);
4637}
4638#endif
4639
4640//===----------------------------------------------------------------------===//
4641
4642/// EmitValue - Emit integer of appropriate size.
4643///
4644void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
4645 switch (Form) {
4646 case DW_FORM_flag: // Fall thru
4647 case DW_FORM_ref1: // Fall thru
4648 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
4649 case DW_FORM_ref2: // Fall thru
4650 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
4651 case DW_FORM_ref4: // Fall thru
4652 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
4653 case DW_FORM_ref8: // Fall thru
4654 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4655 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4656 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
4657 default: assert(0 && "DIE Value form not supported yet"); break;
4658 }
4659}
4660
4661/// SizeOf - Determine size of integer value in bytes.
4662///
4663unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4664 switch (Form) {
4665 case DW_FORM_flag: // Fall thru
4666 case DW_FORM_ref1: // Fall thru
4667 case DW_FORM_data1: return sizeof(int8_t);
4668 case DW_FORM_ref2: // Fall thru
4669 case DW_FORM_data2: return sizeof(int16_t);
4670 case DW_FORM_ref4: // Fall thru
4671 case DW_FORM_data4: return sizeof(int32_t);
4672 case DW_FORM_ref8: // Fall thru
4673 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00004674 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4675 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004676 default: assert(0 && "DIE Value form not supported yet"); break;
4677 }
4678 return 0;
4679}
4680
4681//===----------------------------------------------------------------------===//
4682
4683/// EmitValue - Emit string value.
4684///
4685void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
4686 DD.getAsm()->EmitString(String);
4687}
4688
4689//===----------------------------------------------------------------------===//
4690
4691/// EmitValue - Emit label value.
4692///
4693void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004694 bool IsSmall = Form == DW_FORM_data4;
4695 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004696}
4697
4698/// SizeOf - Determine size of label value in bytes.
4699///
4700unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004701 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004702 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004703}
4704
4705//===----------------------------------------------------------------------===//
4706
4707/// EmitValue - Emit label value.
4708///
4709void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004710 bool IsSmall = Form == DW_FORM_data4;
4711 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004712}
4713
4714/// SizeOf - Determine size of label value in bytes.
4715///
4716unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004717 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004718 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004719}
aslc200b112008-08-16 12:57:46 +00004720
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004721//===----------------------------------------------------------------------===//
4722
4723/// EmitValue - Emit delta value.
4724///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004725void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4726 bool IsSmall = Form == DW_FORM_data4;
4727 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4728 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4729}
4730
4731/// SizeOf - Determine size of delta value in bytes.
4732///
4733unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4734 if (Form == DW_FORM_data4) return 4;
4735 return DD.getTargetData()->getPointerSize();
4736}
aslc200b112008-08-16 12:57:46 +00004737
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004738//===----------------------------------------------------------------------===//
4739
4740/// EmitValue - Emit delta value.
4741///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004742void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4743 bool IsSmall = Form == DW_FORM_data4;
4744 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4745}
4746
4747/// SizeOf - Determine size of delta value in bytes.
4748///
4749unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4750 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004751 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004752}
4753
4754//===----------------------------------------------------------------------===//
4755
4756/// EmitValue - Emit debug information entry offset.
4757///
4758void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4759 DD.getAsm()->EmitInt32(Entry->getOffset());
4760}
aslc200b112008-08-16 12:57:46 +00004761
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004762//===----------------------------------------------------------------------===//
4763
4764/// ComputeSize - calculate the size of the block.
4765///
4766unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4767 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004768 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004769
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004770 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4771 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4772 }
4773 }
4774 return Size;
4775}
4776
4777/// EmitValue - Emit block data.
4778///
4779void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4780 switch (Form) {
4781 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4782 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4783 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4784 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4785 default: assert(0 && "Improper form for block"); break;
4786 }
aslc200b112008-08-16 12:57:46 +00004787
Owen Anderson88dd6232008-06-24 21:44:59 +00004788 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004789
4790 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4791 DD.getAsm()->EOL();
4792 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4793 }
4794}
4795
4796/// SizeOf - Determine size of block data in bytes.
4797///
4798unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4799 switch (Form) {
4800 case DW_FORM_block1: return Size + sizeof(int8_t);
4801 case DW_FORM_block2: return Size + sizeof(int16_t);
4802 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004803 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004804 default: assert(0 && "Improper form for block"); break;
4805 }
4806 return 0;
4807}
4808
4809//===----------------------------------------------------------------------===//
4810/// DIE Implementation
4811
4812DIE::~DIE() {
4813 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4814 delete Children[i];
4815}
aslc200b112008-08-16 12:57:46 +00004816
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004817/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4818///
4819void DIE::AddSiblingOffset() {
4820 DIEInteger *DI = new DIEInteger(0);
4821 Values.insert(Values.begin(), DI);
4822 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4823}
4824
4825/// Profile - Used to gather unique data for the value folding set.
4826///
4827void DIE::Profile(FoldingSetNodeID &ID) {
4828 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004829
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004830 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4831 ID.AddPointer(Children[i]);
4832
4833 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4834 ID.AddPointer(Values[j]);
4835}
4836
4837#ifndef NDEBUG
4838void DIE::print(std::ostream &O, unsigned IncIndent) {
4839 static unsigned IndentCount = 0;
4840 IndentCount += IncIndent;
4841 const std::string Indent(IndentCount, ' ');
4842 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004843
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004844 if (!isBlock) {
4845 O << Indent
4846 << "Die: "
4847 << "0x" << std::hex << (intptr_t)this << std::dec
4848 << ", Offset: " << Offset
4849 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004850 << "\n";
4851
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004852 O << Indent
4853 << TagString(Abbrev.getTag())
4854 << " "
4855 << ChildrenString(Abbrev.getChildrenFlag());
4856 } else {
4857 O << "Size: " << Size;
4858 }
4859 O << "\n";
4860
Owen Anderson88dd6232008-06-24 21:44:59 +00004861 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004862
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004863 IndentCount += 2;
4864 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4865 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004866
4867 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004868 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004869 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004870 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004871
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004872 O << " "
4873 << FormEncodingString(Data[i].getForm())
4874 << " ";
4875 Values[i]->print(O);
4876 O << "\n";
4877 }
4878 IndentCount -= 2;
4879
4880 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4881 Children[j]->print(O, 4);
4882 }
aslc200b112008-08-16 12:57:46 +00004883
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004884 if (!isBlock) O << "\n";
4885 IndentCount -= IncIndent;
4886}
4887
4888void DIE::dump() {
4889 print(cerr);
4890}
4891#endif
4892
4893//===----------------------------------------------------------------------===//
4894/// DwarfWriter Implementation
4895///
4896
Devang Patelaa1e8432009-01-08 23:40:34 +00004897DwarfWriter::DwarfWriter() : ImmutablePass(&ID), DD(NULL), DE(NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004898}
4899
4900DwarfWriter::~DwarfWriter() {
4901 delete DE;
4902 delete DD;
4903}
4904
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004905/// BeginModule - Emit all Dwarf sections that should come prior to the
4906/// content.
Devang Patelaa1e8432009-01-08 23:40:34 +00004907void DwarfWriter::BeginModule(Module *M,
4908 MachineModuleInfo *MMI,
4909 raw_ostream &OS, AsmPrinter *A,
4910 const TargetAsmInfo *T) {
4911 DE = new DwarfException(OS, A, T);
4912 DD = new DwarfDebug(OS, A, T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004913 DE->BeginModule(M);
4914 DD->BeginModule(M);
Devang Patelaa1e8432009-01-08 23:40:34 +00004915 DD->SetModuleInfo(MMI);
4916 DE->SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004917}
4918
4919/// EndModule - Emit all Dwarf sections that should come after the content.
4920///
4921void DwarfWriter::EndModule() {
4922 DE->EndModule();
4923 DD->EndModule();
4924}
4925
aslc200b112008-08-16 12:57:46 +00004926/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004927/// emitted immediately after the function entry point.
4928void DwarfWriter::BeginFunction(MachineFunction *MF) {
4929 DE->BeginFunction(MF);
4930 DD->BeginFunction(MF);
4931}
4932
4933/// EndFunction - Gather and emit post-function debug information.
4934///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004935void DwarfWriter::EndFunction(MachineFunction *MF) {
4936 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004937 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004938
Bill Wendling5b4796a2008-07-22 00:53:37 +00004939 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004940 // Clear function debug information.
4941 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004942}
Devang Patelcb59fd42009-01-12 19:17:34 +00004943
4944/// RecordSourceLine - Records location information and associates it with a
4945/// label. Returns a unique label ID used to generate a label and provide
4946/// correspondence to the source line list.
4947unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4948 unsigned Src) {
4949 return DD->RecordSourceLine(Line, Col, Src);
4950}
4951
4952/// RecordSource - Register a source file with debug info. Returns an source
4953/// ID.
4954unsigned DwarfWriter::RecordSource(const std::string &Dir,
4955 const std::string &File) {
4956 return DD->RecordSource(Dir, File);
4957}
4958
4959/// RecordRegionStart - Indicate the start of a region.
4960unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
4961 return DD->RecordRegionStart(V);
4962}
4963
4964/// RecordRegionEnd - Indicate the end of a region.
4965unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
4966 return DD->RecordRegionEnd(V);
4967}
4968
4969/// getRecordSourceLineCount - Count source lines.
4970unsigned DwarfWriter::getRecordSourceLineCount() {
4971 return DD->getRecordSourceLineCount();
4972}