blob: 3057797e7f21435a1ecdc3bad2a00ecd4d4dec5a [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///
57static const unsigned InitDiesSetSize = 9; // 512
58static const unsigned InitAbbreviationsSetSize = 9; // 512
59static const unsigned InitValuesSetSize = 9; // 512
60
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 {
858
859protected:
860
861 //===--------------------------------------------------------------------===//
862 // Core attributes used by the Dwarf writer.
863 //
aslc200b112008-08-16 12:57:46 +0000864
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865 //
866 /// O - Stream to .s file.
867 ///
Owen Anderson847b99b2008-08-21 00:14:44 +0000868 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869
870 /// Asm - Target of Dwarf emission.
871 ///
872 AsmPrinter *Asm;
aslc200b112008-08-16 12:57:46 +0000873
Bill Wendlingac9639d2008-07-01 23:34:48 +0000874 /// TAI - Target asm information.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875 const TargetAsmInfo *TAI;
aslc200b112008-08-16 12:57:46 +0000876
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000877 /// TD - Target data.
878 const TargetData *TD;
aslc200b112008-08-16 12:57:46 +0000879
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880 /// RI - Register Information.
Dan Gohman1e57df32008-02-10 18:45:23 +0000881 const TargetRegisterInfo *RI;
aslc200b112008-08-16 12:57:46 +0000882
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 /// M - Current module.
884 ///
885 Module *M;
aslc200b112008-08-16 12:57:46 +0000886
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887 /// MF - Current machine function.
888 ///
889 MachineFunction *MF;
aslc200b112008-08-16 12:57:46 +0000890
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000891 /// MMI - Collected machine module information.
892 ///
893 MachineModuleInfo *MMI;
aslc200b112008-08-16 12:57:46 +0000894
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000895 /// SubprogramCount - The running count of functions being compiled.
896 ///
897 unsigned SubprogramCount;
aslc200b112008-08-16 12:57:46 +0000898
Chris Lattnerb3876c72007-09-24 03:35:37 +0000899 /// Flavor - A unique string indicating what dwarf producer this is, used to
900 /// unique labels.
901 const char * const Flavor;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902
903 unsigned SetCounter;
Owen Anderson847b99b2008-08-21 00:14:44 +0000904 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattnerb3876c72007-09-24 03:35:37 +0000905 const char *flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 : O(OS)
907 , Asm(A)
908 , TAI(T)
909 , TD(Asm->TM.getTargetData())
910 , RI(Asm->TM.getRegisterInfo())
911 , M(NULL)
912 , MF(NULL)
913 , MMI(NULL)
914 , SubprogramCount(0)
Chris Lattnerb3876c72007-09-24 03:35:37 +0000915 , Flavor(flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916 , SetCounter(1)
917 {
918 }
919
920public:
921
922 //===--------------------------------------------------------------------===//
923 // Accessors.
924 //
925 AsmPrinter *getAsm() const { return Asm; }
926 MachineModuleInfo *getMMI() const { return MMI; }
927 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohmancfb72b22007-09-27 23:12:31 +0000928 const TargetData *getTargetData() const { return TD; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000930 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
931 const {
932 if (isInSection && TAI->getDwarfSectionOffsetDirective())
933 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohmancfb72b22007-09-27 23:12:31 +0000934 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000935 O << TAI->getData32bitsDirective();
936 else
937 O << TAI->getData64bitsDirective();
938 }
aslc200b112008-08-16 12:57:46 +0000939
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000940 /// PrintLabelName - Print label name in form used by Dwarf writer.
941 ///
942 void PrintLabelName(DWLabel Label) const {
943 PrintLabelName(Label.Tag, Label.Number);
944 }
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000945 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000946 O << TAI->getPrivateGlobalPrefix() << Tag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000947 if (Number) O << Number;
948 }
aslc200b112008-08-16 12:57:46 +0000949
Chris Lattnerb3876c72007-09-24 03:35:37 +0000950 void PrintLabelName(const char *Tag, unsigned Number,
951 const char *Suffix) const {
952 O << TAI->getPrivateGlobalPrefix() << Tag;
953 if (Number) O << Number;
954 O << Suffix;
955 }
aslc200b112008-08-16 12:57:46 +0000956
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000957 /// EmitLabel - Emit location label for internal use by Dwarf.
958 ///
959 void EmitLabel(DWLabel Label) const {
960 EmitLabel(Label.Tag, Label.Number);
961 }
962 void EmitLabel(const char *Tag, unsigned Number) const {
963 PrintLabelName(Tag, Number);
964 O << ":\n";
965 }
aslc200b112008-08-16 12:57:46 +0000966
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000967 /// EmitReference - Emit a reference to a label.
968 ///
Dan Gohman4fd77742007-09-28 15:43:33 +0000969 void EmitReference(DWLabel Label, bool IsPCRelative = false,
970 bool Force32Bit = false) const {
971 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000972 }
973 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman4fd77742007-09-28 15:43:33 +0000974 bool IsPCRelative = false, bool Force32Bit = false) const {
975 PrintRelDirective(Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976 PrintLabelName(Tag, Number);
aslc200b112008-08-16 12:57:46 +0000977
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000978 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
979 }
Dan Gohman4fd77742007-09-28 15:43:33 +0000980 void EmitReference(const std::string &Name, bool IsPCRelative = false,
981 bool Force32Bit = false) const {
982 PrintRelDirective(Force32Bit);
aslc200b112008-08-16 12:57:46 +0000983
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 O << Name;
aslc200b112008-08-16 12:57:46 +0000985
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
987 }
988
989 /// EmitDifference - Emit the difference between two labels. Some
990 /// assemblers do not behave with absolute expressions with data directives,
991 /// so there is an option (needsSet) to use an intermediary set expression.
992 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
993 bool IsSmall = false) {
994 EmitDifference(LabelHi.Tag, LabelHi.Number,
995 LabelLo.Tag, LabelLo.Number,
996 IsSmall);
997 }
998 void EmitDifference(const char *TagHi, unsigned NumberHi,
999 const char *TagLo, unsigned NumberLo,
1000 bool IsSmall = false) {
1001 if (TAI->needsSet()) {
1002 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +00001003 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004 O << ",";
1005 PrintLabelName(TagHi, NumberHi);
1006 O << "-";
1007 PrintLabelName(TagLo, NumberLo);
1008 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001009
1010 PrintRelDirective(IsSmall);
Chris Lattnerb3876c72007-09-24 03:35:37 +00001011 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 ++SetCounter;
1013 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001014 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001015
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001016 PrintLabelName(TagHi, NumberHi);
1017 O << "-";
1018 PrintLabelName(TagLo, NumberLo);
1019 }
1020 }
1021
1022 void EmitSectionOffset(const char* Label, const char* Section,
1023 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesen0ebb2432008-03-26 23:31:39 +00001024 bool IsSmall = false, bool isEH = false,
1025 bool useSet = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026 bool printAbsolute = false;
Dale Johannesen0ebb2432008-03-26 23:31:39 +00001027 if (isEH)
1028 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
1029 else
1030 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
1031
1032 if (TAI->needsSet() && useSet) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001033 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +00001034 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 O << ",";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001036 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001037
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001038 if (!printAbsolute) {
1039 O << "-";
1040 PrintLabelName(Section, SectionNumber);
aslc200b112008-08-16 12:57:46 +00001041 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001042 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001043
1044 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001045
Chris Lattnerb3876c72007-09-24 03:35:37 +00001046 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047 ++SetCounter;
1048 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001049 PrintRelDirective(IsSmall, true);
aslc200b112008-08-16 12:57:46 +00001050
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001051 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001052
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001053 if (!printAbsolute) {
1054 O << "-";
1055 PrintLabelName(Section, SectionNumber);
1056 }
aslc200b112008-08-16 12:57:46 +00001057 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 }
aslc200b112008-08-16 12:57:46 +00001059
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001060 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1061 /// frame.
1062 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenf5a11532007-11-13 19:13:01 +00001063 const std::vector<MachineMove> &Moves, bool isEH) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 int stackGrowth =
1065 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1066 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00001067 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
1069
1070 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1071 const MachineMove &Move = Moves[i];
1072 unsigned LabelID = Move.getLabelID();
aslc200b112008-08-16 12:57:46 +00001073
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 if (LabelID) {
1075 LabelID = MMI->MappedLabel(LabelID);
aslc200b112008-08-16 12:57:46 +00001076
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001077 // Throw out move if the label is invalid.
1078 if (!LabelID) continue;
1079 }
aslc200b112008-08-16 12:57:46 +00001080
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001081 const MachineLocation &Dst = Move.getDestination();
1082 const MachineLocation &Src = Move.getSource();
aslc200b112008-08-16 12:57:46 +00001083
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001084 // Advance row if new location.
1085 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1086 Asm->EmitInt8(DW_CFA_advance_loc4);
1087 Asm->EOL("DW_CFA_advance_loc4");
1088 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1089 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00001090
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001091 BaseLabelID = LabelID;
1092 BaseLabel = "label";
1093 IsLocal = true;
1094 }
aslc200b112008-08-16 12:57:46 +00001095
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001096 // If advancing cfa.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001097 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1098 if (!Src.isReg()) {
1099 if (Src.getReg() == MachineLocation::VirtualFP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001100 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1101 Asm->EOL("DW_CFA_def_cfa_offset");
1102 } else {
1103 Asm->EmitInt8(DW_CFA_def_cfa);
1104 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001105 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001106 Asm->EOL("Register");
1107 }
aslc200b112008-08-16 12:57:46 +00001108
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001109 int Offset = -Src.getOffset();
aslc200b112008-08-16 12:57:46 +00001110
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001111 Asm->EmitULEB128Bytes(Offset);
1112 Asm->EOL("Offset");
1113 } else {
1114 assert(0 && "Machine move no supported yet.");
1115 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001116 } else if (Src.isReg() &&
1117 Src.getReg() == MachineLocation::VirtualFP) {
1118 if (Dst.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001119 Asm->EmitInt8(DW_CFA_def_cfa_register);
1120 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001121 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001122 Asm->EOL("Register");
1123 } else {
1124 assert(0 && "Machine move no supported yet.");
1125 }
1126 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001127 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001128 int Offset = Dst.getOffset() / stackGrowth;
aslc200b112008-08-16 12:57:46 +00001129
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130 if (Offset < 0) {
1131 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1132 Asm->EOL("DW_CFA_offset_extended_sf");
1133 Asm->EmitULEB128Bytes(Reg);
1134 Asm->EOL("Reg");
1135 Asm->EmitSLEB128Bytes(Offset);
1136 Asm->EOL("Offset");
1137 } else if (Reg < 64) {
1138 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng6181e062008-07-09 21:53:02 +00001139 if (VerboseAsm)
1140 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1141 else
1142 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143 Asm->EmitULEB128Bytes(Offset);
1144 Asm->EOL("Offset");
1145 } else {
1146 Asm->EmitInt8(DW_CFA_offset_extended);
1147 Asm->EOL("DW_CFA_offset_extended");
1148 Asm->EmitULEB128Bytes(Reg);
1149 Asm->EOL("Reg");
1150 Asm->EmitULEB128Bytes(Offset);
1151 Asm->EOL("Offset");
1152 }
1153 }
1154 }
1155 }
1156
1157};
1158
1159//===----------------------------------------------------------------------===//
Devang Patel35a078f2009-01-12 22:54:42 +00001160/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001161///
1162class SrcLineInfo {
1163 unsigned Line; // Source line number.
1164 unsigned Column; // Source column.
1165 unsigned SourceID; // Source ID number.
1166 unsigned LabelID; // Label in code ID number.
1167public:
1168 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
1169 : Line(L), Column(C), SourceID(S), LabelID(I) {}
1170
1171 // Accessors
1172 unsigned getLine() const { return Line; }
1173 unsigned getColumn() const { return Column; }
1174 unsigned getSourceID() const { return SourceID; }
1175 unsigned getLabelID() const { return LabelID; }
1176};
1177
1178
1179//===----------------------------------------------------------------------===//
Devang Patel5f244e32009-01-05 22:35:52 +00001180/// SrcFileInfo - This class is used to track source information.
1181///
1182class SrcFileInfo {
1183 unsigned DirectoryID; // Directory ID number.
1184 std::string Name; // File name (not including directory.)
1185public:
1186 SrcFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
1187
1188 // Accessors
1189 unsigned getDirectoryID() const { return DirectoryID; }
1190 const std::string &getName() const { return Name; }
1191
1192 /// operator== - Used by UniqueVector to locate entry.
1193 ///
Devang Patel42f6bed2009-01-13 23:54:55 +00001194 bool operator==(const SrcFileInfo &SI) const {
Devang Patel5f244e32009-01-05 22:35:52 +00001195 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
1196 }
1197
1198 /// operator< - Used by UniqueVector to locate entry.
1199 ///
1200 bool operator<(const SrcFileInfo &SI) const {
1201 return getDirectoryID() < SI.getDirectoryID() ||
1202 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
1203 }
1204};
1205
1206//===----------------------------------------------------------------------===//
Devang Patel4d1709e2009-01-08 02:33:41 +00001207/// DbgVariable - This class is used to track local variable information.
1208///
1209class DbgVariable {
1210private:
Devang Patel7c8a2772009-01-16 19:28:14 +00001211 DIVariable Var; // Variable Descriptor.
Devang Patel4d1709e2009-01-08 02:33:41 +00001212 unsigned FrameIndex; // Variable frame index.
1213
1214public:
Devang Patel7c8a2772009-01-16 19:28:14 +00001215 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel4d1709e2009-01-08 02:33:41 +00001216
1217 // Accessors.
Devang Patel7c8a2772009-01-16 19:28:14 +00001218 DIVariable getVariable() const { return Var; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001219 unsigned getFrameIndex() const { return FrameIndex; }
1220};
1221
1222//===----------------------------------------------------------------------===//
1223/// DbgScope - This class is used to track scope information.
1224///
1225class DbgScope {
1226private:
1227 DbgScope *Parent; // Parent to this scope.
Devang Patel49a3bd92009-01-16 18:01:58 +00001228 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001229 // Either subprogram or block.
1230 unsigned StartLabelID; // Label ID of the beginning of scope.
1231 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel49a3bd92009-01-16 18:01:58 +00001232 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel63c22f42009-01-10 02:42:49 +00001233 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001234
1235public:
Devang Patel2560d922009-01-15 18:25:17 +00001236 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel4d1709e2009-01-08 02:33:41 +00001237 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1238 {}
Devang Patela4162952009-01-12 18:48:36 +00001239 ~DbgScope() {
1240 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1241 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1242 }
Devang Patel4d1709e2009-01-08 02:33:41 +00001243
1244 // Accessors.
Devang Patel49a3bd92009-01-16 18:01:58 +00001245 DbgScope *getParent() const { return Parent; }
1246 DIDescriptor getDesc() const { return Desc; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001247 unsigned getStartLabelID() const { return StartLabelID; }
1248 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel63c22f42009-01-10 02:42:49 +00001249 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1250 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001251 void setStartLabelID(unsigned S) { StartLabelID = S; }
1252 void setEndLabelID(unsigned E) { EndLabelID = E; }
1253
1254 /// AddScope - Add a scope to the scope.
1255 ///
1256 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1257
1258 /// AddVariable - Add a variable to the scope.
1259 ///
1260 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1261};
1262
1263//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00001264/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265///
1266class DwarfDebug : public Dwarf {
1267
1268private:
1269 //===--------------------------------------------------------------------===//
1270 // Attributes used to construct specific Dwarf sections.
1271 //
aslc200b112008-08-16 12:57:46 +00001272
Devang Patel5302e672009-01-17 06:51:37 +00001273 /// DW_CUs - All the compile units involved in this build. The index
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 /// of each entry in this vector corresponds to the sources in MMI.
Devang Patel7dd15a92009-01-08 17:19:22 +00001275 DenseMap<Value *, CompileUnit *> DW_CUs;
aslc200b112008-08-16 12:57:46 +00001276
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001277 /// AbbreviationsSet - Used to uniquely define abbreviations.
1278 ///
1279 FoldingSet<DIEAbbrev> AbbreviationsSet;
1280
1281 /// Abbreviations - A list of all the unique abbreviations in use.
1282 ///
1283 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +00001284
Devang Patelb28de842009-01-17 08:01:33 +00001285 /// Directories - Uniquing vector for directories.
Devang Patel5f244e32009-01-05 22:35:52 +00001286 UniqueVector<std::string> Directories;
1287
Devang Patelb28de842009-01-17 08:01:33 +00001288 /// SourceFiles - Uniquing vector for source files.
Devang Patel5f244e32009-01-05 22:35:52 +00001289 UniqueVector<SrcFileInfo> SrcFiles;
1290
Devang Patel9b829452009-01-16 21:07:53 +00001291 /// Lines - List of of source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001292 std::vector<SrcLineInfo> Lines;
1293
Devang Patel9b829452009-01-16 21:07:53 +00001294 /// ValuesSet - Used to uniquely define values.
1295 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001296 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +00001297
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 /// Values - A list of all the unique values in use.
1299 ///
1300 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +00001301
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302 /// StringPool - A UniqueVector of strings used by indirect references.
1303 ///
1304 UniqueVector<std::string> StringPool;
1305
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306 /// SectionMap - Provides a unique id per text section.
1307 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001308 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +00001309
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001310 /// SectionSourceLines - Tracks line numbers per text section.
1311 ///
Devang Patel35a078f2009-01-12 22:54:42 +00001312 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001313
1314 /// didInitial - Flag to indicate if initial emission has been done.
1315 ///
1316 bool didInitial;
aslc200b112008-08-16 12:57:46 +00001317
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001318 /// shouldEmit - Flag to indicate if debug information should be emitted.
1319 ///
1320 bool shouldEmit;
1321
Devang Patel2560d922009-01-15 18:25:17 +00001322 // RootDbgScope - Top level scope for the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +00001323 //
1324 DbgScope *RootDbgScope;
1325
1326 // DbgScopeMap - Tracks the scopes in the current function.
1327 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
1328
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001329 struct FunctionDebugFrameInfo {
1330 unsigned Number;
1331 std::vector<MachineMove> Moves;
1332
1333 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001334 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001335 };
1336
1337 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001338
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001339public:
aslc200b112008-08-16 12:57:46 +00001340
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001341 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1342 ///
1343 bool ShouldEmitDwarf() const { return shouldEmit; }
1344
1345 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001346 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001347 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1348 // Profile the node so that we can make it unique.
1349 FoldingSetNodeID ID;
1350 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001351
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001352 // Check the set for priors.
1353 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001354
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001355 // If it's newly added.
1356 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001357 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001358 Abbreviations.push_back(&Abbrev);
1359 // Assign the vector position + 1 as its number.
1360 Abbrev.setNumber(Abbreviations.size());
1361 } else {
1362 // Assign existing abbreviation number.
1363 Abbrev.setNumber(InSet->getNumber());
1364 }
1365 }
1366
1367 /// NewString - Add a string to the constant pool and returns a label.
1368 ///
1369 DWLabel NewString(const std::string &String) {
1370 unsigned StringID = StringPool.insert(String);
1371 return DWLabel("string", StringID);
1372 }
aslc200b112008-08-16 12:57:46 +00001373
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001374 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1375 /// entry.
1376 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1377 DIEntry *Value;
aslc200b112008-08-16 12:57:46 +00001378
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379 if (Entry) {
1380 FoldingSetNodeID ID;
1381 DIEntry::Profile(ID, Entry);
1382 void *Where;
1383 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001384
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001385 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001386
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001387 Value = new DIEntry(Entry);
1388 ValuesSet.InsertNode(Value, Where);
1389 } else {
1390 Value = new DIEntry(Entry);
1391 }
aslc200b112008-08-16 12:57:46 +00001392
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393 Values.push_back(Value);
1394 return Value;
1395 }
aslc200b112008-08-16 12:57:46 +00001396
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001397 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1398 ///
1399 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1400 Value->Entry = Entry;
1401 // Add to values set if not already there. If it is, we merely have a
1402 // duplicate in the values list (no harm.)
1403 ValuesSet.GetOrInsertNode(Value);
1404 }
1405
1406 /// AddUInt - Add an unsigned integer attribute data and value.
1407 ///
1408 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1409 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1410
1411 FoldingSetNodeID ID;
1412 DIEInteger::Profile(ID, Integer);
1413 void *Where;
1414 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1415 if (!Value) {
1416 Value = new DIEInteger(Integer);
1417 ValuesSet.InsertNode(Value, Where);
1418 Values.push_back(Value);
1419 }
aslc200b112008-08-16 12:57:46 +00001420
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001421 Die->AddValue(Attribute, Form, Value);
1422 }
aslc200b112008-08-16 12:57:46 +00001423
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001424 /// AddSInt - Add an signed integer attribute data and value.
1425 ///
1426 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1427 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1428
1429 FoldingSetNodeID ID;
1430 DIEInteger::Profile(ID, (uint64_t)Integer);
1431 void *Where;
1432 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1433 if (!Value) {
1434 Value = new DIEInteger(Integer);
1435 ValuesSet.InsertNode(Value, Where);
1436 Values.push_back(Value);
1437 }
aslc200b112008-08-16 12:57:46 +00001438
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001439 Die->AddValue(Attribute, Form, Value);
1440 }
aslc200b112008-08-16 12:57:46 +00001441
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001442 /// AddString - Add a std::string attribute data and value.
1443 ///
1444 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1445 const std::string &String) {
1446 FoldingSetNodeID ID;
1447 DIEString::Profile(ID, String);
1448 void *Where;
1449 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1450 if (!Value) {
1451 Value = new DIEString(String);
1452 ValuesSet.InsertNode(Value, Where);
1453 Values.push_back(Value);
1454 }
aslc200b112008-08-16 12:57:46 +00001455
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001456 Die->AddValue(Attribute, Form, Value);
1457 }
aslc200b112008-08-16 12:57:46 +00001458
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001459 /// AddLabel - Add a Dwarf label attribute data and value.
1460 ///
1461 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1462 const DWLabel &Label) {
1463 FoldingSetNodeID ID;
1464 DIEDwarfLabel::Profile(ID, Label);
1465 void *Where;
1466 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1467 if (!Value) {
1468 Value = new DIEDwarfLabel(Label);
1469 ValuesSet.InsertNode(Value, Where);
1470 Values.push_back(Value);
1471 }
aslc200b112008-08-16 12:57:46 +00001472
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001473 Die->AddValue(Attribute, Form, Value);
1474 }
aslc200b112008-08-16 12:57:46 +00001475
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001476 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1477 ///
1478 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1479 const std::string &Label) {
1480 FoldingSetNodeID ID;
1481 DIEObjectLabel::Profile(ID, Label);
1482 void *Where;
1483 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1484 if (!Value) {
1485 Value = new DIEObjectLabel(Label);
1486 ValuesSet.InsertNode(Value, Where);
1487 Values.push_back(Value);
1488 }
aslc200b112008-08-16 12:57:46 +00001489
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001490 Die->AddValue(Attribute, Form, Value);
1491 }
aslc200b112008-08-16 12:57:46 +00001492
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001493 /// AddSectionOffset - Add a section offset label attribute data and value.
1494 ///
1495 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1496 const DWLabel &Label, const DWLabel &Section,
1497 bool isEH = false, bool useSet = true) {
1498 FoldingSetNodeID ID;
1499 DIESectionOffset::Profile(ID, Label, Section);
1500 void *Where;
1501 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1502 if (!Value) {
1503 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1504 ValuesSet.InsertNode(Value, Where);
1505 Values.push_back(Value);
1506 }
aslc200b112008-08-16 12:57:46 +00001507
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001508 Die->AddValue(Attribute, Form, Value);
1509 }
aslc200b112008-08-16 12:57:46 +00001510
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001511 /// AddDelta - Add a label delta attribute data and value.
1512 ///
1513 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1514 const DWLabel &Hi, const DWLabel &Lo) {
1515 FoldingSetNodeID ID;
1516 DIEDelta::Profile(ID, Hi, Lo);
1517 void *Where;
1518 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1519 if (!Value) {
1520 Value = new DIEDelta(Hi, Lo);
1521 ValuesSet.InsertNode(Value, Where);
1522 Values.push_back(Value);
1523 }
aslc200b112008-08-16 12:57:46 +00001524
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001525 Die->AddValue(Attribute, Form, Value);
1526 }
aslc200b112008-08-16 12:57:46 +00001527
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001528 /// AddDIEntry - Add a DIE attribute data and value.
1529 ///
1530 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1531 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1532 }
1533
1534 /// AddBlock - Add block data.
1535 ///
1536 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1537 Block->ComputeSize(*this);
1538 FoldingSetNodeID ID;
1539 Block->Profile(ID);
1540 void *Where;
1541 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1542 if (!Value) {
1543 Value = Block;
1544 ValuesSet.InsertNode(Value, Where);
1545 Values.push_back(Value);
1546 } else {
Chris Lattner3de66892007-09-21 18:25:53 +00001547 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001548 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001549 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001550 }
aslc200b112008-08-16 12:57:46 +00001551
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001552 Die->AddValue(Attribute, Block->BestForm(), Value);
1553 }
1554
1555private:
1556
1557 /// AddSourceLine - Add location information to specified debug information
1558 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001559 void AddSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001560 unsigned FileID = 0;
1561 unsigned Line = V->getLineNumber();
Devang Patelb49710a2009-01-20 19:22:03 +00001562 if (V->getVersion() <= LLVMDebugVersion6) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001563 // Version6 or earlier. Use compile unit info to get file id.
1564 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1565 FileID = Unit->getID();
1566 } else {
1567 // Version7 or newer, use filename and directory info from DIVariable
1568 // directly.
1569 unsigned DID = Directories.idFor(V->getDirectory());
1570 FileID = SrcFiles.idFor(SrcFileInfo(DID, V->getFilename()));
1571 }
1572 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1573 AddUInt(Die, DW_AT_decl_line, 0, Line);
1574 }
1575
1576 /// AddSourceLine - Add location information to specified debug information
1577 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001578 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Devang Patel5f244e32009-01-05 22:35:52 +00001579 unsigned FileID = 0;
1580 unsigned Line = G->getLineNumber();
Devang Patelb49710a2009-01-20 19:22:03 +00001581 if (G->getVersion() < LLVMDebugVersion6) {
Devang Patel5f244e32009-01-05 22:35:52 +00001582 // Version6 or earlier. Use compile unit info to get file id.
1583 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1584 FileID = Unit->getID();
1585 } else {
1586 // Version7 or newer, use filename and directory info from DIGlobal
1587 // directly.
1588 unsigned DID = Directories.idFor(G->getDirectory());
1589 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1590 }
1591 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1592 AddUInt(Die, DW_AT_decl_line, 0, Line);
1593 }
1594
Devang Patel7c8a2772009-01-16 19:28:14 +00001595 void AddSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel5f244e32009-01-05 22:35:52 +00001596 unsigned FileID = 0;
Devang Patel7c8a2772009-01-16 19:28:14 +00001597 unsigned Line = Ty->getLineNumber();
Devang Patelb49710a2009-01-20 19:22:03 +00001598 if (Ty->getVersion() <= LLVMDebugVersion6) {
Devang Patel5f244e32009-01-05 22:35:52 +00001599 // Version6 or earlier. Use compile unit info to get file id.
Devang Patel7c8a2772009-01-16 19:28:14 +00001600 CompileUnit *Unit = FindCompileUnit(Ty->getCompileUnit());
Devang Patel5f244e32009-01-05 22:35:52 +00001601 FileID = Unit->getID();
1602 } else {
Devang Patel7c8a2772009-01-16 19:28:14 +00001603 // Version7 or newer, use filename and directory info from DIType
Devang Patel5f244e32009-01-05 22:35:52 +00001604 // directly.
Devang Patel7c8a2772009-01-16 19:28:14 +00001605 unsigned DID = Directories.idFor(Ty->getDirectory());
1606 FileID = SrcFiles.idFor(SrcFileInfo(DID, Ty->getFilename()));
Devang Patel5f244e32009-01-05 22:35:52 +00001607 }
1608 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1609 AddUInt(Die, DW_AT_decl_line, 0, Line);
1610 }
1611
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001612 /// AddAddress - Add an address attribute to a die based on the location
1613 /// provided.
1614 void AddAddress(DIE *Die, unsigned Attribute,
1615 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001616 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001617 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001618
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001619 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001620 if (Reg < 32) {
1621 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1622 } else {
1623 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1624 AddUInt(Block, 0, DW_FORM_udata, Reg);
1625 }
1626 } else {
1627 if (Reg < 32) {
1628 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1629 } else {
1630 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1631 AddUInt(Block, 0, DW_FORM_udata, Reg);
1632 }
1633 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1634 }
aslc200b112008-08-16 12:57:46 +00001635
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001636 AddBlock(Die, Attribute, 0, Block);
1637 }
aslc200b112008-08-16 12:57:46 +00001638
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001639 /// AddBasicType - Add a new basic type attribute to the specified entity.
1640 ///
1641 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1642 const std::string &Name,
1643 unsigned Encoding, unsigned Size) {
aslc200b112008-08-16 12:57:46 +00001644
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001645 DIE Buffer(DW_TAG_base_type);
1646 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1647 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1648 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelf49e13d2009-01-05 17:44:11 +00001649 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1650 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001651 }
aslc200b112008-08-16 12:57:46 +00001652
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001653 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1654 ///
1655 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001656 DIE Buffer(DW_TAG_pointer_type);
Dan Gohmancfb72b22007-09-27 23:12:31 +00001657 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001658 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelbbca50b2009-01-05 17:45:59 +00001659 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1660 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001661 }
aslc200b112008-08-16 12:57:46 +00001662
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001663 /// AddType - Add a new type attribute to the specified entity.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001664 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
1665 if (Ty.isNull()) {
1666 AddBasicType(Entity, DW_Unit, "", DW_ATE_signed, sizeof(int32_t));
1667 return;
1668 }
1669
1670 // Check for pre-existence.
1671 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1672 // If it exists then use the existing value.
1673 if (Slot) {
1674 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1675 return;
1676 }
1677
1678 // Set up proxy.
1679 Slot = NewDIEntry();
1680
1681 // Construct type.
1682 DIE Buffer(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001683 if (Ty.isBasicType(Ty.getTag()))
1684 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1685 else if (Ty.isDerivedType(Ty.getTag()))
1686 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1687 else {
1688 assert (Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
1689 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1690 }
1691
Devang Patel4a4cbe72009-01-05 21:47:57 +00001692 // Add debug information entry to entity and unit.
1693 DIE *Die = DW_Unit->AddDie(Buffer);
1694 SetDIEntry(Slot, Die);
1695 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1696 }
1697
Devang Patel46d13752009-01-05 19:07:53 +00001698 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1699 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001700 DIBasicType BTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001701
1702 // Get core information.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001703 const std::string &Name = BTy.getName();
Devang Patelfc187162009-01-05 17:57:47 +00001704 Buffer.setTag(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001705 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patelfc187162009-01-05 17:57:47 +00001706 // Add name if not anonymous or intermediate type.
1707 if (!Name.empty())
1708 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001709 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelfc187162009-01-05 17:57:47 +00001710 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1711 }
1712
Devang Patel46d13752009-01-05 19:07:53 +00001713 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1714 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001715 DIDerivedType DTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001716
1717 // Get core information.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001718 const std::string &Name = DTy.getName();
1719 uint64_t Size = DTy.getSizeInBits() >> 3;
1720 unsigned Tag = DTy.getTag();
Devang Patelfc187162009-01-05 17:57:47 +00001721 // FIXME - Workaround for templates.
1722 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1723
1724 Buffer.setTag(Tag);
1725 // Map to main type, void will not have a type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001726 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001727 AddType(DW_Unit, &Buffer, FromTy);
Devang Patelfc187162009-01-05 17:57:47 +00001728
1729 // Add name if not anonymous or intermediate type.
1730 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1731
1732 // Add size if non-zero (derived types might be zero-sized.)
1733 if (Size)
1734 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1735
1736 // Add source line info if available and TyDesc is not a forward
1737 // declaration.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001738 // FIXME - Enable this. if (!DTy.isForwardDecl())
Devang Patelfc187162009-01-05 17:57:47 +00001739 // FIXME - Enable this. AddSourceLine(&Buffer, *DTy);
1740 }
1741
Devang Patel30c01372009-01-05 19:55:51 +00001742 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1743 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001744 DICompositeType CTy) {
Devang Patel30c01372009-01-05 19:55:51 +00001745
Devang Patelb28de842009-01-17 08:01:33 +00001746 // Get core information.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001747 const std::string &Name = CTy.getName();
1748 uint64_t Size = CTy.getSizeInBits() >> 3;
1749 unsigned Tag = CTy.getTag();
Devang Patel8050bd72009-01-23 01:19:09 +00001750 Buffer.setTag(Tag);
Devang Patel30c01372009-01-05 19:55:51 +00001751 switch (Tag) {
1752 case DW_TAG_vector_type:
1753 case DW_TAG_array_type:
Devang Patelef4bf3b2009-01-15 19:26:23 +00001754 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001755 break;
Devang Patel3798f492009-01-20 18:35:14 +00001756 case DW_TAG_enumeration_type:
1757 {
1758 DIArray Elements = CTy.getTypeArray();
1759 // Add enumerators to enumeration type.
1760 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1761 DIE *ElemDie = NULL;
1762 DIEnumerator Enum(Elements.getElement(i).getGV());
1763 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1764 Buffer.AddChild(ElemDie);
1765 }
1766 }
1767 break;
Devang Patel30c01372009-01-05 19:55:51 +00001768 case DW_TAG_subroutine_type:
1769 {
1770 // Add prototype flag.
1771 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001772 DIArray Elements = CTy.getTypeArray();
Devang Patel30c01372009-01-05 19:55:51 +00001773 // Add return type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001774 DIDescriptor RTy = Elements.getElement(0);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001775 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel4a4cbe72009-01-05 21:47:57 +00001776
Devang Patel30c01372009-01-05 19:55:51 +00001777 // Add arguments.
1778 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1779 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001780 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld40a7e52009-01-17 06:57:25 +00001781 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patel30c01372009-01-05 19:55:51 +00001782 Buffer.AddChild(Arg);
1783 }
1784 }
1785 break;
1786 case DW_TAG_structure_type:
1787 case DW_TAG_union_type:
1788 {
1789 // Add elements to structure type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001790 DIArray Elements = CTy.getTypeArray();
Devang Patelcf7acb12009-01-16 00:50:53 +00001791
1792 // A forward struct declared type may not have elements available.
1793 if (Elements.isNull())
1794 break;
1795
Devang Patel30c01372009-01-05 19:55:51 +00001796 // Add elements to structure type.
1797 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1798 DIDescriptor Element = Elements.getElement(i);
Devang Patelb28de842009-01-17 08:01:33 +00001799 DIE *ElemDie = NULL;
Devang Patelef4bf3b2009-01-15 19:26:23 +00001800 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel245446c2009-01-17 08:05:14 +00001801 ElemDie = CreateSubprogramDIE(DW_Unit,
1802 DISubprogram(Element.getGV()));
Devang Patelb28de842009-01-17 08:01:33 +00001803 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1804 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1805 DIGlobalVariable(Element.getGV()));
Devang Patel5c643892009-01-20 21:02:02 +00001806 else
1807 ElemDie = CreateMemberDIE(DW_Unit,
1808 DIDerivedType(Element.getGV()));
Devang Patel245446c2009-01-17 08:05:14 +00001809 Buffer.AddChild(ElemDie);
Devang Patel30c01372009-01-05 19:55:51 +00001810 }
1811 }
1812 break;
1813 default:
1814 break;
1815 }
1816
1817 // Add name if not anonymous or intermediate type.
1818 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1819
1820 // Add size if non-zero (derived types might be zero-sized.)
1821 if (Size)
1822 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1823 else {
Devang Patel30b75bb2009-01-20 22:27:02 +00001824 // Add zero size if it is not a forward declaration.
1825 if (CTy.isForwardDecl())
1826 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1827 else
1828 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
Devang Patel30c01372009-01-05 19:55:51 +00001829 }
1830
Devang Patel30b75bb2009-01-20 22:27:02 +00001831 // Add source line info if available.
1832 if (!CTy.isForwardDecl())
1833 AddSourceLine(&Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001834 }
1835
Devang Patel6fb54132009-01-05 18:33:01 +00001836 // ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001837 void ConstructSubrangeDIE (DIE &Buffer, DISubrange SR, DIE *IndexTy) {
1838 int64_t L = SR.getLo();
1839 int64_t H = SR.getHi();
Devang Patel6fb54132009-01-05 18:33:01 +00001840 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1841 if (L != H) {
1842 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1843 if (L)
Devang Patel245446c2009-01-17 08:05:14 +00001844 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1845 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel6fb54132009-01-05 18:33:01 +00001846 }
1847 Buffer.AddChild(DW_Subrange);
1848 }
1849
1850 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1851 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1852 DICompositeType *CTy) {
1853 Buffer.setTag(DW_TAG_array_type);
1854 if (CTy->getTag() == DW_TAG_vector_type)
1855 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1856
1857 DIArray Elements = CTy->getTypeArray();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001858 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel6fb54132009-01-05 18:33:01 +00001859
1860 // Construct an anonymous type for index type.
1861 DIE IdxBuffer(DW_TAG_base_type);
1862 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1863 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1864 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1865
1866 // Add subranges to array type.
1867 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel30c01372009-01-05 19:55:51 +00001868 DIDescriptor Element = Elements.getElement(i);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001869 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1870 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +00001871 }
1872 }
1873
Devang Patela566e812009-01-05 18:38:38 +00001874 /// ConstructEnumTypeDIE - Construct enum type DIE from
1875 /// DIEnumerator.
Devang Patel3798f492009-01-20 18:35:14 +00001876 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +00001877
1878 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1879 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1880 int64_t Value = ETy->getEnumValue();
1881 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Patel3798f492009-01-20 18:35:14 +00001882 return Enumerator;
Devang Patela566e812009-01-05 18:38:38 +00001883 }
Devang Patel6fb54132009-01-05 18:33:01 +00001884
Devang Patelb28de842009-01-17 08:01:33 +00001885 /// CreateGlobalVariableDIE - Create new DIE using GV.
1886 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
1887 {
1888 DIE *GVDie = new DIE(DW_TAG_variable);
1889 AddString(GVDie, DW_AT_name, DW_FORM_string, GV.getName());
1890 const std::string &LinkageName = GV.getLinkageName();
Devang Patel526b01d2009-01-05 18:59:44 +00001891 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001892 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1893 AddType(DW_Unit, GVDie, GV.getType());
1894 if (!GV.isLocalToUnit())
1895 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1896 AddSourceLine(GVDie, &GV);
1897 return GVDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001898 }
1899
Devang Patel5c643892009-01-20 21:02:02 +00001900 /// CreateMemberDIE - Create new member DIE.
1901 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1902 DIE *MemberDie = new DIE(DT.getTag());
1903 std::string Name = DT.getName();
1904 if (!Name.empty())
1905 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1906
1907 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1908
1909 AddSourceLine(MemberDie, &DT);
1910
1911 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1912 DIEBlock *Block = new DIEBlock();
1913 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1914 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1915 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1916
Devang Patel2e7ee192009-01-21 00:08:04 +00001917 if (DT.isProtected())
1918 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1919 else if (DT.isPrivate())
1920 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1921
Devang Patel5c643892009-01-20 21:02:02 +00001922 return MemberDie;
1923 }
1924
Devang Patelb28de842009-01-17 08:01:33 +00001925 /// CreateSubprogramDIE - Create new DIE using SP.
1926 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel245446c2009-01-17 08:05:14 +00001927 const DISubprogram &SP,
1928 bool IsConstructor = false) {
Devang Patelb28de842009-01-17 08:01:33 +00001929 DIE *SPDie = new DIE(DW_TAG_subprogram);
1930 AddString(SPDie, DW_AT_name, DW_FORM_string, SP.getName());
Devang Patelef4bf3b2009-01-15 19:26:23 +00001931 const std::string &LinkageName = SP.getLinkageName();
Devang Patel526b01d2009-01-05 18:59:44 +00001932 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001933 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel245446c2009-01-17 08:05:14 +00001934 LinkageName);
Devang Patelb28de842009-01-17 08:01:33 +00001935 AddSourceLine(SPDie, &SP);
Devang Patel526b01d2009-01-05 18:59:44 +00001936
Devang Patelb28de842009-01-17 08:01:33 +00001937 DICompositeType SPTy = SP.getType();
1938 DIArray Args = SPTy.getTypeArray();
1939
Devang Patel526b01d2009-01-05 18:59:44 +00001940 // Add Return Type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001941 if (!IsConstructor)
Devang Patelb28de842009-01-17 08:01:33 +00001942 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1943
Devang Patel526b01d2009-01-05 18:59:44 +00001944 // Add arguments.
Devang Patelb28de842009-01-17 08:01:33 +00001945 if (!Args.isNull())
1946 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Devang Patel245446c2009-01-17 08:05:14 +00001947 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1948 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1949 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1950 SPDie->AddChild(Arg);
Devang Patelb28de842009-01-17 08:01:33 +00001951 }
1952
Devang Patelef4bf3b2009-01-15 19:26:23 +00001953 if (!SP.isLocalToUnit())
Devang Patelb28de842009-01-17 08:01:33 +00001954 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
1955 return SPDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001956 }
1957
Devang Patelb28de842009-01-17 08:01:33 +00001958 /// FindCompileUnit - Get the compile unit for the given descriptor.
1959 ///
Devang Patel5f244e32009-01-05 22:35:52 +00001960 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
1961 CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
1962 assert(DW_Unit && "Missing compile unit.");
1963 return DW_Unit;
1964 }
1965
Devang Patel42f6bed2009-01-13 23:54:55 +00001966 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel4d1709e2009-01-08 02:33:41 +00001967 ///
1968 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1969 // Get the descriptor.
Devang Patel7c8a2772009-01-16 19:28:14 +00001970 const DIVariable &VD = DV->getVariable();
Devang Patel4d1709e2009-01-08 02:33:41 +00001971
1972 // Translate tag to proper Dwarf tag. The result variable is dropped for
1973 // now.
1974 unsigned Tag;
Devang Patel7c8a2772009-01-16 19:28:14 +00001975 switch (VD.getTag()) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001976 case DW_TAG_return_variable: return NULL;
1977 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1978 case DW_TAG_auto_variable: // fall thru
1979 default: Tag = DW_TAG_variable; break;
1980 }
1981
1982 // Define variable debug information entry.
1983 DIE *VariableDie = new DIE(Tag);
Devang Patel7c8a2772009-01-16 19:28:14 +00001984 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD.getName());
Devang Patel4d1709e2009-01-08 02:33:41 +00001985
1986 // Add source line info if available.
Devang Patel7c8a2772009-01-16 19:28:14 +00001987 AddSourceLine(VariableDie, &VD);
Devang Patel4d1709e2009-01-08 02:33:41 +00001988
1989 // Add variable type.
Devang Patel7c8a2772009-01-16 19:28:14 +00001990 AddType(Unit, VariableDie, VD.getType());
Devang Patel4d1709e2009-01-08 02:33:41 +00001991
1992 // Add variable address.
1993 MachineLocation Location;
1994 Location.set(RI->getFrameRegister(*MF),
1995 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1996 AddAddress(VariableDie, DW_AT_location, Location);
1997
1998 return VariableDie;
1999 }
2000
Devang Patel4d1709e2009-01-08 02:33:41 +00002001 /// getOrCreateScope - Returns the scope associated with the given descriptor.
2002 ///
2003 DbgScope *getOrCreateScope(GlobalVariable *V) {
2004 DbgScope *&Slot = DbgScopeMap[V];
2005 if (!Slot) {
2006 // FIXME - breaks down when the context is an inlined function.
2007 DIDescriptor ParentDesc;
Devang Patel2560d922009-01-15 18:25:17 +00002008 DIDescriptor Desc(V);
2009 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
2010 DIBlock Block(V);
2011 ParentDesc = Block.getContext();
Devang Patel4d1709e2009-01-08 02:33:41 +00002012 }
2013 DbgScope *Parent = ParentDesc.isNull() ?
Devang Pateldd49fbb2009-01-10 02:34:18 +00002014 NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel2560d922009-01-15 18:25:17 +00002015 Slot = new DbgScope(Parent, Desc);
Devang Patel4d1709e2009-01-08 02:33:41 +00002016 if (Parent) {
2017 Parent->AddScope(Slot);
2018 } else if (RootDbgScope) {
2019 // FIXME - Add inlined function scopes to the root so we can delete
2020 // them later. Long term, handle inlined functions properly.
2021 RootDbgScope->AddScope(Slot);
2022 } else {
2023 // First function is top level function.
2024 RootDbgScope = Slot;
2025 }
2026 }
2027 return Slot;
2028 }
2029
2030 /// ConstructDbgScope - Construct the components of a scope.
2031 ///
2032 void ConstructDbgScope(DbgScope *ParentScope,
2033 unsigned ParentStartID, unsigned ParentEndID,
2034 DIE *ParentDie, CompileUnit *Unit) {
2035 // Add variables to scope.
Devang Patel63c22f42009-01-10 02:42:49 +00002036 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel4d1709e2009-01-08 02:33:41 +00002037 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2038 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2039 if (VariableDie) ParentDie->AddChild(VariableDie);
2040 }
2041
2042 // Add nested scopes.
Devang Patel63c22f42009-01-10 02:42:49 +00002043 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel4d1709e2009-01-08 02:33:41 +00002044 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2045 // Define the Scope debug information entry.
2046 DbgScope *Scope = Scopes[j];
2047 // FIXME - Ignore inlined functions for the time being.
2048 if (!Scope->getParent()) continue;
2049
Devang Patelb9224922009-01-12 18:41:00 +00002050 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2051 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel4d1709e2009-01-08 02:33:41 +00002052
2053 // Ignore empty scopes.
2054 if (StartID == EndID && StartID != 0) continue;
2055 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2056
2057 if (StartID == ParentStartID && EndID == ParentEndID) {
2058 // Just add stuff to the parent scope.
2059 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2060 } else {
2061 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2062
2063 // Add the scope bounds.
2064 if (StartID) {
2065 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2066 DWLabel("label", StartID));
2067 } else {
2068 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2069 DWLabel("func_begin", SubprogramCount));
2070 }
2071 if (EndID) {
2072 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2073 DWLabel("label", EndID));
2074 } else {
2075 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2076 DWLabel("func_end", SubprogramCount));
2077 }
2078
2079 // Add the scope contents.
2080 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2081 ParentDie->AddChild(ScopeDie);
2082 }
2083 }
2084 }
2085
2086 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2087 ///
2088 void ConstructRootDbgScope(DbgScope *RootScope) {
2089 // Exit if there is no root scope.
2090 if (!RootScope) return;
Devang Patel2560d922009-01-15 18:25:17 +00002091 DIDescriptor Desc = RootScope->getDesc();
2092 if (Desc.isNull())
2093 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002094
2095 // Get the subprogram debug information entry.
Devang Patel2560d922009-01-15 18:25:17 +00002096 DISubprogram SPD(Desc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002097
2098 // Get the compile unit context.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002099 CompileUnit *Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00002100
2101 // Get the subprogram die.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002102 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002103 assert(SPDie && "Missing subprogram descriptor");
2104
2105 // Add the function bounds.
2106 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2107 DWLabel("func_begin", SubprogramCount));
2108 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2109 DWLabel("func_end", SubprogramCount));
2110 MachineLocation Location(RI->getFrameRegister(*MF));
2111 AddAddress(SPDie, DW_AT_frame_base, Location);
2112
2113 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2114 }
2115
2116 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2117 ///
2118 void ConstructDefaultDbgScope(MachineFunction *MF) {
2119 // Find the correct subprogram descriptor.
2120 std::string SPName = "llvm.dbg.subprograms";
2121 std::vector<GlobalVariable*> Result;
2122 getGlobalVariablesUsing(*M, SPName, Result);
2123 for (std::vector<GlobalVariable *>::iterator I = Result.begin(),
2124 E = Result.end(); I != E; ++I) {
2125
Devang Patel7c8a2772009-01-16 19:28:14 +00002126 DISubprogram SPD(*I);
Devang Patel4d1709e2009-01-08 02:33:41 +00002127
Devang Patel7c8a2772009-01-16 19:28:14 +00002128 if (SPD.getName() == MF->getFunction()->getName()) {
Devang Patel4d1709e2009-01-08 02:33:41 +00002129 // Get the compile unit context.
Devang Patel7c8a2772009-01-16 19:28:14 +00002130 CompileUnit *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 Patel4d1709e2009-01-08 02:33:41 +00002134 assert(SPDie && "Missing subprogram descriptor");
2135
2136 // Add the function bounds.
2137 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2138 DWLabel("func_begin", SubprogramCount));
2139 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2140 DWLabel("func_end", SubprogramCount));
2141
2142 MachineLocation Location(RI->getFrameRegister(*MF));
2143 AddAddress(SPDie, DW_AT_frame_base, Location);
2144 return;
2145 }
2146 }
2147#if 0
2148 // FIXME: This is causing an abort because C++ mangled names are compared
2149 // with their unmangled counterparts. See PR2885. Don't do this assert.
2150 assert(0 && "Couldn't find DIE for machine function!");
2151#endif
2152 }
2153
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002154 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2155 /// tools to recognize the object file contains Dwarf information.
2156 void EmitInitial() {
2157 // Check to see if we already emitted intial headers.
2158 if (didInitial) return;
2159 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002160
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002161 // Dwarf sections base addresses.
2162 if (TAI->doesDwarfRequireFrameSection()) {
2163 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2164 EmitLabel("section_debug_frame", 0);
2165 }
2166 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2167 EmitLabel("section_info", 0);
2168 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2169 EmitLabel("section_abbrev", 0);
2170 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2171 EmitLabel("section_aranges", 0);
2172 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2173 EmitLabel("section_macinfo", 0);
2174 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2175 EmitLabel("section_line", 0);
2176 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2177 EmitLabel("section_loc", 0);
2178 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2179 EmitLabel("section_pubnames", 0);
2180 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2181 EmitLabel("section_str", 0);
2182 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2183 EmitLabel("section_ranges", 0);
2184
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002185 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002186 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002187 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002188 EmitLabel("data_begin", 0);
2189 }
2190
2191 /// EmitDIE - Recusively Emits a debug information entry.
2192 ///
2193 void EmitDIE(DIE *Die) {
2194 // Get the abbreviation for this DIE.
2195 unsigned AbbrevNumber = Die->getAbbrevNumber();
2196 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002197
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002198 Asm->EOL();
2199
2200 // Emit the code (index) for the abbreviation.
2201 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002202
2203 if (VerboseAsm)
2204 Asm->EOL(std::string("Abbrev [" +
2205 utostr(AbbrevNumber) +
2206 "] 0x" + utohexstr(Die->getOffset()) +
2207 ":0x" + utohexstr(Die->getSize()) + " " +
2208 TagString(Abbrev->getTag())));
2209 else
2210 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002211
Owen Anderson88dd6232008-06-24 21:44:59 +00002212 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2213 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002214
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002215 // Emit the DIE attribute values.
2216 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2217 unsigned Attr = AbbrevData[i].getAttribute();
2218 unsigned Form = AbbrevData[i].getForm();
2219 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002220
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002221 switch (Attr) {
2222 case DW_AT_sibling: {
2223 Asm->EmitInt32(Die->SiblingOffset());
2224 break;
2225 }
2226 default: {
2227 // Emit an attribute using the defined form.
2228 Values[i]->EmitValue(*this, Form);
2229 break;
2230 }
2231 }
aslc200b112008-08-16 12:57:46 +00002232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002233 Asm->EOL(AttributeString(Attr));
2234 }
aslc200b112008-08-16 12:57:46 +00002235
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002236 // Emit the DIE children if any.
2237 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2238 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002239
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002240 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2241 EmitDIE(Children[j]);
2242 }
aslc200b112008-08-16 12:57:46 +00002243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002244 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2245 }
2246 }
2247
2248 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2249 ///
2250 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2251 // Get the children.
2252 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002253
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002254 // If not last sibling and has children then add sibling offset attribute.
2255 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2256
2257 // Record the abbreviation.
2258 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002259
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002260 // Get the abbreviation for this DIE.
2261 unsigned AbbrevNumber = Die->getAbbrevNumber();
2262 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2263
2264 // Set DIE offset
2265 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002266
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002267 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002268 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2269
Owen Anderson88dd6232008-06-24 21:44:59 +00002270 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2271 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002272
2273 // Size the DIE attribute values.
2274 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2275 // Size attribute value.
2276 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2277 }
aslc200b112008-08-16 12:57:46 +00002278
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002279 // Size the DIE children if any.
2280 if (!Children.empty()) {
2281 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2282 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002283
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002284 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2285 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2286 }
aslc200b112008-08-16 12:57:46 +00002287
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002288 // End of children marker.
2289 Offset += sizeof(int8_t);
2290 }
2291
2292 Die->setSize(Offset - Die->getOffset());
2293 return Offset;
2294 }
2295
2296 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2297 ///
2298 void SizeAndOffsets() {
2299 // Process base compile unit.
Devang Patel6eae2832009-01-12 23:05:55 +00002300 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2301 CE = DW_CUs.end(); CI != CE; ++CI) {
2302 CompileUnit *Unit = CI->second;
2303 // Compute size of compile unit header
2304 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2305 sizeof(int16_t) + // DWARF version number
2306 sizeof(int32_t) + // Offset Into Abbrev. Section
2307 sizeof(int8_t); // Pointer Size (in bytes)
2308 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2309 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002310 }
2311
2312 /// EmitDebugInfo - Emit the debug info section.
2313 ///
2314 void EmitDebugInfo() {
2315 // Start debug info section.
2316 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002317
Devang Patel6eae2832009-01-12 23:05:55 +00002318 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2319 CE = DW_CUs.end(); CI != CE; ++CI) {
2320 CompileUnit *Unit = CI->second;
2321 DIE *Die = Unit->getDie();
2322 // Emit the compile units header.
2323 EmitLabel("info_begin", Unit->getID());
2324 // Emit size of content not including length itself
2325 unsigned ContentSize = Die->getSize() +
2326 sizeof(int16_t) + // DWARF version number
2327 sizeof(int32_t) + // Offset Into Abbrev. Section
2328 sizeof(int8_t) + // Pointer Size (in bytes)
2329 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2330
2331 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2332 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2333 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2334 Asm->EOL("Offset Into Abbrev. Section");
2335 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2336
2337 EmitDIE(Die);
2338 // FIXME - extra padding for gdb bug.
2339 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2340 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2341 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2342 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2343 EmitLabel("info_end", Unit->getID());
2344
2345 Asm->EOL();
2346 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002347 }
2348
2349 /// EmitAbbreviations - Emit the abbreviation section.
2350 ///
2351 void EmitAbbreviations() const {
2352 // Check to see if it is worth the effort.
2353 if (!Abbreviations.empty()) {
2354 // Start the debug abbrev section.
2355 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00002356
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002357 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00002358
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002359 // For each abbrevation.
2360 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2361 // Get abbreviation data
2362 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00002363
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002364 // Emit the abbrevations code (base 1 index.)
2365 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2366 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00002367
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002368 // Emit the abbreviations data.
2369 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00002370
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002371 Asm->EOL();
2372 }
aslc200b112008-08-16 12:57:46 +00002373
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002374 // Mark end of abbreviations.
2375 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2376
2377 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00002378
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002379 Asm->EOL();
2380 }
2381 }
2382
Bill Wendling1983a2a2008-07-20 00:11:19 +00002383 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2384 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00002385 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00002386 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2387 // Define last address of section.
2388 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2389 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2390 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2391 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2392
2393 // Mark end of matrix.
2394 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2395 Asm->EmitULEB128Bytes(1); Asm->EOL();
2396 Asm->EmitInt8(1); Asm->EOL();
2397 }
2398
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002399 /// EmitDebugLines - Emit source line information.
2400 ///
2401 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00002402 // If the target is using .loc/.file, the assembler will be emitting the
2403 // .debug_line table automatically.
2404 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00002405 return;
2406
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002407 // Minimum line delta, thus ranging from -10..(255-10).
2408 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2409 // Maximum line delta, thus ranging from -10..(255-10).
2410 const int MaxLineDelta = 255 + MinLineDelta;
2411
2412 // Start the dwarf line section.
2413 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00002414
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002415 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00002416
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002417 EmitDifference("line_end", 0, "line_begin", 0, true);
2418 Asm->EOL("Length of Source Line Info");
2419 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00002420
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002421 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00002422
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002423 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2424 Asm->EOL("Prolog Length");
2425 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00002426
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002427 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2428
2429 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2430
2431 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00002432
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002433 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2434
2435 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00002436
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002437 // Line number standard opcode encodings argument count
2438 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2439 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2440 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2441 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2442 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2443 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2444 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2445 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2446 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2447
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002448 // Emit directories.
2449 for (unsigned DirectoryID = 1, NDID = Directories.size();
2450 DirectoryID <= NDID; ++DirectoryID) {
2451 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
2452 }
2453 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00002454
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002455 // Emit files.
Devang Patel6ccd57e2009-01-13 00:20:51 +00002456 for (unsigned SourceID = 1, NSID = SrcFiles.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002457 SourceID <= NSID; ++SourceID) {
Devang Patel6ccd57e2009-01-13 00:20:51 +00002458 const SrcFileInfo &SourceFile = SrcFiles[SourceID];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002459 Asm->EmitString(SourceFile.getName());
2460 Asm->EOL("Source");
2461 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2462 Asm->EOL("Directory #");
2463 Asm->EmitULEB128Bytes(0);
2464 Asm->EOL("Mod date");
2465 Asm->EmitULEB128Bytes(0);
2466 Asm->EOL("File size");
2467 }
2468 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00002469
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002470 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00002471
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002472 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00002473 unsigned SecSrcLinesSize = SectionSourceLines.size();
2474
2475 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002476 // Isolate current sections line info.
Devang Patel35a078f2009-01-12 22:54:42 +00002477 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00002478
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002479 if (VerboseAsm) {
2480 const Section* S = SectionMap[j + 1];
2481 Asm->EOL(std::string("Section ") + S->getName());
2482 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00002483 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002484
2485 // Dwarf assumes we start with first line of first source file.
2486 unsigned Source = 1;
2487 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00002488
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002489 // Construct rows of the address, source, line, column matrix.
2490 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patel35a078f2009-01-12 22:54:42 +00002491 const SrcLineInfo &LineInfo = LineInfos[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002492 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2493 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00002494
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002495 unsigned SourceID = LineInfo.getSourceID();
Devang Patel6ccd57e2009-01-13 00:20:51 +00002496 const SrcFileInfo &SourceFile = SrcFiles[SourceID];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002497 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng0eeed442008-07-01 23:18:29 +00002498 if (VerboseAsm)
2499 Asm->EOL(Directories[DirectoryID]
2500 + SourceFile.getName()
2501 + ":"
2502 + utostr_32(LineInfo.getLine()));
2503 else
2504 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002505
2506 // Define the line address.
2507 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002508 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002509 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2510 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00002511
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002512 // If change of source, then switch to the new source.
2513 if (Source != LineInfo.getSourceID()) {
2514 Source = LineInfo.getSourceID();
2515 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2516 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2517 }
aslc200b112008-08-16 12:57:46 +00002518
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002519 // If change of line.
2520 if (Line != LineInfo.getLine()) {
2521 // Determine offset.
2522 int Offset = LineInfo.getLine() - Line;
2523 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00002524
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002525 // Update line.
2526 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00002527
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002528 // If delta is small enough and in range...
2529 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2530 // ... then use fast opcode.
2531 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2532 } else {
2533 // ... otherwise use long hand.
2534 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2535 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2536 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2537 }
2538 } else {
2539 // Copy the previous row (different address or source)
2540 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2541 }
2542 }
2543
Bill Wendling1983a2a2008-07-20 00:11:19 +00002544 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002545 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00002546
2547 if (SecSrcLinesSize == 0)
2548 // Because we're emitting a debug_line section, we still need a line
2549 // table. The linker and friends expect it to exist. If there's nothing to
2550 // put into it, emit an empty table.
2551 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00002552
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002553 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00002554
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002555 Asm->EOL();
2556 }
aslc200b112008-08-16 12:57:46 +00002557
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002558 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2559 ///
2560 void EmitCommonDebugFrame() {
2561 if (!TAI->doesDwarfRequireFrameSection())
2562 return;
2563
2564 int stackGrowth =
2565 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2566 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002567 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002568
2569 // Start the dwarf frame section.
2570 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2571
2572 EmitLabel("debug_frame_common", 0);
2573 EmitDifference("debug_frame_common_end", 0,
2574 "debug_frame_common_begin", 0, true);
2575 Asm->EOL("Length of Common Information Entry");
2576
2577 EmitLabel("debug_frame_common_begin", 0);
2578 Asm->EmitInt32((int)DW_CIE_ID);
2579 Asm->EOL("CIE Identifier Tag");
2580 Asm->EmitInt8(DW_CIE_VERSION);
2581 Asm->EOL("CIE Version");
2582 Asm->EmitString("");
2583 Asm->EOL("CIE Augmentation");
2584 Asm->EmitULEB128Bytes(1);
2585 Asm->EOL("CIE Code Alignment Factor");
2586 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00002587 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002588 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002589 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00002590
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002591 std::vector<MachineMove> Moves;
2592 RI->getInitialFrameState(Moves);
2593
Dale Johannesenf5a11532007-11-13 19:13:01 +00002594 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002595
Evan Cheng7e7d1942008-02-29 19:36:59 +00002596 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002597 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00002598
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002599 Asm->EOL();
2600 }
2601
2602 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2603 /// section.
2604 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2605 if (!TAI->doesDwarfRequireFrameSection())
2606 return;
aslc200b112008-08-16 12:57:46 +00002607
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002608 // Start the dwarf frame section.
2609 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00002610
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002611 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2612 "debug_frame_begin", DebugFrameInfo.Number, true);
2613 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00002614
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002615 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2616
2617 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2618 0, 0, true, false);
2619 Asm->EOL("FDE CIE offset");
2620
2621 EmitReference("func_begin", DebugFrameInfo.Number);
2622 Asm->EOL("FDE initial location");
2623 EmitDifference("func_end", DebugFrameInfo.Number,
2624 "func_begin", DebugFrameInfo.Number);
2625 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00002626
Devang Patelb28de842009-01-17 08:01:33 +00002627 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00002628 false);
aslc200b112008-08-16 12:57:46 +00002629
Evan Cheng7e7d1942008-02-29 19:36:59 +00002630 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002631 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2632
2633 Asm->EOL();
2634 }
2635
2636 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2637 ///
2638 void EmitDebugPubNames() {
2639 // Start the dwarf pubnames section.
2640 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00002641
Devang Patel6eae2832009-01-12 23:05:55 +00002642 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2643 CE = DW_CUs.end(); CI != CE; ++CI) {
2644 CompileUnit *Unit = CI->second;
aslc200b112008-08-16 12:57:46 +00002645
Devang Patel6eae2832009-01-12 23:05:55 +00002646 EmitDifference("pubnames_end", Unit->getID(),
2647 "pubnames_begin", Unit->getID(), true);
2648 Asm->EOL("Length of Public Names Info");
2649
2650 EmitLabel("pubnames_begin", Unit->getID());
2651
2652 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2653
2654 EmitSectionOffset("info_begin", "section_info",
2655 Unit->getID(), 0, true, false);
2656 Asm->EOL("Offset of Compilation Unit Info");
2657
Devang Patelb28de842009-01-17 08:01:33 +00002658 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
Devang Patel245446c2009-01-17 08:05:14 +00002659 true);
Devang Patel6eae2832009-01-12 23:05:55 +00002660 Asm->EOL("Compilation Unit Length");
2661
2662 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2663
2664 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2665 GE = Globals.end();
2666 GI != GE; ++GI) {
2667 const std::string &Name = GI->first;
2668 DIE * Entity = GI->second;
2669
2670 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2671 Asm->EmitString(Name); Asm->EOL("External Name");
2672 }
2673
2674 Asm->EmitInt32(0); Asm->EOL("End Mark");
2675 EmitLabel("pubnames_end", Unit->getID());
2676
2677 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002678 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002679 }
2680
2681 /// EmitDebugStr - Emit visible names into a debug str section.
2682 ///
2683 void EmitDebugStr() {
2684 // Check to see if it is worth the effort.
2685 if (!StringPool.empty()) {
2686 // Start the dwarf str section.
2687 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00002688
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002689 // For each of strings in the string pool.
2690 for (unsigned StringID = 1, N = StringPool.size();
2691 StringID <= N; ++StringID) {
2692 // Emit a label for reference from debug information entries.
2693 EmitLabel("string", StringID);
2694 // Emit the string itself.
2695 const std::string &String = StringPool[StringID];
2696 Asm->EmitString(String); Asm->EOL();
2697 }
aslc200b112008-08-16 12:57:46 +00002698
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002699 Asm->EOL();
2700 }
2701 }
2702
2703 /// EmitDebugLoc - Emit visible names into a debug loc section.
2704 ///
2705 void EmitDebugLoc() {
2706 // Start the dwarf loc section.
2707 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00002708
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002709 Asm->EOL();
2710 }
2711
2712 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2713 ///
2714 void EmitDebugARanges() {
2715 // Start the dwarf aranges section.
2716 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00002717
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002718 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002719#if 0
aslc200b112008-08-16 12:57:46 +00002720 CompileUnit *Unit = GetBaseCompileUnit();
2721
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002722 // Don't include size of length
2723 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00002724
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002725 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00002726
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002727 EmitReference("info_begin", Unit->getID());
2728 Asm->EOL("Offset of Compilation Unit Info");
2729
Dan Gohmancfb72b22007-09-27 23:12:31 +00002730 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002731
2732 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2733
2734 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2735 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2736
2737 // Range 1
2738 EmitReference("text_begin", 0); Asm->EOL("Address");
2739 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2740
2741 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2742 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002743#endif
aslc200b112008-08-16 12:57:46 +00002744
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002745 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002746 }
2747
2748 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2749 ///
2750 void EmitDebugRanges() {
2751 // Start the dwarf ranges section.
2752 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00002753
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002754 Asm->EOL();
2755 }
2756
2757 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2758 ///
2759 void EmitDebugMacInfo() {
2760 // Start the dwarf macinfo section.
2761 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00002762
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002763 Asm->EOL();
2764 }
2765
Devang Patel289f2362009-01-05 23:11:11 +00002766 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00002767 void ConstructCompileUnits() {
2768 std::string CUName = "llvm.dbg.compile_units";
2769 std::vector<GlobalVariable*> Result;
2770 getGlobalVariablesUsing(*M, CUName, Result);
2771 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
2772 RE = Result.end(); RI != RE; ++RI) {
Devang Patel7c8a2772009-01-16 19:28:14 +00002773 DICompileUnit DIUnit(*RI);
2774 unsigned ID = RecordSource(DIUnit.getDirectory(),
2775 DIUnit.getFilename());
Devang Patelb3907da2009-01-05 23:03:32 +00002776
2777 DIE *Die = new DIE(DW_TAG_compile_unit);
2778 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2779 DWLabel("section_line", 0), DWLabel("section_line", 0),
2780 false);
Devang Patel7c8a2772009-01-16 19:28:14 +00002781 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer());
2782 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
2783 AddString(Die, DW_AT_name, DW_FORM_string, DIUnit.getFilename());
2784 if (!DIUnit.getDirectory().empty())
2785 AddString(Die, DW_AT_comp_dir, DW_FORM_string, DIUnit.getDirectory());
Devang Patelb3907da2009-01-05 23:03:32 +00002786
2787 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel7c8a2772009-01-16 19:28:14 +00002788 DW_CUs[DIUnit.getGV()] = Unit;
Devang Patelb3907da2009-01-05 23:03:32 +00002789 }
2790 }
2791
Devang Patel289f2362009-01-05 23:11:11 +00002792 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
2793 /// visible global variables.
2794 void ConstructGlobalVariableDIEs() {
2795 std::string GVName = "llvm.dbg.global_variables";
2796 std::vector<GlobalVariable*> Result;
2797 getGlobalVariablesUsing(*M, GVName, Result);
2798 for (std::vector<GlobalVariable *>::iterator GVI = Result.begin(),
2799 GVE = Result.end(); GVI != GVE; ++GVI) {
Devang Patel7c8a2772009-01-16 19:28:14 +00002800 DIGlobalVariable DI_GV(*GVI);
2801 CompileUnit *DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
Devang Patel289f2362009-01-05 23:11:11 +00002802
2803 // Check for pre-existence.
Devang Patel7c8a2772009-01-16 19:28:14 +00002804 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
Devang Patel289f2362009-01-05 23:11:11 +00002805 if (Slot) continue;
2806
Devang Patelb28de842009-01-17 08:01:33 +00002807 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
Devang Patel289f2362009-01-05 23:11:11 +00002808
2809 // Add address.
2810 DIEBlock *Block = new DIEBlock();
2811 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2812 AddObjectLabel(Block, 0, DW_FORM_udata,
Devang Patel2072db82009-01-20 18:55:39 +00002813 Asm->getGlobalLinkName(DI_GV.getGlobal()));
Devang Patel289f2362009-01-05 23:11:11 +00002814 AddBlock(VariableDie, DW_AT_location, 0, Block);
2815
2816 //Add to map.
2817 Slot = VariableDie;
2818
2819 //Add to context owner.
2820 DW_Unit->getDie()->AddChild(VariableDie);
2821
2822 //Expose as global. FIXME - need to check external flag.
Devang Patel7c8a2772009-01-16 19:28:14 +00002823 DW_Unit->AddGlobal(DI_GV.getName(), VariableDie);
Devang Patel289f2362009-01-05 23:11:11 +00002824 }
2825 }
2826
Devang Patele6caf012009-01-05 23:21:35 +00002827 /// ConstructSubprograms - Create DIEs for each of the externally visible
2828 /// subprograms.
2829 void ConstructSubprograms() {
2830
2831 std::string SPName = "llvm.dbg.subprograms";
2832 std::vector<GlobalVariable*> Result;
2833 getGlobalVariablesUsing(*M, SPName, Result);
2834 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
2835 RE = Result.end(); RI != RE; ++RI) {
2836
Devang Patel7c8a2772009-01-16 19:28:14 +00002837 DISubprogram SP(*RI);
2838 CompileUnit *Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patele6caf012009-01-05 23:21:35 +00002839
Devang Patelb28de842009-01-17 08:01:33 +00002840 // Check for pre-existence.
Devang Patel7c8a2772009-01-16 19:28:14 +00002841 DIE *&Slot = Unit->getDieMapSlotFor(SP.getGV());
Devang Patele6caf012009-01-05 23:21:35 +00002842 if (Slot) continue;
2843
Devang Patelb28de842009-01-17 08:01:33 +00002844 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
Devang Patele6caf012009-01-05 23:21:35 +00002845
Devang Patele6caf012009-01-05 23:21:35 +00002846 //Add to map.
2847 Slot = SubprogramDie;
2848 //Add to context owner.
2849 Unit->getDie()->AddChild(SubprogramDie);
2850 //Expose as global.
Devang Patel7c8a2772009-01-16 19:28:14 +00002851 Unit->AddGlobal(SP.getName(), SubprogramDie);
Devang Patele6caf012009-01-05 23:21:35 +00002852 }
2853 }
2854
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002855public:
2856 //===--------------------------------------------------------------------===//
2857 // Main entry points.
2858 //
Owen Anderson847b99b2008-08-21 00:14:44 +00002859 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00002860 : Dwarf(OS, A, T, "dbg")
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002861 , AbbreviationsSet(InitAbbreviationsSetSize)
2862 , Abbreviations()
2863 , ValuesSet(InitValuesSetSize)
2864 , Values()
2865 , StringPool()
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002866 , SectionMap()
2867 , SectionSourceLines()
2868 , didInitial(false)
2869 , shouldEmit(false)
Devang Patel4d1709e2009-01-08 02:33:41 +00002870 , RootDbgScope(NULL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002871 {
2872 }
2873 virtual ~DwarfDebug() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002874 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2875 delete Values[j];
2876 }
2877
Devang Patel9304b382009-01-06 21:07:30 +00002878 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
2879 /// This is inovked by the target AsmPrinter.
Devang Patel91d27b02009-01-12 23:09:42 +00002880 void SetDebugInfo(MachineModuleInfo *mmi) {
2881
Devang Patel9304b382009-01-06 21:07:30 +00002882 // Create all the compile unit DIEs.
2883 ConstructCompileUnits();
Devang Patel91d27b02009-01-12 23:09:42 +00002884
2885 if (DW_CUs.empty())
2886 return;
2887
2888 MMI = mmi;
2889 shouldEmit = true;
Devang Patel86cfa402009-01-13 23:02:17 +00002890 MMI->setDebugInfoAvailability(true);
Devang Patel9304b382009-01-06 21:07:30 +00002891
2892 // Create DIEs for each of the externally visible global variables.
2893 ConstructGlobalVariableDIEs();
2894
2895 // Create DIEs for each of the externally visible subprograms.
2896 ConstructSubprograms();
2897
2898 // Prime section data.
2899 SectionMap.insert(TAI->getTextSection());
2900
2901 // Print out .file directives to specify files for .loc directives. These
2902 // are printed out early so that they precede any .loc directives.
2903 if (TAI->hasDotLocAndDotFile()) {
2904 for (unsigned i = 1, e = SrcFiles.size(); i <= e; ++i) {
2905 sys::Path FullPath(Directories[SrcFiles[i].getDirectoryID()]);
2906 bool AppendOk = FullPath.appendComponent(SrcFiles[i].getName());
2907 assert(AppendOk && "Could not append filename to directory!");
2908 AppendOk = false;
2909 Asm->EmitFile(i, FullPath.toString());
2910 Asm->EOL();
2911 }
2912 }
2913
2914 // Emit initial sections
2915 EmitInitial();
2916 }
2917
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002918 /// BeginModule - Emit all Dwarf sections that should come prior to the
2919 /// content.
2920 void BeginModule(Module *M) {
2921 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002922 }
2923
2924 /// EndModule - Emit all Dwarf sections that should come after the content.
2925 ///
2926 void EndModule() {
2927 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00002928
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002929 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002930 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002931 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002932 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002933 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00002934
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002935 // End text sections.
2936 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002937 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002938 EmitLabel("section_end", i);
2939 }
2940
2941 // Emit common frame information.
2942 EmitCommonDebugFrame();
2943
2944 // Emit function debug frame information
2945 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2946 E = DebugFrames.end(); I != E; ++I)
2947 EmitFunctionDebugFrame(*I);
2948
2949 // Compute DIE offsets and sizes.
2950 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00002951
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002952 // Emit all the DIEs into a debug info section
2953 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00002954
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002955 // Corresponding abbreviations into a abbrev section.
2956 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00002957
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002958 // Emit source line correspondence into a debug line section.
2959 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00002960
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002961 // Emit info into a debug pubnames section.
2962 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00002963
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002964 // Emit info into a debug str section.
2965 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00002966
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002967 // Emit info into a debug loc section.
2968 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00002969
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002970 // Emit info into a debug aranges section.
2971 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00002972
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002973 // Emit info into a debug ranges section.
2974 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00002975
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002976 // Emit info into a debug macinfo section.
2977 EmitDebugMacInfo();
2978 }
2979
aslc200b112008-08-16 12:57:46 +00002980 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002981 /// emitted immediately after the function entry point.
2982 void BeginFunction(MachineFunction *MF) {
2983 this->MF = MF;
aslc200b112008-08-16 12:57:46 +00002984
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002985 if (!ShouldEmitDwarf()) return;
2986
2987 // Begin accumulating function debug information.
2988 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00002989
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002990 // Assumes in correct section after the entry point.
2991 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00002992
2993 // Emit label for the implicitly defined dbg.stoppoint at the start of
2994 // the function.
Devang Patel35a078f2009-01-12 22:54:42 +00002995 if (!Lines.empty()) {
2996 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth42f91402008-04-03 17:37:43 +00002997 Asm->printLabel(LineInfo.getLabelID());
2998 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002999 }
aslc200b112008-08-16 12:57:46 +00003000
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003001 /// EndFunction - Gather and emit post-function debug information.
3002 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003003 void EndFunction(MachineFunction *MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003004 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00003005
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003006 // Define end label for subprogram.
3007 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003008
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003009 // Get function line info.
Devang Patel35a078f2009-01-12 22:54:42 +00003010 if (!Lines.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003011 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003012 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003013 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patel35a078f2009-01-12 22:54:42 +00003014 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003015 // Append the function info to section info.
3016 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patel35a078f2009-01-12 22:54:42 +00003017 Lines.begin(), Lines.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003018 }
aslc200b112008-08-16 12:57:46 +00003019
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003020 // Construct scopes for subprogram.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003021 if (RootDbgScope)
3022 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003023 else
3024 // FIXME: This is wrong. We are essentially getting past a problem with
3025 // debug information not being able to handle unreachable blocks that have
3026 // debug information in them. In particular, those unreachable blocks that
3027 // have "region end" info in them. That situation results in the "root
3028 // scope" not being created. If that's the case, then emit a "default"
3029 // scope, i.e., one that encompasses the whole function. This isn't
3030 // desirable. And a better way of handling this (and all of the debugging
3031 // information) needs to be explored.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003032 ConstructDefaultDbgScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003033
3034 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3035 MMI->getFrameMoves()));
Devang Patela4162952009-01-12 18:48:36 +00003036
3037 // Clear debug info
3038 if (RootDbgScope) {
3039 delete RootDbgScope;
3040 DbgScopeMap.clear();
3041 RootDbgScope = NULL;
3042 }
3043 Lines.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003044 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003045
3046public:
3047
Devang Patel2da0cc42009-01-15 23:41:32 +00003048 /// ValidDebugInfo - Return true if V represents valid debug info value.
3049 bool ValidDebugInfo(Value *V) {
Devang Patel4d92dded2009-01-16 01:49:46 +00003050
Devang Patel208098b2009-01-19 23:21:49 +00003051 if (!V)
3052 return false;
3053
Devang Patel4d92dded2009-01-16 01:49:46 +00003054 if (!shouldEmit)
3055 return false;
3056
Devang Patel2da0cc42009-01-15 23:41:32 +00003057 GlobalVariable *GV = getGlobalVariable(V);
3058 if (!GV)
3059 return false;
3060
3061 if (GV->getLinkage() != GlobalValue::InternalLinkage
3062 && GV->getLinkage() != GlobalValue::LinkOnceLinkage)
3063 return false;
3064
3065 DIDescriptor DI(GV);
3066 // Check current version. Allow Version6 for now.
3067 unsigned Version = DI.getVersion();
Devang Patelb49710a2009-01-20 19:22:03 +00003068 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
Devang Patel2da0cc42009-01-15 23:41:32 +00003069 return false;
3070
Devang Patel208098b2009-01-19 23:21:49 +00003071 unsigned Tag = DI.getTag();
3072 switch (Tag) {
3073 case DW_TAG_variable:
3074 assert (DIVariable(GV).Verify() && "Invalid DebugInfo value");
3075 break;
3076 case DW_TAG_compile_unit:
3077 assert (DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
3078 break;
3079 case DW_TAG_subprogram:
3080 assert (DISubprogram(GV).Verify() && "Invalid DebugInfo value");
3081 break;
3082 default:
3083 break;
3084 }
3085
Devang Patel2da0cc42009-01-15 23:41:32 +00003086 return true;
3087 }
3088
Devang Patelcb59fd42009-01-12 19:17:34 +00003089 /// RecordSourceLine - Records location information and associates it with a
3090 /// label. Returns a unique label ID used to generate a label and provide
3091 /// correspondence to the source line list.
3092 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
3093 CompileUnit *Unit = DW_CUs[V];
3094 assert (Unit && "Unable to find CompileUnit");
3095 unsigned ID = MMI->NextLabelID();
3096 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
3097 return ID;
3098 }
3099
3100 /// RecordSourceLine - Records location information and associates it with a
3101 /// label. Returns a unique label ID used to generate a label and provide
3102 /// correspondence to the source line list.
3103 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
3104 unsigned ID = MMI->NextLabelID();
3105 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
3106 return ID;
3107 }
3108
3109 unsigned getRecordSourceLineCount() {
3110 return Lines.size();
3111 }
3112
3113 /// RecordSource - Register a source file with debug info. Returns an source
3114 /// ID.
3115 unsigned RecordSource(const std::string &Directory,
3116 const std::string &File) {
3117 unsigned DID = Directories.insert(Directory);
3118 return SrcFiles.insert(SrcFileInfo(DID,File));
3119 }
3120
3121 /// RecordRegionStart - Indicate the start of a region.
3122 ///
3123 unsigned RecordRegionStart(GlobalVariable *V) {
3124 DbgScope *Scope = getOrCreateScope(V);
3125 unsigned ID = MMI->NextLabelID();
3126 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
3127 return ID;
3128 }
3129
3130 /// RecordRegionEnd - Indicate the end of a region.
3131 ///
3132 unsigned RecordRegionEnd(GlobalVariable *V) {
3133 DbgScope *Scope = getOrCreateScope(V);
3134 unsigned ID = MMI->NextLabelID();
3135 Scope->setEndLabelID(ID);
3136 return ID;
3137 }
3138
3139 /// RecordVariable - Indicate the declaration of a local variable.
3140 ///
3141 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Devang Patel2560d922009-01-15 18:25:17 +00003142 DIDescriptor Desc(GV);
3143 DbgScope *Scope = NULL;
3144 if (Desc.getTag() == DW_TAG_variable) {
3145 // GV is a global variable.
3146 DIGlobalVariable DG(GV);
3147 Scope = getOrCreateScope(DG.getContext().getGV());
3148 } else {
3149 // or GV is a local variable.
3150 DIVariable DV(GV);
3151 Scope = getOrCreateScope(DV.getContext().getGV());
3152 }
3153 assert (Scope && "Unable to find variable' scope");
Devang Patel7c8a2772009-01-16 19:28:14 +00003154 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelcb59fd42009-01-12 19:17:34 +00003155 Scope->AddVariable(DV);
3156 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003157};
3158
3159//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003160/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003161///
3162class DwarfException : public Dwarf {
3163
3164private:
3165 struct FunctionEHFrameInfo {
3166 std::string FnName;
3167 unsigned Number;
3168 unsigned PersonalityIndex;
3169 bool hasCalls;
3170 bool hasLandingPads;
3171 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003172 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003173
3174 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3175 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003176 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003177 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003178 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003179 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003180 };
3181
3182 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003183
3184 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3185 /// be emitted.
3186 bool shouldEmitTable;
3187
3188 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3189 /// should be emitted.
3190 bool shouldEmitMoves;
3191
3192 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3193 /// should be emitted.
3194 bool shouldEmitTableModule;
3195
aslc200b112008-08-16 12:57:46 +00003196 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003197 /// should be emitted.
3198 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003199
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003200 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3201 ///
3202 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3203 // Size and sign of stack growth.
3204 int stackGrowth =
3205 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3206 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003207 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003208
3209 // Begin eh frame section.
3210 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003211
3212 if (!TAI->doesRequireNonLocalEHFrameLabel())
3213 O << TAI->getEHGlobalPrefix();
3214 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003215 EmitLabel("section_eh_frame", Index);
3216
3217 // Define base labels.
3218 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003219
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003220 // Define the eh frame length.
3221 EmitDifference("eh_frame_common_end", Index,
3222 "eh_frame_common_begin", Index, true);
3223 Asm->EOL("Length of Common Information Entry");
3224
3225 // EH frame header.
3226 EmitLabel("eh_frame_common_begin", Index);
3227 Asm->EmitInt32((int)0);
3228 Asm->EOL("CIE Identifier Tag");
3229 Asm->EmitInt8(DW_CIE_VERSION);
3230 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003231
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003232 // The personality presence indicates that language specific information
3233 // will show up in the eh frame.
3234 Asm->EmitString(Personality ? "zPLR" : "zR");
3235 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003237 // Round out reader.
3238 Asm->EmitULEB128Bytes(1);
3239 Asm->EOL("CIE Code Alignment Factor");
3240 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003241 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003242 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003243 Asm->EOL("CIE Return Address Column");
3244
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003245 // If there is a personality, we need to indicate the functions location.
3246 if (Personality) {
3247 Asm->EmitULEB128Bytes(7);
3248 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003249
Duncan Sands96144f92008-05-07 19:11:09 +00003250 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003251 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003252 Asm->EOL("Personality (pcrel sdata4 indirect)");
3253 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003254 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003255 Asm->EOL("Personality (pcrel sdata4)");
3256 }
Bill Wendling2d369922007-09-11 17:20:55 +00003257
Duncan Sands96144f92008-05-07 19:11:09 +00003258 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003259 O << TAI->getPersonalityPrefix();
3260 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3261 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003262 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3263 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003264 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003265
Duncan Sands96144f92008-05-07 19:11:09 +00003266 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3267 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003268
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003269 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3270 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003271 } else {
3272 Asm->EmitULEB128Bytes(1);
3273 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003274
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003275 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3276 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003277 }
3278
3279 // Indicate locations of general callee saved registers in frame.
3280 std::vector<MachineMove> Moves;
3281 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003282 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003283
Dale Johannesen388f20f2008-04-30 00:43:29 +00003284 // On Darwin the linker honors the alignment of eh_frame, which means it
3285 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3286 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003287 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003288 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003289 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003290
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003291 Asm->EOL();
3292 }
Duncan Sands96144f92008-05-07 19:11:09 +00003293
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003294 /// EmitEHFrame - Emit function exception frame information.
3295 ///
3296 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003297 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3298
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003299 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3300
3301 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003302 // If the corresponding function is static, this should not be
3303 // externally visible.
Rafael Espindolaa168fc92009-01-15 20:18:42 +00003304 if (linkage != Function::InternalLinkage &&
Devang Patel245446c2009-01-17 08:05:14 +00003305 linkage != Function::PrivateLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003306 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3307 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3308 }
3309
Dale Johannesenf09b5992008-01-10 02:03:30 +00003310 // If corresponding function is weak definition, this should be too.
aslc200b112008-08-16 12:57:46 +00003311 if ((linkage == Function::WeakLinkage ||
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003312 linkage == Function::LinkOnceLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003313 TAI->getWeakDefDirective())
3314 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3315
3316 // If there are no calls then you can't unwind. This may mean we can
3317 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003318 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003319 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003320 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003321 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003322 !UnwindTablesMandatory &&
aslc200b112008-08-16 12:57:46 +00003323 ((linkage != Function::WeakLinkage &&
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003324 linkage != Function::LinkOnceLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003325 !TAI->getWeakDefDirective() ||
3326 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003327 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003328 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003329 // This name has no connection to the function, so it might get
3330 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003331 // dead-stripping unconditionally.
3332 if (const char *UsedDirective = TAI->getUsedDirective())
3333 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003334 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003335 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003336
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003337 // EH frame header.
3338 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3339 "eh_frame_begin", EHFrameInfo.Number, true);
3340 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003341
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003342 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3343
Bill Wendling189bde72008-12-24 08:05:17 +00003344 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3345 PrintRelDirective(true, true);
3346 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3347
3348 if (!TAI->isAbsoluteEHSectionOffsets())
3349 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3350 } else {
3351 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3352 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3353 true, true, false);
3354 }
3355
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003356 Asm->EOL("FDE CIE offset");
3357
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003358 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003359 Asm->EOL("FDE initial location");
3360 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003361 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003362 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003363
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003364 // If there is a personality and landing pads then point to the language
3365 // specific data area in the exception table.
3366 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003367 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003368 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003369
3370 if (EHFrameInfo.hasLandingPads)
3371 EmitReference("exception", EHFrameInfo.Number, true, true);
3372 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003373 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003374 Asm->EOL("Language Specific Data Area");
3375 } else {
3376 Asm->EmitULEB128Bytes(0);
3377 Asm->EOL("Augmentation size");
3378 }
Duncan Sands96144f92008-05-07 19:11:09 +00003379
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003380 // Indicate locations of function specific callee saved registers in
3381 // frame.
Devang Patelb28de842009-01-17 08:01:33 +00003382 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00003383 true);
aslc200b112008-08-16 12:57:46 +00003384
Dale Johannesen388f20f2008-04-30 00:43:29 +00003385 // On Darwin the linker honors the alignment of eh_frame, which means it
3386 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3387 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003388 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003389 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003390 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003391
3392 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003393 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003394 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003395 // that depends on unused functions (calling undefined externals) being
3396 // dead-stripped to link correctly. Yes, there really is.
3397 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3398 if (const char *UsedDirective = TAI->getUsedDirective())
3399 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3400 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003401 }
3402
Duncan Sands241a0c92007-09-05 11:27:52 +00003403 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003404 ///
3405 /// The general organization of the table is complex, but the basic concepts
3406 /// are easy. First there is a header which describes the location and
3407 /// organization of the three components that follow.
3408 /// 1. The landing pad site information describes the range of code covered
3409 /// by the try. In our case it's an accumulation of the ranges covered
3410 /// by the invokes in the try. There is also a reference to the landing
3411 /// pad that handles the exception once processed. Finally an index into
3412 /// the actions table.
3413 /// 2. The action table, in our case, is composed of pairs of type ids
3414 /// and next action offset. Starting with the action index from the
3415 /// landing pad site, each type Id is checked for a match to the current
3416 /// exception. If it matches then the exception and type id are passed
3417 /// on to the landing pad. Otherwise the next action is looked up. This
3418 /// chain is terminated with a next action of zero. If no type id is
3419 /// found the the frame is unwound and handling continues.
3420 /// 3. Type id table contains references to all the C++ typeinfo for all
3421 /// catches in the function. This tables is reversed indexed base 1.
3422
3423 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3424 static unsigned SharedTypeIds(const LandingPadInfo *L,
3425 const LandingPadInfo *R) {
3426 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3427 unsigned LSize = LIds.size(), RSize = RIds.size();
3428 unsigned MinSize = LSize < RSize ? LSize : RSize;
3429 unsigned Count = 0;
3430
3431 for (; Count != MinSize; ++Count)
3432 if (LIds[Count] != RIds[Count])
3433 return Count;
3434
3435 return Count;
3436 }
3437
3438 /// PadLT - Order landing pads lexicographically by type id.
3439 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3440 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3441 unsigned LSize = LIds.size(), RSize = RIds.size();
3442 unsigned MinSize = LSize < RSize ? LSize : RSize;
3443
3444 for (unsigned i = 0; i != MinSize; ++i)
3445 if (LIds[i] != RIds[i])
3446 return LIds[i] < RIds[i];
3447
3448 return LSize < RSize;
3449 }
3450
3451 struct KeyInfo {
3452 static inline unsigned getEmptyKey() { return -1U; }
3453 static inline unsigned getTombstoneKey() { return -2U; }
3454 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00003455 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003456 static bool isPod() { return true; }
3457 };
3458
Duncan Sands241a0c92007-09-05 11:27:52 +00003459 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003460 struct ActionEntry {
3461 int ValueForTypeID; // The value to write - may not be equal to the type id.
3462 int NextAction;
3463 struct ActionEntry *Previous;
3464 };
3465
Duncan Sands241a0c92007-09-05 11:27:52 +00003466 /// PadRange - Structure holding a try-range and the associated landing pad.
3467 struct PadRange {
3468 // The index of the landing pad.
3469 unsigned PadIndex;
3470 // The index of the begin and end labels in the landing pad's label lists.
3471 unsigned RangeIndex;
3472 };
3473
3474 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3475
3476 /// CallSiteEntry - Structure describing an entry in the call-site table.
3477 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00003478 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003479 unsigned BeginLabel; // zero indicates the start of the function.
3480 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003481 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003482 unsigned PadLabel; // zero indicates that there is no landing pad.
3483 unsigned Action;
3484 };
3485
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003486 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003487 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3488 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3489 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3490 if (PadInfos.empty()) return;
3491
3492 // Sort the landing pads in order of their type ids. This is used to fold
3493 // duplicate actions.
3494 SmallVector<const LandingPadInfo *, 64> LandingPads;
3495 LandingPads.reserve(PadInfos.size());
3496 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3497 LandingPads.push_back(&PadInfos[i]);
3498 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3499
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003500 // Negative type ids index into FilterIds, positive type ids index into
3501 // TypeInfos. The value written for a positive type id is just the type
3502 // id itself. For a negative type id, however, the value written is the
3503 // (negative) byte offset of the corresponding FilterIds entry. The byte
3504 // offset is usually equal to the type id, because the FilterIds entries
3505 // are written using a variable width encoding which outputs one byte per
3506 // entry as long as the value written is not too large, but can differ.
3507 // This kind of complication does not occur for positive type ids because
3508 // type infos are output using a fixed width encoding.
3509 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3510 SmallVector<int, 16> FilterOffsets;
3511 FilterOffsets.reserve(FilterIds.size());
3512 int Offset = -1;
3513 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3514 E = FilterIds.end(); I != E; ++I) {
3515 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00003516 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003517 }
3518
Duncan Sands241a0c92007-09-05 11:27:52 +00003519 // Compute the actions table and gather the first action index for each
3520 // landing pad site.
3521 SmallVector<ActionEntry, 32> Actions;
3522 SmallVector<unsigned, 64> FirstActions;
3523 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003524
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003525 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00003526 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003527 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3528 const LandingPadInfo *LP = LandingPads[i];
3529 const std::vector<int> &TypeIds = LP->TypeIds;
3530 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3531 unsigned SizeSiteActions = 0;
3532
3533 if (NumShared < TypeIds.size()) {
3534 unsigned SizeAction = 0;
3535 ActionEntry *PrevAction = 0;
3536
3537 if (NumShared) {
3538 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3539 assert(Actions.size());
3540 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00003541 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3542 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003543 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00003544 SizeAction -=
3545 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003546 SizeAction += -PrevAction->NextAction;
3547 PrevAction = PrevAction->Previous;
3548 }
3549 }
3550
3551 // Compute the actions.
3552 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3553 int TypeID = TypeIds[I];
3554 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3555 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00003556 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003557
3558 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00003559 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003560 SizeSiteActions += SizeAction;
3561
3562 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
3563 Actions.push_back(Action);
3564
3565 PrevAction = &Actions.back();
3566 }
3567
3568 // Record the first action of the landing pad site.
3569 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3570 } // else identical - re-use previous FirstAction
3571
3572 FirstActions.push_back(FirstAction);
3573
3574 // Compute this sites contribution to size.
3575 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003576 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003577
Duncan Sands4ff179f2007-12-19 07:36:31 +00003578 // Compute the call-site table. The entry for an invoke has a try-range
3579 // containing the call, a non-zero landing pad and an appropriate action.
3580 // The entry for an ordinary call has a try-range containing the call and
3581 // zero for the landing pad and the action. Calls marked 'nounwind' have
3582 // no entry and must not be contained in the try-range of any entry - they
3583 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003584 SmallVector<CallSiteEntry, 64> CallSites;
3585
3586 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003587 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3588 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3589 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00003590 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3591 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003592 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003593 unsigned BeginLabel = LandingPad->BeginLabels[j];
3594 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3595 PadRange P = { i, j };
3596 PadMap[BeginLabel] = P;
3597 }
3598 }
3599
Duncan Sands4ff179f2007-12-19 07:36:31 +00003600 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00003601 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003602
3603 // Whether there is a potentially throwing instruction (currently this means
3604 // an ordinary call) between the end of the previous try-range and now.
3605 bool SawPotentiallyThrowing = false;
3606
3607 // Whether the last callsite entry was for an invoke.
3608 bool PreviousIsInvoke = false;
3609
Duncan Sands4ff179f2007-12-19 07:36:31 +00003610 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003611 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3612 I != E; ++I) {
3613 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3614 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00003615 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00003616 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00003617 continue;
3618 }
3619
Chris Lattnerda4cff12007-12-30 20:50:28 +00003620 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00003621 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00003622
Duncan Sands4ff179f2007-12-19 07:36:31 +00003623 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00003624 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00003625 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003626
Duncan Sands4ff179f2007-12-19 07:36:31 +00003627 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00003628 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00003629 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00003630 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00003631 continue;
3632
3633 PadRange P = L->second;
3634 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3635
3636 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3637 "Inconsistent landing pad map!");
3638
3639 // If some instruction between the previous try-range and this one may
3640 // throw, create a call-site entry with no landing pad for the region
3641 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003642 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003643 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3644 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00003645 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003646 }
3647
3648 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003649 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00003650
Duncan Sands4ff179f2007-12-19 07:36:31 +00003651 if (LandingPad->LandingPadLabel) {
3652 // This try-range is for an invoke.
3653 CallSiteEntry Site = {BeginLabel, LastLabel,
3654 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00003655
Duncan Sands4ff179f2007-12-19 07:36:31 +00003656 // Try to merge with the previous call-site.
3657 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00003658 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00003659 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3660 // Extend the range of the previous entry.
3661 Prev.EndLabel = Site.EndLabel;
3662 continue;
3663 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003664 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003665
Duncan Sands4ff179f2007-12-19 07:36:31 +00003666 // Otherwise, create a new call-site.
3667 CallSites.push_back(Site);
3668 PreviousIsInvoke = true;
3669 } else {
3670 // Create a gap.
3671 PreviousIsInvoke = false;
3672 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003673 }
3674 }
3675 // If some instruction between the previous try-range and the end of the
3676 // function may throw, create a call-site entry with no landing pad for the
3677 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003678 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003679 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3680 CallSites.push_back(Site);
3681 }
3682
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003683 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00003684
3685 // Call sites.
3686 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3687 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3688 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3689 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3690 SiteLengthSize +
3691 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00003692 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00003693 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00003694
Duncan Sands96144f92008-05-07 19:11:09 +00003695 // Type infos.
3696 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3697 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003698
3699 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00003700 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003701 SizeSites + SizeActions + SizeTypes;
3702
3703 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3704 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00003705 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003706 TypeOffset;
3707
3708 unsigned SizeAlign = (4 - TotalSize) & 3;
3709
3710 // Begin the exception table.
3711 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00003712 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00003713 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003714 for (unsigned i = 0; i != SizeAlign; ++i) {
3715 Asm->EmitInt8(0);
3716 Asm->EOL("Padding");
3717 }
3718 EmitLabel("exception", SubprogramCount);
3719
3720 // Emit the header.
3721 Asm->EmitInt8(DW_EH_PE_omit);
3722 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3723 Asm->EmitInt8(DW_EH_PE_absptr);
3724 Asm->EOL("TType format (DW_EH_PE_absptr)");
3725 Asm->EmitULEB128Bytes(TypeOffset);
3726 Asm->EOL("TType base offset");
3727 Asm->EmitInt8(DW_EH_PE_udata4);
3728 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3729 Asm->EmitULEB128Bytes(SizeSites);
3730 Asm->EOL("Call-site table length");
3731
Duncan Sands241a0c92007-09-05 11:27:52 +00003732 // Emit the landing pad site information.
3733 for (unsigned i = 0; i < CallSites.size(); ++i) {
3734 CallSiteEntry &S = CallSites[i];
3735 const char *BeginTag;
3736 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003737
Duncan Sands241a0c92007-09-05 11:27:52 +00003738 if (!S.BeginLabel) {
3739 BeginTag = "eh_func_begin";
3740 BeginNumber = SubprogramCount;
3741 } else {
3742 BeginTag = "label";
3743 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003744 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003745
Duncan Sands241a0c92007-09-05 11:27:52 +00003746 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003747 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003748 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003749
Duncan Sands241a0c92007-09-05 11:27:52 +00003750 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00003751 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00003752 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003753 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00003754 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003755 }
3756 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003757
Duncan Sands96144f92008-05-07 19:11:09 +00003758 if (!S.PadLabel)
3759 Asm->EmitInt32(0);
3760 else
Duncan Sands241a0c92007-09-05 11:27:52 +00003761 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003762 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003763 Asm->EOL("Landing pad");
3764
3765 Asm->EmitULEB128Bytes(S.Action);
3766 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003767 }
3768
3769 // Emit the actions.
3770 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3771 ActionEntry &Action = Actions[I];
3772
3773 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
3774 Asm->EOL("TypeInfo index");
3775 Asm->EmitSLEB128Bytes(Action.NextAction);
3776 Asm->EOL("Next action");
3777 }
3778
3779 // Emit the type ids.
3780 for (unsigned M = TypeInfos.size(); M; --M) {
3781 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00003782
3783 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003784
3785 if (GV)
3786 O << Asm->getGlobalLinkName(GV);
3787 else
3788 O << "0";
Duncan Sands241a0c92007-09-05 11:27:52 +00003789
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003790 Asm->EOL("TypeInfo");
3791 }
3792
3793 // Emit the filter typeids.
3794 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3795 unsigned TypeID = FilterIds[j];
3796 Asm->EmitULEB128Bytes(TypeID);
3797 Asm->EOL("Filter TypeInfo index");
3798 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003799
Evan Cheng7e7d1942008-02-29 19:36:59 +00003800 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003801 }
3802
3803public:
3804 //===--------------------------------------------------------------------===//
3805 // Main entry points.
3806 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003807 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00003808 : Dwarf(OS, A, T, "eh")
Dale Johannesen85535762008-04-02 00:25:04 +00003809 , shouldEmitTable(false)
3810 , shouldEmitMoves(false)
3811 , shouldEmitTableModule(false)
3812 , shouldEmitMovesModule(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003813 {}
aslc200b112008-08-16 12:57:46 +00003814
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003815 virtual ~DwarfException() {}
3816
3817 /// SetModuleInfo - Set machine module information when it's known that pass
3818 /// manager has created it. Set by the target AsmPrinter.
3819 void SetModuleInfo(MachineModuleInfo *mmi) {
3820 MMI = mmi;
3821 }
3822
3823 /// BeginModule - Emit all exception information that should come prior to the
3824 /// content.
3825 void BeginModule(Module *M) {
3826 this->M = M;
3827 }
3828
3829 /// EndModule - Emit all exception information that should come after the
3830 /// content.
3831 void EndModule() {
Dale Johannesen85535762008-04-02 00:25:04 +00003832 if (shouldEmitMovesModule || shouldEmitTableModule) {
3833 const std::vector<Function *> Personalities = MMI->getPersonalities();
3834 for (unsigned i =0; i < Personalities.size(); ++i)
3835 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003836
Dale Johannesen85535762008-04-02 00:25:04 +00003837 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3838 E = EHFrames.end(); I != E; ++I)
3839 EmitEHFrame(*I);
3840 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003841 }
3842
aslc200b112008-08-16 12:57:46 +00003843 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003844 /// emitted immediately after the function entry point.
3845 void BeginFunction(MachineFunction *MF) {
3846 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00003847 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen62f0a6d2008-04-02 17:04:45 +00003848 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00003849
3850 // Map all labels and get rid of any dead landing pads.
3851 MMI->TidyLandingPads();
3852 // If any landing pads survive, we need an EH table.
3853 if (MMI->getLandingPads().size())
3854 shouldEmitTable = true;
3855
3856 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00003857 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00003858 shouldEmitMoves = true;
3859
3860 if (shouldEmitMoves || shouldEmitTable)
3861 // Assumes in correct section after the entry point.
3862 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003863 }
Dale Johannesen85535762008-04-02 00:25:04 +00003864 shouldEmitTableModule |= shouldEmitTable;
3865 shouldEmitMovesModule |= shouldEmitMoves;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003866 }
3867
3868 /// EndFunction - Gather and emit post-function exception information.
3869 ///
3870 void EndFunction() {
Dale Johannesen85535762008-04-02 00:25:04 +00003871 if (shouldEmitMoves || shouldEmitTable) {
3872 EmitLabel("eh_func_end", SubprogramCount);
3873 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003874
Dale Johannesen85535762008-04-02 00:25:04 +00003875 // Save EH frame information
3876 EHFrames.
3877 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingef9211a2007-09-18 01:47:22 +00003878 SubprogramCount,
3879 MMI->getPersonalityIndex(),
3880 MF->getFrameInfo()->hasCalls(),
3881 !MMI->getLandingPads().empty(),
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003882 MMI->getFrameMoves(),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003883 MF->getFunction()));
Dale Johannesen85535762008-04-02 00:25:04 +00003884 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003885 }
3886};
3887
3888} // End of namespace llvm
3889
3890//===----------------------------------------------------------------------===//
3891
3892/// Emit - Print the abbreviation using the specified Dwarf writer.
3893///
3894void DIEAbbrev::Emit(const DwarfDebug &DD) const {
3895 // Emit its Dwarf tag type.
3896 DD.getAsm()->EmitULEB128Bytes(Tag);
3897 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00003898
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003899 // Emit whether it has children DIEs.
3900 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3901 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00003902
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003903 // For each attribute description.
3904 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3905 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00003906
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003907 // Emit attribute type.
3908 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3909 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00003910
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003911 // Emit form type.
3912 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3913 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
3914 }
3915
3916 // Mark end of abbreviation.
3917 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3918 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
3919}
3920
3921#ifndef NDEBUG
3922void DIEAbbrev::print(std::ostream &O) {
3923 O << "Abbreviation @"
3924 << std::hex << (intptr_t)this << std::dec
3925 << " "
3926 << TagString(Tag)
3927 << " "
3928 << ChildrenString(ChildrenFlag)
3929 << "\n";
aslc200b112008-08-16 12:57:46 +00003930
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003931 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3932 O << " "
3933 << AttributeString(Data[i].getAttribute())
3934 << " "
3935 << FormEncodingString(Data[i].getForm())
3936 << "\n";
3937 }
3938}
3939void DIEAbbrev::dump() { print(cerr); }
3940#endif
3941
3942//===----------------------------------------------------------------------===//
3943
3944#ifndef NDEBUG
3945void DIEValue::dump() {
3946 print(cerr);
3947}
3948#endif
3949
3950//===----------------------------------------------------------------------===//
3951
3952/// EmitValue - Emit integer of appropriate size.
3953///
3954void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
3955 switch (Form) {
3956 case DW_FORM_flag: // Fall thru
3957 case DW_FORM_ref1: // Fall thru
3958 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
3959 case DW_FORM_ref2: // Fall thru
3960 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
3961 case DW_FORM_ref4: // Fall thru
3962 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
3963 case DW_FORM_ref8: // Fall thru
3964 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
3965 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
3966 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
3967 default: assert(0 && "DIE Value form not supported yet"); break;
3968 }
3969}
3970
3971/// SizeOf - Determine size of integer value in bytes.
3972///
3973unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3974 switch (Form) {
3975 case DW_FORM_flag: // Fall thru
3976 case DW_FORM_ref1: // Fall thru
3977 case DW_FORM_data1: return sizeof(int8_t);
3978 case DW_FORM_ref2: // Fall thru
3979 case DW_FORM_data2: return sizeof(int16_t);
3980 case DW_FORM_ref4: // Fall thru
3981 case DW_FORM_data4: return sizeof(int32_t);
3982 case DW_FORM_ref8: // Fall thru
3983 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00003984 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
3985 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003986 default: assert(0 && "DIE Value form not supported yet"); break;
3987 }
3988 return 0;
3989}
3990
3991//===----------------------------------------------------------------------===//
3992
3993/// EmitValue - Emit string value.
3994///
3995void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
3996 DD.getAsm()->EmitString(String);
3997}
3998
3999//===----------------------------------------------------------------------===//
4000
4001/// EmitValue - Emit label value.
4002///
4003void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004004 bool IsSmall = Form == DW_FORM_data4;
4005 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004006}
4007
4008/// SizeOf - Determine size of label value in bytes.
4009///
4010unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004011 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004012 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004013}
4014
4015//===----------------------------------------------------------------------===//
4016
4017/// EmitValue - Emit label value.
4018///
4019void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004020 bool IsSmall = Form == DW_FORM_data4;
4021 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004022}
4023
4024/// SizeOf - Determine size of label value in bytes.
4025///
4026unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004027 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004028 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004029}
aslc200b112008-08-16 12:57:46 +00004030
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004031//===----------------------------------------------------------------------===//
4032
4033/// EmitValue - Emit delta value.
4034///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004035void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4036 bool IsSmall = Form == DW_FORM_data4;
4037 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4038 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4039}
4040
4041/// SizeOf - Determine size of delta value in bytes.
4042///
4043unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4044 if (Form == DW_FORM_data4) return 4;
4045 return DD.getTargetData()->getPointerSize();
4046}
aslc200b112008-08-16 12:57:46 +00004047
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004048//===----------------------------------------------------------------------===//
4049
4050/// EmitValue - Emit delta value.
4051///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004052void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4053 bool IsSmall = Form == DW_FORM_data4;
4054 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4055}
4056
4057/// SizeOf - Determine size of delta value in bytes.
4058///
4059unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4060 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004061 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004062}
4063
4064//===----------------------------------------------------------------------===//
4065
4066/// EmitValue - Emit debug information entry offset.
4067///
4068void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4069 DD.getAsm()->EmitInt32(Entry->getOffset());
4070}
aslc200b112008-08-16 12:57:46 +00004071
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004072//===----------------------------------------------------------------------===//
4073
4074/// ComputeSize - calculate the size of the block.
4075///
4076unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4077 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004078 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004079
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004080 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4081 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4082 }
4083 }
4084 return Size;
4085}
4086
4087/// EmitValue - Emit block data.
4088///
4089void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4090 switch (Form) {
4091 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4092 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4093 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4094 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4095 default: assert(0 && "Improper form for block"); break;
4096 }
aslc200b112008-08-16 12:57:46 +00004097
Owen Anderson88dd6232008-06-24 21:44:59 +00004098 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004099
4100 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4101 DD.getAsm()->EOL();
4102 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4103 }
4104}
4105
4106/// SizeOf - Determine size of block data in bytes.
4107///
4108unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4109 switch (Form) {
4110 case DW_FORM_block1: return Size + sizeof(int8_t);
4111 case DW_FORM_block2: return Size + sizeof(int16_t);
4112 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004113 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004114 default: assert(0 && "Improper form for block"); break;
4115 }
4116 return 0;
4117}
4118
4119//===----------------------------------------------------------------------===//
4120/// DIE Implementation
4121
4122DIE::~DIE() {
4123 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4124 delete Children[i];
4125}
aslc200b112008-08-16 12:57:46 +00004126
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004127/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4128///
4129void DIE::AddSiblingOffset() {
4130 DIEInteger *DI = new DIEInteger(0);
4131 Values.insert(Values.begin(), DI);
4132 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4133}
4134
4135/// Profile - Used to gather unique data for the value folding set.
4136///
4137void DIE::Profile(FoldingSetNodeID &ID) {
4138 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004139
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004140 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4141 ID.AddPointer(Children[i]);
4142
4143 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4144 ID.AddPointer(Values[j]);
4145}
4146
4147#ifndef NDEBUG
4148void DIE::print(std::ostream &O, unsigned IncIndent) {
4149 static unsigned IndentCount = 0;
4150 IndentCount += IncIndent;
4151 const std::string Indent(IndentCount, ' ');
4152 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004153
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004154 if (!isBlock) {
4155 O << Indent
4156 << "Die: "
4157 << "0x" << std::hex << (intptr_t)this << std::dec
4158 << ", Offset: " << Offset
4159 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004160 << "\n";
4161
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004162 O << Indent
4163 << TagString(Abbrev.getTag())
4164 << " "
4165 << ChildrenString(Abbrev.getChildrenFlag());
4166 } else {
4167 O << "Size: " << Size;
4168 }
4169 O << "\n";
4170
Owen Anderson88dd6232008-06-24 21:44:59 +00004171 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004172
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004173 IndentCount += 2;
4174 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4175 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004176
4177 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004178 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004179 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004180 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004181
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004182 O << " "
4183 << FormEncodingString(Data[i].getForm())
4184 << " ";
4185 Values[i]->print(O);
4186 O << "\n";
4187 }
4188 IndentCount -= 2;
4189
4190 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4191 Children[j]->print(O, 4);
4192 }
aslc200b112008-08-16 12:57:46 +00004193
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004194 if (!isBlock) O << "\n";
4195 IndentCount -= IncIndent;
4196}
4197
4198void DIE::dump() {
4199 print(cerr);
4200}
4201#endif
4202
4203//===----------------------------------------------------------------------===//
4204/// DwarfWriter Implementation
4205///
4206
Devang Patelaa1e8432009-01-08 23:40:34 +00004207DwarfWriter::DwarfWriter() : ImmutablePass(&ID), DD(NULL), DE(NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004208}
4209
4210DwarfWriter::~DwarfWriter() {
4211 delete DE;
4212 delete DD;
4213}
4214
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004215/// BeginModule - Emit all Dwarf sections that should come prior to the
4216/// content.
Devang Patelaa1e8432009-01-08 23:40:34 +00004217void DwarfWriter::BeginModule(Module *M,
4218 MachineModuleInfo *MMI,
4219 raw_ostream &OS, AsmPrinter *A,
4220 const TargetAsmInfo *T) {
4221 DE = new DwarfException(OS, A, T);
4222 DD = new DwarfDebug(OS, A, T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004223 DE->BeginModule(M);
4224 DD->BeginModule(M);
Devang Patel6ccd57e2009-01-13 00:20:51 +00004225 DD->SetDebugInfo(MMI);
Devang Patelaa1e8432009-01-08 23:40:34 +00004226 DE->SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004227}
4228
4229/// EndModule - Emit all Dwarf sections that should come after the content.
4230///
4231void DwarfWriter::EndModule() {
4232 DE->EndModule();
4233 DD->EndModule();
4234}
4235
aslc200b112008-08-16 12:57:46 +00004236/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004237/// emitted immediately after the function entry point.
4238void DwarfWriter::BeginFunction(MachineFunction *MF) {
4239 DE->BeginFunction(MF);
4240 DD->BeginFunction(MF);
4241}
4242
4243/// EndFunction - Gather and emit post-function debug information.
4244///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004245void DwarfWriter::EndFunction(MachineFunction *MF) {
4246 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004247 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004248
Bill Wendling5b4796a2008-07-22 00:53:37 +00004249 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004250 // Clear function debug information.
4251 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004252}
Devang Patelcb59fd42009-01-12 19:17:34 +00004253
Devang Patel2da0cc42009-01-15 23:41:32 +00004254/// ValidDebugInfo - Return true if V represents valid debug info value.
4255bool DwarfWriter::ValidDebugInfo(Value *V) {
Devang Patel943af622009-01-16 02:15:14 +00004256 return DD && DD->ValidDebugInfo(V);
Devang Patel2da0cc42009-01-15 23:41:32 +00004257}
4258
Devang Patelcb59fd42009-01-12 19:17:34 +00004259/// RecordSourceLine - Records location information and associates it with a
4260/// label. Returns a unique label ID used to generate a label and provide
4261/// correspondence to the source line list.
4262unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4263 unsigned Src) {
4264 return DD->RecordSourceLine(Line, Col, Src);
4265}
4266
4267/// RecordSource - Register a source file with debug info. Returns an source
4268/// ID.
4269unsigned DwarfWriter::RecordSource(const std::string &Dir,
4270 const std::string &File) {
4271 return DD->RecordSource(Dir, File);
4272}
4273
4274/// RecordRegionStart - Indicate the start of a region.
4275unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
4276 return DD->RecordRegionStart(V);
4277}
4278
4279/// RecordRegionEnd - Indicate the end of a region.
4280unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
4281 return DD->RecordRegionEnd(V);
4282}
4283
4284/// getRecordSourceLineCount - Count source lines.
4285unsigned DwarfWriter::getRecordSourceLineCount() {
4286 return DD->getRecordSourceLineCount();
4287}
Devang Patel70190872009-01-13 21:25:00 +00004288
Devang Patelfe359e72009-01-13 21:44:10 +00004289/// RecordVariable - Indicate the declaration of a local variable.
4290///
4291void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
4292 DD->RecordVariable(GV, FrameIndex);
4293}
Devang Patel42f6bed2009-01-13 23:54:55 +00004294