blob: a84ef470cfd8a310a8f77d73c869dfca1f8da674 [file] [log] [blame]
Bill Wendling2f921f82009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattnerb14490d2010-03-09 00:39:24 +000013
Devang Patel80ae3492009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling2f921f82009-05-15 09:23:25 +000015#include "DwarfDebug.h"
16#include "llvm/Module.h"
David Greene829b3e82009-08-19 21:52:55 +000017#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000019#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000020#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000021#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000022#include "llvm/MC/MCSymbol.h"
Chris Lattnerf62e3ee2010-01-16 21:57:06 +000023#include "llvm/Target/Mangler.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000024#include "llvm/Target/TargetData.h"
25#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000026#include "llvm/Target/TargetLoweringObjectFile.h"
27#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner06fa1762009-08-24 03:52:50 +000028#include "llvm/ADT/StringExtras.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/ErrorHandling.h"
Devang Patel1973df22010-01-26 21:39:14 +000031#include "llvm/Support/ValueHandle.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000032#include "llvm/Support/FormattedStream.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000033#include "llvm/Support/Timer.h"
34#include "llvm/System/Path.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000035using namespace llvm;
36
Bill Wendling2f921f82009-05-15 09:23:25 +000037//===----------------------------------------------------------------------===//
38
39/// Configuration values for initial hash set sizes (log2).
40///
Bill Wendling2f921f82009-05-15 09:23:25 +000041static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling2f921f82009-05-15 09:23:25 +000042
43namespace llvm {
44
45//===----------------------------------------------------------------------===//
46/// CompileUnit - This dwarf writer support class manages information associate
47/// with a source file.
Nick Lewyckyb7993d62009-11-17 08:11:44 +000048class CompileUnit {
Bill Wendling2f921f82009-05-15 09:23:25 +000049 /// ID - File identifier for source.
50 ///
51 unsigned ID;
52
53 /// Die - Compile unit debug information entry.
54 ///
Douglas Gregor9abb5382010-03-08 02:58:37 +000055 DIE *CUDie;
Bill Wendling2f921f82009-05-15 09:23:25 +000056
Douglas Gregor9abb5382010-03-08 02:58:37 +000057 /// IndexTyDie - An anonymous type for index type.
Devang Patel92e8c652009-11-21 00:31:03 +000058 DIE *IndexTyDie;
59
Bill Wendling2f921f82009-05-15 09:23:25 +000060 /// GVToDieMap - Tracks the mapping of unit level debug informaton
61 /// variables to debug information entries.
Devang Patel80ae3492009-08-28 23:24:31 +000062 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patel018b29b2010-01-19 06:19:05 +000063 DenseMap<MDNode *, DIE *> GVToDieMap;
Bill Wendling2f921f82009-05-15 09:23:25 +000064
65 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
66 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel80ae3492009-08-28 23:24:31 +000067 /// FIXME : Rename
Devang Patel018b29b2010-01-19 06:19:05 +000068 DenseMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendling2f921f82009-05-15 09:23:25 +000069
70 /// Globals - A map of globally visible named entities for this unit.
71 ///
72 StringMap<DIE*> Globals;
73
Devang Patel04d2f2d2009-11-24 01:14:22 +000074 /// GlobalTypes - A map of globally visible types for this unit.
75 ///
76 StringMap<DIE*> GlobalTypes;
77
Bill Wendling2f921f82009-05-15 09:23:25 +000078public:
79 CompileUnit(unsigned I, DIE *D)
Devang Patel930143b2009-11-21 02:48:08 +000080 : ID(I), CUDie(D), IndexTyDie(0) {}
Douglas Gregor9abb5382010-03-08 02:58:37 +000081 ~CompileUnit() { delete CUDie; delete IndexTyDie; }
Bill Wendling2f921f82009-05-15 09:23:25 +000082
83 // Accessors.
Devang Patel04d2f2d2009-11-24 01:14:22 +000084 unsigned getID() const { return ID; }
Douglas Gregor9abb5382010-03-08 02:58:37 +000085 DIE* getCUDie() const { return CUDie; }
Devang Patel04d2f2d2009-11-24 01:14:22 +000086 const StringMap<DIE*> &getGlobals() const { return Globals; }
87 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendling2f921f82009-05-15 09:23:25 +000088
89 /// hasContent - Return true if this compile unit has something to write out.
90 ///
Devang Patel930143b2009-11-21 02:48:08 +000091 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendling2f921f82009-05-15 09:23:25 +000092
Devang Patel930143b2009-11-21 02:48:08 +000093 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendling2f921f82009-05-15 09:23:25 +000094 ///
Devang Patel930143b2009-11-21 02:48:08 +000095 void addGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling2f921f82009-05-15 09:23:25 +000096
Devang Patel04d2f2d2009-11-24 01:14:22 +000097 /// addGlobalType - Add a new global type to the compile unit.
98 ///
99 void addGlobalType(const std::string &Name, DIE *Die) {
100 GlobalTypes[Name] = Die;
101 }
102
Devang Patele064ad42009-11-20 21:37:22 +0000103 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling2f921f82009-05-15 09:23:25 +0000104 /// specified debug variable.
Devang Patele064ad42009-11-20 21:37:22 +0000105 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach042483e2009-11-21 23:12:12 +0000106
Devang Patele064ad42009-11-20 21:37:22 +0000107 /// insertDIE - Insert DIE into the map.
108 void insertDIE(MDNode *N, DIE *D) {
109 GVToDieMap.insert(std::make_pair(N, D));
110 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000111
Devang Patele064ad42009-11-20 21:37:22 +0000112 /// getDIEEntry - Returns the debug information entry for the speciefied
113 /// debug variable.
Devang Patel1f4690c2009-12-15 19:16:48 +0000114 DIEEntry *getDIEEntry(MDNode *N) {
Devang Patel018b29b2010-01-19 06:19:05 +0000115 DenseMap<MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N);
Devang Patel1f4690c2009-12-15 19:16:48 +0000116 if (I == GVToDIEEntryMap.end())
117 return NULL;
118 return I->second;
119 }
Devang Patele064ad42009-11-20 21:37:22 +0000120
121 /// insertDIEEntry - Insert debug information entry into the map.
122 void insertDIEEntry(MDNode *N, DIEEntry *E) {
123 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling2f921f82009-05-15 09:23:25 +0000124 }
125
Devang Patel930143b2009-11-21 02:48:08 +0000126 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling2f921f82009-05-15 09:23:25 +0000127 ///
Devang Patel930143b2009-11-21 02:48:08 +0000128 void addDie(DIE *Buffer) {
129 this->CUDie->addChild(Buffer);
Bill Wendling2f921f82009-05-15 09:23:25 +0000130 }
Devang Patel92e8c652009-11-21 00:31:03 +0000131
132 // getIndexTyDie - Get an anonymous type for index type.
133 DIE *getIndexTyDie() {
134 return IndexTyDie;
135 }
136
Jim Grosbach00e9c612009-11-22 19:20:36 +0000137 // setIndexTyDie - Set D as anonymous type for index which can be reused
138 // later.
Devang Patel92e8c652009-11-21 00:31:03 +0000139 void setIndexTyDie(DIE *D) {
140 IndexTyDie = D;
141 }
142
Bill Wendling2f921f82009-05-15 09:23:25 +0000143};
144
145//===----------------------------------------------------------------------===//
146/// DbgVariable - This class is used to track local variable information.
147///
Devang Patelf3d7c082009-11-16 21:53:40 +0000148class DbgVariable {
Bill Wendling2f921f82009-05-15 09:23:25 +0000149 DIVariable Var; // Variable Descriptor.
150 unsigned FrameIndex; // Variable frame index.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000151 DbgVariable *AbstractVar; // Abstract variable for this variable.
152 DIE *TheDIE;
Bill Wendling2f921f82009-05-15 09:23:25 +0000153public:
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000154 DbgVariable(DIVariable V, unsigned I)
155 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {}
Bill Wendling2f921f82009-05-15 09:23:25 +0000156
157 // Accessors.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000158 DIVariable getVariable() const { return Var; }
159 unsigned getFrameIndex() const { return FrameIndex; }
160 void setAbstractVariable(DbgVariable *V) { AbstractVar = V; }
161 DbgVariable *getAbstractVariable() const { return AbstractVar; }
162 void setDIE(DIE *D) { TheDIE = D; }
163 DIE *getDIE() const { return TheDIE; }
Bill Wendling2f921f82009-05-15 09:23:25 +0000164};
165
166//===----------------------------------------------------------------------===//
167/// DbgScope - This class is used to track scope information.
168///
Devang Patelf3d7c082009-11-16 21:53:40 +0000169class DbgScope {
Bill Wendling2f921f82009-05-15 09:23:25 +0000170 DbgScope *Parent; // Parent to this scope.
Jim Grosbach042483e2009-11-21 23:12:12 +0000171 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel1973df22010-01-26 21:39:14 +0000172 // Location at which this scope is inlined.
173 AssertingVH<MDNode> InlinedAtLocation;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000174 bool AbstractScope; // Abstract Scope
Chris Lattnere13c3722010-03-09 01:58:53 +0000175 MCSymbol *StartLabel; // Label ID of the beginning of scope.
176 MCSymbol *EndLabel; // Label ID of the end of scope.
Devang Patel787f94c2009-10-01 18:25:23 +0000177 const MachineInstr *LastInsn; // Last instruction of this scope.
178 const MachineInstr *FirstInsn; // First instruction of this scope.
Douglas Gregor9abb5382010-03-08 02:58:37 +0000179 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
180 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000181
Owen Anderson9becc182009-06-24 22:53:20 +0000182 // Private state for dump()
183 mutable unsigned IndentLevel;
Bill Wendling2f921f82009-05-15 09:23:25 +0000184public:
Devang Patel6875c5eb2009-10-14 21:08:09 +0000185 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000186 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Chris Lattnere13c3722010-03-09 01:58:53 +0000187 StartLabel(0), EndLabel(0),
Devang Patel6875c5eb2009-10-14 21:08:09 +0000188 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendling2f921f82009-05-15 09:23:25 +0000189 virtual ~DbgScope();
190
191 // Accessors.
192 DbgScope *getParent() const { return Parent; }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000193 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling2f921f82009-05-15 09:23:25 +0000194 DIDescriptor getDesc() const { return Desc; }
Jim Grosbach042483e2009-11-21 23:12:12 +0000195 MDNode *getInlinedAt() const {
Devang Patel1973df22010-01-26 21:39:14 +0000196 return InlinedAtLocation;
Devang Patel6875c5eb2009-10-14 21:08:09 +0000197 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000198 MDNode *getScopeNode() const { return Desc.getNode(); }
Chris Lattnere13c3722010-03-09 01:58:53 +0000199 MCSymbol *getStartLabel() const { return StartLabel; }
200 MCSymbol *getEndLabel() const { return EndLabel; }
Douglas Gregor9abb5382010-03-08 02:58:37 +0000201 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
202 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Chris Lattnere13c3722010-03-09 01:58:53 +0000203 void setStartLabel(MCSymbol *S) { StartLabel = S; }
204 void setEndLabel(MCSymbol *E) { EndLabel = E; }
Devang Patel787f94c2009-10-01 18:25:23 +0000205 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
206 const MachineInstr *getLastInsn() { return LastInsn; }
207 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000208 void setAbstractScope() { AbstractScope = true; }
209 bool isAbstractScope() const { return AbstractScope; }
Devang Patel787f94c2009-10-01 18:25:23 +0000210 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000211
Devang Patel930143b2009-11-21 02:48:08 +0000212 /// addScope - Add a scope to the scope.
Bill Wendling2f921f82009-05-15 09:23:25 +0000213 ///
Devang Patel930143b2009-11-21 02:48:08 +0000214 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000215
Devang Patel930143b2009-11-21 02:48:08 +0000216 /// addVariable - Add a variable to the scope.
Bill Wendling2f921f82009-05-15 09:23:25 +0000217 ///
Devang Patel930143b2009-11-21 02:48:08 +0000218 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000219
Devang Patel530a0752010-01-04 20:44:00 +0000220 void fixInstructionMarkers(DenseMap<const MachineInstr *,
221 unsigned> &MIIndexMap) {
Devang Patel75cc16c2009-10-01 20:31:14 +0000222 assert (getFirstInsn() && "First instruction is missing!");
Devang Patel530a0752010-01-04 20:44:00 +0000223
224 // Use the end of last child scope as end of this scope.
Douglas Gregor9abb5382010-03-08 02:58:37 +0000225 SmallVector<DbgScope *, 4> &Scopes = getScopes();
Devang Pateld146e2e2010-01-05 16:59:17 +0000226 const MachineInstr *LastInsn = getFirstInsn();
Devang Patel530a0752010-01-04 20:44:00 +0000227 unsigned LIndex = 0;
228 if (Scopes.empty()) {
229 assert (getLastInsn() && "Inner most scope does not have last insn!");
230 return;
231 }
Douglas Gregor9abb5382010-03-08 02:58:37 +0000232 for (SmallVector<DbgScope *, 4>::iterator SI = Scopes.begin(),
Devang Patel530a0752010-01-04 20:44:00 +0000233 SE = Scopes.end(); SI != SE; ++SI) {
234 DbgScope *DS = *SI;
235 DS->fixInstructionMarkers(MIIndexMap);
236 const MachineInstr *DSLastInsn = DS->getLastInsn();
237 unsigned DSI = MIIndexMap[DSLastInsn];
238 if (DSI > LIndex) {
239 LastInsn = DSLastInsn;
240 LIndex = DSI;
241 }
242 }
Devang Patelca55a042010-02-17 02:20:34 +0000243
244 unsigned CurrentLastInsnIndex = 0;
245 if (const MachineInstr *CL = getLastInsn())
246 CurrentLastInsnIndex = MIIndexMap[CL];
247 unsigned FIndex = MIIndexMap[getFirstInsn()];
248
249 // Set LastInsn as the last instruction for this scope only if
250 // it follows
251 // 1) this scope's first instruction and
252 // 2) current last instruction for this scope, if any.
253 if (LIndex >= CurrentLastInsnIndex && LIndex >= FIndex)
254 setLastInsn(LastInsn);
Devang Patel75cc16c2009-10-01 20:31:14 +0000255 }
256
Bill Wendling2f921f82009-05-15 09:23:25 +0000257#ifndef NDEBUG
258 void dump() const;
259#endif
260};
261
262#ifndef NDEBUG
263void DbgScope::dump() const {
David Greenec230cb92009-12-24 00:31:35 +0000264 raw_ostream &err = dbgs();
Chris Lattner81e8e022009-08-23 00:51:00 +0000265 err.indent(IndentLevel);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000266 MDNode *N = Desc.getNode();
267 N->dump();
Chris Lattnere13c3722010-03-09 01:58:53 +0000268 err << " [" << StartLabel << ", " << EndLabel << "]\n";
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000269 if (AbstractScope)
270 err << "Abstract Scope\n";
Bill Wendling2f921f82009-05-15 09:23:25 +0000271
272 IndentLevel += 2;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000273 if (!Scopes.empty())
274 err << "Children ...\n";
Bill Wendling2f921f82009-05-15 09:23:25 +0000275 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
276 if (Scopes[i] != this)
277 Scopes[i]->dump();
278
279 IndentLevel -= 2;
280}
281#endif
282
Bill Wendling2f921f82009-05-15 09:23:25 +0000283DbgScope::~DbgScope() {
Douglas Gregor9abb5382010-03-08 02:58:37 +0000284 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
285 delete Scopes[i];
Bill Wendling2f921f82009-05-15 09:23:25 +0000286 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
287 delete Variables[j];
Bill Wendling2f921f82009-05-15 09:23:25 +0000288}
289
290} // end llvm namespace
291
Chris Lattner7b26fce2009-08-22 20:48:53 +0000292DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Chris Lattner4d728122010-03-09 00:00:15 +0000293 : DwarfPrinter(OS, A, T), ModuleCU(0),
Bill Wendling2f921f82009-05-15 09:23:25 +0000294 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Devang Patel930143b2009-11-21 02:48:08 +0000295 DIEValues(), StringPool(),
Bill Wendling2f921f82009-05-15 09:23:25 +0000296 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000297 CurrentFnDbgScope(0), DebugTimer(0) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000298 if (TimePassesIsEnabled)
Chris Lattnerf81add32009-12-28 07:41:18 +0000299 DebugTimer = new Timer("Dwarf Debug Writer");
Bill Wendling2f921f82009-05-15 09:23:25 +0000300}
301DwarfDebug::~DwarfDebug() {
Devang Patel930143b2009-11-21 02:48:08 +0000302 for (unsigned j = 0, M = DIEValues.size(); j < M; ++j)
303 delete DIEValues[j];
Bill Wendling2f921f82009-05-15 09:23:25 +0000304
Bill Wendling2f921f82009-05-15 09:23:25 +0000305 delete DebugTimer;
306}
307
Devang Patel930143b2009-11-21 02:48:08 +0000308/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling2f921f82009-05-15 09:23:25 +0000309///
Devang Patel930143b2009-11-21 02:48:08 +0000310void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000311 // Profile the node so that we can make it unique.
312 FoldingSetNodeID ID;
313 Abbrev.Profile(ID);
314
315 // Check the set for priors.
316 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
317
318 // If it's newly added.
319 if (InSet == &Abbrev) {
320 // Add to abbreviation list.
321 Abbreviations.push_back(&Abbrev);
322
323 // Assign the vector position + 1 as its number.
324 Abbrev.setNumber(Abbreviations.size());
325 } else {
326 // Assign existing abbreviation number.
327 Abbrev.setNumber(InSet->getNumber());
328 }
329}
330
Devang Patel930143b2009-11-21 02:48:08 +0000331/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendlingbcad77a2009-05-20 23:24:48 +0000332/// information entry.
Devang Patel930143b2009-11-21 02:48:08 +0000333DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Devang Patel92e8c652009-11-21 00:31:03 +0000334 DIEEntry *Value = new DIEEntry(Entry);
Devang Patel930143b2009-11-21 02:48:08 +0000335 DIEValues.push_back(Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000336 return Value;
337}
338
Devang Patel930143b2009-11-21 02:48:08 +0000339/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000340///
Devang Patel930143b2009-11-21 02:48:08 +0000341void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000342 unsigned Form, uint64_t Integer) {
343 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Devang Patel92e8c652009-11-21 00:31:03 +0000344 DIEValue *Value = new DIEInteger(Integer);
Devang Patel930143b2009-11-21 02:48:08 +0000345 DIEValues.push_back(Value);
346 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000347}
348
Devang Patel930143b2009-11-21 02:48:08 +0000349/// addSInt - Add an signed integer attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000350///
Devang Patel930143b2009-11-21 02:48:08 +0000351void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000352 unsigned Form, int64_t Integer) {
353 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Devang Patel92e8c652009-11-21 00:31:03 +0000354 DIEValue *Value = new DIEInteger(Integer);
Devang Patel930143b2009-11-21 02:48:08 +0000355 DIEValues.push_back(Value);
356 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000357}
358
Devang Patel8c339592009-12-02 15:25:16 +0000359/// addString - Add a string attribute data and value. DIEString only
360/// keeps string reference.
Devang Patel930143b2009-11-21 02:48:08 +0000361void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerc3f23b82010-01-23 03:11:46 +0000362 StringRef String) {
Devang Patel92e8c652009-11-21 00:31:03 +0000363 DIEValue *Value = new DIEString(String);
Devang Patel930143b2009-11-21 02:48:08 +0000364 DIEValues.push_back(Value);
365 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000366}
367
Devang Patel930143b2009-11-21 02:48:08 +0000368/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000369///
Devang Patel930143b2009-11-21 02:48:08 +0000370void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000371 const MCSymbol *Label) {
Chris Lattner8dcf41e2010-03-08 22:31:46 +0000372 DIEValue *Value = new DIELabel(Label);
Devang Patel930143b2009-11-21 02:48:08 +0000373 DIEValues.push_back(Value);
374 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000375}
376
Devang Patel930143b2009-11-21 02:48:08 +0000377/// addSectionOffset - Add a section offset label attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000378///
Devang Patel930143b2009-11-21 02:48:08 +0000379void DwarfDebug::addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000380 const MCSymbol *Label,const MCSymbol *Section,
Chris Lattner27a97322010-03-08 23:23:25 +0000381 bool isEH) {
382 DIEValue *Value = new DIESectionOffset(Label, Section, isEH);
Devang Patel930143b2009-11-21 02:48:08 +0000383 DIEValues.push_back(Value);
384 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000385}
386
Devang Patel930143b2009-11-21 02:48:08 +0000387/// addDelta - Add a label delta attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000388///
Devang Patel930143b2009-11-21 02:48:08 +0000389void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000390 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel92e8c652009-11-21 00:31:03 +0000391 DIEValue *Value = new DIEDelta(Hi, Lo);
Devang Patel930143b2009-11-21 02:48:08 +0000392 DIEValues.push_back(Value);
393 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000394}
395
Devang Patel930143b2009-11-21 02:48:08 +0000396/// addBlock - Add block data.
Bill Wendling2f921f82009-05-15 09:23:25 +0000397///
Devang Patel930143b2009-11-21 02:48:08 +0000398void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling2f921f82009-05-15 09:23:25 +0000399 DIEBlock *Block) {
400 Block->ComputeSize(TD);
Devang Patel930143b2009-11-21 02:48:08 +0000401 DIEValues.push_back(Block);
402 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling2f921f82009-05-15 09:23:25 +0000403}
404
Devang Patel930143b2009-11-21 02:48:08 +0000405/// addSourceLine - Add location information to specified debug information
Bill Wendling2f921f82009-05-15 09:23:25 +0000406/// entry.
Devang Patel930143b2009-11-21 02:48:08 +0000407void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000408 // If there is no compile unit specified, don't add a line #.
Devang Patel3b548aa2010-03-08 20:52:55 +0000409 if (!V->getCompileUnit().Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000410 return;
411
412 unsigned Line = V->getLineNumber();
Devang Patel8119fe872010-03-08 22:02:50 +0000413 unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(),
414 V->getContext().getFilename());
Bill Wendling2f921f82009-05-15 09:23:25 +0000415 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000416 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
417 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling2f921f82009-05-15 09:23:25 +0000418}
419
Devang Patel930143b2009-11-21 02:48:08 +0000420/// addSourceLine - Add location information to specified debug information
Bill Wendling2f921f82009-05-15 09:23:25 +0000421/// entry.
Devang Patel930143b2009-11-21 02:48:08 +0000422void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000423 // If there is no compile unit specified, don't add a line #.
Devang Patel3b548aa2010-03-08 20:52:55 +0000424 if (!G->getCompileUnit().Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000425 return;
426
427 unsigned Line = G->getLineNumber();
Devang Patel8119fe872010-03-08 22:02:50 +0000428 unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(),
429 G->getContext().getFilename());
Bill Wendling2f921f82009-05-15 09:23:25 +0000430 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000431 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
432 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling2f921f82009-05-15 09:23:25 +0000433}
Devang Patelb2de5fa2009-08-31 22:47:13 +0000434
Devang Patel930143b2009-11-21 02:48:08 +0000435/// addSourceLine - Add location information to specified debug information
Devang Patelb2de5fa2009-08-31 22:47:13 +0000436/// entry.
Devang Patel930143b2009-11-21 02:48:08 +0000437void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patelb2de5fa2009-08-31 22:47:13 +0000438 // If there is no compile unit specified, don't add a line #.
Devang Patel3b548aa2010-03-08 20:52:55 +0000439 if (!SP->getCompileUnit().Verify())
Devang Patelb2de5fa2009-08-31 22:47:13 +0000440 return;
Caroline Tice183a5192009-09-11 18:25:54 +0000441 // If the line number is 0, don't add it.
442 if (SP->getLineNumber() == 0)
443 return;
444
Devang Patelb2de5fa2009-08-31 22:47:13 +0000445 unsigned Line = SP->getLineNumber();
Devang Patel8119fe872010-03-08 22:02:50 +0000446 if (!SP->getContext().Verify())
447 return;
448 unsigned FileID = GetOrCreateSourceID(SP->getContext().getDirectory(),
449 SP->getContext().getFilename());
Devang Patelb2de5fa2009-08-31 22:47:13 +0000450 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000451 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
452 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patelb2de5fa2009-08-31 22:47:13 +0000453}
454
Devang Patel930143b2009-11-21 02:48:08 +0000455/// addSourceLine - Add location information to specified debug information
Devang Patelb2de5fa2009-08-31 22:47:13 +0000456/// entry.
Devang Patel930143b2009-11-21 02:48:08 +0000457void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000458 // If there is no compile unit specified, don't add a line #.
459 DICompileUnit CU = Ty->getCompileUnit();
Devang Patel3b548aa2010-03-08 20:52:55 +0000460 if (!CU.Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000461 return;
462
463 unsigned Line = Ty->getLineNumber();
Devang Patel8119fe872010-03-08 22:02:50 +0000464 if (!Ty->getContext().Verify())
465 return;
466 unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(),
467 Ty->getContext().getFilename());
Bill Wendling2f921f82009-05-15 09:23:25 +0000468 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000469 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
470 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling2f921f82009-05-15 09:23:25 +0000471}
472
Devang Patel1f4690c2009-12-15 19:16:48 +0000473/// addSourceLine - Add location information to specified debug information
474/// entry.
475void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
476 // If there is no compile unit specified, don't add a line #.
Devang Patel3b548aa2010-03-08 20:52:55 +0000477 if (!NS->getCompileUnit().Verify())
Devang Patel1f4690c2009-12-15 19:16:48 +0000478 return;
479
480 unsigned Line = NS->getLineNumber();
481 StringRef FN = NS->getFilename();
482 StringRef Dir = NS->getDirectory();
483
484 unsigned FileID = GetOrCreateSourceID(Dir, FN);
485 assert(FileID && "Invalid file id");
486 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
487 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
488}
489
Caroline Ticec87c1e22009-08-31 21:19:37 +0000490/* Byref variables, in Blocks, are declared by the programmer as
491 "SomeType VarName;", but the compiler creates a
492 __Block_byref_x_VarName struct, and gives the variable VarName
493 either the struct, or a pointer to the struct, as its type. This
494 is necessary for various behind-the-scenes things the compiler
495 needs to do with by-reference variables in blocks.
496
497 However, as far as the original *programmer* is concerned, the
498 variable should still have type 'SomeType', as originally declared.
499
500 The following function dives into the __Block_byref_x_VarName
501 struct to find the original type of the variable. This will be
502 passed back to the code generating the type for the Debug
503 Information Entry for the variable 'VarName'. 'VarName' will then
504 have the original type 'SomeType' in its debug information.
505
506 The original type 'SomeType' will be the type of the field named
507 'VarName' inside the __Block_byref_x_VarName struct.
508
509 NOTE: In order for this to not completely fail on the debugger
510 side, the Debug Information Entry for the variable VarName needs to
511 have a DW_AT_location that tells the debugger how to unwind through
512 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patel930143b2009-11-21 02:48:08 +0000513 value of the variable. The function addBlockByrefType does this. */
Caroline Ticec87c1e22009-08-31 21:19:37 +0000514
515/// Find the type the programmer originally declared the variable to be
516/// and return that type.
517///
Devang Patel930143b2009-11-21 02:48:08 +0000518DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Ticec87c1e22009-08-31 21:19:37 +0000519
520 DIType subType = Ty;
521 unsigned tag = Ty.getTag();
522
523 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stump14cf8ec2009-09-30 00:08:22 +0000524 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticec87c1e22009-08-31 21:19:37 +0000525 subType = DTy.getTypeDerivedFrom();
526 }
527
528 DICompositeType blockStruct = DICompositeType(subType.getNode());
Caroline Ticec87c1e22009-08-31 21:19:37 +0000529 DIArray Elements = blockStruct.getTypeArray();
530
Caroline Ticec87c1e22009-08-31 21:19:37 +0000531 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
532 DIDescriptor Element = Elements.getElement(i);
533 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel2d9caf92009-11-25 17:36:49 +0000534 if (Name == DT.getName())
Caroline Ticec87c1e22009-08-31 21:19:37 +0000535 return (DT.getTypeDerivedFrom());
536 }
537
538 return Ty;
539}
540
Devang Patel930143b2009-11-21 02:48:08 +0000541/// addComplexAddress - Start with the address based on the location provided,
Mike Stump14cf8ec2009-09-30 00:08:22 +0000542/// and generate the DWARF information necessary to find the actual variable
543/// given the extra address information encoded in the DIVariable, starting from
544/// the starting location. Add the DWARF information to the die.
545///
Devang Patel930143b2009-11-21 02:48:08 +0000546void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump14cf8ec2009-09-30 00:08:22 +0000547 unsigned Attribute,
548 const MachineLocation &Location) {
549 const DIVariable &VD = DV->getVariable();
550 DIType Ty = VD.getType();
551
552 // Decode the original location, and use that as the start of the byref
553 // variable's location.
554 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
555 DIEBlock *Block = new DIEBlock();
556
557 if (Location.isReg()) {
558 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000559 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000560 } else {
561 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel930143b2009-11-21 02:48:08 +0000562 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
563 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000564 }
565 } else {
566 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000567 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000568 else {
Devang Patel930143b2009-11-21 02:48:08 +0000569 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
570 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000571 }
572
Devang Patel930143b2009-11-21 02:48:08 +0000573 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump14cf8ec2009-09-30 00:08:22 +0000574 }
575
576 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
577 uint64_t Element = VD.getAddrElement(i);
578
579 if (Element == DIFactory::OpPlus) {
Devang Patel930143b2009-11-21 02:48:08 +0000580 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
581 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stump14cf8ec2009-09-30 00:08:22 +0000582 } else if (Element == DIFactory::OpDeref) {
Devang Patel930143b2009-11-21 02:48:08 +0000583 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000584 } else llvm_unreachable("unknown DIFactory Opcode");
585 }
586
587 // Now attach the location information to the DIE.
Devang Patel930143b2009-11-21 02:48:08 +0000588 addBlock(Die, Attribute, 0, Block);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000589}
590
Caroline Ticec87c1e22009-08-31 21:19:37 +0000591/* Byref variables, in Blocks, are declared by the programmer as "SomeType
592 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
593 gives the variable VarName either the struct, or a pointer to the struct, as
594 its type. This is necessary for various behind-the-scenes things the
595 compiler needs to do with by-reference variables in Blocks.
596
597 However, as far as the original *programmer* is concerned, the variable
598 should still have type 'SomeType', as originally declared.
599
Devang Patel930143b2009-11-21 02:48:08 +0000600 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticec87c1e22009-08-31 21:19:37 +0000601 struct to find the original type of the variable, which is then assigned to
602 the variable's Debug Information Entry as its real type. So far, so good.
603 However now the debugger will expect the variable VarName to have the type
604 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000605 expression that explains to the debugger how to navigate through the
Caroline Ticec87c1e22009-08-31 21:19:37 +0000606 pointers and struct to find the actual variable of type SomeType.
607
608 The following function does just that. We start by getting
609 the "normal" location for the variable. This will be the location
610 of either the struct __Block_byref_x_VarName or the pointer to the
611 struct __Block_byref_x_VarName.
612
613 The struct will look something like:
614
615 struct __Block_byref_x_VarName {
616 ... <various fields>
617 struct __Block_byref_x_VarName *forwarding;
618 ... <various other fields>
619 SomeType VarName;
620 ... <maybe more fields>
621 };
622
623 If we are given the struct directly (as our starting point) we
624 need to tell the debugger to:
625
626 1). Add the offset of the forwarding field.
627
Dan Gohman4a618822010-02-10 16:03:48 +0000628 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticec87c1e22009-08-31 21:19:37 +0000629 struct to use (the real one may have been copied onto the heap).
630
631 3). Add the offset for the field VarName, to find the actual variable.
632
633 If we started with a pointer to the struct, then we need to
634 dereference that pointer first, before the other steps.
635 Translating this into DWARF ops, we will need to append the following
636 to the current location description for the variable:
637
638 DW_OP_deref -- optional, if we start with a pointer
639 DW_OP_plus_uconst <forward_fld_offset>
640 DW_OP_deref
641 DW_OP_plus_uconst <varName_fld_offset>
642
643 That is what this function does. */
644
Devang Patel930143b2009-11-21 02:48:08 +0000645/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticec87c1e22009-08-31 21:19:37 +0000646/// provided, and generate the DWARF information necessary to find the
647/// actual Block variable (navigating the Block struct) based on the
648/// starting location. Add the DWARF information to the die. For
649/// more information, read large comment just above here.
650///
Devang Patel930143b2009-11-21 02:48:08 +0000651void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000652 unsigned Attribute,
653 const MachineLocation &Location) {
Caroline Ticec87c1e22009-08-31 21:19:37 +0000654 const DIVariable &VD = DV->getVariable();
655 DIType Ty = VD.getType();
656 DIType TmpTy = Ty;
657 unsigned Tag = Ty.getTag();
658 bool isPointer = false;
659
Devang Patel2d9caf92009-11-25 17:36:49 +0000660 StringRef varName = VD.getName();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000661
662 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stump14cf8ec2009-09-30 00:08:22 +0000663 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticec87c1e22009-08-31 21:19:37 +0000664 TmpTy = DTy.getTypeDerivedFrom();
665 isPointer = true;
666 }
667
668 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
669
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000670 // Find the __forwarding field and the variable field in the __Block_byref
671 // struct.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000672 DIArray Fields = blockStruct.getTypeArray();
673 DIDescriptor varField = DIDescriptor();
674 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000675
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000676 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
677 DIDescriptor Element = Fields.getElement(i);
678 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel2d9caf92009-11-25 17:36:49 +0000679 StringRef fieldName = DT.getName();
680 if (fieldName == "__forwarding")
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000681 forwardingField = Element;
Devang Patel2d9caf92009-11-25 17:36:49 +0000682 else if (fieldName == varName)
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000683 varField = Element;
684 }
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000685
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000686 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000687 unsigned int forwardingFieldOffset =
688 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
689 unsigned int varFieldOffset =
690 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Ticec87c1e22009-08-31 21:19:37 +0000691
Mike Stump944fa252009-09-24 23:21:26 +0000692 // Decode the original location, and use that as the start of the byref
693 // variable's location.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000694 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
695 DIEBlock *Block = new DIEBlock();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000696
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000697 if (Location.isReg()) {
698 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000699 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000700 else {
701 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel930143b2009-11-21 02:48:08 +0000702 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
703 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000704 }
705 } else {
706 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000707 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000708 else {
Devang Patel930143b2009-11-21 02:48:08 +0000709 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
710 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000711 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000712
Devang Patel930143b2009-11-21 02:48:08 +0000713 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000714 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000715
Mike Stump944fa252009-09-24 23:21:26 +0000716 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000717 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000718 if (isPointer)
Devang Patel930143b2009-11-21 02:48:08 +0000719 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000720
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000721 // Next add the offset for the '__forwarding' field:
722 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
723 // adding the offset if it's 0.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000724 if (forwardingFieldOffset > 0) {
Devang Patel930143b2009-11-21 02:48:08 +0000725 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
726 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000727 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000728
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000729 // Now dereference the __forwarding field to get to the real __Block_byref
730 // struct: DW_OP_deref.
Devang Patel930143b2009-11-21 02:48:08 +0000731 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000732
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000733 // Now that we've got the real __Block_byref... struct, add the offset
734 // for the variable's field to get to the location of the actual variable:
735 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000736 if (varFieldOffset > 0) {
Devang Patel930143b2009-11-21 02:48:08 +0000737 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
738 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000739 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000740
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000741 // Now attach the location information to the DIE.
Devang Patel930143b2009-11-21 02:48:08 +0000742 addBlock(Die, Attribute, 0, Block);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000743}
744
Devang Patel930143b2009-11-21 02:48:08 +0000745/// addAddress - Add an address attribute to a die based on the location
Bill Wendling2f921f82009-05-15 09:23:25 +0000746/// provided.
Devang Patel930143b2009-11-21 02:48:08 +0000747void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000748 const MachineLocation &Location) {
749 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
750 DIEBlock *Block = new DIEBlock();
751
752 if (Location.isReg()) {
753 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000754 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000755 } else {
Devang Patel930143b2009-11-21 02:48:08 +0000756 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
757 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000758 }
759 } else {
760 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000761 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000762 } else {
Devang Patel930143b2009-11-21 02:48:08 +0000763 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
764 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000765 }
766
Devang Patel930143b2009-11-21 02:48:08 +0000767 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling2f921f82009-05-15 09:23:25 +0000768 }
769
Devang Patel930143b2009-11-21 02:48:08 +0000770 addBlock(Die, Attribute, 0, Block);
Bill Wendling2f921f82009-05-15 09:23:25 +0000771}
772
Devang Patel2b75ed22009-12-10 19:14:49 +0000773/// addToContextOwner - Add Die into the list of its context owner's children.
774void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000775 if (Context.isType()) {
Devang Patel2b75ed22009-12-10 19:14:49 +0000776 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
777 ContextDIE->addChild(Die);
Devang Patel1f4690c2009-12-15 19:16:48 +0000778 } else if (Context.isNameSpace()) {
779 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context.getNode()));
780 ContextDIE->addChild(Die);
Devang Patel2b75ed22009-12-10 19:14:49 +0000781 } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
782 ContextDIE->addChild(Die);
783 else
784 ModuleCU->addDie(Die);
785}
786
Devang Patelb5b60ea2009-12-10 18:05:33 +0000787/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
788/// given DIType.
789DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
790 DIE *TyDIE = ModuleCU->getDIE(Ty.getNode());
791 if (TyDIE)
792 return TyDIE;
793
794 // Create new type.
795 TyDIE = new DIE(dwarf::DW_TAG_base_type);
796 ModuleCU->insertDIE(Ty.getNode(), TyDIE);
797 if (Ty.isBasicType())
798 constructTypeDIE(*TyDIE, DIBasicType(Ty.getNode()));
799 else if (Ty.isCompositeType())
800 constructTypeDIE(*TyDIE, DICompositeType(Ty.getNode()));
801 else {
802 assert(Ty.isDerivedType() && "Unknown kind of DIType");
803 constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode()));
804 }
805
Devang Patel2b75ed22009-12-10 19:14:49 +0000806 addToContextOwner(TyDIE, Ty.getContext());
Devang Patelb5b60ea2009-12-10 18:05:33 +0000807 return TyDIE;
808}
809
Devang Patel930143b2009-11-21 02:48:08 +0000810/// addType - Add a new type attribute to the specified entity.
Devang Patel9ccfb642009-12-09 18:24:21 +0000811void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000812 if (!Ty.isValid())
Bill Wendling2f921f82009-05-15 09:23:25 +0000813 return;
814
815 // Check for pre-existence.
Devang Patel9ccfb642009-12-09 18:24:21 +0000816 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty.getNode());
Bill Wendling2f921f82009-05-15 09:23:25 +0000817 // If it exists then use the existing value.
Devang Patel930143b2009-11-21 02:48:08 +0000818 if (Entry) {
819 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000820 return;
821 }
822
823 // Set up proxy.
Devang Patel930143b2009-11-21 02:48:08 +0000824 Entry = createDIEEntry();
Devang Patel9ccfb642009-12-09 18:24:21 +0000825 ModuleCU->insertDIEEntry(Ty.getNode(), Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000826
827 // Construct type.
Devang Patelb5b60ea2009-12-10 18:05:33 +0000828 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling2f921f82009-05-15 09:23:25 +0000829
Devang Patel930143b2009-11-21 02:48:08 +0000830 Entry->setEntry(Buffer);
831 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000832}
833
Devang Patel930143b2009-11-21 02:48:08 +0000834/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel9ccfb642009-12-09 18:24:21 +0000835void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000836 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000837 StringRef Name = BTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000838 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel930143b2009-11-21 02:48:08 +0000839 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling2f921f82009-05-15 09:23:25 +0000840 BTy.getEncoding());
841
842 // Add name if not anonymous or intermediate type.
Devang Patel2d9caf92009-11-25 17:36:49 +0000843 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +0000844 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +0000845 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel930143b2009-11-21 02:48:08 +0000846 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +0000847}
848
Devang Patel930143b2009-11-21 02:48:08 +0000849/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel9ccfb642009-12-09 18:24:21 +0000850void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000851 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000852 StringRef Name = DTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000853 uint64_t Size = DTy.getSizeInBits() >> 3;
854 unsigned Tag = DTy.getTag();
855
856 // FIXME - Workaround for templates.
857 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
858
859 Buffer.setTag(Tag);
860
861 // Map to main type, void will not have a type.
862 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel9ccfb642009-12-09 18:24:21 +0000863 addType(&Buffer, FromTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000864
865 // Add name if not anonymous or intermediate type.
Devang Patelae466ef2009-11-30 23:56:56 +0000866 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +0000867 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +0000868
869 // Add size if non-zero (derived types might be zero-sized.)
870 if (Size)
Devang Patel930143b2009-11-21 02:48:08 +0000871 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +0000872
873 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb5b51592009-11-23 18:43:37 +0000874 if (!DTy.isForwardDecl())
Devang Patel930143b2009-11-21 02:48:08 +0000875 addSourceLine(&Buffer, &DTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000876}
877
Devang Patel930143b2009-11-21 02:48:08 +0000878/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel9ccfb642009-12-09 18:24:21 +0000879void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000880 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000881 StringRef Name = CTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000882
883 uint64_t Size = CTy.getSizeInBits() >> 3;
884 unsigned Tag = CTy.getTag();
885 Buffer.setTag(Tag);
886
887 switch (Tag) {
888 case dwarf::DW_TAG_vector_type:
889 case dwarf::DW_TAG_array_type:
Devang Patel9ccfb642009-12-09 18:24:21 +0000890 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000891 break;
892 case dwarf::DW_TAG_enumeration_type: {
893 DIArray Elements = CTy.getTypeArray();
894
895 // Add enumerators to enumeration type.
896 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
897 DIE *ElemDie = NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +0000898 DIDescriptor Enum(Elements.getElement(i).getNode());
899 if (Enum.isEnumerator()) {
900 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum.getNode()));
Devang Patel930143b2009-11-21 02:48:08 +0000901 Buffer.addChild(ElemDie);
Devang Patelfafa1fe2009-10-09 17:51:49 +0000902 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000903 }
904 }
905 break;
906 case dwarf::DW_TAG_subroutine_type: {
907 // Add return type.
908 DIArray Elements = CTy.getTypeArray();
909 DIDescriptor RTy = Elements.getElement(0);
Devang Patel9ccfb642009-12-09 18:24:21 +0000910 addType(&Buffer, DIType(RTy.getNode()));
Bill Wendling2f921f82009-05-15 09:23:25 +0000911
912 // Add prototype flag.
Devang Patel930143b2009-11-21 02:48:08 +0000913 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +0000914
915 // Add arguments.
916 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
917 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
918 DIDescriptor Ty = Elements.getElement(i);
Devang Patel9ccfb642009-12-09 18:24:21 +0000919 addType(Arg, DIType(Ty.getNode()));
Devang Patel930143b2009-11-21 02:48:08 +0000920 Buffer.addChild(Arg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000921 }
922 }
923 break;
924 case dwarf::DW_TAG_structure_type:
925 case dwarf::DW_TAG_union_type:
926 case dwarf::DW_TAG_class_type: {
927 // Add elements to structure type.
928 DIArray Elements = CTy.getTypeArray();
929
930 // A forward struct declared type may not have elements available.
Devang Patel3b548aa2010-03-08 20:52:55 +0000931 unsigned N = Elements.getNumElements();
932 if (N == 0)
Bill Wendling2f921f82009-05-15 09:23:25 +0000933 break;
934
935 // Add elements to structure type.
Devang Patel3b548aa2010-03-08 20:52:55 +0000936 for (unsigned i = 0; i < N; ++i) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000937 DIDescriptor Element = Elements.getElement(i);
938 DIE *ElemDie = NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +0000939 if (Element.isSubprogram())
Devang Patel525dda02009-12-14 16:18:45 +0000940 ElemDie = createSubprogramDIE(DISubprogram(Element.getNode()));
Devang Patel3b548aa2010-03-08 20:52:55 +0000941 else if (Element.isVariable()) {
Devang Patel160c92d2010-01-30 01:08:30 +0000942 DIVariable DV(Element.getNode());
943 ElemDie = new DIE(dwarf::DW_TAG_variable);
944 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
945 DV.getName());
946 addType(ElemDie, DV.getType());
947 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
948 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
949 addSourceLine(ElemDie, &DV);
Devang Patel3b548aa2010-03-08 20:52:55 +0000950 } else if (Element.isDerivedType())
Devang Patel9ccfb642009-12-09 18:24:21 +0000951 ElemDie = createMemberDIE(DIDerivedType(Element.getNode()));
Devang Patel3b548aa2010-03-08 20:52:55 +0000952 else
953 continue;
Devang Patel930143b2009-11-21 02:48:08 +0000954 Buffer.addChild(ElemDie);
Bill Wendling2f921f82009-05-15 09:23:25 +0000955 }
956
Devang Patel3082c012009-08-27 23:51:51 +0000957 if (CTy.isAppleBlockExtension())
Devang Patel930143b2009-11-21 02:48:08 +0000958 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +0000959
960 unsigned RLang = CTy.getRunTimeLang();
961 if (RLang)
Devang Patel930143b2009-11-21 02:48:08 +0000962 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling2f921f82009-05-15 09:23:25 +0000963 dwarf::DW_FORM_data1, RLang);
Devang Patel303a1be2010-01-26 21:16:06 +0000964
965 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel3b548aa2010-03-08 20:52:55 +0000966 if (DIDescriptor(ContainingType.getNode()).isCompositeType())
Devang Patel303a1be2010-01-26 21:16:06 +0000967 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
968 getOrCreateTypeDIE(DIType(ContainingType.getNode())));
Bill Wendling2f921f82009-05-15 09:23:25 +0000969 break;
970 }
971 default:
972 break;
973 }
974
975 // Add name if not anonymous or intermediate type.
Devang Patel2d9caf92009-11-25 17:36:49 +0000976 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +0000977 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +0000978
Devang Patelaedd6f52010-01-29 18:34:58 +0000979 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type ||
Bill Wendling2f921f82009-05-15 09:23:25 +0000980 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
981 // Add size if non-zero (derived types might be zero-sized.)
982 if (Size)
Devang Patel930143b2009-11-21 02:48:08 +0000983 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +0000984 else {
985 // Add zero size if it is not a forward declaration.
986 if (CTy.isForwardDecl())
Devang Patel930143b2009-11-21 02:48:08 +0000987 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +0000988 else
Devang Patel930143b2009-11-21 02:48:08 +0000989 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling2f921f82009-05-15 09:23:25 +0000990 }
991
992 // Add source line info if available.
993 if (!CTy.isForwardDecl())
Devang Patel930143b2009-11-21 02:48:08 +0000994 addSourceLine(&Buffer, &CTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000995 }
996}
997
Devang Patel930143b2009-11-21 02:48:08 +0000998/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
999void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling2f921f82009-05-15 09:23:25 +00001000 int64_t L = SR.getLo();
1001 int64_t H = SR.getHi();
1002 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1003
Devang Patel930143b2009-11-21 02:48:08 +00001004 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patelf691df32009-08-14 20:59:16 +00001005 if (L)
Devang Patel930143b2009-11-21 02:48:08 +00001006 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Patel8f046022009-12-04 23:10:24 +00001007 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling2f921f82009-05-15 09:23:25 +00001008
Devang Patel930143b2009-11-21 02:48:08 +00001009 Buffer.addChild(DW_Subrange);
Bill Wendling2f921f82009-05-15 09:23:25 +00001010}
1011
Devang Patel930143b2009-11-21 02:48:08 +00001012/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel9ccfb642009-12-09 18:24:21 +00001013void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling2f921f82009-05-15 09:23:25 +00001014 DICompositeType *CTy) {
1015 Buffer.setTag(dwarf::DW_TAG_array_type);
1016 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel930143b2009-11-21 02:48:08 +00001017 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001018
1019 // Emit derived type.
Devang Patel9ccfb642009-12-09 18:24:21 +00001020 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling2f921f82009-05-15 09:23:25 +00001021 DIArray Elements = CTy->getTypeArray();
1022
Devang Patel92e8c652009-11-21 00:31:03 +00001023 // Get an anonymous type for index type.
Devang Patel9ccfb642009-12-09 18:24:21 +00001024 DIE *IdxTy = ModuleCU->getIndexTyDie();
Devang Patel92e8c652009-11-21 00:31:03 +00001025 if (!IdxTy) {
1026 // Construct an anonymous type for index type.
1027 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel930143b2009-11-21 02:48:08 +00001028 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1029 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel92e8c652009-11-21 00:31:03 +00001030 dwarf::DW_ATE_signed);
Devang Patel9ccfb642009-12-09 18:24:21 +00001031 ModuleCU->addDie(IdxTy);
1032 ModuleCU->setIndexTyDie(IdxTy);
Devang Patel92e8c652009-11-21 00:31:03 +00001033 }
Bill Wendling2f921f82009-05-15 09:23:25 +00001034
1035 // Add subranges to array type.
1036 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1037 DIDescriptor Element = Elements.getElement(i);
1038 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel930143b2009-11-21 02:48:08 +00001039 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendling2f921f82009-05-15 09:23:25 +00001040 }
1041}
1042
Devang Patel930143b2009-11-21 02:48:08 +00001043/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3b548aa2010-03-08 20:52:55 +00001044DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001045 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3b548aa2010-03-08 20:52:55 +00001046 StringRef Name = ETy.getName();
Devang Patel930143b2009-11-21 02:48:08 +00001047 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3b548aa2010-03-08 20:52:55 +00001048 int64_t Value = ETy.getEnumValue();
Devang Patel930143b2009-11-21 02:48:08 +00001049 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +00001050 return Enumerator;
1051}
1052
Devang Patel43ef34d2010-01-05 01:46:14 +00001053/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1054/// printer to not emit usual symbol prefix before the symbol name is used then
1055/// return linkage name after skipping this special LLVM prefix.
1056static StringRef getRealLinkageName(StringRef LinkageName) {
1057 char One = '\1';
1058 if (LinkageName.startswith(StringRef(&One, 1)))
1059 return LinkageName.substr(1);
1060 return LinkageName;
1061}
1062
Devang Patel930143b2009-11-21 02:48:08 +00001063/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patel9ccfb642009-12-09 18:24:21 +00001064DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbach00e9c612009-11-22 19:20:36 +00001065 // If the global variable was optmized out then no need to create debug info
1066 // entry.
Devang Patelcc11371b2009-11-06 17:58:12 +00001067 if (!GV.getGlobal()) return NULL;
Devang Patel2d9caf92009-11-25 17:36:49 +00001068 if (GV.getDisplayName().empty()) return NULL;
Devang Patel06ce6502009-11-06 01:30:04 +00001069
Bill Wendling2f921f82009-05-15 09:23:25 +00001070 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach042483e2009-11-21 23:12:12 +00001071 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelb2969422009-09-29 18:40:58 +00001072 GV.getDisplayName());
1073
Devang Patel2d9caf92009-11-25 17:36:49 +00001074 StringRef LinkageName = GV.getLinkageName();
Devang Patel43ef34d2010-01-05 01:46:14 +00001075 if (!LinkageName.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001076 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel43ef34d2010-01-05 01:46:14 +00001077 getRealLinkageName(LinkageName));
1078
Devang Patel9ccfb642009-12-09 18:24:21 +00001079 addType(GVDie, GV.getType());
Bill Wendling2f921f82009-05-15 09:23:25 +00001080 if (!GV.isLocalToUnit())
Devang Patel930143b2009-11-21 02:48:08 +00001081 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1082 addSourceLine(GVDie, &GV);
Devang Patel4144a822009-10-05 23:22:08 +00001083
Bill Wendling2f921f82009-05-15 09:23:25 +00001084 return GVDie;
1085}
1086
Devang Patel930143b2009-11-21 02:48:08 +00001087/// createMemberDIE - Create new member DIE.
Devang Patel9ccfb642009-12-09 18:24:21 +00001088DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001089 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel2d9caf92009-11-25 17:36:49 +00001090 StringRef Name = DT.getName();
1091 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001092 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel2d9caf92009-11-25 17:36:49 +00001093
Devang Patel9ccfb642009-12-09 18:24:21 +00001094 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling2f921f82009-05-15 09:23:25 +00001095
Devang Patel930143b2009-11-21 02:48:08 +00001096 addSourceLine(MemberDie, &DT);
Bill Wendling2f921f82009-05-15 09:23:25 +00001097
Devang Patel67f56f02009-11-04 22:06:12 +00001098 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patel930143b2009-11-21 02:48:08 +00001099 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel67f56f02009-11-04 22:06:12 +00001100
Bill Wendling2f921f82009-05-15 09:23:25 +00001101 uint64_t Size = DT.getSizeInBits();
Devang Patelf05d5722009-11-04 23:48:00 +00001102 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling2f921f82009-05-15 09:23:25 +00001103
1104 if (Size != FieldSize) {
1105 // Handle bitfield.
Devang Patel930143b2009-11-21 02:48:08 +00001106 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1107 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling2f921f82009-05-15 09:23:25 +00001108
1109 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling2f921f82009-05-15 09:23:25 +00001110 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1111 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer2b459982010-01-07 17:50:57 +00001112 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling2f921f82009-05-15 09:23:25 +00001113 Offset -= FieldOffset;
1114
1115 // Maybe we need to work from the other end.
1116 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patel930143b2009-11-21 02:48:08 +00001117 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling2f921f82009-05-15 09:23:25 +00001118
Devang Patel67f56f02009-11-04 22:06:12 +00001119 // Here WD_AT_data_member_location points to the anonymous
1120 // field that includes this bit field.
Devang Patel930143b2009-11-21 02:48:08 +00001121 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel67f56f02009-11-04 22:06:12 +00001122
1123 } else
1124 // This is not a bitfield.
Devang Patel930143b2009-11-21 02:48:08 +00001125 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel67f56f02009-11-04 22:06:12 +00001126
Devang Pateld2316892010-02-03 20:08:48 +00001127 if (DT.getTag() == dwarf::DW_TAG_inheritance
1128 && DT.isVirtual()) {
1129
1130 // For C++, virtual base classes are not at fixed offset. Use following
1131 // expression to extract appropriate offset from vtable.
1132 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1133
1134 DIEBlock *VBaseLocationDie = new DIEBlock();
1135 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1136 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1137 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1138 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1139 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1140 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1141 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1142
1143 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1144 VBaseLocationDie);
1145 } else
1146 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling2f921f82009-05-15 09:23:25 +00001147
1148 if (DT.isProtected())
Devang Pateleb57c592009-12-03 19:11:07 +00001149 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling2f921f82009-05-15 09:23:25 +00001150 dwarf::DW_ACCESS_protected);
1151 else if (DT.isPrivate())
Devang Pateleb57c592009-12-03 19:11:07 +00001152 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling2f921f82009-05-15 09:23:25 +00001153 dwarf::DW_ACCESS_private);
Devang Pateleb57c592009-12-03 19:11:07 +00001154 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1155 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1156 dwarf::DW_ACCESS_public);
1157 if (DT.isVirtual())
1158 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1159 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling2f921f82009-05-15 09:23:25 +00001160 return MemberDie;
1161}
1162
Devang Patel525dda02009-12-14 16:18:45 +00001163/// createSubprogramDIE - Create new DIE using SP.
1164DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
1165 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1166 if (SPDie)
1167 return SPDie;
1168
1169 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patelf200b392010-03-02 17:58:15 +00001170 // Constructors and operators for anonymous aggregates do not have names.
Devang Pateld0fa3042010-03-02 01:26:20 +00001171 if (!SP.getName().empty())
1172 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling2f921f82009-05-15 09:23:25 +00001173
Devang Patel2d9caf92009-11-25 17:36:49 +00001174 StringRef LinkageName = SP.getLinkageName();
Devang Patel43ef34d2010-01-05 01:46:14 +00001175 if (!LinkageName.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001176 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel43ef34d2010-01-05 01:46:14 +00001177 getRealLinkageName(LinkageName));
1178
Devang Patel930143b2009-11-21 02:48:08 +00001179 addSourceLine(SPDie, &SP);
Bill Wendling2f921f82009-05-15 09:23:25 +00001180
Bill Wendling2f921f82009-05-15 09:23:25 +00001181 // Add prototyped tag, if C or ObjC.
1182 unsigned Lang = SP.getCompileUnit().getLanguage();
1183 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1184 Lang == dwarf::DW_LANG_ObjC)
Devang Patel930143b2009-11-21 02:48:08 +00001185 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001186
1187 // Add Return Type.
Devang Patel236526d2009-12-03 01:25:38 +00001188 DICompositeType SPTy = SP.getType();
1189 DIArray Args = SPTy.getTypeArray();
Bill Wendling2f921f82009-05-15 09:23:25 +00001190 unsigned SPTag = SPTy.getTag();
Devang Pateleb57c592009-12-03 19:11:07 +00001191
Devang Patel3b548aa2010-03-08 20:52:55 +00001192 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel9ccfb642009-12-09 18:24:21 +00001193 addType(SPDie, SPTy);
Devang Patel236526d2009-12-03 01:25:38 +00001194 else
Devang Patel9ccfb642009-12-09 18:24:21 +00001195 addType(SPDie, DIType(Args.getElement(0).getNode()));
Devang Patel236526d2009-12-03 01:25:38 +00001196
Devang Pateleb57c592009-12-03 19:11:07 +00001197 unsigned VK = SP.getVirtuality();
1198 if (VK) {
1199 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
1200 DIEBlock *Block = new DIEBlock();
1201 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1202 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1203 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Devang Patel018b29b2010-01-19 06:19:05 +00001204 ContainingTypeMap.insert(std::make_pair(SPDie,
1205 SP.getContainingType().getNode()));
Devang Pateleb57c592009-12-03 19:11:07 +00001206 }
1207
Devang Patel525dda02009-12-14 16:18:45 +00001208 if (MakeDecl || !SP.isDefinition()) {
Devang Patel930143b2009-11-21 02:48:08 +00001209 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001210
1211 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel236526d2009-12-03 01:25:38 +00001212 // be handled while processing variables.
1213 DICompositeType SPTy = SP.getType();
1214 DIArray Args = SPTy.getTypeArray();
1215 unsigned SPTag = SPTy.getTag();
1216
Bill Wendling2f921f82009-05-15 09:23:25 +00001217 if (SPTag == dwarf::DW_TAG_subroutine_type)
1218 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1219 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel6efc8e52010-02-06 01:02:37 +00001220 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1221 addType(Arg, ATy);
1222 if (ATy.isArtificial())
1223 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel930143b2009-11-21 02:48:08 +00001224 SPDie->addChild(Arg);
Bill Wendling2f921f82009-05-15 09:23:25 +00001225 }
1226 }
1227
Devang Patel999b4992010-02-03 19:57:19 +00001228 if (SP.isArtificial())
1229 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1230
Bill Wendling2f921f82009-05-15 09:23:25 +00001231 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel9ccfb642009-12-09 18:24:21 +00001232 ModuleCU->insertDIE(SP.getNode(), SPDie);
Bill Wendling2f921f82009-05-15 09:23:25 +00001233 return SPDie;
1234}
1235
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001236/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1237/// Initialize scope and update scope hierarchy.
1238DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1239 MDNode *InlinedAt) {
1240 assert (N && "Invalid Scope encoding!");
1241 assert (MI && "Missing machine instruction!");
1242 bool GetConcreteScope = (MI && InlinedAt);
1243
1244 DbgScope *NScope = NULL;
1245
1246 if (InlinedAt)
1247 NScope = DbgScopeMap.lookup(InlinedAt);
1248 else
1249 NScope = DbgScopeMap.lookup(N);
1250 assert (NScope && "Unable to find working scope!");
1251
1252 if (NScope->getFirstInsn())
1253 return NScope;
Devang Patel75cc16c2009-10-01 20:31:14 +00001254
1255 DbgScope *Parent = NULL;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001256 if (GetConcreteScope) {
Devang Patel6875c5eb2009-10-14 21:08:09 +00001257 DILocation IL(InlinedAt);
Jim Grosbach042483e2009-11-21 23:12:12 +00001258 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001259 IL.getOrigLocation().getNode());
1260 assert (Parent && "Unable to find Parent scope!");
1261 NScope->setParent(Parent);
Devang Patel930143b2009-11-21 02:48:08 +00001262 Parent->addScope(NScope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001263 } else if (DIDescriptor(N).isLexicalBlock()) {
1264 DILexicalBlock DB(N);
Devang Patel3b548aa2010-03-08 20:52:55 +00001265 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1266 NScope->setParent(Parent);
1267 Parent->addScope(NScope);
Devang Patel6875c5eb2009-10-14 21:08:09 +00001268 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001269
Devang Patelcfeaa482009-10-27 20:47:17 +00001270 NScope->setFirstInsn(MI);
Devang Patel75cc16c2009-10-01 20:31:14 +00001271
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001272 if (!Parent && !InlinedAt) {
Devang Patel78319c62009-11-11 00:31:36 +00001273 StringRef SPName = DISubprogram(N).getLinkageName();
1274 if (SPName == MF->getFunction()->getName())
1275 CurrentFnDbgScope = NScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001276 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001277
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001278 if (GetConcreteScope) {
1279 ConcreteScopes[InlinedAt] = NScope;
1280 getOrCreateAbstractScope(N);
1281 }
1282
Devang Patelcfeaa482009-10-27 20:47:17 +00001283 return NScope;
Devang Patel75cc16c2009-10-01 20:31:14 +00001284}
1285
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001286DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1287 assert (N && "Invalid Scope encoding!");
1288
1289 DbgScope *AScope = AbstractScopes.lookup(N);
1290 if (AScope)
1291 return AScope;
Jim Grosbach042483e2009-11-21 23:12:12 +00001292
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001293 DbgScope *Parent = NULL;
1294
1295 DIDescriptor Scope(N);
1296 if (Scope.isLexicalBlock()) {
1297 DILexicalBlock DB(N);
1298 DIDescriptor ParentDesc = DB.getContext();
Devang Patel3b548aa2010-03-08 20:52:55 +00001299 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001300 }
1301
1302 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1303
1304 if (Parent)
Devang Patel930143b2009-11-21 02:48:08 +00001305 Parent->addScope(AScope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001306 AScope->setAbstractScope();
1307 AbstractScopes[N] = AScope;
1308 if (DIDescriptor(N).isSubprogram())
1309 AbstractScopesList.push_back(AScope);
1310 return AScope;
1311}
Devang Patel75cc16c2009-10-01 20:31:14 +00001312
Jim Grosbach042483e2009-11-21 23:12:12 +00001313/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel930143b2009-11-21 02:48:08 +00001314/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1315/// If there are global variables in this scope then create and insert
1316/// DIEs for these variables.
1317DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001318
Devang Patele064ad42009-11-20 21:37:22 +00001319 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001320 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patel525dda02009-12-14 16:18:45 +00001321 DISubprogram SP(SPNode);
Devang Pateld4be4ce2010-02-05 23:09:20 +00001322 // There is not any need to generate specification DIE for a function
1323 // defined at compile unit level. If a function is defined inside another
1324 // function then gdb prefers the definition at top level and but does not
1325 // expect specification DIE in parent function. So avoid creating
1326 // specification DIE for a function defined inside a function.
1327 if (SP.isDefinition() && !SP.getContext().isCompileUnit()
1328 && !SP.getContext().isSubprogram()) {
Devang Patel525dda02009-12-14 16:18:45 +00001329 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1330 // Add arguments.
1331 DICompositeType SPTy = SP.getType();
1332 DIArray Args = SPTy.getTypeArray();
1333 unsigned SPTag = SPTy.getTag();
1334 if (SPTag == dwarf::DW_TAG_subroutine_type)
1335 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1336 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel6efc8e52010-02-06 01:02:37 +00001337 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1338 addType(Arg, ATy);
1339 if (ATy.isArtificial())
1340 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel525dda02009-12-14 16:18:45 +00001341 SPDie->addChild(Arg);
1342 }
1343 DIE *SPDeclDie = SPDie;
1344 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1345 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1346 SPDeclDie);
Devang Patel525dda02009-12-14 16:18:45 +00001347 ModuleCU->addDie(SPDie);
1348 }
Devang Patel6efc8e52010-02-06 01:02:37 +00001349
Devang Patel930143b2009-11-21 02:48:08 +00001350 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Chris Lattnerbc9210c2010-03-08 22:23:36 +00001351 getDWLabel("func_begin", SubprogramCount));
Devang Patel930143b2009-11-21 02:48:08 +00001352 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Chris Lattnerbc9210c2010-03-08 22:23:36 +00001353 getDWLabel("func_end", SubprogramCount));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001354 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patel930143b2009-11-21 02:48:08 +00001355 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach042483e2009-11-21 23:12:12 +00001356
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001357 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patel930143b2009-11-21 02:48:08 +00001358 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001359
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001360 return SPDie;
1361}
1362
Jim Grosbach042483e2009-11-21 23:12:12 +00001363/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +00001364/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1365DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Chris Lattnere13c3722010-03-09 01:58:53 +00001366 MCSymbol *Start = Scope->getStartLabel();
1367 MCSymbol *End = Scope->getEndLabel();
Chris Lattner54a68762010-03-09 04:48:35 +00001368 if (Start == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001369
Chris Lattnere13c3722010-03-09 01:58:53 +00001370 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1371 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Chris Lattnerc3b70f62010-03-09 01:51:43 +00001372
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001373 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1374 if (Scope->isAbstractScope())
1375 return ScopeDIE;
1376
Devang Patel930143b2009-11-21 02:48:08 +00001377 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Chris Lattnere13c3722010-03-09 01:58:53 +00001378 Start ? Start : getDWLabel("func_begin", SubprogramCount));
Devang Patel930143b2009-11-21 02:48:08 +00001379 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Chris Lattnere13c3722010-03-09 01:58:53 +00001380 End ? End : getDWLabel("func_end", SubprogramCount));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001381
1382 return ScopeDIE;
1383}
1384
Devang Patel930143b2009-11-21 02:48:08 +00001385/// constructInlinedScopeDIE - This scope represents inlined body of
1386/// a function. Construct DIE to represent this concrete inlined copy
1387/// of the function.
1388DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Chris Lattnere13c3722010-03-09 01:58:53 +00001389 MCSymbol *StartLabel = Scope->getStartLabel();
1390 MCSymbol *EndLabel = Scope->getEndLabel();
Chris Lattner54a68762010-03-09 04:48:35 +00001391 if (StartLabel == 0) return 0;
1392
Chris Lattnere13c3722010-03-09 01:58:53 +00001393 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +00001394 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +00001395 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +00001396 "Invalid end label for an inlined scope!");
Devang Patel3b548aa2010-03-08 20:52:55 +00001397 if (!Scope->getScopeNode())
Devang Patelbc97f6b2010-03-08 19:20:38 +00001398 return NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +00001399 DIScope DS(Scope->getScopeNode());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001400 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1401
1402 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patele064ad42009-11-20 21:37:22 +00001403 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001404 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patel930143b2009-11-21 02:48:08 +00001405 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001406 dwarf::DW_FORM_ref4, OriginDIE);
1407
Chris Lattner085b6522010-03-09 00:31:02 +00001408 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnere13c3722010-03-09 01:58:53 +00001409 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001410
1411 InlinedSubprogramDIEs.insert(OriginDIE);
1412
1413 // Track the start label for this inlined function.
Devang Patel018b29b2010-01-19 06:19:05 +00001414 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001415 I = InlineInfo.find(InlinedSP.getNode());
1416
1417 if (I == InlineInfo.end()) {
Chris Lattner085b6522010-03-09 00:31:02 +00001418 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartLabel,
Jim Grosbach00e9c612009-11-22 19:20:36 +00001419 ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001420 InlinedSPNodes.push_back(InlinedSP.getNode());
1421 } else
Chris Lattner085b6522010-03-09 00:31:02 +00001422 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001423
1424 StringPool.insert(InlinedSP.getName());
Devang Patel43ef34d2010-01-05 01:46:14 +00001425 StringPool.insert(getRealLinkageName(InlinedSP.getLinkageName()));
1426
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001427 DILocation DL(Scope->getInlinedAt());
Devang Patel930143b2009-11-21 02:48:08 +00001428 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1429 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001430
1431 return ScopeDIE;
1432}
1433
Devang Patel930143b2009-11-21 02:48:08 +00001434
1435/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel9ccfb642009-12-09 18:24:21 +00001436DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001437 // Get the descriptor.
1438 const DIVariable &VD = DV->getVariable();
Devang Patel2d9caf92009-11-25 17:36:49 +00001439 StringRef Name = VD.getName();
1440 if (Name.empty())
Devang Patel97f99fa2009-11-13 02:25:26 +00001441 return NULL;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001442
1443 // Translate tag to proper Dwarf tag. The result variable is dropped for
1444 // now.
1445 unsigned Tag;
1446 switch (VD.getTag()) {
1447 case dwarf::DW_TAG_return_variable:
1448 return NULL;
1449 case dwarf::DW_TAG_arg_variable:
1450 Tag = dwarf::DW_TAG_formal_parameter;
1451 break;
1452 case dwarf::DW_TAG_auto_variable: // fall thru
1453 default:
1454 Tag = dwarf::DW_TAG_variable;
1455 break;
1456 }
1457
1458 // Define variable debug information entry.
1459 DIE *VariableDie = new DIE(Tag);
1460
1461
1462 DIE *AbsDIE = NULL;
1463 if (DbgVariable *AV = DV->getAbstractVariable())
1464 AbsDIE = AV->getDIE();
Jim Grosbach042483e2009-11-21 23:12:12 +00001465
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001466 if (AbsDIE) {
1467 DIScope DS(Scope->getScopeNode());
1468 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patele064ad42009-11-20 21:37:22 +00001469 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbar75b4d352009-11-11 03:09:50 +00001470 (void) OriginSPDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001471 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1472 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1473 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patel930143b2009-11-21 02:48:08 +00001474 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001475 dwarf::DW_FORM_ref4, AbsDIE);
1476 }
1477 else {
Devang Patel930143b2009-11-21 02:48:08 +00001478 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1479 addSourceLine(VariableDie, &VD);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001480
1481 // Add variable type.
Jim Grosbach042483e2009-11-21 23:12:12 +00001482 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001483 // addresses instead.
1484 if (VD.isBlockByrefVariable())
Devang Patel9ccfb642009-12-09 18:24:21 +00001485 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001486 else
Devang Patel9ccfb642009-12-09 18:24:21 +00001487 addType(VariableDie, VD.getType());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001488 }
1489
1490 // Add variable address.
1491 if (!Scope->isAbstractScope()) {
1492 MachineLocation Location;
Jim Grosbachbf6d3582009-11-22 20:14:00 +00001493 unsigned FrameReg;
1494 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1495 Location.set(FrameReg, Offset);
Jim Grosbach042483e2009-11-21 23:12:12 +00001496
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001497 if (VD.hasComplexAddress())
Devang Patel930143b2009-11-21 02:48:08 +00001498 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001499 else if (VD.isBlockByrefVariable())
Devang Patel930143b2009-11-21 02:48:08 +00001500 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001501 else
Devang Patel930143b2009-11-21 02:48:08 +00001502 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001503 }
Devang Patel6efc8e52010-02-06 01:02:37 +00001504
1505 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1506 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001507 DV->setDIE(VariableDie);
1508 return VariableDie;
1509
1510}
Devang Patel930143b2009-11-21 02:48:08 +00001511
Devang Patel04d2f2d2009-11-24 01:14:22 +00001512void DwarfDebug::addPubTypes(DISubprogram SP) {
1513 DICompositeType SPTy = SP.getType();
1514 unsigned SPTag = SPTy.getTag();
1515 if (SPTag != dwarf::DW_TAG_subroutine_type)
1516 return;
1517
1518 DIArray Args = SPTy.getTypeArray();
Devang Patel04d2f2d2009-11-24 01:14:22 +00001519 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1520 DIType ATy(Args.getElement(i).getNode());
Devang Patel3b548aa2010-03-08 20:52:55 +00001521 if (!ATy.isValid())
Devang Patel04d2f2d2009-11-24 01:14:22 +00001522 continue;
1523 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel3b548aa2010-03-08 20:52:55 +00001524 if (DIDescriptor(CATy.getNode()).Verify() && !CATy.getName().empty()) {
Devang Patel04d2f2d2009-11-24 01:14:22 +00001525 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1526 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1527 }
1528 }
1529}
1530
Devang Patel930143b2009-11-21 02:48:08 +00001531/// constructScopeDIE - Construct a DIE for this scope.
1532DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +00001533 if (!Scope || !Scope->getScopeNode())
1534 return NULL;
1535
1536 DIScope DS(Scope->getScopeNode());
1537 DIE *ScopeDIE = NULL;
1538 if (Scope->getInlinedAt())
1539 ScopeDIE = constructInlinedScopeDIE(Scope);
1540 else if (DS.isSubprogram()) {
1541 if (Scope->isAbstractScope())
1542 ScopeDIE = ModuleCU->getDIE(DS.getNode());
1543 else
1544 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
1545 }
1546 else {
1547 ScopeDIE = constructLexicalScopeDIE(Scope);
1548 if (!ScopeDIE) return NULL;
1549 }
1550
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001551 // Add variables to scope.
Douglas Gregor9abb5382010-03-08 02:58:37 +00001552 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001553 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel9ccfb642009-12-09 18:24:21 +00001554 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach042483e2009-11-21 23:12:12 +00001555 if (VariableDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001556 ScopeDIE->addChild(VariableDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001557 }
1558
1559 // Add nested scopes.
Douglas Gregor9abb5382010-03-08 02:58:37 +00001560 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001561 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1562 // Define the Scope debug information entry.
Devang Patel930143b2009-11-21 02:48:08 +00001563 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach042483e2009-11-21 23:12:12 +00001564 if (NestedDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001565 ScopeDIE->addChild(NestedDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001566 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001567
1568 if (DS.isSubprogram())
1569 addPubTypes(DISubprogram(DS.getNode()));
1570
1571 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001572}
1573
Bill Wendling2b128d72009-05-20 23:19:06 +00001574/// GetOrCreateSourceID - Look up the source id with the given directory and
1575/// source file names. If none currently exists, create a new id and insert it
1576/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1577/// maps as well.
Devang Patel2d9caf92009-11-25 17:36:49 +00001578unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) {
Bill Wendling2b128d72009-05-20 23:19:06 +00001579 unsigned DId;
1580 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1581 if (DI != DirectoryIdMap.end()) {
1582 DId = DI->getValue();
1583 } else {
1584 DId = DirectoryNames.size() + 1;
1585 DirectoryIdMap[DirName] = DId;
1586 DirectoryNames.push_back(DirName);
1587 }
1588
1589 unsigned FId;
1590 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1591 if (FI != SourceFileIdMap.end()) {
1592 FId = FI->getValue();
1593 } else {
1594 FId = SourceFileNames.size() + 1;
1595 SourceFileIdMap[FileName] = FId;
1596 SourceFileNames.push_back(FileName);
1597 }
1598
1599 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1600 SourceIdMap.find(std::make_pair(DId, FId));
1601 if (SI != SourceIdMap.end())
1602 return SI->second;
1603
1604 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1605 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1606 SourceIds.push_back(std::make_pair(DId, FId));
1607
1608 return SrcId;
1609}
1610
Devang Patel1f4690c2009-12-15 19:16:48 +00001611/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1612DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
1613 DIE *NDie = ModuleCU->getDIE(NS.getNode());
1614 if (NDie)
1615 return NDie;
1616 NDie = new DIE(dwarf::DW_TAG_namespace);
1617 ModuleCU->insertDIE(NS.getNode(), NDie);
1618 if (!NS.getName().empty())
1619 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1620 addSourceLine(NDie, &NS);
1621 addToContextOwner(NDie, NS.getContext());
1622 return NDie;
1623}
1624
Devang Patelb314bd62009-12-11 21:37:07 +00001625CompileUnit *DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001626 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +00001627 StringRef FN = DIUnit.getFilename();
1628 StringRef Dir = DIUnit.getDirectory();
Devang Patelb2969422009-09-29 18:40:58 +00001629 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendling2b128d72009-05-20 23:19:06 +00001630
1631 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Chris Lattnerbc9210c2010-03-08 22:23:36 +00001632 // FIXME: Why getting the delta between two identical labels??
Devang Patel930143b2009-11-21 02:48:08 +00001633 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Chris Lattnerbc9210c2010-03-08 22:23:36 +00001634 getTempLabel("section_line"), getTempLabel("section_line"),
Bill Wendling2b128d72009-05-20 23:19:06 +00001635 false);
Devang Patel930143b2009-11-21 02:48:08 +00001636 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelb2969422009-09-29 18:40:58 +00001637 DIUnit.getProducer());
Devang Patel930143b2009-11-21 02:48:08 +00001638 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendling2b128d72009-05-20 23:19:06 +00001639 DIUnit.getLanguage());
Devang Patel930143b2009-11-21 02:48:08 +00001640 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendling2b128d72009-05-20 23:19:06 +00001641
Devang Patel2d9caf92009-11-25 17:36:49 +00001642 if (!Dir.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001643 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +00001644 if (DIUnit.isOptimized())
Devang Patel930143b2009-11-21 02:48:08 +00001645 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendling2b128d72009-05-20 23:19:06 +00001646
Devang Patel2d9caf92009-11-25 17:36:49 +00001647 StringRef Flags = DIUnit.getFlags();
1648 if (!Flags.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001649 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendling2b128d72009-05-20 23:19:06 +00001650
1651 unsigned RVer = DIUnit.getRunTimeVersion();
1652 if (RVer)
Devang Patel930143b2009-11-21 02:48:08 +00001653 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +00001654 dwarf::DW_FORM_data1, RVer);
1655
1656 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel40d78412009-06-29 20:45:18 +00001657 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel86353452009-06-29 20:38:13 +00001658 // Use first compile unit marked as isMain as the compile unit
1659 // for this module.
Devang Patel40d78412009-06-29 20:45:18 +00001660 ModuleCU = Unit;
Devang Patel86353452009-06-29 20:38:13 +00001661 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001662
Devang Patelb314bd62009-12-11 21:37:07 +00001663 return Unit;
Bill Wendling2b128d72009-05-20 23:19:06 +00001664}
1665
Devang Patel930143b2009-11-21 02:48:08 +00001666void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001667 DIGlobalVariable DI_GV(N);
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001668
Devang Patelf5d53602009-09-04 23:59:07 +00001669 // If debug information is malformed then ignore it.
1670 if (DI_GV.Verify() == false)
1671 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001672
1673 // Check for pre-existence.
Devang Patele064ad42009-11-20 21:37:22 +00001674 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel0751a282009-06-26 01:49:18 +00001675 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001676
Devang Patel9ccfb642009-12-09 18:24:21 +00001677 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Patel2eec32d2009-12-10 23:25:41 +00001678 if (!VariableDie)
1679 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001680
Bill Wendling2b128d72009-05-20 23:19:06 +00001681 // Add to map.
Devang Patele064ad42009-11-20 21:37:22 +00001682 ModuleCU->insertDIE(N, VariableDie);
Bill Wendling2b128d72009-05-20 23:19:06 +00001683
1684 // Add to context owner.
Devang Patel89880c82010-01-15 01:12:22 +00001685 DIDescriptor GVContext = DI_GV.getContext();
1686 // Do not create specification DIE if context is either compile unit
1687 // or a subprogram.
1688 if (DI_GV.isDefinition() && !GVContext.isCompileUnit()
1689 && !GVContext.isSubprogram()) {
Devang Patel1f4690c2009-12-15 19:16:48 +00001690 // Create specification DIE.
1691 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1692 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1693 dwarf::DW_FORM_ref4, VariableDie);
1694 DIEBlock *Block = new DIEBlock();
1695 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner8dcf41e2010-03-08 22:31:46 +00001696 addLabel(Block, 0, dwarf::DW_FORM_udata,
1697 Asm->GetGlobalValueSymbol(DI_GV.getGlobal()));
Devang Patel1f4690c2009-12-15 19:16:48 +00001698 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patelce25dd72010-02-09 01:58:33 +00001699 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel1f4690c2009-12-15 19:16:48 +00001700 ModuleCU->addDie(VariableSpecDIE);
1701 } else {
1702 DIEBlock *Block = new DIEBlock();
1703 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner8dcf41e2010-03-08 22:31:46 +00001704 addLabel(Block, 0, dwarf::DW_FORM_udata,
1705 Asm->GetGlobalValueSymbol(DI_GV.getGlobal()));
Devang Patel1f4690c2009-12-15 19:16:48 +00001706 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1707 }
Devang Patel89880c82010-01-15 01:12:22 +00001708 addToContextOwner(VariableDie, GVContext);
Devang Patel2b75ed22009-12-10 19:14:49 +00001709
Bill Wendling2b128d72009-05-20 23:19:06 +00001710 // Expose as global. FIXME - need to check external flag.
Devang Patel930143b2009-11-21 02:48:08 +00001711 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +00001712
1713 DIType GTy = DI_GV.getType();
Devang Patel2d9caf92009-11-25 17:36:49 +00001714 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patel04d2f2d2009-11-24 01:14:22 +00001715 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1716 assert (Entry && "Missing global type!");
1717 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1718 }
Devang Patel0751a282009-06-26 01:49:18 +00001719 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001720}
1721
Devang Patel930143b2009-11-21 02:48:08 +00001722void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001723 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +00001724
1725 // Check for pre-existence.
Devang Patele064ad42009-11-20 21:37:22 +00001726 if (ModuleCU->getDIE(N))
Devang Patel0751a282009-06-26 01:49:18 +00001727 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001728
1729 if (!SP.isDefinition())
1730 // This is a method declaration which will be handled while constructing
1731 // class type.
Devang Patel0751a282009-06-26 01:49:18 +00001732 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001733
Devang Patel9ccfb642009-12-09 18:24:21 +00001734 DIE *SubprogramDie = createSubprogramDIE(SP);
Bill Wendling2b128d72009-05-20 23:19:06 +00001735
1736 // Add to map.
Devang Patele064ad42009-11-20 21:37:22 +00001737 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +00001738
1739 // Add to context owner.
Devang Patel1f4690c2009-12-15 19:16:48 +00001740 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +00001741
Bill Wendling2b128d72009-05-20 23:19:06 +00001742 // Expose as global.
Devang Patel930143b2009-11-21 02:48:08 +00001743 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +00001744
Devang Patel0751a282009-06-26 01:49:18 +00001745 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001746}
1747
Devang Patel930143b2009-11-21 02:48:08 +00001748/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +00001749/// content. Create global DIEs and emit initial debug info sections.
1750/// This is inovked by the target AsmPrinter.
Devang Patel930143b2009-11-21 02:48:08 +00001751void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel0c044ec2009-06-25 22:36:02 +00001752 this->M = M;
1753
Bill Wendling2b128d72009-05-20 23:19:06 +00001754 if (TimePassesIsEnabled)
1755 DebugTimer->startTimer();
1756
Devang Pateld41f1192009-11-11 19:55:08 +00001757 if (!MAI->doesSupportDebugInformation())
1758 return;
1759
Devang Patel63524442009-07-30 18:56:46 +00001760 DebugInfoFinder DbgFinder;
1761 DbgFinder.processModule(*M);
Devang Patel0751a282009-06-26 01:49:18 +00001762
Bill Wendling2b128d72009-05-20 23:19:06 +00001763 // Create all the compile unit DIEs.
Devang Patel63524442009-07-30 18:56:46 +00001764 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1765 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00001766 constructCompileUnit(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00001767
Devang Patel40d78412009-06-29 20:45:18 +00001768 if (!ModuleCU)
Devang Patel8119fe872010-03-08 22:02:50 +00001769 return;
Devang Patel86353452009-06-29 20:38:13 +00001770
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001771 // Create DIEs for each subprogram.
Devang Patel63524442009-07-30 18:56:46 +00001772 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1773 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00001774 constructSubprogramDIE(*I);
Devang Patel0751a282009-06-26 01:49:18 +00001775
Devang Patel2b75ed22009-12-10 19:14:49 +00001776 // Create DIEs for each global variable.
1777 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1778 E = DbgFinder.global_variable_end(); I != E; ++I)
1779 constructGlobalVariableDIE(*I);
1780
Bill Wendling2b128d72009-05-20 23:19:06 +00001781 MMI = mmi;
1782 shouldEmit = true;
1783 MMI->setDebugInfoAvailability(true);
1784
1785 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +00001786 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +00001787
1788 // Print out .file directives to specify files for .loc directives. These are
1789 // printed out early so that they precede any .loc directives.
Chris Lattnere9a75a62009-08-22 21:43:10 +00001790 if (MAI->hasDotLocAndDotFile()) {
Bill Wendling2b128d72009-05-20 23:19:06 +00001791 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1792 // Remember source id starts at 1.
1793 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00001794 // FIXME: don't use sys::path for this! This should not depend on the
1795 // host.
Bill Wendling2b128d72009-05-20 23:19:06 +00001796 sys::Path FullPath(getSourceDirectoryName(Id.first));
1797 bool AppendOk =
1798 FullPath.appendComponent(getSourceFileName(Id.second));
1799 assert(AppendOk && "Could not append filename to directory!");
1800 AppendOk = false;
Chris Lattner601ef332010-01-25 18:58:59 +00001801 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendling2b128d72009-05-20 23:19:06 +00001802 }
1803 }
1804
1805 // Emit initial sections
Devang Patel930143b2009-11-21 02:48:08 +00001806 emitInitial();
Bill Wendling2b128d72009-05-20 23:19:06 +00001807
1808 if (TimePassesIsEnabled)
1809 DebugTimer->stopTimer();
1810}
1811
Devang Patel930143b2009-11-21 02:48:08 +00001812/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +00001813///
Devang Patel930143b2009-11-21 02:48:08 +00001814void DwarfDebug::endModule() {
Devang Pateld859d862009-10-06 00:03:14 +00001815 if (!ModuleCU)
Bill Wendling2b128d72009-05-20 23:19:06 +00001816 return;
1817
1818 if (TimePassesIsEnabled)
1819 DebugTimer->startTimer();
1820
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001821 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1822 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1823 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1824 DIE *ISP = *AI;
Devang Patel930143b2009-11-21 02:48:08 +00001825 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001826 }
1827
Devang Patel236526d2009-12-03 01:25:38 +00001828 // Insert top level DIEs.
1829 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(),
1830 TE = TopLevelDIEsVector.end(); TI != TE; ++TI)
1831 ModuleCU->getCUDie()->addChild(*TI);
1832
Devang Patel018b29b2010-01-19 06:19:05 +00001833 for (DenseMap<DIE *, MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Pateleb57c592009-12-03 19:11:07 +00001834 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1835 DIE *SPDie = CI->first;
1836 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1837 if (!N) continue;
1838 DIE *NDie = ModuleCU->getDIE(N);
1839 if (!NDie) continue;
1840 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel2108ee02010-01-15 00:26:31 +00001841 // FIXME - This is not the correct approach.
1842 // addDIEEntry(NDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Pateleb57c592009-12-03 19:11:07 +00001843 }
1844
Bill Wendling2b128d72009-05-20 23:19:06 +00001845 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001846 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattner722714d2010-03-08 22:44:40 +00001847 Asm->OutStreamer.EmitLabel(getTempLabel("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001848 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattner722714d2010-03-08 22:44:40 +00001849 Asm->OutStreamer.EmitLabel(getTempLabel("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +00001850
1851 // End text sections.
1852 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001853 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattner722714d2010-03-08 22:44:40 +00001854 Asm->OutStreamer.EmitLabel(getDWLabel("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +00001855 }
1856
1857 // Emit common frame information.
Devang Patel930143b2009-11-21 02:48:08 +00001858 emitCommonDebugFrame();
Bill Wendling2b128d72009-05-20 23:19:06 +00001859
1860 // Emit function debug frame information
1861 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1862 E = DebugFrames.end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00001863 emitFunctionDebugFrame(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00001864
1865 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +00001866 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +00001867
1868 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +00001869 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001870
1871 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +00001872 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +00001873
1874 // Emit source line correspondence into a debug line section.
Devang Patel930143b2009-11-21 02:48:08 +00001875 emitDebugLines();
Bill Wendling2b128d72009-05-20 23:19:06 +00001876
1877 // Emit info into a debug pubnames section.
Devang Patel930143b2009-11-21 02:48:08 +00001878 emitDebugPubNames();
Bill Wendling2b128d72009-05-20 23:19:06 +00001879
Devang Patel04d2f2d2009-11-24 01:14:22 +00001880 // Emit info into a debug pubtypes section.
1881 emitDebugPubTypes();
1882
Bill Wendling2b128d72009-05-20 23:19:06 +00001883 // Emit info into a debug str section.
Devang Patel930143b2009-11-21 02:48:08 +00001884 emitDebugStr();
Bill Wendling2b128d72009-05-20 23:19:06 +00001885
1886 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00001887 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +00001888
1889 // Emit info into a debug aranges section.
1890 EmitDebugARanges();
1891
1892 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00001893 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +00001894
1895 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00001896 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001897
1898 // Emit inline info.
Devang Patel930143b2009-11-21 02:48:08 +00001899 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001900
1901 if (TimePassesIsEnabled)
1902 DebugTimer->stopTimer();
1903}
1904
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001905/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach00e9c612009-11-22 19:20:36 +00001906DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1907 unsigned FrameIdx,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001908 DILocation &ScopeLoc) {
1909
1910 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1911 if (AbsDbgVariable)
1912 return AbsDbgVariable;
1913
1914 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1915 if (!Scope)
1916 return NULL;
1917
1918 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patel930143b2009-11-21 02:48:08 +00001919 Scope->addVariable(AbsDbgVariable);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001920 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1921 return AbsDbgVariable;
1922}
1923
Devang Patel930143b2009-11-21 02:48:08 +00001924/// collectVariableInfo - Populate DbgScope entries with variables' info.
1925void DwarfDebug::collectVariableInfo() {
Devang Pateldf45c7f2009-10-09 22:42:28 +00001926 if (!MMI) return;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001927
Devang Patel475d32a2009-10-06 01:26:37 +00001928 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1929 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1930 VE = VMap.end(); VI != VE; ++VI) {
Devang Patelac277eb2010-01-22 22:52:10 +00001931 MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001932 if (!Var) continue;
Devang Patel20b2a772009-10-08 18:48:03 +00001933 DIVariable DV (Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001934 std::pair< unsigned, MDNode *> VP = VI->second;
1935 DILocation ScopeLoc(VP.second);
1936
1937 DbgScope *Scope =
1938 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1939 if (!Scope)
Jim Grosbach042483e2009-11-21 23:12:12 +00001940 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelcdb7d442009-11-10 23:20:04 +00001941 // If variable scope is not found then skip this variable.
1942 if (!Scope)
1943 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001944
1945 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patel930143b2009-11-21 02:48:08 +00001946 Scope->addVariable(RegVar);
Jim Grosbach00e9c612009-11-22 19:20:36 +00001947 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
1948 ScopeLoc))
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001949 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patel475d32a2009-10-06 01:26:37 +00001950 }
1951}
1952
Devang Patel930143b2009-11-21 02:48:08 +00001953/// beginScope - Process beginning of a scope starting at Label.
Chris Lattnerba35a672010-03-09 04:54:43 +00001954void DwarfDebug::beginScope(const MachineInstr *MI, MCSymbol *Label) {
Devang Patel8db360d2009-10-06 01:50:42 +00001955 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1956 if (I == DbgScopeBeginMap.end())
1957 return;
Dan Gohman3650f4e2009-11-23 21:30:55 +00001958 ScopeVector &SD = I->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001959 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach042483e2009-11-21 23:12:12 +00001960 SDI != SDE; ++SDI)
Chris Lattnerba35a672010-03-09 04:54:43 +00001961 (*SDI)->setStartLabel(Label);
Devang Patel8db360d2009-10-06 01:50:42 +00001962}
1963
Devang Patel930143b2009-11-21 02:48:08 +00001964/// endScope - Process end of a scope.
1965void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel8db360d2009-10-06 01:50:42 +00001966 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patel7d838bb2009-10-06 03:15:38 +00001967 if (I == DbgScopeEndMap.end())
Devang Patel8db360d2009-10-06 01:50:42 +00001968 return;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001969
Chris Lattnere13c3722010-03-09 01:58:53 +00001970 MCSymbol *Label = getDWLabel("label", MMI->NextLabelID());
1971 Asm->OutStreamer.EmitLabel(Label);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001972
Chris Lattnere13c3722010-03-09 01:58:53 +00001973 SmallVector<DbgScope*, 2> &SD = I->second;
Devang Patel8db360d2009-10-06 01:50:42 +00001974 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach042483e2009-11-21 23:12:12 +00001975 SDI != SDE; ++SDI)
Chris Lattnere13c3722010-03-09 01:58:53 +00001976 (*SDI)->setEndLabel(Label);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001977 return;
1978}
1979
1980/// createDbgScope - Create DbgScope for the scope.
1981void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1982
1983 if (!InlinedAt) {
1984 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1985 if (WScope)
1986 return;
1987 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1988 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach042483e2009-11-21 23:12:12 +00001989 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel4450f262009-11-11 00:18:40 +00001990 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001991 return;
1992 }
1993
1994 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
1995 if (WScope)
1996 return;
1997
1998 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
1999 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2000 DILocation DL(InlinedAt);
2001 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel8db360d2009-10-06 01:50:42 +00002002}
2003
Devang Patel930143b2009-11-21 02:48:08 +00002004/// extractScopeInformation - Scan machine instructions in this function
Devang Patel75cc16c2009-10-01 20:31:14 +00002005/// and collect DbgScopes. Return true, if atleast one scope was found.
Chris Lattner76555b52010-01-26 23:18:02 +00002006bool DwarfDebug::extractScopeInformation() {
Devang Patel75cc16c2009-10-01 20:31:14 +00002007 // If scope information was extracted using .dbg intrinsics then there is not
2008 // any need to extract these information by scanning each instruction.
2009 if (!DbgScopeMap.empty())
2010 return false;
2011
Devang Patel530a0752010-01-04 20:44:00 +00002012 DenseMap<const MachineInstr *, unsigned> MIIndexMap;
2013 unsigned MIIndex = 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002014 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel75cc16c2009-10-01 20:31:14 +00002015 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2016 I != E; ++I) {
2017 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2018 II != IE; ++II) {
2019 const MachineInstr *MInsn = II;
Devang Patel530a0752010-01-04 20:44:00 +00002020 MIIndexMap[MInsn] = MIIndex++;
Devang Patel75cc16c2009-10-01 20:31:14 +00002021 DebugLoc DL = MInsn->getDebugLoc();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002022 if (DL.isUnknown()) continue;
Devang Patelc0e17df2010-01-16 06:09:35 +00002023 DILocation DLT = MF->getDILocation(DL);
2024 DIScope DLTScope = DLT.getScope();
Devang Patel75cc16c2009-10-01 20:31:14 +00002025 // There is no need to create another DIE for compile unit. For all
Jim Grosbach042483e2009-11-21 23:12:12 +00002026 // other scopes, create one DbgScope now. This will be translated
Devang Patel75cc16c2009-10-01 20:31:14 +00002027 // into a scope DIE at the end.
Devang Patelc0e17df2010-01-16 06:09:35 +00002028 if (DLTScope.isCompileUnit()) continue;
2029 createDbgScope(DLTScope.getNode(), DLT.getOrigLocation().getNode());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002030 }
2031 }
2032
2033
2034 // Build scope hierarchy using working set of scopes.
2035 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2036 I != E; ++I) {
2037 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2038 II != IE; ++II) {
2039 const MachineInstr *MInsn = II;
2040 DebugLoc DL = MInsn->getDebugLoc();
2041 if (DL.isUnknown()) continue;
Devang Patelc0e17df2010-01-16 06:09:35 +00002042 DILocation DLT = MF->getDILocation(DL);
2043 DIScope DLTScope = DLT.getScope();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002044 // There is no need to create another DIE for compile unit. For all
Jim Grosbach042483e2009-11-21 23:12:12 +00002045 // other scopes, create one DbgScope now. This will be translated
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002046 // into a scope DIE at the end.
Devang Patelc0e17df2010-01-16 06:09:35 +00002047 if (DLTScope.isCompileUnit()) continue;
2048 DbgScope *Scope = getUpdatedDbgScope(DLTScope.getNode(), MInsn,
2049 DLT.getOrigLocation().getNode());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002050 Scope->setLastInsn(MInsn);
Devang Patel75cc16c2009-10-01 20:31:14 +00002051 }
2052 }
2053
Devang Patel530a0752010-01-04 20:44:00 +00002054 if (!CurrentFnDbgScope)
2055 return false;
2056
2057 CurrentFnDbgScope->fixInstructionMarkers(MIIndexMap);
Devang Patel75cc16c2009-10-01 20:31:14 +00002058
2059 // Each scope has first instruction and last instruction to mark beginning
2060 // and end of a scope respectively. Create an inverse map that list scopes
2061 // starts (and ends) with an instruction. One instruction may start (or end)
Devang Patel7771b7c2010-01-20 02:05:23 +00002062 // multiple scopes. Ignore scopes that are not reachable.
2063 SmallVector<DbgScope *, 4> WorkList;
2064 WorkList.push_back(CurrentFnDbgScope);
2065 while (!WorkList.empty()) {
2066 DbgScope *S = WorkList.back(); WorkList.pop_back();
2067
Douglas Gregor9abb5382010-03-08 02:58:37 +00002068 SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patel7771b7c2010-01-20 02:05:23 +00002069 if (!Children.empty())
Douglas Gregor9abb5382010-03-08 02:58:37 +00002070 for (SmallVector<DbgScope *, 4>::iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00002071 SE = Children.end(); SI != SE; ++SI)
2072 WorkList.push_back(*SI);
2073
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002074 if (S->isAbstractScope())
2075 continue;
Devang Patel75cc16c2009-10-01 20:31:14 +00002076 const MachineInstr *MI = S->getFirstInsn();
2077 assert (MI && "DbgScope does not have first instruction!");
2078
2079 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2080 if (IDI != DbgScopeBeginMap.end())
2081 IDI->second.push_back(S);
2082 else
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002083 DbgScopeBeginMap[MI].push_back(S);
Devang Patel75cc16c2009-10-01 20:31:14 +00002084
2085 MI = S->getLastInsn();
2086 assert (MI && "DbgScope does not have last instruction!");
2087 IDI = DbgScopeEndMap.find(MI);
2088 if (IDI != DbgScopeEndMap.end())
2089 IDI->second.push_back(S);
2090 else
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002091 DbgScopeEndMap[MI].push_back(S);
Devang Patel75cc16c2009-10-01 20:31:14 +00002092 }
2093
2094 return !DbgScopeMap.empty();
2095}
2096
Devang Patel930143b2009-11-21 02:48:08 +00002097/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00002098/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00002099void DwarfDebug::beginFunction(const MachineFunction *MF) {
Bill Wendling2b128d72009-05-20 23:19:06 +00002100 this->MF = MF;
2101
2102 if (!ShouldEmitDwarfDebug()) return;
2103
2104 if (TimePassesIsEnabled)
2105 DebugTimer->startTimer();
2106
Chris Lattner76555b52010-01-26 23:18:02 +00002107 if (!extractScopeInformation())
Devang Patel4598eb62009-10-06 18:37:31 +00002108 return;
Devang Patel930143b2009-11-21 02:48:08 +00002109
2110 collectVariableInfo();
Devang Patel4598eb62009-10-06 18:37:31 +00002111
Bill Wendling2b128d72009-05-20 23:19:06 +00002112 // Assumes in correct section after the entry point.
Chris Lattner722714d2010-03-08 22:44:40 +00002113 Asm->OutStreamer.EmitLabel(getDWLabel("func_begin", ++SubprogramCount));
Bill Wendling2b128d72009-05-20 23:19:06 +00002114
2115 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2116 // function.
Devang Pateldf45c7f2009-10-09 22:42:28 +00002117 DebugLoc FDL = MF->getDefaultDebugLoc();
2118 if (!FDL.isUnknown()) {
Devang Patelc0e17df2010-01-16 06:09:35 +00002119 DILocation DLT = MF->getDILocation(FDL);
Devang Patelc0e17df2010-01-16 06:09:35 +00002120 DISubprogram SP = getDISubprogram(DLT.getScope().getNode());
Chris Lattnere13c3722010-03-09 01:58:53 +00002121 unsigned Line, Col;
2122 if (SP.Verify()) {
2123 Line = SP.getLineNumber();
2124 Col = 0;
2125 } else {
2126 Line = DLT.getLineNumber();
2127 Col = DLT.getColumnNumber();
2128 }
2129
Chris Lattnerba35a672010-03-09 04:54:43 +00002130 recordSourceLine(Line, Col, DLT.getScope().getNode());
Bill Wendling2b128d72009-05-20 23:19:06 +00002131 }
Bill Wendling2b128d72009-05-20 23:19:06 +00002132 if (TimePassesIsEnabled)
2133 DebugTimer->stopTimer();
2134}
2135
Devang Patel930143b2009-11-21 02:48:08 +00002136/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00002137///
Chris Lattner76555b52010-01-26 23:18:02 +00002138void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendling2b128d72009-05-20 23:19:06 +00002139 if (!ShouldEmitDwarfDebug()) return;
2140
2141 if (TimePassesIsEnabled)
2142 DebugTimer->startTimer();
2143
Devang Pateldf45c7f2009-10-09 22:42:28 +00002144 if (DbgScopeMap.empty())
2145 return;
Devang Patel2904aa92009-11-12 19:02:56 +00002146
Devang Patel530a0752010-01-04 20:44:00 +00002147 if (CurrentFnDbgScope) {
2148 // Define end label for subprogram.
Chris Lattner722714d2010-03-08 22:44:40 +00002149 Asm->OutStreamer.EmitLabel(getDWLabel("func_end", SubprogramCount));
Devang Patel530a0752010-01-04 20:44:00 +00002150
2151 // Get function line info.
2152 if (!Lines.empty()) {
2153 // Get section line info.
2154 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2155 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2156 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2157 // Append the function info to section info.
2158 SectionLineInfos.insert(SectionLineInfos.end(),
2159 Lines.begin(), Lines.end());
2160 }
2161
2162 // Construct abstract scopes.
2163 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2164 AE = AbstractScopesList.end(); AI != AE; ++AI)
2165 constructScopeDIE(*AI);
2166
2167 constructScopeDIE(CurrentFnDbgScope);
2168
2169 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2170 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00002171 }
2172
Bill Wendling2b128d72009-05-20 23:19:06 +00002173 // Clear debug info
Devang Patelfe189e62010-01-19 01:26:02 +00002174 CurrentFnDbgScope = NULL;
Douglas Gregor9abb5382010-03-08 02:58:37 +00002175 DbgScopeMap.clear();
Devang Patelfe189e62010-01-19 01:26:02 +00002176 DbgScopeBeginMap.clear();
2177 DbgScopeEndMap.clear();
2178 ConcreteScopes.clear();
2179 AbstractScopesList.clear();
Bill Wendling2b128d72009-05-20 23:19:06 +00002180 Lines.clear();
Devang Patel0a2c0bc2009-12-01 18:13:48 +00002181
Bill Wendling2b128d72009-05-20 23:19:06 +00002182 if (TimePassesIsEnabled)
2183 DebugTimer->stopTimer();
2184}
2185
Chris Lattnerba35a672010-03-09 04:54:43 +00002186/// recordSourceLine - Register a source line with debug info. Returns the
2187/// unique label that was emitted and which provides correspondence to
2188/// the source line list.
2189MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, MDNode *S) {
Devang Patel80ae3492009-08-28 23:24:31 +00002190 if (!MMI)
2191 return 0;
2192
Bill Wendling2b128d72009-05-20 23:19:06 +00002193 if (TimePassesIsEnabled)
2194 DebugTimer->startTimer();
2195
Devang Patel2d9caf92009-11-25 17:36:49 +00002196 StringRef Dir;
2197 StringRef Fn;
Devang Patel2089d162009-10-05 18:03:19 +00002198
2199 DIDescriptor Scope(S);
2200 if (Scope.isCompileUnit()) {
2201 DICompileUnit CU(S);
2202 Dir = CU.getDirectory();
2203 Fn = CU.getFilename();
2204 } else if (Scope.isSubprogram()) {
2205 DISubprogram SP(S);
2206 Dir = SP.getDirectory();
2207 Fn = SP.getFilename();
2208 } else if (Scope.isLexicalBlock()) {
2209 DILexicalBlock DB(S);
2210 Dir = DB.getDirectory();
2211 Fn = DB.getFilename();
2212 } else
2213 assert (0 && "Unexpected scope info");
2214
2215 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00002216 unsigned ID = MMI->NextLabelID();
2217 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2218
2219 if (TimePassesIsEnabled)
2220 DebugTimer->stopTimer();
2221
Chris Lattnerba35a672010-03-09 04:54:43 +00002222 MCSymbol *Label = getDWLabel("label", ID);
2223 Asm->OutStreamer.EmitLabel(Label);
2224 return Label;
Bill Wendling2b128d72009-05-20 23:19:06 +00002225}
2226
2227/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2228/// timed. Look up the source id with the given directory and source file
2229/// names. If none currently exists, create a new id and insert it in the
2230/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2231/// well.
2232unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2233 const std::string &FileName) {
2234 if (TimePassesIsEnabled)
2235 DebugTimer->startTimer();
2236
Devang Patelb2969422009-09-29 18:40:58 +00002237 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendling2b128d72009-05-20 23:19:06 +00002238
2239 if (TimePassesIsEnabled)
2240 DebugTimer->stopTimer();
2241
2242 return SrcId;
2243}
2244
Bill Wendling806535f2009-05-20 23:22:40 +00002245//===----------------------------------------------------------------------===//
2246// Emit Methods
2247//===----------------------------------------------------------------------===//
2248
Devang Patel930143b2009-11-21 02:48:08 +00002249/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00002250///
Jim Grosbach00e9c612009-11-22 19:20:36 +00002251unsigned
2252DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00002253 // Get the children.
2254 const std::vector<DIE *> &Children = Die->getChildren();
2255
2256 // If not last sibling and has children then add sibling offset attribute.
Devang Patel930143b2009-11-21 02:48:08 +00002257 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00002258
2259 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00002260 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00002261
2262 // Get the abbreviation for this DIE.
2263 unsigned AbbrevNumber = Die->getAbbrevNumber();
2264 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2265
2266 // Set DIE offset
2267 Die->setOffset(Offset);
2268
2269 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00002270 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00002271
2272 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2273 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2274
2275 // Size the DIE attribute values.
2276 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2277 // Size attribute value.
2278 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2279
2280 // Size the DIE children if any.
2281 if (!Children.empty()) {
2282 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2283 "Children flag not set");
2284
2285 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00002286 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00002287
2288 // End of children marker.
2289 Offset += sizeof(int8_t);
2290 }
2291
2292 Die->setSize(Offset - Die->getOffset());
2293 return Offset;
2294}
2295
Devang Patel930143b2009-11-21 02:48:08 +00002296/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00002297///
Devang Patel930143b2009-11-21 02:48:08 +00002298void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling480ff322009-05-20 23:21:38 +00002299 // Compute size of compile unit header.
2300 static unsigned Offset =
2301 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
Devang Patel930143b2009-11-21 02:48:08 +00002306 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel40d78412009-06-29 20:45:18 +00002307 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling480ff322009-05-20 23:21:38 +00002308}
2309
Devang Patel930143b2009-11-21 02:48:08 +00002310/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling480ff322009-05-20 23:21:38 +00002311/// tools to recognize the object file contains Dwarf information.
Devang Patel930143b2009-11-21 02:48:08 +00002312void DwarfDebug::emitInitial() {
Bill Wendling480ff322009-05-20 23:21:38 +00002313 // Check to see if we already emitted intial headers.
2314 if (didInitial) return;
2315 didInitial = true;
2316
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002317 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002318
Bill Wendling480ff322009-05-20 23:21:38 +00002319 // Dwarf sections base addresses.
Chris Lattnere9a75a62009-08-22 21:43:10 +00002320 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002321 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Chris Lattner722714d2010-03-08 22:44:40 +00002322 Asm->OutStreamer.EmitLabel(getTempLabel("section_debug_frame"));
Bill Wendling480ff322009-05-20 23:21:38 +00002323 }
2324
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002325 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Chris Lattner722714d2010-03-08 22:44:40 +00002326 Asm->OutStreamer.EmitLabel(getTempLabel("section_info"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002327 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Chris Lattner722714d2010-03-08 22:44:40 +00002328 Asm->OutStreamer.EmitLabel(getTempLabel("section_abbrev"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002329 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Chris Lattner722714d2010-03-08 22:44:40 +00002330 Asm->OutStreamer.EmitLabel(getTempLabel("section_aranges"));
Bill Wendling480ff322009-05-20 23:21:38 +00002331
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002332 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2333 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Chris Lattner722714d2010-03-08 22:44:40 +00002334 Asm->OutStreamer.EmitLabel(getTempLabel("section_macinfo"));
Bill Wendling480ff322009-05-20 23:21:38 +00002335 }
2336
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002337 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Chris Lattner722714d2010-03-08 22:44:40 +00002338 Asm->OutStreamer.EmitLabel(getTempLabel("section_line"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002339 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Chris Lattner722714d2010-03-08 22:44:40 +00002340 Asm->OutStreamer.EmitLabel(getTempLabel("section_loc"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002341 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Chris Lattner722714d2010-03-08 22:44:40 +00002342 Asm->OutStreamer.EmitLabel(getTempLabel("section_pubnames"));
Devang Patel04d2f2d2009-11-24 01:14:22 +00002343 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
Chris Lattner722714d2010-03-08 22:44:40 +00002344 Asm->OutStreamer.EmitLabel(getTempLabel("section_pubtypes"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002345 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Chris Lattner722714d2010-03-08 22:44:40 +00002346 Asm->OutStreamer.EmitLabel(getTempLabel("section_str"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002347 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Chris Lattner722714d2010-03-08 22:44:40 +00002348 Asm->OutStreamer.EmitLabel(getTempLabel("section_ranges"));
Bill Wendling480ff322009-05-20 23:21:38 +00002349
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002350 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Chris Lattner722714d2010-03-08 22:44:40 +00002351 Asm->OutStreamer.EmitLabel(getTempLabel("text_begin"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002352 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Chris Lattner722714d2010-03-08 22:44:40 +00002353 Asm->OutStreamer.EmitLabel(getTempLabel("data_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00002354}
2355
Devang Patel930143b2009-11-21 02:48:08 +00002356/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00002357///
Devang Patel930143b2009-11-21 02:48:08 +00002358void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00002359 // Get the abbreviation for this DIE.
2360 unsigned AbbrevNumber = Die->getAbbrevNumber();
2361 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2362
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002363 Asm->O << '\n';
Bill Wendling480ff322009-05-20 23:21:38 +00002364
2365 // Emit the code (index) for the abbreviation.
Chris Lattnerfa823552010-01-22 23:18:42 +00002366 if (Asm->VerboseAsm)
2367 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2368 Twine::utohexstr(Die->getOffset()) + ":0x" +
2369 Twine::utohexstr(Die->getSize()) + " " +
2370 dwarf::TagString(Abbrev->getTag()));
2371 EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00002372
2373 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2374 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2375
2376 // Emit the DIE attribute values.
2377 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2378 unsigned Attr = AbbrevData[i].getAttribute();
2379 unsigned Form = AbbrevData[i].getForm();
2380 assert(Form && "Too many attributes for DIE (check abbreviation)");
2381
Chris Lattner5adf9872010-01-24 18:54:17 +00002382 if (Asm->VerboseAsm)
2383 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2384
Bill Wendling480ff322009-05-20 23:21:38 +00002385 switch (Attr) {
2386 case dwarf::DW_AT_sibling:
Devang Patel930143b2009-11-21 02:48:08 +00002387 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002388 break;
2389 case dwarf::DW_AT_abstract_origin: {
2390 DIEEntry *E = cast<DIEEntry>(Values[i]);
2391 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002392 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00002393 Asm->EmitInt32(Addr);
2394 break;
2395 }
2396 default:
2397 // Emit an attribute using the defined form.
2398 Values[i]->EmitValue(this, Form);
Chris Lattner62f28402010-01-24 19:01:06 +00002399 O << "\n"; // REMOVE This once all EmitValue impls emit their own newline.
Bill Wendling480ff322009-05-20 23:21:38 +00002400 break;
2401 }
Bill Wendling480ff322009-05-20 23:21:38 +00002402 }
2403
2404 // Emit the DIE children if any.
2405 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2406 const std::vector<DIE *> &Children = Die->getChildren();
2407
2408 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00002409 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00002410
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002411 Asm->EmitInt8(0); EOL("End Of Children Mark");
Bill Wendling480ff322009-05-20 23:21:38 +00002412 }
2413}
2414
Devang Patel9ccfb642009-12-09 18:24:21 +00002415/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00002416///
Devang Patel9ccfb642009-12-09 18:24:21 +00002417void DwarfDebug::emitDebugInfo() {
2418 // Start debug info section.
2419 Asm->OutStreamer.SwitchSection(
2420 Asm->getObjFileLowering().getDwarfInfoSection());
2421 DIE *Die = ModuleCU->getCUDie();
Bill Wendling480ff322009-05-20 23:21:38 +00002422
2423 // Emit the compile units header.
Chris Lattner722714d2010-03-08 22:44:40 +00002424 Asm->OutStreamer.EmitLabel(getDWLabel("info_begin", ModuleCU->getID()));
Bill Wendling480ff322009-05-20 23:21:38 +00002425
2426 // Emit size of content not including length itself
2427 unsigned ContentSize = Die->getSize() +
2428 sizeof(int16_t) + // DWARF version number
2429 sizeof(int32_t) + // Offset Into Abbrev. Section
2430 sizeof(int8_t) + // Pointer Size (in bytes)
2431 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2432
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002433 Asm->EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2434 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("DWARF version number");
Chris Lattnerbc9210c2010-03-08 22:23:36 +00002435 EmitSectionOffset(getTempLabel("abbrev_begin"),getTempLabel("section_abbrev"),
2436 true, false);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002437 EOL("Offset Into Abbrev. Section");
2438 Asm->EmitInt8(TD->getPointerSize()); EOL("Address Size (in bytes)");
Bill Wendling480ff322009-05-20 23:21:38 +00002439
Devang Patel930143b2009-11-21 02:48:08 +00002440 emitDIE(Die);
Bill Wendling480ff322009-05-20 23:21:38 +00002441 // FIXME - extra padding for gdb bug.
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002442 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
2443 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
2444 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
2445 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
Chris Lattner722714d2010-03-08 22:44:40 +00002446 Asm->OutStreamer.EmitLabel(getDWLabel("info_end", ModuleCU->getID()));
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002447 Asm->O << '\n';
Bill Wendling480ff322009-05-20 23:21:38 +00002448}
2449
Devang Patel930143b2009-11-21 02:48:08 +00002450/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00002451///
Devang Patel930143b2009-11-21 02:48:08 +00002452void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00002453 // Check to see if it is worth the effort.
2454 if (!Abbreviations.empty()) {
2455 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002456 Asm->OutStreamer.SwitchSection(
2457 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002458
Chris Lattner722714d2010-03-08 22:44:40 +00002459 Asm->OutStreamer.EmitLabel(getTempLabel("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00002460
2461 // For each abbrevation.
2462 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2463 // Get abbreviation data
2464 const DIEAbbrev *Abbrev = Abbreviations[i];
2465
2466 // Emit the abbrevations code (base 1 index.)
Chris Lattnerfa823552010-01-22 23:18:42 +00002467 EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00002468
2469 // Emit the abbreviations data.
Chris Lattnerfa823552010-01-22 23:18:42 +00002470 Abbrev->Emit(this);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002471 Asm->O << '\n';
Bill Wendling480ff322009-05-20 23:21:38 +00002472 }
2473
2474 // Mark end of abbreviations.
Chris Lattnerfa823552010-01-22 23:18:42 +00002475 EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00002476
Chris Lattner722714d2010-03-08 22:44:40 +00002477 Asm->OutStreamer.EmitLabel(getTempLabel("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00002478 }
2479}
2480
Devang Patel930143b2009-11-21 02:48:08 +00002481/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00002482/// the line matrix.
2483///
Devang Patel930143b2009-11-21 02:48:08 +00002484void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00002485 // Define last address of section.
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002486 Asm->EmitInt8(0); EOL("Extended Op");
2487 Asm->EmitInt8(TD->getPointerSize() + 1); EOL("Op size");
2488 Asm->EmitInt8(dwarf::DW_LNE_set_address); EOL("DW_LNE_set_address");
Chris Lattnerb779eb62010-03-08 22:47:57 +00002489 EmitReference(getDWLabel("section_end", SectionEnd));
2490 EOL("Section end label");
Bill Wendling480ff322009-05-20 23:21:38 +00002491
2492 // Mark end of matrix.
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002493 Asm->EmitInt8(0); EOL("DW_LNE_end_sequence");
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002494 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00002495 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002496}
2497
Devang Patel930143b2009-11-21 02:48:08 +00002498/// emitDebugLines - Emit source line information.
Bill Wendling480ff322009-05-20 23:21:38 +00002499///
Devang Patel930143b2009-11-21 02:48:08 +00002500void DwarfDebug::emitDebugLines() {
Bill Wendling480ff322009-05-20 23:21:38 +00002501 // If the target is using .loc/.file, the assembler will be emitting the
2502 // .debug_line table automatically.
Chris Lattnere9a75a62009-08-22 21:43:10 +00002503 if (MAI->hasDotLocAndDotFile())
Bill Wendling480ff322009-05-20 23:21:38 +00002504 return;
2505
2506 // Minimum line delta, thus ranging from -10..(255-10).
2507 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2508 // Maximum line delta, thus ranging from -10..(255-10).
2509 const int MaxLineDelta = 255 + MinLineDelta;
2510
2511 // Start the dwarf line section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002512 Asm->OutStreamer.SwitchSection(
2513 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002514
2515 // Construct the section header.
Chris Lattner722714d2010-03-08 22:44:40 +00002516 EmitDifference(getTempLabel("line_end"), getTempLabel("line_begin"), true);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002517 EOL("Length of Source Line Info");
Chris Lattner722714d2010-03-08 22:44:40 +00002518 Asm->OutStreamer.EmitLabel(getTempLabel("line_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00002519
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002520 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("DWARF version number");
Bill Wendling480ff322009-05-20 23:21:38 +00002521
Chris Lattner722714d2010-03-08 22:44:40 +00002522 EmitDifference(getTempLabel("line_prolog_end"),
2523 getTempLabel("line_prolog_begin"), true);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002524 EOL("Prolog Length");
Chris Lattner722714d2010-03-08 22:44:40 +00002525 Asm->OutStreamer.EmitLabel(getTempLabel("line_prolog_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00002526
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002527 Asm->EmitInt8(1); EOL("Minimum Instruction Length");
2528 Asm->EmitInt8(1); EOL("Default is_stmt_start flag");
2529 Asm->EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
2530 Asm->EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
2531 Asm->EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Bill Wendling480ff322009-05-20 23:21:38 +00002532
2533 // Line number standard opcode encodings argument count
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002534 Asm->EmitInt8(0); EOL("DW_LNS_copy arg count");
2535 Asm->EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2536 Asm->EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2537 Asm->EmitInt8(1); EOL("DW_LNS_set_file arg count");
2538 Asm->EmitInt8(1); EOL("DW_LNS_set_column arg count");
2539 Asm->EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2540 Asm->EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2541 Asm->EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2542 Asm->EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Bill Wendling480ff322009-05-20 23:21:38 +00002543
2544 // Emit directories.
2545 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattnerc3f23b82010-01-23 03:11:46 +00002546 const std::string &Dir = getSourceDirectoryName(DI);
2547 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("Directory");
2548 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling480ff322009-05-20 23:21:38 +00002549 }
2550
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002551 Asm->EmitInt8(0); EOL("End of directories");
Bill Wendling480ff322009-05-20 23:21:38 +00002552
2553 // Emit files.
2554 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2555 // Remember source id starts at 1.
2556 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattnerc3f23b82010-01-23 03:11:46 +00002557 const std::string &FN = getSourceFileName(Id.second);
2558 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("Source");
2559 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
2560
Chris Lattnerfa823552010-01-22 23:18:42 +00002561 EmitULEB128(Id.first, "Directory #");
2562 EmitULEB128(0, "Mod date");
2563 EmitULEB128(0, "File size");
Bill Wendling480ff322009-05-20 23:21:38 +00002564 }
2565
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002566 Asm->EmitInt8(0); EOL("End of files");
Bill Wendling480ff322009-05-20 23:21:38 +00002567
Chris Lattner722714d2010-03-08 22:44:40 +00002568 Asm->OutStreamer.EmitLabel(getTempLabel("line_prolog_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00002569
2570 // A sequence for each text section.
2571 unsigned SecSrcLinesSize = SectionSourceLines.size();
2572
2573 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2574 // Isolate current sections line info.
2575 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2576
Chris Lattner591105c2009-08-08 23:39:42 +00002577 /*if (Asm->isVerbose()) {
Chris Lattner4d2c0f92009-07-31 18:48:30 +00002578 const MCSection *S = SectionMap[j + 1];
Chris Lattnere9a75a62009-08-22 21:43:10 +00002579 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling480ff322009-05-20 23:21:38 +00002580 << S->getName() << '\n';
Chris Lattner591105c2009-08-08 23:39:42 +00002581 }*/
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002582 Asm->O << '\n';
Bill Wendling480ff322009-05-20 23:21:38 +00002583
2584 // Dwarf assumes we start with first line of first source file.
2585 unsigned Source = 1;
2586 unsigned Line = 1;
2587
2588 // Construct rows of the address, source, line, column matrix.
2589 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2590 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattnerc3b70f62010-03-09 01:51:43 +00002591 unsigned LabelID = LineInfo.getLabelID();
2592 if (MMI->isLabelDeleted(LabelID)) continue;
Bill Wendling480ff322009-05-20 23:21:38 +00002593
Caroline Tice183a5192009-09-11 18:25:54 +00002594 if (LineInfo.getLine() == 0) continue;
2595
Bill Wendling480ff322009-05-20 23:21:38 +00002596 if (!Asm->isVerbose())
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002597 Asm->O << '\n';
Bill Wendling480ff322009-05-20 23:21:38 +00002598 else {
2599 std::pair<unsigned, unsigned> SourceID =
2600 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattnere9a75a62009-08-22 21:43:10 +00002601 O << '\t' << MAI->getCommentString() << ' '
Dan Gohman0891d752009-12-05 02:00:34 +00002602 << getSourceDirectoryName(SourceID.first) << '/'
Bill Wendling480ff322009-05-20 23:21:38 +00002603 << getSourceFileName(SourceID.second)
Dan Gohman0891d752009-12-05 02:00:34 +00002604 << ':' << utostr_32(LineInfo.getLine()) << '\n';
Bill Wendling480ff322009-05-20 23:21:38 +00002605 }
2606
2607 // Define the line address.
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002608 Asm->EmitInt8(0); EOL("Extended Op");
2609 Asm->EmitInt8(TD->getPointerSize() + 1); EOL("Op size");
2610 Asm->EmitInt8(dwarf::DW_LNE_set_address); EOL("DW_LNE_set_address");
Chris Lattnerb779eb62010-03-08 22:47:57 +00002611 EmitReference(getDWLabel("label", LabelID)); EOL("Location label");
Bill Wendling480ff322009-05-20 23:21:38 +00002612
2613 // If change of source, then switch to the new source.
2614 if (Source != LineInfo.getSourceID()) {
2615 Source = LineInfo.getSourceID();
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002616 Asm->EmitInt8(dwarf::DW_LNS_set_file); EOL("DW_LNS_set_file");
Chris Lattnerfa823552010-01-22 23:18:42 +00002617 EmitULEB128(Source, "New Source");
Bill Wendling480ff322009-05-20 23:21:38 +00002618 }
2619
2620 // If change of line.
2621 if (Line != LineInfo.getLine()) {
2622 // Determine offset.
2623 int Offset = LineInfo.getLine() - Line;
2624 int Delta = Offset - MinLineDelta;
2625
2626 // Update line.
2627 Line = LineInfo.getLine();
2628
2629 // If delta is small enough and in range...
2630 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2631 // ... then use fast opcode.
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002632 Asm->EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
Bill Wendling480ff322009-05-20 23:21:38 +00002633 } else {
2634 // ... otherwise use long hand.
2635 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002636 EOL("DW_LNS_advance_line");
Chris Lattner23031452010-01-22 22:56:55 +00002637 EmitSLEB128(Offset, "Line Offset");
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002638 Asm->EmitInt8(dwarf::DW_LNS_copy); EOL("DW_LNS_copy");
Bill Wendling480ff322009-05-20 23:21:38 +00002639 }
2640 } else {
2641 // Copy the previous row (different address or source)
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002642 Asm->EmitInt8(dwarf::DW_LNS_copy); EOL("DW_LNS_copy");
Bill Wendling480ff322009-05-20 23:21:38 +00002643 }
2644 }
2645
Devang Patel930143b2009-11-21 02:48:08 +00002646 emitEndOfLineMatrix(j + 1);
Bill Wendling480ff322009-05-20 23:21:38 +00002647 }
2648
2649 if (SecSrcLinesSize == 0)
2650 // Because we're emitting a debug_line section, we still need a line
2651 // table. The linker and friends expect it to exist. If there's nothing to
2652 // put into it, emit an empty table.
Devang Patel930143b2009-11-21 02:48:08 +00002653 emitEndOfLineMatrix(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002654
Chris Lattner722714d2010-03-08 22:44:40 +00002655 Asm->OutStreamer.EmitLabel(getTempLabel("line_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00002656}
2657
Devang Patel930143b2009-11-21 02:48:08 +00002658/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling480ff322009-05-20 23:21:38 +00002659///
Devang Patel930143b2009-11-21 02:48:08 +00002660void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnere9a75a62009-08-22 21:43:10 +00002661 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002662 return;
2663
2664 int stackGrowth =
2665 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2666 TargetFrameInfo::StackGrowsUp ?
2667 TD->getPointerSize() : -TD->getPointerSize();
2668
2669 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002670 Asm->OutStreamer.SwitchSection(
2671 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002672
Chris Lattner722714d2010-03-08 22:44:40 +00002673 Asm->OutStreamer.EmitLabel(getTempLabel("debug_frame_common"));
Chris Lattner449a9ff2010-03-08 23:02:59 +00002674 EmitDifference(getTempLabel("debug_frame_common_end"),
2675 getTempLabel("debug_frame_common_begin"), true);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002676 EOL("Length of Common Information Entry");
Bill Wendling480ff322009-05-20 23:21:38 +00002677
Chris Lattner722714d2010-03-08 22:44:40 +00002678 Asm->OutStreamer.EmitLabel(getTempLabel("debug_frame_common_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00002679 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002680 EOL("CIE Identifier Tag");
Bill Wendling480ff322009-05-20 23:21:38 +00002681 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002682 EOL("CIE Version");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00002683 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002684 EOL("CIE Augmentation");
Chris Lattnerfa823552010-01-22 23:18:42 +00002685 EmitULEB128(1, "CIE Code Alignment Factor");
Chris Lattner23031452010-01-22 22:56:55 +00002686 EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Bill Wendling480ff322009-05-20 23:21:38 +00002687 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002688 EOL("CIE RA Column");
Bill Wendling480ff322009-05-20 23:21:38 +00002689
2690 std::vector<MachineMove> Moves;
2691 RI->getInitialFrameState(Moves);
2692
2693 EmitFrameMoves(NULL, 0, Moves, false);
2694
2695 Asm->EmitAlignment(2, 0, 0, false);
Chris Lattner722714d2010-03-08 22:44:40 +00002696 Asm->OutStreamer.EmitLabel(getTempLabel("debug_frame_common_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00002697}
2698
Devang Patel930143b2009-11-21 02:48:08 +00002699/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling480ff322009-05-20 23:21:38 +00002700/// section.
2701void
Devang Patel930143b2009-11-21 02:48:08 +00002702DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattnere9a75a62009-08-22 21:43:10 +00002703 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002704 return;
2705
2706 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002707 Asm->OutStreamer.SwitchSection(
2708 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002709
Chris Lattner449a9ff2010-03-08 23:02:59 +00002710 EmitDifference(getDWLabel("debug_frame_end", DebugFrameInfo.Number),
2711 getDWLabel("debug_frame_begin", DebugFrameInfo.Number), true);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002712 EOL("Length of Frame Information Entry");
Bill Wendling480ff322009-05-20 23:21:38 +00002713
Chris Lattner722714d2010-03-08 22:44:40 +00002714 Asm->OutStreamer.EmitLabel(getDWLabel("debug_frame_begin",
2715 DebugFrameInfo.Number));
Bill Wendling480ff322009-05-20 23:21:38 +00002716
Chris Lattnerbc9210c2010-03-08 22:23:36 +00002717 EmitSectionOffset(getTempLabel("debug_frame_common"),
2718 getTempLabel("section_debug_frame"), true, false);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002719 EOL("FDE CIE offset");
Bill Wendling480ff322009-05-20 23:21:38 +00002720
Chris Lattnerb779eb62010-03-08 22:47:57 +00002721 EmitReference(getDWLabel("func_begin", DebugFrameInfo.Number));
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002722 EOL("FDE initial location");
Chris Lattnerb779eb62010-03-08 22:47:57 +00002723 EmitDifference(getDWLabel("func_end", DebugFrameInfo.Number),
2724 getDWLabel("func_begin", DebugFrameInfo.Number));
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002725 EOL("FDE address range");
Bill Wendling480ff322009-05-20 23:21:38 +00002726
2727 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2728 false);
2729
2730 Asm->EmitAlignment(2, 0, 0, false);
Chris Lattner722714d2010-03-08 22:44:40 +00002731 Asm->OutStreamer.EmitLabel(getDWLabel("debug_frame_end",
2732 DebugFrameInfo.Number));
Bill Wendling480ff322009-05-20 23:21:38 +00002733}
2734
Devang Patel9ccfb642009-12-09 18:24:21 +00002735/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2736///
2737void DwarfDebug::emitDebugPubNames() {
2738 // Start the dwarf pubnames section.
2739 Asm->OutStreamer.SwitchSection(
2740 Asm->getObjFileLowering().getDwarfPubNamesSection());
2741
Chris Lattner449a9ff2010-03-08 23:02:59 +00002742 EmitDifference(getDWLabel("pubnames_end", ModuleCU->getID()),
2743 getDWLabel("pubnames_begin", ModuleCU->getID()), true);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002744 EOL("Length of Public Names Info");
Bill Wendling480ff322009-05-20 23:21:38 +00002745
Chris Lattner722714d2010-03-08 22:44:40 +00002746 Asm->OutStreamer.EmitLabel(getDWLabel("pubnames_begin", ModuleCU->getID()));
Bill Wendling480ff322009-05-20 23:21:38 +00002747
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002748 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("DWARF Version");
Bill Wendling480ff322009-05-20 23:21:38 +00002749
Chris Lattnerbc9210c2010-03-08 22:23:36 +00002750 EmitSectionOffset(getDWLabel("info_begin", ModuleCU->getID()),
2751 getTempLabel("section_info"),
2752 true, false);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002753 EOL("Offset of Compilation Unit Info");
Bill Wendling480ff322009-05-20 23:21:38 +00002754
Chris Lattner449a9ff2010-03-08 23:02:59 +00002755 EmitDifference(getDWLabel("info_end", ModuleCU->getID()),
2756 getDWLabel("info_begin", ModuleCU->getID()),
Bill Wendling480ff322009-05-20 23:21:38 +00002757 true);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002758 EOL("Compilation Unit Length");
Bill Wendling480ff322009-05-20 23:21:38 +00002759
Devang Patel9ccfb642009-12-09 18:24:21 +00002760 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling480ff322009-05-20 23:21:38 +00002761 for (StringMap<DIE*>::const_iterator
2762 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2763 const char *Name = GI->getKeyData();
2764 DIE * Entity = GI->second;
2765
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002766 Asm->EmitInt32(Entity->getOffset()); EOL("DIE offset");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00002767
2768 if (Asm->VerboseAsm)
2769 Asm->OutStreamer.AddComment("External Name");
2770 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
Bill Wendling480ff322009-05-20 23:21:38 +00002771 }
2772
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002773 Asm->EmitInt32(0); EOL("End Mark");
Chris Lattner722714d2010-03-08 22:44:40 +00002774 Asm->OutStreamer.EmitLabel(getDWLabel("pubnames_end", ModuleCU->getID()));
Bill Wendling480ff322009-05-20 23:21:38 +00002775}
2776
Devang Patel04d2f2d2009-11-24 01:14:22 +00002777void DwarfDebug::emitDebugPubTypes() {
Devang Patelc8654eb2009-11-24 19:18:41 +00002778 // Start the dwarf pubnames section.
2779 Asm->OutStreamer.SwitchSection(
2780 Asm->getObjFileLowering().getDwarfPubTypesSection());
Chris Lattner449a9ff2010-03-08 23:02:59 +00002781 EmitDifference(getDWLabel("pubtypes_end", ModuleCU->getID()),
2782 getDWLabel("pubtypes_begin", ModuleCU->getID()), true);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002783 EOL("Length of Public Types Info");
Devang Patel04d2f2d2009-11-24 01:14:22 +00002784
Chris Lattner722714d2010-03-08 22:44:40 +00002785 Asm->OutStreamer.EmitLabel(getDWLabel("pubtypes_begin", ModuleCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00002786
Devang Patel057c6422010-02-02 03:47:27 +00002787 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DWARF Version");
2788 Asm->EmitInt16(dwarf::DWARF_VERSION);
Devang Patel04d2f2d2009-11-24 01:14:22 +00002789
Chris Lattnerbc9210c2010-03-08 22:23:36 +00002790 EmitSectionOffset(getDWLabel("info_begin", ModuleCU->getID()),
2791 getTempLabel("section_info"), true, false);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002792 EOL("Offset of Compilation ModuleCU Info");
Devang Patel04d2f2d2009-11-24 01:14:22 +00002793
Chris Lattner449a9ff2010-03-08 23:02:59 +00002794 EmitDifference(getDWLabel("info_end", ModuleCU->getID()),
2795 getDWLabel("info_begin", ModuleCU->getID()),
Devang Patel04d2f2d2009-11-24 01:14:22 +00002796 true);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002797 EOL("Compilation ModuleCU Length");
Devang Patel04d2f2d2009-11-24 01:14:22 +00002798
2799 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2800 for (StringMap<DIE*>::const_iterator
2801 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2802 const char *Name = GI->getKeyData();
2803 DIE * Entity = GI->second;
2804
Devang Patel057c6422010-02-02 03:47:27 +00002805 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DIE offset");
2806 Asm->EmitInt32(Entity->getOffset());
Chris Lattnerc3f23b82010-01-23 03:11:46 +00002807
2808 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("External Name");
Devang Patel6d404ad2010-02-02 03:37:03 +00002809 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
Devang Patel04d2f2d2009-11-24 01:14:22 +00002810 }
2811
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002812 Asm->EmitInt32(0); EOL("End Mark");
Chris Lattner722714d2010-03-08 22:44:40 +00002813 Asm->OutStreamer.EmitLabel(getDWLabel("pubtypes_end", ModuleCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00002814}
2815
Devang Patel930143b2009-11-21 02:48:08 +00002816/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00002817///
Devang Patel930143b2009-11-21 02:48:08 +00002818void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00002819 // Check to see if it is worth the effort.
2820 if (!StringPool.empty()) {
2821 // Start the dwarf str section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002822 Asm->OutStreamer.SwitchSection(
2823 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002824
2825 // For each of strings in the string pool.
2826 for (unsigned StringID = 1, N = StringPool.size();
2827 StringID <= N; ++StringID) {
2828 // Emit a label for reference from debug information entries.
Chris Lattner722714d2010-03-08 22:44:40 +00002829 Asm->OutStreamer.EmitLabel(getDWLabel("string", StringID));
Bill Wendling480ff322009-05-20 23:21:38 +00002830
2831 // Emit the string itself.
2832 const std::string &String = StringPool[StringID];
Chris Lattnerc3f23b82010-01-23 03:11:46 +00002833 Asm->OutStreamer.EmitBytes(StringRef(String.c_str(), String.size()+1), 0);
Bill Wendling480ff322009-05-20 23:21:38 +00002834 }
2835
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002836 Asm->O << '\n';
Bill Wendling480ff322009-05-20 23:21:38 +00002837 }
2838}
2839
Devang Patel930143b2009-11-21 02:48:08 +00002840/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00002841///
Devang Patel930143b2009-11-21 02:48:08 +00002842void DwarfDebug::emitDebugLoc() {
Bill Wendling480ff322009-05-20 23:21:38 +00002843 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002844 Asm->OutStreamer.SwitchSection(
2845 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002846}
2847
2848/// EmitDebugARanges - Emit visible names into a debug aranges section.
2849///
2850void DwarfDebug::EmitDebugARanges() {
2851 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002852 Asm->OutStreamer.SwitchSection(
2853 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002854
2855 // FIXME - Mock up
2856#if 0
2857 CompileUnit *Unit = GetBaseCompileUnit();
2858
2859 // Don't include size of length
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002860 Asm->EmitInt32(0x1c); EOL("Length of Address Ranges Info");
Bill Wendling480ff322009-05-20 23:21:38 +00002861
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002862 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("Dwarf Version");
Bill Wendling480ff322009-05-20 23:21:38 +00002863
2864 EmitReference("info_begin", Unit->getID());
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002865 EOL("Offset of Compilation Unit Info");
Bill Wendling480ff322009-05-20 23:21:38 +00002866
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002867 Asm->EmitInt8(TD->getPointerSize()); EOL("Size of Address");
Bill Wendling480ff322009-05-20 23:21:38 +00002868
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002869 Asm->EmitInt8(0); EOL("Size of Segment Descriptor");
Bill Wendling480ff322009-05-20 23:21:38 +00002870
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002871 Asm->EmitInt16(0); EOL("Pad (1)");
2872 Asm->EmitInt16(0); EOL("Pad (2)");
Bill Wendling480ff322009-05-20 23:21:38 +00002873
2874 // Range 1
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002875 EmitReference("text_begin", 0); EOL("Address");
Chris Lattner449a9ff2010-03-08 23:02:59 +00002876 EmitDifference(getTempLabel("text_end"), getTempLabel("text_begin"),
2877 true); EOL("Length");
Bill Wendling480ff322009-05-20 23:21:38 +00002878
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002879 Asm->EmitInt32(0); EOL("EOM (1)");
2880 Asm->EmitInt32(0); EOL("EOM (2)");
Bill Wendling480ff322009-05-20 23:21:38 +00002881#endif
Bill Wendling480ff322009-05-20 23:21:38 +00002882}
2883
Devang Patel930143b2009-11-21 02:48:08 +00002884/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00002885///
Devang Patel930143b2009-11-21 02:48:08 +00002886void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002887 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002888 Asm->OutStreamer.SwitchSection(
2889 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002890}
2891
Devang Patel930143b2009-11-21 02:48:08 +00002892/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00002893///
Devang Patel930143b2009-11-21 02:48:08 +00002894void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002895 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00002896 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002897 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002898 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002899 }
2900}
2901
Devang Patel930143b2009-11-21 02:48:08 +00002902/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00002903/// Section Header:
2904/// 1. length of section
2905/// 2. Dwarf version number
2906/// 3. address size.
2907///
2908/// Entries (one "entry" for each function that was inlined):
2909///
2910/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2911/// otherwise offset into __debug_str for regular function name.
2912/// 2. offset into __debug_str section for regular function name.
2913/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2914/// instances for the function.
2915///
2916/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2917/// inlined instance; the die_offset points to the inlined_subroutine die in the
2918/// __debug_info section, and the low_pc is the starting address for the
2919/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00002920void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnere9a75a62009-08-22 21:43:10 +00002921 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002922 return;
2923
Devang Patel40d78412009-06-29 20:45:18 +00002924 if (!ModuleCU)
Bill Wendling480ff322009-05-20 23:21:38 +00002925 return;
2926
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002927 Asm->OutStreamer.SwitchSection(
2928 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002929
Chris Lattner449a9ff2010-03-08 23:02:59 +00002930 EmitDifference(getDWLabel("debug_inlined_end", 1),
2931 getDWLabel("debug_inlined_begin", 1), true);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002932 EOL("Length of Debug Inlined Information Entry");
Bill Wendling480ff322009-05-20 23:21:38 +00002933
Chris Lattner722714d2010-03-08 22:44:40 +00002934 Asm->OutStreamer.EmitLabel(getDWLabel("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002935
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002936 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("Dwarf Version");
2937 Asm->EmitInt8(TD->getPointerSize()); EOL("Address Size (in bytes)");
Bill Wendling480ff322009-05-20 23:21:38 +00002938
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002939 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2940 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00002941
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002942 MDNode *Node = *I;
Devang Patel018b29b2010-01-19 06:19:05 +00002943 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00002944 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002945 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00002946 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00002947 StringRef LName = SP.getLinkageName();
2948 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00002949
Chris Lattnerc3f23b82010-01-23 03:11:46 +00002950 if (LName.empty()) {
2951 Asm->OutStreamer.EmitBytes(Name, 0);
2952 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
2953 } else
Chris Lattnerbc9210c2010-03-08 22:23:36 +00002954 EmitSectionOffset(getDWLabel("string",
2955 StringPool.idFor(getRealLinkageName(LName))),
2956 getTempLabel("section_str"), true);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002957
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002958 EOL("MIPS linkage name");
Chris Lattnerbc9210c2010-03-08 22:23:36 +00002959 EmitSectionOffset(getDWLabel("string", StringPool.idFor(Name)),
2960 getTempLabel("section_str"), false, true);
Chris Lattner6f2d99d2010-01-22 23:47:11 +00002961 EOL("Function name");
Chris Lattnerfa823552010-01-22 23:18:42 +00002962 EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00002963
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002964 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00002965 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner085b6522010-03-09 00:31:02 +00002966 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DIE offset");
2967 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002968
Chris Lattner53d6d1e2010-03-09 00:26:09 +00002969 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerb14490d2010-03-09 00:39:24 +00002970 Asm->OutStreamer.EmitSymbolValue(LI->first, TD->getPointerSize(), 0);
Bill Wendling480ff322009-05-20 23:21:38 +00002971 }
2972 }
2973
Chris Lattner722714d2010-03-08 22:44:40 +00002974 Asm->OutStreamer.EmitLabel(getDWLabel("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002975}