blob: 6121d4bfa66428a4f5a76f044ce0ff4b296ed651 [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"
Devang Patel2da0cc42009-01-15 23:41:32 +000022#include "llvm/Constants.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CodeGen/AsmPrinter.h"
24#include "llvm/CodeGen/MachineModuleInfo.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineLocation.h"
Devang Patelfc187162009-01-05 17:57:47 +000027#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/Dwarf.h"
30#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/DataTypes.h"
32#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000033#include "llvm/Support/raw_ostream.h"
Dan Gohman80bbde72007-09-24 21:32:18 +000034#include "llvm/System/Path.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000036#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Target/TargetData.h"
38#include "llvm/Target/TargetFrameInfo.h"
39#include "llvm/Target/TargetInstrInfo.h"
40#include "llvm/Target/TargetMachine.h"
41#include "llvm/Target/TargetOptions.h"
42#include <ostream>
43#include <string>
44using namespace llvm;
45using namespace llvm::dwarf;
46
Devang Patelaa1e8432009-01-08 23:40:34 +000047static RegisterPass<DwarfWriter>
48X("dwarfwriter", "DWARF Information Writer");
49char DwarfWriter::ID = 0;
50
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051namespace llvm {
aslc200b112008-08-16 12:57:46 +000052
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053//===----------------------------------------------------------------------===//
54
55/// Configuration values for initial hash set sizes (log2).
56///
Bill Wendling824a8bf2009-02-03 21:17:20 +000057static const unsigned InitDiesSetSize = 9; // log2(512)
58static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
59static const unsigned InitValuesSetSize = 9; // log2(512)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060
61//===----------------------------------------------------------------------===//
62/// Forward declarations.
63///
64class DIE;
65class DIEValue;
66
67//===----------------------------------------------------------------------===//
Devang Patelb3907da2009-01-05 23:03:32 +000068/// Utility routines.
69///
Devang Patelb28de842009-01-17 08:01:33 +000070/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
71/// specified value in their initializer somewhere.
Devang Patelb3907da2009-01-05 23:03:32 +000072static void
73getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
Devang Patelb28de842009-01-17 08:01:33 +000074 // Scan though value users.
Devang Patelb3907da2009-01-05 23:03:32 +000075 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
76 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
Devang Patelb28de842009-01-17 08:01:33 +000077 // If the user is a GlobalVariable then add to result.
Devang Patelb3907da2009-01-05 23:03:32 +000078 Result.push_back(GV);
79 } else if (Constant *C = dyn_cast<Constant>(*I)) {
Devang Patelb28de842009-01-17 08:01:33 +000080 // If the user is a constant variable then scan its users.
Devang Patelb3907da2009-01-05 23:03:32 +000081 getGlobalVariablesUsing(C, Result);
82 }
83 }
84}
85
Devang Patelb28de842009-01-17 08:01:33 +000086/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
87/// named GlobalVariable.
Devang Patelb3907da2009-01-05 23:03:32 +000088static void
89getGlobalVariablesUsing(Module &M, const std::string &RootName,
90 std::vector<GlobalVariable*> &Result) {
91 std::vector<const Type*> FieldTypes;
92 FieldTypes.push_back(Type::Int32Ty);
93 FieldTypes.push_back(Type::Int32Ty);
94
Devang Patelb28de842009-01-17 08:01:33 +000095 // Get the GlobalVariable root.
Devang Patelb3907da2009-01-05 23:03:32 +000096 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
97 StructType::get(FieldTypes));
98
Devang Patelb28de842009-01-17 08:01:33 +000099 // If present and linkonce then scan for users.
Devang Patelb3907da2009-01-05 23:03:32 +0000100 if (UseRoot && UseRoot->hasLinkOnceLinkage())
101 getGlobalVariablesUsing(UseRoot, Result);
102}
103
Devang Patel2da0cc42009-01-15 23:41:32 +0000104/// getGlobalVariable - Return either a direct or cast Global value.
105///
106static GlobalVariable *getGlobalVariable(Value *V) {
107 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
108 return GV;
109 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
110 if (CE->getOpcode() == Instruction::BitCast) {
111 return dyn_cast<GlobalVariable>(CE->getOperand(0));
112 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
113 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
114 if (!CE->getOperand(i)->isNullValue())
115 return NULL;
116 }
117 return dyn_cast<GlobalVariable>(CE->getOperand(0));
118 }
119 }
120 return NULL;
121}
122
Devang Patelb3907da2009-01-05 23:03:32 +0000123//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124/// DWLabel - Labels are used to track locations in the assembler file.
aslc200b112008-08-16 12:57:46 +0000125/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
126/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer37c7cea2007-08-05 20:06:04 +0000127/// unique in that category.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128class DWLabel {
129public:
130 /// Tag - Label category tag. Should always be a staticly declared C string.
131 ///
132 const char *Tag;
aslc200b112008-08-16 12:57:46 +0000133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 /// Number - Value to make label unique.
135 ///
136 unsigned Number;
137
138 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
aslc200b112008-08-16 12:57:46 +0000139
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 void Profile(FoldingSetNodeID &ID) const {
141 ID.AddString(std::string(Tag));
142 ID.AddInteger(Number);
143 }
aslc200b112008-08-16 12:57:46 +0000144
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145#ifndef NDEBUG
146 void print(std::ostream *O) const {
147 if (O) print(*O);
148 }
149 void print(std::ostream &O) const {
150 O << "." << Tag;
151 if (Number) O << Number;
152 }
153#endif
154};
155
156//===----------------------------------------------------------------------===//
157/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
158/// Dwarf abbreviation.
159class DIEAbbrevData {
160private:
161 /// Attribute - Dwarf attribute code.
162 ///
163 unsigned Attribute;
aslc200b112008-08-16 12:57:46 +0000164
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 /// Form - Dwarf form code.
aslc200b112008-08-16 12:57:46 +0000166 ///
167 unsigned Form;
168
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169public:
170 DIEAbbrevData(unsigned A, unsigned F)
171 : Attribute(A)
172 , Form(F)
173 {}
aslc200b112008-08-16 12:57:46 +0000174
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 // Accessors.
176 unsigned getAttribute() const { return Attribute; }
177 unsigned getForm() const { return Form; }
178
179 /// Profile - Used to gather unique data for the abbreviation folding set.
180 ///
181 void Profile(FoldingSetNodeID &ID)const {
182 ID.AddInteger(Attribute);
183 ID.AddInteger(Form);
184 }
185};
186
187//===----------------------------------------------------------------------===//
188/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
189/// information object.
190class DIEAbbrev : public FoldingSetNode {
191private:
192 /// Tag - Dwarf tag code.
193 ///
194 unsigned Tag;
aslc200b112008-08-16 12:57:46 +0000195
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 /// Unique number for node.
197 ///
198 unsigned Number;
199
200 /// ChildrenFlag - Dwarf children flag.
201 ///
202 unsigned ChildrenFlag;
203
204 /// Data - Raw data bytes for abbreviation.
205 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000206 SmallVector<DIEAbbrevData, 8> Data;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207
208public:
209
210 DIEAbbrev(unsigned T, unsigned C)
211 : Tag(T)
212 , ChildrenFlag(C)
213 , Data()
214 {}
215 ~DIEAbbrev() {}
aslc200b112008-08-16 12:57:46 +0000216
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 // Accessors.
218 unsigned getTag() const { return Tag; }
219 unsigned getNumber() const { return Number; }
220 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000221 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 void setTag(unsigned T) { Tag = T; }
223 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
224 void setNumber(unsigned N) { Number = N; }
aslc200b112008-08-16 12:57:46 +0000225
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 /// AddAttribute - Adds another set of attribute information to the
227 /// abbreviation.
228 void AddAttribute(unsigned Attribute, unsigned Form) {
229 Data.push_back(DIEAbbrevData(Attribute, Form));
230 }
aslc200b112008-08-16 12:57:46 +0000231
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 /// AddFirstAttribute - Adds a set of attribute information to the front
233 /// of the abbreviation.
234 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
235 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
236 }
aslc200b112008-08-16 12:57:46 +0000237
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 /// Profile - Used to gather unique data for the abbreviation folding set.
239 ///
240 void Profile(FoldingSetNodeID &ID) {
241 ID.AddInteger(Tag);
242 ID.AddInteger(ChildrenFlag);
aslc200b112008-08-16 12:57:46 +0000243
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 // For each attribute description.
245 for (unsigned i = 0, N = Data.size(); i < N; ++i)
246 Data[i].Profile(ID);
247 }
aslc200b112008-08-16 12:57:46 +0000248
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 /// Emit - Print the abbreviation using the specified Dwarf writer.
250 ///
aslc200b112008-08-16 12:57:46 +0000251 void Emit(const DwarfDebug &DD) const;
252
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253#ifndef NDEBUG
254 void print(std::ostream *O) {
255 if (O) print(*O);
256 }
257 void print(std::ostream &O);
258 void dump();
259#endif
260};
261
262//===----------------------------------------------------------------------===//
263/// DIE - A structured debug information entry. Has an abbreviation which
264/// describes it's organization.
265class DIE : public FoldingSetNode {
266protected:
267 /// Abbrev - Buffer for constructing abbreviation.
268 ///
269 DIEAbbrev Abbrev;
aslc200b112008-08-16 12:57:46 +0000270
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 /// Offset - Offset in debug info section.
272 ///
273 unsigned Offset;
aslc200b112008-08-16 12:57:46 +0000274
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 /// Size - Size of instance + children.
276 ///
277 unsigned Size;
aslc200b112008-08-16 12:57:46 +0000278
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 /// Children DIEs.
280 ///
281 std::vector<DIE *> Children;
aslc200b112008-08-16 12:57:46 +0000282
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 /// Attributes values.
284 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000285 SmallVector<DIEValue*, 32> Values;
aslc200b112008-08-16 12:57:46 +0000286
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000288 explicit DIE(unsigned Tag)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 : Abbrev(Tag, DW_CHILDREN_no)
290 , Offset(0)
291 , Size(0)
292 , Children()
293 , Values()
294 {}
295 virtual ~DIE();
aslc200b112008-08-16 12:57:46 +0000296
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 // Accessors.
298 DIEAbbrev &getAbbrev() { return Abbrev; }
299 unsigned getAbbrevNumber() const {
300 return Abbrev.getNumber();
301 }
302 unsigned getTag() const { return Abbrev.getTag(); }
303 unsigned getOffset() const { return Offset; }
304 unsigned getSize() const { return Size; }
305 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000306 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
308 void setOffset(unsigned O) { Offset = O; }
309 void setSize(unsigned S) { Size = S; }
aslc200b112008-08-16 12:57:46 +0000310
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 /// AddValue - Add a value and attributes to a DIE.
312 ///
313 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
314 Abbrev.AddAttribute(Attribute, Form);
315 Values.push_back(Value);
316 }
aslc200b112008-08-16 12:57:46 +0000317
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 /// SiblingOffset - Return the offset of the debug information entry's
319 /// sibling.
320 unsigned SiblingOffset() const { return Offset + Size; }
aslc200b112008-08-16 12:57:46 +0000321
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
323 ///
324 void AddSiblingOffset();
325
326 /// AddChild - Add a child to the DIE.
327 ///
328 void AddChild(DIE *Child) {
329 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
330 Children.push_back(Child);
331 }
aslc200b112008-08-16 12:57:46 +0000332
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 /// Detach - Detaches objects connected to it after copying.
334 ///
335 void Detach() {
336 Children.clear();
337 }
aslc200b112008-08-16 12:57:46 +0000338
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 /// Profile - Used to gather unique data for the value folding set.
340 ///
341 void Profile(FoldingSetNodeID &ID) ;
aslc200b112008-08-16 12:57:46 +0000342
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343#ifndef NDEBUG
344 void print(std::ostream *O, unsigned IncIndent = 0) {
345 if (O) print(*O, IncIndent);
346 }
347 void print(std::ostream &O, unsigned IncIndent = 0);
348 void dump();
349#endif
350};
351
352//===----------------------------------------------------------------------===//
353/// DIEValue - A debug information entry value.
354///
355class DIEValue : public FoldingSetNode {
356public:
357 enum {
358 isInteger,
359 isString,
360 isLabel,
361 isAsIsLabel,
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000362 isSectionOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 isDelta,
364 isEntry,
365 isBlock
366 };
aslc200b112008-08-16 12:57:46 +0000367
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 /// Type - Type of data stored in the value.
369 ///
370 unsigned Type;
aslc200b112008-08-16 12:57:46 +0000371
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000372 explicit DIEValue(unsigned T)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 : Type(T)
374 {}
375 virtual ~DIEValue() {}
aslc200b112008-08-16 12:57:46 +0000376
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 // Accessors
378 unsigned getType() const { return Type; }
aslc200b112008-08-16 12:57:46 +0000379
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 // Implement isa/cast/dyncast.
381 static bool classof(const DIEValue *) { return true; }
aslc200b112008-08-16 12:57:46 +0000382
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 /// EmitValue - Emit value via the Dwarf writer.
384 ///
385 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
aslc200b112008-08-16 12:57:46 +0000386
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 /// SizeOf - Return the size of a value in bytes.
388 ///
389 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
aslc200b112008-08-16 12:57:46 +0000390
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 /// Profile - Used to gather unique data for the value folding set.
392 ///
393 virtual void Profile(FoldingSetNodeID &ID) = 0;
aslc200b112008-08-16 12:57:46 +0000394
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395#ifndef NDEBUG
396 void print(std::ostream *O) {
397 if (O) print(*O);
398 }
399 virtual void print(std::ostream &O) = 0;
400 void dump();
401#endif
402};
403
404//===----------------------------------------------------------------------===//
405/// DWInteger - An integer value DIE.
aslc200b112008-08-16 12:57:46 +0000406///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407class DIEInteger : public DIEValue {
408private:
409 uint64_t Integer;
aslc200b112008-08-16 12:57:46 +0000410
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000412 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413
414 // Implement isa/cast/dyncast.
415 static bool classof(const DIEInteger *) { return true; }
416 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
aslc200b112008-08-16 12:57:46 +0000417
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 /// BestForm - Choose the best form for integer.
419 ///
420 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
421 if (IsSigned) {
422 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
423 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
424 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
425 } else {
426 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
427 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
428 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
429 }
430 return DW_FORM_data8;
431 }
aslc200b112008-08-16 12:57:46 +0000432
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 /// EmitValue - Emit integer of appropriate size.
434 ///
435 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000436
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 /// SizeOf - Determine size of integer value in bytes.
438 ///
439 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000440
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 /// Profile - Used to gather unique data for the value folding set.
442 ///
443 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
444 ID.AddInteger(isInteger);
445 ID.AddInteger(Integer);
446 }
447 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
aslc200b112008-08-16 12:57:46 +0000448
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449#ifndef NDEBUG
450 virtual void print(std::ostream &O) {
451 O << "Int: " << (int64_t)Integer
452 << " 0x" << std::hex << Integer << std::dec;
453 }
454#endif
455};
456
457//===----------------------------------------------------------------------===//
458/// DIEString - A string value DIE.
aslc200b112008-08-16 12:57:46 +0000459///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460class DIEString : public DIEValue {
461public:
462 const std::string String;
aslc200b112008-08-16 12:57:46 +0000463
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000464 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465
466 // Implement isa/cast/dyncast.
467 static bool classof(const DIEString *) { return true; }
468 static bool classof(const DIEValue *S) { return S->Type == isString; }
aslc200b112008-08-16 12:57:46 +0000469
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 /// EmitValue - Emit string value.
471 ///
472 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000473
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 /// SizeOf - Determine size of string value in bytes.
475 ///
476 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
477 return String.size() + sizeof(char); // sizeof('\0');
478 }
aslc200b112008-08-16 12:57:46 +0000479
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 /// Profile - Used to gather unique data for the value folding set.
481 ///
482 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
483 ID.AddInteger(isString);
484 ID.AddString(String);
485 }
486 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
aslc200b112008-08-16 12:57:46 +0000487
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488#ifndef NDEBUG
489 virtual void print(std::ostream &O) {
490 O << "Str: \"" << String << "\"";
491 }
492#endif
493};
494
495//===----------------------------------------------------------------------===//
496/// DIEDwarfLabel - A Dwarf internal label expression DIE.
497//
498class DIEDwarfLabel : public DIEValue {
499public:
500
501 const DWLabel Label;
aslc200b112008-08-16 12:57:46 +0000502
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000503 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504
505 // Implement isa/cast/dyncast.
506 static bool classof(const DIEDwarfLabel *) { return true; }
507 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
aslc200b112008-08-16 12:57:46 +0000508
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 /// EmitValue - Emit label value.
510 ///
511 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000512
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 /// SizeOf - Determine size of label value in bytes.
514 ///
515 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000516
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 /// Profile - Used to gather unique data for the value folding set.
518 ///
519 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
520 ID.AddInteger(isLabel);
521 Label.Profile(ID);
522 }
523 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
aslc200b112008-08-16 12:57:46 +0000524
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525#ifndef NDEBUG
526 virtual void print(std::ostream &O) {
527 O << "Lbl: ";
528 Label.print(O);
529 }
530#endif
531};
532
533
534//===----------------------------------------------------------------------===//
535/// DIEObjectLabel - A label to an object in code or data.
536//
537class DIEObjectLabel : public DIEValue {
538public:
539 const std::string Label;
aslc200b112008-08-16 12:57:46 +0000540
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000541 explicit DIEObjectLabel(const std::string &L)
542 : DIEValue(isAsIsLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543
544 // Implement isa/cast/dyncast.
545 static bool classof(const DIEObjectLabel *) { return true; }
546 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
aslc200b112008-08-16 12:57:46 +0000547
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 /// EmitValue - Emit label value.
549 ///
550 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000551
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 /// SizeOf - Determine size of label value in bytes.
553 ///
554 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000555
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 /// Profile - Used to gather unique data for the value folding set.
557 ///
558 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
559 ID.AddInteger(isAsIsLabel);
560 ID.AddString(Label);
561 }
562 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
563
564#ifndef NDEBUG
565 virtual void print(std::ostream &O) {
566 O << "Obj: " << Label;
567 }
568#endif
569};
570
571//===----------------------------------------------------------------------===//
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000572/// DIESectionOffset - A section offset DIE.
573//
574class DIESectionOffset : public DIEValue {
575public:
576 const DWLabel Label;
577 const DWLabel Section;
578 bool IsEH : 1;
579 bool UseSet : 1;
aslc200b112008-08-16 12:57:46 +0000580
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000581 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
582 bool isEH = false, bool useSet = true)
583 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
584 IsEH(isEH), UseSet(useSet) {}
585
586 // Implement isa/cast/dyncast.
587 static bool classof(const DIESectionOffset *) { return true; }
588 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
aslc200b112008-08-16 12:57:46 +0000589
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000590 /// EmitValue - Emit section offset.
591 ///
592 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000593
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000594 /// SizeOf - Determine size of section offset value in bytes.
595 ///
596 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000597
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000598 /// Profile - Used to gather unique data for the value folding set.
599 ///
600 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
601 const DWLabel &Section) {
602 ID.AddInteger(isSectionOffset);
603 Label.Profile(ID);
604 Section.Profile(ID);
605 // IsEH and UseSet are specific to the Label/Section that we will emit
606 // the offset for; so Label/Section are enough for uniqueness.
607 }
608 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
609
610#ifndef NDEBUG
611 virtual void print(std::ostream &O) {
612 O << "Off: ";
613 Label.print(O);
614 O << "-";
615 Section.print(O);
616 O << "-" << IsEH << "-" << UseSet;
617 }
618#endif
619};
620
621//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622/// DIEDelta - A simple label difference DIE.
aslc200b112008-08-16 12:57:46 +0000623///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624class DIEDelta : public DIEValue {
625public:
626 const DWLabel LabelHi;
627 const DWLabel LabelLo;
aslc200b112008-08-16 12:57:46 +0000628
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
630 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
631
632 // Implement isa/cast/dyncast.
633 static bool classof(const DIEDelta *) { return true; }
634 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
aslc200b112008-08-16 12:57:46 +0000635
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 /// EmitValue - Emit delta value.
637 ///
638 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000639
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 /// SizeOf - Determine size of delta value in bytes.
641 ///
642 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000643
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 /// Profile - Used to gather unique data for the value folding set.
645 ///
646 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
647 const DWLabel &LabelLo) {
648 ID.AddInteger(isDelta);
649 LabelHi.Profile(ID);
650 LabelLo.Profile(ID);
651 }
652 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
653
654#ifndef NDEBUG
655 virtual void print(std::ostream &O) {
656 O << "Del: ";
657 LabelHi.print(O);
658 O << "-";
659 LabelLo.print(O);
660 }
661#endif
662};
663
664//===----------------------------------------------------------------------===//
665/// DIEntry - A pointer to another debug information entry. An instance of this
666/// class can also be used as a proxy for a debug information entry not yet
667/// defined (ie. types.)
668class DIEntry : public DIEValue {
669public:
670 DIE *Entry;
aslc200b112008-08-16 12:57:46 +0000671
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000672 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
aslc200b112008-08-16 12:57:46 +0000673
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674 // Implement isa/cast/dyncast.
675 static bool classof(const DIEntry *) { return true; }
676 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
aslc200b112008-08-16 12:57:46 +0000677
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 /// EmitValue - Emit debug information entry offset.
679 ///
680 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000681
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 /// SizeOf - Determine size of debug information entry in bytes.
683 ///
684 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
685 return sizeof(int32_t);
686 }
aslc200b112008-08-16 12:57:46 +0000687
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688 /// Profile - Used to gather unique data for the value folding set.
689 ///
690 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
691 ID.AddInteger(isEntry);
692 ID.AddPointer(Entry);
693 }
694 virtual void Profile(FoldingSetNodeID &ID) {
695 ID.AddInteger(isEntry);
aslc200b112008-08-16 12:57:46 +0000696
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 if (Entry) {
698 ID.AddPointer(Entry);
699 } else {
700 ID.AddPointer(this);
701 }
702 }
aslc200b112008-08-16 12:57:46 +0000703
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704#ifndef NDEBUG
705 virtual void print(std::ostream &O) {
706 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
707 }
708#endif
709};
710
711//===----------------------------------------------------------------------===//
712/// DIEBlock - A block of values. Primarily used for location expressions.
713//
714class DIEBlock : public DIEValue, public DIE {
715public:
716 unsigned Size; // Size in bytes excluding size header.
aslc200b112008-08-16 12:57:46 +0000717
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 DIEBlock()
719 : DIEValue(isBlock)
720 , DIE(0)
721 , Size(0)
722 {}
723 ~DIEBlock() {
724 }
aslc200b112008-08-16 12:57:46 +0000725
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726 // Implement isa/cast/dyncast.
727 static bool classof(const DIEBlock *) { return true; }
728 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
aslc200b112008-08-16 12:57:46 +0000729
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 /// ComputeSize - calculate the size of the block.
731 ///
732 unsigned ComputeSize(DwarfDebug &DD);
aslc200b112008-08-16 12:57:46 +0000733
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 /// BestForm - Choose the best form for data.
735 ///
736 unsigned BestForm() const {
737 if ((unsigned char)Size == Size) return DW_FORM_block1;
738 if ((unsigned short)Size == Size) return DW_FORM_block2;
739 if ((unsigned int)Size == Size) return DW_FORM_block4;
740 return DW_FORM_block;
741 }
742
743 /// EmitValue - Emit block data.
744 ///
745 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000746
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747 /// SizeOf - Determine size of block data in bytes.
748 ///
749 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000750
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751
752 /// Profile - Used to gather unique data for the value folding set.
753 ///
754 virtual void Profile(FoldingSetNodeID &ID) {
755 ID.AddInteger(isBlock);
756 DIE::Profile(ID);
757 }
aslc200b112008-08-16 12:57:46 +0000758
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759#ifndef NDEBUG
760 virtual void print(std::ostream &O) {
761 O << "Blk: ";
762 DIE::print(O, 5);
763 }
764#endif
765};
766
767//===----------------------------------------------------------------------===//
768/// CompileUnit - This dwarf writer support class manages information associate
769/// with a source file.
770class CompileUnit {
771private:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772 /// ID - File identifier for source.
773 ///
774 unsigned ID;
aslc200b112008-08-16 12:57:46 +0000775
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776 /// Die - Compile unit debug information entry.
777 ///
778 DIE *Die;
aslc200b112008-08-16 12:57:46 +0000779
Devang Patel42f6bed2009-01-13 23:54:55 +0000780 /// GVToDieMap - Tracks the mapping of unit level debug informaton
781 /// variables to debug information entries.
Devang Patel56b1d132009-01-20 00:58:55 +0000782 std::map<GlobalVariable *, DIE *> GVToDieMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000783
Devang Patel42f6bed2009-01-13 23:54:55 +0000784 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patel56b1d132009-01-20 00:58:55 +0000786 std::map<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787
788 /// Globals - A map of globally visible named entities for this unit.
789 ///
790 std::map<std::string, DIE *> Globals;
791
792 /// DiesSet - Used to uniquely define dies within the compile unit.
793 ///
794 FoldingSet<DIE> DiesSet;
aslc200b112008-08-16 12:57:46 +0000795
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796public:
Devang Patelb3907da2009-01-05 23:03:32 +0000797 CompileUnit(unsigned I, DIE *D)
Devang Patel42f6bed2009-01-13 23:54:55 +0000798 : ID(I), Die(D), GVToDieMap(),
Devang Patel5302e672009-01-17 06:51:37 +0000799 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Patelb3907da2009-01-05 23:03:32 +0000800 {}
801
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802 ~CompileUnit() {
803 delete Die;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 }
aslc200b112008-08-16 12:57:46 +0000805
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 // Accessors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807 unsigned getID() const { return ID; }
808 DIE* getDie() const { return Die; }
809 std::map<std::string, DIE *> &getGlobals() { return Globals; }
810
811 /// hasContent - Return true if this compile unit has something to write out.
812 ///
813 bool hasContent() const {
814 return !Die->getChildren().empty();
815 }
816
817 /// AddGlobal - Add a new global entity to the compile unit.
818 ///
819 void AddGlobal(const std::string &Name, DIE *Die) {
820 Globals[Name] = Die;
821 }
aslc200b112008-08-16 12:57:46 +0000822
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000824 /// specified debug variable.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000825 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
826 return GVToDieMap[GV];
827 }
aslc200b112008-08-16 12:57:46 +0000828
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000829 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000830 /// specified debug variable.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000831 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
832 return GVToDIEntryMap[GV];
833 }
aslc200b112008-08-16 12:57:46 +0000834
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835 /// AddDie - Adds or interns the DIE to the compile unit.
836 ///
837 DIE *AddDie(DIE &Buffer) {
838 FoldingSetNodeID ID;
839 Buffer.Profile(ID);
840 void *Where;
841 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
aslc200b112008-08-16 12:57:46 +0000842
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843 if (!Die) {
844 Die = new DIE(Buffer);
845 DiesSet.InsertNode(Die, Where);
846 this->Die->AddChild(Die);
847 Buffer.Detach();
848 }
aslc200b112008-08-16 12:57:46 +0000849
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 return Die;
851 }
852};
853
854//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +0000855/// Dwarf - Emits general Dwarf directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856///
857class Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858protected:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 //===--------------------------------------------------------------------===//
860 // Core attributes used by the Dwarf writer.
861 //
aslc200b112008-08-16 12:57:46 +0000862
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863 //
864 /// O - Stream to .s file.
865 ///
Owen Anderson847b99b2008-08-21 00:14:44 +0000866 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867
868 /// Asm - Target of Dwarf emission.
869 ///
870 AsmPrinter *Asm;
aslc200b112008-08-16 12:57:46 +0000871
Bill Wendlingac9639d2008-07-01 23:34:48 +0000872 /// TAI - Target asm information.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873 const TargetAsmInfo *TAI;
aslc200b112008-08-16 12:57:46 +0000874
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875 /// TD - Target data.
876 const TargetData *TD;
aslc200b112008-08-16 12:57:46 +0000877
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 /// RI - Register Information.
Dan Gohman1e57df32008-02-10 18:45:23 +0000879 const TargetRegisterInfo *RI;
aslc200b112008-08-16 12:57:46 +0000880
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881 /// M - Current module.
882 ///
883 Module *M;
aslc200b112008-08-16 12:57:46 +0000884
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000885 /// MF - Current machine function.
886 ///
887 MachineFunction *MF;
aslc200b112008-08-16 12:57:46 +0000888
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889 /// MMI - Collected machine module information.
890 ///
891 MachineModuleInfo *MMI;
aslc200b112008-08-16 12:57:46 +0000892
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000893 /// SubprogramCount - The running count of functions being compiled.
894 ///
895 unsigned SubprogramCount;
aslc200b112008-08-16 12:57:46 +0000896
Chris Lattnerb3876c72007-09-24 03:35:37 +0000897 /// Flavor - A unique string indicating what dwarf producer this is, used to
898 /// unique labels.
899 const char * const Flavor;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000900
901 unsigned SetCounter;
Owen Anderson847b99b2008-08-21 00:14:44 +0000902 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattnerb3876c72007-09-24 03:35:37 +0000903 const char *flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904 : O(OS)
905 , Asm(A)
906 , TAI(T)
907 , TD(Asm->TM.getTargetData())
908 , RI(Asm->TM.getRegisterInfo())
909 , M(NULL)
910 , MF(NULL)
911 , MMI(NULL)
912 , SubprogramCount(0)
Chris Lattnerb3876c72007-09-24 03:35:37 +0000913 , Flavor(flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914 , SetCounter(1)
915 {
916 }
917
918public:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 //===--------------------------------------------------------------------===//
920 // Accessors.
921 //
922 AsmPrinter *getAsm() const { return Asm; }
923 MachineModuleInfo *getMMI() const { return MMI; }
924 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohmancfb72b22007-09-27 23:12:31 +0000925 const TargetData *getTargetData() const { return TD; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000926
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000927 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
928 const {
929 if (isInSection && TAI->getDwarfSectionOffsetDirective())
930 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohmancfb72b22007-09-27 23:12:31 +0000931 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000932 O << TAI->getData32bitsDirective();
933 else
934 O << TAI->getData64bitsDirective();
935 }
aslc200b112008-08-16 12:57:46 +0000936
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000937 /// PrintLabelName - Print label name in form used by Dwarf writer.
938 ///
939 void PrintLabelName(DWLabel Label) const {
940 PrintLabelName(Label.Tag, Label.Number);
941 }
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000942 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000943 O << TAI->getPrivateGlobalPrefix() << Tag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000944 if (Number) O << Number;
945 }
aslc200b112008-08-16 12:57:46 +0000946
Chris Lattnerb3876c72007-09-24 03:35:37 +0000947 void PrintLabelName(const char *Tag, unsigned Number,
948 const char *Suffix) const {
949 O << TAI->getPrivateGlobalPrefix() << Tag;
950 if (Number) O << Number;
951 O << Suffix;
952 }
aslc200b112008-08-16 12:57:46 +0000953
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000954 /// EmitLabel - Emit location label for internal use by Dwarf.
955 ///
956 void EmitLabel(DWLabel Label) const {
957 EmitLabel(Label.Tag, Label.Number);
958 }
959 void EmitLabel(const char *Tag, unsigned Number) const {
960 PrintLabelName(Tag, Number);
961 O << ":\n";
962 }
aslc200b112008-08-16 12:57:46 +0000963
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000964 /// EmitReference - Emit a reference to a label.
965 ///
Dan Gohman4fd77742007-09-28 15:43:33 +0000966 void EmitReference(DWLabel Label, bool IsPCRelative = false,
967 bool Force32Bit = false) const {
968 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969 }
970 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman4fd77742007-09-28 15:43:33 +0000971 bool IsPCRelative = false, bool Force32Bit = false) const {
972 PrintRelDirective(Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000973 PrintLabelName(Tag, Number);
aslc200b112008-08-16 12:57:46 +0000974
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000975 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
976 }
Dan Gohman4fd77742007-09-28 15:43:33 +0000977 void EmitReference(const std::string &Name, bool IsPCRelative = false,
978 bool Force32Bit = false) const {
979 PrintRelDirective(Force32Bit);
aslc200b112008-08-16 12:57:46 +0000980
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000981 O << Name;
aslc200b112008-08-16 12:57:46 +0000982
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
984 }
985
986 /// EmitDifference - Emit the difference between two labels. Some
987 /// assemblers do not behave with absolute expressions with data directives,
988 /// so there is an option (needsSet) to use an intermediary set expression.
989 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
990 bool IsSmall = false) {
991 EmitDifference(LabelHi.Tag, LabelHi.Number,
992 LabelLo.Tag, LabelLo.Number,
993 IsSmall);
994 }
995 void EmitDifference(const char *TagHi, unsigned NumberHi,
996 const char *TagLo, unsigned NumberLo,
997 bool IsSmall = false) {
998 if (TAI->needsSet()) {
999 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +00001000 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001001 O << ",";
1002 PrintLabelName(TagHi, NumberHi);
1003 O << "-";
1004 PrintLabelName(TagLo, NumberLo);
1005 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001006
1007 PrintRelDirective(IsSmall);
Chris Lattnerb3876c72007-09-24 03:35:37 +00001008 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 ++SetCounter;
1010 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001011 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001012
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013 PrintLabelName(TagHi, NumberHi);
1014 O << "-";
1015 PrintLabelName(TagLo, NumberLo);
1016 }
1017 }
1018
1019 void EmitSectionOffset(const char* Label, const char* Section,
1020 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesen0ebb2432008-03-26 23:31:39 +00001021 bool IsSmall = false, bool isEH = false,
1022 bool useSet = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001023 bool printAbsolute = false;
Dale Johannesen0ebb2432008-03-26 23:31:39 +00001024 if (isEH)
1025 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
1026 else
1027 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
1028
1029 if (TAI->needsSet() && useSet) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +00001031 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032 O << ",";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001033 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001034
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 if (!printAbsolute) {
1036 O << "-";
1037 PrintLabelName(Section, SectionNumber);
aslc200b112008-08-16 12:57:46 +00001038 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001040
1041 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001042
Chris Lattnerb3876c72007-09-24 03:35:37 +00001043 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 ++SetCounter;
1045 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001046 PrintRelDirective(IsSmall, true);
aslc200b112008-08-16 12:57:46 +00001047
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001048 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001050 if (!printAbsolute) {
1051 O << "-";
1052 PrintLabelName(Section, SectionNumber);
1053 }
aslc200b112008-08-16 12:57:46 +00001054 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 }
aslc200b112008-08-16 12:57:46 +00001056
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001057 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1058 /// frame.
1059 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenf5a11532007-11-13 19:13:01 +00001060 const std::vector<MachineMove> &Moves, bool isEH) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061 int stackGrowth =
1062 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1063 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00001064 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001065 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
1066
1067 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1068 const MachineMove &Move = Moves[i];
1069 unsigned LabelID = Move.getLabelID();
aslc200b112008-08-16 12:57:46 +00001070
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001071 if (LabelID) {
1072 LabelID = MMI->MappedLabel(LabelID);
aslc200b112008-08-16 12:57:46 +00001073
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 // Throw out move if the label is invalid.
1075 if (!LabelID) continue;
1076 }
aslc200b112008-08-16 12:57:46 +00001077
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078 const MachineLocation &Dst = Move.getDestination();
1079 const MachineLocation &Src = Move.getSource();
aslc200b112008-08-16 12:57:46 +00001080
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001081 // Advance row if new location.
1082 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1083 Asm->EmitInt8(DW_CFA_advance_loc4);
1084 Asm->EOL("DW_CFA_advance_loc4");
1085 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1086 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00001087
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001088 BaseLabelID = LabelID;
1089 BaseLabel = "label";
1090 IsLocal = true;
1091 }
aslc200b112008-08-16 12:57:46 +00001092
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093 // If advancing cfa.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001094 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1095 if (!Src.isReg()) {
1096 if (Src.getReg() == MachineLocation::VirtualFP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001097 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1098 Asm->EOL("DW_CFA_def_cfa_offset");
1099 } else {
1100 Asm->EmitInt8(DW_CFA_def_cfa);
1101 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001102 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001103 Asm->EOL("Register");
1104 }
aslc200b112008-08-16 12:57:46 +00001105
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001106 int Offset = -Src.getOffset();
aslc200b112008-08-16 12:57:46 +00001107
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001108 Asm->EmitULEB128Bytes(Offset);
1109 Asm->EOL("Offset");
1110 } else {
1111 assert(0 && "Machine move no supported yet.");
1112 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001113 } else if (Src.isReg() &&
1114 Src.getReg() == MachineLocation::VirtualFP) {
1115 if (Dst.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001116 Asm->EmitInt8(DW_CFA_def_cfa_register);
1117 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001118 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001119 Asm->EOL("Register");
1120 } else {
1121 assert(0 && "Machine move no supported yet.");
1122 }
1123 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001124 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125 int Offset = Dst.getOffset() / stackGrowth;
aslc200b112008-08-16 12:57:46 +00001126
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001127 if (Offset < 0) {
1128 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1129 Asm->EOL("DW_CFA_offset_extended_sf");
1130 Asm->EmitULEB128Bytes(Reg);
1131 Asm->EOL("Reg");
1132 Asm->EmitSLEB128Bytes(Offset);
1133 Asm->EOL("Offset");
1134 } else if (Reg < 64) {
1135 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng6181e062008-07-09 21:53:02 +00001136 if (VerboseAsm)
1137 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1138 else
1139 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001140 Asm->EmitULEB128Bytes(Offset);
1141 Asm->EOL("Offset");
1142 } else {
1143 Asm->EmitInt8(DW_CFA_offset_extended);
1144 Asm->EOL("DW_CFA_offset_extended");
1145 Asm->EmitULEB128Bytes(Reg);
1146 Asm->EOL("Reg");
1147 Asm->EmitULEB128Bytes(Offset);
1148 Asm->EOL("Offset");
1149 }
1150 }
1151 }
1152 }
1153
1154};
1155
1156//===----------------------------------------------------------------------===//
Devang Patel35a078f2009-01-12 22:54:42 +00001157/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001158///
1159class SrcLineInfo {
1160 unsigned Line; // Source line number.
1161 unsigned Column; // Source column.
1162 unsigned SourceID; // Source ID number.
1163 unsigned LabelID; // Label in code ID number.
1164public:
1165 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendling824a8bf2009-02-03 21:17:20 +00001166 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Devang Patel7dd15a92009-01-08 17:19:22 +00001167
1168 // Accessors
1169 unsigned getLine() const { return Line; }
1170 unsigned getColumn() const { return Column; }
1171 unsigned getSourceID() const { return SourceID; }
1172 unsigned getLabelID() const { return LabelID; }
1173};
1174
Devang Patel7dd15a92009-01-08 17:19:22 +00001175//===----------------------------------------------------------------------===//
Devang Patel5f244e32009-01-05 22:35:52 +00001176/// SrcFileInfo - This class is used to track source information.
1177///
1178class SrcFileInfo {
1179 unsigned DirectoryID; // Directory ID number.
1180 std::string Name; // File name (not including directory.)
1181public:
1182 SrcFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
1183
1184 // Accessors
1185 unsigned getDirectoryID() const { return DirectoryID; }
1186 const std::string &getName() const { return Name; }
1187
1188 /// operator== - Used by UniqueVector to locate entry.
1189 ///
Devang Patel42f6bed2009-01-13 23:54:55 +00001190 bool operator==(const SrcFileInfo &SI) const {
Devang Patel5f244e32009-01-05 22:35:52 +00001191 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
1192 }
1193
1194 /// operator< - Used by UniqueVector to locate entry.
1195 ///
1196 bool operator<(const SrcFileInfo &SI) const {
1197 return getDirectoryID() < SI.getDirectoryID() ||
1198 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
1199 }
1200};
1201
1202//===----------------------------------------------------------------------===//
Devang Patel4d1709e2009-01-08 02:33:41 +00001203/// DbgVariable - This class is used to track local variable information.
1204///
1205class DbgVariable {
Devang Patel7c8a2772009-01-16 19:28:14 +00001206 DIVariable Var; // Variable Descriptor.
Devang Patel4d1709e2009-01-08 02:33:41 +00001207 unsigned FrameIndex; // Variable frame index.
Devang Patel4d1709e2009-01-08 02:33:41 +00001208public:
Devang Patel7c8a2772009-01-16 19:28:14 +00001209 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel4d1709e2009-01-08 02:33:41 +00001210
1211 // Accessors.
Devang Patel7c8a2772009-01-16 19:28:14 +00001212 DIVariable getVariable() const { return Var; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001213 unsigned getFrameIndex() const { return FrameIndex; }
1214};
1215
1216//===----------------------------------------------------------------------===//
1217/// DbgScope - This class is used to track scope information.
1218///
1219class DbgScope {
Devang Patel4d1709e2009-01-08 02:33:41 +00001220 DbgScope *Parent; // Parent to this scope.
Devang Patel49a3bd92009-01-16 18:01:58 +00001221 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001222 // Either subprogram or block.
1223 unsigned StartLabelID; // Label ID of the beginning of scope.
1224 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel49a3bd92009-01-16 18:01:58 +00001225 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel63c22f42009-01-10 02:42:49 +00001226 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001227public:
Devang Patel2560d922009-01-15 18:25:17 +00001228 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel4d1709e2009-01-08 02:33:41 +00001229 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1230 {}
Devang Patela4162952009-01-12 18:48:36 +00001231 ~DbgScope() {
1232 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1233 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1234 }
Devang Patel4d1709e2009-01-08 02:33:41 +00001235
1236 // Accessors.
Devang Patel49a3bd92009-01-16 18:01:58 +00001237 DbgScope *getParent() const { return Parent; }
1238 DIDescriptor getDesc() const { return Desc; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001239 unsigned getStartLabelID() const { return StartLabelID; }
1240 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel63c22f42009-01-10 02:42:49 +00001241 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1242 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001243 void setStartLabelID(unsigned S) { StartLabelID = S; }
1244 void setEndLabelID(unsigned E) { EndLabelID = E; }
1245
1246 /// AddScope - Add a scope to the scope.
1247 ///
1248 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1249
1250 /// AddVariable - Add a variable to the scope.
1251 ///
1252 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1253};
1254
1255//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00001256/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001257///
1258class DwarfDebug : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 //===--------------------------------------------------------------------===//
1260 // Attributes used to construct specific Dwarf sections.
1261 //
aslc200b112008-08-16 12:57:46 +00001262
Devang Patel5302e672009-01-17 06:51:37 +00001263 /// DW_CUs - All the compile units involved in this build. The index
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264 /// of each entry in this vector corresponds to the sources in MMI.
Devang Patel7dd15a92009-01-08 17:19:22 +00001265 DenseMap<Value *, CompileUnit *> DW_CUs;
aslc200b112008-08-16 12:57:46 +00001266
Devang Patel2ae1db52009-01-30 18:20:31 +00001267 /// MainCU - Some platform prefers one compile unit per .o file. In such
1268 /// cases, all dies are inserted in MainCU.
1269 CompileUnit *MainCU;
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001270
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001271 /// AbbreviationsSet - Used to uniquely define abbreviations.
1272 ///
1273 FoldingSet<DIEAbbrev> AbbreviationsSet;
1274
1275 /// Abbreviations - A list of all the unique abbreviations in use.
1276 ///
1277 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +00001278
Devang Patelb28de842009-01-17 08:01:33 +00001279 /// Directories - Uniquing vector for directories.
Devang Patel5f244e32009-01-05 22:35:52 +00001280 UniqueVector<std::string> Directories;
1281
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001282 /// SrcFiles - Uniquing vector for source files.
Devang Patel5f244e32009-01-05 22:35:52 +00001283 UniqueVector<SrcFileInfo> SrcFiles;
1284
Devang Patel9b829452009-01-16 21:07:53 +00001285 /// Lines - List of of source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001286 std::vector<SrcLineInfo> Lines;
1287
Devang Patel9b829452009-01-16 21:07:53 +00001288 /// ValuesSet - Used to uniquely define values.
1289 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +00001291
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001292 /// Values - A list of all the unique values in use.
1293 ///
1294 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +00001295
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001296 /// StringPool - A UniqueVector of strings used by indirect references.
1297 ///
1298 UniqueVector<std::string> StringPool;
1299
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001300 /// SectionMap - Provides a unique id per text section.
1301 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001302 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +00001303
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001304 /// SectionSourceLines - Tracks line numbers per text section.
1305 ///
Devang Patel35a078f2009-01-12 22:54:42 +00001306 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001307
1308 /// didInitial - Flag to indicate if initial emission has been done.
1309 ///
1310 bool didInitial;
aslc200b112008-08-16 12:57:46 +00001311
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312 /// shouldEmit - Flag to indicate if debug information should be emitted.
1313 ///
1314 bool shouldEmit;
1315
Devang Patel2560d922009-01-15 18:25:17 +00001316 // RootDbgScope - Top level scope for the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +00001317 //
1318 DbgScope *RootDbgScope;
1319
1320 // DbgScopeMap - Tracks the scopes in the current function.
1321 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
1322
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001323 struct FunctionDebugFrameInfo {
1324 unsigned Number;
1325 std::vector<MachineMove> Moves;
1326
1327 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001328 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001329 };
1330
1331 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001332
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001333public:
aslc200b112008-08-16 12:57:46 +00001334
Bill Wendling50db0792009-02-20 00:44:43 +00001335 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
1336 /// be emitted.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001337 ///
Bill Wendling50db0792009-02-20 00:44:43 +00001338 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001339
1340 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001341 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001342 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1343 // Profile the node so that we can make it unique.
1344 FoldingSetNodeID ID;
1345 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001346
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001347 // Check the set for priors.
1348 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001349
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001350 // If it's newly added.
1351 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001352 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353 Abbreviations.push_back(&Abbrev);
1354 // Assign the vector position + 1 as its number.
1355 Abbrev.setNumber(Abbreviations.size());
1356 } else {
1357 // Assign existing abbreviation number.
1358 Abbrev.setNumber(InSet->getNumber());
1359 }
1360 }
1361
1362 /// NewString - Add a string to the constant pool and returns a label.
1363 ///
1364 DWLabel NewString(const std::string &String) {
1365 unsigned StringID = StringPool.insert(String);
1366 return DWLabel("string", StringID);
1367 }
aslc200b112008-08-16 12:57:46 +00001368
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001369 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1370 /// entry.
1371 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1372 DIEntry *Value;
aslc200b112008-08-16 12:57:46 +00001373
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001374 if (Entry) {
1375 FoldingSetNodeID ID;
1376 DIEntry::Profile(ID, Entry);
1377 void *Where;
1378 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001379
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001380 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001381
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001382 Value = new DIEntry(Entry);
1383 ValuesSet.InsertNode(Value, Where);
1384 } else {
1385 Value = new DIEntry(Entry);
1386 }
aslc200b112008-08-16 12:57:46 +00001387
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388 Values.push_back(Value);
1389 return Value;
1390 }
aslc200b112008-08-16 12:57:46 +00001391
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001392 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1393 ///
1394 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1395 Value->Entry = Entry;
1396 // Add to values set if not already there. If it is, we merely have a
1397 // duplicate in the values list (no harm.)
1398 ValuesSet.GetOrInsertNode(Value);
1399 }
1400
1401 /// AddUInt - Add an unsigned integer attribute data and value.
1402 ///
1403 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1404 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1405
1406 FoldingSetNodeID ID;
1407 DIEInteger::Profile(ID, Integer);
1408 void *Where;
1409 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1410 if (!Value) {
1411 Value = new DIEInteger(Integer);
1412 ValuesSet.InsertNode(Value, Where);
1413 Values.push_back(Value);
1414 }
aslc200b112008-08-16 12:57:46 +00001415
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001416 Die->AddValue(Attribute, Form, Value);
1417 }
aslc200b112008-08-16 12:57:46 +00001418
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001419 /// AddSInt - Add an signed integer attribute data and value.
1420 ///
1421 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1422 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1423
1424 FoldingSetNodeID ID;
1425 DIEInteger::Profile(ID, (uint64_t)Integer);
1426 void *Where;
1427 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1428 if (!Value) {
1429 Value = new DIEInteger(Integer);
1430 ValuesSet.InsertNode(Value, Where);
1431 Values.push_back(Value);
1432 }
aslc200b112008-08-16 12:57:46 +00001433
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001434 Die->AddValue(Attribute, Form, Value);
1435 }
aslc200b112008-08-16 12:57:46 +00001436
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001437 /// AddString - Add a std::string attribute data and value.
1438 ///
1439 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1440 const std::string &String) {
1441 FoldingSetNodeID ID;
1442 DIEString::Profile(ID, String);
1443 void *Where;
1444 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1445 if (!Value) {
1446 Value = new DIEString(String);
1447 ValuesSet.InsertNode(Value, Where);
1448 Values.push_back(Value);
1449 }
aslc200b112008-08-16 12:57:46 +00001450
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001451 Die->AddValue(Attribute, Form, Value);
1452 }
aslc200b112008-08-16 12:57:46 +00001453
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001454 /// AddLabel - Add a Dwarf label attribute data and value.
1455 ///
1456 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1457 const DWLabel &Label) {
1458 FoldingSetNodeID ID;
1459 DIEDwarfLabel::Profile(ID, Label);
1460 void *Where;
1461 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1462 if (!Value) {
1463 Value = new DIEDwarfLabel(Label);
1464 ValuesSet.InsertNode(Value, Where);
1465 Values.push_back(Value);
1466 }
aslc200b112008-08-16 12:57:46 +00001467
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001468 Die->AddValue(Attribute, Form, Value);
1469 }
aslc200b112008-08-16 12:57:46 +00001470
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001471 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1472 ///
1473 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1474 const std::string &Label) {
1475 FoldingSetNodeID ID;
1476 DIEObjectLabel::Profile(ID, Label);
1477 void *Where;
1478 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1479 if (!Value) {
1480 Value = new DIEObjectLabel(Label);
1481 ValuesSet.InsertNode(Value, Where);
1482 Values.push_back(Value);
1483 }
aslc200b112008-08-16 12:57:46 +00001484
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001485 Die->AddValue(Attribute, Form, Value);
1486 }
aslc200b112008-08-16 12:57:46 +00001487
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001488 /// AddSectionOffset - Add a section offset label attribute data and value.
1489 ///
1490 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1491 const DWLabel &Label, const DWLabel &Section,
1492 bool isEH = false, bool useSet = true) {
1493 FoldingSetNodeID ID;
1494 DIESectionOffset::Profile(ID, Label, Section);
1495 void *Where;
1496 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1497 if (!Value) {
1498 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1499 ValuesSet.InsertNode(Value, Where);
1500 Values.push_back(Value);
1501 }
aslc200b112008-08-16 12:57:46 +00001502
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001503 Die->AddValue(Attribute, Form, Value);
1504 }
aslc200b112008-08-16 12:57:46 +00001505
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001506 /// AddDelta - Add a label delta attribute data and value.
1507 ///
1508 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1509 const DWLabel &Hi, const DWLabel &Lo) {
1510 FoldingSetNodeID ID;
1511 DIEDelta::Profile(ID, Hi, Lo);
1512 void *Where;
1513 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1514 if (!Value) {
1515 Value = new DIEDelta(Hi, Lo);
1516 ValuesSet.InsertNode(Value, Where);
1517 Values.push_back(Value);
1518 }
aslc200b112008-08-16 12:57:46 +00001519
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001520 Die->AddValue(Attribute, Form, Value);
1521 }
aslc200b112008-08-16 12:57:46 +00001522
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001523 /// AddDIEntry - Add a DIE attribute data and value.
1524 ///
1525 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1526 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1527 }
1528
1529 /// AddBlock - Add block data.
1530 ///
1531 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1532 Block->ComputeSize(*this);
1533 FoldingSetNodeID ID;
1534 Block->Profile(ID);
1535 void *Where;
1536 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1537 if (!Value) {
1538 Value = Block;
1539 ValuesSet.InsertNode(Value, Where);
1540 Values.push_back(Value);
1541 } else {
Chris Lattner3de66892007-09-21 18:25:53 +00001542 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001543 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001544 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545 }
aslc200b112008-08-16 12:57:46 +00001546
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001547 Die->AddValue(Attribute, Block->BestForm(), Value);
1548 }
1549
1550private:
1551
1552 /// AddSourceLine - Add location information to specified debug information
1553 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001554 void AddSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001555 unsigned FileID = 0;
1556 unsigned Line = V->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001557 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1558 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001559 assert (FileID && "Invalid file id");
Devang Patel4d1709e2009-01-08 02:33:41 +00001560 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1561 AddUInt(Die, DW_AT_decl_line, 0, Line);
1562 }
1563
1564 /// AddSourceLine - Add location information to specified debug information
1565 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001566 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Devang Patel5f244e32009-01-05 22:35:52 +00001567 unsigned FileID = 0;
1568 unsigned Line = G->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001569 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1570 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001571 assert (FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001572 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1573 AddUInt(Die, DW_AT_decl_line, 0, Line);
1574 }
1575
Devang Patel7c8a2772009-01-16 19:28:14 +00001576 void AddSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel5f244e32009-01-05 22:35:52 +00001577 unsigned FileID = 0;
Devang Patel7c8a2772009-01-16 19:28:14 +00001578 unsigned Line = Ty->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001579 DICompileUnit CU = Ty->getCompileUnit();
1580 if (CU.isNull())
1581 return;
1582 CompileUnit *Unit = FindCompileUnit(CU);
1583 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001584 assert (FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001585 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1586 AddUInt(Die, DW_AT_decl_line, 0, Line);
1587 }
1588
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001589 /// AddAddress - Add an address attribute to a die based on the location
1590 /// provided.
1591 void AddAddress(DIE *Die, unsigned Attribute,
1592 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001593 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001594 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001595
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001596 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001597 if (Reg < 32) {
1598 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1599 } else {
1600 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1601 AddUInt(Block, 0, DW_FORM_udata, Reg);
1602 }
1603 } else {
1604 if (Reg < 32) {
1605 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1606 } else {
1607 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1608 AddUInt(Block, 0, DW_FORM_udata, Reg);
1609 }
1610 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1611 }
aslc200b112008-08-16 12:57:46 +00001612
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001613 AddBlock(Die, Attribute, 0, Block);
1614 }
aslc200b112008-08-16 12:57:46 +00001615
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001616 /// AddType - Add a new type attribute to the specified entity.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001617 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel165ed512009-01-23 19:13:31 +00001618 if (Ty.isNull())
Devang Patel4a4cbe72009-01-05 21:47:57 +00001619 return;
Devang Patel4a4cbe72009-01-05 21:47:57 +00001620
1621 // Check for pre-existence.
1622 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1623 // If it exists then use the existing value.
1624 if (Slot) {
1625 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1626 return;
1627 }
1628
1629 // Set up proxy.
1630 Slot = NewDIEntry();
1631
1632 // Construct type.
1633 DIE Buffer(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001634 if (Ty.isBasicType(Ty.getTag()))
1635 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1636 else if (Ty.isDerivedType(Ty.getTag()))
1637 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1638 else {
Bill Wendling824a8bf2009-02-03 21:17:20 +00001639 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelef4bf3b2009-01-15 19:26:23 +00001640 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1641 }
1642
Devang Patelb0cb07c2009-01-27 23:22:55 +00001643 // Add debug information entry to entity and appropriate context.
1644 DIE *Die = NULL;
1645 DIDescriptor Context = Ty.getContext();
1646 if (!Context.isNull())
1647 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1648
1649 if (Die) {
1650 DIE *Child = new DIE(Buffer);
1651 Die->AddChild(Child);
1652 Buffer.Detach();
1653 SetDIEntry(Slot, Child);
Bill Wendling824a8bf2009-02-03 21:17:20 +00001654 } else {
Devang Patelb0cb07c2009-01-27 23:22:55 +00001655 Die = DW_Unit->AddDie(Buffer);
1656 SetDIEntry(Slot, Die);
1657 }
1658
Devang Patel4a4cbe72009-01-05 21:47:57 +00001659 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1660 }
1661
Devang Patel46d13752009-01-05 19:07:53 +00001662 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1663 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001664 DIBasicType BTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001665
1666 // Get core information.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001667 const std::string &Name = BTy.getName();
Devang Patelfc187162009-01-05 17:57:47 +00001668 Buffer.setTag(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001669 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patelfc187162009-01-05 17:57:47 +00001670 // Add name if not anonymous or intermediate type.
1671 if (!Name.empty())
1672 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001673 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelfc187162009-01-05 17:57:47 +00001674 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1675 }
1676
Devang Patel46d13752009-01-05 19:07:53 +00001677 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1678 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001679 DIDerivedType DTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001680
1681 // Get core information.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001682 const std::string &Name = DTy.getName();
1683 uint64_t Size = DTy.getSizeInBits() >> 3;
1684 unsigned Tag = DTy.getTag();
Devang Patelfc187162009-01-05 17:57:47 +00001685 // FIXME - Workaround for templates.
1686 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1687
1688 Buffer.setTag(Tag);
1689 // Map to main type, void will not have a type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001690 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001691 AddType(DW_Unit, &Buffer, FromTy);
Devang Patelfc187162009-01-05 17:57:47 +00001692
1693 // Add name if not anonymous or intermediate type.
1694 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1695
1696 // Add size if non-zero (derived types might be zero-sized.)
1697 if (Size)
1698 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1699
1700 // Add source line info if available and TyDesc is not a forward
1701 // declaration.
Devang Patele34e0882009-01-27 00:45:04 +00001702 if (!DTy.isForwardDecl())
1703 AddSourceLine(&Buffer, &DTy);
Devang Patelfc187162009-01-05 17:57:47 +00001704 }
1705
Devang Patel30c01372009-01-05 19:55:51 +00001706 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1707 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001708 DICompositeType CTy) {
Devang Patel30c01372009-01-05 19:55:51 +00001709
Devang Patelb28de842009-01-17 08:01:33 +00001710 // Get core information.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001711 const std::string &Name = CTy.getName();
1712 uint64_t Size = CTy.getSizeInBits() >> 3;
1713 unsigned Tag = CTy.getTag();
Devang Patel8050bd72009-01-23 01:19:09 +00001714 Buffer.setTag(Tag);
Devang Patel30c01372009-01-05 19:55:51 +00001715 switch (Tag) {
1716 case DW_TAG_vector_type:
1717 case DW_TAG_array_type:
Devang Patelef4bf3b2009-01-15 19:26:23 +00001718 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001719 break;
Devang Patel3798f492009-01-20 18:35:14 +00001720 case DW_TAG_enumeration_type:
1721 {
1722 DIArray Elements = CTy.getTypeArray();
1723 // Add enumerators to enumeration type.
1724 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1725 DIE *ElemDie = NULL;
1726 DIEnumerator Enum(Elements.getElement(i).getGV());
1727 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1728 Buffer.AddChild(ElemDie);
1729 }
1730 }
1731 break;
Devang Patel30c01372009-01-05 19:55:51 +00001732 case DW_TAG_subroutine_type:
1733 {
1734 // Add prototype flag.
1735 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001736 DIArray Elements = CTy.getTypeArray();
Devang Patel30c01372009-01-05 19:55:51 +00001737 // Add return type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001738 DIDescriptor RTy = Elements.getElement(0);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001739 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel4a4cbe72009-01-05 21:47:57 +00001740
Devang Patel30c01372009-01-05 19:55:51 +00001741 // Add arguments.
1742 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1743 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001744 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld40a7e52009-01-17 06:57:25 +00001745 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patel30c01372009-01-05 19:55:51 +00001746 Buffer.AddChild(Arg);
1747 }
1748 }
1749 break;
1750 case DW_TAG_structure_type:
1751 case DW_TAG_union_type:
1752 {
1753 // Add elements to structure type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001754 DIArray Elements = CTy.getTypeArray();
Devang Patelcf7acb12009-01-16 00:50:53 +00001755
1756 // A forward struct declared type may not have elements available.
1757 if (Elements.isNull())
1758 break;
1759
Devang Patel30c01372009-01-05 19:55:51 +00001760 // Add elements to structure type.
1761 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1762 DIDescriptor Element = Elements.getElement(i);
Devang Patelb28de842009-01-17 08:01:33 +00001763 DIE *ElemDie = NULL;
Devang Patelef4bf3b2009-01-15 19:26:23 +00001764 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel245446c2009-01-17 08:05:14 +00001765 ElemDie = CreateSubprogramDIE(DW_Unit,
1766 DISubprogram(Element.getGV()));
Devang Patelb28de842009-01-17 08:01:33 +00001767 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1768 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1769 DIGlobalVariable(Element.getGV()));
Devang Patel5c643892009-01-20 21:02:02 +00001770 else
1771 ElemDie = CreateMemberDIE(DW_Unit,
1772 DIDerivedType(Element.getGV()));
Devang Patel245446c2009-01-17 08:05:14 +00001773 Buffer.AddChild(ElemDie);
Devang Patel30c01372009-01-05 19:55:51 +00001774 }
Devang Patel74193d72009-02-17 22:43:44 +00001775 unsigned RLang = CTy.getRunTimeLang();
1776 if (RLang)
1777 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patel30c01372009-01-05 19:55:51 +00001778 }
1779 break;
1780 default:
1781 break;
1782 }
1783
1784 // Add name if not anonymous or intermediate type.
1785 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1786
Devang Patele34e0882009-01-27 00:45:04 +00001787 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1788 || Tag == DW_TAG_union_type) {
1789 // Add size if non-zero (derived types might be zero-sized.)
1790 if (Size)
1791 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1792 else {
1793 // Add zero size if it is not a forward declaration.
1794 if (CTy.isForwardDecl())
1795 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1796 else
1797 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1798 }
1799
1800 // Add source line info if available.
1801 if (!CTy.isForwardDecl())
1802 AddSourceLine(&Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001803 }
Devang Patel30c01372009-01-05 19:55:51 +00001804 }
1805
Bill Wendling824a8bf2009-02-03 21:17:20 +00001806 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1807 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelef4bf3b2009-01-15 19:26:23 +00001808 int64_t L = SR.getLo();
1809 int64_t H = SR.getHi();
Devang Patel6fb54132009-01-05 18:33:01 +00001810 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1811 if (L != H) {
1812 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1813 if (L)
Devang Patel245446c2009-01-17 08:05:14 +00001814 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1815 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel6fb54132009-01-05 18:33:01 +00001816 }
1817 Buffer.AddChild(DW_Subrange);
1818 }
1819
1820 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1821 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1822 DICompositeType *CTy) {
1823 Buffer.setTag(DW_TAG_array_type);
1824 if (CTy->getTag() == DW_TAG_vector_type)
1825 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1826
Devang Patel6ab30e52009-01-28 21:08:20 +00001827 // Emit derived type.
1828 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel6fb54132009-01-05 18:33:01 +00001829 DIArray Elements = CTy->getTypeArray();
Devang Patel6fb54132009-01-05 18:33:01 +00001830
1831 // Construct an anonymous type for index type.
1832 DIE IdxBuffer(DW_TAG_base_type);
1833 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1834 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1835 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1836
1837 // Add subranges to array type.
1838 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel30c01372009-01-05 19:55:51 +00001839 DIDescriptor Element = Elements.getElement(i);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001840 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1841 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +00001842 }
1843 }
1844
Bill Wendling824a8bf2009-02-03 21:17:20 +00001845 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3798f492009-01-20 18:35:14 +00001846 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +00001847
1848 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1849 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1850 int64_t Value = ETy->getEnumValue();
1851 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Patel3798f492009-01-20 18:35:14 +00001852 return Enumerator;
Devang Patela566e812009-01-05 18:38:38 +00001853 }
Devang Patel6fb54132009-01-05 18:33:01 +00001854
Devang Patelb28de842009-01-17 08:01:33 +00001855 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendling824a8bf2009-02-03 21:17:20 +00001856 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patelb28de842009-01-17 08:01:33 +00001857 {
1858 DIE *GVDie = new DIE(DW_TAG_variable);
1859 AddString(GVDie, DW_AT_name, DW_FORM_string, GV.getName());
1860 const std::string &LinkageName = GV.getLinkageName();
Devang Patel526b01d2009-01-05 18:59:44 +00001861 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001862 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1863 AddType(DW_Unit, GVDie, GV.getType());
1864 if (!GV.isLocalToUnit())
1865 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1866 AddSourceLine(GVDie, &GV);
1867 return GVDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001868 }
1869
Devang Patel5c643892009-01-20 21:02:02 +00001870 /// CreateMemberDIE - Create new member DIE.
1871 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1872 DIE *MemberDie = new DIE(DT.getTag());
1873 std::string Name = DT.getName();
1874 if (!Name.empty())
1875 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1876
1877 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1878
1879 AddSourceLine(MemberDie, &DT);
1880
Devang Patelf1f30d42009-02-17 21:23:59 +00001881 uint64_t Size = DT.getSizeInBits();
1882 uint64_t FieldSize = DT.getOriginalTypeSize();
1883
1884 if (Size != FieldSize) {
1885 // Handle bitfield.
1886 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1887 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1888
1889 uint64_t Offset = DT.getOffsetInBits();
1890 uint64_t FieldOffset = Offset;
1891 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1892 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1893 FieldOffset = (HiMark - FieldSize);
1894 Offset -= FieldOffset;
1895 // Maybe we need to work from the other end.
1896 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1897 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1898 }
Devang Patel5c643892009-01-20 21:02:02 +00001899 DIEBlock *Block = new DIEBlock();
1900 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1901 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1902 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1903
Devang Patel2e7ee192009-01-21 00:08:04 +00001904 if (DT.isProtected())
1905 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1906 else if (DT.isPrivate())
1907 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1908
Devang Patel5c643892009-01-20 21:02:02 +00001909 return MemberDie;
1910 }
1911
Devang Patelb28de842009-01-17 08:01:33 +00001912 /// CreateSubprogramDIE - Create new DIE using SP.
1913 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel245446c2009-01-17 08:05:14 +00001914 const DISubprogram &SP,
1915 bool IsConstructor = false) {
Devang Patelb28de842009-01-17 08:01:33 +00001916 DIE *SPDie = new DIE(DW_TAG_subprogram);
1917 AddString(SPDie, DW_AT_name, DW_FORM_string, SP.getName());
Devang Patelef4bf3b2009-01-15 19:26:23 +00001918 const std::string &LinkageName = SP.getLinkageName();
Devang Patel526b01d2009-01-05 18:59:44 +00001919 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001920 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel245446c2009-01-17 08:05:14 +00001921 LinkageName);
Devang Patelb28de842009-01-17 08:01:33 +00001922 AddSourceLine(SPDie, &SP);
Devang Patel526b01d2009-01-05 18:59:44 +00001923
Devang Patelb28de842009-01-17 08:01:33 +00001924 DICompositeType SPTy = SP.getType();
1925 DIArray Args = SPTy.getTypeArray();
1926
Devang Patel526b01d2009-01-05 18:59:44 +00001927 // Add Return Type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001928 if (!IsConstructor)
Devang Patelb28de842009-01-17 08:01:33 +00001929 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
Devang Patel922d1592009-01-30 01:21:46 +00001930
Devang Patelace2cf62009-02-02 17:51:41 +00001931 if (!SP.isDefinition()) {
1932 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
1933 // Add arguments.
1934 // Do not add arguments for subprogram definition. They will be
1935 // handled through RecordVariable.
1936 if (!Args.isNull())
1937 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1938 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1939 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1940 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1941 SPDie->AddChild(Arg);
1942 }
1943 }
Devang Patel922d1592009-01-30 01:21:46 +00001944
Devang Patelef4bf3b2009-01-15 19:26:23 +00001945 if (!SP.isLocalToUnit())
Devang Patel922d1592009-01-30 01:21:46 +00001946 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Devang Patelb28de842009-01-17 08:01:33 +00001947 return SPDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001948 }
1949
Devang Patelb28de842009-01-17 08:01:33 +00001950 /// FindCompileUnit - Get the compile unit for the given descriptor.
1951 ///
Devang Patel5f244e32009-01-05 22:35:52 +00001952 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
1953 CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
1954 assert(DW_Unit && "Missing compile unit.");
1955 return DW_Unit;
1956 }
1957
Devang Patel42f6bed2009-01-13 23:54:55 +00001958 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel4d1709e2009-01-08 02:33:41 +00001959 ///
1960 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1961 // Get the descriptor.
Devang Patel7c8a2772009-01-16 19:28:14 +00001962 const DIVariable &VD = DV->getVariable();
Devang Patel4d1709e2009-01-08 02:33:41 +00001963
1964 // Translate tag to proper Dwarf tag. The result variable is dropped for
1965 // now.
1966 unsigned Tag;
Devang Patel7c8a2772009-01-16 19:28:14 +00001967 switch (VD.getTag()) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001968 case DW_TAG_return_variable: return NULL;
1969 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1970 case DW_TAG_auto_variable: // fall thru
1971 default: Tag = DW_TAG_variable; break;
1972 }
1973
1974 // Define variable debug information entry.
1975 DIE *VariableDie = new DIE(Tag);
Devang Patel7c8a2772009-01-16 19:28:14 +00001976 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD.getName());
Devang Patel4d1709e2009-01-08 02:33:41 +00001977
1978 // Add source line info if available.
Devang Patel7c8a2772009-01-16 19:28:14 +00001979 AddSourceLine(VariableDie, &VD);
Devang Patel4d1709e2009-01-08 02:33:41 +00001980
1981 // Add variable type.
Devang Patel7c8a2772009-01-16 19:28:14 +00001982 AddType(Unit, VariableDie, VD.getType());
Devang Patel4d1709e2009-01-08 02:33:41 +00001983
1984 // Add variable address.
1985 MachineLocation Location;
1986 Location.set(RI->getFrameRegister(*MF),
1987 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1988 AddAddress(VariableDie, DW_AT_location, Location);
1989
1990 return VariableDie;
1991 }
1992
Devang Patel4d1709e2009-01-08 02:33:41 +00001993 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1994 ///
1995 DbgScope *getOrCreateScope(GlobalVariable *V) {
1996 DbgScope *&Slot = DbgScopeMap[V];
Bill Wendlinge0f3a262009-02-20 20:40:28 +00001997 if (Slot) return Slot;
1998
1999 // FIXME - breaks down when the context is an inlined function.
2000 DIDescriptor ParentDesc;
2001 DIDescriptor Desc(V);
2002
2003 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
2004 DIBlock Block(V);
2005 ParentDesc = Block.getContext();
Devang Patel4d1709e2009-01-08 02:33:41 +00002006 }
Bill Wendlinge0f3a262009-02-20 20:40:28 +00002007
2008 DbgScope *Parent = ParentDesc.isNull() ?
2009 NULL : getOrCreateScope(ParentDesc.getGV());
2010 Slot = new DbgScope(Parent, Desc);
2011
2012 if (Parent) {
2013 Parent->AddScope(Slot);
2014 } else if (RootDbgScope) {
2015 // FIXME - Add inlined function scopes to the root so we can delete them
2016 // later. Long term, handle inlined functions properly.
2017 RootDbgScope->AddScope(Slot);
2018 } else {
2019 // First function is top level function.
2020 RootDbgScope = Slot;
2021 }
2022
Devang Patel4d1709e2009-01-08 02:33:41 +00002023 return Slot;
2024 }
2025
2026 /// ConstructDbgScope - Construct the components of a scope.
2027 ///
2028 void ConstructDbgScope(DbgScope *ParentScope,
2029 unsigned ParentStartID, unsigned ParentEndID,
2030 DIE *ParentDie, CompileUnit *Unit) {
2031 // Add variables to scope.
Devang Patel63c22f42009-01-10 02:42:49 +00002032 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel4d1709e2009-01-08 02:33:41 +00002033 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2034 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2035 if (VariableDie) ParentDie->AddChild(VariableDie);
2036 }
2037
2038 // Add nested scopes.
Devang Patel63c22f42009-01-10 02:42:49 +00002039 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel4d1709e2009-01-08 02:33:41 +00002040 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2041 // Define the Scope debug information entry.
2042 DbgScope *Scope = Scopes[j];
2043 // FIXME - Ignore inlined functions for the time being.
2044 if (!Scope->getParent()) continue;
2045
Devang Patelb9224922009-01-12 18:41:00 +00002046 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2047 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel4d1709e2009-01-08 02:33:41 +00002048
2049 // Ignore empty scopes.
2050 if (StartID == EndID && StartID != 0) continue;
2051 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2052
2053 if (StartID == ParentStartID && EndID == ParentEndID) {
2054 // Just add stuff to the parent scope.
2055 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2056 } else {
2057 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2058
2059 // Add the scope bounds.
2060 if (StartID) {
2061 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2062 DWLabel("label", StartID));
2063 } else {
2064 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2065 DWLabel("func_begin", SubprogramCount));
2066 }
2067 if (EndID) {
2068 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2069 DWLabel("label", EndID));
2070 } else {
2071 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2072 DWLabel("func_end", SubprogramCount));
2073 }
2074
2075 // Add the scope contents.
2076 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2077 ParentDie->AddChild(ScopeDie);
2078 }
2079 }
2080 }
2081
2082 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2083 ///
2084 void ConstructRootDbgScope(DbgScope *RootScope) {
2085 // Exit if there is no root scope.
2086 if (!RootScope) return;
Devang Patel2560d922009-01-15 18:25:17 +00002087 DIDescriptor Desc = RootScope->getDesc();
2088 if (Desc.isNull())
2089 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002090
2091 // Get the subprogram debug information entry.
Devang Patel2560d922009-01-15 18:25:17 +00002092 DISubprogram SPD(Desc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002093
2094 // Get the compile unit context.
Devang Patel2ae1db52009-01-30 18:20:31 +00002095 CompileUnit *Unit = MainCU;
2096 if (!Unit)
2097 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00002098
2099 // Get the subprogram die.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002100 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002101 assert(SPDie && "Missing subprogram descriptor");
2102
2103 // Add the function bounds.
2104 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2105 DWLabel("func_begin", SubprogramCount));
2106 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2107 DWLabel("func_end", SubprogramCount));
2108 MachineLocation Location(RI->getFrameRegister(*MF));
2109 AddAddress(SPDie, DW_AT_frame_base, Location);
2110
2111 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2112 }
2113
2114 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2115 ///
2116 void ConstructDefaultDbgScope(MachineFunction *MF) {
2117 // Find the correct subprogram descriptor.
2118 std::string SPName = "llvm.dbg.subprograms";
2119 std::vector<GlobalVariable*> Result;
2120 getGlobalVariablesUsing(*M, SPName, Result);
Bill Wendling824a8bf2009-02-03 21:17:20 +00002121
Devang Patel4d1709e2009-01-08 02:33:41 +00002122 for (std::vector<GlobalVariable *>::iterator I = Result.begin(),
2123 E = Result.end(); I != E; ++I) {
Devang Patel7c8a2772009-01-16 19:28:14 +00002124 DISubprogram SPD(*I);
Devang Patel4d1709e2009-01-08 02:33:41 +00002125
Devang Patel7c8a2772009-01-16 19:28:14 +00002126 if (SPD.getName() == MF->getFunction()->getName()) {
Devang Patel4d1709e2009-01-08 02:33:41 +00002127 // Get the compile unit context.
Devang Patel2ae1db52009-01-30 18:20:31 +00002128 CompileUnit *Unit = MainCU;
2129 if (!Unit)
2130 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00002131
2132 // Get the subprogram die.
Devang Patel7c8a2772009-01-16 19:28:14 +00002133 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7f3f1982009-02-18 17:29:38 +00002134 if (!SPDie)
2135 /* A subprogram die may not exist if the corresponding function
2136 does not have any debug info. */
2137 continue;
Devang Patel4d1709e2009-01-08 02:33:41 +00002138
2139 // Add the function bounds.
2140 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2141 DWLabel("func_begin", SubprogramCount));
2142 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2143 DWLabel("func_end", SubprogramCount));
2144
2145 MachineLocation Location(RI->getFrameRegister(*MF));
2146 AddAddress(SPDie, DW_AT_frame_base, Location);
2147 return;
2148 }
2149 }
2150#if 0
2151 // FIXME: This is causing an abort because C++ mangled names are compared
2152 // with their unmangled counterparts. See PR2885. Don't do this assert.
2153 assert(0 && "Couldn't find DIE for machine function!");
2154#endif
2155 }
2156
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002157 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2158 /// tools to recognize the object file contains Dwarf information.
2159 void EmitInitial() {
2160 // Check to see if we already emitted intial headers.
2161 if (didInitial) return;
2162 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002163
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002164 // Dwarf sections base addresses.
2165 if (TAI->doesDwarfRequireFrameSection()) {
2166 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2167 EmitLabel("section_debug_frame", 0);
2168 }
2169 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2170 EmitLabel("section_info", 0);
2171 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2172 EmitLabel("section_abbrev", 0);
2173 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2174 EmitLabel("section_aranges", 0);
Scott Michel79f01f52009-01-26 22:32:51 +00002175 if (TAI->doesSupportMacInfoSection()) {
2176 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2177 EmitLabel("section_macinfo", 0);
2178 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002179 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2180 EmitLabel("section_line", 0);
2181 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2182 EmitLabel("section_loc", 0);
2183 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2184 EmitLabel("section_pubnames", 0);
2185 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2186 EmitLabel("section_str", 0);
2187 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2188 EmitLabel("section_ranges", 0);
2189
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002190 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002191 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002192 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002193 EmitLabel("data_begin", 0);
2194 }
2195
2196 /// EmitDIE - Recusively Emits a debug information entry.
2197 ///
2198 void EmitDIE(DIE *Die) {
2199 // Get the abbreviation for this DIE.
2200 unsigned AbbrevNumber = Die->getAbbrevNumber();
2201 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002202
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002203 Asm->EOL();
2204
2205 // Emit the code (index) for the abbreviation.
2206 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002207
2208 if (VerboseAsm)
2209 Asm->EOL(std::string("Abbrev [" +
2210 utostr(AbbrevNumber) +
2211 "] 0x" + utohexstr(Die->getOffset()) +
2212 ":0x" + utohexstr(Die->getSize()) + " " +
2213 TagString(Abbrev->getTag())));
2214 else
2215 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002216
Owen Anderson88dd6232008-06-24 21:44:59 +00002217 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2218 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002219
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002220 // Emit the DIE attribute values.
2221 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2222 unsigned Attr = AbbrevData[i].getAttribute();
2223 unsigned Form = AbbrevData[i].getForm();
2224 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002225
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002226 switch (Attr) {
2227 case DW_AT_sibling: {
2228 Asm->EmitInt32(Die->SiblingOffset());
2229 break;
2230 }
2231 default: {
2232 // Emit an attribute using the defined form.
2233 Values[i]->EmitValue(*this, Form);
2234 break;
2235 }
2236 }
aslc200b112008-08-16 12:57:46 +00002237
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002238 Asm->EOL(AttributeString(Attr));
2239 }
aslc200b112008-08-16 12:57:46 +00002240
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002241 // Emit the DIE children if any.
2242 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2243 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002244
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002245 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2246 EmitDIE(Children[j]);
2247 }
aslc200b112008-08-16 12:57:46 +00002248
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002249 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2250 }
2251 }
2252
2253 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2254 ///
2255 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2256 // Get the children.
2257 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002258
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002259 // If not last sibling and has children then add sibling offset attribute.
2260 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2261
2262 // Record the abbreviation.
2263 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002264
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002265 // Get the abbreviation for this DIE.
2266 unsigned AbbrevNumber = Die->getAbbrevNumber();
2267 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2268
2269 // Set DIE offset
2270 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002271
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002272 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002273 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2274
Owen Anderson88dd6232008-06-24 21:44:59 +00002275 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2276 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002277
2278 // Size the DIE attribute values.
2279 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2280 // Size attribute value.
2281 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2282 }
aslc200b112008-08-16 12:57:46 +00002283
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002284 // Size the DIE children if any.
2285 if (!Children.empty()) {
2286 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2287 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002288
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002289 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2290 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2291 }
aslc200b112008-08-16 12:57:46 +00002292
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002293 // End of children marker.
2294 Offset += sizeof(int8_t);
2295 }
2296
2297 Die->setSize(Offset - Die->getOffset());
2298 return Offset;
2299 }
2300
2301 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2302 ///
2303 void SizeAndOffsets() {
2304 // Process base compile unit.
Devang Patel2ae1db52009-01-30 18:20:31 +00002305 if (MainCU) {
2306 // Compute size of compile unit header
2307 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2308 sizeof(int16_t) + // DWARF version number
2309 sizeof(int32_t) + // Offset Into Abbrev. Section
2310 sizeof(int8_t); // Pointer Size (in bytes)
2311 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2312 return;
2313 }
Devang Patel6eae2832009-01-12 23:05:55 +00002314 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2315 CE = DW_CUs.end(); CI != CE; ++CI) {
2316 CompileUnit *Unit = CI->second;
2317 // Compute size of compile unit header
2318 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2319 sizeof(int16_t) + // DWARF version number
2320 sizeof(int32_t) + // Offset Into Abbrev. Section
2321 sizeof(int8_t); // Pointer Size (in bytes)
2322 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2323 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002324 }
2325
2326 /// EmitDebugInfo - Emit the debug info section.
2327 ///
2328 void EmitDebugInfo() {
2329 // Start debug info section.
2330 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002331
Devang Patel6eae2832009-01-12 23:05:55 +00002332 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2333 CE = DW_CUs.end(); CI != CE; ++CI) {
2334 CompileUnit *Unit = CI->second;
Devang Patel2ae1db52009-01-30 18:20:31 +00002335 if (MainCU)
2336 Unit = MainCU;
Devang Patel6eae2832009-01-12 23:05:55 +00002337 DIE *Die = Unit->getDie();
2338 // Emit the compile units header.
2339 EmitLabel("info_begin", Unit->getID());
2340 // Emit size of content not including length itself
2341 unsigned ContentSize = Die->getSize() +
2342 sizeof(int16_t) + // DWARF version number
2343 sizeof(int32_t) + // Offset Into Abbrev. Section
2344 sizeof(int8_t) + // Pointer Size (in bytes)
2345 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2346
2347 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2348 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2349 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2350 Asm->EOL("Offset Into Abbrev. Section");
2351 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2352
2353 EmitDIE(Die);
2354 // FIXME - extra padding for gdb bug.
2355 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2356 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2357 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2358 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2359 EmitLabel("info_end", Unit->getID());
2360
2361 Asm->EOL();
Devang Patel2ae1db52009-01-30 18:20:31 +00002362 if (MainCU)
2363 return;
Devang Patel6eae2832009-01-12 23:05:55 +00002364 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002365 }
2366
2367 /// EmitAbbreviations - Emit the abbreviation section.
2368 ///
2369 void EmitAbbreviations() const {
2370 // Check to see if it is worth the effort.
2371 if (!Abbreviations.empty()) {
2372 // Start the debug abbrev section.
2373 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00002374
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002375 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00002376
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002377 // For each abbrevation.
2378 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2379 // Get abbreviation data
2380 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00002381
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002382 // Emit the abbrevations code (base 1 index.)
2383 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2384 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00002385
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002386 // Emit the abbreviations data.
2387 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00002388
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002389 Asm->EOL();
2390 }
aslc200b112008-08-16 12:57:46 +00002391
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002392 // Mark end of abbreviations.
2393 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2394
2395 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00002396
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002397 Asm->EOL();
2398 }
2399 }
2400
Bill Wendling1983a2a2008-07-20 00:11:19 +00002401 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2402 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00002403 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00002404 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2405 // Define last address of section.
2406 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2407 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2408 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2409 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2410
2411 // Mark end of matrix.
2412 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2413 Asm->EmitULEB128Bytes(1); Asm->EOL();
2414 Asm->EmitInt8(1); Asm->EOL();
2415 }
2416
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002417 /// EmitDebugLines - Emit source line information.
2418 ///
2419 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00002420 // If the target is using .loc/.file, the assembler will be emitting the
2421 // .debug_line table automatically.
2422 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00002423 return;
2424
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002425 // Minimum line delta, thus ranging from -10..(255-10).
2426 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2427 // Maximum line delta, thus ranging from -10..(255-10).
2428 const int MaxLineDelta = 255 + MinLineDelta;
2429
2430 // Start the dwarf line section.
2431 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00002432
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002433 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00002434
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002435 EmitDifference("line_end", 0, "line_begin", 0, true);
2436 Asm->EOL("Length of Source Line Info");
2437 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00002438
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002439 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00002440
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002441 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2442 Asm->EOL("Prolog Length");
2443 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00002444
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002445 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2446
2447 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2448
2449 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00002450
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002451 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2452
2453 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00002454
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002455 // Line number standard opcode encodings argument count
2456 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2457 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2458 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2459 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2460 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2461 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2462 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2463 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2464 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2465
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002466 // Emit directories.
2467 for (unsigned DirectoryID = 1, NDID = Directories.size();
2468 DirectoryID <= NDID; ++DirectoryID) {
2469 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
2470 }
2471 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00002472
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002473 // Emit files.
Devang Patel6ccd57e2009-01-13 00:20:51 +00002474 for (unsigned SourceID = 1, NSID = SrcFiles.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002475 SourceID <= NSID; ++SourceID) {
Devang Patel6ccd57e2009-01-13 00:20:51 +00002476 const SrcFileInfo &SourceFile = SrcFiles[SourceID];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002477 Asm->EmitString(SourceFile.getName());
2478 Asm->EOL("Source");
2479 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2480 Asm->EOL("Directory #");
2481 Asm->EmitULEB128Bytes(0);
2482 Asm->EOL("Mod date");
2483 Asm->EmitULEB128Bytes(0);
2484 Asm->EOL("File size");
2485 }
2486 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00002487
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002488 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00002489
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002490 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00002491 unsigned SecSrcLinesSize = SectionSourceLines.size();
2492
2493 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002494 // Isolate current sections line info.
Devang Patel35a078f2009-01-12 22:54:42 +00002495 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00002496
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002497 if (VerboseAsm) {
2498 const Section* S = SectionMap[j + 1];
2499 Asm->EOL(std::string("Section ") + S->getName());
2500 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00002501 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002502
2503 // Dwarf assumes we start with first line of first source file.
2504 unsigned Source = 1;
2505 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00002506
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002507 // Construct rows of the address, source, line, column matrix.
2508 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patel35a078f2009-01-12 22:54:42 +00002509 const SrcLineInfo &LineInfo = LineInfos[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002510 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2511 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00002512
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002513 unsigned SourceID = LineInfo.getSourceID();
Devang Patel6ccd57e2009-01-13 00:20:51 +00002514 const SrcFileInfo &SourceFile = SrcFiles[SourceID];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002515 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng0eeed442008-07-01 23:18:29 +00002516 if (VerboseAsm)
2517 Asm->EOL(Directories[DirectoryID]
2518 + SourceFile.getName()
2519 + ":"
2520 + utostr_32(LineInfo.getLine()));
2521 else
2522 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002523
2524 // Define the line address.
2525 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002526 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002527 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2528 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00002529
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002530 // If change of source, then switch to the new source.
2531 if (Source != LineInfo.getSourceID()) {
2532 Source = LineInfo.getSourceID();
2533 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2534 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2535 }
aslc200b112008-08-16 12:57:46 +00002536
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002537 // If change of line.
2538 if (Line != LineInfo.getLine()) {
2539 // Determine offset.
2540 int Offset = LineInfo.getLine() - Line;
2541 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00002542
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002543 // Update line.
2544 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00002545
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002546 // If delta is small enough and in range...
2547 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2548 // ... then use fast opcode.
2549 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2550 } else {
2551 // ... otherwise use long hand.
2552 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2553 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2554 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2555 }
2556 } else {
2557 // Copy the previous row (different address or source)
2558 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2559 }
2560 }
2561
Bill Wendling1983a2a2008-07-20 00:11:19 +00002562 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002563 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00002564
2565 if (SecSrcLinesSize == 0)
2566 // Because we're emitting a debug_line section, we still need a line
2567 // table. The linker and friends expect it to exist. If there's nothing to
2568 // put into it, emit an empty table.
2569 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00002570
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002571 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00002572
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002573 Asm->EOL();
2574 }
aslc200b112008-08-16 12:57:46 +00002575
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002576 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2577 ///
2578 void EmitCommonDebugFrame() {
2579 if (!TAI->doesDwarfRequireFrameSection())
2580 return;
2581
2582 int stackGrowth =
2583 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2584 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002585 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002586
2587 // Start the dwarf frame section.
2588 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2589
2590 EmitLabel("debug_frame_common", 0);
2591 EmitDifference("debug_frame_common_end", 0,
2592 "debug_frame_common_begin", 0, true);
2593 Asm->EOL("Length of Common Information Entry");
2594
2595 EmitLabel("debug_frame_common_begin", 0);
2596 Asm->EmitInt32((int)DW_CIE_ID);
2597 Asm->EOL("CIE Identifier Tag");
2598 Asm->EmitInt8(DW_CIE_VERSION);
2599 Asm->EOL("CIE Version");
2600 Asm->EmitString("");
2601 Asm->EOL("CIE Augmentation");
2602 Asm->EmitULEB128Bytes(1);
2603 Asm->EOL("CIE Code Alignment Factor");
2604 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00002605 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002606 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002607 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00002608
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002609 std::vector<MachineMove> Moves;
2610 RI->getInitialFrameState(Moves);
2611
Dale Johannesenf5a11532007-11-13 19:13:01 +00002612 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002613
Evan Cheng7e7d1942008-02-29 19:36:59 +00002614 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002615 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00002616
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002617 Asm->EOL();
2618 }
2619
2620 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2621 /// section.
2622 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2623 if (!TAI->doesDwarfRequireFrameSection())
2624 return;
aslc200b112008-08-16 12:57:46 +00002625
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002626 // Start the dwarf frame section.
2627 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00002628
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002629 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2630 "debug_frame_begin", DebugFrameInfo.Number, true);
2631 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00002632
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002633 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2634
2635 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2636 0, 0, true, false);
2637 Asm->EOL("FDE CIE offset");
2638
2639 EmitReference("func_begin", DebugFrameInfo.Number);
2640 Asm->EOL("FDE initial location");
2641 EmitDifference("func_end", DebugFrameInfo.Number,
2642 "func_begin", DebugFrameInfo.Number);
2643 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00002644
Devang Patelb28de842009-01-17 08:01:33 +00002645 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00002646 false);
aslc200b112008-08-16 12:57:46 +00002647
Evan Cheng7e7d1942008-02-29 19:36:59 +00002648 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002649 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2650
2651 Asm->EOL();
2652 }
2653
2654 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2655 ///
2656 void EmitDebugPubNames() {
2657 // Start the dwarf pubnames section.
2658 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00002659
Devang Patel6eae2832009-01-12 23:05:55 +00002660 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2661 CE = DW_CUs.end(); CI != CE; ++CI) {
2662 CompileUnit *Unit = CI->second;
Devang Patel2ae1db52009-01-30 18:20:31 +00002663 if (MainCU)
2664 Unit = MainCU;
aslc200b112008-08-16 12:57:46 +00002665
Devang Patel6eae2832009-01-12 23:05:55 +00002666 EmitDifference("pubnames_end", Unit->getID(),
2667 "pubnames_begin", Unit->getID(), true);
2668 Asm->EOL("Length of Public Names Info");
2669
2670 EmitLabel("pubnames_begin", Unit->getID());
2671
2672 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2673
2674 EmitSectionOffset("info_begin", "section_info",
2675 Unit->getID(), 0, true, false);
2676 Asm->EOL("Offset of Compilation Unit Info");
2677
Devang Patelb28de842009-01-17 08:01:33 +00002678 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
Devang Patel245446c2009-01-17 08:05:14 +00002679 true);
Devang Patel6eae2832009-01-12 23:05:55 +00002680 Asm->EOL("Compilation Unit Length");
2681
2682 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2683
2684 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2685 GE = Globals.end();
2686 GI != GE; ++GI) {
2687 const std::string &Name = GI->first;
2688 DIE * Entity = GI->second;
2689
2690 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2691 Asm->EmitString(Name); Asm->EOL("External Name");
2692 }
2693
2694 Asm->EmitInt32(0); Asm->EOL("End Mark");
2695 EmitLabel("pubnames_end", Unit->getID());
2696
2697 Asm->EOL();
Devang Patel2ae1db52009-01-30 18:20:31 +00002698 if (MainCU)
2699 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002700 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002701 }
2702
2703 /// EmitDebugStr - Emit visible names into a debug str section.
2704 ///
2705 void EmitDebugStr() {
2706 // Check to see if it is worth the effort.
2707 if (!StringPool.empty()) {
2708 // Start the dwarf str section.
2709 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00002710
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002711 // For each of strings in the string pool.
2712 for (unsigned StringID = 1, N = StringPool.size();
2713 StringID <= N; ++StringID) {
2714 // Emit a label for reference from debug information entries.
2715 EmitLabel("string", StringID);
2716 // Emit the string itself.
2717 const std::string &String = StringPool[StringID];
2718 Asm->EmitString(String); Asm->EOL();
2719 }
aslc200b112008-08-16 12:57:46 +00002720
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002721 Asm->EOL();
2722 }
2723 }
2724
2725 /// EmitDebugLoc - Emit visible names into a debug loc section.
2726 ///
2727 void EmitDebugLoc() {
2728 // Start the dwarf loc section.
2729 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00002730
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002731 Asm->EOL();
2732 }
2733
2734 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2735 ///
2736 void EmitDebugARanges() {
2737 // Start the dwarf aranges section.
2738 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00002739
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002740 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002741#if 0
aslc200b112008-08-16 12:57:46 +00002742 CompileUnit *Unit = GetBaseCompileUnit();
2743
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002744 // Don't include size of length
2745 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00002746
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002747 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00002748
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002749 EmitReference("info_begin", Unit->getID());
2750 Asm->EOL("Offset of Compilation Unit Info");
2751
Dan Gohmancfb72b22007-09-27 23:12:31 +00002752 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002753
2754 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2755
2756 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2757 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2758
2759 // Range 1
2760 EmitReference("text_begin", 0); Asm->EOL("Address");
2761 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2762
2763 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2764 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002765#endif
aslc200b112008-08-16 12:57:46 +00002766
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002767 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002768 }
2769
2770 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2771 ///
2772 void EmitDebugRanges() {
2773 // Start the dwarf ranges section.
2774 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00002775
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002776 Asm->EOL();
2777 }
2778
2779 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2780 ///
2781 void EmitDebugMacInfo() {
Scott Michel79f01f52009-01-26 22:32:51 +00002782 if (TAI->doesSupportMacInfoSection()) {
2783 // Start the dwarf macinfo section.
2784 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00002785
Scott Michel79f01f52009-01-26 22:32:51 +00002786 Asm->EOL();
2787 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002788 }
2789
Devang Patel289f2362009-01-05 23:11:11 +00002790 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00002791 void ConstructCompileUnits() {
2792 std::string CUName = "llvm.dbg.compile_units";
2793 std::vector<GlobalVariable*> Result;
2794 getGlobalVariablesUsing(*M, CUName, Result);
2795 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
2796 RE = Result.end(); RI != RE; ++RI) {
Devang Patel7c8a2772009-01-16 19:28:14 +00002797 DICompileUnit DIUnit(*RI);
2798 unsigned ID = RecordSource(DIUnit.getDirectory(),
2799 DIUnit.getFilename());
Devang Patelb3907da2009-01-05 23:03:32 +00002800
2801 DIE *Die = new DIE(DW_TAG_compile_unit);
2802 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2803 DWLabel("section_line", 0), DWLabel("section_line", 0),
2804 false);
Devang Patel7c8a2772009-01-16 19:28:14 +00002805 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer());
2806 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
2807 AddString(Die, DW_AT_name, DW_FORM_string, DIUnit.getFilename());
2808 if (!DIUnit.getDirectory().empty())
2809 AddString(Die, DW_AT_comp_dir, DW_FORM_string, DIUnit.getDirectory());
Devang Patela880b1e2009-01-23 22:33:47 +00002810 if (DIUnit.isOptimized())
2811 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
2812 const std::string &Flags = DIUnit.getFlags();
2813 if (!Flags.empty())
2814 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
Devang Patel74193d72009-02-17 22:43:44 +00002815 unsigned RVer = DIUnit.getRunTimeVersion();
2816 if (RVer)
2817 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
Devang Patelb3907da2009-01-05 23:03:32 +00002818
2819 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel2ae1db52009-01-30 18:20:31 +00002820 if (DIUnit.isMain()) {
Bill Wendling6baa18d2009-02-03 21:38:21 +00002821 assert(!MainCU && "Multiple main compile units are found!");
Devang Patel2ae1db52009-01-30 18:20:31 +00002822 MainCU = Unit;
2823 }
Devang Patel7c8a2772009-01-16 19:28:14 +00002824 DW_CUs[DIUnit.getGV()] = Unit;
Devang Patelb3907da2009-01-05 23:03:32 +00002825 }
2826 }
2827
Devang Patel289f2362009-01-05 23:11:11 +00002828 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
Devang Patela9169c32009-02-24 00:02:15 +00002829 /// visible global variables. Return true if at least one global DIE is
2830 /// created.
2831 bool ConstructGlobalVariableDIEs() {
Devang Patel289f2362009-01-05 23:11:11 +00002832 std::string GVName = "llvm.dbg.global_variables";
2833 std::vector<GlobalVariable*> Result;
2834 getGlobalVariablesUsing(*M, GVName, Result);
Devang Patela9169c32009-02-24 00:02:15 +00002835 bool result = false;
Devang Patel289f2362009-01-05 23:11:11 +00002836 for (std::vector<GlobalVariable *>::iterator GVI = Result.begin(),
2837 GVE = Result.end(); GVI != GVE; ++GVI) {
Devang Patel7c8a2772009-01-16 19:28:14 +00002838 DIGlobalVariable DI_GV(*GVI);
Devang Patel2ae1db52009-01-30 18:20:31 +00002839 CompileUnit *DW_Unit = MainCU;
2840 if (!DW_Unit)
2841 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
Devang Patel289f2362009-01-05 23:11:11 +00002842
2843 // Check for pre-existence.
Devang Patel7c8a2772009-01-16 19:28:14 +00002844 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
Devang Patel289f2362009-01-05 23:11:11 +00002845 if (Slot) continue;
2846
Devang Patelb28de842009-01-17 08:01:33 +00002847 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
Devang Patel289f2362009-01-05 23:11:11 +00002848
2849 // Add address.
2850 DIEBlock *Block = new DIEBlock();
2851 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2852 AddObjectLabel(Block, 0, DW_FORM_udata,
Devang Patel2072db82009-01-20 18:55:39 +00002853 Asm->getGlobalLinkName(DI_GV.getGlobal()));
Devang Patel289f2362009-01-05 23:11:11 +00002854 AddBlock(VariableDie, DW_AT_location, 0, Block);
2855
2856 //Add to map.
2857 Slot = VariableDie;
Devang Patel289f2362009-01-05 23:11:11 +00002858 //Add to context owner.
2859 DW_Unit->getDie()->AddChild(VariableDie);
Devang Patel289f2362009-01-05 23:11:11 +00002860 //Expose as global. FIXME - need to check external flag.
Devang Patel7c8a2772009-01-16 19:28:14 +00002861 DW_Unit->AddGlobal(DI_GV.getName(), VariableDie);
Devang Patela9169c32009-02-24 00:02:15 +00002862
2863 if (!result)
2864 result = true;
Devang Patel289f2362009-01-05 23:11:11 +00002865 }
Devang Patela9169c32009-02-24 00:02:15 +00002866 return result;
Devang Patel289f2362009-01-05 23:11:11 +00002867 }
2868
Devang Patele6caf012009-01-05 23:21:35 +00002869 /// ConstructSubprograms - Create DIEs for each of the externally visible
Devang Patela9169c32009-02-24 00:02:15 +00002870 /// subprograms. Return true if at least one subprogram DIE is created.
2871 bool ConstructSubprograms() {
Devang Patele6caf012009-01-05 23:21:35 +00002872 std::string SPName = "llvm.dbg.subprograms";
2873 std::vector<GlobalVariable*> Result;
2874 getGlobalVariablesUsing(*M, SPName, Result);
Devang Patela9169c32009-02-24 00:02:15 +00002875 bool result = false;
Devang Patele6caf012009-01-05 23:21:35 +00002876 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
2877 RE = Result.end(); RI != RE; ++RI) {
2878
Devang Patel7c8a2772009-01-16 19:28:14 +00002879 DISubprogram SP(*RI);
Devang Patel2ae1db52009-01-30 18:20:31 +00002880 CompileUnit *Unit = MainCU;
2881 if (!Unit)
2882 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patele6caf012009-01-05 23:21:35 +00002883
Devang Patelb28de842009-01-17 08:01:33 +00002884 // Check for pre-existence.
Devang Patel7c8a2772009-01-16 19:28:14 +00002885 DIE *&Slot = Unit->getDieMapSlotFor(SP.getGV());
Devang Patele6caf012009-01-05 23:21:35 +00002886 if (Slot) continue;
2887
Devang Patelace2cf62009-02-02 17:51:41 +00002888 if (!SP.isDefinition())
2889 // This is a method declaration which will be handled while
2890 // constructing class type.
2891 continue;
2892
Devang Patelb28de842009-01-17 08:01:33 +00002893 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
Devang Patele6caf012009-01-05 23:21:35 +00002894
Devang Patele6caf012009-01-05 23:21:35 +00002895 //Add to map.
2896 Slot = SubprogramDie;
2897 //Add to context owner.
2898 Unit->getDie()->AddChild(SubprogramDie);
2899 //Expose as global.
Devang Patel7c8a2772009-01-16 19:28:14 +00002900 Unit->AddGlobal(SP.getName(), SubprogramDie);
Devang Patela9169c32009-02-24 00:02:15 +00002901
2902 if (!result)
2903 result = true;
Devang Patele6caf012009-01-05 23:21:35 +00002904 }
Devang Patela9169c32009-02-24 00:02:15 +00002905 return result;
Devang Patele6caf012009-01-05 23:21:35 +00002906 }
2907
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002908public:
2909 //===--------------------------------------------------------------------===//
2910 // Main entry points.
2911 //
Owen Anderson847b99b2008-08-21 00:14:44 +00002912 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00002913 : Dwarf(OS, A, T, "dbg")
Devang Patel2ae1db52009-01-30 18:20:31 +00002914 , MainCU(NULL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002915 , AbbreviationsSet(InitAbbreviationsSetSize)
2916 , Abbreviations()
2917 , ValuesSet(InitValuesSetSize)
2918 , Values()
2919 , StringPool()
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002920 , SectionMap()
2921 , SectionSourceLines()
2922 , didInitial(false)
2923 , shouldEmit(false)
Devang Patel4d1709e2009-01-08 02:33:41 +00002924 , RootDbgScope(NULL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002925 {
2926 }
2927 virtual ~DwarfDebug() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002928 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2929 delete Values[j];
2930 }
2931
Devang Patel9304b382009-01-06 21:07:30 +00002932 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
2933 /// This is inovked by the target AsmPrinter.
Devang Patel91d27b02009-01-12 23:09:42 +00002934 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling6baa18d2009-02-03 21:38:21 +00002935 // Create all the compile unit DIEs.
2936 ConstructCompileUnits();
Devang Patel91d27b02009-01-12 23:09:42 +00002937
Bill Wendling6baa18d2009-02-03 21:38:21 +00002938 if (DW_CUs.empty())
2939 return;
Devang Patel91d27b02009-01-12 23:09:42 +00002940
Devang Patela9169c32009-02-24 00:02:15 +00002941 // Create DIEs for each of the externally visible global variables.
2942 bool globalDIEs = ConstructGlobalVariableDIEs();
2943
2944 // Create DIEs for each of the externally visible subprograms.
2945 bool subprogramDIEs = ConstructSubprograms();
2946
2947 // If there is not any debug info available for any global variables
2948 // and any subprograms then there is not any debug info to emit.
2949 if (!globalDIEs && !subprogramDIEs)
2950 return;
2951
Bill Wendling6baa18d2009-02-03 21:38:21 +00002952 MMI = mmi;
2953 shouldEmit = true;
2954 MMI->setDebugInfoAvailability(true);
Devang Patel9304b382009-01-06 21:07:30 +00002955
Bill Wendling6baa18d2009-02-03 21:38:21 +00002956 // Prime section data.
2957 SectionMap.insert(TAI->getTextSection());
Devang Patel9304b382009-01-06 21:07:30 +00002958
Bill Wendling6baa18d2009-02-03 21:38:21 +00002959 // Print out .file directives to specify files for .loc directives. These
2960 // are printed out early so that they precede any .loc directives.
2961 if (TAI->hasDotLocAndDotFile()) {
2962 for (unsigned i = 1, e = SrcFiles.size(); i <= e; ++i) {
2963 sys::Path FullPath(Directories[SrcFiles[i].getDirectoryID()]);
2964 bool AppendOk = FullPath.appendComponent(SrcFiles[i].getName());
2965 assert(AppendOk && "Could not append filename to directory!");
2966 AppendOk = false;
2967 Asm->EmitFile(i, FullPath.toString());
2968 Asm->EOL();
Devang Patel9304b382009-01-06 21:07:30 +00002969 }
Bill Wendling6baa18d2009-02-03 21:38:21 +00002970 }
Devang Patel9304b382009-01-06 21:07:30 +00002971
Bill Wendling6baa18d2009-02-03 21:38:21 +00002972 // Emit initial sections
2973 EmitInitial();
Devang Patel9304b382009-01-06 21:07:30 +00002974 }
2975
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002976 /// BeginModule - Emit all Dwarf sections that should come prior to the
2977 /// content.
2978 void BeginModule(Module *M) {
2979 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002980 }
2981
2982 /// EndModule - Emit all Dwarf sections that should come after the content.
2983 ///
2984 void EndModule() {
Bill Wendling50db0792009-02-20 00:44:43 +00002985 if (!ShouldEmitDwarfDebug()) return;
aslc200b112008-08-16 12:57:46 +00002986
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002987 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002988 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002989 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002990 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002991 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00002992
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002993 // End text sections.
2994 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002995 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002996 EmitLabel("section_end", i);
2997 }
2998
2999 // Emit common frame information.
3000 EmitCommonDebugFrame();
3001
3002 // Emit function debug frame information
3003 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
3004 E = DebugFrames.end(); I != E; ++I)
3005 EmitFunctionDebugFrame(*I);
3006
3007 // Compute DIE offsets and sizes.
3008 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00003009
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003010 // Emit all the DIEs into a debug info section
3011 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00003012
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003013 // Corresponding abbreviations into a abbrev section.
3014 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00003015
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003016 // Emit source line correspondence into a debug line section.
3017 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00003018
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003019 // Emit info into a debug pubnames section.
3020 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00003021
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003022 // Emit info into a debug str section.
3023 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00003024
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003025 // Emit info into a debug loc section.
3026 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00003027
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003028 // Emit info into a debug aranges section.
3029 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00003030
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003031 // Emit info into a debug ranges section.
3032 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00003033
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003034 // Emit info into a debug macinfo section.
3035 EmitDebugMacInfo();
3036 }
3037
aslc200b112008-08-16 12:57:46 +00003038 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003039 /// emitted immediately after the function entry point.
3040 void BeginFunction(MachineFunction *MF) {
3041 this->MF = MF;
aslc200b112008-08-16 12:57:46 +00003042
Bill Wendling50db0792009-02-20 00:44:43 +00003043 if (!ShouldEmitDwarfDebug()) return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003044
3045 // Begin accumulating function debug information.
3046 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00003047
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003048 // Assumes in correct section after the entry point.
3049 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00003050
3051 // Emit label for the implicitly defined dbg.stoppoint at the start of
3052 // the function.
Devang Patel35a078f2009-01-12 22:54:42 +00003053 if (!Lines.empty()) {
3054 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth42f91402008-04-03 17:37:43 +00003055 Asm->printLabel(LineInfo.getLabelID());
3056 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003057 }
aslc200b112008-08-16 12:57:46 +00003058
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003059 /// EndFunction - Gather and emit post-function debug information.
3060 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003061 void EndFunction(MachineFunction *MF) {
Bill Wendling50db0792009-02-20 00:44:43 +00003062 if (!ShouldEmitDwarfDebug()) return;
aslc200b112008-08-16 12:57:46 +00003063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003064 // Define end label for subprogram.
3065 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003066
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003067 // Get function line info.
Devang Patel35a078f2009-01-12 22:54:42 +00003068 if (!Lines.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003069 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003070 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003071 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patel35a078f2009-01-12 22:54:42 +00003072 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003073 // Append the function info to section info.
3074 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patel35a078f2009-01-12 22:54:42 +00003075 Lines.begin(), Lines.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003076 }
aslc200b112008-08-16 12:57:46 +00003077
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003078 // Construct scopes for subprogram.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003079 if (RootDbgScope)
3080 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003081 else
3082 // FIXME: This is wrong. We are essentially getting past a problem with
3083 // debug information not being able to handle unreachable blocks that have
3084 // debug information in them. In particular, those unreachable blocks that
3085 // have "region end" info in them. That situation results in the "root
3086 // scope" not being created. If that's the case, then emit a "default"
3087 // scope, i.e., one that encompasses the whole function. This isn't
3088 // desirable. And a better way of handling this (and all of the debugging
3089 // information) needs to be explored.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003090 ConstructDefaultDbgScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003091
3092 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3093 MMI->getFrameMoves()));
Devang Patela4162952009-01-12 18:48:36 +00003094
3095 // Clear debug info
3096 if (RootDbgScope) {
3097 delete RootDbgScope;
3098 DbgScopeMap.clear();
3099 RootDbgScope = NULL;
3100 }
3101 Lines.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003102 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003103
3104public:
3105
Devang Patel2da0cc42009-01-15 23:41:32 +00003106 /// ValidDebugInfo - Return true if V represents valid debug info value.
3107 bool ValidDebugInfo(Value *V) {
Devang Patel208098b2009-01-19 23:21:49 +00003108 if (!V)
3109 return false;
3110
Devang Patel4d92dded2009-01-16 01:49:46 +00003111 if (!shouldEmit)
3112 return false;
3113
Devang Patel2da0cc42009-01-15 23:41:32 +00003114 GlobalVariable *GV = getGlobalVariable(V);
3115 if (!GV)
3116 return false;
3117
3118 if (GV->getLinkage() != GlobalValue::InternalLinkage
3119 && GV->getLinkage() != GlobalValue::LinkOnceLinkage)
3120 return false;
3121
3122 DIDescriptor DI(GV);
3123 // Check current version. Allow Version6 for now.
3124 unsigned Version = DI.getVersion();
Devang Patelb49710a2009-01-20 19:22:03 +00003125 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
Devang Patel2da0cc42009-01-15 23:41:32 +00003126 return false;
3127
Devang Patel208098b2009-01-19 23:21:49 +00003128 unsigned Tag = DI.getTag();
3129 switch (Tag) {
3130 case DW_TAG_variable:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003131 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003132 break;
3133 case DW_TAG_compile_unit:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003134 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003135 break;
3136 case DW_TAG_subprogram:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003137 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003138 break;
3139 default:
3140 break;
3141 }
3142
Devang Patel2da0cc42009-01-15 23:41:32 +00003143 return true;
3144 }
3145
Devang Patelcb59fd42009-01-12 19:17:34 +00003146 /// RecordSourceLine - Records location information and associates it with a
3147 /// label. Returns a unique label ID used to generate a label and provide
3148 /// correspondence to the source line list.
3149 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
3150 CompileUnit *Unit = DW_CUs[V];
Bill Wendling6baa18d2009-02-03 21:38:21 +00003151 assert(Unit && "Unable to find CompileUnit");
Devang Patelcb59fd42009-01-12 19:17:34 +00003152 unsigned ID = MMI->NextLabelID();
3153 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
3154 return ID;
3155 }
3156
3157 /// RecordSourceLine - Records location information and associates it with a
3158 /// label. Returns a unique label ID used to generate a label and provide
3159 /// correspondence to the source line list.
3160 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
3161 unsigned ID = MMI->NextLabelID();
3162 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
3163 return ID;
3164 }
3165
3166 unsigned getRecordSourceLineCount() {
3167 return Lines.size();
3168 }
3169
3170 /// RecordSource - Register a source file with debug info. Returns an source
3171 /// ID.
3172 unsigned RecordSource(const std::string &Directory,
3173 const std::string &File) {
3174 unsigned DID = Directories.insert(Directory);
3175 return SrcFiles.insert(SrcFileInfo(DID,File));
3176 }
3177
3178 /// RecordRegionStart - Indicate the start of a region.
3179 ///
3180 unsigned RecordRegionStart(GlobalVariable *V) {
3181 DbgScope *Scope = getOrCreateScope(V);
3182 unsigned ID = MMI->NextLabelID();
3183 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
3184 return ID;
3185 }
3186
3187 /// RecordRegionEnd - Indicate the end of a region.
3188 ///
3189 unsigned RecordRegionEnd(GlobalVariable *V) {
3190 DbgScope *Scope = getOrCreateScope(V);
3191 unsigned ID = MMI->NextLabelID();
3192 Scope->setEndLabelID(ID);
3193 return ID;
3194 }
3195
3196 /// RecordVariable - Indicate the declaration of a local variable.
3197 ///
3198 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Devang Patel2560d922009-01-15 18:25:17 +00003199 DIDescriptor Desc(GV);
3200 DbgScope *Scope = NULL;
3201 if (Desc.getTag() == DW_TAG_variable) {
3202 // GV is a global variable.
3203 DIGlobalVariable DG(GV);
3204 Scope = getOrCreateScope(DG.getContext().getGV());
3205 } else {
3206 // or GV is a local variable.
3207 DIVariable DV(GV);
3208 Scope = getOrCreateScope(DV.getContext().getGV());
3209 }
Bill Wendling6baa18d2009-02-03 21:38:21 +00003210 assert(Scope && "Unable to find variable' scope");
Devang Patel7c8a2772009-01-16 19:28:14 +00003211 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelcb59fd42009-01-12 19:17:34 +00003212 Scope->AddVariable(DV);
3213 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003214};
3215
3216//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003217/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003218///
3219class DwarfException : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003220 struct FunctionEHFrameInfo {
3221 std::string FnName;
3222 unsigned Number;
3223 unsigned PersonalityIndex;
3224 bool hasCalls;
3225 bool hasLandingPads;
3226 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003227 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003228
3229 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3230 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003231 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003232 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003233 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003234 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003235 };
3236
3237 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003238
3239 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3240 /// be emitted.
3241 bool shouldEmitTable;
3242
3243 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3244 /// should be emitted.
3245 bool shouldEmitMoves;
3246
3247 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3248 /// should be emitted.
3249 bool shouldEmitTableModule;
3250
aslc200b112008-08-16 12:57:46 +00003251 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003252 /// should be emitted.
3253 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003254
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003255 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3256 ///
3257 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3258 // Size and sign of stack growth.
3259 int stackGrowth =
3260 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3261 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003262 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003263
3264 // Begin eh frame section.
3265 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003266
3267 if (!TAI->doesRequireNonLocalEHFrameLabel())
3268 O << TAI->getEHGlobalPrefix();
3269 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003270 EmitLabel("section_eh_frame", Index);
3271
3272 // Define base labels.
3273 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003274
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003275 // Define the eh frame length.
3276 EmitDifference("eh_frame_common_end", Index,
3277 "eh_frame_common_begin", Index, true);
3278 Asm->EOL("Length of Common Information Entry");
3279
3280 // EH frame header.
3281 EmitLabel("eh_frame_common_begin", Index);
3282 Asm->EmitInt32((int)0);
3283 Asm->EOL("CIE Identifier Tag");
3284 Asm->EmitInt8(DW_CIE_VERSION);
3285 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003286
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003287 // The personality presence indicates that language specific information
3288 // will show up in the eh frame.
3289 Asm->EmitString(Personality ? "zPLR" : "zR");
3290 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003291
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003292 // Round out reader.
3293 Asm->EmitULEB128Bytes(1);
3294 Asm->EOL("CIE Code Alignment Factor");
3295 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003296 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003297 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003298 Asm->EOL("CIE Return Address Column");
3299
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003300 // If there is a personality, we need to indicate the functions location.
3301 if (Personality) {
3302 Asm->EmitULEB128Bytes(7);
3303 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003304
Duncan Sands96144f92008-05-07 19:11:09 +00003305 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003306 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003307 Asm->EOL("Personality (pcrel sdata4 indirect)");
3308 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003309 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003310 Asm->EOL("Personality (pcrel sdata4)");
3311 }
Bill Wendling2d369922007-09-11 17:20:55 +00003312
Duncan Sands96144f92008-05-07 19:11:09 +00003313 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003314 O << TAI->getPersonalityPrefix();
3315 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3316 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003317 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3318 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003319 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003320
Duncan Sands96144f92008-05-07 19:11:09 +00003321 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3322 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003323
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003324 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3325 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003326 } else {
3327 Asm->EmitULEB128Bytes(1);
3328 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003329
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003330 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3331 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003332 }
3333
3334 // Indicate locations of general callee saved registers in frame.
3335 std::vector<MachineMove> Moves;
3336 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003337 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003338
Dale Johannesen388f20f2008-04-30 00:43:29 +00003339 // On Darwin the linker honors the alignment of eh_frame, which means it
3340 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3341 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003342 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003343 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003344 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003345
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003346 Asm->EOL();
3347 }
Duncan Sands96144f92008-05-07 19:11:09 +00003348
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003349 /// EmitEHFrame - Emit function exception frame information.
3350 ///
3351 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003352 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3353
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003354 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3355
3356 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003357 // If the corresponding function is static, this should not be
3358 // externally visible.
Rafael Espindolaa168fc92009-01-15 20:18:42 +00003359 if (linkage != Function::InternalLinkage &&
Devang Patel245446c2009-01-17 08:05:14 +00003360 linkage != Function::PrivateLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003361 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3362 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3363 }
3364
Dale Johannesenf09b5992008-01-10 02:03:30 +00003365 // If corresponding function is weak definition, this should be too.
aslc200b112008-08-16 12:57:46 +00003366 if ((linkage == Function::WeakLinkage ||
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003367 linkage == Function::LinkOnceLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003368 TAI->getWeakDefDirective())
3369 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3370
3371 // If there are no calls then you can't unwind. This may mean we can
3372 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003373 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003374 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003375 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003376 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003377 !UnwindTablesMandatory &&
aslc200b112008-08-16 12:57:46 +00003378 ((linkage != Function::WeakLinkage &&
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003379 linkage != Function::LinkOnceLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003380 !TAI->getWeakDefDirective() ||
3381 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003382 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003383 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003384 // This name has no connection to the function, so it might get
3385 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003386 // dead-stripping unconditionally.
3387 if (const char *UsedDirective = TAI->getUsedDirective())
3388 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003389 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003390 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003391
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003392 // EH frame header.
3393 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3394 "eh_frame_begin", EHFrameInfo.Number, true);
3395 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003396
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003397 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3398
Bill Wendling189bde72008-12-24 08:05:17 +00003399 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3400 PrintRelDirective(true, true);
3401 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3402
3403 if (!TAI->isAbsoluteEHSectionOffsets())
3404 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3405 } else {
3406 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3407 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3408 true, true, false);
3409 }
3410
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003411 Asm->EOL("FDE CIE offset");
3412
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003413 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003414 Asm->EOL("FDE initial location");
3415 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003416 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003417 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003418
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003419 // If there is a personality and landing pads then point to the language
3420 // specific data area in the exception table.
3421 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003422 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003423 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003424
3425 if (EHFrameInfo.hasLandingPads)
3426 EmitReference("exception", EHFrameInfo.Number, true, true);
3427 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003428 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003429 Asm->EOL("Language Specific Data Area");
3430 } else {
3431 Asm->EmitULEB128Bytes(0);
3432 Asm->EOL("Augmentation size");
3433 }
Duncan Sands96144f92008-05-07 19:11:09 +00003434
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003435 // Indicate locations of function specific callee saved registers in
3436 // frame.
Devang Patelb28de842009-01-17 08:01:33 +00003437 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00003438 true);
aslc200b112008-08-16 12:57:46 +00003439
Dale Johannesen388f20f2008-04-30 00:43:29 +00003440 // On Darwin the linker honors the alignment of eh_frame, which means it
3441 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3442 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003443 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003444 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003445 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003446
3447 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003448 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003449 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003450 // that depends on unused functions (calling undefined externals) being
3451 // dead-stripped to link correctly. Yes, there really is.
3452 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3453 if (const char *UsedDirective = TAI->getUsedDirective())
3454 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3455 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003456 }
3457
Duncan Sands241a0c92007-09-05 11:27:52 +00003458 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003459 ///
3460 /// The general organization of the table is complex, but the basic concepts
3461 /// are easy. First there is a header which describes the location and
3462 /// organization of the three components that follow.
3463 /// 1. The landing pad site information describes the range of code covered
3464 /// by the try. In our case it's an accumulation of the ranges covered
3465 /// by the invokes in the try. There is also a reference to the landing
3466 /// pad that handles the exception once processed. Finally an index into
3467 /// the actions table.
3468 /// 2. The action table, in our case, is composed of pairs of type ids
3469 /// and next action offset. Starting with the action index from the
3470 /// landing pad site, each type Id is checked for a match to the current
3471 /// exception. If it matches then the exception and type id are passed
3472 /// on to the landing pad. Otherwise the next action is looked up. This
3473 /// chain is terminated with a next action of zero. If no type id is
3474 /// found the the frame is unwound and handling continues.
3475 /// 3. Type id table contains references to all the C++ typeinfo for all
3476 /// catches in the function. This tables is reversed indexed base 1.
3477
3478 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3479 static unsigned SharedTypeIds(const LandingPadInfo *L,
3480 const LandingPadInfo *R) {
3481 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3482 unsigned LSize = LIds.size(), RSize = RIds.size();
3483 unsigned MinSize = LSize < RSize ? LSize : RSize;
3484 unsigned Count = 0;
3485
3486 for (; Count != MinSize; ++Count)
3487 if (LIds[Count] != RIds[Count])
3488 return Count;
3489
3490 return Count;
3491 }
3492
3493 /// PadLT - Order landing pads lexicographically by type id.
3494 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3495 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3496 unsigned LSize = LIds.size(), RSize = RIds.size();
3497 unsigned MinSize = LSize < RSize ? LSize : RSize;
3498
3499 for (unsigned i = 0; i != MinSize; ++i)
3500 if (LIds[i] != RIds[i])
3501 return LIds[i] < RIds[i];
3502
3503 return LSize < RSize;
3504 }
3505
3506 struct KeyInfo {
3507 static inline unsigned getEmptyKey() { return -1U; }
3508 static inline unsigned getTombstoneKey() { return -2U; }
3509 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00003510 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003511 static bool isPod() { return true; }
3512 };
3513
Duncan Sands241a0c92007-09-05 11:27:52 +00003514 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003515 struct ActionEntry {
3516 int ValueForTypeID; // The value to write - may not be equal to the type id.
3517 int NextAction;
3518 struct ActionEntry *Previous;
3519 };
3520
Duncan Sands241a0c92007-09-05 11:27:52 +00003521 /// PadRange - Structure holding a try-range and the associated landing pad.
3522 struct PadRange {
3523 // The index of the landing pad.
3524 unsigned PadIndex;
3525 // The index of the begin and end labels in the landing pad's label lists.
3526 unsigned RangeIndex;
3527 };
3528
3529 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3530
3531 /// CallSiteEntry - Structure describing an entry in the call-site table.
3532 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00003533 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003534 unsigned BeginLabel; // zero indicates the start of the function.
3535 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003536 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003537 unsigned PadLabel; // zero indicates that there is no landing pad.
3538 unsigned Action;
3539 };
3540
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003541 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003542 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3543 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3544 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3545 if (PadInfos.empty()) return;
3546
3547 // Sort the landing pads in order of their type ids. This is used to fold
3548 // duplicate actions.
3549 SmallVector<const LandingPadInfo *, 64> LandingPads;
3550 LandingPads.reserve(PadInfos.size());
3551 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3552 LandingPads.push_back(&PadInfos[i]);
3553 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3554
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003555 // Negative type ids index into FilterIds, positive type ids index into
3556 // TypeInfos. The value written for a positive type id is just the type
3557 // id itself. For a negative type id, however, the value written is the
3558 // (negative) byte offset of the corresponding FilterIds entry. The byte
3559 // offset is usually equal to the type id, because the FilterIds entries
3560 // are written using a variable width encoding which outputs one byte per
3561 // entry as long as the value written is not too large, but can differ.
3562 // This kind of complication does not occur for positive type ids because
3563 // type infos are output using a fixed width encoding.
3564 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3565 SmallVector<int, 16> FilterOffsets;
3566 FilterOffsets.reserve(FilterIds.size());
3567 int Offset = -1;
3568 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3569 E = FilterIds.end(); I != E; ++I) {
3570 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00003571 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003572 }
3573
Duncan Sands241a0c92007-09-05 11:27:52 +00003574 // Compute the actions table and gather the first action index for each
3575 // landing pad site.
3576 SmallVector<ActionEntry, 32> Actions;
3577 SmallVector<unsigned, 64> FirstActions;
3578 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003579
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003580 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00003581 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003582 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3583 const LandingPadInfo *LP = LandingPads[i];
3584 const std::vector<int> &TypeIds = LP->TypeIds;
3585 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3586 unsigned SizeSiteActions = 0;
3587
3588 if (NumShared < TypeIds.size()) {
3589 unsigned SizeAction = 0;
3590 ActionEntry *PrevAction = 0;
3591
3592 if (NumShared) {
3593 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3594 assert(Actions.size());
3595 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00003596 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3597 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003598 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00003599 SizeAction -=
3600 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003601 SizeAction += -PrevAction->NextAction;
3602 PrevAction = PrevAction->Previous;
3603 }
3604 }
3605
3606 // Compute the actions.
3607 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3608 int TypeID = TypeIds[I];
3609 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3610 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00003611 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003612
3613 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00003614 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003615 SizeSiteActions += SizeAction;
3616
3617 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
3618 Actions.push_back(Action);
3619
3620 PrevAction = &Actions.back();
3621 }
3622
3623 // Record the first action of the landing pad site.
3624 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3625 } // else identical - re-use previous FirstAction
3626
3627 FirstActions.push_back(FirstAction);
3628
3629 // Compute this sites contribution to size.
3630 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003631 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003632
Duncan Sands4ff179f2007-12-19 07:36:31 +00003633 // Compute the call-site table. The entry for an invoke has a try-range
3634 // containing the call, a non-zero landing pad and an appropriate action.
3635 // The entry for an ordinary call has a try-range containing the call and
3636 // zero for the landing pad and the action. Calls marked 'nounwind' have
3637 // no entry and must not be contained in the try-range of any entry - they
3638 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003639 SmallVector<CallSiteEntry, 64> CallSites;
3640
3641 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003642 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3643 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3644 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00003645 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3646 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003647 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003648 unsigned BeginLabel = LandingPad->BeginLabels[j];
3649 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3650 PadRange P = { i, j };
3651 PadMap[BeginLabel] = P;
3652 }
3653 }
3654
Duncan Sands4ff179f2007-12-19 07:36:31 +00003655 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00003656 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003657
3658 // Whether there is a potentially throwing instruction (currently this means
3659 // an ordinary call) between the end of the previous try-range and now.
3660 bool SawPotentiallyThrowing = false;
3661
3662 // Whether the last callsite entry was for an invoke.
3663 bool PreviousIsInvoke = false;
3664
Duncan Sands4ff179f2007-12-19 07:36:31 +00003665 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003666 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3667 I != E; ++I) {
3668 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3669 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00003670 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00003671 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00003672 continue;
3673 }
3674
Chris Lattnerda4cff12007-12-30 20:50:28 +00003675 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00003676 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00003677
Duncan Sands4ff179f2007-12-19 07:36:31 +00003678 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00003679 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00003680 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003681
Duncan Sands4ff179f2007-12-19 07:36:31 +00003682 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00003683 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00003684 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00003685 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00003686 continue;
3687
3688 PadRange P = L->second;
3689 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3690
3691 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3692 "Inconsistent landing pad map!");
3693
3694 // If some instruction between the previous try-range and this one may
3695 // throw, create a call-site entry with no landing pad for the region
3696 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003697 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003698 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3699 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00003700 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003701 }
3702
3703 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003704 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00003705
Duncan Sands4ff179f2007-12-19 07:36:31 +00003706 if (LandingPad->LandingPadLabel) {
3707 // This try-range is for an invoke.
3708 CallSiteEntry Site = {BeginLabel, LastLabel,
3709 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00003710
Duncan Sands4ff179f2007-12-19 07:36:31 +00003711 // Try to merge with the previous call-site.
3712 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00003713 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00003714 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3715 // Extend the range of the previous entry.
3716 Prev.EndLabel = Site.EndLabel;
3717 continue;
3718 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003719 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003720
Duncan Sands4ff179f2007-12-19 07:36:31 +00003721 // Otherwise, create a new call-site.
3722 CallSites.push_back(Site);
3723 PreviousIsInvoke = true;
3724 } else {
3725 // Create a gap.
3726 PreviousIsInvoke = false;
3727 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003728 }
3729 }
3730 // If some instruction between the previous try-range and the end of the
3731 // function may throw, create a call-site entry with no landing pad for the
3732 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003733 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003734 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3735 CallSites.push_back(Site);
3736 }
3737
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003738 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00003739
3740 // Call sites.
3741 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3742 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3743 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3744 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3745 SiteLengthSize +
3746 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00003747 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00003748 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00003749
Duncan Sands96144f92008-05-07 19:11:09 +00003750 // Type infos.
3751 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3752 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003753
3754 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00003755 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003756 SizeSites + SizeActions + SizeTypes;
3757
3758 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3759 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00003760 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003761 TypeOffset;
3762
3763 unsigned SizeAlign = (4 - TotalSize) & 3;
3764
3765 // Begin the exception table.
3766 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00003767 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00003768 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003769 for (unsigned i = 0; i != SizeAlign; ++i) {
3770 Asm->EmitInt8(0);
3771 Asm->EOL("Padding");
3772 }
3773 EmitLabel("exception", SubprogramCount);
3774
3775 // Emit the header.
3776 Asm->EmitInt8(DW_EH_PE_omit);
3777 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3778 Asm->EmitInt8(DW_EH_PE_absptr);
3779 Asm->EOL("TType format (DW_EH_PE_absptr)");
3780 Asm->EmitULEB128Bytes(TypeOffset);
3781 Asm->EOL("TType base offset");
3782 Asm->EmitInt8(DW_EH_PE_udata4);
3783 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3784 Asm->EmitULEB128Bytes(SizeSites);
3785 Asm->EOL("Call-site table length");
3786
Duncan Sands241a0c92007-09-05 11:27:52 +00003787 // Emit the landing pad site information.
3788 for (unsigned i = 0; i < CallSites.size(); ++i) {
3789 CallSiteEntry &S = CallSites[i];
3790 const char *BeginTag;
3791 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003792
Duncan Sands241a0c92007-09-05 11:27:52 +00003793 if (!S.BeginLabel) {
3794 BeginTag = "eh_func_begin";
3795 BeginNumber = SubprogramCount;
3796 } else {
3797 BeginTag = "label";
3798 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003799 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003800
Duncan Sands241a0c92007-09-05 11:27:52 +00003801 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003802 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003803 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003804
Duncan Sands241a0c92007-09-05 11:27:52 +00003805 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00003806 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00003807 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003808 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00003809 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003810 }
3811 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003812
Duncan Sands96144f92008-05-07 19:11:09 +00003813 if (!S.PadLabel)
3814 Asm->EmitInt32(0);
3815 else
Duncan Sands241a0c92007-09-05 11:27:52 +00003816 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003817 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003818 Asm->EOL("Landing pad");
3819
3820 Asm->EmitULEB128Bytes(S.Action);
3821 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003822 }
3823
3824 // Emit the actions.
3825 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3826 ActionEntry &Action = Actions[I];
3827
3828 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
3829 Asm->EOL("TypeInfo index");
3830 Asm->EmitSLEB128Bytes(Action.NextAction);
3831 Asm->EOL("Next action");
3832 }
3833
3834 // Emit the type ids.
3835 for (unsigned M = TypeInfos.size(); M; --M) {
3836 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00003837
3838 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003839
3840 if (GV)
3841 O << Asm->getGlobalLinkName(GV);
3842 else
3843 O << "0";
Duncan Sands241a0c92007-09-05 11:27:52 +00003844
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003845 Asm->EOL("TypeInfo");
3846 }
3847
3848 // Emit the filter typeids.
3849 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3850 unsigned TypeID = FilterIds[j];
3851 Asm->EmitULEB128Bytes(TypeID);
3852 Asm->EOL("Filter TypeInfo index");
3853 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003854
Evan Cheng7e7d1942008-02-29 19:36:59 +00003855 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003856 }
3857
3858public:
3859 //===--------------------------------------------------------------------===//
3860 // Main entry points.
3861 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003862 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00003863 : Dwarf(OS, A, T, "eh")
Dale Johannesen85535762008-04-02 00:25:04 +00003864 , shouldEmitTable(false)
3865 , shouldEmitMoves(false)
3866 , shouldEmitTableModule(false)
3867 , shouldEmitMovesModule(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003868 {}
aslc200b112008-08-16 12:57:46 +00003869
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003870 virtual ~DwarfException() {}
3871
3872 /// SetModuleInfo - Set machine module information when it's known that pass
3873 /// manager has created it. Set by the target AsmPrinter.
3874 void SetModuleInfo(MachineModuleInfo *mmi) {
3875 MMI = mmi;
3876 }
3877
3878 /// BeginModule - Emit all exception information that should come prior to the
3879 /// content.
3880 void BeginModule(Module *M) {
3881 this->M = M;
3882 }
3883
3884 /// EndModule - Emit all exception information that should come after the
3885 /// content.
3886 void EndModule() {
Dale Johannesen85535762008-04-02 00:25:04 +00003887 if (shouldEmitMovesModule || shouldEmitTableModule) {
3888 const std::vector<Function *> Personalities = MMI->getPersonalities();
3889 for (unsigned i =0; i < Personalities.size(); ++i)
3890 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003891
Dale Johannesen85535762008-04-02 00:25:04 +00003892 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3893 E = EHFrames.end(); I != E; ++I)
3894 EmitEHFrame(*I);
3895 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003896 }
3897
aslc200b112008-08-16 12:57:46 +00003898 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003899 /// emitted immediately after the function entry point.
3900 void BeginFunction(MachineFunction *MF) {
3901 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00003902 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen62f0a6d2008-04-02 17:04:45 +00003903 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00003904
3905 // Map all labels and get rid of any dead landing pads.
3906 MMI->TidyLandingPads();
3907 // If any landing pads survive, we need an EH table.
3908 if (MMI->getLandingPads().size())
3909 shouldEmitTable = true;
3910
3911 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00003912 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00003913 shouldEmitMoves = true;
3914
3915 if (shouldEmitMoves || shouldEmitTable)
3916 // Assumes in correct section after the entry point.
3917 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003918 }
Dale Johannesen85535762008-04-02 00:25:04 +00003919 shouldEmitTableModule |= shouldEmitTable;
3920 shouldEmitMovesModule |= shouldEmitMoves;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003921 }
3922
3923 /// EndFunction - Gather and emit post-function exception information.
3924 ///
3925 void EndFunction() {
Dale Johannesen85535762008-04-02 00:25:04 +00003926 if (shouldEmitMoves || shouldEmitTable) {
3927 EmitLabel("eh_func_end", SubprogramCount);
3928 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003929
Dale Johannesen85535762008-04-02 00:25:04 +00003930 // Save EH frame information
3931 EHFrames.
3932 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingef9211a2007-09-18 01:47:22 +00003933 SubprogramCount,
3934 MMI->getPersonalityIndex(),
3935 MF->getFrameInfo()->hasCalls(),
3936 !MMI->getLandingPads().empty(),
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003937 MMI->getFrameMoves(),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003938 MF->getFunction()));
Dale Johannesen85535762008-04-02 00:25:04 +00003939 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003940 }
3941};
3942
3943} // End of namespace llvm
3944
3945//===----------------------------------------------------------------------===//
3946
3947/// Emit - Print the abbreviation using the specified Dwarf writer.
3948///
3949void DIEAbbrev::Emit(const DwarfDebug &DD) const {
3950 // Emit its Dwarf tag type.
3951 DD.getAsm()->EmitULEB128Bytes(Tag);
3952 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00003953
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003954 // Emit whether it has children DIEs.
3955 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3956 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00003957
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003958 // For each attribute description.
3959 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3960 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00003961
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003962 // Emit attribute type.
3963 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3964 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00003965
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003966 // Emit form type.
3967 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3968 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
3969 }
3970
3971 // Mark end of abbreviation.
3972 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3973 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
3974}
3975
3976#ifndef NDEBUG
3977void DIEAbbrev::print(std::ostream &O) {
3978 O << "Abbreviation @"
3979 << std::hex << (intptr_t)this << std::dec
3980 << " "
3981 << TagString(Tag)
3982 << " "
3983 << ChildrenString(ChildrenFlag)
3984 << "\n";
aslc200b112008-08-16 12:57:46 +00003985
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003986 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3987 O << " "
3988 << AttributeString(Data[i].getAttribute())
3989 << " "
3990 << FormEncodingString(Data[i].getForm())
3991 << "\n";
3992 }
3993}
3994void DIEAbbrev::dump() { print(cerr); }
3995#endif
3996
3997//===----------------------------------------------------------------------===//
3998
3999#ifndef NDEBUG
4000void DIEValue::dump() {
4001 print(cerr);
4002}
4003#endif
4004
4005//===----------------------------------------------------------------------===//
4006
4007/// EmitValue - Emit integer of appropriate size.
4008///
4009void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
4010 switch (Form) {
4011 case DW_FORM_flag: // Fall thru
4012 case DW_FORM_ref1: // Fall thru
4013 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
4014 case DW_FORM_ref2: // Fall thru
4015 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
4016 case DW_FORM_ref4: // Fall thru
4017 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
4018 case DW_FORM_ref8: // Fall thru
4019 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4020 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4021 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
4022 default: assert(0 && "DIE Value form not supported yet"); break;
4023 }
4024}
4025
4026/// SizeOf - Determine size of integer value in bytes.
4027///
4028unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4029 switch (Form) {
4030 case DW_FORM_flag: // Fall thru
4031 case DW_FORM_ref1: // Fall thru
4032 case DW_FORM_data1: return sizeof(int8_t);
4033 case DW_FORM_ref2: // Fall thru
4034 case DW_FORM_data2: return sizeof(int16_t);
4035 case DW_FORM_ref4: // Fall thru
4036 case DW_FORM_data4: return sizeof(int32_t);
4037 case DW_FORM_ref8: // Fall thru
4038 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00004039 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4040 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004041 default: assert(0 && "DIE Value form not supported yet"); break;
4042 }
4043 return 0;
4044}
4045
4046//===----------------------------------------------------------------------===//
4047
4048/// EmitValue - Emit string value.
4049///
4050void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
4051 DD.getAsm()->EmitString(String);
4052}
4053
4054//===----------------------------------------------------------------------===//
4055
4056/// EmitValue - Emit label value.
4057///
4058void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004059 bool IsSmall = Form == DW_FORM_data4;
4060 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004061}
4062
4063/// SizeOf - Determine size of label value in bytes.
4064///
4065unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004066 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004067 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004068}
4069
4070//===----------------------------------------------------------------------===//
4071
4072/// EmitValue - Emit label value.
4073///
4074void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004075 bool IsSmall = Form == DW_FORM_data4;
4076 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004077}
4078
4079/// SizeOf - Determine size of label value in bytes.
4080///
4081unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004082 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004083 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004084}
aslc200b112008-08-16 12:57:46 +00004085
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004086//===----------------------------------------------------------------------===//
4087
4088/// EmitValue - Emit delta value.
4089///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004090void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4091 bool IsSmall = Form == DW_FORM_data4;
4092 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4093 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4094}
4095
4096/// SizeOf - Determine size of delta value in bytes.
4097///
4098unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4099 if (Form == DW_FORM_data4) return 4;
4100 return DD.getTargetData()->getPointerSize();
4101}
aslc200b112008-08-16 12:57:46 +00004102
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004103//===----------------------------------------------------------------------===//
4104
4105/// EmitValue - Emit delta value.
4106///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004107void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4108 bool IsSmall = Form == DW_FORM_data4;
4109 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4110}
4111
4112/// SizeOf - Determine size of delta value in bytes.
4113///
4114unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4115 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004116 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004117}
4118
4119//===----------------------------------------------------------------------===//
4120
4121/// EmitValue - Emit debug information entry offset.
4122///
4123void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4124 DD.getAsm()->EmitInt32(Entry->getOffset());
4125}
aslc200b112008-08-16 12:57:46 +00004126
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004127//===----------------------------------------------------------------------===//
4128
4129/// ComputeSize - calculate the size of the block.
4130///
4131unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4132 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004133 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004134
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004135 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4136 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4137 }
4138 }
4139 return Size;
4140}
4141
4142/// EmitValue - Emit block data.
4143///
4144void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4145 switch (Form) {
4146 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4147 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4148 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4149 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4150 default: assert(0 && "Improper form for block"); break;
4151 }
aslc200b112008-08-16 12:57:46 +00004152
Owen Anderson88dd6232008-06-24 21:44:59 +00004153 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004154
4155 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4156 DD.getAsm()->EOL();
4157 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4158 }
4159}
4160
4161/// SizeOf - Determine size of block data in bytes.
4162///
4163unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4164 switch (Form) {
4165 case DW_FORM_block1: return Size + sizeof(int8_t);
4166 case DW_FORM_block2: return Size + sizeof(int16_t);
4167 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004168 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004169 default: assert(0 && "Improper form for block"); break;
4170 }
4171 return 0;
4172}
4173
4174//===----------------------------------------------------------------------===//
4175/// DIE Implementation
4176
4177DIE::~DIE() {
4178 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4179 delete Children[i];
4180}
aslc200b112008-08-16 12:57:46 +00004181
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004182/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4183///
4184void DIE::AddSiblingOffset() {
4185 DIEInteger *DI = new DIEInteger(0);
4186 Values.insert(Values.begin(), DI);
4187 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4188}
4189
4190/// Profile - Used to gather unique data for the value folding set.
4191///
4192void DIE::Profile(FoldingSetNodeID &ID) {
4193 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004194
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004195 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4196 ID.AddPointer(Children[i]);
4197
4198 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4199 ID.AddPointer(Values[j]);
4200}
4201
4202#ifndef NDEBUG
4203void DIE::print(std::ostream &O, unsigned IncIndent) {
4204 static unsigned IndentCount = 0;
4205 IndentCount += IncIndent;
4206 const std::string Indent(IndentCount, ' ');
4207 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004208
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004209 if (!isBlock) {
4210 O << Indent
4211 << "Die: "
4212 << "0x" << std::hex << (intptr_t)this << std::dec
4213 << ", Offset: " << Offset
4214 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004215 << "\n";
4216
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004217 O << Indent
4218 << TagString(Abbrev.getTag())
4219 << " "
4220 << ChildrenString(Abbrev.getChildrenFlag());
4221 } else {
4222 O << "Size: " << Size;
4223 }
4224 O << "\n";
4225
Owen Anderson88dd6232008-06-24 21:44:59 +00004226 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004227
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004228 IndentCount += 2;
4229 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4230 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004231
4232 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004233 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004234 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004235 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004237 O << " "
4238 << FormEncodingString(Data[i].getForm())
4239 << " ";
4240 Values[i]->print(O);
4241 O << "\n";
4242 }
4243 IndentCount -= 2;
4244
4245 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4246 Children[j]->print(O, 4);
4247 }
aslc200b112008-08-16 12:57:46 +00004248
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004249 if (!isBlock) O << "\n";
4250 IndentCount -= IncIndent;
4251}
4252
4253void DIE::dump() {
4254 print(cerr);
4255}
4256#endif
4257
4258//===----------------------------------------------------------------------===//
4259/// DwarfWriter Implementation
4260///
4261
Devang Patelaa1e8432009-01-08 23:40:34 +00004262DwarfWriter::DwarfWriter() : ImmutablePass(&ID), DD(NULL), DE(NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004263}
4264
4265DwarfWriter::~DwarfWriter() {
4266 delete DE;
4267 delete DD;
4268}
4269
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004270/// BeginModule - Emit all Dwarf sections that should come prior to the
4271/// content.
Devang Patelaa1e8432009-01-08 23:40:34 +00004272void DwarfWriter::BeginModule(Module *M,
4273 MachineModuleInfo *MMI,
4274 raw_ostream &OS, AsmPrinter *A,
4275 const TargetAsmInfo *T) {
4276 DE = new DwarfException(OS, A, T);
4277 DD = new DwarfDebug(OS, A, T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004278 DE->BeginModule(M);
4279 DD->BeginModule(M);
Devang Patel6ccd57e2009-01-13 00:20:51 +00004280 DD->SetDebugInfo(MMI);
Devang Patelaa1e8432009-01-08 23:40:34 +00004281 DE->SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004282}
4283
4284/// EndModule - Emit all Dwarf sections that should come after the content.
4285///
4286void DwarfWriter::EndModule() {
4287 DE->EndModule();
4288 DD->EndModule();
4289}
4290
aslc200b112008-08-16 12:57:46 +00004291/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004292/// emitted immediately after the function entry point.
4293void DwarfWriter::BeginFunction(MachineFunction *MF) {
4294 DE->BeginFunction(MF);
4295 DD->BeginFunction(MF);
4296}
4297
4298/// EndFunction - Gather and emit post-function debug information.
4299///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004300void DwarfWriter::EndFunction(MachineFunction *MF) {
4301 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004302 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004303
Bill Wendling5b4796a2008-07-22 00:53:37 +00004304 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004305 // Clear function debug information.
4306 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004307}
Devang Patelcb59fd42009-01-12 19:17:34 +00004308
Devang Patel2da0cc42009-01-15 23:41:32 +00004309/// ValidDebugInfo - Return true if V represents valid debug info value.
4310bool DwarfWriter::ValidDebugInfo(Value *V) {
Devang Patel943af622009-01-16 02:15:14 +00004311 return DD && DD->ValidDebugInfo(V);
Devang Patel2da0cc42009-01-15 23:41:32 +00004312}
4313
Devang Patelcb59fd42009-01-12 19:17:34 +00004314/// RecordSourceLine - Records location information and associates it with a
4315/// label. Returns a unique label ID used to generate a label and provide
4316/// correspondence to the source line list.
4317unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4318 unsigned Src) {
4319 return DD->RecordSourceLine(Line, Col, Src);
4320}
4321
4322/// RecordSource - Register a source file with debug info. Returns an source
4323/// ID.
4324unsigned DwarfWriter::RecordSource(const std::string &Dir,
4325 const std::string &File) {
4326 return DD->RecordSource(Dir, File);
4327}
4328
4329/// RecordRegionStart - Indicate the start of a region.
4330unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
4331 return DD->RecordRegionStart(V);
4332}
4333
4334/// RecordRegionEnd - Indicate the end of a region.
4335unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
4336 return DD->RecordRegionEnd(V);
4337}
4338
4339/// getRecordSourceLineCount - Count source lines.
4340unsigned DwarfWriter::getRecordSourceLineCount() {
4341 return DD->getRecordSourceLineCount();
4342}
Devang Patel70190872009-01-13 21:25:00 +00004343
Devang Patelfe359e72009-01-13 21:44:10 +00004344/// RecordVariable - Indicate the declaration of a local variable.
4345///
4346void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
4347 DD->RecordVariable(GV, FrameIndex);
4348}
Devang Patel42f6bed2009-01-13 23:54:55 +00004349
Bill Wendling50db0792009-02-20 00:44:43 +00004350/// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
4351/// be emitted.
4352bool DwarfWriter::ShouldEmitDwarfDebug() const {
4353 return DD->ShouldEmitDwarfDebug();
4354}