blob: 6a29121acc845bdff1d1ef0ac350daa91eb47d0a [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf info into asm files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/DwarfWriter.h"
15
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/FoldingSet.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/ADT/UniqueVector.h"
20#include "llvm/Module.h"
Devang Patelb3907da2009-01-05 23:03:32 +000021#include "llvm/DerivedTypes.h"
Devang Patel2da0cc42009-01-15 23:41:32 +000022#include "llvm/Constants.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CodeGen/AsmPrinter.h"
24#include "llvm/CodeGen/MachineModuleInfo.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineLocation.h"
Devang Patelfc187162009-01-05 17:57:47 +000027#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/Dwarf.h"
30#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/DataTypes.h"
32#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000033#include "llvm/Support/raw_ostream.h"
Dan Gohman80bbde72007-09-24 21:32:18 +000034#include "llvm/System/Path.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000036#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Target/TargetData.h"
38#include "llvm/Target/TargetFrameInfo.h"
39#include "llvm/Target/TargetInstrInfo.h"
40#include "llvm/Target/TargetMachine.h"
41#include "llvm/Target/TargetOptions.h"
42#include <ostream>
43#include <string>
44using namespace llvm;
45using namespace llvm::dwarf;
46
Devang Patelaa1e8432009-01-08 23:40:34 +000047static RegisterPass<DwarfWriter>
48X("dwarfwriter", "DWARF Information Writer");
49char DwarfWriter::ID = 0;
50
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051namespace llvm {
aslc200b112008-08-16 12:57:46 +000052
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053//===----------------------------------------------------------------------===//
54
55/// Configuration values for initial hash set sizes (log2).
56///
Bill Wendling824a8bf2009-02-03 21:17:20 +000057static const unsigned InitDiesSetSize = 9; // log2(512)
58static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
59static const unsigned InitValuesSetSize = 9; // log2(512)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060
61//===----------------------------------------------------------------------===//
62/// Forward declarations.
63///
64class DIE;
65class DIEValue;
66
67//===----------------------------------------------------------------------===//
Devang Patelb3907da2009-01-05 23:03:32 +000068/// Utility routines.
69///
Devang Patelb28de842009-01-17 08:01:33 +000070/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
71/// specified value in their initializer somewhere.
Devang Patelb3907da2009-01-05 23:03:32 +000072static void
73getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
Devang Patelb28de842009-01-17 08:01:33 +000074 // Scan though value users.
Devang Patelb3907da2009-01-05 23:03:32 +000075 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
76 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
Devang Patelb28de842009-01-17 08:01:33 +000077 // If the user is a GlobalVariable then add to result.
Devang Patelb3907da2009-01-05 23:03:32 +000078 Result.push_back(GV);
79 } else if (Constant *C = dyn_cast<Constant>(*I)) {
Devang Patelb28de842009-01-17 08:01:33 +000080 // If the user is a constant variable then scan its users.
Devang Patelb3907da2009-01-05 23:03:32 +000081 getGlobalVariablesUsing(C, Result);
82 }
83 }
84}
85
Devang Patelb28de842009-01-17 08:01:33 +000086/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
87/// named GlobalVariable.
Devang Patelb3907da2009-01-05 23:03:32 +000088static void
89getGlobalVariablesUsing(Module &M, const std::string &RootName,
90 std::vector<GlobalVariable*> &Result) {
91 std::vector<const Type*> FieldTypes;
92 FieldTypes.push_back(Type::Int32Ty);
93 FieldTypes.push_back(Type::Int32Ty);
94
Devang Patelb28de842009-01-17 08:01:33 +000095 // Get the GlobalVariable root.
Devang Patelb3907da2009-01-05 23:03:32 +000096 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
97 StructType::get(FieldTypes));
98
Devang Patelb28de842009-01-17 08:01:33 +000099 // If present and linkonce then scan for users.
Devang Patelb3907da2009-01-05 23:03:32 +0000100 if (UseRoot && UseRoot->hasLinkOnceLinkage())
101 getGlobalVariablesUsing(UseRoot, Result);
102}
103
Devang Patel2da0cc42009-01-15 23:41:32 +0000104/// getGlobalVariable - Return either a direct or cast Global value.
105///
106static GlobalVariable *getGlobalVariable(Value *V) {
107 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
108 return GV;
109 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
110 if (CE->getOpcode() == Instruction::BitCast) {
111 return dyn_cast<GlobalVariable>(CE->getOperand(0));
112 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
113 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
114 if (!CE->getOperand(i)->isNullValue())
115 return NULL;
116 }
117 return dyn_cast<GlobalVariable>(CE->getOperand(0));
118 }
119 }
120 return NULL;
121}
122
Devang Patelb3907da2009-01-05 23:03:32 +0000123//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124/// DWLabel - Labels are used to track locations in the assembler file.
aslc200b112008-08-16 12:57:46 +0000125/// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
126/// where the tag is a category of label (Ex. location) and number is a value
Reid Spencer37c7cea2007-08-05 20:06:04 +0000127/// unique in that category.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128class DWLabel {
129public:
130 /// Tag - Label category tag. Should always be a staticly declared C string.
131 ///
132 const char *Tag;
aslc200b112008-08-16 12:57:46 +0000133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 /// Number - Value to make label unique.
135 ///
136 unsigned Number;
137
138 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
aslc200b112008-08-16 12:57:46 +0000139
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 void Profile(FoldingSetNodeID &ID) const {
141 ID.AddString(std::string(Tag));
142 ID.AddInteger(Number);
143 }
aslc200b112008-08-16 12:57:46 +0000144
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145#ifndef NDEBUG
146 void print(std::ostream *O) const {
147 if (O) print(*O);
148 }
149 void print(std::ostream &O) const {
150 O << "." << Tag;
151 if (Number) O << Number;
152 }
153#endif
154};
155
156//===----------------------------------------------------------------------===//
157/// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
158/// Dwarf abbreviation.
159class DIEAbbrevData {
160private:
161 /// Attribute - Dwarf attribute code.
162 ///
163 unsigned Attribute;
aslc200b112008-08-16 12:57:46 +0000164
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 /// Form - Dwarf form code.
aslc200b112008-08-16 12:57:46 +0000166 ///
167 unsigned Form;
168
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169public:
170 DIEAbbrevData(unsigned A, unsigned F)
171 : Attribute(A)
172 , Form(F)
173 {}
aslc200b112008-08-16 12:57:46 +0000174
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 // Accessors.
176 unsigned getAttribute() const { return Attribute; }
177 unsigned getForm() const { return Form; }
178
179 /// Profile - Used to gather unique data for the abbreviation folding set.
180 ///
181 void Profile(FoldingSetNodeID &ID)const {
182 ID.AddInteger(Attribute);
183 ID.AddInteger(Form);
184 }
185};
186
187//===----------------------------------------------------------------------===//
188/// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
189/// information object.
190class DIEAbbrev : public FoldingSetNode {
191private:
192 /// Tag - Dwarf tag code.
193 ///
194 unsigned Tag;
aslc200b112008-08-16 12:57:46 +0000195
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 /// Unique number for node.
197 ///
198 unsigned Number;
199
200 /// ChildrenFlag - Dwarf children flag.
201 ///
202 unsigned ChildrenFlag;
203
204 /// Data - Raw data bytes for abbreviation.
205 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000206 SmallVector<DIEAbbrevData, 8> Data;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207
208public:
209
210 DIEAbbrev(unsigned T, unsigned C)
211 : Tag(T)
212 , ChildrenFlag(C)
213 , Data()
214 {}
215 ~DIEAbbrev() {}
aslc200b112008-08-16 12:57:46 +0000216
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 // Accessors.
218 unsigned getTag() const { return Tag; }
219 unsigned getNumber() const { return Number; }
220 unsigned getChildrenFlag() const { return ChildrenFlag; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000221 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 void setTag(unsigned T) { Tag = T; }
223 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
224 void setNumber(unsigned N) { Number = N; }
aslc200b112008-08-16 12:57:46 +0000225
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 /// AddAttribute - Adds another set of attribute information to the
227 /// abbreviation.
228 void AddAttribute(unsigned Attribute, unsigned Form) {
229 Data.push_back(DIEAbbrevData(Attribute, Form));
230 }
aslc200b112008-08-16 12:57:46 +0000231
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 /// AddFirstAttribute - Adds a set of attribute information to the front
233 /// of the abbreviation.
234 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
235 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
236 }
aslc200b112008-08-16 12:57:46 +0000237
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 /// Profile - Used to gather unique data for the abbreviation folding set.
239 ///
240 void Profile(FoldingSetNodeID &ID) {
241 ID.AddInteger(Tag);
242 ID.AddInteger(ChildrenFlag);
aslc200b112008-08-16 12:57:46 +0000243
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 // For each attribute description.
245 for (unsigned i = 0, N = Data.size(); i < N; ++i)
246 Data[i].Profile(ID);
247 }
aslc200b112008-08-16 12:57:46 +0000248
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 /// Emit - Print the abbreviation using the specified Dwarf writer.
250 ///
aslc200b112008-08-16 12:57:46 +0000251 void Emit(const DwarfDebug &DD) const;
252
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253#ifndef NDEBUG
254 void print(std::ostream *O) {
255 if (O) print(*O);
256 }
257 void print(std::ostream &O);
258 void dump();
259#endif
260};
261
262//===----------------------------------------------------------------------===//
263/// DIE - A structured debug information entry. Has an abbreviation which
264/// describes it's organization.
265class DIE : public FoldingSetNode {
266protected:
267 /// Abbrev - Buffer for constructing abbreviation.
268 ///
269 DIEAbbrev Abbrev;
aslc200b112008-08-16 12:57:46 +0000270
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 /// Offset - Offset in debug info section.
272 ///
273 unsigned Offset;
aslc200b112008-08-16 12:57:46 +0000274
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 /// Size - Size of instance + children.
276 ///
277 unsigned Size;
aslc200b112008-08-16 12:57:46 +0000278
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 /// Children DIEs.
280 ///
281 std::vector<DIE *> Children;
aslc200b112008-08-16 12:57:46 +0000282
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 /// Attributes values.
284 ///
Owen Anderson88dd6232008-06-24 21:44:59 +0000285 SmallVector<DIEValue*, 32> Values;
aslc200b112008-08-16 12:57:46 +0000286
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000288 explicit DIE(unsigned Tag)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 : Abbrev(Tag, DW_CHILDREN_no)
290 , Offset(0)
291 , Size(0)
292 , Children()
293 , Values()
294 {}
295 virtual ~DIE();
aslc200b112008-08-16 12:57:46 +0000296
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 // Accessors.
298 DIEAbbrev &getAbbrev() { return Abbrev; }
299 unsigned getAbbrevNumber() const {
300 return Abbrev.getNumber();
301 }
302 unsigned getTag() const { return Abbrev.getTag(); }
303 unsigned getOffset() const { return Offset; }
304 unsigned getSize() const { return Size; }
305 const std::vector<DIE *> &getChildren() const { return Children; }
Owen Anderson88dd6232008-06-24 21:44:59 +0000306 SmallVector<DIEValue*, 32> &getValues() { return Values; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
308 void setOffset(unsigned O) { Offset = O; }
309 void setSize(unsigned S) { Size = S; }
aslc200b112008-08-16 12:57:46 +0000310
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 /// AddValue - Add a value and attributes to a DIE.
312 ///
313 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
314 Abbrev.AddAttribute(Attribute, Form);
315 Values.push_back(Value);
316 }
aslc200b112008-08-16 12:57:46 +0000317
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 /// SiblingOffset - Return the offset of the debug information entry's
319 /// sibling.
320 unsigned SiblingOffset() const { return Offset + Size; }
aslc200b112008-08-16 12:57:46 +0000321
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
323 ///
324 void AddSiblingOffset();
325
326 /// AddChild - Add a child to the DIE.
327 ///
328 void AddChild(DIE *Child) {
329 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
330 Children.push_back(Child);
331 }
aslc200b112008-08-16 12:57:46 +0000332
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 /// Detach - Detaches objects connected to it after copying.
334 ///
335 void Detach() {
336 Children.clear();
337 }
aslc200b112008-08-16 12:57:46 +0000338
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 /// Profile - Used to gather unique data for the value folding set.
340 ///
341 void Profile(FoldingSetNodeID &ID) ;
aslc200b112008-08-16 12:57:46 +0000342
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343#ifndef NDEBUG
344 void print(std::ostream *O, unsigned IncIndent = 0) {
345 if (O) print(*O, IncIndent);
346 }
347 void print(std::ostream &O, unsigned IncIndent = 0);
348 void dump();
349#endif
350};
351
352//===----------------------------------------------------------------------===//
353/// DIEValue - A debug information entry value.
354///
355class DIEValue : public FoldingSetNode {
356public:
357 enum {
358 isInteger,
359 isString,
360 isLabel,
361 isAsIsLabel,
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000362 isSectionOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 isDelta,
364 isEntry,
365 isBlock
366 };
aslc200b112008-08-16 12:57:46 +0000367
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 /// Type - Type of data stored in the value.
369 ///
370 unsigned Type;
aslc200b112008-08-16 12:57:46 +0000371
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000372 explicit DIEValue(unsigned T)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 : Type(T)
374 {}
375 virtual ~DIEValue() {}
aslc200b112008-08-16 12:57:46 +0000376
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 // Accessors
378 unsigned getType() const { return Type; }
aslc200b112008-08-16 12:57:46 +0000379
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 // Implement isa/cast/dyncast.
381 static bool classof(const DIEValue *) { return true; }
aslc200b112008-08-16 12:57:46 +0000382
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 /// EmitValue - Emit value via the Dwarf writer.
384 ///
385 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
aslc200b112008-08-16 12:57:46 +0000386
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 /// SizeOf - Return the size of a value in bytes.
388 ///
389 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
aslc200b112008-08-16 12:57:46 +0000390
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 /// Profile - Used to gather unique data for the value folding set.
392 ///
393 virtual void Profile(FoldingSetNodeID &ID) = 0;
aslc200b112008-08-16 12:57:46 +0000394
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395#ifndef NDEBUG
396 void print(std::ostream *O) {
397 if (O) print(*O);
398 }
399 virtual void print(std::ostream &O) = 0;
400 void dump();
401#endif
402};
403
404//===----------------------------------------------------------------------===//
405/// DWInteger - An integer value DIE.
aslc200b112008-08-16 12:57:46 +0000406///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407class DIEInteger : public DIEValue {
408private:
409 uint64_t Integer;
aslc200b112008-08-16 12:57:46 +0000410
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411public:
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000412 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413
414 // Implement isa/cast/dyncast.
415 static bool classof(const DIEInteger *) { return true; }
416 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
aslc200b112008-08-16 12:57:46 +0000417
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 /// BestForm - Choose the best form for integer.
419 ///
420 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
421 if (IsSigned) {
422 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
423 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
424 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
425 } else {
426 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
427 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
428 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
429 }
430 return DW_FORM_data8;
431 }
aslc200b112008-08-16 12:57:46 +0000432
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 /// EmitValue - Emit integer of appropriate size.
434 ///
435 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000436
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 /// SizeOf - Determine size of integer value in bytes.
438 ///
439 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000440
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 /// Profile - Used to gather unique data for the value folding set.
442 ///
443 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
444 ID.AddInteger(isInteger);
445 ID.AddInteger(Integer);
446 }
447 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
aslc200b112008-08-16 12:57:46 +0000448
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449#ifndef NDEBUG
450 virtual void print(std::ostream &O) {
451 O << "Int: " << (int64_t)Integer
452 << " 0x" << std::hex << Integer << std::dec;
453 }
454#endif
455};
456
457//===----------------------------------------------------------------------===//
458/// DIEString - A string value DIE.
aslc200b112008-08-16 12:57:46 +0000459///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460class DIEString : public DIEValue {
461public:
462 const std::string String;
aslc200b112008-08-16 12:57:46 +0000463
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000464 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465
466 // Implement isa/cast/dyncast.
467 static bool classof(const DIEString *) { return true; }
468 static bool classof(const DIEValue *S) { return S->Type == isString; }
aslc200b112008-08-16 12:57:46 +0000469
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 /// EmitValue - Emit string value.
471 ///
472 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000473
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 /// SizeOf - Determine size of string value in bytes.
475 ///
476 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
477 return String.size() + sizeof(char); // sizeof('\0');
478 }
aslc200b112008-08-16 12:57:46 +0000479
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 /// Profile - Used to gather unique data for the value folding set.
481 ///
482 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
483 ID.AddInteger(isString);
484 ID.AddString(String);
485 }
486 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
aslc200b112008-08-16 12:57:46 +0000487
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488#ifndef NDEBUG
489 virtual void print(std::ostream &O) {
490 O << "Str: \"" << String << "\"";
491 }
492#endif
493};
494
495//===----------------------------------------------------------------------===//
496/// DIEDwarfLabel - A Dwarf internal label expression DIE.
497//
498class DIEDwarfLabel : public DIEValue {
499public:
500
501 const DWLabel Label;
aslc200b112008-08-16 12:57:46 +0000502
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000503 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504
505 // Implement isa/cast/dyncast.
506 static bool classof(const DIEDwarfLabel *) { return true; }
507 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
aslc200b112008-08-16 12:57:46 +0000508
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 /// EmitValue - Emit label value.
510 ///
511 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000512
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 /// SizeOf - Determine size of label value in bytes.
514 ///
515 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000516
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 /// Profile - Used to gather unique data for the value folding set.
518 ///
519 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
520 ID.AddInteger(isLabel);
521 Label.Profile(ID);
522 }
523 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
aslc200b112008-08-16 12:57:46 +0000524
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525#ifndef NDEBUG
526 virtual void print(std::ostream &O) {
527 O << "Lbl: ";
528 Label.print(O);
529 }
530#endif
531};
532
533
534//===----------------------------------------------------------------------===//
535/// DIEObjectLabel - A label to an object in code or data.
536//
537class DIEObjectLabel : public DIEValue {
538public:
539 const std::string Label;
aslc200b112008-08-16 12:57:46 +0000540
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000541 explicit DIEObjectLabel(const std::string &L)
542 : DIEValue(isAsIsLabel), Label(L) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543
544 // Implement isa/cast/dyncast.
545 static bool classof(const DIEObjectLabel *) { return true; }
546 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
aslc200b112008-08-16 12:57:46 +0000547
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 /// EmitValue - Emit label value.
549 ///
550 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000551
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 /// SizeOf - Determine size of label value in bytes.
553 ///
554 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000555
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 /// Profile - Used to gather unique data for the value folding set.
557 ///
558 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
559 ID.AddInteger(isAsIsLabel);
560 ID.AddString(Label);
561 }
562 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
563
564#ifndef NDEBUG
565 virtual void print(std::ostream &O) {
566 O << "Obj: " << Label;
567 }
568#endif
569};
570
571//===----------------------------------------------------------------------===//
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000572/// DIESectionOffset - A section offset DIE.
573//
574class DIESectionOffset : public DIEValue {
575public:
576 const DWLabel Label;
577 const DWLabel Section;
578 bool IsEH : 1;
579 bool UseSet : 1;
aslc200b112008-08-16 12:57:46 +0000580
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000581 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
582 bool isEH = false, bool useSet = true)
583 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
584 IsEH(isEH), UseSet(useSet) {}
585
586 // Implement isa/cast/dyncast.
587 static bool classof(const DIESectionOffset *) { return true; }
588 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
aslc200b112008-08-16 12:57:46 +0000589
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000590 /// EmitValue - Emit section offset.
591 ///
592 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000593
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000594 /// SizeOf - Determine size of section offset value in bytes.
595 ///
596 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000597
Argiris Kirtzidis03449652008-06-18 19:27:37 +0000598 /// Profile - Used to gather unique data for the value folding set.
599 ///
600 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
601 const DWLabel &Section) {
602 ID.AddInteger(isSectionOffset);
603 Label.Profile(ID);
604 Section.Profile(ID);
605 // IsEH and UseSet are specific to the Label/Section that we will emit
606 // the offset for; so Label/Section are enough for uniqueness.
607 }
608 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
609
610#ifndef NDEBUG
611 virtual void print(std::ostream &O) {
612 O << "Off: ";
613 Label.print(O);
614 O << "-";
615 Section.print(O);
616 O << "-" << IsEH << "-" << UseSet;
617 }
618#endif
619};
620
621//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622/// DIEDelta - A simple label difference DIE.
aslc200b112008-08-16 12:57:46 +0000623///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624class DIEDelta : public DIEValue {
625public:
626 const DWLabel LabelHi;
627 const DWLabel LabelLo;
aslc200b112008-08-16 12:57:46 +0000628
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
630 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
631
632 // Implement isa/cast/dyncast.
633 static bool classof(const DIEDelta *) { return true; }
634 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
aslc200b112008-08-16 12:57:46 +0000635
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 /// EmitValue - Emit delta value.
637 ///
638 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000639
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 /// SizeOf - Determine size of delta value in bytes.
641 ///
642 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000643
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 /// Profile - Used to gather unique data for the value folding set.
645 ///
646 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
647 const DWLabel &LabelLo) {
648 ID.AddInteger(isDelta);
649 LabelHi.Profile(ID);
650 LabelLo.Profile(ID);
651 }
652 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
653
654#ifndef NDEBUG
655 virtual void print(std::ostream &O) {
656 O << "Del: ";
657 LabelHi.print(O);
658 O << "-";
659 LabelLo.print(O);
660 }
661#endif
662};
663
664//===----------------------------------------------------------------------===//
665/// DIEntry - A pointer to another debug information entry. An instance of this
666/// class can also be used as a proxy for a debug information entry not yet
667/// defined (ie. types.)
668class DIEntry : public DIEValue {
669public:
670 DIE *Entry;
aslc200b112008-08-16 12:57:46 +0000671
Dan Gohman9ba5d4d2007-08-27 14:50:10 +0000672 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
aslc200b112008-08-16 12:57:46 +0000673
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674 // Implement isa/cast/dyncast.
675 static bool classof(const DIEntry *) { return true; }
676 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
aslc200b112008-08-16 12:57:46 +0000677
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 /// EmitValue - Emit debug information entry offset.
679 ///
680 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000681
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 /// SizeOf - Determine size of debug information entry in bytes.
683 ///
684 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
685 return sizeof(int32_t);
686 }
aslc200b112008-08-16 12:57:46 +0000687
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688 /// Profile - Used to gather unique data for the value folding set.
689 ///
690 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
691 ID.AddInteger(isEntry);
692 ID.AddPointer(Entry);
693 }
694 virtual void Profile(FoldingSetNodeID &ID) {
695 ID.AddInteger(isEntry);
aslc200b112008-08-16 12:57:46 +0000696
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 if (Entry) {
698 ID.AddPointer(Entry);
699 } else {
700 ID.AddPointer(this);
701 }
702 }
aslc200b112008-08-16 12:57:46 +0000703
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704#ifndef NDEBUG
705 virtual void print(std::ostream &O) {
706 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
707 }
708#endif
709};
710
711//===----------------------------------------------------------------------===//
712/// DIEBlock - A block of values. Primarily used for location expressions.
713//
714class DIEBlock : public DIEValue, public DIE {
715public:
716 unsigned Size; // Size in bytes excluding size header.
aslc200b112008-08-16 12:57:46 +0000717
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 DIEBlock()
719 : DIEValue(isBlock)
720 , DIE(0)
721 , Size(0)
722 {}
723 ~DIEBlock() {
724 }
aslc200b112008-08-16 12:57:46 +0000725
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726 // Implement isa/cast/dyncast.
727 static bool classof(const DIEBlock *) { return true; }
728 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
aslc200b112008-08-16 12:57:46 +0000729
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 /// ComputeSize - calculate the size of the block.
731 ///
732 unsigned ComputeSize(DwarfDebug &DD);
aslc200b112008-08-16 12:57:46 +0000733
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 /// BestForm - Choose the best form for data.
735 ///
736 unsigned BestForm() const {
737 if ((unsigned char)Size == Size) return DW_FORM_block1;
738 if ((unsigned short)Size == Size) return DW_FORM_block2;
739 if ((unsigned int)Size == Size) return DW_FORM_block4;
740 return DW_FORM_block;
741 }
742
743 /// EmitValue - Emit block data.
744 ///
745 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
aslc200b112008-08-16 12:57:46 +0000746
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747 /// SizeOf - Determine size of block data in bytes.
748 ///
749 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
aslc200b112008-08-16 12:57:46 +0000750
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751
752 /// Profile - Used to gather unique data for the value folding set.
753 ///
754 virtual void Profile(FoldingSetNodeID &ID) {
755 ID.AddInteger(isBlock);
756 DIE::Profile(ID);
757 }
aslc200b112008-08-16 12:57:46 +0000758
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759#ifndef NDEBUG
760 virtual void print(std::ostream &O) {
761 O << "Blk: ";
762 DIE::print(O, 5);
763 }
764#endif
765};
766
767//===----------------------------------------------------------------------===//
768/// CompileUnit - This dwarf writer support class manages information associate
769/// with a source file.
770class CompileUnit {
771private:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772 /// ID - File identifier for source.
773 ///
774 unsigned ID;
aslc200b112008-08-16 12:57:46 +0000775
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776 /// Die - Compile unit debug information entry.
777 ///
778 DIE *Die;
aslc200b112008-08-16 12:57:46 +0000779
Devang Patel42f6bed2009-01-13 23:54:55 +0000780 /// GVToDieMap - Tracks the mapping of unit level debug informaton
781 /// variables to debug information entries.
Devang Patel56b1d132009-01-20 00:58:55 +0000782 std::map<GlobalVariable *, DIE *> GVToDieMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000783
Devang Patel42f6bed2009-01-13 23:54:55 +0000784 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785 /// descriptors to debug information entries using a DIEntry proxy.
Devang Patel56b1d132009-01-20 00:58:55 +0000786 std::map<GlobalVariable *, DIEntry *> GVToDIEntryMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787
788 /// Globals - A map of globally visible named entities for this unit.
789 ///
790 std::map<std::string, DIE *> Globals;
791
792 /// DiesSet - Used to uniquely define dies within the compile unit.
793 ///
794 FoldingSet<DIE> DiesSet;
aslc200b112008-08-16 12:57:46 +0000795
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796public:
Devang Patelb3907da2009-01-05 23:03:32 +0000797 CompileUnit(unsigned I, DIE *D)
Devang Patel42f6bed2009-01-13 23:54:55 +0000798 : ID(I), Die(D), GVToDieMap(),
Devang Patel5302e672009-01-17 06:51:37 +0000799 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize)
Devang Patelb3907da2009-01-05 23:03:32 +0000800 {}
801
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802 ~CompileUnit() {
803 delete Die;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 }
aslc200b112008-08-16 12:57:46 +0000805
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 // Accessors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807 unsigned getID() const { return ID; }
808 DIE* getDie() const { return Die; }
809 std::map<std::string, DIE *> &getGlobals() { return Globals; }
810
811 /// hasContent - Return true if this compile unit has something to write out.
812 ///
813 bool hasContent() const {
814 return !Die->getChildren().empty();
815 }
816
817 /// AddGlobal - Add a new global entity to the compile unit.
818 ///
819 void AddGlobal(const std::string &Name, DIE *Die) {
820 Globals[Name] = Die;
821 }
aslc200b112008-08-16 12:57:46 +0000822
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823 /// getDieMapSlotFor - Returns the debug information entry map slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000824 /// specified debug variable.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000825 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
826 return GVToDieMap[GV];
827 }
aslc200b112008-08-16 12:57:46 +0000828
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000829 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
Devang Patel42f6bed2009-01-13 23:54:55 +0000830 /// specified debug variable.
Devang Patel4a4cbe72009-01-05 21:47:57 +0000831 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
832 return GVToDIEntryMap[GV];
833 }
aslc200b112008-08-16 12:57:46 +0000834
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835 /// AddDie - Adds or interns the DIE to the compile unit.
836 ///
837 DIE *AddDie(DIE &Buffer) {
838 FoldingSetNodeID ID;
839 Buffer.Profile(ID);
840 void *Where;
841 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
aslc200b112008-08-16 12:57:46 +0000842
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843 if (!Die) {
844 Die = new DIE(Buffer);
845 DiesSet.InsertNode(Die, Where);
846 this->Die->AddChild(Die);
847 Buffer.Detach();
848 }
aslc200b112008-08-16 12:57:46 +0000849
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 return Die;
851 }
852};
853
854//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +0000855/// Dwarf - Emits general Dwarf directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856///
857class Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858protected:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 //===--------------------------------------------------------------------===//
860 // Core attributes used by the Dwarf writer.
861 //
aslc200b112008-08-16 12:57:46 +0000862
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863 //
864 /// O - Stream to .s file.
865 ///
Owen Anderson847b99b2008-08-21 00:14:44 +0000866 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867
868 /// Asm - Target of Dwarf emission.
869 ///
870 AsmPrinter *Asm;
aslc200b112008-08-16 12:57:46 +0000871
Bill Wendlingac9639d2008-07-01 23:34:48 +0000872 /// TAI - Target asm information.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873 const TargetAsmInfo *TAI;
aslc200b112008-08-16 12:57:46 +0000874
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875 /// TD - Target data.
876 const TargetData *TD;
aslc200b112008-08-16 12:57:46 +0000877
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 /// RI - Register Information.
Dan Gohman1e57df32008-02-10 18:45:23 +0000879 const TargetRegisterInfo *RI;
aslc200b112008-08-16 12:57:46 +0000880
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881 /// M - Current module.
882 ///
883 Module *M;
aslc200b112008-08-16 12:57:46 +0000884
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000885 /// MF - Current machine function.
886 ///
887 MachineFunction *MF;
aslc200b112008-08-16 12:57:46 +0000888
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889 /// MMI - Collected machine module information.
890 ///
891 MachineModuleInfo *MMI;
aslc200b112008-08-16 12:57:46 +0000892
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000893 /// SubprogramCount - The running count of functions being compiled.
894 ///
895 unsigned SubprogramCount;
aslc200b112008-08-16 12:57:46 +0000896
Chris Lattnerb3876c72007-09-24 03:35:37 +0000897 /// Flavor - A unique string indicating what dwarf producer this is, used to
898 /// unique labels.
899 const char * const Flavor;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000900
901 unsigned SetCounter;
Owen Anderson847b99b2008-08-21 00:14:44 +0000902 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
Chris Lattnerb3876c72007-09-24 03:35:37 +0000903 const char *flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904 : O(OS)
905 , Asm(A)
906 , TAI(T)
907 , TD(Asm->TM.getTargetData())
908 , RI(Asm->TM.getRegisterInfo())
909 , M(NULL)
910 , MF(NULL)
911 , MMI(NULL)
912 , SubprogramCount(0)
Chris Lattnerb3876c72007-09-24 03:35:37 +0000913 , Flavor(flavor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914 , SetCounter(1)
915 {
916 }
917
918public:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 //===--------------------------------------------------------------------===//
920 // Accessors.
921 //
922 AsmPrinter *getAsm() const { return Asm; }
923 MachineModuleInfo *getMMI() const { return MMI; }
924 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
Dan Gohmancfb72b22007-09-27 23:12:31 +0000925 const TargetData *getTargetData() const { return TD; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000926
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000927 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
928 const {
929 if (isInSection && TAI->getDwarfSectionOffsetDirective())
930 O << TAI->getDwarfSectionOffsetDirective();
Dan Gohmancfb72b22007-09-27 23:12:31 +0000931 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000932 O << TAI->getData32bitsDirective();
933 else
934 O << TAI->getData64bitsDirective();
935 }
aslc200b112008-08-16 12:57:46 +0000936
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000937 /// PrintLabelName - Print label name in form used by Dwarf writer.
938 ///
939 void PrintLabelName(DWLabel Label) const {
940 PrintLabelName(Label.Tag, Label.Number);
941 }
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000942 void PrintLabelName(const char *Tag, unsigned Number) const {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +0000943 O << TAI->getPrivateGlobalPrefix() << Tag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000944 if (Number) O << Number;
945 }
aslc200b112008-08-16 12:57:46 +0000946
Chris Lattnerb3876c72007-09-24 03:35:37 +0000947 void PrintLabelName(const char *Tag, unsigned Number,
948 const char *Suffix) const {
949 O << TAI->getPrivateGlobalPrefix() << Tag;
950 if (Number) O << Number;
951 O << Suffix;
952 }
aslc200b112008-08-16 12:57:46 +0000953
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000954 /// EmitLabel - Emit location label for internal use by Dwarf.
955 ///
956 void EmitLabel(DWLabel Label) const {
957 EmitLabel(Label.Tag, Label.Number);
958 }
959 void EmitLabel(const char *Tag, unsigned Number) const {
960 PrintLabelName(Tag, Number);
961 O << ":\n";
962 }
aslc200b112008-08-16 12:57:46 +0000963
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000964 /// EmitReference - Emit a reference to a label.
965 ///
Dan Gohman4fd77742007-09-28 15:43:33 +0000966 void EmitReference(DWLabel Label, bool IsPCRelative = false,
967 bool Force32Bit = false) const {
968 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969 }
970 void EmitReference(const char *Tag, unsigned Number,
Dan Gohman4fd77742007-09-28 15:43:33 +0000971 bool IsPCRelative = false, bool Force32Bit = false) const {
972 PrintRelDirective(Force32Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000973 PrintLabelName(Tag, Number);
aslc200b112008-08-16 12:57:46 +0000974
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000975 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
976 }
Dan Gohman4fd77742007-09-28 15:43:33 +0000977 void EmitReference(const std::string &Name, bool IsPCRelative = false,
978 bool Force32Bit = false) const {
979 PrintRelDirective(Force32Bit);
aslc200b112008-08-16 12:57:46 +0000980
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000981 O << Name;
aslc200b112008-08-16 12:57:46 +0000982
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
984 }
985
986 /// EmitDifference - Emit the difference between two labels. Some
987 /// assemblers do not behave with absolute expressions with data directives,
988 /// so there is an option (needsSet) to use an intermediary set expression.
989 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
990 bool IsSmall = false) {
991 EmitDifference(LabelHi.Tag, LabelHi.Number,
992 LabelLo.Tag, LabelLo.Number,
993 IsSmall);
994 }
995 void EmitDifference(const char *TagHi, unsigned NumberHi,
996 const char *TagLo, unsigned NumberLo,
997 bool IsSmall = false) {
998 if (TAI->needsSet()) {
999 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +00001000 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001001 O << ",";
1002 PrintLabelName(TagHi, NumberHi);
1003 O << "-";
1004 PrintLabelName(TagLo, NumberLo);
1005 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001006
1007 PrintRelDirective(IsSmall);
Chris Lattnerb3876c72007-09-24 03:35:37 +00001008 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 ++SetCounter;
1010 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001011 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001012
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013 PrintLabelName(TagHi, NumberHi);
1014 O << "-";
1015 PrintLabelName(TagLo, NumberLo);
1016 }
1017 }
1018
1019 void EmitSectionOffset(const char* Label, const char* Section,
1020 unsigned LabelNumber, unsigned SectionNumber,
Dale Johannesen0ebb2432008-03-26 23:31:39 +00001021 bool IsSmall = false, bool isEH = false,
1022 bool useSet = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001023 bool printAbsolute = false;
Dale Johannesen0ebb2432008-03-26 23:31:39 +00001024 if (isEH)
1025 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
1026 else
1027 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
1028
1029 if (TAI->needsSet() && useSet) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 O << "\t.set\t";
Chris Lattnerb3876c72007-09-24 03:35:37 +00001031 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032 O << ",";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001033 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001034
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 if (!printAbsolute) {
1036 O << "-";
1037 PrintLabelName(Section, SectionNumber);
aslc200b112008-08-16 12:57:46 +00001038 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039 O << "\n";
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001040
1041 PrintRelDirective(IsSmall);
aslc200b112008-08-16 12:57:46 +00001042
Chris Lattnerb3876c72007-09-24 03:35:37 +00001043 PrintLabelName("set", SetCounter, Flavor);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 ++SetCounter;
1045 } else {
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001046 PrintRelDirective(IsSmall, true);
aslc200b112008-08-16 12:57:46 +00001047
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00001048 PrintLabelName(Label, LabelNumber);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001050 if (!printAbsolute) {
1051 O << "-";
1052 PrintLabelName(Section, SectionNumber);
1053 }
aslc200b112008-08-16 12:57:46 +00001054 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 }
aslc200b112008-08-16 12:57:46 +00001056
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001057 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1058 /// frame.
1059 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
Dale Johannesenf5a11532007-11-13 19:13:01 +00001060 const std::vector<MachineMove> &Moves, bool isEH) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061 int stackGrowth =
1062 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1063 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00001064 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001065 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
1066
1067 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1068 const MachineMove &Move = Moves[i];
1069 unsigned LabelID = Move.getLabelID();
aslc200b112008-08-16 12:57:46 +00001070
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001071 if (LabelID) {
1072 LabelID = MMI->MappedLabel(LabelID);
aslc200b112008-08-16 12:57:46 +00001073
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 // Throw out move if the label is invalid.
1075 if (!LabelID) continue;
1076 }
aslc200b112008-08-16 12:57:46 +00001077
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078 const MachineLocation &Dst = Move.getDestination();
1079 const MachineLocation &Src = Move.getSource();
aslc200b112008-08-16 12:57:46 +00001080
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001081 // Advance row if new location.
1082 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1083 Asm->EmitInt8(DW_CFA_advance_loc4);
1084 Asm->EOL("DW_CFA_advance_loc4");
1085 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1086 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00001087
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001088 BaseLabelID = LabelID;
1089 BaseLabel = "label";
1090 IsLocal = true;
1091 }
aslc200b112008-08-16 12:57:46 +00001092
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093 // If advancing cfa.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001094 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1095 if (!Src.isReg()) {
1096 if (Src.getReg() == MachineLocation::VirtualFP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001097 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1098 Asm->EOL("DW_CFA_def_cfa_offset");
1099 } else {
1100 Asm->EmitInt8(DW_CFA_def_cfa);
1101 Asm->EOL("DW_CFA_def_cfa");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001102 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001103 Asm->EOL("Register");
1104 }
aslc200b112008-08-16 12:57:46 +00001105
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001106 int Offset = -Src.getOffset();
aslc200b112008-08-16 12:57:46 +00001107
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001108 Asm->EmitULEB128Bytes(Offset);
1109 Asm->EOL("Offset");
1110 } else {
1111 assert(0 && "Machine move no supported yet.");
1112 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001113 } else if (Src.isReg() &&
1114 Src.getReg() == MachineLocation::VirtualFP) {
1115 if (Dst.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001116 Asm->EmitInt8(DW_CFA_def_cfa_register);
1117 Asm->EOL("DW_CFA_def_cfa_register");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001118 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001119 Asm->EOL("Register");
1120 } else {
1121 assert(0 && "Machine move no supported yet.");
1122 }
1123 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001124 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125 int Offset = Dst.getOffset() / stackGrowth;
aslc200b112008-08-16 12:57:46 +00001126
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001127 if (Offset < 0) {
1128 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1129 Asm->EOL("DW_CFA_offset_extended_sf");
1130 Asm->EmitULEB128Bytes(Reg);
1131 Asm->EOL("Reg");
1132 Asm->EmitSLEB128Bytes(Offset);
1133 Asm->EOL("Offset");
1134 } else if (Reg < 64) {
1135 Asm->EmitInt8(DW_CFA_offset + Reg);
Evan Cheng6181e062008-07-09 21:53:02 +00001136 if (VerboseAsm)
1137 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1138 else
1139 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001140 Asm->EmitULEB128Bytes(Offset);
1141 Asm->EOL("Offset");
1142 } else {
1143 Asm->EmitInt8(DW_CFA_offset_extended);
1144 Asm->EOL("DW_CFA_offset_extended");
1145 Asm->EmitULEB128Bytes(Reg);
1146 Asm->EOL("Reg");
1147 Asm->EmitULEB128Bytes(Offset);
1148 Asm->EOL("Offset");
1149 }
1150 }
1151 }
1152 }
1153
1154};
1155
1156//===----------------------------------------------------------------------===//
Devang Patel35a078f2009-01-12 22:54:42 +00001157/// SrcLineInfo - This class is used to record source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001158///
1159class SrcLineInfo {
1160 unsigned Line; // Source line number.
1161 unsigned Column; // Source column.
1162 unsigned SourceID; // Source ID number.
1163 unsigned LabelID; // Label in code ID number.
1164public:
1165 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
Bill Wendling824a8bf2009-02-03 21:17:20 +00001166 : Line(L), Column(C), SourceID(S), LabelID(I) {}
Devang Patel7dd15a92009-01-08 17:19:22 +00001167
1168 // Accessors
1169 unsigned getLine() const { return Line; }
1170 unsigned getColumn() const { return Column; }
1171 unsigned getSourceID() const { return SourceID; }
1172 unsigned getLabelID() const { return LabelID; }
1173};
1174
Devang Patel7dd15a92009-01-08 17:19:22 +00001175//===----------------------------------------------------------------------===//
Devang Patel5f244e32009-01-05 22:35:52 +00001176/// SrcFileInfo - This class is used to track source information.
1177///
1178class SrcFileInfo {
1179 unsigned DirectoryID; // Directory ID number.
1180 std::string Name; // File name (not including directory.)
1181public:
1182 SrcFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
1183
1184 // Accessors
1185 unsigned getDirectoryID() const { return DirectoryID; }
1186 const std::string &getName() const { return Name; }
1187
1188 /// operator== - Used by UniqueVector to locate entry.
1189 ///
Devang Patel42f6bed2009-01-13 23:54:55 +00001190 bool operator==(const SrcFileInfo &SI) const {
Devang Patel5f244e32009-01-05 22:35:52 +00001191 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
1192 }
1193
1194 /// operator< - Used by UniqueVector to locate entry.
1195 ///
1196 bool operator<(const SrcFileInfo &SI) const {
1197 return getDirectoryID() < SI.getDirectoryID() ||
1198 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
1199 }
1200};
1201
1202//===----------------------------------------------------------------------===//
Devang Patel4d1709e2009-01-08 02:33:41 +00001203/// DbgVariable - This class is used to track local variable information.
1204///
1205class DbgVariable {
Devang Patel7c8a2772009-01-16 19:28:14 +00001206 DIVariable Var; // Variable Descriptor.
Devang Patel4d1709e2009-01-08 02:33:41 +00001207 unsigned FrameIndex; // Variable frame index.
Devang Patel4d1709e2009-01-08 02:33:41 +00001208public:
Devang Patel7c8a2772009-01-16 19:28:14 +00001209 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
Devang Patel4d1709e2009-01-08 02:33:41 +00001210
1211 // Accessors.
Devang Patel7c8a2772009-01-16 19:28:14 +00001212 DIVariable getVariable() const { return Var; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001213 unsigned getFrameIndex() const { return FrameIndex; }
1214};
1215
1216//===----------------------------------------------------------------------===//
1217/// DbgScope - This class is used to track scope information.
1218///
1219class DbgScope {
Devang Patel4d1709e2009-01-08 02:33:41 +00001220 DbgScope *Parent; // Parent to this scope.
Devang Patel49a3bd92009-01-16 18:01:58 +00001221 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001222 // Either subprogram or block.
1223 unsigned StartLabelID; // Label ID of the beginning of scope.
1224 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel49a3bd92009-01-16 18:01:58 +00001225 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
Devang Patel63c22f42009-01-10 02:42:49 +00001226 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Devang Patel4d1709e2009-01-08 02:33:41 +00001227public:
Devang Patel2560d922009-01-15 18:25:17 +00001228 DbgScope(DbgScope *P, DIDescriptor D)
Devang Patel4d1709e2009-01-08 02:33:41 +00001229 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1230 {}
Devang Patela4162952009-01-12 18:48:36 +00001231 ~DbgScope() {
1232 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1233 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1234 }
Devang Patel4d1709e2009-01-08 02:33:41 +00001235
1236 // Accessors.
Devang Patel49a3bd92009-01-16 18:01:58 +00001237 DbgScope *getParent() const { return Parent; }
1238 DIDescriptor getDesc() const { return Desc; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001239 unsigned getStartLabelID() const { return StartLabelID; }
1240 unsigned getEndLabelID() const { return EndLabelID; }
Devang Patel63c22f42009-01-10 02:42:49 +00001241 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1242 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Devang Patel4d1709e2009-01-08 02:33:41 +00001243 void setStartLabelID(unsigned S) { StartLabelID = S; }
1244 void setEndLabelID(unsigned E) { EndLabelID = E; }
1245
1246 /// AddScope - Add a scope to the scope.
1247 ///
1248 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1249
1250 /// AddVariable - Add a variable to the scope.
1251 ///
1252 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1253};
1254
1255//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00001256/// DwarfDebug - Emits Dwarf debug directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001257///
1258class DwarfDebug : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 //===--------------------------------------------------------------------===//
1260 // Attributes used to construct specific Dwarf sections.
1261 //
aslc200b112008-08-16 12:57:46 +00001262
Devang Patel5302e672009-01-17 06:51:37 +00001263 /// DW_CUs - All the compile units involved in this build. The index
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264 /// of each entry in this vector corresponds to the sources in MMI.
Devang Patel7dd15a92009-01-08 17:19:22 +00001265 DenseMap<Value *, CompileUnit *> DW_CUs;
aslc200b112008-08-16 12:57:46 +00001266
Devang Patel2ae1db52009-01-30 18:20:31 +00001267 /// MainCU - Some platform prefers one compile unit per .o file. In such
1268 /// cases, all dies are inserted in MainCU.
1269 CompileUnit *MainCU;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001270 /// AbbreviationsSet - Used to uniquely define abbreviations.
1271 ///
1272 FoldingSet<DIEAbbrev> AbbreviationsSet;
1273
1274 /// Abbreviations - A list of all the unique abbreviations in use.
1275 ///
1276 std::vector<DIEAbbrev *> Abbreviations;
aslc200b112008-08-16 12:57:46 +00001277
Devang Patelb28de842009-01-17 08:01:33 +00001278 /// Directories - Uniquing vector for directories.
Devang Patel5f244e32009-01-05 22:35:52 +00001279 UniqueVector<std::string> Directories;
1280
Devang Patelb28de842009-01-17 08:01:33 +00001281 /// SourceFiles - Uniquing vector for source files.
Devang Patel5f244e32009-01-05 22:35:52 +00001282 UniqueVector<SrcFileInfo> SrcFiles;
1283
Devang Patel9b829452009-01-16 21:07:53 +00001284 /// Lines - List of of source line correspondence.
Devang Patel7dd15a92009-01-08 17:19:22 +00001285 std::vector<SrcLineInfo> Lines;
1286
Devang Patel9b829452009-01-16 21:07:53 +00001287 /// ValuesSet - Used to uniquely define values.
1288 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001289 FoldingSet<DIEValue> ValuesSet;
aslc200b112008-08-16 12:57:46 +00001290
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001291 /// Values - A list of all the unique values in use.
1292 ///
1293 std::vector<DIEValue *> Values;
aslc200b112008-08-16 12:57:46 +00001294
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001295 /// StringPool - A UniqueVector of strings used by indirect references.
1296 ///
1297 UniqueVector<std::string> StringPool;
1298
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001299 /// SectionMap - Provides a unique id per text section.
1300 ///
Anton Korobeynikov55b94962008-09-24 22:15:21 +00001301 UniqueVector<const Section*> SectionMap;
aslc200b112008-08-16 12:57:46 +00001302
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001303 /// SectionSourceLines - Tracks line numbers per text section.
1304 ///
Devang Patel35a078f2009-01-12 22:54:42 +00001305 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306
1307 /// didInitial - Flag to indicate if initial emission has been done.
1308 ///
1309 bool didInitial;
aslc200b112008-08-16 12:57:46 +00001310
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001311 /// shouldEmit - Flag to indicate if debug information should be emitted.
1312 ///
1313 bool shouldEmit;
1314
Devang Patel2560d922009-01-15 18:25:17 +00001315 // RootDbgScope - Top level scope for the current function.
Devang Patel4d1709e2009-01-08 02:33:41 +00001316 //
1317 DbgScope *RootDbgScope;
1318
1319 // DbgScopeMap - Tracks the scopes in the current function.
1320 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
1321
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001322 struct FunctionDebugFrameInfo {
1323 unsigned Number;
1324 std::vector<MachineMove> Moves;
1325
1326 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
Dan Gohman9ba5d4d2007-08-27 14:50:10 +00001327 Number(Num), Moves(M) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001328 };
1329
1330 std::vector<FunctionDebugFrameInfo> DebugFrames;
aslc200b112008-08-16 12:57:46 +00001331
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001332public:
aslc200b112008-08-16 12:57:46 +00001333
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001334 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1335 ///
1336 bool ShouldEmitDwarf() const { return shouldEmit; }
1337
1338 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
aslc200b112008-08-16 12:57:46 +00001339 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001340 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1341 // Profile the node so that we can make it unique.
1342 FoldingSetNodeID ID;
1343 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00001344
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001345 // Check the set for priors.
1346 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
aslc200b112008-08-16 12:57:46 +00001347
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001348 // If it's newly added.
1349 if (InSet == &Abbrev) {
aslc200b112008-08-16 12:57:46 +00001350 // Add to abbreviation list.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001351 Abbreviations.push_back(&Abbrev);
1352 // Assign the vector position + 1 as its number.
1353 Abbrev.setNumber(Abbreviations.size());
1354 } else {
1355 // Assign existing abbreviation number.
1356 Abbrev.setNumber(InSet->getNumber());
1357 }
1358 }
1359
1360 /// NewString - Add a string to the constant pool and returns a label.
1361 ///
1362 DWLabel NewString(const std::string &String) {
1363 unsigned StringID = StringPool.insert(String);
1364 return DWLabel("string", StringID);
1365 }
aslc200b112008-08-16 12:57:46 +00001366
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001367 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1368 /// entry.
1369 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1370 DIEntry *Value;
aslc200b112008-08-16 12:57:46 +00001371
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001372 if (Entry) {
1373 FoldingSetNodeID ID;
1374 DIEntry::Profile(ID, Entry);
1375 void *Where;
1376 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
aslc200b112008-08-16 12:57:46 +00001377
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001378 if (Value) return Value;
aslc200b112008-08-16 12:57:46 +00001379
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001380 Value = new DIEntry(Entry);
1381 ValuesSet.InsertNode(Value, Where);
1382 } else {
1383 Value = new DIEntry(Entry);
1384 }
aslc200b112008-08-16 12:57:46 +00001385
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386 Values.push_back(Value);
1387 return Value;
1388 }
aslc200b112008-08-16 12:57:46 +00001389
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1391 ///
1392 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1393 Value->Entry = Entry;
1394 // Add to values set if not already there. If it is, we merely have a
1395 // duplicate in the values list (no harm.)
1396 ValuesSet.GetOrInsertNode(Value);
1397 }
1398
1399 /// AddUInt - Add an unsigned integer attribute data and value.
1400 ///
1401 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1402 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1403
1404 FoldingSetNodeID ID;
1405 DIEInteger::Profile(ID, Integer);
1406 void *Where;
1407 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1408 if (!Value) {
1409 Value = new DIEInteger(Integer);
1410 ValuesSet.InsertNode(Value, Where);
1411 Values.push_back(Value);
1412 }
aslc200b112008-08-16 12:57:46 +00001413
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001414 Die->AddValue(Attribute, Form, Value);
1415 }
aslc200b112008-08-16 12:57:46 +00001416
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417 /// AddSInt - Add an signed integer attribute data and value.
1418 ///
1419 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1420 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1421
1422 FoldingSetNodeID ID;
1423 DIEInteger::Profile(ID, (uint64_t)Integer);
1424 void *Where;
1425 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1426 if (!Value) {
1427 Value = new DIEInteger(Integer);
1428 ValuesSet.InsertNode(Value, Where);
1429 Values.push_back(Value);
1430 }
aslc200b112008-08-16 12:57:46 +00001431
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001432 Die->AddValue(Attribute, Form, Value);
1433 }
aslc200b112008-08-16 12:57:46 +00001434
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001435 /// AddString - Add a std::string attribute data and value.
1436 ///
1437 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1438 const std::string &String) {
1439 FoldingSetNodeID ID;
1440 DIEString::Profile(ID, String);
1441 void *Where;
1442 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1443 if (!Value) {
1444 Value = new DIEString(String);
1445 ValuesSet.InsertNode(Value, Where);
1446 Values.push_back(Value);
1447 }
aslc200b112008-08-16 12:57:46 +00001448
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001449 Die->AddValue(Attribute, Form, Value);
1450 }
aslc200b112008-08-16 12:57:46 +00001451
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001452 /// AddLabel - Add a Dwarf label attribute data and value.
1453 ///
1454 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1455 const DWLabel &Label) {
1456 FoldingSetNodeID ID;
1457 DIEDwarfLabel::Profile(ID, Label);
1458 void *Where;
1459 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1460 if (!Value) {
1461 Value = new DIEDwarfLabel(Label);
1462 ValuesSet.InsertNode(Value, Where);
1463 Values.push_back(Value);
1464 }
aslc200b112008-08-16 12:57:46 +00001465
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001466 Die->AddValue(Attribute, Form, Value);
1467 }
aslc200b112008-08-16 12:57:46 +00001468
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001469 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1470 ///
1471 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1472 const std::string &Label) {
1473 FoldingSetNodeID ID;
1474 DIEObjectLabel::Profile(ID, Label);
1475 void *Where;
1476 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1477 if (!Value) {
1478 Value = new DIEObjectLabel(Label);
1479 ValuesSet.InsertNode(Value, Where);
1480 Values.push_back(Value);
1481 }
aslc200b112008-08-16 12:57:46 +00001482
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001483 Die->AddValue(Attribute, Form, Value);
1484 }
aslc200b112008-08-16 12:57:46 +00001485
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001486 /// AddSectionOffset - Add a section offset label attribute data and value.
1487 ///
1488 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1489 const DWLabel &Label, const DWLabel &Section,
1490 bool isEH = false, bool useSet = true) {
1491 FoldingSetNodeID ID;
1492 DIESectionOffset::Profile(ID, Label, Section);
1493 void *Where;
1494 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1495 if (!Value) {
1496 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1497 ValuesSet.InsertNode(Value, Where);
1498 Values.push_back(Value);
1499 }
aslc200b112008-08-16 12:57:46 +00001500
Argiris Kirtzidis03449652008-06-18 19:27:37 +00001501 Die->AddValue(Attribute, Form, Value);
1502 }
aslc200b112008-08-16 12:57:46 +00001503
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504 /// AddDelta - Add a label delta attribute data and value.
1505 ///
1506 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1507 const DWLabel &Hi, const DWLabel &Lo) {
1508 FoldingSetNodeID ID;
1509 DIEDelta::Profile(ID, Hi, Lo);
1510 void *Where;
1511 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1512 if (!Value) {
1513 Value = new DIEDelta(Hi, Lo);
1514 ValuesSet.InsertNode(Value, Where);
1515 Values.push_back(Value);
1516 }
aslc200b112008-08-16 12:57:46 +00001517
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518 Die->AddValue(Attribute, Form, Value);
1519 }
aslc200b112008-08-16 12:57:46 +00001520
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001521 /// AddDIEntry - Add a DIE attribute data and value.
1522 ///
1523 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1524 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1525 }
1526
1527 /// AddBlock - Add block data.
1528 ///
1529 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1530 Block->ComputeSize(*this);
1531 FoldingSetNodeID ID;
1532 Block->Profile(ID);
1533 void *Where;
1534 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1535 if (!Value) {
1536 Value = Block;
1537 ValuesSet.InsertNode(Value, Where);
1538 Values.push_back(Value);
1539 } else {
Chris Lattner3de66892007-09-21 18:25:53 +00001540 // Already exists, reuse the previous one.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001541 delete Block;
Chris Lattner3de66892007-09-21 18:25:53 +00001542 Block = cast<DIEBlock>(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001543 }
aslc200b112008-08-16 12:57:46 +00001544
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545 Die->AddValue(Attribute, Block->BestForm(), Value);
1546 }
1547
1548private:
1549
1550 /// AddSourceLine - Add location information to specified debug information
1551 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001552 void AddSourceLine(DIE *Die, const DIVariable *V) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001553 unsigned FileID = 0;
1554 unsigned Line = V->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001555 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1556 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001557 assert (FileID && "Invalid file id");
Devang Patel4d1709e2009-01-08 02:33:41 +00001558 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1559 AddUInt(Die, DW_AT_decl_line, 0, Line);
1560 }
1561
1562 /// AddSourceLine - Add location information to specified debug information
1563 /// entry.
Devang Patel7c8a2772009-01-16 19:28:14 +00001564 void AddSourceLine(DIE *Die, const DIGlobal *G) {
Devang Patel5f244e32009-01-05 22:35:52 +00001565 unsigned FileID = 0;
1566 unsigned Line = G->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001567 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1568 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001569 assert (FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001570 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1571 AddUInt(Die, DW_AT_decl_line, 0, Line);
1572 }
1573
Devang Patel7c8a2772009-01-16 19:28:14 +00001574 void AddSourceLine(DIE *Die, const DIType *Ty) {
Devang Patel5f244e32009-01-05 22:35:52 +00001575 unsigned FileID = 0;
Devang Patel7c8a2772009-01-16 19:28:14 +00001576 unsigned Line = Ty->getLineNumber();
Devang Patel2ae1db52009-01-30 18:20:31 +00001577 DICompileUnit CU = Ty->getCompileUnit();
1578 if (CU.isNull())
1579 return;
1580 CompileUnit *Unit = FindCompileUnit(CU);
1581 FileID = Unit->getID();
Devang Pateld94b6932009-02-10 06:04:08 +00001582 assert (FileID && "Invalid file id");
Devang Patel5f244e32009-01-05 22:35:52 +00001583 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1584 AddUInt(Die, DW_AT_decl_line, 0, Line);
1585 }
1586
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001587 /// AddAddress - Add an address attribute to a die based on the location
1588 /// provided.
1589 void AddAddress(DIE *Die, unsigned Attribute,
1590 const MachineLocation &Location) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001591 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001592 DIEBlock *Block = new DIEBlock();
aslc200b112008-08-16 12:57:46 +00001593
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001594 if (Location.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001595 if (Reg < 32) {
1596 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1597 } else {
1598 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1599 AddUInt(Block, 0, DW_FORM_udata, Reg);
1600 }
1601 } else {
1602 if (Reg < 32) {
1603 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1604 } else {
1605 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1606 AddUInt(Block, 0, DW_FORM_udata, Reg);
1607 }
1608 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1609 }
aslc200b112008-08-16 12:57:46 +00001610
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001611 AddBlock(Die, Attribute, 0, Block);
1612 }
aslc200b112008-08-16 12:57:46 +00001613
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001614 /// AddType - Add a new type attribute to the specified entity.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001615 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Devang Patel165ed512009-01-23 19:13:31 +00001616 if (Ty.isNull())
Devang Patel4a4cbe72009-01-05 21:47:57 +00001617 return;
Devang Patel4a4cbe72009-01-05 21:47:57 +00001618
1619 // Check for pre-existence.
1620 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1621 // If it exists then use the existing value.
1622 if (Slot) {
1623 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1624 return;
1625 }
1626
1627 // Set up proxy.
1628 Slot = NewDIEntry();
1629
1630 // Construct type.
1631 DIE Buffer(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001632 if (Ty.isBasicType(Ty.getTag()))
1633 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1634 else if (Ty.isDerivedType(Ty.getTag()))
1635 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1636 else {
Bill Wendling824a8bf2009-02-03 21:17:20 +00001637 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
Devang Patelef4bf3b2009-01-15 19:26:23 +00001638 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1639 }
1640
Devang Patelb0cb07c2009-01-27 23:22:55 +00001641 // Add debug information entry to entity and appropriate context.
1642 DIE *Die = NULL;
1643 DIDescriptor Context = Ty.getContext();
1644 if (!Context.isNull())
1645 Die = DW_Unit->getDieMapSlotFor(Context.getGV());
1646
1647 if (Die) {
1648 DIE *Child = new DIE(Buffer);
1649 Die->AddChild(Child);
1650 Buffer.Detach();
1651 SetDIEntry(Slot, Child);
Bill Wendling824a8bf2009-02-03 21:17:20 +00001652 } else {
Devang Patelb0cb07c2009-01-27 23:22:55 +00001653 Die = DW_Unit->AddDie(Buffer);
1654 SetDIEntry(Slot, Die);
1655 }
1656
Devang Patel4a4cbe72009-01-05 21:47:57 +00001657 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1658 }
1659
Devang Patel46d13752009-01-05 19:07:53 +00001660 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1661 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001662 DIBasicType BTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001663
1664 // Get core information.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001665 const std::string &Name = BTy.getName();
Devang Patelfc187162009-01-05 17:57:47 +00001666 Buffer.setTag(DW_TAG_base_type);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001667 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
Devang Patelfc187162009-01-05 17:57:47 +00001668 // Add name if not anonymous or intermediate type.
1669 if (!Name.empty())
1670 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001671 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelfc187162009-01-05 17:57:47 +00001672 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1673 }
1674
Devang Patel46d13752009-01-05 19:07:53 +00001675 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1676 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001677 DIDerivedType DTy) {
Devang Patelfc187162009-01-05 17:57:47 +00001678
1679 // Get core information.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001680 const std::string &Name = DTy.getName();
1681 uint64_t Size = DTy.getSizeInBits() >> 3;
1682 unsigned Tag = DTy.getTag();
Devang Patelfc187162009-01-05 17:57:47 +00001683 // FIXME - Workaround for templates.
1684 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1685
1686 Buffer.setTag(Tag);
1687 // Map to main type, void will not have a type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001688 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel4a4cbe72009-01-05 21:47:57 +00001689 AddType(DW_Unit, &Buffer, FromTy);
Devang Patelfc187162009-01-05 17:57:47 +00001690
1691 // Add name if not anonymous or intermediate type.
1692 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1693
1694 // Add size if non-zero (derived types might be zero-sized.)
1695 if (Size)
1696 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1697
1698 // Add source line info if available and TyDesc is not a forward
1699 // declaration.
Devang Patele34e0882009-01-27 00:45:04 +00001700 if (!DTy.isForwardDecl())
1701 AddSourceLine(&Buffer, &DTy);
Devang Patelfc187162009-01-05 17:57:47 +00001702 }
1703
Devang Patel30c01372009-01-05 19:55:51 +00001704 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1705 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Devang Patelef4bf3b2009-01-15 19:26:23 +00001706 DICompositeType CTy) {
Devang Patel30c01372009-01-05 19:55:51 +00001707
Devang Patelb28de842009-01-17 08:01:33 +00001708 // Get core information.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001709 const std::string &Name = CTy.getName();
1710 uint64_t Size = CTy.getSizeInBits() >> 3;
1711 unsigned Tag = CTy.getTag();
Devang Patel8050bd72009-01-23 01:19:09 +00001712 Buffer.setTag(Tag);
Devang Patel30c01372009-01-05 19:55:51 +00001713 switch (Tag) {
1714 case DW_TAG_vector_type:
1715 case DW_TAG_array_type:
Devang Patelef4bf3b2009-01-15 19:26:23 +00001716 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001717 break;
Devang Patel3798f492009-01-20 18:35:14 +00001718 case DW_TAG_enumeration_type:
1719 {
1720 DIArray Elements = CTy.getTypeArray();
1721 // Add enumerators to enumeration type.
1722 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1723 DIE *ElemDie = NULL;
1724 DIEnumerator Enum(Elements.getElement(i).getGV());
1725 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1726 Buffer.AddChild(ElemDie);
1727 }
1728 }
1729 break;
Devang Patel30c01372009-01-05 19:55:51 +00001730 case DW_TAG_subroutine_type:
1731 {
1732 // Add prototype flag.
1733 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001734 DIArray Elements = CTy.getTypeArray();
Devang Patel30c01372009-01-05 19:55:51 +00001735 // Add return type.
Devang Patel4a4cbe72009-01-05 21:47:57 +00001736 DIDescriptor RTy = Elements.getElement(0);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001737 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
Devang Patel4a4cbe72009-01-05 21:47:57 +00001738
Devang Patel30c01372009-01-05 19:55:51 +00001739 // Add arguments.
1740 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1741 DIE *Arg = new DIE(DW_TAG_formal_parameter);
Devang Patel4a4cbe72009-01-05 21:47:57 +00001742 DIDescriptor Ty = Elements.getElement(i);
Devang Pateld40a7e52009-01-17 06:57:25 +00001743 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
Devang Patel30c01372009-01-05 19:55:51 +00001744 Buffer.AddChild(Arg);
1745 }
1746 }
1747 break;
1748 case DW_TAG_structure_type:
1749 case DW_TAG_union_type:
1750 {
1751 // Add elements to structure type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001752 DIArray Elements = CTy.getTypeArray();
Devang Patelcf7acb12009-01-16 00:50:53 +00001753
1754 // A forward struct declared type may not have elements available.
1755 if (Elements.isNull())
1756 break;
1757
Devang Patel30c01372009-01-05 19:55:51 +00001758 // Add elements to structure type.
1759 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1760 DIDescriptor Element = Elements.getElement(i);
Devang Patelb28de842009-01-17 08:01:33 +00001761 DIE *ElemDie = NULL;
Devang Patelef4bf3b2009-01-15 19:26:23 +00001762 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel245446c2009-01-17 08:05:14 +00001763 ElemDie = CreateSubprogramDIE(DW_Unit,
1764 DISubprogram(Element.getGV()));
Devang Patelb28de842009-01-17 08:01:33 +00001765 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1766 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1767 DIGlobalVariable(Element.getGV()));
Devang Patel5c643892009-01-20 21:02:02 +00001768 else
1769 ElemDie = CreateMemberDIE(DW_Unit,
1770 DIDerivedType(Element.getGV()));
Devang Patel245446c2009-01-17 08:05:14 +00001771 Buffer.AddChild(ElemDie);
Devang Patel30c01372009-01-05 19:55:51 +00001772 }
Devang Patel74193d72009-02-17 22:43:44 +00001773 unsigned RLang = CTy.getRunTimeLang();
1774 if (RLang)
1775 AddUInt(&Buffer, DW_AT_APPLE_runtime_class, DW_FORM_data1, RLang);
Devang Patel30c01372009-01-05 19:55:51 +00001776 }
1777 break;
1778 default:
1779 break;
1780 }
1781
1782 // Add name if not anonymous or intermediate type.
1783 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1784
Devang Patele34e0882009-01-27 00:45:04 +00001785 if (Tag == DW_TAG_enumeration_type || Tag == DW_TAG_structure_type
1786 || Tag == DW_TAG_union_type) {
1787 // Add size if non-zero (derived types might be zero-sized.)
1788 if (Size)
1789 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1790 else {
1791 // Add zero size if it is not a forward declaration.
1792 if (CTy.isForwardDecl())
1793 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1794 else
1795 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1796 }
1797
1798 // Add source line info if available.
1799 if (!CTy.isForwardDecl())
1800 AddSourceLine(&Buffer, &CTy);
Devang Patel30c01372009-01-05 19:55:51 +00001801 }
Devang Patel30c01372009-01-05 19:55:51 +00001802 }
1803
Bill Wendling824a8bf2009-02-03 21:17:20 +00001804 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1805 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Devang Patelef4bf3b2009-01-15 19:26:23 +00001806 int64_t L = SR.getLo();
1807 int64_t H = SR.getHi();
Devang Patel6fb54132009-01-05 18:33:01 +00001808 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1809 if (L != H) {
1810 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1811 if (L)
Devang Patel245446c2009-01-17 08:05:14 +00001812 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1813 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
Devang Patel6fb54132009-01-05 18:33:01 +00001814 }
1815 Buffer.AddChild(DW_Subrange);
1816 }
1817
1818 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1819 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1820 DICompositeType *CTy) {
1821 Buffer.setTag(DW_TAG_array_type);
1822 if (CTy->getTag() == DW_TAG_vector_type)
1823 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1824
Devang Patel6ab30e52009-01-28 21:08:20 +00001825 // Emit derived type.
1826 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Devang Patel6fb54132009-01-05 18:33:01 +00001827 DIArray Elements = CTy->getTypeArray();
Devang Patel6fb54132009-01-05 18:33:01 +00001828
1829 // Construct an anonymous type for index type.
1830 DIE IdxBuffer(DW_TAG_base_type);
1831 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1832 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1833 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1834
1835 // Add subranges to array type.
1836 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel30c01372009-01-05 19:55:51 +00001837 DIDescriptor Element = Elements.getElement(i);
Devang Patelef4bf3b2009-01-15 19:26:23 +00001838 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1839 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
Devang Patel6fb54132009-01-05 18:33:01 +00001840 }
1841 }
1842
Bill Wendling824a8bf2009-02-03 21:17:20 +00001843 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3798f492009-01-20 18:35:14 +00001844 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Devang Patela566e812009-01-05 18:38:38 +00001845
1846 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1847 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1848 int64_t Value = ETy->getEnumValue();
1849 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
Devang Patel3798f492009-01-20 18:35:14 +00001850 return Enumerator;
Devang Patela566e812009-01-05 18:38:38 +00001851 }
Devang Patel6fb54132009-01-05 18:33:01 +00001852
Devang Patelb28de842009-01-17 08:01:33 +00001853 /// CreateGlobalVariableDIE - Create new DIE using GV.
Bill Wendling824a8bf2009-02-03 21:17:20 +00001854 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
Devang Patelb28de842009-01-17 08:01:33 +00001855 {
1856 DIE *GVDie = new DIE(DW_TAG_variable);
1857 AddString(GVDie, DW_AT_name, DW_FORM_string, GV.getName());
1858 const std::string &LinkageName = GV.getLinkageName();
Devang Patel526b01d2009-01-05 18:59:44 +00001859 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001860 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1861 AddType(DW_Unit, GVDie, GV.getType());
1862 if (!GV.isLocalToUnit())
1863 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1864 AddSourceLine(GVDie, &GV);
1865 return GVDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001866 }
1867
Devang Patel5c643892009-01-20 21:02:02 +00001868 /// CreateMemberDIE - Create new member DIE.
1869 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1870 DIE *MemberDie = new DIE(DT.getTag());
1871 std::string Name = DT.getName();
1872 if (!Name.empty())
1873 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1874
1875 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1876
1877 AddSourceLine(MemberDie, &DT);
1878
Devang Patelf1f30d42009-02-17 21:23:59 +00001879 uint64_t Size = DT.getSizeInBits();
1880 uint64_t FieldSize = DT.getOriginalTypeSize();
1881
1882 if (Size != FieldSize) {
1883 // Handle bitfield.
1884 AddUInt(MemberDie, DW_AT_byte_size, 0, DT.getOriginalTypeSize() >> 3);
1885 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1886
1887 uint64_t Offset = DT.getOffsetInBits();
1888 uint64_t FieldOffset = Offset;
1889 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1890 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1891 FieldOffset = (HiMark - FieldSize);
1892 Offset -= FieldOffset;
1893 // Maybe we need to work from the other end.
1894 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1895 AddUInt(MemberDie, DW_AT_bit_offset, 0, Offset);
1896 }
Devang Patel5c643892009-01-20 21:02:02 +00001897 DIEBlock *Block = new DIEBlock();
1898 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1899 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1900 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1901
Devang Patel2e7ee192009-01-21 00:08:04 +00001902 if (DT.isProtected())
1903 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_protected);
1904 else if (DT.isPrivate())
1905 AddUInt(MemberDie, DW_AT_accessibility, 0, DW_ACCESS_private);
1906
Devang Patel5c643892009-01-20 21:02:02 +00001907 return MemberDie;
1908 }
1909
Devang Patelb28de842009-01-17 08:01:33 +00001910 /// CreateSubprogramDIE - Create new DIE using SP.
1911 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
Devang Patel245446c2009-01-17 08:05:14 +00001912 const DISubprogram &SP,
1913 bool IsConstructor = false) {
Devang Patelb28de842009-01-17 08:01:33 +00001914 DIE *SPDie = new DIE(DW_TAG_subprogram);
1915 AddString(SPDie, DW_AT_name, DW_FORM_string, SP.getName());
Devang Patelef4bf3b2009-01-15 19:26:23 +00001916 const std::string &LinkageName = SP.getLinkageName();
Devang Patel526b01d2009-01-05 18:59:44 +00001917 if (!LinkageName.empty())
Devang Patelb28de842009-01-17 08:01:33 +00001918 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
Devang Patel245446c2009-01-17 08:05:14 +00001919 LinkageName);
Devang Patelb28de842009-01-17 08:01:33 +00001920 AddSourceLine(SPDie, &SP);
Devang Patel526b01d2009-01-05 18:59:44 +00001921
Devang Patelb28de842009-01-17 08:01:33 +00001922 DICompositeType SPTy = SP.getType();
1923 DIArray Args = SPTy.getTypeArray();
1924
Devang Patel526b01d2009-01-05 18:59:44 +00001925 // Add Return Type.
Devang Patelef4bf3b2009-01-15 19:26:23 +00001926 if (!IsConstructor)
Devang Patelb28de842009-01-17 08:01:33 +00001927 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
Devang Patel922d1592009-01-30 01:21:46 +00001928
Devang Patelace2cf62009-02-02 17:51:41 +00001929 if (!SP.isDefinition()) {
1930 AddUInt(SPDie, DW_AT_declaration, DW_FORM_flag, 1);
1931 // Add arguments.
1932 // Do not add arguments for subprogram definition. They will be
1933 // handled through RecordVariable.
1934 if (!Args.isNull())
1935 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1936 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1937 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1938 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1939 SPDie->AddChild(Arg);
1940 }
1941 }
Devang Patel922d1592009-01-30 01:21:46 +00001942
Devang Patelef4bf3b2009-01-15 19:26:23 +00001943 if (!SP.isLocalToUnit())
Devang Patel922d1592009-01-30 01:21:46 +00001944 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
Devang Patelb28de842009-01-17 08:01:33 +00001945 return SPDie;
Devang Patel526b01d2009-01-05 18:59:44 +00001946 }
1947
Devang Patelb28de842009-01-17 08:01:33 +00001948 /// FindCompileUnit - Get the compile unit for the given descriptor.
1949 ///
Devang Patel5f244e32009-01-05 22:35:52 +00001950 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
1951 CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
1952 assert(DW_Unit && "Missing compile unit.");
1953 return DW_Unit;
1954 }
1955
Devang Patel42f6bed2009-01-13 23:54:55 +00001956 /// NewDbgScopeVariable - Create a new scope variable.
Devang Patel4d1709e2009-01-08 02:33:41 +00001957 ///
1958 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1959 // Get the descriptor.
Devang Patel7c8a2772009-01-16 19:28:14 +00001960 const DIVariable &VD = DV->getVariable();
Devang Patel4d1709e2009-01-08 02:33:41 +00001961
1962 // Translate tag to proper Dwarf tag. The result variable is dropped for
1963 // now.
1964 unsigned Tag;
Devang Patel7c8a2772009-01-16 19:28:14 +00001965 switch (VD.getTag()) {
Devang Patel4d1709e2009-01-08 02:33:41 +00001966 case DW_TAG_return_variable: return NULL;
1967 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1968 case DW_TAG_auto_variable: // fall thru
1969 default: Tag = DW_TAG_variable; break;
1970 }
1971
1972 // Define variable debug information entry.
1973 DIE *VariableDie = new DIE(Tag);
Devang Patel7c8a2772009-01-16 19:28:14 +00001974 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD.getName());
Devang Patel4d1709e2009-01-08 02:33:41 +00001975
1976 // Add source line info if available.
Devang Patel7c8a2772009-01-16 19:28:14 +00001977 AddSourceLine(VariableDie, &VD);
Devang Patel4d1709e2009-01-08 02:33:41 +00001978
1979 // Add variable type.
Devang Patel7c8a2772009-01-16 19:28:14 +00001980 AddType(Unit, VariableDie, VD.getType());
Devang Patel4d1709e2009-01-08 02:33:41 +00001981
1982 // Add variable address.
1983 MachineLocation Location;
1984 Location.set(RI->getFrameRegister(*MF),
1985 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1986 AddAddress(VariableDie, DW_AT_location, Location);
1987
1988 return VariableDie;
1989 }
1990
Devang Patel4d1709e2009-01-08 02:33:41 +00001991 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1992 ///
1993 DbgScope *getOrCreateScope(GlobalVariable *V) {
1994 DbgScope *&Slot = DbgScopeMap[V];
1995 if (!Slot) {
1996 // FIXME - breaks down when the context is an inlined function.
1997 DIDescriptor ParentDesc;
Devang Patel2560d922009-01-15 18:25:17 +00001998 DIDescriptor Desc(V);
1999 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
2000 DIBlock Block(V);
2001 ParentDesc = Block.getContext();
Devang Patel4d1709e2009-01-08 02:33:41 +00002002 }
2003 DbgScope *Parent = ParentDesc.isNull() ?
Devang Pateldd49fbb2009-01-10 02:34:18 +00002004 NULL : getOrCreateScope(ParentDesc.getGV());
Devang Patel2560d922009-01-15 18:25:17 +00002005 Slot = new DbgScope(Parent, Desc);
Devang Patel4d1709e2009-01-08 02:33:41 +00002006 if (Parent) {
2007 Parent->AddScope(Slot);
2008 } else if (RootDbgScope) {
2009 // FIXME - Add inlined function scopes to the root so we can delete
2010 // them later. Long term, handle inlined functions properly.
2011 RootDbgScope->AddScope(Slot);
2012 } else {
2013 // First function is top level function.
2014 RootDbgScope = Slot;
2015 }
2016 }
2017 return Slot;
2018 }
2019
2020 /// ConstructDbgScope - Construct the components of a scope.
2021 ///
2022 void ConstructDbgScope(DbgScope *ParentScope,
2023 unsigned ParentStartID, unsigned ParentEndID,
2024 DIE *ParentDie, CompileUnit *Unit) {
2025 // Add variables to scope.
Devang Patel63c22f42009-01-10 02:42:49 +00002026 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
Devang Patel4d1709e2009-01-08 02:33:41 +00002027 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2028 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2029 if (VariableDie) ParentDie->AddChild(VariableDie);
2030 }
2031
2032 // Add nested scopes.
Devang Patel63c22f42009-01-10 02:42:49 +00002033 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
Devang Patel4d1709e2009-01-08 02:33:41 +00002034 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2035 // Define the Scope debug information entry.
2036 DbgScope *Scope = Scopes[j];
2037 // FIXME - Ignore inlined functions for the time being.
2038 if (!Scope->getParent()) continue;
2039
Devang Patelb9224922009-01-12 18:41:00 +00002040 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2041 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
Devang Patel4d1709e2009-01-08 02:33:41 +00002042
2043 // Ignore empty scopes.
2044 if (StartID == EndID && StartID != 0) continue;
2045 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2046
2047 if (StartID == ParentStartID && EndID == ParentEndID) {
2048 // Just add stuff to the parent scope.
2049 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2050 } else {
2051 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2052
2053 // Add the scope bounds.
2054 if (StartID) {
2055 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2056 DWLabel("label", StartID));
2057 } else {
2058 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2059 DWLabel("func_begin", SubprogramCount));
2060 }
2061 if (EndID) {
2062 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2063 DWLabel("label", EndID));
2064 } else {
2065 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2066 DWLabel("func_end", SubprogramCount));
2067 }
2068
2069 // Add the scope contents.
2070 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2071 ParentDie->AddChild(ScopeDie);
2072 }
2073 }
2074 }
2075
2076 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2077 ///
2078 void ConstructRootDbgScope(DbgScope *RootScope) {
2079 // Exit if there is no root scope.
2080 if (!RootScope) return;
Devang Patel2560d922009-01-15 18:25:17 +00002081 DIDescriptor Desc = RootScope->getDesc();
2082 if (Desc.isNull())
2083 return;
Devang Patel4d1709e2009-01-08 02:33:41 +00002084
2085 // Get the subprogram debug information entry.
Devang Patel2560d922009-01-15 18:25:17 +00002086 DISubprogram SPD(Desc.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002087
2088 // Get the compile unit context.
Devang Patel2ae1db52009-01-30 18:20:31 +00002089 CompileUnit *Unit = MainCU;
2090 if (!Unit)
2091 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00002092
2093 // Get the subprogram die.
Devang Patel57ec9ac2009-01-12 22:58:14 +00002094 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel4d1709e2009-01-08 02:33:41 +00002095 assert(SPDie && "Missing subprogram descriptor");
2096
2097 // Add the function bounds.
2098 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2099 DWLabel("func_begin", SubprogramCount));
2100 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2101 DWLabel("func_end", SubprogramCount));
2102 MachineLocation Location(RI->getFrameRegister(*MF));
2103 AddAddress(SPDie, DW_AT_frame_base, Location);
2104
2105 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2106 }
2107
2108 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2109 ///
2110 void ConstructDefaultDbgScope(MachineFunction *MF) {
2111 // Find the correct subprogram descriptor.
2112 std::string SPName = "llvm.dbg.subprograms";
2113 std::vector<GlobalVariable*> Result;
2114 getGlobalVariablesUsing(*M, SPName, Result);
Bill Wendling824a8bf2009-02-03 21:17:20 +00002115
Devang Patel4d1709e2009-01-08 02:33:41 +00002116 for (std::vector<GlobalVariable *>::iterator I = Result.begin(),
2117 E = Result.end(); I != E; ++I) {
Devang Patel7c8a2772009-01-16 19:28:14 +00002118 DISubprogram SPD(*I);
Devang Patel4d1709e2009-01-08 02:33:41 +00002119
Devang Patel7c8a2772009-01-16 19:28:14 +00002120 if (SPD.getName() == MF->getFunction()->getName()) {
Devang Patel4d1709e2009-01-08 02:33:41 +00002121 // Get the compile unit context.
Devang Patel2ae1db52009-01-30 18:20:31 +00002122 CompileUnit *Unit = MainCU;
2123 if (!Unit)
2124 Unit = FindCompileUnit(SPD.getCompileUnit());
Devang Patel4d1709e2009-01-08 02:33:41 +00002125
2126 // Get the subprogram die.
Devang Patel7c8a2772009-01-16 19:28:14 +00002127 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
Devang Patel7f3f1982009-02-18 17:29:38 +00002128 if (!SPDie)
2129 /* A subprogram die may not exist if the corresponding function
2130 does not have any debug info. */
2131 continue;
Devang Patel4d1709e2009-01-08 02:33:41 +00002132
2133 // Add the function bounds.
2134 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2135 DWLabel("func_begin", SubprogramCount));
2136 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2137 DWLabel("func_end", SubprogramCount));
2138
2139 MachineLocation Location(RI->getFrameRegister(*MF));
2140 AddAddress(SPDie, DW_AT_frame_base, Location);
2141 return;
2142 }
2143 }
2144#if 0
2145 // FIXME: This is causing an abort because C++ mangled names are compared
2146 // with their unmangled counterparts. See PR2885. Don't do this assert.
2147 assert(0 && "Couldn't find DIE for machine function!");
2148#endif
2149 }
2150
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002151 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2152 /// tools to recognize the object file contains Dwarf information.
2153 void EmitInitial() {
2154 // Check to see if we already emitted intial headers.
2155 if (didInitial) return;
2156 didInitial = true;
aslc200b112008-08-16 12:57:46 +00002157
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002158 // Dwarf sections base addresses.
2159 if (TAI->doesDwarfRequireFrameSection()) {
2160 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2161 EmitLabel("section_debug_frame", 0);
2162 }
2163 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2164 EmitLabel("section_info", 0);
2165 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2166 EmitLabel("section_abbrev", 0);
2167 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2168 EmitLabel("section_aranges", 0);
Scott Michel79f01f52009-01-26 22:32:51 +00002169 if (TAI->doesSupportMacInfoSection()) {
2170 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2171 EmitLabel("section_macinfo", 0);
2172 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002173 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2174 EmitLabel("section_line", 0);
2175 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2176 EmitLabel("section_loc", 0);
2177 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2178 EmitLabel("section_pubnames", 0);
2179 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2180 EmitLabel("section_str", 0);
2181 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2182 EmitLabel("section_ranges", 0);
2183
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002184 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002185 EmitLabel("text_begin", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002186 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002187 EmitLabel("data_begin", 0);
2188 }
2189
2190 /// EmitDIE - Recusively Emits a debug information entry.
2191 ///
2192 void EmitDIE(DIE *Die) {
2193 // Get the abbreviation for this DIE.
2194 unsigned AbbrevNumber = Die->getAbbrevNumber();
2195 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
aslc200b112008-08-16 12:57:46 +00002196
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002197 Asm->EOL();
2198
2199 // Emit the code (index) for the abbreviation.
2200 Asm->EmitULEB128Bytes(AbbrevNumber);
Evan Cheng0eeed442008-07-01 23:18:29 +00002201
2202 if (VerboseAsm)
2203 Asm->EOL(std::string("Abbrev [" +
2204 utostr(AbbrevNumber) +
2205 "] 0x" + utohexstr(Die->getOffset()) +
2206 ":0x" + utohexstr(Die->getSize()) + " " +
2207 TagString(Abbrev->getTag())));
2208 else
2209 Asm->EOL();
aslc200b112008-08-16 12:57:46 +00002210
Owen Anderson88dd6232008-06-24 21:44:59 +00002211 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2212 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
aslc200b112008-08-16 12:57:46 +00002213
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002214 // Emit the DIE attribute values.
2215 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2216 unsigned Attr = AbbrevData[i].getAttribute();
2217 unsigned Form = AbbrevData[i].getForm();
2218 assert(Form && "Too many attributes for DIE (check abbreviation)");
aslc200b112008-08-16 12:57:46 +00002219
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002220 switch (Attr) {
2221 case DW_AT_sibling: {
2222 Asm->EmitInt32(Die->SiblingOffset());
2223 break;
2224 }
2225 default: {
2226 // Emit an attribute using the defined form.
2227 Values[i]->EmitValue(*this, Form);
2228 break;
2229 }
2230 }
aslc200b112008-08-16 12:57:46 +00002231
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002232 Asm->EOL(AttributeString(Attr));
2233 }
aslc200b112008-08-16 12:57:46 +00002234
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002235 // Emit the DIE children if any.
2236 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2237 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002238
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002239 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2240 EmitDIE(Children[j]);
2241 }
aslc200b112008-08-16 12:57:46 +00002242
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002243 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2244 }
2245 }
2246
2247 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2248 ///
2249 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2250 // Get the children.
2251 const std::vector<DIE *> &Children = Die->getChildren();
aslc200b112008-08-16 12:57:46 +00002252
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002253 // If not last sibling and has children then add sibling offset attribute.
2254 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2255
2256 // Record the abbreviation.
2257 AssignAbbrevNumber(Die->getAbbrev());
aslc200b112008-08-16 12:57:46 +00002258
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002259 // Get the abbreviation for this DIE.
2260 unsigned AbbrevNumber = Die->getAbbrevNumber();
2261 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2262
2263 // Set DIE offset
2264 Die->setOffset(Offset);
aslc200b112008-08-16 12:57:46 +00002265
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002266 // Start the size with the size of abbreviation code.
aslc200b112008-08-16 12:57:46 +00002267 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2268
Owen Anderson88dd6232008-06-24 21:44:59 +00002269 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2270 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002271
2272 // Size the DIE attribute values.
2273 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2274 // Size attribute value.
2275 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2276 }
aslc200b112008-08-16 12:57:46 +00002277
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002278 // Size the DIE children if any.
2279 if (!Children.empty()) {
2280 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2281 "Children flag not set");
aslc200b112008-08-16 12:57:46 +00002282
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002283 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2284 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2285 }
aslc200b112008-08-16 12:57:46 +00002286
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002287 // End of children marker.
2288 Offset += sizeof(int8_t);
2289 }
2290
2291 Die->setSize(Offset - Die->getOffset());
2292 return Offset;
2293 }
2294
2295 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2296 ///
2297 void SizeAndOffsets() {
2298 // Process base compile unit.
Devang Patel2ae1db52009-01-30 18:20:31 +00002299 if (MainCU) {
2300 // Compute size of compile unit header
2301 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2302 sizeof(int16_t) + // DWARF version number
2303 sizeof(int32_t) + // Offset Into Abbrev. Section
2304 sizeof(int8_t); // Pointer Size (in bytes)
2305 SizeAndOffsetDie(MainCU->getDie(), Offset, true);
2306 return;
2307 }
Devang Patel6eae2832009-01-12 23:05:55 +00002308 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2309 CE = DW_CUs.end(); CI != CE; ++CI) {
2310 CompileUnit *Unit = CI->second;
2311 // Compute size of compile unit header
2312 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2313 sizeof(int16_t) + // DWARF version number
2314 sizeof(int32_t) + // Offset Into Abbrev. Section
2315 sizeof(int8_t); // Pointer Size (in bytes)
2316 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2317 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002318 }
2319
2320 /// EmitDebugInfo - Emit the debug info section.
2321 ///
2322 void EmitDebugInfo() {
2323 // Start debug info section.
2324 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
aslc200b112008-08-16 12:57:46 +00002325
Devang Patel6eae2832009-01-12 23:05:55 +00002326 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2327 CE = DW_CUs.end(); CI != CE; ++CI) {
2328 CompileUnit *Unit = CI->second;
Devang Patel2ae1db52009-01-30 18:20:31 +00002329 if (MainCU)
2330 Unit = MainCU;
Devang Patel6eae2832009-01-12 23:05:55 +00002331 DIE *Die = Unit->getDie();
2332 // Emit the compile units header.
2333 EmitLabel("info_begin", Unit->getID());
2334 // Emit size of content not including length itself
2335 unsigned ContentSize = Die->getSize() +
2336 sizeof(int16_t) + // DWARF version number
2337 sizeof(int32_t) + // Offset Into Abbrev. Section
2338 sizeof(int8_t) + // Pointer Size (in bytes)
2339 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2340
2341 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2342 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2343 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2344 Asm->EOL("Offset Into Abbrev. Section");
2345 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2346
2347 EmitDIE(Die);
2348 // FIXME - extra padding for gdb bug.
2349 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2350 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2351 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2352 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2353 EmitLabel("info_end", Unit->getID());
2354
2355 Asm->EOL();
Devang Patel2ae1db52009-01-30 18:20:31 +00002356 if (MainCU)
2357 return;
Devang Patel6eae2832009-01-12 23:05:55 +00002358 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002359 }
2360
2361 /// EmitAbbreviations - Emit the abbreviation section.
2362 ///
2363 void EmitAbbreviations() const {
2364 // Check to see if it is worth the effort.
2365 if (!Abbreviations.empty()) {
2366 // Start the debug abbrev section.
2367 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
aslc200b112008-08-16 12:57:46 +00002368
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002369 EmitLabel("abbrev_begin", 0);
aslc200b112008-08-16 12:57:46 +00002370
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002371 // For each abbrevation.
2372 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2373 // Get abbreviation data
2374 const DIEAbbrev *Abbrev = Abbreviations[i];
aslc200b112008-08-16 12:57:46 +00002375
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002376 // Emit the abbrevations code (base 1 index.)
2377 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2378 Asm->EOL("Abbreviation Code");
aslc200b112008-08-16 12:57:46 +00002379
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002380 // Emit the abbreviations data.
2381 Abbrev->Emit(*this);
aslc200b112008-08-16 12:57:46 +00002382
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002383 Asm->EOL();
2384 }
aslc200b112008-08-16 12:57:46 +00002385
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002386 // Mark end of abbreviations.
2387 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2388
2389 EmitLabel("abbrev_end", 0);
aslc200b112008-08-16 12:57:46 +00002390
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002391 Asm->EOL();
2392 }
2393 }
2394
Bill Wendling1983a2a2008-07-20 00:11:19 +00002395 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2396 /// the line matrix.
aslc200b112008-08-16 12:57:46 +00002397 ///
Bill Wendling1983a2a2008-07-20 00:11:19 +00002398 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2399 // Define last address of section.
2400 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2401 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2402 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2403 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2404
2405 // Mark end of matrix.
2406 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2407 Asm->EmitULEB128Bytes(1); Asm->EOL();
2408 Asm->EmitInt8(1); Asm->EOL();
2409 }
2410
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002411 /// EmitDebugLines - Emit source line information.
2412 ///
2413 void EmitDebugLines() {
Bill Wendling1983a2a2008-07-20 00:11:19 +00002414 // If the target is using .loc/.file, the assembler will be emitting the
2415 // .debug_line table automatically.
2416 if (TAI->hasDotLocAndDotFile())
Dan Gohmanc55b34a2007-09-24 21:43:52 +00002417 return;
2418
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002419 // Minimum line delta, thus ranging from -10..(255-10).
2420 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2421 // Maximum line delta, thus ranging from -10..(255-10).
2422 const int MaxLineDelta = 255 + MinLineDelta;
2423
2424 // Start the dwarf line section.
2425 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
aslc200b112008-08-16 12:57:46 +00002426
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002427 // Construct the section header.
aslc200b112008-08-16 12:57:46 +00002428
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002429 EmitDifference("line_end", 0, "line_begin", 0, true);
2430 Asm->EOL("Length of Source Line Info");
2431 EmitLabel("line_begin", 0);
aslc200b112008-08-16 12:57:46 +00002432
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002433 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
aslc200b112008-08-16 12:57:46 +00002434
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002435 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2436 Asm->EOL("Prolog Length");
2437 EmitLabel("line_prolog_begin", 0);
aslc200b112008-08-16 12:57:46 +00002438
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002439 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2440
2441 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2442
2443 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
aslc200b112008-08-16 12:57:46 +00002444
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002445 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2446
2447 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
aslc200b112008-08-16 12:57:46 +00002448
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002449 // Line number standard opcode encodings argument count
2450 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2451 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2452 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2453 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2454 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2455 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2456 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2457 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2458 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2459
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002460 // Emit directories.
2461 for (unsigned DirectoryID = 1, NDID = Directories.size();
2462 DirectoryID <= NDID; ++DirectoryID) {
2463 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
2464 }
2465 Asm->EmitInt8(0); Asm->EOL("End of directories");
aslc200b112008-08-16 12:57:46 +00002466
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002467 // Emit files.
Devang Patel6ccd57e2009-01-13 00:20:51 +00002468 for (unsigned SourceID = 1, NSID = SrcFiles.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002469 SourceID <= NSID; ++SourceID) {
Devang Patel6ccd57e2009-01-13 00:20:51 +00002470 const SrcFileInfo &SourceFile = SrcFiles[SourceID];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002471 Asm->EmitString(SourceFile.getName());
2472 Asm->EOL("Source");
2473 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2474 Asm->EOL("Directory #");
2475 Asm->EmitULEB128Bytes(0);
2476 Asm->EOL("Mod date");
2477 Asm->EmitULEB128Bytes(0);
2478 Asm->EOL("File size");
2479 }
2480 Asm->EmitInt8(0); Asm->EOL("End of files");
aslc200b112008-08-16 12:57:46 +00002481
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002482 EmitLabel("line_prolog_end", 0);
aslc200b112008-08-16 12:57:46 +00002483
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002484 // A sequence for each text section.
Bill Wendling1983a2a2008-07-20 00:11:19 +00002485 unsigned SecSrcLinesSize = SectionSourceLines.size();
2486
2487 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002488 // Isolate current sections line info.
Devang Patel35a078f2009-01-12 22:54:42 +00002489 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
Evan Cheng0eeed442008-07-01 23:18:29 +00002490
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002491 if (VerboseAsm) {
2492 const Section* S = SectionMap[j + 1];
2493 Asm->EOL(std::string("Section ") + S->getName());
2494 } else
Evan Cheng0eeed442008-07-01 23:18:29 +00002495 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002496
2497 // Dwarf assumes we start with first line of first source file.
2498 unsigned Source = 1;
2499 unsigned Line = 1;
aslc200b112008-08-16 12:57:46 +00002500
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002501 // Construct rows of the address, source, line, column matrix.
2502 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Devang Patel35a078f2009-01-12 22:54:42 +00002503 const SrcLineInfo &LineInfo = LineInfos[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002504 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2505 if (!LabelID) continue;
aslc200b112008-08-16 12:57:46 +00002506
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002507 unsigned SourceID = LineInfo.getSourceID();
Devang Patel6ccd57e2009-01-13 00:20:51 +00002508 const SrcFileInfo &SourceFile = SrcFiles[SourceID];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002509 unsigned DirectoryID = SourceFile.getDirectoryID();
Evan Cheng0eeed442008-07-01 23:18:29 +00002510 if (VerboseAsm)
2511 Asm->EOL(Directories[DirectoryID]
2512 + SourceFile.getName()
2513 + ":"
2514 + utostr_32(LineInfo.getLine()));
2515 else
2516 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002517
2518 // Define the line address.
2519 Asm->EmitInt8(0); Asm->EOL("Extended Op");
Dan Gohmancfb72b22007-09-27 23:12:31 +00002520 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002521 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2522 EmitReference("label", LabelID); Asm->EOL("Location label");
aslc200b112008-08-16 12:57:46 +00002523
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002524 // If change of source, then switch to the new source.
2525 if (Source != LineInfo.getSourceID()) {
2526 Source = LineInfo.getSourceID();
2527 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2528 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2529 }
aslc200b112008-08-16 12:57:46 +00002530
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002531 // If change of line.
2532 if (Line != LineInfo.getLine()) {
2533 // Determine offset.
2534 int Offset = LineInfo.getLine() - Line;
2535 int Delta = Offset - MinLineDelta;
aslc200b112008-08-16 12:57:46 +00002536
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002537 // Update line.
2538 Line = LineInfo.getLine();
aslc200b112008-08-16 12:57:46 +00002539
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002540 // If delta is small enough and in range...
2541 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2542 // ... then use fast opcode.
2543 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2544 } else {
2545 // ... otherwise use long hand.
2546 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2547 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2548 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2549 }
2550 } else {
2551 // Copy the previous row (different address or source)
2552 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2553 }
2554 }
2555
Bill Wendling1983a2a2008-07-20 00:11:19 +00002556 EmitEndOfLineMatrix(j + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002557 }
Bill Wendling1983a2a2008-07-20 00:11:19 +00002558
2559 if (SecSrcLinesSize == 0)
2560 // Because we're emitting a debug_line section, we still need a line
2561 // table. The linker and friends expect it to exist. If there's nothing to
2562 // put into it, emit an empty table.
2563 EmitEndOfLineMatrix(1);
aslc200b112008-08-16 12:57:46 +00002564
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002565 EmitLabel("line_end", 0);
aslc200b112008-08-16 12:57:46 +00002566
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002567 Asm->EOL();
2568 }
aslc200b112008-08-16 12:57:46 +00002569
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002570 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2571 ///
2572 void EmitCommonDebugFrame() {
2573 if (!TAI->doesDwarfRequireFrameSection())
2574 return;
2575
2576 int stackGrowth =
2577 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2578 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00002579 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002580
2581 // Start the dwarf frame section.
2582 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2583
2584 EmitLabel("debug_frame_common", 0);
2585 EmitDifference("debug_frame_common_end", 0,
2586 "debug_frame_common_begin", 0, true);
2587 Asm->EOL("Length of Common Information Entry");
2588
2589 EmitLabel("debug_frame_common_begin", 0);
2590 Asm->EmitInt32((int)DW_CIE_ID);
2591 Asm->EOL("CIE Identifier Tag");
2592 Asm->EmitInt8(DW_CIE_VERSION);
2593 Asm->EOL("CIE Version");
2594 Asm->EmitString("");
2595 Asm->EOL("CIE Augmentation");
2596 Asm->EmitULEB128Bytes(1);
2597 Asm->EOL("CIE Code Alignment Factor");
2598 Asm->EmitSLEB128Bytes(stackGrowth);
aslc200b112008-08-16 12:57:46 +00002599 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00002600 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002601 Asm->EOL("CIE RA Column");
aslc200b112008-08-16 12:57:46 +00002602
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002603 std::vector<MachineMove> Moves;
2604 RI->getInitialFrameState(Moves);
2605
Dale Johannesenf5a11532007-11-13 19:13:01 +00002606 EmitFrameMoves(NULL, 0, Moves, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002607
Evan Cheng7e7d1942008-02-29 19:36:59 +00002608 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002609 EmitLabel("debug_frame_common_end", 0);
aslc200b112008-08-16 12:57:46 +00002610
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002611 Asm->EOL();
2612 }
2613
2614 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2615 /// section.
2616 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2617 if (!TAI->doesDwarfRequireFrameSection())
2618 return;
aslc200b112008-08-16 12:57:46 +00002619
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620 // Start the dwarf frame section.
2621 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
aslc200b112008-08-16 12:57:46 +00002622
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002623 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2624 "debug_frame_begin", DebugFrameInfo.Number, true);
2625 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00002626
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002627 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2628
2629 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2630 0, 0, true, false);
2631 Asm->EOL("FDE CIE offset");
2632
2633 EmitReference("func_begin", DebugFrameInfo.Number);
2634 Asm->EOL("FDE initial location");
2635 EmitDifference("func_end", DebugFrameInfo.Number,
2636 "func_begin", DebugFrameInfo.Number);
2637 Asm->EOL("FDE address range");
aslc200b112008-08-16 12:57:46 +00002638
Devang Patelb28de842009-01-17 08:01:33 +00002639 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00002640 false);
aslc200b112008-08-16 12:57:46 +00002641
Evan Cheng7e7d1942008-02-29 19:36:59 +00002642 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002643 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2644
2645 Asm->EOL();
2646 }
2647
2648 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2649 ///
2650 void EmitDebugPubNames() {
2651 // Start the dwarf pubnames section.
2652 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
aslc200b112008-08-16 12:57:46 +00002653
Devang Patel6eae2832009-01-12 23:05:55 +00002654 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2655 CE = DW_CUs.end(); CI != CE; ++CI) {
2656 CompileUnit *Unit = CI->second;
Devang Patel2ae1db52009-01-30 18:20:31 +00002657 if (MainCU)
2658 Unit = MainCU;
aslc200b112008-08-16 12:57:46 +00002659
Devang Patel6eae2832009-01-12 23:05:55 +00002660 EmitDifference("pubnames_end", Unit->getID(),
2661 "pubnames_begin", Unit->getID(), true);
2662 Asm->EOL("Length of Public Names Info");
2663
2664 EmitLabel("pubnames_begin", Unit->getID());
2665
2666 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2667
2668 EmitSectionOffset("info_begin", "section_info",
2669 Unit->getID(), 0, true, false);
2670 Asm->EOL("Offset of Compilation Unit Info");
2671
Devang Patelb28de842009-01-17 08:01:33 +00002672 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
Devang Patel245446c2009-01-17 08:05:14 +00002673 true);
Devang Patel6eae2832009-01-12 23:05:55 +00002674 Asm->EOL("Compilation Unit Length");
2675
2676 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2677
2678 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2679 GE = Globals.end();
2680 GI != GE; ++GI) {
2681 const std::string &Name = GI->first;
2682 DIE * Entity = GI->second;
2683
2684 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2685 Asm->EmitString(Name); Asm->EOL("External Name");
2686 }
2687
2688 Asm->EmitInt32(0); Asm->EOL("End Mark");
2689 EmitLabel("pubnames_end", Unit->getID());
2690
2691 Asm->EOL();
Devang Patel2ae1db52009-01-30 18:20:31 +00002692 if (MainCU)
2693 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002694 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002695 }
2696
2697 /// EmitDebugStr - Emit visible names into a debug str section.
2698 ///
2699 void EmitDebugStr() {
2700 // Check to see if it is worth the effort.
2701 if (!StringPool.empty()) {
2702 // Start the dwarf str section.
2703 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
aslc200b112008-08-16 12:57:46 +00002704
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002705 // For each of strings in the string pool.
2706 for (unsigned StringID = 1, N = StringPool.size();
2707 StringID <= N; ++StringID) {
2708 // Emit a label for reference from debug information entries.
2709 EmitLabel("string", StringID);
2710 // Emit the string itself.
2711 const std::string &String = StringPool[StringID];
2712 Asm->EmitString(String); Asm->EOL();
2713 }
aslc200b112008-08-16 12:57:46 +00002714
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002715 Asm->EOL();
2716 }
2717 }
2718
2719 /// EmitDebugLoc - Emit visible names into a debug loc section.
2720 ///
2721 void EmitDebugLoc() {
2722 // Start the dwarf loc section.
2723 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
aslc200b112008-08-16 12:57:46 +00002724
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002725 Asm->EOL();
2726 }
2727
2728 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2729 ///
2730 void EmitDebugARanges() {
2731 // Start the dwarf aranges section.
2732 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
aslc200b112008-08-16 12:57:46 +00002733
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002734 // FIXME - Mock up
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002735#if 0
aslc200b112008-08-16 12:57:46 +00002736 CompileUnit *Unit = GetBaseCompileUnit();
2737
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002738 // Don't include size of length
2739 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
aslc200b112008-08-16 12:57:46 +00002740
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002741 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
aslc200b112008-08-16 12:57:46 +00002742
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002743 EmitReference("info_begin", Unit->getID());
2744 Asm->EOL("Offset of Compilation Unit Info");
2745
Dan Gohmancfb72b22007-09-27 23:12:31 +00002746 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002747
2748 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2749
2750 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2751 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2752
2753 // Range 1
2754 EmitReference("text_begin", 0); Asm->EOL("Address");
2755 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2756
2757 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2758 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00002759#endif
aslc200b112008-08-16 12:57:46 +00002760
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002761 Asm->EOL();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002762 }
2763
2764 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2765 ///
2766 void EmitDebugRanges() {
2767 // Start the dwarf ranges section.
2768 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
aslc200b112008-08-16 12:57:46 +00002769
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002770 Asm->EOL();
2771 }
2772
2773 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2774 ///
2775 void EmitDebugMacInfo() {
Scott Michel79f01f52009-01-26 22:32:51 +00002776 if (TAI->doesSupportMacInfoSection()) {
2777 // Start the dwarf macinfo section.
2778 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
aslc200b112008-08-16 12:57:46 +00002779
Scott Michel79f01f52009-01-26 22:32:51 +00002780 Asm->EOL();
2781 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002782 }
2783
Devang Patel289f2362009-01-05 23:11:11 +00002784 /// ConstructCompileUnits - Create a compile unit DIEs.
Devang Patelb3907da2009-01-05 23:03:32 +00002785 void ConstructCompileUnits() {
2786 std::string CUName = "llvm.dbg.compile_units";
2787 std::vector<GlobalVariable*> Result;
2788 getGlobalVariablesUsing(*M, CUName, Result);
2789 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
2790 RE = Result.end(); RI != RE; ++RI) {
Devang Patel7c8a2772009-01-16 19:28:14 +00002791 DICompileUnit DIUnit(*RI);
2792 unsigned ID = RecordSource(DIUnit.getDirectory(),
2793 DIUnit.getFilename());
Devang Patelb3907da2009-01-05 23:03:32 +00002794
2795 DIE *Die = new DIE(DW_TAG_compile_unit);
2796 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2797 DWLabel("section_line", 0), DWLabel("section_line", 0),
2798 false);
Devang Patel7c8a2772009-01-16 19:28:14 +00002799 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer());
2800 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
2801 AddString(Die, DW_AT_name, DW_FORM_string, DIUnit.getFilename());
2802 if (!DIUnit.getDirectory().empty())
2803 AddString(Die, DW_AT_comp_dir, DW_FORM_string, DIUnit.getDirectory());
Devang Patela880b1e2009-01-23 22:33:47 +00002804 if (DIUnit.isOptimized())
2805 AddUInt(Die, DW_AT_APPLE_optimized, DW_FORM_flag, 1);
2806 const std::string &Flags = DIUnit.getFlags();
2807 if (!Flags.empty())
2808 AddString(Die, DW_AT_APPLE_flags, DW_FORM_string, Flags);
Devang Patel74193d72009-02-17 22:43:44 +00002809 unsigned RVer = DIUnit.getRunTimeVersion();
2810 if (RVer)
2811 AddUInt(Die, DW_AT_APPLE_major_runtime_vers, DW_FORM_data1, RVer);
Devang Patelb3907da2009-01-05 23:03:32 +00002812
2813 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel2ae1db52009-01-30 18:20:31 +00002814 if (DIUnit.isMain()) {
Bill Wendling6baa18d2009-02-03 21:38:21 +00002815 assert(!MainCU && "Multiple main compile units are found!");
Devang Patel2ae1db52009-01-30 18:20:31 +00002816 MainCU = Unit;
2817 }
Devang Patel7c8a2772009-01-16 19:28:14 +00002818 DW_CUs[DIUnit.getGV()] = Unit;
Devang Patelb3907da2009-01-05 23:03:32 +00002819 }
2820 }
2821
Devang Patel289f2362009-01-05 23:11:11 +00002822 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
2823 /// visible global variables.
2824 void ConstructGlobalVariableDIEs() {
2825 std::string GVName = "llvm.dbg.global_variables";
2826 std::vector<GlobalVariable*> Result;
2827 getGlobalVariablesUsing(*M, GVName, Result);
2828 for (std::vector<GlobalVariable *>::iterator GVI = Result.begin(),
2829 GVE = Result.end(); GVI != GVE; ++GVI) {
Devang Patel7c8a2772009-01-16 19:28:14 +00002830 DIGlobalVariable DI_GV(*GVI);
Devang Patel2ae1db52009-01-30 18:20:31 +00002831 CompileUnit *DW_Unit = MainCU;
2832 if (!DW_Unit)
2833 DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
Devang Patel289f2362009-01-05 23:11:11 +00002834
2835 // Check for pre-existence.
Devang Patel7c8a2772009-01-16 19:28:14 +00002836 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
Devang Patel289f2362009-01-05 23:11:11 +00002837 if (Slot) continue;
2838
Devang Patelb28de842009-01-17 08:01:33 +00002839 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
Devang Patel289f2362009-01-05 23:11:11 +00002840
2841 // Add address.
2842 DIEBlock *Block = new DIEBlock();
2843 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2844 AddObjectLabel(Block, 0, DW_FORM_udata,
Devang Patel2072db82009-01-20 18:55:39 +00002845 Asm->getGlobalLinkName(DI_GV.getGlobal()));
Devang Patel289f2362009-01-05 23:11:11 +00002846 AddBlock(VariableDie, DW_AT_location, 0, Block);
2847
2848 //Add to map.
2849 Slot = VariableDie;
2850
2851 //Add to context owner.
2852 DW_Unit->getDie()->AddChild(VariableDie);
2853
2854 //Expose as global. FIXME - need to check external flag.
Devang Patel7c8a2772009-01-16 19:28:14 +00002855 DW_Unit->AddGlobal(DI_GV.getName(), VariableDie);
Devang Patel289f2362009-01-05 23:11:11 +00002856 }
2857 }
2858
Devang Patele6caf012009-01-05 23:21:35 +00002859 /// ConstructSubprograms - Create DIEs for each of the externally visible
2860 /// subprograms.
2861 void ConstructSubprograms() {
2862
2863 std::string SPName = "llvm.dbg.subprograms";
2864 std::vector<GlobalVariable*> Result;
2865 getGlobalVariablesUsing(*M, SPName, Result);
2866 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
2867 RE = Result.end(); RI != RE; ++RI) {
2868
Devang Patel7c8a2772009-01-16 19:28:14 +00002869 DISubprogram SP(*RI);
Devang Patel2ae1db52009-01-30 18:20:31 +00002870 CompileUnit *Unit = MainCU;
2871 if (!Unit)
2872 Unit = FindCompileUnit(SP.getCompileUnit());
Devang Patele6caf012009-01-05 23:21:35 +00002873
Devang Patelb28de842009-01-17 08:01:33 +00002874 // Check for pre-existence.
Devang Patel7c8a2772009-01-16 19:28:14 +00002875 DIE *&Slot = Unit->getDieMapSlotFor(SP.getGV());
Devang Patele6caf012009-01-05 23:21:35 +00002876 if (Slot) continue;
2877
Devang Patelace2cf62009-02-02 17:51:41 +00002878 if (!SP.isDefinition())
2879 // This is a method declaration which will be handled while
2880 // constructing class type.
2881 continue;
2882
Devang Patelb28de842009-01-17 08:01:33 +00002883 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
Devang Patele6caf012009-01-05 23:21:35 +00002884
Devang Patele6caf012009-01-05 23:21:35 +00002885 //Add to map.
2886 Slot = SubprogramDie;
2887 //Add to context owner.
2888 Unit->getDie()->AddChild(SubprogramDie);
2889 //Expose as global.
Devang Patel7c8a2772009-01-16 19:28:14 +00002890 Unit->AddGlobal(SP.getName(), SubprogramDie);
Devang Patele6caf012009-01-05 23:21:35 +00002891 }
2892 }
2893
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002894public:
2895 //===--------------------------------------------------------------------===//
2896 // Main entry points.
2897 //
Owen Anderson847b99b2008-08-21 00:14:44 +00002898 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00002899 : Dwarf(OS, A, T, "dbg")
Devang Patel2ae1db52009-01-30 18:20:31 +00002900 , MainCU(NULL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002901 , AbbreviationsSet(InitAbbreviationsSetSize)
2902 , Abbreviations()
2903 , ValuesSet(InitValuesSetSize)
2904 , Values()
2905 , StringPool()
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002906 , SectionMap()
2907 , SectionSourceLines()
2908 , didInitial(false)
2909 , shouldEmit(false)
Devang Patel4d1709e2009-01-08 02:33:41 +00002910 , RootDbgScope(NULL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002911 {
2912 }
2913 virtual ~DwarfDebug() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002914 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2915 delete Values[j];
2916 }
2917
Devang Patel9304b382009-01-06 21:07:30 +00002918 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
2919 /// This is inovked by the target AsmPrinter.
Devang Patel91d27b02009-01-12 23:09:42 +00002920 void SetDebugInfo(MachineModuleInfo *mmi) {
Bill Wendling6baa18d2009-02-03 21:38:21 +00002921 // Create all the compile unit DIEs.
2922 ConstructCompileUnits();
Devang Patel91d27b02009-01-12 23:09:42 +00002923
Bill Wendling6baa18d2009-02-03 21:38:21 +00002924 if (DW_CUs.empty())
2925 return;
Devang Patel91d27b02009-01-12 23:09:42 +00002926
Bill Wendling6baa18d2009-02-03 21:38:21 +00002927 MMI = mmi;
2928 shouldEmit = true;
2929 MMI->setDebugInfoAvailability(true);
Devang Patel9304b382009-01-06 21:07:30 +00002930
Bill Wendling6baa18d2009-02-03 21:38:21 +00002931 // Create DIEs for each of the externally visible global variables.
2932 ConstructGlobalVariableDIEs();
Devang Patel9304b382009-01-06 21:07:30 +00002933
Bill Wendling6baa18d2009-02-03 21:38:21 +00002934 // Create DIEs for each of the externally visible subprograms.
2935 ConstructSubprograms();
Devang Patel9304b382009-01-06 21:07:30 +00002936
Bill Wendling6baa18d2009-02-03 21:38:21 +00002937 // Prime section data.
2938 SectionMap.insert(TAI->getTextSection());
Devang Patel9304b382009-01-06 21:07:30 +00002939
Bill Wendling6baa18d2009-02-03 21:38:21 +00002940 // Print out .file directives to specify files for .loc directives. These
2941 // are printed out early so that they precede any .loc directives.
2942 if (TAI->hasDotLocAndDotFile()) {
2943 for (unsigned i = 1, e = SrcFiles.size(); i <= e; ++i) {
2944 sys::Path FullPath(Directories[SrcFiles[i].getDirectoryID()]);
2945 bool AppendOk = FullPath.appendComponent(SrcFiles[i].getName());
2946 assert(AppendOk && "Could not append filename to directory!");
2947 AppendOk = false;
2948 Asm->EmitFile(i, FullPath.toString());
2949 Asm->EOL();
Devang Patel9304b382009-01-06 21:07:30 +00002950 }
Bill Wendling6baa18d2009-02-03 21:38:21 +00002951 }
Devang Patel9304b382009-01-06 21:07:30 +00002952
Bill Wendling6baa18d2009-02-03 21:38:21 +00002953 // Emit initial sections
2954 EmitInitial();
Devang Patel9304b382009-01-06 21:07:30 +00002955 }
2956
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002957 /// BeginModule - Emit all Dwarf sections that should come prior to the
2958 /// content.
2959 void BeginModule(Module *M) {
2960 this->M = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002961 }
2962
2963 /// EndModule - Emit all Dwarf sections that should come after the content.
2964 ///
2965 void EndModule() {
2966 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00002967
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002968 // Standard sections final addresses.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002969 Asm->SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002970 EmitLabel("text_end", 0);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +00002971 Asm->SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002972 EmitLabel("data_end", 0);
aslc200b112008-08-16 12:57:46 +00002973
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002974 // End text sections.
2975 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Anton Korobeynikov55b94962008-09-24 22:15:21 +00002976 Asm->SwitchToSection(SectionMap[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002977 EmitLabel("section_end", i);
2978 }
2979
2980 // Emit common frame information.
2981 EmitCommonDebugFrame();
2982
2983 // Emit function debug frame information
2984 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2985 E = DebugFrames.end(); I != E; ++I)
2986 EmitFunctionDebugFrame(*I);
2987
2988 // Compute DIE offsets and sizes.
2989 SizeAndOffsets();
aslc200b112008-08-16 12:57:46 +00002990
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002991 // Emit all the DIEs into a debug info section
2992 EmitDebugInfo();
aslc200b112008-08-16 12:57:46 +00002993
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002994 // Corresponding abbreviations into a abbrev section.
2995 EmitAbbreviations();
aslc200b112008-08-16 12:57:46 +00002996
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002997 // Emit source line correspondence into a debug line section.
2998 EmitDebugLines();
aslc200b112008-08-16 12:57:46 +00002999
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003000 // Emit info into a debug pubnames section.
3001 EmitDebugPubNames();
aslc200b112008-08-16 12:57:46 +00003002
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003003 // Emit info into a debug str section.
3004 EmitDebugStr();
aslc200b112008-08-16 12:57:46 +00003005
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003006 // Emit info into a debug loc section.
3007 EmitDebugLoc();
aslc200b112008-08-16 12:57:46 +00003008
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003009 // Emit info into a debug aranges section.
3010 EmitDebugARanges();
aslc200b112008-08-16 12:57:46 +00003011
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003012 // Emit info into a debug ranges section.
3013 EmitDebugRanges();
aslc200b112008-08-16 12:57:46 +00003014
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003015 // Emit info into a debug macinfo section.
3016 EmitDebugMacInfo();
3017 }
3018
aslc200b112008-08-16 12:57:46 +00003019 /// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003020 /// emitted immediately after the function entry point.
3021 void BeginFunction(MachineFunction *MF) {
3022 this->MF = MF;
aslc200b112008-08-16 12:57:46 +00003023
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003024 if (!ShouldEmitDwarf()) return;
3025
3026 // Begin accumulating function debug information.
3027 MMI->BeginFunction(MF);
aslc200b112008-08-16 12:57:46 +00003028
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003029 // Assumes in correct section after the entry point.
3030 EmitLabel("func_begin", ++SubprogramCount);
Evan Chenga53c40a2008-02-01 09:10:45 +00003031
3032 // Emit label for the implicitly defined dbg.stoppoint at the start of
3033 // the function.
Devang Patel35a078f2009-01-12 22:54:42 +00003034 if (!Lines.empty()) {
3035 const SrcLineInfo &LineInfo = Lines[0];
Andrew Lenharth42f91402008-04-03 17:37:43 +00003036 Asm->printLabel(LineInfo.getLabelID());
3037 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003038 }
aslc200b112008-08-16 12:57:46 +00003039
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003040 /// EndFunction - Gather and emit post-function debug information.
3041 ///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003042 void EndFunction(MachineFunction *MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003043 if (!ShouldEmitDwarf()) return;
aslc200b112008-08-16 12:57:46 +00003044
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003045 // Define end label for subprogram.
3046 EmitLabel("func_end", SubprogramCount);
aslc200b112008-08-16 12:57:46 +00003047
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003048 // Get function line info.
Devang Patel35a078f2009-01-12 22:54:42 +00003049 if (!Lines.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003050 // Get section line info.
Anton Korobeynikov55b94962008-09-24 22:15:21 +00003051 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003052 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
Devang Patel35a078f2009-01-12 22:54:42 +00003053 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003054 // Append the function info to section info.
3055 SectionLineInfos.insert(SectionLineInfos.end(),
Devang Patel35a078f2009-01-12 22:54:42 +00003056 Lines.begin(), Lines.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003057 }
aslc200b112008-08-16 12:57:46 +00003058
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003059 // Construct scopes for subprogram.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003060 if (RootDbgScope)
3061 ConstructRootDbgScope(RootDbgScope);
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00003062 else
3063 // FIXME: This is wrong. We are essentially getting past a problem with
3064 // debug information not being able to handle unreachable blocks that have
3065 // debug information in them. In particular, those unreachable blocks that
3066 // have "region end" info in them. That situation results in the "root
3067 // scope" not being created. If that's the case, then emit a "default"
3068 // scope, i.e., one that encompasses the whole function. This isn't
3069 // desirable. And a better way of handling this (and all of the debugging
3070 // information) needs to be explored.
Devang Patel6ccd57e2009-01-13 00:20:51 +00003071 ConstructDefaultDbgScope(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003072
3073 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3074 MMI->getFrameMoves()));
Devang Patela4162952009-01-12 18:48:36 +00003075
3076 // Clear debug info
3077 if (RootDbgScope) {
3078 delete RootDbgScope;
3079 DbgScopeMap.clear();
3080 RootDbgScope = NULL;
3081 }
3082 Lines.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003083 }
Devang Patelcb59fd42009-01-12 19:17:34 +00003084
3085public:
3086
Devang Patel2da0cc42009-01-15 23:41:32 +00003087 /// ValidDebugInfo - Return true if V represents valid debug info value.
3088 bool ValidDebugInfo(Value *V) {
Devang Patel4d92dded2009-01-16 01:49:46 +00003089
Devang Patel208098b2009-01-19 23:21:49 +00003090 if (!V)
3091 return false;
3092
Devang Patel4d92dded2009-01-16 01:49:46 +00003093 if (!shouldEmit)
3094 return false;
3095
Devang Patel2da0cc42009-01-15 23:41:32 +00003096 GlobalVariable *GV = getGlobalVariable(V);
3097 if (!GV)
3098 return false;
3099
3100 if (GV->getLinkage() != GlobalValue::InternalLinkage
3101 && GV->getLinkage() != GlobalValue::LinkOnceLinkage)
3102 return false;
3103
3104 DIDescriptor DI(GV);
3105 // Check current version. Allow Version6 for now.
3106 unsigned Version = DI.getVersion();
Devang Patelb49710a2009-01-20 19:22:03 +00003107 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
Devang Patel2da0cc42009-01-15 23:41:32 +00003108 return false;
3109
Devang Patel208098b2009-01-19 23:21:49 +00003110 unsigned Tag = DI.getTag();
3111 switch (Tag) {
3112 case DW_TAG_variable:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003113 assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003114 break;
3115 case DW_TAG_compile_unit:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003116 assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003117 break;
3118 case DW_TAG_subprogram:
Bill Wendling6baa18d2009-02-03 21:38:21 +00003119 assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
Devang Patel208098b2009-01-19 23:21:49 +00003120 break;
3121 default:
3122 break;
3123 }
3124
Devang Patel2da0cc42009-01-15 23:41:32 +00003125 return true;
3126 }
3127
Devang Patelcb59fd42009-01-12 19:17:34 +00003128 /// RecordSourceLine - Records location information and associates it with a
3129 /// label. Returns a unique label ID used to generate a label and provide
3130 /// correspondence to the source line list.
3131 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
3132 CompileUnit *Unit = DW_CUs[V];
Bill Wendling6baa18d2009-02-03 21:38:21 +00003133 assert(Unit && "Unable to find CompileUnit");
Devang Patelcb59fd42009-01-12 19:17:34 +00003134 unsigned ID = MMI->NextLabelID();
3135 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
3136 return ID;
3137 }
3138
3139 /// RecordSourceLine - Records location information and associates it with a
3140 /// label. Returns a unique label ID used to generate a label and provide
3141 /// correspondence to the source line list.
3142 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
3143 unsigned ID = MMI->NextLabelID();
3144 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
3145 return ID;
3146 }
3147
3148 unsigned getRecordSourceLineCount() {
3149 return Lines.size();
3150 }
3151
3152 /// RecordSource - Register a source file with debug info. Returns an source
3153 /// ID.
3154 unsigned RecordSource(const std::string &Directory,
3155 const std::string &File) {
3156 unsigned DID = Directories.insert(Directory);
3157 return SrcFiles.insert(SrcFileInfo(DID,File));
3158 }
3159
3160 /// RecordRegionStart - Indicate the start of a region.
3161 ///
3162 unsigned RecordRegionStart(GlobalVariable *V) {
3163 DbgScope *Scope = getOrCreateScope(V);
3164 unsigned ID = MMI->NextLabelID();
3165 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
3166 return ID;
3167 }
3168
3169 /// RecordRegionEnd - Indicate the end of a region.
3170 ///
3171 unsigned RecordRegionEnd(GlobalVariable *V) {
3172 DbgScope *Scope = getOrCreateScope(V);
3173 unsigned ID = MMI->NextLabelID();
3174 Scope->setEndLabelID(ID);
3175 return ID;
3176 }
3177
3178 /// RecordVariable - Indicate the declaration of a local variable.
3179 ///
3180 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
Devang Patel2560d922009-01-15 18:25:17 +00003181 DIDescriptor Desc(GV);
3182 DbgScope *Scope = NULL;
3183 if (Desc.getTag() == DW_TAG_variable) {
3184 // GV is a global variable.
3185 DIGlobalVariable DG(GV);
3186 Scope = getOrCreateScope(DG.getContext().getGV());
3187 } else {
3188 // or GV is a local variable.
3189 DIVariable DV(GV);
3190 Scope = getOrCreateScope(DV.getContext().getGV());
3191 }
Bill Wendling6baa18d2009-02-03 21:38:21 +00003192 assert(Scope && "Unable to find variable' scope");
Devang Patel7c8a2772009-01-16 19:28:14 +00003193 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
Devang Patelcb59fd42009-01-12 19:17:34 +00003194 Scope->AddVariable(DV);
3195 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003196};
3197
3198//===----------------------------------------------------------------------===//
aslc200b112008-08-16 12:57:46 +00003199/// DwarfException - Emits Dwarf exception handling directives.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003200///
3201class DwarfException : public Dwarf {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003202 struct FunctionEHFrameInfo {
3203 std::string FnName;
3204 unsigned Number;
3205 unsigned PersonalityIndex;
3206 bool hasCalls;
3207 bool hasLandingPads;
3208 std::vector<MachineMove> Moves;
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003209 const Function * function;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003210
3211 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3212 bool hC, bool hL,
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003213 const std::vector<MachineMove> &M,
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003214 const Function *f):
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003215 FnName(FN), Number(Num), PersonalityIndex(P),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003216 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003217 };
3218
3219 std::vector<FunctionEHFrameInfo> EHFrames;
Dale Johannesen85535762008-04-02 00:25:04 +00003220
3221 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3222 /// be emitted.
3223 bool shouldEmitTable;
3224
3225 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3226 /// should be emitted.
3227 bool shouldEmitMoves;
3228
3229 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3230 /// should be emitted.
3231 bool shouldEmitTableModule;
3232
aslc200b112008-08-16 12:57:46 +00003233 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
Dale Johannesen85535762008-04-02 00:25:04 +00003234 /// should be emitted.
3235 bool shouldEmitMovesModule;
Duncan Sands96144f92008-05-07 19:11:09 +00003236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003237 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3238 ///
3239 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3240 // Size and sign of stack growth.
3241 int stackGrowth =
3242 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3243 TargetFrameInfo::StackGrowsUp ?
Dan Gohmancfb72b22007-09-27 23:12:31 +00003244 TD->getPointerSize() : -TD->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003245
3246 // Begin eh frame section.
3247 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
Bill Wendling189bde72008-12-24 08:05:17 +00003248
3249 if (!TAI->doesRequireNonLocalEHFrameLabel())
3250 O << TAI->getEHGlobalPrefix();
3251 O << "EH_frame" << Index << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003252 EmitLabel("section_eh_frame", Index);
3253
3254 // Define base labels.
3255 EmitLabel("eh_frame_common", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003256
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003257 // Define the eh frame length.
3258 EmitDifference("eh_frame_common_end", Index,
3259 "eh_frame_common_begin", Index, true);
3260 Asm->EOL("Length of Common Information Entry");
3261
3262 // EH frame header.
3263 EmitLabel("eh_frame_common_begin", Index);
3264 Asm->EmitInt32((int)0);
3265 Asm->EOL("CIE Identifier Tag");
3266 Asm->EmitInt8(DW_CIE_VERSION);
3267 Asm->EOL("CIE Version");
Duncan Sands96144f92008-05-07 19:11:09 +00003268
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003269 // The personality presence indicates that language specific information
3270 // will show up in the eh frame.
3271 Asm->EmitString(Personality ? "zPLR" : "zR");
3272 Asm->EOL("CIE Augmentation");
Duncan Sands96144f92008-05-07 19:11:09 +00003273
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003274 // Round out reader.
3275 Asm->EmitULEB128Bytes(1);
3276 Asm->EOL("CIE Code Alignment Factor");
3277 Asm->EmitSLEB128Bytes(stackGrowth);
Duncan Sands96144f92008-05-07 19:11:09 +00003278 Asm->EOL("CIE Data Alignment Factor");
Dale Johannesenf5a11532007-11-13 19:13:01 +00003279 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Duncan Sands96144f92008-05-07 19:11:09 +00003280 Asm->EOL("CIE Return Address Column");
3281
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003282 // If there is a personality, we need to indicate the functions location.
3283 if (Personality) {
3284 Asm->EmitULEB128Bytes(7);
3285 Asm->EOL("Augmentation Size");
Bill Wendling2d369922007-09-11 17:20:55 +00003286
Duncan Sands96144f92008-05-07 19:11:09 +00003287 if (TAI->getNeedsIndirectEncoding()) {
Bill Wendling2d369922007-09-11 17:20:55 +00003288 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
Duncan Sands96144f92008-05-07 19:11:09 +00003289 Asm->EOL("Personality (pcrel sdata4 indirect)");
3290 } else {
Bill Wendling2d369922007-09-11 17:20:55 +00003291 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
Duncan Sands96144f92008-05-07 19:11:09 +00003292 Asm->EOL("Personality (pcrel sdata4)");
3293 }
Bill Wendling2d369922007-09-11 17:20:55 +00003294
Duncan Sands96144f92008-05-07 19:11:09 +00003295 PrintRelDirective(true);
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003296 O << TAI->getPersonalityPrefix();
3297 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3298 O << TAI->getPersonalitySuffix();
Duncan Sands4cc39532008-05-08 12:33:11 +00003299 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3300 O << "-" << TAI->getPCSymbol();
Bill Wendlingd1bda4f2007-09-11 08:27:17 +00003301 Asm->EOL("Personality");
Bill Wendling38cb7c92007-08-25 00:51:55 +00003302
Duncan Sands96144f92008-05-07 19:11:09 +00003303 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3304 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003305
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003306 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3307 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003308 } else {
3309 Asm->EmitULEB128Bytes(1);
3310 Asm->EOL("Augmentation Size");
Bill Wendling6bd1b792008-12-24 05:25:49 +00003311
Bill Wendlingedc2dbe2009-01-05 22:53:45 +00003312 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3313 Asm->EOL("FDE Encoding (pcrel sdata4)");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003314 }
3315
3316 // Indicate locations of general callee saved registers in frame.
3317 std::vector<MachineMove> Moves;
3318 RI->getInitialFrameState(Moves);
Dale Johannesenf5a11532007-11-13 19:13:01 +00003319 EmitFrameMoves(NULL, 0, Moves, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003320
Dale Johannesen388f20f2008-04-30 00:43:29 +00003321 // On Darwin the linker honors the alignment of eh_frame, which means it
3322 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3323 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003324 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003325 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003326 EmitLabel("eh_frame_common_end", Index);
Duncan Sands96144f92008-05-07 19:11:09 +00003327
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003328 Asm->EOL();
3329 }
Duncan Sands96144f92008-05-07 19:11:09 +00003330
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003331 /// EmitEHFrame - Emit function exception frame information.
3332 ///
3333 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003334 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3335
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003336 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3337
3338 // Externally visible entry into the functions eh frame info.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003339 // If the corresponding function is static, this should not be
3340 // externally visible.
Rafael Espindolaa168fc92009-01-15 20:18:42 +00003341 if (linkage != Function::InternalLinkage &&
Devang Patel245446c2009-01-17 08:05:14 +00003342 linkage != Function::PrivateLinkage) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003343 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3344 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3345 }
3346
Dale Johannesenf09b5992008-01-10 02:03:30 +00003347 // If corresponding function is weak definition, this should be too.
aslc200b112008-08-16 12:57:46 +00003348 if ((linkage == Function::WeakLinkage ||
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003349 linkage == Function::LinkOnceLinkage) &&
Dale Johannesenf09b5992008-01-10 02:03:30 +00003350 TAI->getWeakDefDirective())
3351 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3352
3353 // If there are no calls then you can't unwind. This may mean we can
3354 // omit the EH Frame, but some environments do not handle weak absolute
aslc200b112008-08-16 12:57:46 +00003355 // symbols.
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003356 // If UnwindTablesMandatory is set we cannot do this optimization; the
Dale Johannesena9b3e482008-04-08 00:10:24 +00003357 // unwind info is to be available for non-EH uses.
Dale Johannesenf09b5992008-01-10 02:03:30 +00003358 if (!EHFrameInfo.hasCalls &&
Dale Johannesenb369e8d2008-04-14 17:54:17 +00003359 !UnwindTablesMandatory &&
aslc200b112008-08-16 12:57:46 +00003360 ((linkage != Function::WeakLinkage &&
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003361 linkage != Function::LinkOnceLinkage) ||
Dale Johannesenf09b5992008-01-10 02:03:30 +00003362 !TAI->getWeakDefDirective() ||
3363 TAI->getSupportsWeakOmittedEHFrame()))
aslc200b112008-08-16 12:57:46 +00003364 {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003365 O << EHFrameInfo.FnName << " = 0\n";
aslc200b112008-08-16 12:57:46 +00003366 // This name has no connection to the function, so it might get
3367 // dead-stripped when the function is not, erroneously. Prohibit
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003368 // dead-stripping unconditionally.
3369 if (const char *UsedDirective = TAI->getUsedDirective())
3370 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003371 } else {
Bill Wendlingef9211a2007-09-18 01:47:22 +00003372 O << EHFrameInfo.FnName << ":\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003373
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003374 // EH frame header.
3375 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3376 "eh_frame_begin", EHFrameInfo.Number, true);
3377 Asm->EOL("Length of Frame Information Entry");
aslc200b112008-08-16 12:57:46 +00003378
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003379 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3380
Bill Wendling189bde72008-12-24 08:05:17 +00003381 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3382 PrintRelDirective(true, true);
3383 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3384
3385 if (!TAI->isAbsoluteEHSectionOffsets())
3386 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3387 } else {
3388 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3389 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3390 true, true, false);
3391 }
3392
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003393 Asm->EOL("FDE CIE offset");
3394
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003395 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003396 Asm->EOL("FDE initial location");
3397 EmitDifference("eh_func_end", EHFrameInfo.Number,
Bill Wendlingdd9127d2009-01-06 19:13:55 +00003398 "eh_func_begin", EHFrameInfo.Number, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003399 Asm->EOL("FDE address range");
Duncan Sands96144f92008-05-07 19:11:09 +00003400
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003401 // If there is a personality and landing pads then point to the language
3402 // specific data area in the exception table.
3403 if (EHFrameInfo.PersonalityIndex) {
Duncan Sands96144f92008-05-07 19:11:09 +00003404 Asm->EmitULEB128Bytes(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003405 Asm->EOL("Augmentation size");
Duncan Sands96144f92008-05-07 19:11:09 +00003406
3407 if (EHFrameInfo.hasLandingPads)
3408 EmitReference("exception", EHFrameInfo.Number, true, true);
3409 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003410 Asm->EmitInt32((int)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003411 Asm->EOL("Language Specific Data Area");
3412 } else {
3413 Asm->EmitULEB128Bytes(0);
3414 Asm->EOL("Augmentation size");
3415 }
Duncan Sands96144f92008-05-07 19:11:09 +00003416
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003417 // Indicate locations of function specific callee saved registers in
3418 // frame.
Devang Patelb28de842009-01-17 08:01:33 +00003419 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Devang Patel245446c2009-01-17 08:05:14 +00003420 true);
aslc200b112008-08-16 12:57:46 +00003421
Dale Johannesen388f20f2008-04-30 00:43:29 +00003422 // On Darwin the linker honors the alignment of eh_frame, which means it
3423 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3424 // you get holes which confuse readers of eh_frame.
aslc200b112008-08-16 12:57:46 +00003425 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
Dale Johannesen837d7ab2008-04-29 22:58:20 +00003426 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003427 EmitLabel("eh_frame_end", EHFrameInfo.Number);
aslc200b112008-08-16 12:57:46 +00003428
3429 // If the function is marked used, this table should be also. We cannot
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003430 // make the mark unconditional in this case, since retaining the table
aslc200b112008-08-16 12:57:46 +00003431 // also retains the function in this case, and there is code around
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003432 // that depends on unused functions (calling undefined externals) being
3433 // dead-stripped to link correctly. Yes, there really is.
3434 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3435 if (const char *UsedDirective = TAI->getUsedDirective())
3436 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3437 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003438 }
3439
Duncan Sands241a0c92007-09-05 11:27:52 +00003440 /// EmitExceptionTable - Emit landing pads and actions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003441 ///
3442 /// The general organization of the table is complex, but the basic concepts
3443 /// are easy. First there is a header which describes the location and
3444 /// organization of the three components that follow.
3445 /// 1. The landing pad site information describes the range of code covered
3446 /// by the try. In our case it's an accumulation of the ranges covered
3447 /// by the invokes in the try. There is also a reference to the landing
3448 /// pad that handles the exception once processed. Finally an index into
3449 /// the actions table.
3450 /// 2. The action table, in our case, is composed of pairs of type ids
3451 /// and next action offset. Starting with the action index from the
3452 /// landing pad site, each type Id is checked for a match to the current
3453 /// exception. If it matches then the exception and type id are passed
3454 /// on to the landing pad. Otherwise the next action is looked up. This
3455 /// chain is terminated with a next action of zero. If no type id is
3456 /// found the the frame is unwound and handling continues.
3457 /// 3. Type id table contains references to all the C++ typeinfo for all
3458 /// catches in the function. This tables is reversed indexed base 1.
3459
3460 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3461 static unsigned SharedTypeIds(const LandingPadInfo *L,
3462 const LandingPadInfo *R) {
3463 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3464 unsigned LSize = LIds.size(), RSize = RIds.size();
3465 unsigned MinSize = LSize < RSize ? LSize : RSize;
3466 unsigned Count = 0;
3467
3468 for (; Count != MinSize; ++Count)
3469 if (LIds[Count] != RIds[Count])
3470 return Count;
3471
3472 return Count;
3473 }
3474
3475 /// PadLT - Order landing pads lexicographically by type id.
3476 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3477 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3478 unsigned LSize = LIds.size(), RSize = RIds.size();
3479 unsigned MinSize = LSize < RSize ? LSize : RSize;
3480
3481 for (unsigned i = 0; i != MinSize; ++i)
3482 if (LIds[i] != RIds[i])
3483 return LIds[i] < RIds[i];
3484
3485 return LSize < RSize;
3486 }
3487
3488 struct KeyInfo {
3489 static inline unsigned getEmptyKey() { return -1U; }
3490 static inline unsigned getTombstoneKey() { return -2U; }
3491 static unsigned getHashValue(const unsigned &Key) { return Key; }
Chris Lattner92eea072007-09-17 18:34:04 +00003492 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003493 static bool isPod() { return true; }
3494 };
3495
Duncan Sands241a0c92007-09-05 11:27:52 +00003496 /// ActionEntry - Structure describing an entry in the actions table.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003497 struct ActionEntry {
3498 int ValueForTypeID; // The value to write - may not be equal to the type id.
3499 int NextAction;
3500 struct ActionEntry *Previous;
3501 };
3502
Duncan Sands241a0c92007-09-05 11:27:52 +00003503 /// PadRange - Structure holding a try-range and the associated landing pad.
3504 struct PadRange {
3505 // The index of the landing pad.
3506 unsigned PadIndex;
3507 // The index of the begin and end labels in the landing pad's label lists.
3508 unsigned RangeIndex;
3509 };
3510
3511 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3512
3513 /// CallSiteEntry - Structure describing an entry in the call-site table.
3514 struct CallSiteEntry {
Duncan Sands4ff179f2007-12-19 07:36:31 +00003515 // The 'try-range' is BeginLabel .. EndLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003516 unsigned BeginLabel; // zero indicates the start of the function.
3517 unsigned EndLabel; // zero indicates the end of the function.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003518 // The landing pad starts at PadLabel.
Duncan Sands241a0c92007-09-05 11:27:52 +00003519 unsigned PadLabel; // zero indicates that there is no landing pad.
3520 unsigned Action;
3521 };
3522
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003523 void EmitExceptionTable() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003524 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3525 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3526 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3527 if (PadInfos.empty()) return;
3528
3529 // Sort the landing pads in order of their type ids. This is used to fold
3530 // duplicate actions.
3531 SmallVector<const LandingPadInfo *, 64> LandingPads;
3532 LandingPads.reserve(PadInfos.size());
3533 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3534 LandingPads.push_back(&PadInfos[i]);
3535 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3536
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003537 // Negative type ids index into FilterIds, positive type ids index into
3538 // TypeInfos. The value written for a positive type id is just the type
3539 // id itself. For a negative type id, however, the value written is the
3540 // (negative) byte offset of the corresponding FilterIds entry. The byte
3541 // offset is usually equal to the type id, because the FilterIds entries
3542 // are written using a variable width encoding which outputs one byte per
3543 // entry as long as the value written is not too large, but can differ.
3544 // This kind of complication does not occur for positive type ids because
3545 // type infos are output using a fixed width encoding.
3546 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3547 SmallVector<int, 16> FilterOffsets;
3548 FilterOffsets.reserve(FilterIds.size());
3549 int Offset = -1;
3550 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3551 E = FilterIds.end(); I != E; ++I) {
3552 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +00003553 Offset -= TargetAsmInfo::getULEB128Size(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003554 }
3555
Duncan Sands241a0c92007-09-05 11:27:52 +00003556 // Compute the actions table and gather the first action index for each
3557 // landing pad site.
3558 SmallVector<ActionEntry, 32> Actions;
3559 SmallVector<unsigned, 64> FirstActions;
3560 FirstActions.reserve(LandingPads.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003561
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003562 int FirstAction = 0;
Duncan Sands241a0c92007-09-05 11:27:52 +00003563 unsigned SizeActions = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003564 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3565 const LandingPadInfo *LP = LandingPads[i];
3566 const std::vector<int> &TypeIds = LP->TypeIds;
3567 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3568 unsigned SizeSiteActions = 0;
3569
3570 if (NumShared < TypeIds.size()) {
3571 unsigned SizeAction = 0;
3572 ActionEntry *PrevAction = 0;
3573
3574 if (NumShared) {
3575 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3576 assert(Actions.size());
3577 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +00003578 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3579 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003580 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +00003581 SizeAction -=
3582 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003583 SizeAction += -PrevAction->NextAction;
3584 PrevAction = PrevAction->Previous;
3585 }
3586 }
3587
3588 // Compute the actions.
3589 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3590 int TypeID = TypeIds[I];
3591 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3592 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +00003593 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003594
3595 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +00003596 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003597 SizeSiteActions += SizeAction;
3598
3599 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
3600 Actions.push_back(Action);
3601
3602 PrevAction = &Actions.back();
3603 }
3604
3605 // Record the first action of the landing pad site.
3606 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3607 } // else identical - re-use previous FirstAction
3608
3609 FirstActions.push_back(FirstAction);
3610
3611 // Compute this sites contribution to size.
3612 SizeActions += SizeSiteActions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003613 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003614
Duncan Sands4ff179f2007-12-19 07:36:31 +00003615 // Compute the call-site table. The entry for an invoke has a try-range
3616 // containing the call, a non-zero landing pad and an appropriate action.
3617 // The entry for an ordinary call has a try-range containing the call and
3618 // zero for the landing pad and the action. Calls marked 'nounwind' have
3619 // no entry and must not be contained in the try-range of any entry - they
3620 // form gaps in the table. Entries must be ordered by try-range address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003621 SmallVector<CallSiteEntry, 64> CallSites;
3622
3623 RangeMapType PadMap;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003624 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3625 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3626 // try-ranges for them need be deduced.
Duncan Sands241a0c92007-09-05 11:27:52 +00003627 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3628 const LandingPadInfo *LandingPad = LandingPads[i];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003629 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003630 unsigned BeginLabel = LandingPad->BeginLabels[j];
3631 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3632 PadRange P = { i, j };
3633 PadMap[BeginLabel] = P;
3634 }
3635 }
3636
Duncan Sands4ff179f2007-12-19 07:36:31 +00003637 // The end label of the previous invoke or nounwind try-range.
Duncan Sands241a0c92007-09-05 11:27:52 +00003638 unsigned LastLabel = 0;
Duncan Sands4ff179f2007-12-19 07:36:31 +00003639
3640 // Whether there is a potentially throwing instruction (currently this means
3641 // an ordinary call) between the end of the previous try-range and now.
3642 bool SawPotentiallyThrowing = false;
3643
3644 // Whether the last callsite entry was for an invoke.
3645 bool PreviousIsInvoke = false;
3646
Duncan Sands4ff179f2007-12-19 07:36:31 +00003647 // Visit all instructions in order of address.
Duncan Sands241a0c92007-09-05 11:27:52 +00003648 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3649 I != E; ++I) {
3650 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3651 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00003652 if (!MI->isLabel()) {
Chris Lattner5b930372008-01-07 07:27:27 +00003653 SawPotentiallyThrowing |= MI->getDesc().isCall();
Duncan Sands241a0c92007-09-05 11:27:52 +00003654 continue;
3655 }
3656
Chris Lattnerda4cff12007-12-30 20:50:28 +00003657 unsigned BeginLabel = MI->getOperand(0).getImm();
Duncan Sands241a0c92007-09-05 11:27:52 +00003658 assert(BeginLabel && "Invalid label!");
Duncan Sands89372f62007-09-05 14:12:46 +00003659
Duncan Sands4ff179f2007-12-19 07:36:31 +00003660 // End of the previous try-range?
Duncan Sands89372f62007-09-05 14:12:46 +00003661 if (BeginLabel == LastLabel)
Duncan Sands4ff179f2007-12-19 07:36:31 +00003662 SawPotentiallyThrowing = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003663
Duncan Sands4ff179f2007-12-19 07:36:31 +00003664 // Beginning of a new try-range?
Duncan Sands241a0c92007-09-05 11:27:52 +00003665 RangeMapType::iterator L = PadMap.find(BeginLabel);
Duncan Sands241a0c92007-09-05 11:27:52 +00003666 if (L == PadMap.end())
Duncan Sands4ff179f2007-12-19 07:36:31 +00003667 // Nope, it was just some random label.
Duncan Sands241a0c92007-09-05 11:27:52 +00003668 continue;
3669
3670 PadRange P = L->second;
3671 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3672
3673 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3674 "Inconsistent landing pad map!");
3675
3676 // If some instruction between the previous try-range and this one may
3677 // throw, create a call-site entry with no landing pad for the region
3678 // between the try-ranges.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003679 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003680 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3681 CallSites.push_back(Site);
Duncan Sands4ff179f2007-12-19 07:36:31 +00003682 PreviousIsInvoke = false;
Duncan Sands241a0c92007-09-05 11:27:52 +00003683 }
3684
3685 LastLabel = LandingPad->EndLabels[P.RangeIndex];
Duncan Sands4ff179f2007-12-19 07:36:31 +00003686 assert(BeginLabel && LastLabel && "Invalid landing pad!");
Duncan Sands241a0c92007-09-05 11:27:52 +00003687
Duncan Sands4ff179f2007-12-19 07:36:31 +00003688 if (LandingPad->LandingPadLabel) {
3689 // This try-range is for an invoke.
3690 CallSiteEntry Site = {BeginLabel, LastLabel,
3691 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
Duncan Sands241a0c92007-09-05 11:27:52 +00003692
Duncan Sands4ff179f2007-12-19 07:36:31 +00003693 // Try to merge with the previous call-site.
3694 if (PreviousIsInvoke) {
Dan Gohman3d436002008-06-21 22:00:54 +00003695 CallSiteEntry &Prev = CallSites.back();
Duncan Sands4ff179f2007-12-19 07:36:31 +00003696 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3697 // Extend the range of the previous entry.
3698 Prev.EndLabel = Site.EndLabel;
3699 continue;
3700 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003701 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003702
Duncan Sands4ff179f2007-12-19 07:36:31 +00003703 // Otherwise, create a new call-site.
3704 CallSites.push_back(Site);
3705 PreviousIsInvoke = true;
3706 } else {
3707 // Create a gap.
3708 PreviousIsInvoke = false;
3709 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003710 }
3711 }
3712 // If some instruction between the previous try-range and the end of the
3713 // function may throw, create a call-site entry with no landing pad for the
3714 // region following the try-range.
Duncan Sands4ff179f2007-12-19 07:36:31 +00003715 if (SawPotentiallyThrowing) {
Duncan Sands241a0c92007-09-05 11:27:52 +00003716 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3717 CallSites.push_back(Site);
3718 }
3719
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003720 // Final tallies.
Duncan Sands96144f92008-05-07 19:11:09 +00003721
3722 // Call sites.
3723 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3724 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3725 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3726 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3727 SiteLengthSize +
3728 LandingPadSize);
Duncan Sands241a0c92007-09-05 11:27:52 +00003729 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +00003730 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Duncan Sands241a0c92007-09-05 11:27:52 +00003731
Duncan Sands96144f92008-05-07 19:11:09 +00003732 // Type infos.
3733 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3734 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003735
3736 unsigned TypeOffset = sizeof(int8_t) + // Call site format
aslc200b112008-08-16 12:57:46 +00003737 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003738 SizeSites + SizeActions + SizeTypes;
3739
3740 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3741 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +00003742 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003743 TypeOffset;
3744
3745 unsigned SizeAlign = (4 - TotalSize) & 3;
3746
3747 // Begin the exception table.
3748 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
Evan Cheng7e7d1942008-02-29 19:36:59 +00003749 Asm->EmitAlignment(2, 0, 0, false);
Dale Johannesen841e4982008-10-08 21:50:21 +00003750 O << "GCC_except_table" << SubprogramCount << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003751 for (unsigned i = 0; i != SizeAlign; ++i) {
3752 Asm->EmitInt8(0);
3753 Asm->EOL("Padding");
3754 }
3755 EmitLabel("exception", SubprogramCount);
3756
3757 // Emit the header.
3758 Asm->EmitInt8(DW_EH_PE_omit);
3759 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3760 Asm->EmitInt8(DW_EH_PE_absptr);
3761 Asm->EOL("TType format (DW_EH_PE_absptr)");
3762 Asm->EmitULEB128Bytes(TypeOffset);
3763 Asm->EOL("TType base offset");
3764 Asm->EmitInt8(DW_EH_PE_udata4);
3765 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3766 Asm->EmitULEB128Bytes(SizeSites);
3767 Asm->EOL("Call-site table length");
3768
Duncan Sands241a0c92007-09-05 11:27:52 +00003769 // Emit the landing pad site information.
3770 for (unsigned i = 0; i < CallSites.size(); ++i) {
3771 CallSiteEntry &S = CallSites[i];
3772 const char *BeginTag;
3773 unsigned BeginNumber;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003774
Duncan Sands241a0c92007-09-05 11:27:52 +00003775 if (!S.BeginLabel) {
3776 BeginTag = "eh_func_begin";
3777 BeginNumber = SubprogramCount;
3778 } else {
3779 BeginTag = "label";
3780 BeginNumber = S.BeginLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003781 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003782
Duncan Sands241a0c92007-09-05 11:27:52 +00003783 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003784 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003785 Asm->EOL("Region start");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003786
Duncan Sands241a0c92007-09-05 11:27:52 +00003787 if (!S.EndLabel) {
Dale Johannesen4670be42008-01-15 23:24:56 +00003788 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
Duncan Sands96144f92008-05-07 19:11:09 +00003789 true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003790 } else {
Duncan Sands96144f92008-05-07 19:11:09 +00003791 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003792 }
3793 Asm->EOL("Region length");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003794
Duncan Sands96144f92008-05-07 19:11:09 +00003795 if (!S.PadLabel)
3796 Asm->EmitInt32(0);
3797 else
Duncan Sands241a0c92007-09-05 11:27:52 +00003798 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
Duncan Sands96144f92008-05-07 19:11:09 +00003799 true, true);
Duncan Sands241a0c92007-09-05 11:27:52 +00003800 Asm->EOL("Landing pad");
3801
3802 Asm->EmitULEB128Bytes(S.Action);
3803 Asm->EOL("Action");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003804 }
3805
3806 // Emit the actions.
3807 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3808 ActionEntry &Action = Actions[I];
3809
3810 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
3811 Asm->EOL("TypeInfo index");
3812 Asm->EmitSLEB128Bytes(Action.NextAction);
3813 Asm->EOL("Next action");
3814 }
3815
3816 // Emit the type ids.
3817 for (unsigned M = TypeInfos.size(); M; --M) {
3818 GlobalVariable *GV = TypeInfos[M - 1];
Anton Korobeynikov5ef86702007-09-02 22:07:21 +00003819
3820 PrintRelDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003821
3822 if (GV)
3823 O << Asm->getGlobalLinkName(GV);
3824 else
3825 O << "0";
Duncan Sands241a0c92007-09-05 11:27:52 +00003826
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003827 Asm->EOL("TypeInfo");
3828 }
3829
3830 // Emit the filter typeids.
3831 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3832 unsigned TypeID = FilterIds[j];
3833 Asm->EmitULEB128Bytes(TypeID);
3834 Asm->EOL("Filter TypeInfo index");
3835 }
Duncan Sands241a0c92007-09-05 11:27:52 +00003836
Evan Cheng7e7d1942008-02-29 19:36:59 +00003837 Asm->EmitAlignment(2, 0, 0, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003838 }
3839
3840public:
3841 //===--------------------------------------------------------------------===//
3842 // Main entry points.
3843 //
Owen Anderson847b99b2008-08-21 00:14:44 +00003844 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
Chris Lattnerb3876c72007-09-24 03:35:37 +00003845 : Dwarf(OS, A, T, "eh")
Dale Johannesen85535762008-04-02 00:25:04 +00003846 , shouldEmitTable(false)
3847 , shouldEmitMoves(false)
3848 , shouldEmitTableModule(false)
3849 , shouldEmitMovesModule(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003850 {}
aslc200b112008-08-16 12:57:46 +00003851
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003852 virtual ~DwarfException() {}
3853
3854 /// SetModuleInfo - Set machine module information when it's known that pass
3855 /// manager has created it. Set by the target AsmPrinter.
3856 void SetModuleInfo(MachineModuleInfo *mmi) {
3857 MMI = mmi;
3858 }
3859
3860 /// BeginModule - Emit all exception information that should come prior to the
3861 /// content.
3862 void BeginModule(Module *M) {
3863 this->M = M;
3864 }
3865
3866 /// EndModule - Emit all exception information that should come after the
3867 /// content.
3868 void EndModule() {
Dale Johannesen85535762008-04-02 00:25:04 +00003869 if (shouldEmitMovesModule || shouldEmitTableModule) {
3870 const std::vector<Function *> Personalities = MMI->getPersonalities();
3871 for (unsigned i =0; i < Personalities.size(); ++i)
3872 EmitCommonEHFrame(Personalities[i], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003873
Dale Johannesen85535762008-04-02 00:25:04 +00003874 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3875 E = EHFrames.end(); I != E; ++I)
3876 EmitEHFrame(*I);
3877 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003878 }
3879
aslc200b112008-08-16 12:57:46 +00003880 /// BeginFunction - Gather pre-function exception information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003881 /// emitted immediately after the function entry point.
3882 void BeginFunction(MachineFunction *MF) {
3883 this->MF = MF;
Dale Johannesen85535762008-04-02 00:25:04 +00003884 shouldEmitTable = shouldEmitMoves = false;
Dale Johannesen62f0a6d2008-04-02 17:04:45 +00003885 if (MMI && TAI->doesSupportExceptionHandling()) {
Dale Johannesen85535762008-04-02 00:25:04 +00003886
3887 // Map all labels and get rid of any dead landing pads.
3888 MMI->TidyLandingPads();
3889 // If any landing pads survive, we need an EH table.
3890 if (MMI->getLandingPads().size())
3891 shouldEmitTable = true;
3892
3893 // See if we need frame move info.
Duncan Sandscbc28b12008-07-04 09:55:48 +00003894 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
Dale Johannesen85535762008-04-02 00:25:04 +00003895 shouldEmitMoves = true;
3896
3897 if (shouldEmitMoves || shouldEmitTable)
3898 // Assumes in correct section after the entry point.
3899 EmitLabel("eh_func_begin", ++SubprogramCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003900 }
Dale Johannesen85535762008-04-02 00:25:04 +00003901 shouldEmitTableModule |= shouldEmitTable;
3902 shouldEmitMovesModule |= shouldEmitMoves;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003903 }
3904
3905 /// EndFunction - Gather and emit post-function exception information.
3906 ///
3907 void EndFunction() {
Dale Johannesen85535762008-04-02 00:25:04 +00003908 if (shouldEmitMoves || shouldEmitTable) {
3909 EmitLabel("eh_func_end", SubprogramCount);
3910 EmitExceptionTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003911
Dale Johannesen85535762008-04-02 00:25:04 +00003912 // Save EH frame information
3913 EHFrames.
3914 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingef9211a2007-09-18 01:47:22 +00003915 SubprogramCount,
3916 MMI->getPersonalityIndex(),
3917 MF->getFrameInfo()->hasCalls(),
3918 !MMI->getLandingPads().empty(),
Dale Johannesenfb3ac732007-11-20 23:24:42 +00003919 MMI->getFrameMoves(),
Dale Johannesen3dadeb32008-01-16 19:59:28 +00003920 MF->getFunction()));
Dale Johannesen85535762008-04-02 00:25:04 +00003921 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003922 }
3923};
3924
3925} // End of namespace llvm
3926
3927//===----------------------------------------------------------------------===//
3928
3929/// Emit - Print the abbreviation using the specified Dwarf writer.
3930///
3931void DIEAbbrev::Emit(const DwarfDebug &DD) const {
3932 // Emit its Dwarf tag type.
3933 DD.getAsm()->EmitULEB128Bytes(Tag);
3934 DD.getAsm()->EOL(TagString(Tag));
aslc200b112008-08-16 12:57:46 +00003935
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003936 // Emit whether it has children DIEs.
3937 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3938 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
aslc200b112008-08-16 12:57:46 +00003939
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003940 // For each attribute description.
3941 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3942 const DIEAbbrevData &AttrData = Data[i];
aslc200b112008-08-16 12:57:46 +00003943
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003944 // Emit attribute type.
3945 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3946 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
aslc200b112008-08-16 12:57:46 +00003947
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003948 // Emit form type.
3949 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3950 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
3951 }
3952
3953 // Mark end of abbreviation.
3954 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3955 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
3956}
3957
3958#ifndef NDEBUG
3959void DIEAbbrev::print(std::ostream &O) {
3960 O << "Abbreviation @"
3961 << std::hex << (intptr_t)this << std::dec
3962 << " "
3963 << TagString(Tag)
3964 << " "
3965 << ChildrenString(ChildrenFlag)
3966 << "\n";
aslc200b112008-08-16 12:57:46 +00003967
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003968 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3969 O << " "
3970 << AttributeString(Data[i].getAttribute())
3971 << " "
3972 << FormEncodingString(Data[i].getForm())
3973 << "\n";
3974 }
3975}
3976void DIEAbbrev::dump() { print(cerr); }
3977#endif
3978
3979//===----------------------------------------------------------------------===//
3980
3981#ifndef NDEBUG
3982void DIEValue::dump() {
3983 print(cerr);
3984}
3985#endif
3986
3987//===----------------------------------------------------------------------===//
3988
3989/// EmitValue - Emit integer of appropriate size.
3990///
3991void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
3992 switch (Form) {
3993 case DW_FORM_flag: // Fall thru
3994 case DW_FORM_ref1: // Fall thru
3995 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
3996 case DW_FORM_ref2: // Fall thru
3997 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
3998 case DW_FORM_ref4: // Fall thru
3999 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
4000 case DW_FORM_ref8: // Fall thru
4001 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
4002 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
4003 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
4004 default: assert(0 && "DIE Value form not supported yet"); break;
4005 }
4006}
4007
4008/// SizeOf - Determine size of integer value in bytes.
4009///
4010unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4011 switch (Form) {
4012 case DW_FORM_flag: // Fall thru
4013 case DW_FORM_ref1: // Fall thru
4014 case DW_FORM_data1: return sizeof(int8_t);
4015 case DW_FORM_ref2: // Fall thru
4016 case DW_FORM_data2: return sizeof(int16_t);
4017 case DW_FORM_ref4: // Fall thru
4018 case DW_FORM_data4: return sizeof(int32_t);
4019 case DW_FORM_ref8: // Fall thru
4020 case DW_FORM_data8: return sizeof(int64_t);
aslc200b112008-08-16 12:57:46 +00004021 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
4022 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004023 default: assert(0 && "DIE Value form not supported yet"); break;
4024 }
4025 return 0;
4026}
4027
4028//===----------------------------------------------------------------------===//
4029
4030/// EmitValue - Emit string value.
4031///
4032void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
4033 DD.getAsm()->EmitString(String);
4034}
4035
4036//===----------------------------------------------------------------------===//
4037
4038/// EmitValue - Emit label value.
4039///
4040void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004041 bool IsSmall = Form == DW_FORM_data4;
4042 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004043}
4044
4045/// SizeOf - Determine size of label value in bytes.
4046///
4047unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004048 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004049 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004050}
4051
4052//===----------------------------------------------------------------------===//
4053
4054/// EmitValue - Emit label value.
4055///
4056void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
Dan Gohman597b4842007-09-28 16:50:28 +00004057 bool IsSmall = Form == DW_FORM_data4;
4058 DD.EmitReference(Label, false, IsSmall);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004059}
4060
4061/// SizeOf - Determine size of label value in bytes.
4062///
4063unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
Dan Gohman597b4842007-09-28 16:50:28 +00004064 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004065 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004066}
aslc200b112008-08-16 12:57:46 +00004067
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004068//===----------------------------------------------------------------------===//
4069
4070/// EmitValue - Emit delta value.
4071///
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004072void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4073 bool IsSmall = Form == DW_FORM_data4;
4074 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4075 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4076}
4077
4078/// SizeOf - Determine size of delta value in bytes.
4079///
4080unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4081 if (Form == DW_FORM_data4) return 4;
4082 return DD.getTargetData()->getPointerSize();
4083}
aslc200b112008-08-16 12:57:46 +00004084
Argiris Kirtzidis03449652008-06-18 19:27:37 +00004085//===----------------------------------------------------------------------===//
4086
4087/// EmitValue - Emit delta value.
4088///
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004089void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4090 bool IsSmall = Form == DW_FORM_data4;
4091 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4092}
4093
4094/// SizeOf - Determine size of delta value in bytes.
4095///
4096unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4097 if (Form == DW_FORM_data4) return 4;
Dan Gohmancfb72b22007-09-27 23:12:31 +00004098 return DD.getTargetData()->getPointerSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004099}
4100
4101//===----------------------------------------------------------------------===//
4102
4103/// EmitValue - Emit debug information entry offset.
4104///
4105void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4106 DD.getAsm()->EmitInt32(Entry->getOffset());
4107}
aslc200b112008-08-16 12:57:46 +00004108
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004109//===----------------------------------------------------------------------===//
4110
4111/// ComputeSize - calculate the size of the block.
4112///
4113unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4114 if (!Size) {
Owen Anderson88dd6232008-06-24 21:44:59 +00004115 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004116
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004117 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4118 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4119 }
4120 }
4121 return Size;
4122}
4123
4124/// EmitValue - Emit block data.
4125///
4126void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4127 switch (Form) {
4128 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4129 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4130 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4131 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4132 default: assert(0 && "Improper form for block"); break;
4133 }
aslc200b112008-08-16 12:57:46 +00004134
Owen Anderson88dd6232008-06-24 21:44:59 +00004135 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004136
4137 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4138 DD.getAsm()->EOL();
4139 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4140 }
4141}
4142
4143/// SizeOf - Determine size of block data in bytes.
4144///
4145unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4146 switch (Form) {
4147 case DW_FORM_block1: return Size + sizeof(int8_t);
4148 case DW_FORM_block2: return Size + sizeof(int16_t);
4149 case DW_FORM_block4: return Size + sizeof(int32_t);
aslc200b112008-08-16 12:57:46 +00004150 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004151 default: assert(0 && "Improper form for block"); break;
4152 }
4153 return 0;
4154}
4155
4156//===----------------------------------------------------------------------===//
4157/// DIE Implementation
4158
4159DIE::~DIE() {
4160 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4161 delete Children[i];
4162}
aslc200b112008-08-16 12:57:46 +00004163
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004164/// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4165///
4166void DIE::AddSiblingOffset() {
4167 DIEInteger *DI = new DIEInteger(0);
4168 Values.insert(Values.begin(), DI);
4169 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4170}
4171
4172/// Profile - Used to gather unique data for the value folding set.
4173///
4174void DIE::Profile(FoldingSetNodeID &ID) {
4175 Abbrev.Profile(ID);
aslc200b112008-08-16 12:57:46 +00004176
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004177 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4178 ID.AddPointer(Children[i]);
4179
4180 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4181 ID.AddPointer(Values[j]);
4182}
4183
4184#ifndef NDEBUG
4185void DIE::print(std::ostream &O, unsigned IncIndent) {
4186 static unsigned IndentCount = 0;
4187 IndentCount += IncIndent;
4188 const std::string Indent(IndentCount, ' ');
4189 bool isBlock = Abbrev.getTag() == 0;
aslc200b112008-08-16 12:57:46 +00004190
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004191 if (!isBlock) {
4192 O << Indent
4193 << "Die: "
4194 << "0x" << std::hex << (intptr_t)this << std::dec
4195 << ", Offset: " << Offset
4196 << ", Size: " << Size
aslc200b112008-08-16 12:57:46 +00004197 << "\n";
4198
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004199 O << Indent
4200 << TagString(Abbrev.getTag())
4201 << " "
4202 << ChildrenString(Abbrev.getChildrenFlag());
4203 } else {
4204 O << "Size: " << Size;
4205 }
4206 O << "\n";
4207
Owen Anderson88dd6232008-06-24 21:44:59 +00004208 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
aslc200b112008-08-16 12:57:46 +00004209
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004210 IndentCount += 2;
4211 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4212 O << Indent;
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004213
4214 if (!isBlock)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004215 O << AttributeString(Data[i].getAttribute());
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004216 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004217 O << "Blk[" << i << "]";
Bill Wendlingdc7b5a52008-07-22 00:28:47 +00004218
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004219 O << " "
4220 << FormEncodingString(Data[i].getForm())
4221 << " ";
4222 Values[i]->print(O);
4223 O << "\n";
4224 }
4225 IndentCount -= 2;
4226
4227 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4228 Children[j]->print(O, 4);
4229 }
aslc200b112008-08-16 12:57:46 +00004230
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004231 if (!isBlock) O << "\n";
4232 IndentCount -= IncIndent;
4233}
4234
4235void DIE::dump() {
4236 print(cerr);
4237}
4238#endif
4239
4240//===----------------------------------------------------------------------===//
4241/// DwarfWriter Implementation
4242///
4243
Devang Patelaa1e8432009-01-08 23:40:34 +00004244DwarfWriter::DwarfWriter() : ImmutablePass(&ID), DD(NULL), DE(NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004245}
4246
4247DwarfWriter::~DwarfWriter() {
4248 delete DE;
4249 delete DD;
4250}
4251
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004252/// BeginModule - Emit all Dwarf sections that should come prior to the
4253/// content.
Devang Patelaa1e8432009-01-08 23:40:34 +00004254void DwarfWriter::BeginModule(Module *M,
4255 MachineModuleInfo *MMI,
4256 raw_ostream &OS, AsmPrinter *A,
4257 const TargetAsmInfo *T) {
4258 DE = new DwarfException(OS, A, T);
4259 DD = new DwarfDebug(OS, A, T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004260 DE->BeginModule(M);
4261 DD->BeginModule(M);
Devang Patel6ccd57e2009-01-13 00:20:51 +00004262 DD->SetDebugInfo(MMI);
Devang Patelaa1e8432009-01-08 23:40:34 +00004263 DE->SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004264}
4265
4266/// EndModule - Emit all Dwarf sections that should come after the content.
4267///
4268void DwarfWriter::EndModule() {
4269 DE->EndModule();
4270 DD->EndModule();
4271}
4272
aslc200b112008-08-16 12:57:46 +00004273/// BeginFunction - Gather pre-function debug information. Assumes being
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004274/// emitted immediately after the function entry point.
4275void DwarfWriter::BeginFunction(MachineFunction *MF) {
4276 DE->BeginFunction(MF);
4277 DD->BeginFunction(MF);
4278}
4279
4280/// EndFunction - Gather and emit post-function debug information.
4281///
Bill Wendlingb22ae7d2008-09-26 00:28:12 +00004282void DwarfWriter::EndFunction(MachineFunction *MF) {
4283 DD->EndFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004284 DE->EndFunction();
aslc200b112008-08-16 12:57:46 +00004285
Bill Wendling5b4796a2008-07-22 00:53:37 +00004286 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004287 // Clear function debug information.
4288 MMI->EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004289}
Devang Patelcb59fd42009-01-12 19:17:34 +00004290
Devang Patel2da0cc42009-01-15 23:41:32 +00004291/// ValidDebugInfo - Return true if V represents valid debug info value.
4292bool DwarfWriter::ValidDebugInfo(Value *V) {
Devang Patel943af622009-01-16 02:15:14 +00004293 return DD && DD->ValidDebugInfo(V);
Devang Patel2da0cc42009-01-15 23:41:32 +00004294}
4295
Devang Patelcb59fd42009-01-12 19:17:34 +00004296/// RecordSourceLine - Records location information and associates it with a
4297/// label. Returns a unique label ID used to generate a label and provide
4298/// correspondence to the source line list.
4299unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4300 unsigned Src) {
4301 return DD->RecordSourceLine(Line, Col, Src);
4302}
4303
4304/// RecordSource - Register a source file with debug info. Returns an source
4305/// ID.
4306unsigned DwarfWriter::RecordSource(const std::string &Dir,
4307 const std::string &File) {
4308 return DD->RecordSource(Dir, File);
4309}
4310
4311/// RecordRegionStart - Indicate the start of a region.
4312unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
4313 return DD->RecordRegionStart(V);
4314}
4315
4316/// RecordRegionEnd - Indicate the end of a region.
4317unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
4318 return DD->RecordRegionEnd(V);
4319}
4320
4321/// getRecordSourceLineCount - Count source lines.
4322unsigned DwarfWriter::getRecordSourceLineCount() {
4323 return DD->getRecordSourceLineCount();
4324}
Devang Patel70190872009-01-13 21:25:00 +00004325
Devang Patelfe359e72009-01-13 21:44:10 +00004326/// RecordVariable - Indicate the declaration of a local variable.
4327///
4328void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
4329 DD->RecordVariable(GV, FrameIndex);
4330}
Devang Patel42f6bed2009-01-13 23:54:55 +00004331